:root {
  --bg: #f7f6fb;
  --panel: #ffffff;
  --panel-2: #efedf6;
  --ink: #1b1a22;
  --ink-2: #5b5868;
  --ink-3: #8c8999;
  --line: #e3e0ec;
  --accent: #5b4bd6;
  --accent-ink: #ffffff;
  --accent-soft: #ebe8fb;
  --user: #5b4bd6;
  --user-ink: #ffffff;
  --danger: #c2334d;
  --code-bg: #f1eff7;
  --shadow: 0 10px 30px rgba(27, 26, 34, .12);
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14131a;
    --panel: #1c1b24;
    --panel-2: #24232e;
    --ink: #eeedf3;
    --ink-2: #b2afbf;
    --ink-3: #807d8e;
    --line: #2e2c39;
    --accent: #8b7cf6;
    --accent-ink: #14131a;
    --accent-soft: #2b2845;
    --user: #3a3466;
    --user-ink: #f2f0ff;
    --danger: #ef6b82;
    --code-bg: #24232e;
    --shadow: 0 10px 30px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.app { height: 100dvh; }
.boot { height: 100%; display: grid; place-items: center; }
.boot-mark { width: 56px; height: 56px; opacity: .85; animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; transform: scale(.94); } }

/* ── buttons ─────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  cursor: pointer; font-weight: 500;
}
.btn:hover { background: var(--panel-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.06); background: var(--accent); }
.btn-wide { width: 100%; }
.btn-danger { color: var(--danger); }
.icon-btn {
  display: inline-grid; place-items: center; width: 44px; height: 44px;
  border: 0; border-radius: 12px; background: transparent; color: var(--ink-2); cursor: pointer;
}
.icon-btn:hover { background: var(--panel-2); color: var(--ink); }

/* ── sign-in / enrol gate ────────────────────────────────── */
.gate {
  min-height: 100dvh; display: grid; place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
}
.gate-card { width: 100%; max-width: 380px; text-align: center; }
.gate-mark { width: 72px; height: 72px; }
.gate-title { margin: 14px 0 4px; font-size: 30px; letter-spacing: -.02em; }
.gate-sub { margin: 0 0 28px; color: var(--ink-2); }
.gate-msg { min-height: 24px; margin: 14px 0 0; color: var(--ink-2); font-size: 14px; }
.gate-msg.err { color: var(--danger); }
.gate-foot { margin-top: 28px; color: var(--ink-3); font-size: 13px; }
.field { display: block; text-align: left; margin: 0 0 16px; }
.field span { display: block; font-size: 13px; color: var(--ink-2); margin-bottom: 6px; }
.field input {
  width: 100%; min-height: 44px; padding: 0 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel);
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ── shell ───────────────────────────────────────────────── */
.shell { height: 100dvh; display: flex; }
.drawer {
  width: 284px; flex: none; display: flex; flex-direction: column;
  background: var(--panel); border-right: 1px solid var(--line);
  padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 4px 12px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 18px; letter-spacing: -.01em; }
