/* Pitch accent trainer.
   Look: a Japanese accent dictionary on school notebook paper. Japanese words are set in Mincho;
   high morae carry the dictionary's red overline with a hook where the pitch drops (.accent-kana).
   Red (--shu) means "the expected pitch" and nothing else; indigo (--ai) is for actions and for you. */

/* ---------- tokens ---------- */
:root {
  --paper: #f1f4ef;      /* cool notebook paper */
  --panel: #fafbf8;
  --ink: #18202b;        /* blue-black sumi */
  --muted: #5b6570;
  --rule: #bfd3c3;       /* genkō yōshi green ruling */
  --line: #d8e1d9;
  --shu: #c9381f;        /* teacher's red pen: expected pitch, high morae */
  --shu-wash: #f6ddd5;
  --ai: #2b4c8c;         /* indigo: buttons, links, your line */
  --on-ai: #f6f8fc;
  --you: #2f63c8;
  --pitch: #47505e;      /* the teacher's recorded pitch */
  --good: #2c7448;
  --bad: #a3192b;
  --sel: #e7eee8;

  /* old names, still used by a few rules and by JS-built markup */
  --bg: var(--paper);
  --accent: var(--ai);
  --accent-ink: var(--on-ai);
  --target: var(--shu);
  --hi: var(--shu-wash);

  --radius: 4px;
  --shadow: none;
  --mincho: "Hiragino Mincho ProN", "Hiragino Mincho Pro", "Yu Mincho", "YuMincho", "Noto Serif CJK JP",
    "Noto Serif JP", "Source Han Serif JP", "BIZ UDPMincho", serif;
  --gothic: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans CJK JP", "Noto Sans JP", "Source Han Sans JP",
    "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  font-family: var(--gothic);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #121a27;    /* 紺 navy */
    --panel: #172131;
    --ink: #e3e7e0;
    --muted: #96a1ae;
    --rule: #2e4446;
    --line: #253244;
    --shu: #f2795d;
    --shu-wash: #3b2423;
    --ai: #92b1ec;
    --on-ai: #0f1724;
    --you: #82b2ff;
    --pitch: #b7bfca;
    --good: #6cc48d;
    --bad: #ff8b80;
    --sel: #1d2a3c;
  }
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--paper);
  color: var(--ink);
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--ai); outline-offset: 2px; }
[lang="ja"], .jp { font-family: var(--mincho); }
select, input[type=search] {
  background: var(--panel); border: 1px solid var(--rule); border-radius: var(--radius); padding: 8px 10px;
}
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px;
  border: 0; border-radius: var(--radius); background: none; color: var(--ink);
}
.icon-btn:hover { background: var(--sel); }

/* ---------- accent notation (dictionary style) ---------- */
.accent-kana { --t: max(1.5px, 0.075em); display: inline-flex; align-items: flex-end; font-family: var(--mincho); }
.accent-kana .mo {
  position: relative; display: inline-block; padding: 0.16em 0.03em 0;
  border-top: var(--t) solid transparent;
}
.accent-kana .mo.H { border-top-color: var(--shu); }
.accent-kana .mo.k::after { /* the hook where the pitch falls */
  content: ""; position: absolute; right: 0; top: calc(-1 * var(--t));
  width: var(--t); height: 0.42em; background: var(--shu);
}
.accent-kana .mo.pt { color: var(--muted); margin-left: 0.08em; }

.no-results { color: var(--muted); font-size: 14px; padding: 24px 4px; }
.flag {
  font-style: normal; font-size: 10px; font-weight: 700; display: inline-block;
  min-width: 15px; height: 15px; line-height: 13px; text-align: center; border-radius: 50%;
  vertical-align: 2px; margin-left: 4px; border: 1px solid currentColor;
}
.flag.one-source { color: var(--muted); }
.flag.disagree, .flag.missing, .flag.homophones { color: var(--shu); }

