:root {
  --bg: #0f1115;
  --panel: #161922;
  --panel-2: #1f2330;
  --border: #2a2f3d;
  --text: #e8ecf1;
  --muted: #8a93a6;
  --accent: #7aa2ff;
  --accent-fg: #0c1020;
  --user: #2b3a5b;
  --assistant: #1f2330;
  --task: #2b3024;
  --error: #ff7a7a;
  /* Mandarin tone colours — Pleco/Skritter-ish convention */
  --tone-1: #5dade2;   /* high level */
  --tone-2: #58d68d;   /* rising */
  --tone-3: #f5b041;   /* dipping */
  --tone-4: #ec7063;   /* falling */
  --tone-neutral: #95a5a6;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
html, body { overflow-x: hidden; max-width: 100vw; }
body {
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, sans-serif;
  background: var(--bg); color: var(--text);
}
.hidden { display: none !important; }
button { cursor: pointer; }

/* login */
#login-view { display: grid; place-items: center; height: 100vh; height: 100dvh; padding: 1rem; }
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 1.5rem; width: min(360px, 100%);
  display: flex; flex-direction: column; gap: 0.75rem;
}
.card h1 { margin: 0 0 0.25rem; font-weight: 600; }
.card label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 13px; color: var(--muted); }
.card input, textarea, dialog input {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.55rem 0.7rem; font: inherit;
}
.card input:focus, textarea:focus, dialog input:focus { outline: 2px solid var(--accent); }
.card button, #chat-form button, dialog menu button[value="default"] {
  background: var(--accent); color: var(--accent-fg); border: 0; border-radius: 8px;
  padding: 0.55rem 0.9rem; font-weight: 600;
}
.error { color: var(--error); margin: 0; min-height: 1.2em; font-size: 13px; }

/* chat layout — 100dvh so Chrome/Safari mobile toolbars don't occlude the
   input row. Header + input stay pinned; only #messages scrolls. */
#chat-view { display: grid; grid-template-columns: 240px 1fr; height: 100vh; height: 100dvh; }
#sidebar {
  background: var(--panel); border-right: 1px solid var(--border);
  display: grid; grid-template-rows: auto 1fr auto; min-height: 0;
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem; border-bottom: 1px solid var(--border);
}
.new-chat-wrap { position: relative; }
#new-chat-btn {
  background: var(--accent); color: var(--accent-fg); border: 0; border-radius: 6px;
  width: 28px; height: 28px; font-size: 18px; line-height: 1; font-weight: 700;
  cursor: pointer;
}
#new-chat-menu.popover {
  position: absolute; right: 0; top: calc(100% + 6px);
  z-index: 50; min-width: 230px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  display: flex; flex-direction: column; padding: 4px;
}
#new-chat-menu.popover.hidden { display: none; }
#new-chat-menu .model-card {
  background: transparent; border: 0; border-radius: 6px; cursor: pointer;
  text-align: left; padding: 8px 10px; color: var(--text); font: inherit;
}
#new-chat-menu .model-card + .model-card { margin-top: 2px; }
#new-chat-menu .model-card:hover { background: var(--panel-2); }
#new-chat-menu .model-name { font-weight: 600; font-size: 13px; }
#new-chat-menu .model-name .model-tier {
  color: var(--muted); font-weight: 400; font-size: 11px; margin-left: 4px;
}
#new-chat-menu .model-id { color: var(--muted); font-size: 11px; font-family: ui-monospace,monospace; }
#new-chat-menu .model-cost { color: var(--muted); font-size: 11px; margin-top: 2px; }

.model-badge {
  font-size: 10px; padding: 1px 5px; border-radius: 4px; font-weight: 600;
  text-transform: lowercase; letter-spacing: 0.02em; flex-shrink: 0;
}
.model-badge-flash { background: rgba(110, 200, 130, 0.15); color: #6ec882; }
.model-badge-pro   { background: rgba(180, 140, 250, 0.15); color: #b48cfa; }
#chat-list { list-style: none; margin: 0; padding: 0.5rem; overflow-y: auto; min-height: 0; }
#chat-list li {
  padding: 0.5rem 0.6rem; border-radius: 6px; cursor: pointer; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px;
  display: flex; align-items: center; gap: 0.4rem;
}
#chat-list li:hover { background: var(--panel-2); }
#chat-list li.active { background: var(--panel-2); border: 1px solid var(--border); }
#chat-list li .chat-name { flex: 1; overflow: hidden; text-overflow: ellipsis; }
#chat-list li .del-btn {
  background: transparent; border: 0; color: var(--muted); padding: 0 0.2rem;
  visibility: hidden; font-size: 14px;
}
#chat-list li:hover .del-btn { visibility: visible; }
#chat-list li .del-btn:hover { color: var(--error); }

