:root {
  --accent: #0f766e;
  --accent-hover: #115e59;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #16181d;
  --muted: #6b7280;
  --border: #e3e5e9;
  --mine: #d7f3ee;
  --theirs: #ffffff;
  --danger: #c0392b;
  --radius: 14px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --accent: #2dd4bf;
    --accent-hover: #5eead4;
    --bg: #0e1013;
    --surface: #171a1f;
    --surface-2: #20242b;
    --text: #e8eaed;
    --muted: #9aa0a9;
    --border: #2a2f37;
    --mine: #134e48;
    --theirs: #20242b;
    --danger: #ef6b5b;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--border); background: var(--surface-2); border-radius: 10px; padding: 10px 14px; }
button:disabled { opacity: .55; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
@media (prefers-color-scheme: dark) { button.primary { color: #04201d; } }
button.primary:hover:not(:disabled) { background: var(--accent-hover); }
button.danger { color: var(--danger); }
button.link, button.back { background: none; border: none; padding: 6px 0; color: var(--accent); }
button.back { align-self: flex-start; }
button.icon { background: none; border: none; font-size: 20px; padding: 6px 10px; line-height: 1; }
input, textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.4em; margin: 8px 0 0; text-align: center; }

/* Onboarding */
.onboarding { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 28px 24px; }
.brand { font-weight: 700; font-size: 18px; margin-bottom: 18px; display: flex; gap: 8px; align-items: center; }
.step { display: flex; flex-direction: column; gap: 12px; }
.step h1 { font-size: 22px; margin: 0; }
.step p { margin: 0; }
.step label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 14px; }
.step label small { font-weight: 400; }
.digits { text-align: center; font-size: 26px; letter-spacing: .5em; padding-left: calc(14px + .5em); font-variant-numeric: tabular-nums; }
.digits::placeholder { letter-spacing: .3em; }
#pin-confirm { font-size: 18px; letter-spacing: .4em; }
#pin-confirm::placeholder { letter-spacing: normal; }
.dev-note { background: #fef3c7; color: #78350f; border-radius: 10px; padding: 8px 12px; font-size: 14px; }
.prefixed { display: flex; align-items: center; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); padding-left: 14px; }
.prefixed:focus-within { border-color: var(--accent); }
.prefixed span { color: var(--muted); font-weight: 400; }
.prefixed input { border: none; padding-left: 2px; background: transparent; }
small.ok { color: var(--accent); }
small.bad { color: var(--danger); }

/* App layout */
.app { height: 100dvh; display: grid; grid-template-columns: 340px 1fr; }
.sidebar { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.bar { display: flex; align-items: center; gap: 10px; padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top)); border-bottom: 1px solid var(--border); background: var(--surface); min-height: 60px; }
.bar h2 { flex: 1; margin: 0; font-size: 18px; }
.bar .title { display: flex; flex-direction: column; min-width: 0; }
.bar .title strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back-mobile { display: none; font-size: 28px !important; }
.conn { background: #fef3c7; color: #78350f; text-align: center; font-size: 13px; padding: 4px; }

.avatar, .avatar-btn {
  width: 40px; height: 40px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 15px;
  background: var(--avatar, var(--accent)); position: relative; padding: 0; border: none;
}
.avatar.large { width: 72px; height: 72px; font-size: 28px; }
.avatar.online::after {
  content: ""; position: absolute; right: 0; bottom: 0; width: 11px; height: 11px;
  border-radius: 50%; background: #22c55e; border: 2px solid var(--surface);
}

.conversations, .results { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conversations li, .results li { display: flex; gap: 12px; align-items: center; padding: 10px 14px; cursor: pointer; }
.conversations li:hover, .results li:hover { background: var(--surface-2); }
.conversations li.active { background: var(--surface-2); }
.conversations .meta { flex: 1; min-width: 0; }
.conversations .row { display: flex; justify-content: space-between; gap: 8px; }
.conversations .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conversations .time { color: var(--muted); font-size: 12px; flex: none; }
.conversations .preview { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conversations li.unread .preview { color: var(--text); font-weight: 500; }
.badge { background: var(--accent); color: #fff; border-radius: 999px; font-size: 12px; font-weight: 700; padding: 1px 7px; flex: none; }
@media (prefers-color-scheme: dark) { .badge { color: #04201d; } }
.empty { padding: 32px 20px; text-align: center; color: var(--muted); }
.empty.center { margin: auto; }

/* Messages */
.chat-view { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.messages { flex: 1; overflow-y: auto; padding: 16px 12px; display: flex; flex-direction: column; gap: 2px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px; margin: 12px 0 8px; }
.msg { max-width: min(75%, 560px); padding: 7px 10px 5px; border-radius: var(--radius); background: var(--theirs); border: 1px solid var(--border); align-self: flex-start; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.mine { align-self: flex-end; background: var(--mine); border-color: transparent; }
.msg.grouped { margin-top: 0; }
.msg:not(.grouped) { margin-top: 6px; }
.msg .stamp { float: right; margin: 6px 0 0 10px; font-size: 11px; color: var(--muted); }
.msg .ticks.read { color: var(--accent); }
.msg.pending { opacity: .6; }
.msg.failed { border-color: var(--danger); }
.load-more { align-self: center; margin-bottom: 8px; font-size: 13px; }

.composer { display: flex; gap: 8px; padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); background: var(--surface); align-items: flex-end; }
.composer textarea { resize: none; max-height: 140px; border-radius: 20px; padding: 10px 16px; }
.send { width: 44px; height: 44px; border-radius: 50%; background: var(--accent); color: #fff; border: none; flex: none; padding: 0; font-size: 18px; }

/* Dialogs */
dialog { border: 1px solid var(--border); border-radius: 18px; padding: 0; width: min(420px, calc(100vw - 32px)); background: var(--surface); color: var(--text); }
dialog::backdrop { background: rgb(0 0 0 / .4); }
.dialog-body { display: flex; flex-direction: column; gap: 12px; padding: 16px; max-height: 70vh; }
.dialog-body header { display: flex; justify-content: space-between; align-items: center; }
.dialog-body h3 { margin: 0; }
.results { max-height: 50vh; margin: 0 -16px; }
.results .hint { color: var(--muted); cursor: default; justify-content: center; }
.results .hint:hover { background: none; }
.profile-summary { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 0 12px; }

/* Phone layout: one pane at a time */
@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .chat { display: none; }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .chat { display: flex; }
  .back-mobile { display: block; }
  .msg { max-width: 85%; }
}
