:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --border: #e2dfd6;
  --text: #1f2328;
  --muted: #6b6f76;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --accent-soft: #e6eefe;
  --new: #2f6fed;
  --learn: #d9480f;
  --review: #2b8a3e;
  --again: #c92a2a;
  --hard: #a6620a;
  --good: #2b8a3e;
  --easy: #1c7ed6;
  --ok-bg: #e9f6ec;
  --fix-bg: #fff4e6;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 16px rgb(0 0 0 / 0.04);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #1d2024;
    --surface-2: #25292e;
    --border: #33383f;
    --text: #e8e9eb;
    --muted: #9aa0a8;
    --accent: #6b9cff;
    --accent-ink: #0d1117;
    --accent-soft: #1f2a40;
    --new: #74a3ff;
    --learn: #ff8a4c;
    --review: #5cc472;
    --again: #ff6b6b;
    --hard: #e3a447;
    --good: #5cc472;
    --easy: #5aa9f0;
    --ok-bg: #18301f;
    --fix-bg: #3a2a14;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 0.5em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
[hidden] { display: none !important; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 24px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 800; font-size: 1.15rem; color: var(--text); letter-spacing: -0.02em; }
.brand span { color: var(--accent); }
.topbar nav { display: flex; gap: 4px; align-items: center; flex: 1; }
.topbar nav > a, .menu-btn {
  color: var(--muted); padding: 6px 12px; border-radius: 999px; font-weight: 500; font-size: 0.95rem;
  border: 1px solid transparent; background: none;
}
.topbar nav > a:hover, .menu-btn:hover { color: var(--text); background: var(--surface-2); }
.topbar nav > a.active, .menu-btn.active { color: var(--text); background: var(--surface); box-shadow: var(--shadow); border-color: var(--border); }
.menu { position: relative; margin-left: auto; }
.menu-btn { gap: 6px; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 170px; padding: 6px;
  display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: 0 12px 40px rgb(0 0 0 / 0.18);
}
.menu-list a { color: var(--text); padding: 9px 12px; border-radius: 8px; font-weight: 500; }
.menu-list a:hover { background: var(--surface-2); }
.menu-list a.active { background: var(--accent-soft); color: var(--accent); }
.badge {
  display: inline-block; min-width: 1.5em; padding: 0 6px; margin-left: 4px;
  border-radius: 999px; background: var(--accent); color: var(--accent-ink);
  font-size: 0.75rem; font-weight: 700; text-align: center; line-height: 1.5;
}

main { max-width: 960px; margin: 0 auto; padding: 24px 16px 80px; }

/* generic pieces */
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow); margin-bottom: 16px;
}
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.stack > * + * { margin-top: 12px; }
.grid { display: grid; gap: 12px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.tile .num { font-size: 1.8rem; font-weight: 750; letter-spacing: -0.02em; }
.tile .lbl { color: var(--muted); font-size: 0.85rem; }

button, .btn {
  font: inherit; font-weight: 600; font-size: 0.95rem;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 16px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  transition: background 0.12s, transform 0.05s;
}
button:hover, .btn:hover { background: var(--surface-2); }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.5; cursor: default; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.primary:hover { background: color-mix(in srgb, var(--accent) 88%, black); }
.ghost { background: transparent; border-color: transparent; }
.danger { color: var(--again); }
.icon-btn { padding: 6px 9px; border-radius: 8px; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; min-height: 80px; }
label { display: block; font-weight: 600; font-size: 0.9rem; margin-bottom: 4px; }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
label.check input { width: auto; }
.field { margin-bottom: 14px; }
.fields-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 600px) { .fields-2 { grid-template-columns: 1fr; } }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 0.8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.c-new { color: var(--new); font-weight: 700; }
.c-learn { color: var(--learn); font-weight: 700; }
.c-review { color: var(--review); font-weight: 700; }
.zero { color: var(--muted); opacity: 0.6; font-weight: 500; }

.pill { display: inline-block; font-size: 0.75rem; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-weight: 600; }
.pill.new { color: var(--new); }
.pill.learning, .pill.relearning { color: var(--learn); }
.pill.review { color: var(--review); }
.pill.suspended { color: var(--hard); }

.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty .big { font-size: 2.5rem; }

/* forecast */
.bars { display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; align-items: end; height: 110px; margin-top: 8px; }
.bar { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; font-size: 0.75rem; color: var(--muted); }
.bar i { display: block; width: 100%; max-width: 36px; background: var(--review); border-radius: 6px 6px 2px 2px; min-height: 2px; opacity: 0.85; }
.bar b { color: var(--text); font-weight: 600; }

