:root {
  --bg: #f4f1ea;
  --panel: #fffdf8;
  --panel-2: #f8f5ee;
  --line: #e4ded1;
  --text: #1d2420;
  --muted: #6b726d;
  --accent: #0f5f4c;
  --accent-2: #13795f;
  --accent-soft: #dcefe7;
  --on-accent: #ffffff;
  --out: #dff3e8;
  --in: #ffffff;
  --wall: #ece6da;
  --wall-dot: rgba(15, 95, 76, .06);
  --danger: #b3261e;
  --danger-soft: #fbe3e1;
  --warn: #8a5a00;
  --warn-soft: #fdf0d5;
  --tick: #1f8fdb;
  --shadow: 0 1px 1px rgba(0,0,0,.06), 0 1px 3px rgba(0,0,0,.06);
  --radius: 14px;
  --font: ui-sans-serif, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Devanagari", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1411; --panel: #111c18; --panel-2: #16231e; --line: #22332c; --text: #e8eee9; --muted: #93a39b;
    --accent: #3fb68f; --accent-2: #5cc9a2; --accent-soft: #173a2f; --on-accent: #06221a;
    --out: #164034; --in: #18241f; --wall: #0e1916; --wall-dot: rgba(63, 182, 143, .05);
    --danger: #ff8a80; --danger-soft: #3a1714; --warn: #ffc860; --warn-soft: #33270e; --tick: #53bdeb;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1411; --panel: #111c18; --panel-2: #16231e; --line: #22332c; --text: #e8eee9; --muted: #93a39b;
  --accent: #3fb68f; --accent-2: #5cc9a2; --accent-soft: #173a2f; --on-accent: #06221a;
  --out: #164034; --in: #18241f; --wall: #0e1916; --wall-dot: rgba(63, 182, 143, .05);
  --danger: #ff8a80; --danger-soft: #3a1714; --warn: #ffc860; --warn-soft: #33270e; --tick: #53bdeb;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent-2); }
.hidden { display: none !important; }
svg.i { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; flex: none; }

#app { height: 100dvh; display: flex; }

/* ── sign-in ───────────────────────────────────────────────────────────── */
.gate {
  margin: auto; width: 100%; max-width: 400px; padding: 32px 20px calc(32px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 18px; text-align: center;
}
.gate .mark { width: 72px; height: 72px; margin: 0 auto; border-radius: 20px; background: var(--accent); display: grid; place-items: center; color: var(--on-accent); box-shadow: 0 10px 30px -10px var(--accent); }
.gate .mark svg { width: 38px; height: 38px; }
.gate h1 { font-size: 24px; margin: 4px 0 0; letter-spacing: -.01em; }
.gate p { margin: 0; color: var(--muted); }
.gate form { display: flex; flex-direction: column; gap: 10px; text-align: left; }
.gate .alt { font-size: 14px; }
.gate .alt button { background: none; border: 0; color: var(--accent-2); padding: 6px; text-decoration: underline; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 600; }
.input, textarea.input, select.input {
  width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 12px; padding: 12px 14px; outline: none;
  font-size: 16px; /* iOS will not zoom at 16px */
}
.input: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; min-height: 48px; padding: 0 18px;
  border-radius: 12px; border: 0; background: var(--accent); color: var(--on-accent); font-weight: 650; font-size: 16px;
}
.btn:disabled { opacity: .55; }
.btn.ghost { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: var(--danger-soft); color: var(--danger); }
.btn.small { min-height: 36px; font-size: 14px; padding: 0 12px; border-radius: 10px; }
.error { color: var(--danger); font-size: 14px; min-height: 1em; }

/* ── the two panes ─────────────────────────────────────────────────────── */
.shell { display: flex; width: 100%; height: 100%; }
.list-pane { width: 100%; display: flex; flex-direction: column; background: var(--panel); min-width: 0; }
.chat-pane { width: 100%; display: none; flex-direction: column; min-width: 0; background: var(--wall); }
.shell.in-chat .list-pane { display: none; }
.shell.in-chat .chat-pane { display: flex; }
@media (min-width: 900px) {
  .list-pane { width: 380px; flex: none; border-right: 1px solid var(--line); display: flex !important; }
  .chat-pane { display: flex !important; flex: 1; }
  .chat-back { display: none !important; }
}

.bar {
  display: flex; align-items: center; gap: 6px; padding: calc(8px + env(safe-area-inset-top)) 8px 8px 16px;
  background: var(--accent); color: var(--on-accent); min-height: 56px;
}
.bar h1 { font-size: 20px; margin: 0; flex: 1; letter-spacing: -.01em; }
.icon-btn { width: 42px; height: 42px; border-radius: 50%; border: 0; background: transparent; display: grid; place-items: center; color: inherit; flex: none; }
.icon-btn:active { background: rgba(255,255,255,.14); }

.search { padding: 10px 12px 6px; background: var(--panel); }
.search .input { padding: 10px 14px 10px 40px; border-radius: 999px; background: var(--panel-2); border-color: transparent; }
.search { position: relative; }
.search svg { position: absolute; left: 25px; top: 21px; width: 18px; height: 18px; color: var(--muted); }
.chips { display: flex; gap: 8px; padding: 6px 12px 10px; overflow-x: auto; }
.chip { border: 0; background: var(--panel-2); color: var(--muted); border-radius: 999px; padding: 6px 14px; font-size: 14px; font-weight: 600; white-space: nowrap; }
.chip[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }

.chats { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: calc(80px + env(safe-area-inset-bottom)); }
.chat-row { display: flex; gap: 12px; align-items: center; width: 100%; border: 0; background: none; text-align: left; padding: 10px 14px; }
.chat-row:active, .chat-row[aria-current="true"] { background: var(--panel-2); }
.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex: none; display: grid; place-items: center; font-weight: 700; font-size: 17px;
  background: var(--accent-soft); color: var(--accent);
}
.chat-row .mid { flex: 1; min-width: 0; border-bottom: 1px solid var(--line); padding-bottom: 10px; margin-bottom: -10px; }
.chat-row .top { display: flex; align-items: baseline; gap: 8px; }
.chat-row .name { font-weight: 650; font-size: 16px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row .when { font-size: 12px; color: var(--muted); flex: none; }
.chat-row .when.hot { color: var(--accent-2); font-weight: 700; }
.chat-row .bottom { display: flex; align-items: center; gap: 8px; margin-top: 2px; }
.chat-row .preview { flex: 1; min-width: 0; color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { min-width: 21px; height: 21px; padding: 0 6px; border-radius: 999px; background: var(--accent-2); color: var(--on-accent); font-size: 12px; font-weight: 700; display: grid; place-items: center; }
.empty { text-align: center; color: var(--muted); padding: 48px 24px; }

.fab {
  position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); width: 58px; height: 58px; border-radius: 18px; border: 0;
  background: var(--accent-2); color: var(--on-accent); display: grid; place-items: center; box-shadow: 0 8px 20px -6px rgba(0,0,0,.35); z-index: 5;
}
@media (min-width: 900px) { .fab { left: 304px; right: auto; } }
.shell.in-chat .fab { display: none; }
@media (min-width: 900px) { .shell.in-chat .fab { display: grid; } }

/* ── a conversation ────────────────────────────────────────────────────── */
.chat-head { padding-left: 4px; gap: 4px; }
.chat-head .who { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; background: none; border: 0; color: inherit; text-align: left; padding: 0; }
.chat-head .avatar { width: 40px; height: 40px; font-size: 15px; background: rgba(255,255,255,.18); color: var(--on-accent); }
.chat-head .t { min-width: 0; }
.chat-head .t b { display: block; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .t span { display: block; font-size: 12.5px; opacity: .85; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.thread {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 12px 10px 16px;
  background-color: var(--wall);
  background-image: radial-gradient(var(--wall-dot) 1.2px, transparent 1.3px);
  background-size: 18px 18px;
  display: flex; flex-direction: column; gap: 3px;
}
.day { align-self: center; margin: 10px 0 6px; background: var(--panel); color: var(--muted); font-size: 12px; font-weight: 600; padding: 5px 12px; border-radius: 8px; box-shadow: var(--shadow); }
.more { align-self: center; margin-bottom: 8px; }

.msg { max-width: min(82%, 520px); padding: 6px 9px 5px; border-radius: 12px; box-shadow: var(--shadow); position: relative; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.in { align-self: flex-start; background: var(--in); border-top-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--out); border-top-right-radius: 4px; }
.msg + .msg.in, .msg + .msg.out { margin-top: 1px; }
.msg.first { margin-top: 8px; }
.msg .body { white-space: pre-wrap; font-size: 15px; }
.msg .body code { font-family: ui-monospace, Menlo, monospace; font-size: 13.5px; }
.msg .foot { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 11.5px; color: var(--muted); margin-top: 2px; float: right; margin-left: 12px; position: relative; top: 4px; }
.msg .foot svg { width: 17px; height: 17px; }
.msg .foot .read { color: var(--tick); }
.msg .tag { font-size: 11.5px; font-weight: 700; color: var(--accent-2); margin-bottom: 3px; display: flex; gap: 6px; align-items: center; letter-spacing: .01em; }
.msg .tag.auto { color: var(--muted); }
.msg.failed { outline: 1.5px solid var(--danger); }
.msg .err { font-size: 12.5px; color: var(--danger); margin-top: 4px; }
.msg img.photo { display: block; max-width: 100%; max-height: 340px; border-radius: 9px; margin: -2px -5px 4px; width: calc(100% + 10px); object-fit: cover; background: var(--panel-2); }
.msg video, .msg audio { display: block; max-width: 100%; margin-bottom: 4px; }
.doc { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 9px; background: rgba(0,0,0,.05); text-decoration: none; color: inherit; margin-bottom: 4px; min-width: 220px; }
:root[data-theme="dark"] .doc, .dark .doc { background: rgba(255,255,255,.06); }
.doc .ico { width: 36px; height: 42px; border-radius: 6px; background: var(--danger); color: #fff; display: grid; place-items: center; font-size: 10px; font-weight: 800; flex: none; }
.doc .n { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc .s { font-size: 12px; color: var(--muted); }
.buttons { display: flex; flex-direction: column; gap: 1px; margin: 6px -9px -5px; border-top: 1px solid var(--line); }
.buttons span { text-align: center; padding: 9px; color: var(--accent-2); font-weight: 600; font-size: 14.5px; }
.buttons span + span { border-top: 1px solid var(--line); }
.quote { border-left: 4px solid var(--accent-2); background: rgba(0,0,0,.05); border-radius: 6px; padding: 5px 8px; font-size: 13px; color: var(--muted); margin-bottom: 5px; max-height: 60px; overflow: hidden; }
.reaction { align-self: flex-start; font-size: 13px; color: var(--muted); padding: 0 8px; }
.choice { display: inline-flex; gap: 6px; align-items: center; font-weight: 600; }

.window-note { font-size: 12.5px; text-align: center; padding: 6px 12px; background: var(--warn-soft); color: var(--warn); }
.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); background: var(--wall); }
.composer .box { flex: 1; display: flex; align-items: flex-end; background: var(--panel); border-radius: 24px; box-shadow: var(--shadow); padding: 2px 4px 2px 4px; min-width: 0; }
.composer textarea { flex: 1; border: 0; background: transparent; resize: none; outline: none; padding: 11px 6px; max-height: 140px; font-size: 16px; min-width: 0; }
.composer .icon-btn { color: var(--muted); width: 40px; height: 44px; }
.send { width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--accent-2); color: var(--on-accent); display: grid; place-items: center; flex: none; }
.send:disabled { opacity: .5; }
.replying { display: flex; gap: 8px; align-items: center; margin: 0 8px; padding: 8px 10px; background: var(--panel); border-radius: 12px 12px 0 0; font-size: 13px; color: var(--muted); }
.replying div { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.closed-bar { padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 8px; background: var(--panel); border-top: 1px solid var(--line); }
.closed-bar p { margin: 0; font-size: 13px; color: var(--muted); text-align: center; }
.placeholder { margin: auto; text-align: center; color: var(--muted); padding: 24px; max-width: 360px; }
.placeholder svg { width: 64px; height: 64px; color: var(--accent); opacity: .5; }

/* ── sheets ────────────────────────────────────────────────────────────── */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 20; animation: fade .15s ease-out; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--panel); border-radius: 20px 20px 0 0;
  max-height: 92dvh; display: flex; flex-direction: column; animation: up .2s ease-out; padding-bottom: env(safe-area-inset-bottom);
}
@media (min-width: 700px) {
  .sheet { left: 50%; right: auto; bottom: auto; top: 50%; transform: translate(-50%, -50%); width: 560px; border-radius: 20px; max-height: 86dvh; animation: fade .15s ease-out; }
}
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 8px auto 0; }
.sheet header { display: flex; align-items: center; gap: 8px; padding: 10px 10px 10px 18px; }
.sheet header h2 { flex: 1; margin: 0; font-size: 18px; }
.sheet .content { overflow-y: auto; padding: 4px 18px 18px; display: flex; flex-direction: column; gap: 14px; }
.sheet .actions { display: flex; gap: 10px; padding: 12px 18px 16px; border-top: 1px solid var(--line); }
.sheet .actions .btn { flex: 1; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

.tpl-list { display: flex; flex-direction: column; gap: 8px; }
.tpl-item { text-align: left; border: 1px solid var(--line); border-radius: 12px; background: var(--panel-2); padding: 12px; display: flex; flex-direction: column; gap: 4px; }
.tpl-item b { font-size: 15px; }
.tpl-item span { font-size: 12.5px; color: var(--muted); }
.tpl-item p { margin: 2px 0 0; font-size: 13.5px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.pill { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); text-transform: uppercase; letter-spacing: .03em; }
.pill.mkt { background: var(--warn-soft); color: var(--warn); }
.preview-wall { background: var(--wall); border-radius: 14px; padding: 14px 10px; display: flex; flex-direction: column; }
.preview-wall .msg { max-width: 92%; }

.people { display: flex; flex-direction: column; gap: 12px; }
.person { border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.person .ph { display: flex; justify-content: space-between; align-items: center; font-weight: 650; }
.device { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 14px; color: var(--muted); padding: 8px 0 0; }
.code { font: 700 28px/1.2 ui-monospace, Menlo, monospace; letter-spacing: .12em; text-align: center; padding: 14px; background: var(--panel-2); border-radius: 12px; user-select: all; }
.stat { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.stat span { color: var(--muted); }

#toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 40;
  background: #1d2420; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: calc(100% - 32px);
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
#toast.show { opacity: 1; }
