:root {
  --bg: #faf8f4;
  --card: #ffffff;
  --ink: #1a1a1a;
  --muted: #6b6b6b;
  --line: #e7e3db;
  --accent: #b33a3a;
  --accent-soft: #f4d8d8;
  --good: #2f8c4a;
  --warn: #d28a00;
  --bad: #c44545;
  --easy: #3578c4;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang TC",
    "Heiti TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.4;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: #fff;
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 {
  font-size: 18px;
  margin: 0;
  color: var(--accent);
  letter-spacing: 0.5px;
}
.topbar nav { display: flex; gap: 4px; }
.tab {
  background: transparent;
  border: 1px solid transparent;
  padding: 6px 14px;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
  color: var(--muted);
}
.tab.active {
  color: var(--ink);
  background: var(--accent-soft);
  border-color: var(--accent);
}

main { max-width: 880px; margin: 0 auto; padding: 20px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.screen { display: block; }
.screen.hidden { display: none !important; }

.who { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.who-profile { color: var(--ink); font-weight: 500; }

.auth-card { max-width: 460px; margin: 60px auto; }
.auth-card h2 { margin-bottom: 8px; }
.auth-tabs { display: flex; gap: 4px; margin-bottom: 18px; border-bottom: 1px solid var(--line); }
.auth-tab {
  flex: 1;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 10px 14px;
  font: inherit;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  border-radius: 0;
}
.auth-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.auth-card button.primary { width: 100%; padding: 12px; margin-top: 18px; }
label.block { display: block; margin: 14px 0 6px; font-size: 12px; color: var(--muted); }
label.block input {
  display: block;
  width: 100%;
  padding: 10px;
  font-size: 15px;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-top: 4px;
}
.auth-status { margin-top: 16px; min-height: 1.5em; font-size: 13px; }

.profiles-list { display: flex; flex-direction: column; gap: 10px; margin: 16px 0; }
.profile-row { display: flex; gap: 10px; align-items: center; }
.profile-pick {
  flex: 1;
  text-align: left;
  padding: 14px 18px;
  font-size: 18px;
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--ink);
}
.profile-pick:hover { filter: brightness(0.97); }
.sep { border: none; border-top: 1px solid var(--line); margin: 24px 0 12px; }
.inline-form { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.inline-form input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; font-size: 14px; }
.inline-form label { display: flex; gap: 6px; align-items: center; font-size: 12px; color: var(--muted); }
.inline-form select { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 12px; font-size: 16px; }

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  margin-bottom: 16px;
}
.controls label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: var(--muted);
  gap: 4px;
}
.controls select, .controls input {
  font-size: 14px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

button {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--line);
  background: #fff;
  padding: 8px 16px;
  border-radius: 8px;
}
button.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
button.primary:hover { filter: brightness(1.05); }
button.ghost {
  background: transparent;
  color: var(--muted);
}
button:disabled { opacity: 0.5; cursor: default; }

/* ----------- review card ----------- */