/* ---------- main ---------- */
main { min-width: 0; }
.content { max-width: 1040px; margin: 0 auto; padding: 36px 40px 56px; }
.word-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.word { font-family: var(--mincho); font-size: 72px; font-weight: 600; line-height: 1.05; letter-spacing: 0.02em; }
.reading { font-size: 24px; color: var(--ink); margin-top: 10px; display: flex; align-items: flex-end; flex-wrap: wrap; gap: 4px 14px; }
.reading .accent-kana { letter-spacing: 0.06em; }
.particle { color: var(--muted); margin-left: 2px; }
.romaji { font-family: var(--gothic); font-size: 15px; color: var(--muted); letter-spacing: 0.02em; padding-bottom: 3px; }
.meta { display: flex; align-items: center; gap: 10px; }
.badge {
  color: var(--shu); font-weight: 600; font-size: 13px;
  padding: 2px 8px; border: 1px solid currentColor; border-radius: 2px;
}
.badge.none { color: var(--muted); }
.accent-num { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* dictionary panel */
.sources-box { margin-top: 4px; }
.sources-summary { font-size: 13px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.sum-acc { color: var(--muted); }
.sources { margin-top: 10px; }
.status { font-weight: 600; }
.status.agree { color: var(--good); }
.status.one-source { color: var(--muted); }
.status.disagree, .status.missing, .status.homophones { color: var(--shu); }
.source-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); border-top: 1px solid var(--rule); border-left: 1px solid var(--rule); }
.source {
  display: grid; grid-template-columns: auto auto 1fr; align-items: baseline; gap: 12px;
  background: var(--panel); border-right: 1px solid var(--rule); border-bottom: 1px solid var(--rule); padding: 10px 14px;
}
.source.absent { opacity: 0.75; }
.src-name { font-size: 12px; font-weight: 600; color: var(--muted); }
.lic { display: block; font-weight: 400; font-size: 10px; }
.src-acc { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.source.absent .src-acc { color: var(--muted); }
.src-entry { font-size: 13px; }
.src-note { font-size: 11px; color: var(--muted); }
.src-extra { font-size: 12px; color: var(--muted); margin-top: 8px; }

/* controls */
.controls { display: flex; align-items: center; gap: 10px; margin: 24px 0 14px; }
.nav-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; flex: none;
  border: 1px solid var(--rule); background: var(--panel); border-radius: var(--radius); color: var(--ink);
}
.nav-btn:hover:not(:disabled) { border-color: var(--ink); }
.nav-btn:disabled { opacity: 0.35; cursor: default; }
#next { order: 9; margin-left: auto; }
.takes { display: flex; border: 1px solid var(--rule); border-radius: var(--radius); background: var(--panel); }
.takes button {
  font-size: 13px; border: 0; background: none; color: var(--muted);
  padding: 9px 12px; white-space: nowrap;
}
.takes button + button { border-left: 1px solid var(--rule); }
.takes button[aria-selected=true] { color: var(--ink); font-weight: 700; box-shadow: inset 0 -2px 0 var(--ink); }
.play {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font-weight: 600;
  background: var(--ai); color: var(--on-ai); border: 0; border-radius: var(--radius); padding: 10px 20px;
}
.play:hover:not(:disabled) { filter: brightness(1.1); }
.play:disabled { opacity: 0.5; cursor: default; }
.slow { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; user-select: none; }
.slow input { width: 16px; height: 16px; accent-color: var(--ai); }

/* chart: ruled like a notebook page */
.chart-card {
  background: var(--panel); border: 1px solid var(--rule); border-radius: var(--radius);
  padding: 16px 16px 10px;
}
#chart { width: 100%; height: 360px; display: block; touch-action: pan-y; }
#chart text { fill: var(--muted); font-size: 12px; font-family: var(--gothic); }
#chart .mora-label { fill: var(--ink); font-size: 20px; font-family: var(--mincho); }
#chart .mora-label.particle { fill: var(--muted); }
#chart .grid { stroke: var(--rule); stroke-opacity: 0.7; }
#chart .mora-sep { stroke: var(--rule); stroke-dasharray: 2 4; }
#chart .hi-band { fill: var(--shu-wash); opacity: 0.55; }
#chart .target { stroke: var(--shu); stroke-width: 3; fill: none; stroke-linejoin: miter; opacity: 0.9; }
#chart .pitch { stroke: var(--pitch); stroke-width: 2.25; fill: none; stroke-linejoin: round; stroke-linecap: round; }
#chart .dot { fill: var(--panel); stroke: var(--ink); stroke-width: 2; }
#chart .playhead { stroke: var(--ai); stroke-width: 1.5; opacity: 0.8; }
#chart .empty { fill: var(--muted); font-size: 14px; }
.legend { display: flex; gap: 6px 20px; flex-wrap: wrap; font-size: 12px; color: var(--muted); padding: 8px 4px 2px; }
.sw { display: inline-block; width: 18px; height: 3px; vertical-align: middle; margin-right: 6px; }
.sw-pitch { background: var(--pitch); height: 2px; }
.sw-target { background: var(--shu); }
.sw-dot { width: 9px; height: 9px; border-radius: 50%; border: 2px solid var(--ink); }

/* pattern table + verdict */
.pattern-table { margin-top: 18px; overflow-x: auto; }
.pattern-table table { border-collapse: collapse; font-size: 14px; }
.pattern-table th { text-align: left; font-weight: 500; color: var(--muted); padding: 6px 14px 6px 0; white-space: nowrap; }
.pattern-table td { text-align: center; min-width: 44px; padding: 6px 4px; }
.pattern-table .kana, .you-table .kana { font-family: var(--mincho); font-size: 20px; }
/* H/L marks echo the notation: high carries the red line on top, low sits on the ruling */
.hl { display: inline-block; min-width: 26px; padding: 2px 6px; font-weight: 700; font-size: 12px; }
.hl.H { color: var(--shu); border-top: 2px solid var(--shu); }
.hl.L { color: var(--muted); border-bottom: 2px solid var(--rule); }
.drop { font-size: 20px; font-weight: 700; }
.drop.expected { color: var(--shu); }
.drop.match { color: var(--good); }
.drop.miss { color: var(--bad); }
.drop.neutral { color: var(--ink); }
.verdict { margin-top: 12px; font-size: 15px; font-weight: 500; line-height: 1.5; }
.verdict.match { color: var(--good); }
.verdict.miss { color: var(--bad); }
.verdict.neutral { color: var(--muted); }
.summary { margin-top: 8px; font-size: 13px; color: var(--muted); }

.note { font-size: 12px; color: var(--muted); margin-top: 24px; max-width: 70ch; line-height: 1.55; }
.keys { font-size: 12px; color: var(--muted); }
kbd {
  font: inherit; font-size: 11px; border: 1px solid var(--rule); border-bottom-width: 2px;
  border-radius: 3px; padding: 1px 5px; margin-right: 2px; background: var(--panel);
}

