:root {
  color-scheme: light dark;
  --bg: #efe9e1;
  --panel: #fffdf9;
  --panel-2: #f5f0ea;
  --thread: #f3ede5;
  --ink: #1e1a17;
  --muted: #7a7168;
  --line: #e5ddd3;
  --accent: #ec4e1a;
  --accent-2: #ff9a3d;
  --accent-ink: #ffffff;
  --accent-soft: #fde5d8;
  --bubble-in: #ffffff;
  --bubble-out: #ffe0cd;
  --ok: #17864a;
  --warn-bg: #fff3d6;
  --warn-ink: #7a4b00;
  --danger: #cf3030;
  --shadow: 0 1px 1px rgb(60 40 20 / 0.06), 0 2px 10px rgb(60 40 20 / 0.05);
  --radius: 18px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0b0a;
    --panel: #171412;
    --panel-2: #201c19;
    --thread: #110f0d;
    --ink: #f3ede6;
    --muted: #a0968c;
    --line: #2c2723;
    --accent: #ff6a2b;
    --accent-2: #ffa152;
    --accent-soft: #3b2014;
    --bubble-in: #221e1b;
    --bubble-out: #4a2513;
    --ok: #4cc37f;
    --warn-bg: #3a2a0c;
    --warn-ink: #ffd27a;
    --danger: #ff6464;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app { height: var(--app-h, 100dvh); overflow: hidden; }

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.icon { width: 22px; height: 22px; flex: none; }
.icon.tiny { width: 15px; height: 15px; }
.ok { color: var(--ok); }
.muted { color: var(--muted); }

.boot, .fatal {
  display: grid; place-content: center; height: 100%; text-align: center; color: var(--muted); padding: 24px;
}

/* ---------- buttons & inputs ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel-2); font-weight: 600;
}
.btn.primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn.primary:disabled { opacity: 0.6; }
.btn.danger { color: var(--danger); background: transparent; border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn.small { min-height: 32px; padding: 0 12px; font-size: 14px; border-radius: 9px; }

.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 50%; background: transparent; color: var(--muted);
}
.icon-btn:hover:not(:disabled) { background: var(--panel-2); color: var(--ink); }
.icon-btn.ok { color: var(--ok); }
.icon-btn:disabled { opacity: 0.4; }

input, select, textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel); padding: 12px 14px; font-size: 16px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
label { font-weight: 600; font-size: 14px; }
.hint { margin: -4px 0 4px; font-size: 13px; color: var(--muted); }
.form-error { margin: 0; min-height: 1.2em; color: var(--danger); font-size: 14px; }
.stack { display: grid; gap: 10px; }

/* ---------- brand ---------- */

.brand { display: flex; align-items: center; gap: 10px; font-weight: 750; font-size: 20px; letter-spacing: -0.01em; }
.logo { width: 30px; height: 30px; flex: none; }
.brand.big { font-size: 26px; }
.brand.big .logo { width: 44px; height: 44px; }
.logo.huge { width: 72px; height: 72px; }

/* ---------- welcome ---------- */

.welcome {
  height: 100%; overflow-y: auto; display: grid; place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
  background:
    radial-gradient(1000px 500px at 50% -10%, color-mix(in srgb, var(--accent) 16%, transparent), transparent 70%),
    var(--bg);
}
.welcome-card {
  width: 100%; max-width: 420px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 24px; padding: 28px 24px; box-shadow: var(--shadow);
}
.welcome h1 { font-size: 26px; line-height: 1.15; letter-spacing: -0.02em; margin: 22px 0 8px; text-wrap: balance; }
.lede { margin: 0 0 22px; color: var(--muted); }
.welcome-form { display: grid; gap: 8px; }
.welcome-form .btn { margin-top: 8px; }
.notice {
  margin: 0 0 16px; padding: 12px 14px; border-radius: 12px;
  background: var(--warn-bg); color: var(--warn-ink); font-size: 14px;
}
.notice p { margin: 4px 0 0; }
.btn.link {
  min-height: 0; padding: 4px 0; border: 0; background: none;
  color: var(--muted); font-weight: 500; text-decoration: underline; margin-bottom: 12px;
}
.facts { list-style: none; padding: 18px 0 0; margin: 18px 0 0; border-top: 1px solid var(--line); display: grid; gap: 10px; font-size: 14px; color: var(--muted); }
.facts li { display: flex; align-items: center; gap: 10px; }
.facts .icon { color: var(--accent); width: 20px; height: 20px; }

/* ---------- shell ---------- */

.shell {
  display: grid; grid-template-columns: 340px 1fr; height: 100%;
  max-width: 1280px; margin: 0 auto; background: var(--panel);
  border-inline: 1px solid var(--line);
}
.sidebar { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); position: relative; }
.conversation { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--thread); }

