:root {
  --bg: #ffffff; --bg2: #f3f4f6; --fg: #111827; --muted: #6b7280; --line: #e5e7eb;
  --accent: #1f6feb; --accent-fg: #fff; --out: #dbeafe; --in: #f3f4f6; --danger: #dc2626; --sys: #fef3c7;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0d1117; --bg2: #161b22; --fg: #e6edf3; --muted: #8b949e; --line: #30363d;
    --out: #1f3a5f; --in: #21262d; --sys: #3d2e00; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--fg);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left); }
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { border: 1px solid var(--line); background: var(--bg2); border-radius: 8px; padding: 8px 12px; cursor: pointer; }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
button.danger { color: var(--danger); }
button.icon { border: 0; background: transparent; padding: 6px 10px; font-size: 18px; line-height: 1; }
button.icon.primary { background: var(--accent); color: var(--accent-fg); border-radius: 50%; width: 40px; height: 40px; padding: 0; }
button:disabled { opacity: .5; cursor: default; }
input, textarea { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 9px 10px; width: 100%; }
textarea { resize: none; max-height: 140px; }
label { display: block; margin: 10px 0; font-size: 13px; color: var(--muted); }
label input { margin-top: 4px; font-size: 15px; color: var(--fg); }
.center { display: flex; align-items: center; justify-content: center; flex-direction: column; min-height: 100%; text-align: center; padding: 16px; }
.muted { color: var(--muted); } .small { font-size: 12px; } .err { color: var(--danger); min-height: 1.2em; }
.hint { font-size: 12px; color: var(--muted); }
.grow { flex: 1; min-width: 0; }
.row { display: flex; align-items: center; gap: 6px; }
.gap { gap: 10px; margin-top: 16px; }
.center-row { justify-content: center; }
.card { width: 100%; max-width: 360px; text-align: left; }
.card h1 { text-align: center; font-size: 22px; }
.card button[type=submit] { width: 100%; margin-top: 8px; padding: 11px; }
.tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.tabs button { flex: 1; } .tabs button.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); flex: none; }
.dot.on { background: #16a34a; }

#main { display: flex; height: 100dvh; height: 100vh; }
@supports (height: 100dvh) { #main { height: 100dvh; } }
#sidebar { width: 320px; border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.bar { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-bottom: 1px solid var(--line); min-height: 52px; }
#new-chat { padding: 8px 10px; border-bottom: 1px solid var(--line); }
#chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#chat-list li { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; display: flex; gap: 8px; align-items: center; }
#chat-list li.active { background: var(--bg2); }
#chat-list .name { font-weight: 600; }
#chat-list .prev { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chat-list .badge { background: var(--accent); color: var(--accent-fg); border-radius: 10px; font-size: 12px; padding: 1px 7px; }
#chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chat-view { display: flex; flex-direction: column; height: 100%; }
#btn-back { display: none; font-size: 28px; }
#messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 4px; }
.msg { max-width: 78%; padding: 7px 10px; border-radius: 12px; background: var(--in); align-self: flex-start; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.out { background: var(--out); align-self: flex-end; }
.msg.sys { background: var(--sys); align-self: center; font-size: 12px; max-width: 90%; text-align: center; }
.msg .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; user-select: none; }
.msg .st-read { color: var(--accent); } .msg .st-failed { color: var(--danger); }
.msg img { max-width: 100%; max-height: 320px; border-radius: 8px; display: block; cursor: zoom-in; }
.msg audio { max-width: 240px; display: block; }
.msg a { color: var(--accent); }
.msg .file { display: flex; gap: 8px; align-items: center; }
#composer { display: flex; align-items: flex-end; gap: 4px; padding: 8px; border-top: 1px solid var(--line);
  padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
#recording { display: flex; align-items: center; gap: 10px; padding: 12px; border-top: 1px solid var(--line); padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
.rec-dot { width: 10px; height: 10px; background: var(--danger); border-radius: 50%; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--bg); color: var(--fg); width: min(440px, 92vw); max-height: 85vh; }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog h2 { margin-top: 0; } dialog h3 { font-size: 14px; margin: 16px 0 6px; }
ul.plain { list-style: none; padding: 0; margin: 0; }
ul.plain li { padding: 8px 0; border-bottom: 1px solid var(--line); display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
code.sn { font-size: 13px; letter-spacing: 1px; word-spacing: 4px; display: block; width: 100%; }
.call { text-align: center; padding: 10px; }
#toast { position: fixed; left: 50%; bottom: 80px; transform: translateX(-50%); background: var(--fg); color: var(--bg); padding: 8px 14px; border-radius: 8px; z-index: 10; max-width: 90vw; }

@media (max-width: 700px) {
  #sidebar { width: 100%; border-right: 0; }
  #chat { display: none; }
  #main.in-chat #sidebar { display: none; }
  #main.in-chat #chat { display: flex; }
  #btn-back { display: inline-block; }
}
dialog.lightbox { width: auto; max-width: 96vw; max-height: 96vh; padding: 0; background: #000; border: 0; }
dialog.lightbox img { max-width: 96vw; max-height: 92vh; display: block; }
dialog.lightbox .close { position: absolute; right: 4px; top: 4px; color: #fff; background: rgba(0,0,0,.5); border-radius: 50%; }
.media { min-width: 120px; }