.load-error {
  background: var(--panel); border: 1px solid var(--rule); border-radius: var(--radius);
  padding: 20px 22px; max-width: 520px; line-height: 1.5;
}
.load-error p { margin: 0 0 12px; }

/* recording yourself */
.record {
  display: inline-flex; align-items: center; gap: 8px; font-weight: 600;
  border: 1px solid var(--rule); background: var(--panel); color: var(--ink);
  border-radius: var(--radius); padding: 9px 14px; --level: 0;
}
.record:hover { border-color: var(--ink); }
.rec-dot { width: 12px; height: 12px; border-radius: 50%; background: #d42c2c; flex: none;
  box-shadow: 0 0 0 calc(var(--level) * 8px) rgb(212 44 44 / 0.25); transition: box-shadow 0.08s; }
.record.on { border-color: #d42c2c; color: #d42c2c; }
.record.on .rec-dot { border-radius: 2px; }
#chart .you { stroke: var(--you); stroke-width: 2.5; fill: none; stroke-dasharray: 7 4; stroke-linecap: round; }
.sw-you { background: repeating-linear-gradient(90deg, var(--you) 0 6px, transparent 6px 9px); }
.attempt { margin-top: 16px; }
.attempt-card {
  background: var(--panel); border: 1px solid var(--rule); border-left: 4px solid var(--rule);
  border-radius: var(--radius); padding: 16px 20px;
}
.attempt-card.match { border-left-color: var(--good); }
.attempt-card.near { border-left-color: var(--ink); }
.attempt-card.miss { border-left-color: var(--bad); }
.attempt-msg { margin: 0; line-height: 1.5; }
.attempt-msg .say { font-family: var(--mincho); font-size: 1.4em; }
.attempt-top { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.grade { display: flex; align-items: baseline; gap: 12px; }
.grade-num { font-family: var(--mincho); font-size: 48px; font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums; }
.attempt-card.match .grade-num { color: var(--good); }
.attempt-card.near .grade-num { color: var(--ink); }
.attempt-card.miss .grade-num { color: var(--bad); }
.grade-label { font-weight: 600; color: var(--muted); }
.attempt-actions { display: flex; gap: 8px; }
.nav-btn.wide { width: auto; padding: 0 12px; font-size: 13px; font-weight: 600; }
.tips { margin: 12px 0 8px; padding-left: 18px; line-height: 1.55; }
.you-table { border-collapse: collapse; font-size: 14px; margin-top: 4px; }
.you-table th { text-align: left; font-weight: 500; color: var(--muted); padding: 4px 14px 4px 0; }
.you-table td { text-align: center; min-width: 40px; padding: 4px; }
.hl.ok { outline: 2px solid var(--good); outline-offset: 1px; }
.hl.bad { outline: 2px solid var(--bad); outline-offset: 1px; }
.attempt-meta { margin: 10px 0 0; font-size: 13px; color: var(--muted); }
@media (max-width: 760px) {
  .record { width: 48px; height: 44px; padding: 0; justify-content: center; }
  .record .rec-label { display: none; }
  .record.on { width: auto; padding: 0 10px; }
  .record.on .rec-label { display: inline; font-size: 13px; }
  .grade-num { font-size: 40px; }
}

/* ---------- tablets / narrow laptops ---------- */
@media (max-width: 1100px) {
  .content { padding: 28px 28px 44px; }
}

/* ---------- phones ---------- */
@media (max-width: 760px) {
  body { display: block; height: auto; min-height: 100dvh; }

  #search { font-size: 16px; } /* 16px stops iOS zooming into the field */

  .content { padding: 20px 16px calc(96px + env(safe-area-inset-bottom)); }
  .word { font-size: 52px; }
  .reading { font-size: 20px; }
  .word-head { align-items: flex-start; flex-direction: column; gap: 12px; }
  .sources-box { margin-top: 14px; }
  .source { grid-template-columns: 1fr auto; gap: 2px 10px; }
  .source .src-entry { grid-column: 1 / -1; }

  /* controls become a bar along the bottom of the screen */
  .controls {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; margin: 0;
    padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: color-mix(in srgb, var(--panel) 94%, transparent);
    backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
    border-top: 1px solid var(--rule); gap: 8px;
  }
  .controls .play { flex: 1; padding: 12px; font-size: 16px; }
  .nav-btn { width: 44px; height: 44px; }
  #next { margin-left: 0; }
  .slow { font-size: 12px; flex-direction: column; gap: 2px; }
  .takes button { padding: 11px 10px; }
  .takes button .long { display: none; }

  .chart-card { padding: 10px 6px 8px; }
  #chart { height: 260px; }
  .legend { padding: 6px 8px 2px; }
  .keys { display: none; }
}

/* ================= header bar (all sizes) ================= */
.topbar {
  display: flex; align-items: center; gap: 18px;
  position: sticky; top: 0; z-index: 20;
  padding: 0 28px;
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--rule);
}
.brand-mark { font-family: var(--mincho); font-size: 24px; font-weight: 600; line-height: 1; padding: 8px 0 6px; }
.tabs { display: flex; gap: 4px; align-self: stretch; }
.tab {
  border: 0; background: none; color: var(--muted); font-weight: 600; font-size: 14px;
  padding: 16px 10px 14px; box-shadow: inset 0 -2px 0 transparent;
}
.tab:hover { color: var(--ink); }
.tab[aria-current] { color: var(--ink); box-shadow: inset 0 -2px 0 var(--ink); }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; font-size: 13px; font-weight: 700;
  border: 1px solid var(--rule); background: var(--panel); border-radius: var(--radius); padding: 4px 9px;
  color: var(--muted); cursor: pointer; font-variant-numeric: tabular-nums;
}
.chip.streak.lit { color: var(--shu); border-color: color-mix(in srgb, var(--shu) 45%, var(--rule)); }
.chip.today .ring { transform: rotate(-90deg); }
.chip.today .track { fill: none; stroke: var(--line); stroke-width: 3; }
.chip.today .fill { fill: none; stroke: var(--viz-1); stroke-width: 3; stroke-linecap: round; transition: stroke-dasharray 0.3s; }
.chip.today.done { color: var(--ink); }
.chip.today.done .fill { stroke: var(--good); }
.account-btn {
  white-space: nowrap; display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--rule); background: var(--panel);
  border-radius: var(--radius); padding: 3px 12px 3px 3px; font-weight: 600; font-size: 14px; color: var(--ink);
}
.account-btn:not(:has(.avatar)) { padding: 7px 14px; background: var(--ai); color: var(--on-ai); border-color: transparent; }
.avatar {
  width: 28px; height: 28px; border-radius: 2px; display: inline-grid; place-items: center;
  background: var(--viz-1); color: var(--on-ai); font-size: 13px; font-weight: 700;
}

