:root {
  --econ: #7b1113;          /* The Economist red */
  --econ-dark: #5a0c0e;
  --ink: #1a1a1a;
  --paper: #f7f4ee;
  --card: #ffffff;
  --muted: #6b6b6b;
  --line: #e2ddd3;
  --accent: #0e6e6e;
  --zh: #14324b;
  --max: 760px;
  --scale: 1;               /* text-size preference multiplier */
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }   /* beats explicit display: on .reveal-controls, .chat, etc. */
html, body { margin: 0; }
body {
  font-family: Georgia, "Times New Roman", serif;
  color: var(--ink);
  background: var(--paper);
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}
.zh { font-family: "PingFang SC", "PingFang TC", "Microsoft YaHei", "Noto Sans SC", "Noto Sans TC", sans-serif; color: var(--zh); }

/* Header */
.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px;
  background: var(--econ); color: #fff;
  padding-top: max(10px, env(safe-area-inset-top));
}
.brand {
  display: flex; align-items: center; gap: 8px;
  background: none; border: 0; color: #fff; cursor: pointer;
  font-family: inherit; font-size: 20px; font-weight: bold; letter-spacing: .3px;
}
.brand img { display: block; }
/* min-width:0 lets the row shrink; the day-select is the only flexible child, so it
   absorbs the squeeze on narrow phones and the header never overflows the viewport. */
.top-right { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1 1 auto; justify-content: flex-end; }
.brand { flex: 0 0 auto; }
#daySelect {
  font-family: inherit; font-size: 14px; padding: 5px 8px;
  border-radius: 6px; border: 0; background: #fff; color: var(--ink);
  flex: 0 1 auto; min-width: 0; max-width: 62vw;
}
.icon-btn {
  flex: 0 0 auto;
  font-family: inherit; font-size: 13px; cursor: pointer;
  background: rgba(255,255,255,.15); color: #fff;
  border: 1px solid rgba(255,255,255,.35); border-radius: 6px; padding: 5px 9px;
}
@media (max-width: 470px) {
  .top { gap: 8px; }
  .brand { font-size: 17px; }
  .top-right { gap: 5px; }
  #daySelect { font-size: 12px; padding: 5px 6px; }
  .icon-btn { padding: 5px 7px; }
}
.icon-btn:hover { background: rgba(255,255,255,.28); }
#reviewBtn { position: relative; }
/* Count of cards due, riding the top-right corner of the Review button. Amber so it
   reads against the red header (a red badge would vanish into it). */
.due-badge {
  position: absolute; top: -6px; right: -6px;
  min-width: 17px; height: 17px; padding: 0 4px;
  border-radius: 9px; background: #f5a623; color: #3a2600;
  font-family: Georgia, serif; font-size: 11px; font-weight: bold;
  line-height: 17px; text-align: center; box-shadow: 0 1px 3px rgba(0,0,0,.3);
}

/* Layout */
main { max-width: var(--max); margin: 0 auto; padding: 18px 16px 64px; }
.view { animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }
h1 { font-size: 26px; margin: 6px 0 4px; }
.meta { color: var(--muted); font-size: 14px; margin: 0 0 16px; }
.back {
  background: none; border: 0; color: var(--econ); cursor: pointer;
  font-family: inherit; font-size: 15px; padding: 6px 0; margin-bottom: 6px;
}

/* Story list */
.stories { list-style: none; margin: 0; padding: 0; counter-reset: s; }
.stories li {
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  padding: 14px 16px 14px 52px; margin-bottom: 12px; position: relative; cursor: pointer;
  transition: box-shadow .15s, transform .05s;
}
.stories li:hover { box-shadow: 0 3px 14px rgba(0,0,0,.09); }
.stories li:active { transform: scale(.997); }
.stories li::before {
  counter-increment: s; content: counter(s);
  position: absolute; left: 14px; top: 14px;
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--econ); color: #fff; font-size: 14px;
  display: flex; align-items: center; justify-content: center; font-family: Georgia, serif;
}
.st-primary { font-size: calc(18px * var(--scale)); font-weight: bold; }
.st-secondary { font-size: calc(15px * var(--scale)); color: var(--muted); margin-top: 3px; }
.st-secondary.zh { color: var(--zh); }

/* Reader */
#storyTitleEn { font-size: calc(23px * var(--scale)); margin: 4px 0 2px; }
.title-zh { margin: 0 0 14px; font-size: calc(17px * var(--scale)); }

