/* Cantonese Finance Learner — clean, premium, light + dark. */
:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #f0f0f2;
  --text: #1b1b1f;
  --muted: #6b6b73;
  --line: #e3e3e8;
  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.12);
  --ok: #2ea043;
  --ok-soft: rgba(46, 160, 67, 0.16);
  --bad: #e5484d;
  --bad-soft: rgba(229, 72, 77, 0.14);
  --warn: #b7791f;
  --radius: 16px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 8px 24px rgba(0, 0, 0, 0.05);
  --cjk: "PingFang HK", "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK HK",
    "Microsoft YaHei", -apple-system, system-ui, sans-serif;
}
:root[data-theme="dark"] {
  --bg: #0d0d0f;
  --surface: #17171a;
  --surface-2: #202024;
  --text: #f2f2f4;
  --muted: #9a9aa3;
  --line: #2a2a30;
  --accent: #4a9eff;
  --accent-soft: rgba(74, 158, 255, 0.16);
  --ok: #3fb950;
  --ok-soft: rgba(63, 185, 80, 0.18);
  --bad: #ff6b6f;
  --bad-soft: rgba(255, 107, 111, 0.16);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 12px 32px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }
/* Ensure the `hidden` attribute always wins, even on flex/grid elements. */
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 12px clamp(16px, 5vw, 48px) 6px;
  max-width: 1180px;
  margin: 0 auto;
}
.brand h1 { font-size: clamp(20px, 3vw, 26px); font-weight: 650; margin: 0; letter-spacing: -0.02em; }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
.top-controls { display: flex; align-items: center; gap: 10px; }