/* review */
.study { max-width: 680px; margin: 0 auto; }
.study-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; gap: 12px; }
.counts { display: flex; gap: 14px; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.counts span { padding-bottom: 2px; border-bottom: 2px solid transparent; }
.counts span.current { border-bottom-color: currentColor; }
.flashcard {
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px; box-shadow: var(--shadow);
  padding: 36px 28px; min-height: 300px; text-align: center; display: flex; flex-direction: column; justify-content: center;
}
.flashcard .deck-tag { color: var(--muted); font-size: 0.8rem; margin-bottom: 12px; }
.flashcard .face { font-size: clamp(1.6rem, 4vw, 2.3rem); font-weight: 650; letter-spacing: -0.01em; word-break: break-word; }
.flashcard .back { font-size: 1.4rem; font-weight: 550; }
.flashcard hr { border: none; border-top: 1px solid var(--border); margin: 22px 0; }
.flashcard .notes { color: var(--muted); margin-top: 12px; white-space: pre-line; }
.flashcard .typed { margin-top: 16px; }
.flashcard .typed input { text-align: center; font-size: 1.15rem; max-width: 420px; }
.typed-result { margin-top: 10px; font-size: 0.95rem; }
.typed-result.ok { color: var(--good); }
.typed-result.close { color: var(--hard); }
.typed-result.bad { color: var(--again); }
.answer-bar { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 16px; }
.answer-bar.single { grid-template-columns: 1fr; }
.answer-bar button { flex-direction: column; gap: 0; padding: 10px 6px; }
.answer-bar button small { font-weight: 500; color: var(--muted); font-size: 0.78rem; }
.answer-bar .r1 { color: var(--again); }
.answer-bar .r2 { color: var(--hard); }
.answer-bar .r3 { color: var(--good); }
.answer-bar .r4 { color: var(--easy); }
.kbd { font-size: 0.75rem; color: var(--muted); font-weight: 500; }
.study-foot { display: flex; justify-content: space-between; margin-top: 12px; }

/* scenario picker */
.scenarios { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; }
.scenario {
  text-align: left; justify-content: flex-start; align-items: flex-start; flex-direction: column; gap: 2px;
  padding: 12px 14px; font-weight: 600; min-height: 76px;
}
.scenario small { font-weight: 450; color: var(--muted); font-size: 0.8rem; line-height: 1.35; }
.scenario.selected { border-color: var(--accent); background: var(--accent-soft); }
.list-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 4px; border-bottom: 1px solid var(--border); }
.list-item:last-child { border-bottom: none; }

