/* urbild shell — mobile first; wide layout from 900px. Tokens on :root. */
:root {
  color-scheme: light;
  --bg: #f6f6f3; --surface: #ffffff; --surface-2: #eeeee9; --surface-3: #e4e4dd;
  --text: #1b1d21; --muted: #555b66; --border: #d9d9d2; --border-strong: #b9b9b0;
  --accent: #2153b8; --accent-hover: #1a4396; --on-accent: #ffffff;
  --user: #dfe8f8; --user-text: #13213d;
  --danger: #a3221b; --danger-bg: #fbe9e7; --ok: #1d6b35; --warn-bg: #fdf5df; --warn: #6b4e00;
  --focus: #2153b8; --shadow: 0 8px 30px rgba(20, 22, 30, .18);
  --backdrop: rgba(15, 17, 22, .45);
  --radius: 14px; --gutter: 16px; --header-h: 56px; --tap: 44px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111318; --surface: #1a1d24; --surface-2: #232731; --surface-3: #2c313d;
  --text: #e8eaef; --muted: #a7aebb; --border: #2f3441; --border-strong: #454c5c;
  --accent: #8db4ff; --accent-hover: #b0cbff; --on-accent: #0b1530;
  --user: #233656; --user-text: #e8eefb;
  --danger: #ffb4ab; --danger-bg: #3a1f1d; --ok: #7fd99b; --warn-bg: #3a3220; --warn: #f3d98b;
  --focus: #8db4ff; --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  --backdrop: rgba(0, 0, 0, .6);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111318; --surface: #1a1d24; --surface-2: #232731; --surface-3: #2c313d;
    --text: #e8eaef; --muted: #a7aebb; --border: #2f3441; --border-strong: #454c5c;
    --accent: #8db4ff; --accent-hover: #b0cbff; --on-accent: #0b1530;
    --user: #233656; --user-text: #e8eefb;
    --danger: #ffb4ab; --danger-bg: #3a1f1d; --ok: #7fd99b; --warn-bg: #3a3220; --warn: #f3d98b;
    --focus: #8db4ff; --shadow: 0 8px 30px rgba(0, 0, 0, .5);
    --backdrop: rgba(0, 0, 0, .6);
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; background: var(--bg); }
html, body { margin: 0; padding: 0; }
body {
  position: fixed; left: 0; right: 0; top: var(--app-top, 0px);
  height: 100vh; height: 100dvh; height: var(--app-h, 100dvh);
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--bg); color: var(--text);
  font: 16px/1.55 var(--font);
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: .55; }
a { color: var(--accent); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0);
  clip-path: inset(50%); white-space: nowrap; border: 0; padding: 0; margin: -1px; }
.skip { position: absolute; left: 8px; top: -60px; z-index: 50; background: var(--surface);
  padding: 10px 14px; border-radius: 8px; }
.skip:focus { top: 8px; }
.noscript { padding: 24px; }

/* ---------- header ---------- */
.top {
  order: 0; flex: none; display: flex; align-items: center; gap: 8px;
  min-height: var(--header-h);
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) 0 max(var(--gutter), env(safe-area-inset-left));
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: relative; z-index: 5;
}
.title { margin: 0; font-size: 17px; font-weight: 650; line-height: 1.2;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 0 1 auto; }
.spacer { flex: 1 1 0; }
.badge {
  flex: none; min-height: 32px; padding: 3px 10px; border-radius: 999px;
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text);
  font-size: 13px; font-weight: 600; white-space: nowrap; position: relative;
}
.badge::after { content: ""; position: absolute; inset: -6px -2px; } /* 44px hit area */
.iconbtn {
  flex: none; width: var(--tap); height: var(--tap); display: inline-flex; align-items: center;
  justify-content: center; border: 0; border-radius: 10px; background: transparent; color: var(--text);
}
.iconbtn:hover { background: var(--surface-2); }
.langbtn { position: relative; font-size: 14px; font-weight: 700; letter-spacing: .04em; border: 1px solid var(--border-strong);
  width: auto; min-width: var(--tap); padding: 0 10px; height: 36px; }