.bar {
  display: flex; align-items: center; gap: 10px; min-height: 64px;
  padding: max(10px, env(safe-area-inset-top)) 12px 10px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.bar .brand { flex: 1; }
.conn {
  display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted);
  max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conn::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.conn[data-state="online"]::before { background: var(--ok); }
.conn[data-state="connecting"]::before { background: var(--accent-2); animation: pulse 1s infinite alternate; }

/* ---------- chat list ---------- */

.chat-list { flex: 1; overflow-y: auto; padding: 6px 0 96px; }
.chat-item {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px;
  width: 100%; padding: 10px 16px; border: 0; background: transparent; text-align: left;
}
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: var(--accent-soft); }
.chat-text { display: grid; min-width: 0; }
.chat-name { display: flex; align-items: center; gap: 5px; font-weight: 650; }
.chat-sub { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item.unread .chat-sub { color: var(--accent); font-weight: 600; }
.chat-item.warn .chat-sub { color: var(--danger); }
.chat-side { display: grid; justify-items: end; gap: 4px; font-size: 12px; color: var(--muted); }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 12px; display: grid; place-items: center;
}
.list-empty { padding: 32px 24px; text-align: center; }
.list-empty p { margin: 0 0 6px; }

.avatar {
  width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; flex: none;
  font-weight: 700; font-size: 18px; color: #fff;
  background: linear-gradient(135deg, hsl(var(--hue) 70% 58%), hsl(calc(var(--hue) + 35) 65% 45%));
}
.bar .avatar { width: 38px; height: 38px; font-size: 16px; }

.fab {
  position: absolute; right: 16px; bottom: max(16px, env(safe-area-inset-bottom));
  display: inline-flex; align-items: center; gap: 8px; height: 52px; padding: 0 20px 0 16px;
  border: 0; border-radius: 26px; background: var(--accent); color: var(--accent-ink);
  font-weight: 650; box-shadow: 0 6px 20px color-mix(in srgb, var(--accent) 40%, transparent);
}

/* ---------- conversation ---------- */

.convo-empty { margin: auto; max-width: 360px; text-align: center; padding: 24px; color: var(--muted); }
.convo-empty h2 { color: var(--ink); margin: 16px 0 6px; }