/* chat */
.chat { display: flex; flex-direction: column; height: calc(100vh - 140px); min-height: 480px; max-width: 760px; margin: 0 auto; }
.chat-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 8px; flex-wrap: wrap; }
.chat-head h1 { font-size: 1.25rem; margin: 0; }
.targets { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.target-word { font-size: 0.8rem; padding: 2px 10px; border-radius: 999px; border: 1px dashed var(--border); color: var(--muted); }
.target-word.used { border-style: solid; border-color: var(--good); color: var(--good); background: var(--ok-bg); }
.messages { flex: 1; overflow-y: auto; padding: 8px 4px 16px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: 85%; }
.msg.model { align-self: flex-start; }
.msg.user { align-self: flex-end; }
.bubble { padding: 10px 14px; border-radius: 16px; white-space: pre-wrap; word-break: break-word; }
.msg.model .bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; box-shadow: var(--shadow); }
.msg.user .bubble { background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 4px; }
.msg-tools { display: flex; gap: 2px; margin-top: 2px; }
.msg-tools button { padding: 2px 8px; font-size: 0.8rem; font-weight: 500; color: var(--muted); border: none; background: transparent; }
.msg-tools button:hover { color: var(--text); background: var(--surface-2); }
.translation { color: var(--muted); font-size: 0.92rem; margin: 4px 14px 0; font-style: italic; }
.vocab { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.vocab-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 0.82rem; padding: 2px 4px 2px 10px; border-radius: 999px; background: var(--surface-2); }
.vocab-chip button { padding: 0 7px; border-radius: 999px; font-size: 0.8rem; line-height: 1.6; }
.vocab-chip.added { opacity: 0.6; }
.feedback { margin-top: 6px; font-size: 0.9rem; border-radius: 12px; padding: 8px 12px; }
.feedback.ok { background: var(--ok-bg); }
.feedback.fix { background: var(--fix-bg); }
.feedback .corrected { font-weight: 600; }
.feedback ul { margin: 6px 0 0; padding-left: 18px; }
.feedback li { margin: 2px 0; }
.feedback del { color: var(--again); }
.feedback ins { color: var(--good); text-decoration: none; font-weight: 600; }
.suggestion { align-self: flex-start; font-size: 0.88rem; color: var(--muted); padding: 0 4px; }
.suggestion button { font-size: 0.88rem; font-weight: 500; padding: 3px 10px; border-style: dashed; }
.typing { align-self: flex-start; color: var(--muted); padding: 10px 14px; }
.typing span { animation: blink 1.2s infinite both; font-size: 1.4rem; line-height: 0.5; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.2; } 40% { opacity: 1; } }
.composer { display: flex; gap: 8px; align-items: flex-end; padding-top: 10px; border-top: 1px solid var(--border); }
.composer textarea { min-height: 46px; max-height: 160px; resize: none; }
.composer .mic.listening { background: var(--again); color: #fff; border-color: var(--again); }

/* reader */
.read-text { font-size: 1.15rem; line-height: 2; white-space: pre-wrap; word-break: break-word; }
.read-text .chunk.pending, .read-text .chunk.working { color: var(--muted); animation: pulse 1.6s ease-in-out infinite; }
.read-text .chunk.error { color: var(--muted); }
@keyframes pulse { 50% { opacity: 0.55; } }
.w { cursor: pointer; border-radius: 4px; text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 5px; }
.w.new { text-decoration-color: color-mix(in srgb, var(--accent) 55%, transparent); }
.w.known { text-decoration-color: color-mix(in srgb, var(--good) 60%, transparent); }
.w.minor { text-decoration-color: transparent; }
.w:hover, .w:focus-visible { background: var(--accent-soft); outline: none; }
.w.sel { background: var(--accent-soft); box-shadow: 0 0 0 2px var(--accent-soft); }
.read-legend { margin: 14px 0 0; }
.read-legend .w { cursor: default; margin-right: 8px; }
.chunk-retry { font-size: 0.8rem; padding: 1px 8px; margin: 0 6px; vertical-align: middle; }
.read-tr { white-space: pre-wrap; color: var(--muted); }
button.small { font-size: 0.82rem; padding: 4px 10px; }
.word-list { display: grid; }
.word-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 4px; border-bottom: 1px solid var(--border); }
.word-row:last-child { border-bottom: none; }
.word-row.known .wr-lemma { color: var(--good); }
.wr-main { min-width: 0; }
.wr-lemma { font-size: 1.05rem; font-weight: 700; padding: 0; border: none; background: none; color: var(--text); margin-right: 6px; }
.wr-lemma:hover { background: none; text-decoration: underline; }
.wr-meaning { font-size: 0.92rem; }
.wr-actions, .pop-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; flex-wrap: wrap; justify-content: flex-end; }
.in-deck { font-size: 0.82rem; color: var(--good); font-weight: 600; }
.word-pop {
  position: absolute; z-index: 50; width: min(340px, calc(100vw - 32px));
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.2); padding: 14px 16px;
}
.word-pop.sheet { position: fixed; left: 8px !important; right: 8px; bottom: 8px; top: auto !important; width: auto; padding-bottom: 18px; }
.pop-x { position: absolute; top: 6px; right: 6px; color: var(--muted); }
.pop-word { font-size: 1.3rem; padding-right: 28px; }
.pop-meaning { font-size: 1.05rem; font-weight: 600; margin: 4px 0 2px; }
.pop-sentence { font-size: 0.9rem; color: var(--muted); font-style: italic; margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
.pop-actions { justify-content: flex-start; margin-top: 12px; }

/* dialogs */
dialog {
  border: 1px solid var(--border); border-radius: 18px; padding: 0; background: var(--surface); color: var(--text);
  width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 48px); box-shadow: 0 20px 60px rgb(0 0 0 / 0.25);
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
.dlg { padding: 22px; overflow-y: auto; max-height: calc(100vh - 48px); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.gen-list { max-height: 50vh; overflow-y: auto; border: 1px solid var(--border); border-radius: 12px; }
.gen-item { display: flex; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--border); align-items: flex-start; }
.gen-item:last-child { border-bottom: none; }
.gen-item input { width: auto; margin-top: 5px; }
.gen-item .notes { color: var(--muted); font-size: 0.85rem; white-space: pre-line; }

/* toasts */
#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 100; align-items: center; }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 0.92rem; box-shadow: 0 6px 24px rgb(0 0 0 / 0.2); max-width: 90vw; }
.toast.error { background: var(--again); color: #fff; }

.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 0.7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 600px) {
  .topbar { gap: 8px; padding: 8px 12px; }
  .topbar nav { gap: 0; }
  .topbar nav > a, .menu-btn { padding: 6px 10px; }
  .flashcard { padding: 28px 16px; min-height: 240px; }
  .answer-bar { gap: 6px; }
  .msg { max-width: 94%; }
  .hide-sm { display: none; }
}

/* login */
body.logged-out .topbar nav { display: none; }
.login { max-width: 380px; margin: 10vh auto 0; padding: 32px 28px; }
.login-brand { font-weight: 800; font-size: 1.6rem; letter-spacing: -0.02em; margin-bottom: 4px; }
.login-brand span { color: var(--accent); }
.login-error { color: var(--again); font-size: 0.9rem; margin: 0 0 12px; }
