:root {
  color-scheme: light dark;
  --bg: #edf1ef;
  --surface: #ffffff;
  --surface-2: #f2f6f4;
  --text: #13201d;
  --muted: #586763;
  --border: #d8e0dd;
  --accent: #0f6e62;
  --on-accent: #ffffff;
  --own: #d3ede5;
  --danger: #b3261e;
  --on-danger: #ffffff;
  --warn-bg: #fff3d1;
  --warn-text: #644800;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c1110;
    --surface: #141b19;
    --surface-2: #1d2724;
    --text: #e5efec;
    --muted: #94a5a0;
    --border: #2a3531;
    --accent: #52d0bc;
    --on-accent: #03241f;
    --own: #154138;
    --danger: #ff8f85;
    --on-danger: #35060a;
    --warn-bg: #372d0f;
    --warn-text: #ffd97a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 0.95rem; margin: 12px 0 6px; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.icon {
  width: 22px;
  height: 22px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--accent); }

/* ---------- Formulare und Knöpfe ---------- */

.form { display: flex; flex-direction: column; gap: 14px; margin-top: 12px; }
.field, .field label { display: flex; flex-direction: column; gap: 5px; font-size: 0.9rem; font-weight: 600; }
.field small { font-weight: 400; color: var(--muted); }

input:not([type="checkbox"]), textarea, select {
  width: 100%;
  min-width: 0;
  font: inherit;
  font-weight: 400;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}

input[type="checkbox"] { width: 20px; height: 20px; flex: none; accent-color: var(--accent); }

.button {
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
}
.button.primary { background: var(--accent); border-color: transparent; color: var(--on-accent); }
.button.danger { color: var(--danger); }
.dialog .actions .button.danger:last-child { background: var(--danger); border-color: transparent; color: var(--on-danger); }
.button.small { font-size: 0.85rem; padding: 7px 12px; }
.button:disabled, .send:disabled { opacity: 0.5; cursor: default; }

.link {
  display: block;
  margin: 16px 0 0;
  padding: 0;
  font: inherit;
  font-weight: 600;
  color: var(--accent);
  background: none;
  border: 0;
  cursor: pointer;
  text-decoration: underline;
}

.icon-button {
  position: relative;
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  color: var(--text);
  background: transparent;
  border: 0;
  border-radius: 10px;
  cursor: pointer;
}
.icon-button:hover { background: var(--surface-2); }

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--on-accent);
  background: var(--accent);
  border-radius: 999px;
}
.icon-button .badge { position: absolute; top: 0; right: -2px; }

.form-error { margin: 0; color: var(--danger); font-weight: 600; }
.warning, .notice {
  padding: 10px 12px;
  font-size: 0.88rem;
  color: var(--warn-text);
  background: var(--warn-bg);
  border-radius: 10px;
}

/* ---------- Anmeldung ---------- */

.auth { display: flex; align-items: center; justify-content: center; min-height: 100dvh; padding: 24px 14px; }
.card {
  width: 100%;
  max-width: 420px;
  padding: 26px 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
}
.card .brand { margin-bottom: 16px; }
.card > .button { margin-top: 8px; }

.code {
  display: grid;
  grid-template-columns: repeat(4, auto);
  justify-content: center;
  gap: 6px 14px;
  margin: 10px 0 14px;
  padding: 14px 8px;
  font: 700 1.15rem/1.3 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: 12px;
}

/* ---------- Aufbau ---------- */

.layout { display: grid; grid-template-columns: minmax(260px, 340px) 1fr; height: 100dvh; }
.sidebar, .pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); }