.lang-switch { display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.lang-btn {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  padding: 5px 12px; border-radius: 999px; font-size: 14px; font-weight: 550;
}
.lang-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.icon-btn {
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  width: 38px; height: 38px; border-radius: 999px; cursor: pointer; font-size: 16px;
}

.wrap { max-width: 1180px; margin: 0 auto; padding: 6px clamp(16px, 5vw, 48px) 16px; }
.section-title { font-size: 15px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 600; margin: 18px 0 12px; }
.banner {
  background: var(--accent-soft); color: var(--accent); border-radius: 12px;
  padding: 10px 14px; font-size: 14px; font-weight: 500; margin: 8px 0 4px;
}

/* Article cards */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.card {
  text-align: left; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 20px; cursor: pointer; color: inherit;
  box-shadow: var(--shadow); transition: transform 0.14s ease, border-color 0.14s ease;
}
.card:hover { transform: translateY(-2px); border-color: var(--accent); }
.card-title { font-family: var(--cjk); font-size: 21px; font-weight: 600; margin: 0 0 14px; line-height: 1.4; }
.card-meta { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; }
.card .warn { margin-top: 10px; color: var(--warn); font-size: 12px; }

/* Reader */
.reader-head { display: flex; align-items: center; gap: 16px; margin: 6px 0 12px; }
.text-btn { border: 0; background: none; color: var(--accent); cursor: pointer; font-size: 15px; font-weight: 550; padding: 0; white-space: nowrap; }
.reader-titlewrap { flex: 1; min-width: 0; }
.reader-title { font-family: var(--cjk); font-size: clamp(20px, 3vw, 27px); margin: 0; font-weight: 650; }
.reader-sub { display: flex; gap: 12px; align-items: center; color: var(--muted); font-size: 13px; }
.orig-link { color: var(--accent); text-decoration: none; }
.orig-link:hover { text-decoration: underline; }
.counter { color: var(--muted); font-size: 13px; white-space: nowrap; }

.panes { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 720px) { .panes { grid-template-columns: 1fr; } }
.pane { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.pane-label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.09em; color: var(--muted); font-weight: 600; padding: 12px 20px; border-bottom: 1px solid var(--line); background: var(--surface-2); }
/* Both panes share the SAME base character size and line spacing so the
   written (left) and spoken (right) columns read as a matched pair. The ruby
   jyutping on the right needs the taller line-height, so the left uses it too. */
.pane-body { padding: 14px 20px; max-height: 34vh; overflow-y: auto; font-family: var(--cjk); line-height: 2.0; }
.para { margin: 0 0 10px; }

.sentence { cursor: pointer; border-radius: 8px; transition: background 0.12s ease, opacity 0.12s ease; }
.formal .sentence, .colloquial .sentence { font-size: 22px; opacity: 0.5; }
.sentence.current { opacity: 1; background: var(--accent-soft); padding: 2px 4px; }
.formal .sentence.current { font-size: 26px; }
.colloquial .sentence.current .cc { font-size: 26px; }

/* Written pane: consistent ~0.35em gap between characters (matches the pitch of
   the spoken pane's fixed-width cells). */
.pane-body.formal { letter-spacing: 0.35em; }

/* Spoken pane: each character is a uniform fixed-width cell with the jyutping
   centred above it, so gaps stay consistent no matter how long the jyutping is. */
.cc-sentence { display: inline; }
.cc {
  display: inline-flex; flex-direction: column; align-items: center;
  width: 1.35em; font-size: 22px; vertical-align: top; text-align: center;
}
.cc .jp { font-size: 0.4em; line-height: 1.25; height: 1.3em; color: var(--muted); font-weight: 500; white-space: nowrap; }
.cc .hz { line-height: 1.15; }
.cc.ok .hz { color: var(--ok); background: var(--ok-soft); border-radius: 4px; }
/* Homophone match (right sound, wrong character) — amber, still counts. */
.cc.soft .hz { color: var(--warn); background: rgba(183, 121, 31, 0.16); border-radius: 4px; }
.cc.bad .hz { color: var(--bad); background: var(--bad-soft); border-radius: 4px; }

/* Feedback */
.feedback { margin: 18px 0 4px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 18px; box-shadow: var(--shadow); }
.score-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.score { font-weight: 700; padding: 4px 12px; border-radius: 999px; }
.score.good { color: var(--ok); background: var(--ok-soft); }
.score.mid { color: var(--warn); background: rgba(183, 121, 31, 0.14); }
.verdict { color: var(--muted); }
.status { color: var(--accent); font-weight: 550; }
.heard { margin-top: 8px; color: var(--muted); font-family: var(--cjk); font-size: 18px; }

/* Transport */
.transport { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin: 12px 0 8px; }
.tbtn {
  display: inline-flex; flex-direction: column; align-items: center; gap: 2px;
  min-width: 80px; padding: 7px 14px; border-radius: 14px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  box-shadow: var(--shadow); transition: transform 0.1s ease, background 0.12s ease;
}
.tbtn:hover:not(:disabled) { transform: translateY(-1px); }
.tbtn:disabled { opacity: 0.4; cursor: not-allowed; }
.tbtn .key { font-size: 11px; color: var(--muted); font-weight: 600; letter-spacing: 0.02em; }
.tbtn .lbl { font-size: 15px; font-weight: 600; }
.tbtn.primary { background: var(--accent); color: #fff; border-color: transparent; }
.tbtn.primary .key { color: rgba(255, 255, 255, 0.8); }
.tbtn.record.active { background: var(--bad); color: #fff; border-color: transparent; }
.tbtn.record.live { animation: pulse 1s infinite; }
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 var(--bad-soft); } 50% { box-shadow: 0 0 0 8px transparent; } }

/* Toggles */
.toggles { display: flex; gap: 18px; justify-content: center; flex-wrap: wrap; margin: 4px 0; }
.toggle { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.toggle select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; cursor: pointer; }
.toggle select:disabled { opacity: 0.4; }

.note { text-align: center; color: var(--warn); font-size: 13px; margin: 4px 0 0; }
.kbd-hint { text-align: center; color: var(--muted); font-size: 12px; margin-top: 8px; }

.foot { text-align: center; color: var(--muted); font-size: 11px; padding: 8px 16px 10px; border-top: 1px solid var(--line); margin-top: 10px; }

/* On short viewports, also cap the feedback panel so the reader stays on-screen. */
.feedback { margin: 10px 0 2px; }

/* ── Mobile / touch (v2) ─────────────────────────────────────────────────────
   Compact everything so the written pane, the spoken pane, the score, and the
   transport buttons all fit on one phone screen without scrolling. Smaller
   fonts, tighter panes, single-row buttons, and desktop-only chrome hidden. */
@media (max-width: 560px) {
  .topbar { flex-wrap: wrap; gap: 8px; padding: max(10px, env(safe-area-inset-top)) 14px 4px; }
  .brand h1 { font-size: 19px; }
  .tagline { display: none; } /* free a line */
  .wrap { padding: 4px max(12px, env(safe-area-inset-left)) 8px max(12px, env(safe-area-inset-right)); }

  .reader-head { margin: 4px 0 8px; gap: 10px; }
  .reader-title { font-size: 17px; }
  .reader-sub { font-size: 12px; }
  .counter { font-size: 12px; }

  .panes { gap: 8px; }
  .pane-label { padding: 6px 12px; font-size: 10px; }
  .pane-body { max-height: 23vh; padding: 8px 12px; line-height: 1.9; } /* both panes fit */
  .para { margin: 0 0 7px; }

  /* smaller characters so both panes stay visible at once */
  .formal .sentence, .colloquial .sentence { font-size: 17px; }
  .formal .sentence.current { font-size: 20px; }
  .cc { font-size: 17px; }
  .colloquial .sentence.current .cc { font-size: 20px; }
  .colloquial .plain { font-size: 17px; }

  .feedback { margin: 6px 0 2px; padding: 8px 12px; }
  .heard { font-size: 15px; }

  /* transport: one compact row; hide keyboard hints (irrelevant on touch) */
  .transport { gap: 6px; margin: 8px 0 6px; flex-wrap: nowrap; }
  .tbtn { min-width: 0; flex: 1 1 0; padding: 9px 4px; border-radius: 12px; }
  .tbtn .key { display: none; }
  .tbtn .lbl { font-size: 13px; }

  .toggles { gap: 12px; margin: 4px 0; font-size: 13px; }
  .toggle select { padding: 5px 8px; }

  .kbd-hint { display: none; } /* desktop keyboard-shortcut hint */
  .foot { display: none; } /* reclaim the last line on a phone */
}