.sidebar-foot {
  border-top: 1px solid var(--border); padding: 0.6rem 0.75rem;
  display: flex; flex-direction: column; gap: 0.4rem; font-size: 12px; color: var(--muted);
}
.sidebar-actions { display: flex; gap: 0.4rem; }
.sidebar-actions button {
  flex: 1; background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 0.35rem; font-size: 12px;
}

#chat-pane { display: grid; grid-template-rows: auto 1fr auto; min-height: 0; }
#chat-header {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.6rem 1rem; border-bottom: 1px solid var(--border);
}
#chat-title { font-weight: 600; }
#rename-btn, #debug-toggle, #sidebar-toggle {
  background: transparent; border: 0; color: var(--muted); padding: 0 0.3rem; font-size: 16px;
}
#rename-btn:hover, #debug-toggle:hover, #sidebar-toggle:hover { color: var(--text); }
#sidebar-toggle { display: none; }
#debug-toggle[aria-pressed="true"] { color: var(--accent); background: var(--panel-2); border-radius: 6px; }
.meta { color: var(--muted); margin-left: auto; font-size: 12px; }

#messages {
  overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 0.5rem;
  min-height: 0;
}
.msg { padding: 0.6rem 0.8rem; border-radius: 10px; max-width: 75ch; white-space: pre-wrap; }
.msg.user { background: var(--user); align-self: flex-end; }
.msg.assistant { background: var(--assistant); border: 1px solid var(--border); align-self: flex-start; }
.msg.assistant.error { border-color: var(--error); color: var(--error); }

/* Bubble content — chronological mix of .msg-text segments and .tool-call
   segments (debug-only). Tool calls appear inline between text segments,
   showing the model's actual think → call → think rhythm. */
.msg.assistant .msg-content { display: flex; flex-direction: column; gap: 0.4rem; }
.msg.assistant .msg-content > .msg-text { margin: 0; }

/* Tool-call segments — hidden by default, visible when body.debug. Sized
   like inline notes, not the dominant element of the bubble. */
.msg.assistant .tool-call {
  display: none;
  padding: 0.35rem 0.5rem;
  margin: 0.1rem 0;
  border-left: 2px solid var(--border);
  background: rgba(0,0,0,0.18);
  border-radius: 0 6px 6px 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  color: var(--muted);
  line-height: 1.5;
}
body.debug .msg.assistant .tool-call { display: block; }

/* Cost / usage strip — shown when debug is on, pinned below tool-strip */
.msg.assistant .cost-strip { display: none; }
body.debug .msg.assistant .cost-strip {
  display: flex; gap: 0.7rem; align-items: center; flex-wrap: wrap;
  margin-top: 0.35rem;
  padding: 0.25rem 0.4rem;
  border: 1px dashed var(--border); border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px; color: var(--muted);
  cursor: pointer;
}
body.debug .msg.assistant .cost-strip:hover { border-color: var(--accent); }
.cost-strip .cost-cost { color: var(--accent); }
.cost-strip .cost-cum  { color: var(--text); opacity: 0.85; }

/* Cumulative pill in chat header (only visible in debug) */
#cum-cost { display: none; }
body.debug #cum-cost {
  display: inline-block; margin-left: 0.5rem; padding: 0.15rem 0.5rem;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px;
  font-family: ui-monospace, monospace; font-size: 11px; color: var(--accent);
}

/* Cost breakdown popover (when user clicks a cost-strip) */
.cost-popover {
  position: fixed; z-index: 100; background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.7rem 0.85rem; box-shadow: 0 8px 30px rgba(0,0,0,0.6);
  font-family: ui-monospace, monospace; font-size: 11px; color: var(--text);
  min-width: 280px; max-width: 90vw;
}
.cost-popover h4 { margin: 0 0 0.4rem; font-size: 12px; color: var(--accent); font-weight: 600; }
.cost-popover .row { display: flex; justify-content: space-between; padding: 0.1rem 0; gap: 1rem; }
.cost-popover .row .lbl { color: var(--muted); }
.cost-popover .close { position: absolute; top: 4px; right: 8px; cursor: pointer; color: var(--muted); }
.tool-call .tool-line { padding: 0.1rem 0; word-break: break-word; overflow-wrap: anywhere; }
.tool-call .tool-line.in .name { color: var(--accent); }
.tool-call .tool-line.in .name::before { content: "→ "; color: var(--accent); }
.tool-call .tool-line.out { color: var(--muted); opacity: 0.8; padding-left: 1.2em; }
.tool-call .tool-line.out::before { content: "↩ "; }
.tool-call .args { white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere; }