#card {
  min-height: 320px;
  max-height: calc(100vh - 170px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
/* Card content fills the card; examples are the only region that scrolls, so
   the prompt and action buttons stay visible at any viewport height. */
#card-content {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}
#card-content > #prompt { flex: 0 0 auto; }
#card-content > #reveal-btn { flex: 0 0 auto; }
#card-content > #answer { flex: 0 0 auto; }
#prompt-examples { flex: 0 1 auto; min-height: 0; overflow-y: auto; }

.prompt {
  font-size: 64px;
  text-align: center;
  padding: 12px 10px 6px;
  line-height: 1.1;
  letter-spacing: 4px;
}
.prompt.small { font-size: 44px; letter-spacing: 1px; }
.prompt .sub {
  display: block;
  font-size: 20px;
  color: var(--muted);
  margin-top: 4px;
  letter-spacing: 1px;
}

/* ---- session: start screen, mode badge, live timer ---- */
.session-idle {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  text-align: center;
  padding: 40px 20px;
}
.session-idle #start-btn { min-width: 220px; padding: 14px; font-size: 16px; }
.session-idle .hint { max-width: 380px; }

.mode-badge {
  display: inline-block;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.5px;
  padding: 5px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
}
.mode-badge.reading { background: #eaf2fb; color: var(--easy); border-color: #cfe0f4; }
.mode-badge.writing { background: var(--accent-soft); color: var(--accent); border-color: #ecc9c9; }

.session-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 14px;
  margin-bottom: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fcfbf8;
}
.session-bar .mode-task { font-size: 13px; }
.timer {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  font-size: 26px;
  font-weight: 600;
  letter-spacing: 1px;
  min-width: 74px;
  text-align: right;
}
.session-controls { display: flex; gap: 8px; }
.paused-mark { font-size: 34px; margin-bottom: 4px; letter-spacing: 2px; }

#reveal-btn {
  display: block;
  margin: 12px auto;
  min-width: 200px;
}

.answer { margin-top: 4px; }
.answer-row {
  display: grid;
  grid-template-columns: 80px 1fr;
  gap: 12px;
  padding: 6px 0;
  border-top: 1px dashed var(--line);
  align-items: baseline;
}
.answer-row .label { color: var(--muted); font-size: 13px; }
.answer-row .big { font-size: 58px; line-height: 1; }
.answer-row .med { font-size: 28px; }
.readings-list { display: flex; flex-direction: column; gap: 10px; }
.reading-row {
  display: flex;
  align-items: baseline;
  gap: 18px;
  padding: 6px 0;
}
.reading-row.alt {
  border-top: 1px dotted var(--line);
  padding-top: 10px;
}
.alt-tag {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 1px;
  text-transform: uppercase;
  min-width: 32px;
}
.reading-row.primary .alt-tag { display: none; }
.zhuyin-block {
  font-size: 28px;
  color: var(--accent);
  letter-spacing: 4px;
  font-weight: 500;
  min-width: 4em;
}
.pinyin-block {
  font-size: 22px;
  font-style: italic;
  color: var(--ink);
  letter-spacing: 0.5px;
}
.reading-row.alt .zhuyin-block { color: var(--ink); font-weight: 400; }
.reading-row.alt .pinyin-block { color: var(--muted); }

.multi-hint {
  font-size: 12px;
  color: var(--muted);
  margin-top: 6px;
  letter-spacing: 1px;
  font-style: italic;
}
.examples {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 10px 28px;
}

.phrase-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
}
/* Sentences get their own full-width line; short phrases pack side by side. */
.phrase-row.sentence { gap: 8px; flex-basis: 100%; }

.char-cell {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.15;
  min-width: 1.4em;
}
.char-cell .anno-pinyin {
  font-size: 11px;
  color: var(--muted);
  font-style: italic;
  letter-spacing: 0.5px;
  height: 14px;
}
.char-cell .anno-zhuyin {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 1px;
  height: 15px;
}
.char-cell .char {
  font-size: 24px;
  line-height: 1;
}
.phrase-row.sentence .char-cell .char { font-size: 22px; }
.phrase-row.sentence .char-cell .anno-pinyin,
.phrase-row.sentence .char-cell .anno-zhuyin {
  font-size: 11px;
}

.char-cell .focal { color: var(--accent); font-weight: 600; }
.char-cell .blank {
  display: inline-block;
  min-width: 1em;
  border-bottom: 3px solid var(--accent);
  color: transparent;
}

.examples-block {
  display: grid;
  grid-template-columns: 48px 1fr;
  gap: 12px;
  margin: 4px 16px 8px;
  padding: 8px 0;
  border-top: 1px dashed var(--line);
  align-items: start;
}
.examples-block .label { color: var(--muted); font-size: 13px; }

.grade-buttons {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-top: 14px;
}
.grade {
  padding: 16px 4px;
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  border: none;
}
.grade.wrong { background: var(--bad); }
.grade.right { background: var(--good); }

.hint { text-align: center; margin-top: 12px; font-size: 12px; }

.empty { text-align: center; padding: 60px 20px; color: var(--muted); }
.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ----------- forms ----------- */

.form-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.form-grid label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: var(--muted);
  gap: 4px;
}
.form-grid input {
  font-size: 14px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
}
.form-grid button[type="submit"] {
  grid-column: 1 / -1;
  justify-self: start;
}

.form-row {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.form-row label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: var(--muted);
  gap: 4px;
}
.form-row input {
  font-size: 14px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

textarea {
  width: 100%;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 13px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 10px;
}

/* ----------- tables ----------- */

table { width: 100%; border-collapse: collapse; }
th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
}
th { font-weight: 600; color: var(--muted); font-size: 12px; text-transform: uppercase; }
td.actions { text-align: right; }
td .icon-btn {
  border: none;
  background: transparent;
  font-size: 16px;
  padding: 4px 8px;
  color: var(--muted);
}
td .icon-btn.danger:hover { color: var(--bad); }

code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  background: var(--accent-soft);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 12px;
}

/* ----------- story mode ----------- */

.story-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.story-body {
  font-size: 22px;
  line-height: 1.9;
  white-space: pre-wrap;
  margin: 16px 0 28px;
  padding: 18px;
  background: #fefcf7;
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  letter-spacing: 1px;
}