.brand img { width: 28px; height: 28px; }
.btn-new { width: 100%; justify-content: flex-start; }
.conv-list { flex: 1; overflow-y: auto; margin: 12px -4px; padding: 0 4px; }
.conv-item {
  display: flex; align-items: center; gap: 6px; width: 100%;
  border-radius: 10px; margin: 1px 0;
}
.conv-item > button.conv-open {
  flex: 1; min-width: 0; text-align: left; min-height: 40px; padding: 8px 10px;
  border: 0; background: transparent; border-radius: 10px; cursor: pointer;
  color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px;
}
.conv-item:hover { background: var(--panel-2); }
.conv-item.active { background: var(--accent-soft); }
.conv-item.active > button.conv-open { color: var(--ink); font-weight: 550; }
.conv-del { width: 34px; height: 34px; opacity: 0; }
.conv-del svg { width: 17px; height: 17px; }
.conv-item:hover .conv-del, .conv-item.active .conv-del { opacity: 1; }
.conv-group { font-size: 12px; color: var(--ink-3); margin: 14px 10px 4px; text-transform: uppercase; letter-spacing: .04em; }
.conv-empty { color: var(--ink-3); font-size: 14px; padding: 8px 10px; }
.drawer-foot { border-top: 1px solid var(--line); padding-top: 10px; }
.me {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 48px; padding: 6px 8px;
  border: 0; border-radius: 12px; background: transparent; cursor: pointer; color: var(--ink-2);
}
.me:hover { background: var(--panel-2); }
.me-name { flex: 1; text-align: left; color: var(--ink); font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: var(--accent-soft); color: var(--accent); font-weight: 650; font-size: 14px;
}
.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 4px; min-height: 56px;
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line); background: var(--bg);
}
.topbar-mid { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.engine {
  display: inline-flex; align-items: center; gap: 5px; min-height: 24px; padding: 2px 10px;
  border-radius: 999px; border: 1px solid var(--line); background: var(--panel);
  color: var(--ink-2); font: inherit; font-size: 12px; cursor: pointer;
}
.engine:hover { border-color: var(--accent); color: var(--ink); }
.engine::after { content: "⇄"; font-size: 11px; opacity: .7; }
.topbar-title {
  max-width: 100%; margin: 0; font-size: 16px; font-weight: 600; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.only-narrow { display: none; }

/* ── thread ──────────────────────────────────────────────── */
.thread { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.thread-inner {
  max-width: 760px; margin: 0 auto;
  padding: 20px max(16px, env(safe-area-inset-right)) 28px max(16px, env(safe-area-inset-left));
}
.empty { text-align: center; padding: 14vh 8px 0; color: var(--ink-2); }
.empty img { width: 56px; height: 56px; }
.empty h3 { margin: 12px 0 6px; color: var(--ink); font-size: 22px; letter-spacing: -.01em; }
.empty p { margin: 0 auto; max-width: 340px; }
.suggestions { display: grid; gap: 8px; margin: 24px auto 0; max-width: 420px; }
.suggestions button {
  text-align: left; min-height: 44px; padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer; color: var(--ink-2);
}
.suggestions button:hover { border-color: var(--accent); color: var(--ink); }

.msg { margin: 0 0 18px; display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { max-width: 100%; overflow-wrap: anywhere; }
.msg.user .bubble {
  max-width: 82%; background: var(--user); color: var(--user-ink);
  padding: 10px 14px; border-radius: 18px 18px 6px 18px; white-space: pre-wrap;
}
.msg.assistant .bubble { width: 100%; }
.msg.error .bubble {
  width: 100%; color: var(--danger); background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px;
}
.status { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; color: var(--ink-3); font-size: 14px; margin: 2px 0 10px; }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--ink-3); animation: blink 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 40% { opacity: .2; } }
.chip { font-size: 12.5px; padding: 3px 9px; border-radius: 999px; background: var(--panel-2); color: var(--ink-2); }

/* markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h1, .md h2, .md h3, .md h4 { margin: 18px 0 8px; line-height: 1.3; letter-spacing: -.01em; }
.md h1 { font-size: 22px; } .md h2 { font-size: 19px; } .md h3 { font-size: 17px; } .md h4 { font-size: 16px; }
.md ul, .md ol { margin: 0 0 12px; padding-left: 22px; }
.md li { margin: 3px 0; }
.md a { color: var(--accent); text-underline-offset: 2px; }
.md code { font-family: var(--mono); font-size: .9em; background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.md pre { background: var(--code-bg); padding: 12px 14px; border-radius: 10px; overflow-x: auto; margin: 0 0 12px; }
.md pre code { background: none; padding: 0; font-size: 13.5px; line-height: 1.5; }
.md blockquote { margin: 0 0 12px; padding: 2px 14px; border-left: 3px solid var(--line); color: var(--ink-2); }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.md .table-wrap { overflow-x: auto; margin: 0 0 12px; }
.md table { border-collapse: collapse; font-size: 14.5px; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--panel-2); font-weight: 600; }
.msg-tools { display: flex; gap: 2px; margin-top: 6px; }
.msg-tools .icon-btn { width: 32px; height: 32px; color: var(--ink-3); }
.msg-tools svg { width: 17px; height: 17px; }

/* ── composer ────────────────────────────────────────────── */
.composer {
  padding: 8px max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  background: var(--bg);
}
.composer-row, .attachments { max-width: 760px; margin: 0 auto; }
.composer-row {
  display: flex; align-items: flex-end; gap: 4px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 22px; padding: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
}
.composer-row:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; min-height: 44px; max-height: 40vh; resize: none; border: 0; outline: 0; background: transparent;
  padding: 10px 6px; line-height: 1.45; font-size: 16px;
}
.attach { flex: none; }
.attach input { display: none; }
.send {
  flex: none; width: 40px; height: 40px; margin: 2px; border: 0; border-radius: 50%;
  display: grid; place-items: center; background: var(--accent); color: var(--accent-ink); cursor: pointer;
}
.send:disabled { background: var(--panel-2); color: var(--ink-3); cursor: default; }
.send svg { width: 20px; height: 20px; stroke-width: 2.2; }
.attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.att {
  display: inline-flex; align-items: center; gap: 6px; max-width: 220px;
  padding: 4px 4px 4px 10px; border-radius: 10px; background: var(--panel-2); font-size: 13px;
}
.att span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.att button { width: 26px; height: 26px; border: 0; background: transparent; border-radius: 6px; cursor: pointer; color: var(--ink-2); }
.att button svg { width: 15px; height: 15px; }

/* ── settings sheet ──────────────────────────────────────── */
.sheet-wrap { position: fixed; inset: 0; background: rgba(10, 9, 14, .45); display: grid; place-items: center; z-index: 30; padding: 16px; }
.sheet {
  width: 100%; max-width: 460px; max-height: 88dvh; overflow-y: auto;
  background: var(--panel); border-radius: 18px; box-shadow: var(--shadow); padding: 8px 20px 20px;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--panel); padding-top: 8px; }