/* Visible right/wrong chip for a graded attempt (always shown, not debug-gated) */
.attempt-chip {
  display: inline-flex; align-items: center; gap: 0.45em;
  margin: 0.3rem 0; padding: 0.18rem 0.55rem;
  border-radius: 0.6rem; font-size: 0.85rem;
  border: 1px solid transparent; width: fit-content; max-width: 100%;
}
.attempt-chip .mark { font-weight: 700; }
.attempt-chip .word { font-weight: 600; }
.attempt-chip .note { color: var(--muted); opacity: 0.85; font-size: 0.78rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attempt-chip.ok { background: rgba(34, 197, 94, 0.12); border-color: rgba(34, 197, 94, 0.35); }
.attempt-chip.ok .mark { color: #22c55e; }
.attempt-chip.miss { background: rgba(239, 68, 68, 0.12); border-color: rgba(239, 68, 68, 0.35); }
.attempt-chip.miss .mark { color: #ef4444; }

/* Word-progress panel — read-only overlay over the chat pane */
#chat-pane { position: relative; }
#progress-view {
  position: absolute; inset: 0; z-index: 20;
  background: var(--bg);
  display: flex; flex-direction: column; overflow: hidden;
}
#progress-view.hidden { display: none; }
.progress-head {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.7rem 1rem; border-bottom: 1px solid var(--border);
  font-weight: 600; flex: 0 0 auto;
}
.progress-head .progress-counts { color: var(--muted); font-weight: 400; font-size: 0.82rem; }
#progress-close {
  margin-left: auto; background: none; border: none; color: var(--muted);
  font-size: 1.1rem; cursor: pointer; line-height: 1; padding: 0.2rem 0.4rem;
}
#progress-close:hover { color: var(--text); }
.progress-body { overflow-y: auto; padding: 0.25rem 1rem 2.5rem; }
.progress-body h4 { margin: 1.1rem 0 0.4rem; font-size: 0.95rem; }
.progress-body h4 .psub { font-weight: 400; }
.ptable { width: 100%; border-collapse: collapse; }
.ptable td { padding: 0.32rem 0.5rem; border-bottom: 1px solid var(--panel-2); vertical-align: middle; }
.ptable .pw { font-weight: 600; font-size: 1.05rem; white-space: nowrap; }
.ptable .pbar { color: var(--accent); white-space: nowrap; font-variant-numeric: tabular-nums; }
.psub { color: var(--muted); font-size: 0.82rem; }
.pempty { padding: 0.25rem 0.5rem; }
.ptabs { display: flex; gap: 0.35rem; }
.ptab {
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--border);
  border-radius: 0.5rem; padding: 0.25rem 0.7rem; cursor: pointer; font-size: 0.9rem;
}
.ptab.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); font-weight: 600; }
.poff { color: var(--tone-3); font-size: 0.8rem; }
.pcoverage { display: flex; flex-wrap: wrap; gap: 0.9rem; align-items: baseline; padding: 0.4rem 0.1rem 0.2rem; color: var(--muted); font-size: 0.85rem; }
.pcov b { color: var(--text); font-variant-numeric: tabular-nums; }
/* sidebar progress button (above logout) */
.sidebar-btn {
  width: 100%; text-align: left; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 0.5rem; padding: 0.45rem 0.7rem;
  cursor: pointer; font-size: 0.9rem; margin-bottom: 0.5rem;
}
.sidebar-btn:hover { border-color: var(--accent); }
.sidebar-btn[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }

/* streaming feedback */
.msg.assistant.streaming::after {
  content: "▍";
  animation: blink 1s steps(1) infinite;
  color: var(--accent);
  margin-left: 1px;
}
.msg.assistant[data-status]::before {
  content: attr(data-status) "…";
  display: block;
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
  margin-bottom: 0.25rem;
}
@keyframes blink { 50% { opacity: 0; } }