.tip {
  position: absolute; z-index: 20; left: var(--gutter); right: var(--gutter);
  top: calc(var(--header-h) + env(safe-area-inset-top) + 4px);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: 10px;
  padding: 10px 12px; font-size: 14px; box-shadow: var(--shadow); max-width: 420px;
}

/* ---------- modes: bottom bar on narrow ---------- */
.modes {
  order: 3; flex: none; display: flex; gap: 0;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.modes button {
  flex: 1 1 0; min-height: 52px; border: 0; background: transparent; color: var(--muted);
  font-size: 14px; font-weight: 600; padding: 6px 4px; border-top: 3px solid transparent;
}
.modes button[aria-selected="true"] { color: var(--accent); border-top-color: var(--accent); }
body.kb-open .modes { display: none; }

/* ---------- main area ---------- */
.main { order: 1; flex: 1 1 auto; min-height: 0; display: flex; position: relative; }
.panes { flex: 1 1 auto; min-width: 0; position: relative; }
.thread {
  position: absolute; inset: 0; overflow-y: auto; overflow-x: hidden;
  overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  scrollbar-color: var(--border-strong) transparent;
  padding: 12px max(var(--gutter), env(safe-area-inset-right)) 24px max(var(--gutter), env(safe-area-inset-left));
}
.thread-inner { max-width: 860px; margin: 0 auto; }
.exside { display: none; }

.empty { color: var(--muted); text-align: center; margin: 12vh auto 0; max-width: 34em; padding: 0 8px; }
.empty h2 { color: var(--text); font-size: 20px; margin: 0 0 8px; }
.empty p { margin: 0 0 12px; }
.empty .exlist { text-align: left; margin-top: 16px; }

/* ---------- messages ---------- */
.msg { margin: 14px 0; display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { min-width: 0; max-width: 100%; overflow-wrap: anywhere; border-radius: var(--radius); }
.msg.user .bubble { background: var(--user); color: var(--user-text); padding: 10px 14px;
  max-width: 88%; white-space: pre-wrap; border-bottom-right-radius: 4px; }
.msg.bot .bubble { width: 100%; background: var(--surface); border: 1px solid var(--border);
  padding: 12px 14px; }
.body > :first-child { margin-top: 0; }
.body > :last-child { margin-bottom: 0; }
.body p, .body ul, .body ol { margin: 0 0 .7em; }
.body ul, .body ol { padding-left: 1.4em; }
.body li { margin: .2em 0; }
.body h4 { font-size: 1em; margin: .9em 0 .4em; }
.body hr { border: 0; border-top: 1px solid var(--border-strong); margin: 12px 0; }
.body code { font-family: var(--mono); font-size: .9em; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
.body .tail { white-space: pre-wrap; }
.katex { font-size: 1.08em; }
.katex-display { max-width: 100%; overflow-x: auto; overflow-y: hidden; padding: 4px 0 8px; margin: .5em 0; }
.katex-display > .katex { white-space: nowrap; }
.katex-display::-webkit-scrollbar { height: 6px; }
.katex-display::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 999px; }
.katex-error { color: var(--danger) !important; }

.typing { display: inline-flex; gap: 4px; align-items: center; color: var(--muted); }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .4;
  animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .typing i { animation: none; } }

.cite { font: inherit; color: var(--accent); background: none; border: 0; padding: 0;
  text-decoration: underline; text-decoration-style: dotted; text-underline-offset: 3px; cursor: pointer; }
.sources { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 10px;
  padding-top: 8px; border-top: 1px solid var(--border); font-size: 13px; color: var(--muted); }
.srcchip { position: relative; font-size: 13px; min-height: 32px; padding: 4px 10px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); }
button.srcchip:hover { border-color: var(--accent); }
button.srcchip::after, .langbtn::after { content: ""; position: absolute; inset: -6px -2px; } /* 44px hit area */
.body blockquote { margin: 0 0 .7em; padding: 2px 0 2px 12px; border-left: 3px solid var(--border-strong); color: var(--muted); }
.note-line { margin-top: 8px; font-size: 13px; color: var(--muted); }
.note-line.warn { color: var(--warn); background: var(--warn-bg); border-radius: 8px; padding: 6px 10px; }
.note-line.err, .err { color: var(--danger); }
.msgactions { display: flex; justify-content: flex-end; gap: 4px; margin-top: 4px; }
.textbtn { background: none; border: 0; color: var(--accent); min-height: var(--tap); padding: 0 8px;
  font-size: 14px; border-radius: 8px; }