/* ================= learn view additions ================= */
.lvl { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 8px; vertical-align: 1px; flex: none; }
.lvl-none { background: transparent; }
.lvl-learning { border: 2px solid var(--viz-m0); }
.lvl-good { background: var(--viz-m1); }
.lvl-mastered { background: var(--viz-m2); box-shadow: 0 0 0 2px color-mix(in srgb, var(--viz-m2) 30%, transparent); }
.level-pill {
  display: inline-block; font-size: 12px; font-weight: 600; border-radius: 2px; padding: 2px 8px;
  border: 1px solid var(--rule); color: var(--muted); background: var(--panel); white-space: nowrap;
}
.level-pill.lvl-learning, .level-pill.lvl-good, .level-pill.lvl-mastered { border: 1px solid var(--rule); box-shadow: none; width: auto; height: auto; margin: 0; }
.level-pill.lvl-learning { color: var(--ink); }
.level-pill.lvl-good { color: var(--ink); border-color: color-mix(in srgb, var(--viz-m1) 60%, var(--rule)); background: color-mix(in srgb, var(--viz-m1) 12%, var(--panel)); }
.level-pill.lvl-mastered { color: var(--ink); border-color: color-mix(in srgb, var(--viz-m2) 60%, var(--rule)); background: color-mix(in srgb, var(--viz-m2) 16%, var(--panel)); }
.lvl-text { font-weight: 600; color: var(--ink); }
.suggest {
  border: 1px solid var(--rule); background: var(--panel); color: var(--ai); font-size: 13px; font-weight: 600;
  border-radius: var(--radius); padding: 4px 12px;
}
.suggest:hover { border-color: var(--ai); }
.attempt-next { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; flex-wrap: wrap; }

/* buttons used across dialogs and views */
.primary { background: var(--ai); color: var(--on-ai); border: 0; border-radius: var(--radius); padding: 10px 16px; font-weight: 600; }
.primary:hover:not(:disabled) { filter: brightness(1.1); }
.primary:disabled { opacity: 0.5; cursor: default; }
.ghost { background: var(--panel); color: var(--ink); border: 1px solid var(--rule); border-radius: var(--radius); padding: 9px 14px; font-weight: 600; }
.ghost:hover { border-color: var(--ink); }
.danger-btn { background: var(--bad); color: var(--paper); border: 0; border-radius: var(--radius); padding: 9px 14px; font-weight: 600; }