.quiz-form { display: flex; flex-direction: column; gap: 22px; margin: 16px 0; }
.quiz-question { display: flex; flex-direction: column; gap: 6px; }
.quiz-prompt { font-size: 16px; line-height: 1.4; }
.quiz-choice {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  font-size: 15px;
}
.quiz-choice:hover { background: var(--accent-soft); }
.quiz-choice input { margin: 0; }
.quiz-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }

.result-score { font-size: 18px; margin: 4px 0 14px; }
.result-detail { display: flex; flex-direction: column; gap: 14px; margin-bottom: 16px; }
.quiz-detail { padding: 10px 12px; border-radius: 6px; border-left: 3px solid var(--line); }
.quiz-detail.q-correct { border-left-color: var(--good); background: #f4fbf4; }
.quiz-detail.q-wrong { border-left-color: var(--bad); background: #fdf4f4; }
.quiz-detail .mark { float: right; font-weight: 600; }

/* ============================ responsive ============================ */

/* Small laptops / tablets: viewport-fit cap still applies; just tighten space. */
@media (max-width: 860px) {
  main { padding: 16px; }
  .card { padding: 16px; }
}

/* Phones: let the page scroll instead of capping the card, stack the controls,
   wrap the topbar, and scale down the large display type. */
@media (max-width: 600px) {
  main { padding: 12px; }
  .card { padding: 14px; margin-bottom: 12px; overflow-x: auto; }

  /* Topbar wraps: title + account on row 1, nav centered on its own row. */
  .topbar { flex-wrap: wrap; gap: 8px; padding: 10px 12px; }
  .topbar h1 { font-size: 16px; }
  #main-nav { order: 3; width: 100%; justify-content: center; }
  .tab { padding: 6px 12px; }
  .who { font-size: 12px; gap: 6px; }

  /* Controls: Mode full width, Grade + Lesson share a row, Reload full width. */
  .controls { gap: 8px 10px; }
  .controls label { gap: 2px; }
  .controls > label:first-child,
  #reload-card { flex: 1 1 100%; }
  .controls > label:nth-child(2),
  .controls > label:nth-child(3) { flex: 1 1 calc(50% - 5px); }
  .controls select, .controls input { width: 100%; }

  /* The card grows naturally; the whole page scrolls on small screens. */
  #card { max-height: none; overflow: visible; }
  #prompt-examples { overflow: visible; }

  /* Session bar: badge + timer + buttons on row 1, task on its own line. */
  .session-bar { padding: 8px 10px; gap: 6px 10px; }
  .session-idle { padding: 28px 16px; }
  .mode-badge { font-size: 12px; padding: 4px 10px; }
  .session-bar .mode-task { flex: 1 1 100%; order: 5; font-size: 12px; }
  .timer { font-size: 22px; min-width: 56px; }

  /* Display type. */
  .prompt { font-size: 56px; padding: 10px 6px 6px; }
  .prompt.small { font-size: 38px; }
  .prompt .sub { font-size: 18px; }
  .char-cell .char { font-size: 22px; }
  .phrase-row.sentence .char-cell .char { font-size: 20px; }
  .examples-block { grid-template-columns: 30px 1fr; margin: 4px 0 8px; }

  /* Answer block. */
  .answer-row { grid-template-columns: 60px 1fr; gap: 10px; }
  .answer-row .big { font-size: 48px; }
  .answer-row .med { font-size: 22px; }
  .zhuyin-block { font-size: 24px; min-width: 3em; }
  .pinyin-block { font-size: 18px; }
  .grade { padding: 14px 4px; font-size: 14px; }

  /* Wide admin table scrolls horizontally instead of squishing. */
  #words-table { min-width: 560px; }

  /* Story + forms. */
  .story-body { font-size: 19px; line-height: 1.8; padding: 14px; }
  .form-grid { grid-template-columns: 1fr; }
}

/* Large displays (big monitors — not laptops): scale the practice content up
   and widen the card to use the extra space. Gated on height too, so short
   laptops keep the compact, viewport-fitting layout. */
@media (min-width: 1200px) and (min-height: 1000px) {
  main { max-width: 1120px; }

  .prompt { font-size: 108px; padding: 18px 10px 8px; }
  .prompt.small { font-size: 72px; }
  .prompt .sub { font-size: 28px; }

  .examples { gap: 14px 32px; }
  .char-cell .char { font-size: 34px; }
  .phrase-row.sentence .char-cell .char { font-size: 28px; }
  .char-cell .anno-pinyin,
  .char-cell .anno-zhuyin { font-size: 12px; height: 15px; }

  .answer-row .big { font-size: 84px; }
  .answer-row .med { font-size: 30px; }
  .zhuyin-block { font-size: 34px; }
  .pinyin-block { font-size: 24px; }
}
