:root {
  --bg: #0f1115;
  --bg-2: #151922;
  --bg-3: #1c2230;
  --line: #262d3b;
  --text: #e6e9ef;
  --muted: #9099ab;
  --accent: #5b8cff;
  --accent-soft: #23304d;
  --danger: #ff6b6b;
  --ok: #3ecf8e;
  --radius: 12px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f9;
    --bg-2: #ffffff;
    --bg-3: #eceff4;
    --line: #dde1e8;
    --text: #131722;
    --muted: #667085;
    --accent: #2f6bff;
    --accent-soft: #e2ebff;
    --shadow: 0 10px 30px rgba(16, 24, 40, 0.08);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior-y: none;
}

/* Als installierte App gibt es keine Browserleiste – dafuer Notch und
   Home-Indikator. dvh statt vh, weil die Adressleiste am Handy ein- und
   ausfaehrt. */
@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea, select {
  font: inherit;
  color: inherit;
}

/* ---------------- Formular-Seiten (Login / Setup / Beitritt) ------- */

.centered {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}

.card {
  width: min(420px, 100%);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 28px;
  box-shadow: var(--shadow);
}

.card h1 {
  margin: 0 0 6px;
  font-size: 22px;
}

.card .sub {
  margin: 0 0 22px;
  color: var(--muted);
  font-size: 14px;
}

.logo {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: var(--accent);
  display: grid;
  place-items: center;
  margin-bottom: 16px;
  color: #fff;
  font-weight: 700;
}

label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin: 14px 0 6px;
}

input[type="text"], input[type="password"], input[type="number"], textarea, select {
  width: 100%;
  padding: 11px 12px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
}

input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg-3);
  cursor: pointer;
  transition: filter .15s ease, background .15s ease;
}

.btn:hover:not(:disabled) { filter: brightness(1.12); }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; margin-top: 20px; }
.btn.small { padding: 6px 10px; font-size: 13px; border-radius: 8px; }

.msg {
  margin-top: 16px;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 14px;
  display: none;
}

.msg.show { display: block; }
.msg.error { background: rgba(255, 107, 107, .12); color: var(--danger); }
.msg.ok { background: rgba(62, 207, 142, .12); color: var(--ok); }
.msg.info { background: var(--accent-soft); color: var(--text); }

.hint { font-size: 13px; color: var(--muted); margin-top: 16px; }
.hidden { display: none !important; }

.code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 15px;
  letter-spacing: .04em;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  overflow-wrap: anywhere;
}

/* ---------------- App-Layout --------------------------------------- */

.app {
  display: grid;
  grid-template-columns: 300px 1fr;
  height: 100%;
}

.sidebar {
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.side-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 10px;
}

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 13px;
  flex: none;
  text-transform: uppercase;
}

.avatar.online { box-shadow: 0 0 0 2px var(--bg-2), 0 0 0 4px var(--ok); }

.who { min-width: 0; flex: 1; }
.who strong { display: block; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who span { font-size: 12px; color: var(--muted); }

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 16px;
  flex: none;
}

.icon-btn:hover { background: var(--bg-3); color: var(--text); }

.side-actions {
  display: flex;
  gap: 8px;
  padding: 12px 12px 8px;
}

.side-actions .btn { flex: 1; font-size: 13px; padding: 8px 10px; }

.search { padding: 0 12px 8px; }
.search input { padding: 8px 10px; border-radius: 8px; }

.list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 12px;
  min-height: 0;
}

.list-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  padding: 14px 8px 6px;
}

.room {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  background: transparent;
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
  color: inherit;
}

.room:hover { background: var(--bg-3); }
.room.active { background: var(--accent-soft); }
.room-body { min-width: 0; flex: 1; }
.room-name { display: block; font-size: 14px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-last { display: block; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: grid;
  place-items: center;
  flex: none;
}

.empty { color: var(--muted); font-size: 13px; padding: 10px; }

/* ---------------- Chat ---------------------------------------------- */

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.chat-head {
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-2);
}

.chat-head h2 { margin: 0; font-size: 16px; }
.chat-head .meta { font-size: 12px; color: var(--muted); }
.spacer { flex: 1; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 0;
}

.day {
  align-self: center;
  font-size: 12px;
  color: var(--muted);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 3px 12px;
  margin: 14px 0 8px;
}

.bubble-row {
  display: flex;
  gap: 10px;
  padding: 2px 0;
  align-items: flex-end;
}