.task-card {
  align-self: flex-start; max-width: 75ch; background: var(--task);
  border: 1px solid var(--border); border-radius: 10px; padding: 1rem;
  display: flex; flex-direction: column; gap: 0.6rem;
}
.task-card .task-type { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.task-card .task-prompt { font-weight: 500; font-size: 14px; color: var(--muted); }

/* Mandarin display blocks. Stacked vertically, large hanzi prominent. */
.task-card .display-stack { display: flex; flex-direction: column; align-items: flex-start; gap: 0.15rem; padding: 0.5rem 0; }
.task-card .display-stack .hanzi { font-size: 36px; font-weight: 600; line-height: 1.15; letter-spacing: 0.02em; }
.task-card .display-stack .pinyin { font-size: 22px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 500; }
.task-card .display-stack .english { font-size: 14px; color: var(--muted); font-style: italic; }

/* Tone-coloured pinyin syllables */
.tone-1 { color: var(--tone-1); }
.tone-2 { color: var(--tone-2); }
.tone-3 { color: var(--tone-3); }
.tone-4 { color: var(--tone-4); }
.tone-5 { color: var(--tone-neutral); }

/* Markdown rendered inside assistant bubbles. Tight paragraph margins so the
   first/last paragraph hug the bubble edges. */
.msg.assistant .msg-text > p { margin: 0; }
.msg.assistant .msg-text > p + p { margin-top: 0.6em; }
.msg.assistant .msg-text strong { font-weight: 700; color: #ffffff; }
.msg.assistant .msg-text em { font-style: italic; color: var(--muted); }
.msg.assistant .msg-text code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.92em; padding: 1px 5px; border-radius: 4px;
  background: rgba(255,255,255,0.08);
}

/* <zh h="..." p="..." e="..."/> — inline teaching callout in chat prose.
   Hanzi big-and-bold, pinyin tone-coloured, english muted subtitle. Stacks
   vertically as an inline block so it sits naturally in flowing text. */
.zh-inline {
  display: inline-flex; flex-direction: column; align-items: flex-start;
  vertical-align: middle; padding: 0.05rem 0.4rem; margin: 0 0.15rem;
  border-radius: 6px; background: rgba(122, 162, 255, 0.08);
  line-height: 1.15;
}
.zh-inline .zh-hanzi { font-size: 1.45em; font-weight: 600; letter-spacing: 0.02em; }
.zh-inline .zh-pinyin {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.95em; font-weight: 500;
}
.zh-inline .zh-english { font-size: 0.78em; color: var(--muted); font-style: italic; }

/* Choices: rich card stack (hanzi big / pinyin coloured / english subtitle) when objects, plain pill when strings. */
.task-card .choices { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.task-card .choices .choice-pill {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 0.4rem 0.7rem; cursor: pointer; font: inherit;
}
.task-card .choices .choice-pill:hover { border-color: var(--accent); }
.task-card .choices .choice-card {
  /* color: var(--text) is REQUIRED — without it, the user-agent default for
     <button> elements wins (black text), which renders unreadable hanzi in
     dark mode. Tone-coloured pinyin spans set their own color via .tone-N
     and override this; english uses its own var(--muted) below. */
  color: var(--text);
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 0.55rem 0.75rem; cursor: pointer; min-width: 110px; text-align: center;
  display: flex; flex-direction: column; gap: 0.1rem;
}
.task-card .choices .choice-card:hover { border-color: var(--accent); }
.task-card .choices .choice-card .hanzi { font-size: 22px; font-weight: 600; }
.task-card .choices .choice-card .pinyin { font-size: 13px; font-family: ui-monospace, monospace; }
.task-card .choices .choice-card .english { font-size: 11px; color: var(--muted); font-style: italic; }

/* Cue pointing the learner to the bottom chat input — replaces the in-card
   answer form to avoid two-inputs-on-screen confusion. */
.task-card .answer-cue {
  font-size: 13px; color: var(--accent); font-weight: 500; padding-top: 0.2rem;
}
.task-card .reveal-row { display: flex; gap: 0.4rem; align-items: center; font-size: 12px; }
.task-card .reveal-btn { background: transparent; border: 1px dashed var(--border); color: var(--muted); border-radius: 6px; padding: 0.25rem 0.55rem; cursor: pointer; font-size: 12px; }
.task-card .reveal-btn:hover { color: var(--text); border-color: var(--accent); }
.task-card .hint { font-size: 12px; color: var(--muted); }
.task-card .hint-wrap { margin-top: 0.4rem; }
.task-card .hint-btn {
  background: transparent; border: 1px dashed var(--border);
  border-radius: 6px; padding: 0.2rem 0.55rem;
  color: var(--muted); font-size: 11px; cursor: pointer;
  font: inherit; line-height: 1.2;
}
.task-card .hint-btn:hover { border-color: var(--accent); color: var(--accent); }
.task-card .hint-text { font-size: 12px; color: var(--muted); margin-top: 0.3rem; }
.task-card .hint-text.hidden { display: none; }

#chat-form {
  display: flex; gap: 0.5rem; padding: 0.75rem; border-top: 1px solid var(--border); background: var(--panel);
  /* Add the iOS home-indicator safe-area to the bottom padding so the input
     never sits underneath the gesture bar on phones in fullscreen mode. */
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
}
#chat-form textarea { flex: 1; resize: vertical; min-height: 2.5em; max-height: 30vh; }

dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  border-radius: 12px; padding: 1.25rem;
  max-width: 640px; width: 90vw; max-height: 90vh; overflow-y: auto;
}
dialog::backdrop { background: rgba(0,0,0,0.5); }
dialog h2 { margin: 0 0 0.75rem; font-size: 1rem; }
dialog fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 0.75rem 1rem; margin: 0 0 0.75rem; }
dialog legend { font-size: 12px; color: var(--muted); padding: 0 0.4rem; }
dialog label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 13px; color: var(--muted); margin-bottom: 0.6rem; }
dialog .row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
dialog input, dialog textarea, dialog select {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 0.45rem 0.6rem; font: inherit;
}
dialog textarea { resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
dialog menu { display: flex; gap: 0.5rem; justify-content: flex-end; padding: 0; margin: 0; }
dialog menu button {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 0.4rem 0.8rem;
}
dialog menu button[value="default"] { background: var(--accent); color: var(--accent-fg); border: 0; font-weight: 600; }

/* ============================================================
   Mobile / narrow viewport
   ============================================================ */
@media (max-width: 720px) {
  /* On mobile the chat-view becomes a single column. Grid template stays
     `auto 1fr auto` (header / messages / input) so the input row stays pinned
     to the bottom of the visible viewport. */
  #chat-view { grid-template-columns: 1fr; grid-template-rows: auto 1fr auto; }
  #sidebar {
    position: fixed; inset: 0 30% 0 0; z-index: 50; transform: translateX(-100%);
    transition: transform 0.2s ease; box-shadow: 4px 0 20px rgba(0,0,0,0.5);
  }
  body.sidebar-open #sidebar { transform: translateX(0); }
  body.sidebar-open::before {
    content: ""; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 49;
  }
  #sidebar-toggle { display: inline-block; }
  /* Slimmer task card paddings + tighter typography on tiny viewports */
  .task-card { padding: 0.7rem; }
  .task-card .display-stack .hanzi { font-size: 32px; }
  .task-card .display-stack .pinyin { font-size: 19px; }
  .task-card .choices .choice-card { min-width: 90px; padding: 0.45rem 0.55rem; }
  .task-card .choices .choice-card .hanzi { font-size: 19px; }
  /* Bigger tap targets on mobile inputs */
  #chat-form { padding: 0.5rem; }
  #chat-form textarea { font-size: 16px; /* prevent iOS zoom */ }
  .msg { max-width: 95%; }
  /* Settings dialog full-screen on mobile */
  dialog#settings-dialog { max-width: 100vw; width: 100vw; max-height: 100vh; height: 100vh; border-radius: 0; }
}

/* ===== v2: language modes, in-context focus, listening tag ===== */
.lang-badge {
  font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px;
  white-space: nowrap; vertical-align: middle;
}
.lang-badge-zh { background: rgba(235, 120, 110, 0.16); color: #eb786e; }
.lang-badge-ms { background: rgba(110, 170, 235, 0.16); color: #6eaaeb; }
#new-chat-menu .menu-label {
  font-size: 11px; color: var(--muted); padding: 0.2rem 0.5rem 0.35rem; text-transform: uppercase; letter-spacing: 0.04em;
}
.task-card .display-stack .listen-tag {
  font-size: 12px; color: var(--muted); margin-bottom: 0.25rem;
}
/* focus word inside an in-context sentence */
.task-card .display-stack .hanzi .focus {
  color: var(--accent); border-bottom: 2px solid var(--accent); padding-bottom: 1px;
}