/* ================= dialogs and toast ================= */
.dialog {
  border: 1px solid var(--rule); border-radius: var(--radius); padding: 0; width: min(420px, calc(100vw - 32px));
  background: var(--panel); color: var(--ink); box-shadow: 0 24px 48px -12px rgb(16 24 40 / 0.35);
}
.dialog::backdrop { background: rgb(18 26 39 / 0.5); }
.dialog > form, .dialog > .dialog-body { padding: 24px 24px 20px; display: flex; flex-direction: column; gap: 12px; }
.dialog h2 { margin: 0; font-size: 21px; font-weight: 700; }
.dialog label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; }
.dialog input { font: inherit; font-size: 16px; padding: 10px 12px; border-radius: var(--radius); border: 1px solid var(--rule); background: var(--paper); color: var(--ink); }
.dialog input:focus-visible { outline: 2px solid var(--ai); outline-offset: 1px; }
.seg { display: flex; border-bottom: 1px solid var(--rule); }
.seg button { flex: 1; border: 0; background: none; color: var(--muted); padding: 9px 7px; font-weight: 600; font-size: 14px; box-shadow: inset 0 -2px 0 transparent; }
.seg button[aria-selected="true"] { color: var(--ink); box-shadow: inset 0 -2px 0 var(--ink); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.form-error { margin: 0; color: var(--bad); font-size: 14px; }
.hint, .fine { margin: 0; font-size: 12px; color: var(--muted); line-height: 1.5; }
.notice { margin: 0; font-size: 14px; line-height: 1.5; background: var(--shu-wash); color: var(--ink); padding: 10px 12px; border-left: 3px solid var(--shu); }
.notice a { color: var(--ai); font-weight: 600; word-break: break-all; }
.row-form { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: nowrap; }
.dialog .row-form label { flex: 0 1 auto; flex-direction: row; align-items: center; justify-content: flex-start; gap: 8px; font-weight: 500; white-space: nowrap; }
.dialog .row-form input { width: 76px; padding: 8px 10px; }
.stack-form { display: flex; flex-direction: column; gap: 10px; padding-top: 10px; }
.dialog details { border-top: 1px solid var(--line); padding-top: 10px; }
.dialog summary { cursor: pointer; font-weight: 600; font-size: 14px; }
.dialog details.danger summary { color: var(--bad); }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60;
  background: var(--ink); color: var(--paper); padding: 11px 16px; border-radius: var(--radius); font-size: 14px; font-weight: 600;
  box-shadow: 0 12px 28px -8px rgb(16 24 40 / 0.4); max-width: min(520px, calc(100vw - 32px)); text-align: center;
}
.toast.good { background: var(--good); color: var(--paper); }
@media (max-width: 760px) { .toast { bottom: calc(84px + env(safe-area-inset-bottom)); } }