.bubble-row.mine { flex-direction: row-reverse; }
.bubble-row .avatar { width: 28px; height: 28px; font-size: 11px; }
.bubble-row.stacked .avatar { visibility: hidden; }

.bubble {
  max-width: min(620px, 74%);
  background: var(--bg-2);
  border: 1px solid var(--line);
  padding: 8px 12px;
  border-radius: 14px;
  position: relative;
  display: flex;
  flex-direction: column;
}

.bubble-row.mine .bubble {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.bubble .author {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 2px;
}

.bubble-row.mine .bubble .author { display: none; }
.bubble-row.stacked .bubble .author { display: none; }

.bubble .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble .text a { color: inherit; }
.bubble.deleted .text { font-style: italic; opacity: .6; }

.bubble .time {
  font-size: 11px;
  color: var(--muted);
  align-self: flex-end;
  margin-top: 2px;
  line-height: 1.2;
}

.bubble-row.mine .time { color: rgba(255, 255, 255, .75); }
.bubble.pending { opacity: .6; }

.del {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--bg-3);
  color: var(--muted);
  cursor: pointer;
  display: none;
  place-items: center;
  font-size: 12px;
  line-height: 1;
}

.bubble:hover .del { display: grid; }

.typing { height: 20px; padding: 0 18px 4px; font-size: 12px; color: var(--muted); }

.composer {
  border-top: 1px solid var(--line);
  padding: 12px 18px 16px;
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: var(--bg-2);
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 160px;
  min-height: 44px;
  border-radius: 12px;
  padding: 11px 14px;
}

.composer .btn.primary { height: 44px; padding: 0 18px; }

.placeholder {
  flex: 1;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-align: center;
  padding: 40px;
}

.conn {
  font-size: 12px;
  padding: 6px 18px;
  background: rgba(255, 107, 107, .14);
  color: var(--danger);
  text-align: center;
}

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

dialog {
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--text);
  border-radius: 16px;
  padding: 0;
  width: min(520px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}

dialog::backdrop { background: rgba(0, 0, 0, .55); }

.dialog-head {
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 10px;
}

.dialog-head h3 { margin: 0; font-size: 16px; flex: 1; }
.dialog-body { padding: 16px 20px; max-height: 60vh; overflow-y: auto; }
.dialog-foot { padding: 14px 20px; border-top: 1px solid var(--line); display: flex; gap: 10px; justify-content: flex-end; }

.pick {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: 10px;
  cursor: pointer;
}

.pick:hover { background: var(--bg-3); }
.pick input { width: auto; }

.row-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-bottom: 1px solid var(--line);
}

.row-item:last-child { border-bottom: 0; }
.row-item .grow { flex: 1; min-width: 0; }
.row-item small { color: var(--muted); display: block; overflow-wrap: anywhere; }

.tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 20px;
  border: 1px solid var(--line);
  color: var(--muted);
}

.tag.open { color: var(--ok); border-color: rgba(62, 207, 142, .4); }
.tag.redeemed { color: var(--accent); border-color: var(--accent); }
.tag.expired, .tag.revoked { color: var(--danger); border-color: rgba(255, 107, 107, .4); }

.link-out {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.link-out input { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }

/* ---------------- Mobil --------------------------------------------- */

.back-btn { display: none; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; }
  .app.show-chat .sidebar { display: none; }
  .app:not(.show-chat) .chat { display: none; }
  .back-btn { display: grid; }
  .bubble { max-width: 86%; }

  /* Randbereiche des Geraets (Notch, Home-Indikator) freihalten */
  .side-head, .chat-head { padding-top: calc(12px + env(safe-area-inset-top)); }
  .sidebar { padding-left: env(safe-area-inset-left); }
  .composer {
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    padding-left: calc(14px + env(safe-area-inset-left));
    padding-right: calc(14px + env(safe-area-inset-right));
  }
  .list { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .messages { padding-left: 12px; padding-right: 12px; }

  /* Groessere Ziele fuer Finger, und 16px verhindert das Hineinzoomen von iOS */
  .icon-btn { width: 40px; height: 40px; }
  .room { padding: 11px 10px; }
  .btn { padding: 12px 16px; }
  input[type="text"], input[type="password"], input[type="number"], textarea, select {
    font-size: 16px;
  }
  .composer textarea { min-height: 46px; }
  .del { display: grid; }
  dialog { width: 100%; max-height: 92dvh; border-radius: 16px 16px 0 0; margin-bottom: 0; }
  .dialog-body { max-height: 68dvh; }
}