.textbtn:hover { background: var(--surface-2); }
.msgactions .textbtn { color: var(--muted); font-size: 13px; }

/* ---------- practice card (reusable for later card modes) ---------- */
.card { width: 100%; background: var(--surface); border: 1px solid var(--border-strong);
  border-left: 4px solid var(--accent); border-radius: var(--radius); padding: 12px 14px; }
.card.past { border-left-color: var(--border-strong); opacity: .85; }
.card .kicker { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--accent); }
.card .ctitle { font-size: 17px; font-weight: 650; margin: 2px 0 2px; }
.card .cmeta { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.card .statement { padding: 8px 0; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.card .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.card .progress { font-size: 13px; color: var(--muted); margin-top: 8px; }
.solution { margin: 0 0 10px; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); }
.solution .kicker { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--ok); margin-bottom: 4px; }

/* ---------- chips & buttons ---------- */
.chip { min-height: 36px; padding: 6px 14px; border-radius: 999px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); font-size: 14px; font-weight: 550; position: relative; }
.chip::after { content: ""; position: absolute; inset: -4px 0; } /* 44px hit area */
.chip:hover:not(:disabled) { border-color: var(--accent); }
.chip.accent { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.primary { min-height: var(--tap); padding: 10px 18px; border-radius: 10px; border: 0;
  background: var(--accent); color: var(--on-accent); font-weight: 650; }
.primary:hover:not(:disabled) { background: var(--accent-hover); }
.primary.wide { width: 100%; margin-top: 12px; }

/* ---------- composer ---------- */
.composer {
  order: 2; flex: none; background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px max(12px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  position: relative; z-index: 4;
}
body.has-modes:not(.kb-open) .composer { padding-bottom: 8px; }
.chiprow { display: flex; align-items: center; gap: 8px; margin: 2px 0 6px; max-width: 860px; margin-left: auto; margin-right: auto; }
.hintline { display: none; font-size: 12px; color: var(--muted); margin-left: auto; }
.inputrow { display: flex; align-items: flex-end; gap: 8px; max-width: 860px; margin: 0 auto; }
#q { flex: 1 1 auto; min-width: 0; resize: none; font-size: 16px; line-height: 1.4;
  min-height: var(--tap); max-height: 30vh; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--border-strong); background: var(--bg); color: var(--text); }
#q::placeholder { color: var(--muted); opacity: 1; }
.sendbtn { flex: none; width: var(--tap); height: var(--tap); border-radius: 50%; border: 0;
  background: var(--accent); color: var(--on-accent); display: inline-flex; align-items: center; justify-content: center; }
.sendbtn .i-stop { display: none; }
.sendbtn.stop { background: var(--danger); color: var(--surface); }
.sendbtn.stop .i-stop { display: block; }
.sendbtn.stop .i-send { display: none; }

/* ---------- examples ---------- */
.exlist { display: flex; flex-direction: column; gap: 8px; }
.exq { text-align: left; min-height: var(--tap); padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 15px; line-height: 1.35; }
.exq:hover { border-color: var(--accent); }
.exhead { font-size: 14px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 0 0 10px; }

/* ---------- citation popover ---------- */
.citepop {
  position: fixed; z-index: 40; left: 8px; right: 8px; bottom: calc(8px + env(safe-area-inset-bottom));
  max-height: 60%; overflow: auto; background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px 14px;
}
.citepop .pv-top { display: flex; align-items: flex-start; gap: 8px; }
.citepop .pv-head { font-weight: 650; flex: 1 1 auto; padding-top: 10px; }
.citepop .pv-sec { font-size: 13px; color: var(--muted); margin: 2px 0 8px; }
.citepop .pv-body { overflow-wrap: anywhere; }