.bar {
  display: flex;
  flex: none;
  align-items: center;
  gap: 8px;
  min-height: 62px;
  padding: calc(8px + env(safe-area-inset-top)) 10px 8px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.bar .brand { flex: 1; }
.bar-actions { display: flex; gap: 2px; }
.bar-title { flex: 1; min-width: 0; }
.bar-title > * { overflow: hidden; margin: 0; white-space: nowrap; text-overflow: ellipsis; }
.back { display: none; margin-left: -8px; }

.banner { margin: 0; padding: 8px 14px; font-size: 0.85rem; color: var(--warn-text); background: var(--warn-bg); }
.banner.danger { color: var(--on-danger); background: var(--danger); }

/* ---------- Chatliste ---------- */

.chat-list { flex: 1; overflow-y: auto; margin: 0; padding: 6px; list-style: none; }
.list-empty { padding: 24px 14px; color: var(--muted); text-align: center; }
.chat-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px;
  font: inherit;
  color: inherit;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 12px;
  cursor: pointer;
}
.chat-row:hover, .chat-row.selected { background: var(--surface-2); }
.avatar {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  font-weight: 700;
  border-radius: 50%;
}
.tone-0 { color: #07463d; background: #bfe5da; }
.tone-1 { color: #1c3a66; background: #cbdcf6; }
.tone-2 { color: #5b2a63; background: #ead2ee; }
.tone-3 { color: #6a3608; background: #f6dcc0; }
.tone-4 { color: #3f4a0c; background: #dde6b5; }
.chat-row-text { display: flex; flex: 1; flex-direction: column; min-width: 0; }
.chat-row-title, .chat-row-preview { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-row-title { font-weight: 600; }
.chat-row-preview { font-size: 0.88rem; color: var(--muted); }
.chat-row-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: 0.75rem; color: var(--muted); }

/* ---------- Unterhaltung ---------- */

.empty { display: flex; flex: 1; flex-direction: column; align-items: center; justify-content: center; padding: 24px; text-align: center; }
.empty .icon { width: 48px; height: 48px; color: var(--accent); }
.empty p { margin: 8px 0 0; }

.messages { display: flex; flex: 1; flex-direction: column; gap: 6px; overflow-y: auto; padding: 14px; }
.messages > * { flex: none; }
.messages > :first-child { margin-top: auto; }
.messages .older { align-self: center; }
.day { align-self: center; margin: 10px 0 4px; padding: 2px 12px; font-size: 0.75rem; color: var(--muted); background: var(--surface); border-radius: 999px; }
.day.key-change { max-width: 90%; padding: 7px 14px; font-family: inherit; line-height: 1.4; color: var(--warn-text); background: var(--warn-bg); border: 0; border-radius: 14px; cursor: pointer; }
.message {
  align-self: flex-start;
  max-width: min(78%, 620px);
  padding: 8px 12px 5px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px 16px 16px 5px;
}
.message.own { align-self: flex-end; background: var(--own); border-color: transparent; border-radius: 16px 16px 5px 16px; }
.message.invalid { color: var(--danger); border: 1px dashed var(--danger); }
.message .sender { display: block; font-size: 0.8rem; font-weight: 700; color: var(--accent); }
.message .text { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.message time { display: block; margin-top: 1px; font-size: 0.7rem; color: var(--muted); text-align: right; }

.composer {
  display: flex;
  flex: none;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.composer textarea { flex: 1; max-height: 140px; resize: none; border-radius: 22px; }
.send {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 46px;
  height: 46px;
  color: var(--on-accent);
  background: var(--accent);
  border: 0;
  border-radius: 50%;
  cursor: pointer;
}
.composer-notice { margin: 0; padding: 14px 14px calc(14px + env(safe-area-inset-bottom)); text-align: center; color: var(--warn-text); background: var(--warn-bg); }

/* ---------- Dialoge ---------- */

.dialog {
  width: min(460px, calc(100vw - 20px));
  max-height: calc(100dvh - 24px);
  padding: 0;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
}
.dialog[open] { display: flex; flex-direction: column; }
.dialog::backdrop { background: rgb(0 0 0 / 0.5); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 10px 0 20px; }
.dialog-head h2 { overflow-wrap: anywhere; }
.dialog-body { overflow-y: auto; padding: 2px 20px 20px; }
.actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.check { display: flex; align-items: center; gap: 10px; padding: 7px 0; cursor: pointer; }
.person { display: flex; flex-direction: column; min-width: 0; overflow-wrap: anywhere; }
.person small { color: var(--muted); font-weight: 400; }
.people, .members { margin: 0; padding: 0; list-style: none; }
fieldset.people { border: 0; }
fieldset.people legend { padding: 0; font-size: 0.9rem; font-weight: 600; }
.person-button {
  width: 100%;
  padding: 9px 10px;
  font: inherit;
  color: inherit;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 10px;
  cursor: pointer;
}
.person-button:hover { background: var(--surface-2); }
.members li { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.add-member, .copy-row { display: flex; gap: 8px; margin-top: 12px; }
.copy-row { margin-top: 0; }
details { margin-top: 14px; }
summary { padding: 6px 0; font-weight: 600; color: var(--accent); cursor: pointer; }
details .field { margin-top: 10px; }

/* ---------- Verwaltung ---------- */

.admin { position: fixed; inset: 0; z-index: 10; display: flex; flex-direction: column; background: var(--bg); }
.admin-body { width: 100%; max-width: 720px; margin: 0 auto; padding: 14px 14px calc(24px + env(safe-area-inset-bottom)); }
.admin { overflow-y: auto; }
.panel { margin-bottom: 14px; padding: 14px 16px 16px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; }
.panel h3 { margin-top: 0; }
.user { display: flex; flex-direction: column; gap: 10px; padding: 14px 0; border-top: 1px solid var(--border); }
.user-info { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.user-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.user-actions:empty { display: none; }
.status { flex: none; padding: 3px 10px; font-size: 0.75rem; font-weight: 700; background: var(--surface-2); border-radius: 999px; }
.status.pending { color: var(--warn-text); background: var(--warn-bg); }
.status.active { background: var(--own); }
.status.blocked { color: var(--on-danger); background: var(--danger); }

/* ---------- Hinweise ---------- */

.toasts {
  position: fixed;
  inset: calc(70px + env(safe-area-inset-top)) auto auto 50%;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(420px, calc(100vw - 28px));
  margin: 0;
  padding: 0;
  overflow: visible;
  background: transparent;
  border: 0;
  transform: translateX(-50%);
  pointer-events: none;
}
.toast { padding: 11px 14px; color: var(--bg); background: var(--text); border-radius: 12px; box-shadow: 0 6px 24px rgb(0 0 0 / 0.22); }
.toast.error { color: var(--on-danger); background: var(--danger); }

/* ---------- Handy ---------- */

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; }
  .layout .pane, .layout.open .sidebar { display: none; }
  .layout.open .pane { display: flex; }
  .back { display: inline-flex; }
  .message { max-width: 86%; }
}