.sheet-head h3 { margin: 0; font-size: 18px; }
.sheet-sec { padding: 14px 0; border-top: 1px solid var(--line); }
.sheet-sec:first-of-type { border-top: 0; }
.sheet-sec h4 { margin: 0 0 6px; font-size: 15px; }
.hint { margin: 0 0 12px; color: var(--ink-2); font-size: 14px; }
.devices { list-style: none; margin: 0 0 12px; padding: 0; }
.devices li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.devices li:last-child { border-bottom: 0; }
.dev-name { flex: 1; min-width: 0; }
.dev-name b { display: block; font-weight: 550; }
.dev-name small { color: var(--ink-3); }
.link-box { margin-top: 12px; }
.link-row { display: flex; gap: 8px; }
.link-row input { flex: 1; min-width: 0; min-height: 44px; padding: 0 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-size: 13px; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  box-shadow: var(--shadow); z-index: 40; max-width: calc(100vw - 32px);
}

/* ── narrow screens: drawer slides over ──────────────────── */
@media (max-width: 899px) {
  .only-narrow { display: inline-grid; }
  .drawer {
    position: fixed; z-index: 20; top: 0; bottom: 0; left: 0; width: min(86vw, 320px);
    transform: translateX(-102%); transition: transform .22s ease; box-shadow: var(--shadow);
  }
  .shell.drawer-open .drawer { transform: none; }
  .shell.drawer-open .scrim { display: block; position: fixed; inset: 0; z-index: 15; background: rgba(10, 9, 14, .35); }
  .msg.user .bubble { max-width: 88%; }
}
@media (prefers-reduced-motion: reduce) {
  .drawer { transition: none; }
  .boot-mark, .dots i { animation: none; }
}