.steps { display: flex; flex-wrap: wrap; gap: 6px; margin: 14px 0 4px; }
.step {
  font-family: inherit; font-size: 14px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 20px; padding: 7px 13px;
}
.step.active { background: var(--econ); color: #fff; border-color: var(--econ); }
/* Ask Socrates: the reward step, pushed to the right end of the row. */
.step-ai { margin-left: auto; border-color: var(--accent); color: var(--accent); }
.step-ai.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.step-ai.locked { color: var(--muted); border-color: var(--line); cursor: default; opacity: .7; }
.hint { color: var(--muted); font-size: 14px; font-style: italic; margin: 8px 0 4px; min-height: 20px; }

.reveal-controls { display: flex; gap: 8px; margin: 6px 0 10px; }
.mini {
  font-family: inherit; font-size: 12px; cursor: pointer; color: var(--muted);
  background: none; border: 1px solid var(--line); border-radius: 6px; padding: 4px 9px;
}
.mini:hover { color: var(--ink); }

/* Sentences */
.sentences { list-style: none; margin: 0; padding: 0; }
.sentences li { border-bottom: 1px solid var(--line); padding: 12px 0; }
.s-line {
  display: flex; align-items: flex-start; gap: 8px;
  font-size: calc(18px * var(--scale)); line-height: 1.6;
}
.tok-wrap { flex: 1; }
.tok { cursor: pointer; border-radius: 3px; }
.tok:hover { background: rgba(14,110,110,.12); }
.tok.looking { background: rgba(14,110,110,.22); box-shadow: 0 1px 0 var(--accent); }
.spk {
  flex: none; cursor: pointer; background: none; border: 0; padding: 2px;
  font-size: calc(15px * var(--scale)); line-height: 1.4; opacity: .5;
  filter: grayscale(1);
}
.spk:hover { opacity: 1; filter: none; }

.show-ans {
  margin-top: 8px; font-family: inherit; font-size: 13px; cursor: pointer;
  background: none; border: 1px solid var(--accent); color: var(--accent);
  border-radius: 6px; padding: 4px 11px;
}
.show-ans:hover { background: rgba(14,110,110,.08); }
.answer-line { margin-top: 8px; }
.answer-line .s-line { color: var(--ink); }

/* Word-lookup popover */
.lookup-pop {
  position: fixed; z-index: 40; max-width: min(320px, 88vw);
  background: var(--card); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0,0,0,.22); padding: 10px 12px;
  font-family: Georgia, serif; animation: fade .12s ease;
}
.lp-word { font-size: 17px; font-weight: bold; }
.lp-read { font-weight: normal; font-style: italic; color: var(--accent); margin-left: 8px; font-size: 14px; }
.lp-gloss { font-size: 15px; margin-top: 3px; color: var(--muted); }
/* Add-to-review toggle inside the lookup popover. */
.lp-add {
  margin-top: 9px; font-family: inherit; font-size: 13px; cursor: pointer;
  background: none; border: 1px solid var(--accent); color: var(--accent);
  border-radius: 7px; padding: 5px 11px;
}
.lp-add:hover { background: rgba(14,110,110,.08); }
.lp-add.in { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Ask Socrates — its own page, shown when the Socrates step is active. */
.socratic { margin-top: 10px; }
.ask {
  font-family: inherit; font-size: 15px; cursor: pointer; width: 100%;
  background: var(--card); border: 1px dashed var(--accent); color: var(--accent);
  border-radius: 10px; padding: 12px;
}
.chat { margin-top: 12px; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.chat-log { padding: 12px; max-height: 46vh; overflow-y: auto; background: var(--card); }
.bubble { padding: 9px 12px; border-radius: 12px; margin: 6px 0; max-width: 88%; font-size: 15px; }
.bubble.ai { background: #eef3f3; color: var(--ink); border-bottom-left-radius: 3px; }
.bubble.me { background: var(--econ); color: #fff; margin-left: auto; border-bottom-right-radius: 3px; }
.bubble.think { color: var(--muted); font-style: italic; background: #eef3f3; }
.bubble-spk { margin-left: 6px; font-size: 14px; vertical-align: baseline; }
.chat-form { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--line); background: var(--paper); }
.chat-form input {
  flex: 1; font-family: inherit; font-size: 15px; padding: 9px 11px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--ink);
}
.chat-form button {
  font-family: inherit; font-size: 15px; cursor: pointer;
  background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 9px 15px;
}

/* Review (flashcards) */
.review-start { text-align: center; padding: 24px 0; }
.review-msg { font-size: 16px; color: var(--muted); margin: 0 0 18px; }
#reviewBegin { max-width: 360px; margin: 0 auto; }
/* "Study anyway" — a quieter, secondary action beneath the caught-up message. */
.review-practice {
  display: block; margin: 4px auto 0; max-width: 360px;
  font-family: inherit; font-size: 15px; cursor: pointer;
  background: transparent; color: var(--accent);
  border: 1px solid var(--accent); border-radius: 10px; padding: 11px 16px;
}
.review-practice:hover { background: rgba(14,110,110,.09); }
/* A real flashcard: a tall, centred card. Word big and centred on the front; the answer
   centred beneath a divider once flipped. */
.review-card {
  display: flex; flex-direction: column;
  min-height: 46vh;
  background: var(--card); border: 1px solid var(--line); border-radius: 16px;
  padding: 16px 20px 20px; margin-top: 10px;
  box-shadow: 0 10px 34px rgba(0,0,0,.13);
}
.card-progress {
  color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums;
  text-align: right; letter-spacing: .3px;
}
.card-face { line-height: 1.5; }
/* Front: the word alone, large and centred, filling the card's height. */
#cardFront {
  flex: 1; display: flex; align-items: center; justify-content: center;
  text-align: center; padding: 18px 0 8px;
}
#cardFront .s-line {
  justify-content: center; align-items: center; gap: 12px;
  font-size: calc(42px * var(--scale)); font-weight: bold; line-height: 1.25;
}
#cardFront .spk { font-size: calc(22px * var(--scale)); }
/* Back: reading → meaning → the example sentence it came from, all centred. */
.card-back { margin-top: 4px; padding-top: 18px; border-top: 1px dashed var(--line); text-align: center; }
.card-reading { font-style: italic; color: var(--accent); font-size: calc(18px * var(--scale)); }
.card-gloss { font-size: calc(23px * var(--scale)); margin-top: 4px; font-weight: 600; }
.card-ex { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
.card-ex.s-line { justify-content: center; font-size: calc(16px * var(--scale)); color: var(--muted); font-style: italic; }
/* Show-answer: a clear, centred flip button (not the dashed Socrates style). */
#reviewCard #cardShow {
  margin: 6px auto 0; max-width: 320px;
  background: var(--accent); color: #fff; border: 0; border-radius: 10px;
}
#reviewCard #cardShow:hover { filter: brightness(1.08); }
/* Grade buttons: Again (needs work) → Easy (nailed it), red-amber-green. */
.grades { display: flex; gap: 8px; margin-top: 16px; }
.grade {
  flex: 1; font-family: inherit; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  border-radius: 8px; padding: 10px 6px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink);
}
.g-lab { font-size: 15px; }
/* The Anki-style interval each grade will schedule (e.g. "1 day", "4 days"). */
.g-when { font-size: 11px; opacity: .7; font-variant-numeric: tabular-nums; }
.grade.again { border-color: var(--econ); color: var(--econ); }
.grade.again:hover { background: rgba(123,17,19,.08); }
.grade.good { border-color: #b8860b; color: #8a6400; }
.grade.good:hover { background: rgba(184,134,11,.1); }
.grade.easy { border-color: var(--accent); color: var(--accent); }
.grade.easy:hover { background: rgba(14,110,110,.1); }

/* Settings */
.setting { padding: 14px 0; border-bottom: 1px solid var(--line); }
.s-label { font-size: 17px; font-weight: bold; margin-bottom: 8px; }
.set-version { color: var(--muted); font-size: 12px; text-align: center; margin: 24px 0 4px; letter-spacing: .3px; }
.seg { display: inline-flex; flex-wrap: wrap; gap: 6px; }
.s-row { margin-top: 10px; }
.seg button {
  font-family: inherit; font-size: 14px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 8px; padding: 8px 14px;
}
.seg button.on { background: var(--econ); color: #fff; border-color: var(--econ); }
.s-rate { display: flex; align-items: center; gap: 12px; }
.s-rate input[type=range] { flex: 1; max-width: 260px; accent-color: var(--econ); }
.rate-val { font-variant-numeric: tabular-nums; min-width: 44px; }

/* Stats */
.st-section-h { font-size: 15px; font-weight: bold; color: var(--econ); margin: 22px 0 6px; letter-spacing: .3px; }
.stats-cards { display: grid; gap: 12px; }
.st-nums { display: grid; grid-template-columns: repeat(auto-fit, minmax(80px, 1fr)); gap: 10px; text-align: center; }
.st-num { display: flex; flex-direction: column; gap: 2px; }
.st-num b { font-size: 24px; font-variant-numeric: tabular-nums; color: var(--ink); }
.st-num span { font-size: 12px; color: var(--muted); }
#sendTeacher { margin-top: 22px; }
#sentMsg { text-align: center; margin: 10px 0 0; color: var(--accent); }
.stat-card {
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px;
}
.stat-row { display: flex; justify-content: space-between; align-items: baseline; padding: 5px 0; }
.stat-row .label { font-size: 16px; }
.stat-row .vals { font-variant-numeric: tabular-nums; }
.stat-row .today { font-weight: bold; font-size: 17px; }
.stat-row .all { color: var(--muted); font-size: 13px; margin-left: 8px; }

/* About */
.about p { margin: 0 0 14px; }
.about p.zh { margin-top: -8px; font-size: 15px; }
.about ol { padding-left: 22px; }
.about li { margin: 4px 0; }
.about .credit { margin-top: 20px; padding-top: 14px; border-top: 1px solid var(--line); color: var(--muted); font-size: 15px; }

.empty { color: var(--muted); font-style: italic; padding: 30px 0; text-align: center; }

@media (prefers-color-scheme: dark) {
  :root {
    --ink: #ececec; --paper: #171514; --card: #211e1c; --muted: #9a938c;
    --line: #38332f; --zh: #cfe3ef;
  }
  #daySelect { background: #2b2724; color: var(--ink); }
  .bubble.ai, .bubble.think { background: #2a2f2f; }
}
