:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #14161a;
  --muted: #667085;
  --border: #dfe3e8;
  --accent: #1d4ed8;
  --accent-text: #ffffff;
  --mine: #dbe7ff;
  --theirs: #ffffff;
  --danger: #b42318;
  --ok: #067647;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --panel: #171a21;
    --text: #e7e9ee;
    --muted: #98a2b3;
    --border: #2a2f3a;
    --accent: #4f7df3;
    --mine: #1f3266;
    --theirs: #1e222b;
    --danger: #f97066;
    --ok: #47cd89;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 15px; -webkit-text-size-adjust: 100%; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: 8px 12px; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.link { border: 0; background: none; color: var(--accent); padding: 4px 0; }
button.danger { color: var(--danger); }
button:disabled { opacity: 0.6; cursor: default; }
input, textarea, select { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 9px 10px; width: 100%; }
label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }
.boot { text-align: center; color: var(--muted); margin-top: 30vh; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 14px; min-height: 1.2em; margin: 8px 0 0; }
.ok { color: var(--ok); }

/* Auth */
.auth { max-width: 380px; margin: 10vh auto; padding: 24px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); }
.auth h1 { margin: 0 0 4px; font-size: 22px; }
.auth form button.primary { width: 100%; margin-top: 16px; padding: 10px; }
.auth .switch { margin-top: 14px; text-align: center; }

/* Layout */
.app { display: grid; grid-template-columns: 280px 1fr; height: 100vh; height: 100dvh; }
.sidebar { display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--border); min-height: 0; }
.sidebar header { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border); padding-top: max(12px, env(safe-area-inset-top)); }
.sidebar header strong { font-size: 17px; }
.conv-list { flex: 1; overflow-y: auto; padding: 6px; }
.conv { display: flex; justify-content: space-between; align-items: center; width: 100%; text-align: left; border: 0; background: none; padding: 10px; border-radius: 8px; }
.conv.active { background: var(--mine); }
.conv .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { background: var(--accent); color: var(--accent-text); border-radius: 999px; font-size: 12px; padding: 1px 7px; min-width: 20px; text-align: center; }
.new-dm { padding: 10px; border-top: 1px solid var(--border); padding-bottom: max(10px, env(safe-area-inset-bottom)); }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-header { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--border); padding-top: max(10px, env(safe-area-inset-top)); }
.chat-header h1 { font-size: 17px; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-header .back { display: none; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--danger); }
.status-dot.online { background: var(--ok); }
.banner { display: flex; gap: 10px; align-items: center; justify-content: space-between; padding: 8px 14px; background: var(--mine); font-size: 14px; }
.messages { flex: 1; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: min(75%, 640px); padding: 7px 10px; border-radius: var(--radius); background: var(--theirs); border: 1px solid var(--border); align-self: flex-start; overflow-wrap: anywhere; }
.msg.mine { align-self: flex-end; background: var(--mine); border-color: transparent; }
.msg .meta { font-size: 12px; color: var(--muted); margin-bottom: 2px; display: flex; gap: 6px; }
.msg .meta .author { font-weight: 600; color: var(--text); }
.msg .text { white-space: pre-wrap; }
.msg .undecryptable { color: var(--muted); font-style: italic; }
.msg .receipt { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.msg.pending { opacity: 0.6; }
.msg.failed { border-color: var(--danger); }
.msg .file { margin-top: 6px; }
.msg .file img { display: block; max-width: 100%; max-height: 320px; border-radius: 8px; margin-top: 4px; }
.load-more { align-self: center; }
.typing { min-height: 20px; padding: 0 16px; font-size: 13px; color: var(--muted); }
.composer { display: flex; gap: 8px; align-items: flex-end; padding: 8px 10px; background: var(--panel); border-top: 1px solid var(--border); padding-bottom: max(8px, env(safe-area-inset-bottom)); }
.composer .grow { flex: 1; min-width: 0; }
.composer textarea { resize: none; max-height: 140px; min-height: 40px; }
.composer .attach { flex: none; }
.composer .attachment { font-size: 12px; color: var(--muted); }
.empty { margin: auto; color: var(--muted); text-align: center; }

/* Settings dialog */
dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); color: var(--text); width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); padding: 0; }
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--panel); }
.dialog-body { padding: 4px 16px 16px; }
section.setting { border-bottom: 1px solid var(--border); padding: 12px 0; }
section.setting:last-child { border-bottom: 0; }
section.setting h2 { font-size: 15px; margin: 0 0 6px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 8px; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; letter-spacing: 0.5px; }
.device-list { list-style: none; padding: 0; margin: 6px 0 0; font-size: 14px; }
.device-list li { padding: 4px 0; display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.results { font-size: 13px; margin-top: 6px; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #111827; color: #fff; padding: 8px 14px; border-radius: 8px; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity 0.2s; max-width: calc(100vw - 32px); }
#toast.show { opacity: 0.95; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .app .chat { display: none; }
  .app.show-chat .sidebar { display: none; }
  .app.show-chat .chat { display: flex; }
  .chat-header .back { display: inline-block; }
  .msg { max-width: 88%; }
}