.bar .back { display: none; margin-left: -8px; }
.peer { flex: 1; min-width: 0; display: grid; text-align: left; border: 0; background: transparent; padding: 0; }
.peer b { font-size: 17px; }
.peer span { font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.banners:empty { display: none; }
.banner {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; font-size: 14px;
  background: var(--panel-2); border-bottom: 1px solid var(--line);
}
.banner.warn { background: var(--warn-bg); color: var(--warn-ink); }
.banner.subtle { color: var(--muted); justify-content: center; }
.banner span:not(.spinner) { flex: 1; }
.banner.subtle span:not(.spinner) { flex: none; }
.spinner {
  width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite; flex: none;
}

.thread {
  flex: 1; overflow-y: auto; padding: 14px 14px 20px; display: flex; flex-direction: column; gap: 6px;
  overscroll-behavior: contain;
}
.system-note {
  align-self: center; display: flex; align-items: center; gap: 8px; max-width: 420px;
  margin: 4px auto 12px; padding: 8px 12px; border: 0; border-radius: 10px;
  background: var(--warn-bg); color: var(--warn-ink); font-size: 12.5px; text-align: left;
}
.system-note .icon { width: 16px; height: 16px; }

.row { display: flex; }
.row.out { justify-content: flex-end; }

.bubble {
  position: relative; max-width: min(78%, 520px); padding: 8px 12px 6px; border-radius: var(--radius);
  background: var(--bubble-in); box-shadow: var(--shadow); overflow: hidden;
  -webkit-user-select: text; user-select: text;
}
.row.in .bubble { border-bottom-left-radius: 6px; }
.row.out .bubble { background: var(--bubble-out); border-bottom-right-radius: 6px; }
.text { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble.has-photo { padding: 4px 4px 6px; }
.photo {
  display: block; max-width: 100%; max-height: 60vh; border-radius: 14px;
  -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; pointer-events: none;
}
.has-photo .meta { padding: 0 8px; }

.meta { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin-top: 2px; font-size: 11.5px; color: var(--muted); }
.fuse { display: inline-flex; align-items: center; gap: 2px; color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }
.fuse .icon { width: 13px; height: 13px; }
.status.opened { color: var(--accent); font-weight: 600; }
.fuse-bar {
  position: absolute; left: 0; bottom: 0; height: 3px; width: 100%;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  transform-origin: left; animation: burn var(--dur) linear var(--delay) forwards;
}

.bubble.locked {
  display: flex; align-items: center; gap: 12px; padding: 10px 14px 10px 10px; border: 0; text-align: left;
  background: var(--panel); border: 1.5px dashed color-mix(in srgb, var(--accent) 55%, transparent);
}
.bubble.locked:hover:not(:disabled) { background: var(--accent-soft); }
.lock-badge {
  width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: linear-gradient(135deg, var(--accent-2), var(--accent)); color: #fff;
}
.lock-badge .icon { width: 19px; height: 19px; }
.locked-text { display: grid; }
.locked-text small { color: var(--muted); font-size: 12px; }
.bubble.locked time { align-self: flex-end; font-size: 11.5px; color: var(--muted); }
.bubble.locked:disabled .lock-badge { animation: pulse 0.6s infinite alternate; }

.stub {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border-radius: 12px;
  font-size: 12.5px; color: var(--muted); background: color-mix(in srgb, var(--panel) 60%, transparent);
  border: 1px solid var(--line);
}
.stub .icon { width: 14px; height: 14px; }
.stub time { opacity: 0.8; }
.stub.opened .icon { color: var(--accent); }
.stub.failed { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, transparent); }

/* ---------- composer ---------- */

.composer {
  display: flex; align-items: flex-end; gap: 6px;
  padding: 8px 10px max(8px, env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; min-height: 44px; max-height: 140px; border-radius: 22px; padding: 10px 16px; line-height: 1.4;
  background: var(--panel-2);
}
.timer-chip {
  display: inline-flex; align-items: center; gap: 4px; height: 44px; padding: 0 10px 0 8px; flex: none;
  border: 0; border-radius: 22px; background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.timer-chip .icon { width: 19px; height: 19px; }
.composer .icon-btn { height: 44px; width: 44px; }
.send {
  width: 44px; height: 44px; border-radius: 50%; border: 0; flex: none; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink);
}
.send:disabled { opacity: 0.4; }
.send .icon { width: 20px; height: 20px; margin-left: -2px; }

/* ---------- dialogs ---------- */

.dialog {
  width: min(440px, calc(100vw - 32px)); max-height: calc(100dvh - 48px); overflow-y: auto;
  border: 1px solid var(--line); border-radius: 22px; padding: 18px 20px 22px;
  background: var(--panel); color: var(--ink); box-shadow: 0 20px 60px rgb(0 0 0 / 0.3);
}
.dialog::backdrop { background: rgb(10 6 3 / 0.5); backdrop-filter: blur(3px); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.dialog-head h2 { margin: 0; font-size: 20px; }
.dialog-head .icon-btn { margin-right: -8px; }
.dialog > p { margin: 0 0 14px; color: var(--muted); font-size: 15px; }
.dialog > .btn { width: 100%; margin-top: 6px; }
.dialog label { display: block; margin: 12px 0 6px; }

.safety-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px 8px; margin: 4px 0 18px;
  padding: 16px; border-radius: 14px; background: var(--panel-2);
  font: 600 17px/1 ui-monospace, "SF Mono", Menlo, Consolas, monospace; letter-spacing: 0.04em; text-align: center;
}
.warn-text { color: var(--warn-ink) !important; background: var(--warn-bg); padding: 10px 12px; border-radius: 10px; font-size: 14px !important; }

.me-card { display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: 14px; background: var(--panel-2); margin: 6px 0 4px; }
.me-card div { display: grid; }
.me-card .muted { font-size: 13px; }
.explain { margin-top: 16px; font-size: 14px; }
.explain summary { cursor: pointer; font-weight: 600; }
.explain ul { padding-left: 18px; color: var(--muted); display: grid; gap: 6px; }
.danger-zone { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line); }
.danger-zone .btn { width: 100%; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 10;
  transform: translate(-50%, 12px); opacity: 0; transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 12px;
  background: var(--ink); color: var(--panel); font-size: 14px; box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- phone layout ---------- */

@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; border: 0; }
  .shell.chat-open .sidebar { display: none; }
  .shell:not(.chat-open) .conversation { display: none; }
  .bar .back { display: inline-grid; }
  .bubble { max-width: 84%; }
}

@keyframes burn { from { transform: scaleX(1); } to { transform: scaleX(0); } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { from { opacity: 1; } to { opacity: 0.45; } }

@media (prefers-reduced-motion: reduce) {
  .fuse-bar, .spinner, .conn::before, .lock-badge { animation: none !important; }
}