/* ---------- dialogs / sheets ---------- */
dialog { color: var(--text); background: var(--surface); border: 0; padding: 0; }
dialog::backdrop { background: var(--backdrop); }
.sheet {
  width: 100%; max-width: 100%; margin: auto 0 0; max-height: 88%;
  border-radius: 18px 18px 0 0; box-shadow: var(--shadow); overflow: auto;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet[open] { animation: slideup .18s ease-out; }
@keyframes slideup { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .sheet[open] { animation: none; } }
.sheethead { display: flex; align-items: center; gap: 8px; padding: 10px 8px 4px var(--gutter);
  position: sticky; top: 0; background: var(--surface); z-index: 1; }
.sheethead h2 { flex: 1 1 auto; margin: 0; font-size: 18px; }
.sheetbody { padding: 4px var(--gutter) 20px; }
.sheetbody h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 18px 0 6px; }
.row { display: flex; align-items: center; width: 100%; min-height: var(--tap); padding: 8px 4px;
  border: 0; border-bottom: 1px solid var(--border); background: none; color: var(--text);
  text-decoration: none; text-align: left; font-size: 16px; }
.row:hover { color: var(--accent); }
.field { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: var(--tap); margin: 4px 0; }
.field.stack { flex-direction: column; align-items: stretch; }
.field select, .field textarea, .loginbox input {
  font-size: 16px; min-height: var(--tap); padding: 8px 10px; border-radius: 10px;
  border: 1px solid var(--border-strong); background: var(--bg); color: var(--text);
}
.field textarea { resize: vertical; width: 100%; }
.note { font-size: 13px; color: var(--muted); margin: 6px 0; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 13px; margin: 8px 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-family: var(--mono); overflow-wrap: anywhere; }
.prose p { margin: 0 0 12px; line-height: 1.6; }
.prose h3 { margin-top: 16px; }

.modal.login { width: min(380px, calc(100% - 32px)); border-radius: 16px; box-shadow: var(--shadow); }
.modal.login::backdrop { background: var(--bg); }
.loginbox { padding: 24px 22px 16px; display: flex; flex-direction: column; gap: 10px; }
.loginbox h2 { margin: 0; font-size: 20px; }
.loginbox p { margin: 0; color: var(--muted); }
.loginbox input { width: 100%; }
.loginbox .err { min-height: 1.2em; color: var(--danger); }

/* ---------- wide ---------- */
@media (min-width: 900px) {
  :root { --gutter: 24px; }
  .modes {
    position: absolute; top: env(safe-area-inset-top); left: 50%; transform: translateX(-50%);
    height: var(--header-h); z-index: 6; align-items: center; gap: 2px;
    background: transparent; border: 0; padding: 0;
  }
  .modes::before { content: ""; position: absolute; inset: 9px -4px; background: var(--surface-2);
    border-radius: 12px; z-index: -1; }
  .modes button { min-height: 38px; padding: 6px 18px; border-radius: 9px; border-top: 0; color: var(--muted); }
  .modes button[aria-selected="true"] { background: var(--surface); color: var(--text);
    box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }
  body.kb-open .modes { display: flex; }
  .title { max-width: calc(50% - 220px); }
  .exside:not([hidden]) {
    display: flex; flex-direction: column; flex: none; width: 300px; overflow-y: auto;
    padding: 16px; border-right: 1px solid var(--border); background: var(--surface);
  }
  .empty .exlist { display: none; }
  #exbtn { display: none; }
  body.has-side .composer { padding-left: 300px; }
  .msg.user .bubble { max-width: 78%; }
  .msg.bot .bubble, .msg .card { max-width: 78%; }
  .msg.wide .card { max-width: 100%; }
  .hintline { display: inline; }
  .citepop { left: auto; right: auto; bottom: auto; width: 460px; max-height: 60vh; }
  .sheet { width: 520px; max-width: calc(100% - 48px); margin: auto; border-radius: 16px; max-height: 84vh; }
  .tip { left: auto; right: 64px; }
}
@media (min-width: 900px) and (max-width: 1100px) {
  .exside:not([hidden]) { width: 240px; }
  body.has-side .composer { padding-left: 240px; }
}