/* ================= progress view ================= */
:root {
  --viz-1: #2b4c8c;
  --viz-m0: #9fb3da; --viz-m1: #4a6db0; --viz-m2: #1d3363;
  --cal-0: #e0e8e1; --cal-1: #c2cfea; --cal-2: #8ba4d6; --cal-3: #4a6db0; --cal-4: #1d3363;
  --viz-grid: #dbe4dc; --viz-axis: #aec2b2; --viz-ink2: #47505e; --viz-up: #2c7448;
}
@media (prefers-color-scheme: dark) {
  :root {
    --viz-1: #92b1ec;
    --viz-m0: #34508a; --viz-m1: #6f92d8; --viz-m2: #c0d2f5;
    --cal-0: #1d2939; --cal-1: #2c4475; --cal-2: #3f62a8; --cal-3: #6f92d8; --cal-4: #c0d2f5;
    --viz-grid: #233041; --viz-axis: #3a4d52; --viz-ink2: #b7bfca; --viz-up: #6cc48d;
  }
}
.progress { display: flex; flex-direction: column; gap: 16px; }
.hero-row { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 16px; }
.hero, .goal-card, .tile, .card {
  background: var(--panel); border: 1px solid var(--rule); border-radius: var(--radius);
}
.hero { display: flex; align-items: center; gap: 20px; padding: 20px 24px; }
.hero-num { font-family: var(--mincho); font-size: 72px; font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums; }
.hero-label { font-size: 21px; font-weight: 700; }
.hero-sub { font-size: 14px; color: var(--muted); margin-top: 4px; line-height: 1.5; }
.goal-card { padding: 18px 20px; display: flex; flex-direction: column; gap: 6px; justify-content: center; }
.tile-label { font-size: 13px; color: var(--muted); font-weight: 600; }
.tile-value { font-size: 30px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.tile-value .of { font-size: 16px; color: var(--muted); font-weight: 600; }
.tile-sub { font-size: 13px; color: var(--muted); }
.meter { height: 6px; background: color-mix(in srgb, var(--viz-1) 16%, var(--panel)); overflow: hidden; }
.meter span { display: block; height: 100%; background: var(--viz-1); transition: width 0.3s; }
/* the four tiles share one ruled strip */
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0; border: 1px solid var(--rule); border-radius: var(--radius); background: var(--panel); }
.tiles .tile { border: 0; border-radius: 0; border-left: 1px solid var(--rule); }
.tiles .tile:first-child { border-left: 0; }
.tile { padding: 14px 18px; display: flex; flex-direction: column; gap: 4px; }
.delta { font-weight: 700; color: var(--ink); }
.delta.up { color: var(--viz-up); }
.card { padding: 18px 20px; min-width: 0; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head h3 { margin: 0; font-size: 16px; font-weight: 700; }
.card-note { font-size: 12px; color: var(--muted); }
.chart-pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.fig { position: relative; height: 220px; }
.fig > svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.fig .grid { stroke: var(--viz-grid); stroke-width: 1; }
.fig .axis { stroke: var(--viz-axis); stroke-width: 1; }
.fig .tick, .cal .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.fig .col { fill: var(--viz-1); }
.fig .goal { stroke: var(--shu); stroke-width: 1.25; stroke-dasharray: 4 4; opacity: 0.85; }
.fig .line { fill: none; stroke: var(--viz-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.fig .dot { fill: var(--viz-1); stroke: var(--panel); stroke-width: 2; }
.fig .dot.hover { r: 6; }
.fig .end-label { fill: var(--ink); font-size: 12px; font-weight: 700; }
.fig .cross { stroke: var(--viz-axis); stroke-width: 1; }
.fig .hit { fill: transparent; cursor: crosshair; }
.fig .hit.on { fill: color-mix(in srgb, var(--viz-1) 8%, transparent); }
.cal { overflow-x: auto; }
.cal svg { display: block; }
.cal .cell { stroke: none; }
.cal .c0, .cal-legend .c0 { fill: var(--cal-0); background: var(--cal-0); }
.cal .c1, .cal-legend .c1 { fill: var(--cal-1); background: var(--cal-1); }
.cal .c2, .cal-legend .c2 { fill: var(--cal-2); background: var(--cal-2); }
.cal .c3, .cal-legend .c3 { fill: var(--cal-3); background: var(--cal-3); }
.cal .c4, .cal-legend .c4 { fill: var(--cal-4); background: var(--cal-4); }
.cal .cell:hover { stroke: var(--ink); stroke-width: 1.5; }
.cal-legend { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); margin-top: 8px; flex-wrap: wrap; }
.cal-legend i { width: 12px; height: 12px; border-radius: 1px; display: inline-block; }
.range-row, .filters { display: flex; gap: 0; flex-wrap: wrap; }
.range-row button, .filters button {
  border: 0; background: none; color: var(--muted); border-radius: 0;
  padding: 6px 12px 7px; font-size: 13px; font-weight: 600; box-shadow: inset 0 -1px 0 var(--rule);
}
.range-row button:hover, .filters button:hover { color: var(--ink); }
.range-row button[aria-pressed="true"], .filters button[aria-pressed="true"] { color: var(--ink); box-shadow: inset 0 -2px 0 var(--ink); }
.table-view summary { cursor: pointer; font-size: 13px; color: var(--muted); font-weight: 600; }
.table-view[open] summary { margin-bottom: 8px; }
.hbars { display: flex; flex-direction: column; gap: 12px; padding-top: 4px; }
.hbar { display: grid; grid-template-columns: 84px minmax(0, 1fr) 82px; align-items: center; gap: 10px; font-size: 13px; }
.hbar-label { color: var(--ink); font-weight: 600; }
.hbar-track { height: 12px; background: color-mix(in srgb, var(--viz-1) 10%, var(--panel)); overflow: hidden; }
.hbar-fill { display: block; height: 100%; background: var(--viz-1); }
.hbar-value { font-weight: 700; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); font-weight: 500; }
.levels { padding-top: 6px; }
.seg-bar { display: flex; gap: 2px; height: 28px; }
.seg-bar .seg { border: 0; border-radius: 1px; color: #fff; font-size: 12px; font-weight: 700; display: flex; align-items: center; padding-left: 8px; min-width: 4px; }
.seg-bar .s0 { background: var(--viz-m0); color: #18202b; }
.seg-bar .s1 { background: var(--viz-m1); }
.seg-bar .s2 { background: var(--viz-m2); }
@media (prefers-color-scheme: dark) { .seg-bar .s0 { color: #fff; } .seg-bar .s1, .seg-bar .s2 { color: #0f1724; } }
.seg-legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; font-size: 13px; color: var(--muted); }
.seg-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 1px; margin-right: 6px; vertical-align: -1px; }
.seg-legend .s0 { background: var(--viz-m0); } .seg-legend .s1 { background: var(--viz-m1); } .seg-legend .s2 { background: var(--viz-m2); }
.seg-legend strong { color: var(--ink); }
.levels-note { margin: 12px 0 0; line-height: 1.5; }
.table-wrap { overflow-x: auto; }
.words-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.words-table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--ink); padding: 6px 8px; white-space: nowrap; }
.words-table th button { border: 0; background: none; color: inherit; font: inherit; padding: 0; }
.words-table td { padding: 8px; border-bottom: 1px solid var(--rule); white-space: nowrap; }
.words-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.words-table tbody tr[data-rec] { cursor: pointer; }
.words-table tbody tr[data-rec]:hover { background: var(--sel); }
.tw { font-family: var(--mincho); font-size: 18px; font-weight: 600; }
.tr { font-size: 12px; color: var(--muted); }
.due { font-size: 11px; font-weight: 700; color: var(--shu); }
.recent { list-style: none; margin: 0; padding: 0; }
.recent li { display: flex; align-items: center; gap: 10px; padding: 8px 4px; border-bottom: 1px solid var(--rule); cursor: pointer; }
.recent li:hover { background: var(--sel); }
.recent .when { margin-left: auto; font-size: 12px; white-space: nowrap; }
.recent .ok { font-size: 12px; font-weight: 600; color: var(--muted); }
.recent .ok.yes { color: var(--viz-up); }
.grade-chip { min-width: 34px; text-align: center; font-weight: 700; font-size: 13px; border-radius: 2px; padding: 3px 6px; background: var(--paper); border: 1px solid var(--rule); font-variant-numeric: tabular-nums; }
.grade-chip.g-hi { border-color: color-mix(in srgb, var(--good) 55%, var(--rule)); color: var(--good); }
.grade-chip.g-lo { border-color: color-mix(in srgb, var(--bad) 55%, var(--rule)); color: var(--bad); }
.empty-state { background: var(--panel); border: 1px solid var(--rule); border-radius: var(--radius); padding: 36px 28px; text-align: center; max-width: 560px; margin: 24px auto; }
.empty-state h2 { margin: 0 0 8px; }
.empty-state p { color: var(--muted); line-height: 1.6; }
.empty-actions { display: flex; gap: 10px; justify-content: center; margin-top: 16px; }
.loading { color: var(--muted); }
.viz-tip {
  position: fixed; z-index: 70; pointer-events: none; background: var(--panel);
  border: 1px solid var(--rule); border-radius: var(--radius); padding: 8px 10px; font-size: 12px; line-height: 1.5;
  box-shadow: 0 10px 24px -8px rgb(16 24 40 / 0.3); color: var(--muted);
}
.viz-tip strong { color: var(--ink); font-size: 13px; }
.viz-tip .tip-main strong { font-size: 15px; }
@media (max-width: 900px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tiles .tile:nth-child(3) { border-left: 0; }
  .tiles .tile:nth-child(n+3) { border-top: 1px solid var(--rule); }
  .chart-pair, .hero-row { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 760px) {
  .progress { padding-bottom: 40px; }
  .hero-num { font-size: 56px; }
  .tile-value { font-size: 24px; }
  .fig { height: 190px; }
  .meta { flex-wrap: wrap; }
  .words-table td:nth-child(2), .words-table th:nth-child(2), .words-table td:nth-child(5), .words-table th:nth-child(5) { display: none; }
}

/* ---------- recordings notice, grade feedback ---------- */
.rec-notice { margin: -4px 0 10px; }
.fair { display: flex; align-items: center; gap: 6px; margin-top: 12px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.fair-btn { border: 1px solid var(--rule); background: var(--panel); border-radius: var(--radius); padding: 3px 10px; font-size: 16px; line-height: 1.3; }
.fair-btn[aria-pressed="true"] { border-color: var(--ai); background: color-mix(in srgb, var(--ai) 14%, var(--panel)); }
.fair-thanks { color: var(--good); font-weight: 600; }

/* ---------- rating page (/#rate) ---------- */
.rate { display: flex; flex-direction: column; gap: 16px; max-width: 720px; }
.rate-head h2 { margin: 0 0 4px; }
.rate-card { display: flex; flex-direction: column; gap: 14px; padding: 22px; }
.rate-word .word { font-size: 48px; }
.rate-play { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.rate-q { margin: 6px 0 0; font-size: 18px; }
.rate-drop { display: flex; flex-direction: column; gap: 6px; }
.drop-chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip-btn { border: 1px solid var(--rule); background: var(--panel); color: var(--ink); border-radius: var(--radius); padding: 5px 12px; font-size: 14px; }
.chip-btn[aria-pressed="true"] { border-color: var(--ai); background: color-mix(in srgb, var(--ai) 14%, var(--panel)); font-weight: 600; }
.rate-verdicts { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.rate-verdicts .verdict { margin: 0; border: 1px solid var(--rule); background: var(--panel); color: var(--ink); border-radius: var(--radius); padding: 14px 10px; font-weight: 700; font-size: 15px; }
.rate-verdicts .verdict.right { border-color: color-mix(in srgb, var(--good) 60%, var(--rule)); }
.rate-verdicts .verdict.wrong { border-color: color-mix(in srgb, var(--bad) 60%, var(--rule)); }
.rate-verdicts .verdict:hover { background: var(--sel); }
.rate-verdicts .verdict kbd { font-size: 11px; opacity: 0.6; margin-left: 4px; }
@media (max-width: 760px) {
  .rate-verdicts { grid-template-columns: minmax(0, 1fr); }
  .rate-verdicts kbd, .rate-card .keys { display: none; }
}
@media (max-width: 760px) {
  /* Raters get a third tab: make room in the top bar. */
  .topbar:has(#tab-rate:not([hidden])) .tab { padding: 10px 6px; font-size: 13px; }
  .topbar:has(#tab-rate:not([hidden])) .chip.today { display: none; }
}

/* ================= words home ================= */
.words { display: flex; flex-direction: column; gap: 20px; }
.words-hero { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px 24px; flex-wrap: wrap; }
.words-hero h1 { margin: 0; font-size: 36px; font-weight: 700; line-height: 1.15; }
.lead { margin: 8px 0 0; color: var(--muted); font-size: 16px; line-height: 1.55; max-width: 56ch; }
.primary.big { font-size: 16px; padding: 13px 22px; white-space: nowrap; }

/* lessons: a dictionary's thumb index, one ruled strip */
.lessons {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule);
}
.lesson {
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; align-items: center;
  gap: 4px 14px; text-align: left; color: var(--ink);
  background: none; border: 0; border-left: 1px solid var(--rule); border-radius: 0;
  padding: 16px 18px 14px; box-shadow: inset 0 3px 0 transparent;
}
.lesson:first-child { border-left: 0; }
.lesson:hover { background: color-mix(in srgb, var(--panel) 70%, transparent); }
.lesson[aria-checked="true"] { background: var(--panel); box-shadow: inset 0 3px 0 var(--shu); }
.lesson-top { display: contents; }
.lesson-jp { grid-row: 1 / span 2; grid-column: 1; font-size: 34px; font-weight: 600; color: var(--muted); line-height: 1; writing-mode: horizontal-tb; }
.lesson[aria-checked="true"] .lesson-jp { color: var(--ink); }
.lesson-title { grid-column: 2; font-weight: 700; font-size: 15px; }
.lesson .pattern-pic { grid-column: 2; grid-row: 2; }
.lesson-blurb { grid-column: 1 / -1; font-size: 13px; color: var(--muted); line-height: 1.45; margin-top: 8px; }
.pattern-pic { display: block; overflow: visible; }
.pp-line { fill: none; stroke: var(--shu); stroke-width: 1.75; stroke-linejoin: round; opacity: 0.75; }
.pp-dot { fill: var(--shu); }
.pp-particle { fill: var(--paper); stroke: var(--shu); stroke-width: 1.5; }
.lesson[aria-checked="true"] .pp-particle { fill: var(--panel); }

.grid-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px 20px; flex-wrap: wrap; margin-top: 12px; }
.grid-head h2 { margin: 0; font-size: 24px; font-weight: 700; }
.grid-sub { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
#search { width: min(320px, 100%); background: var(--panel); padding: 10px 12px; }
#search::placeholder { color: var(--muted); }
.filters .n { font-weight: 500; opacity: 0.7; margin-left: 2px; font-variant-numeric: tabular-nums; }
.word-grid .group-label {
  display: flex; align-items: center; gap: 12px;
  font-size: 14px; font-weight: 600; color: var(--muted); margin: 26px 0 10px;
}
.word-grid .group-label::after { content: ""; flex: 1; border-top: 1px solid var(--rule); }
.word-grid .group-label:first-child { margin-top: 6px; }

/* the word grid is ruled like genkō yōshi: cells share their lines */
.cards {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  border-top: 1px solid var(--rule); border-left: 1px solid var(--rule); background: var(--panel);
}
.word-item {
  position: relative; display: flex; flex-direction: column; align-items: flex-start; gap: 4px; text-align: left; min-width: 0;
  background: none; color: var(--ink); border: 0; border-right: 1px solid var(--rule); border-bottom: 1px solid var(--rule);
  border-radius: 0; padding: 10px 12px 14px;
}
.word-item:hover { background: var(--sel); }
.word-item[aria-current="true"] { box-shadow: inset 0 0 0 2px var(--ink); }
.word-item:focus-visible { outline-offset: -3px; }
.word-item .wi-top { display: flex; align-items: center; justify-content: space-between; width: 100%; min-height: 16px; }
.wi-status { font-size: 11px; font-weight: 600; color: var(--muted); display: inline-flex; align-items: center; }
.wi-status .lvl { width: 8px; height: 8px; margin-right: 5px; }
.wi-status.new { opacity: 0.6; font-weight: 500; }
.lvl-text-mastered, .lvl-text-good { color: var(--ink); }
.word-item .w { font-family: var(--mincho); font-size: 30px; font-weight: 600; line-height: 1.15; margin-top: 4px; overflow-wrap: anywhere; }
.word-item .r { font-size: 16px; color: var(--ink); margin-top: 2px; }
.word-item .r .mo.pt { font-size: 0.9em; }
.word-item .flag { margin-left: auto; }
.word-grid.busy { opacity: 0.45; pointer-events: none; transition: opacity 0.15s 0.1s; }

/* ================= practice screen ================= */
.practice-head { display: flex; align-items: center; gap: 14px; margin: -12px 0 26px; }
.back {
  display: inline-flex; align-items: center; gap: 4px; border: 0; background: none;
  padding: 6px 6px 6px 0; font-weight: 600; font-size: 14px; color: var(--ai);
}
.back:hover span { text-decoration: underline; text-underline-offset: 3px; }
.lesson-name { font-weight: 700; font-size: 14px; }
.lesson-bar { flex: 1; height: 3px; background: var(--rule); overflow: hidden; min-width: 40px; }
.lesson-bar span { display: block; height: 100%; background: var(--ink); transition: width 0.25s; }
.position { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.howto { margin: 20px 0 0; font-size: 14px; color: var(--muted); max-width: 70ch; }
.howto .k-target { color: var(--shu); font-weight: 600; }
.howto .k-you { color: var(--you); font-weight: 600; }
.more { margin-top: 24px; border-top: 1px solid var(--rule); padding-top: 14px; }
.more > summary { cursor: pointer; font-weight: 600; font-size: 14px; color: var(--muted); }
.more[open] > summary { margin-bottom: 12px; }
@media (max-width: 760px) {
  .words-hero h1 { font-size: 28px; }
  .primary.big { width: 100%; }
  .lessons { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .lesson { padding: 12px; gap: 4px 10px; }
  .lesson:nth-child(3) { border-left: 0; }
  .lesson:nth-child(n+3) { border-top: 1px solid var(--rule); }
  .lesson-jp { font-size: 26px; }
  .lesson-blurb { font-size: 12px; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); }
  .word-item .w { font-size: 26px; }
  #search { width: 100%; }
  .practice-head { margin: -6px 0 18px; gap: 10px; }
  .lesson-name { display: none; }
  .howto { margin: 12px 0 4px; font-size: 13px; }
  .rec-notice { margin: 6px 0 10px; }
}
@media (max-width: 760px) {
  /* after the header rules above, so these win */
  .topbar { padding: 0 10px; padding-top: env(safe-area-inset-top); gap: 6px; }
  .account-btn .uname { display: none; }
  .account-btn { padding: 3px; }
  .account-btn:not(:has(.avatar)) { padding: 7px 10px; }
  .tabs { gap: 0; }
  .tab { padding: 14px 6px 12px; font-size: 13.5px; }
  .chip { padding: 3px 6px; }
  .top-right { gap: 6px; }
  .brand-mark { display: none; }
}
@media (max-width: 420px) {
  /* the ring alone shows today's goal when the bar is this narrow */
  .chip.today .num { display: none; }
  .chip.today { padding: 3px 4px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
}
