/* ---------- Colours (light + dark follow the phone's setting) ---------- */
:root {
  --bg: #f6f2ea;         /* washi paper */
  --surface: #fffdf8;
  --ink: #1f1d1a;
  --muted: #7a736a;
  --line: #e4ddd0;
  --accent: #c0392b;     /* hanko red */
  --good: #2e7d5b;
  --again: #b5651d;
  --shadow: 0 6px 24px rgba(40, 30, 10, .10);
  --jp: "Noto Sans JP", "Noto Sans CJK JP", "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
  --jp-serif: "Noto Serif JP", "Noto Serif CJK JP", "Hiragino Mincho ProN", "Yu Mincho", serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161513; --surface: #211f1c; --ink: #f1ece3; --muted: #a39b8f;
    --line: #34312c; --accent: #e3574a; --good: #4fb486; --again: #d98a43;
    --shadow: 0 6px 24px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, var(--jp);
  display: flex; flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }

/* ---------- Header ---------- */
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
}
.top h1 { margin: 0; font: 700 22px var(--jp-serif); letter-spacing: .02em; }
.top h1 span { font: 600 13px system-ui; color: var(--accent); margin-left: 6px; vertical-align: middle;
  border: 1.5px solid var(--accent); border-radius: 4px; padding: 0 4px; }
.deck-picker { display: flex; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 3px; }
.deck-picker button { border: 0; background: none; padding: 5px 11px; border-radius: 999px; font-size: 14px; color: var(--muted); }
.deck-picker button.on { background: var(--ink); color: var(--bg); }

/* ---------- Views ---------- */
main { flex: 1; overflow-y: auto; padding: 0 16px 96px; }
.view { display: none; max-width: 560px; margin: 0 auto; }
.view.on { display: block; }

/* ---------- Flashcard ---------- */
.progress { display: flex; align-items: center; gap: 10px; margin: 4px 0 12px; font-size: 13px; color: var(--muted); }
.bar { flex: 1; height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--good); transition: width .3s; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
  box-shadow: var(--shadow); min-height: 52vh; padding: 20px;
  display: flex; flex-direction: column; outline: none; user-select: none;
}
.card .lvl { align-self: flex-start; font-size: 12px; font-weight: 600; color: var(--accent);
  border: 1px solid var(--accent); border-radius: 4px; padding: 0 6px; }
.card .star { position: absolute; }
.card-head { display: flex; justify-content: space-between; align-items: center; }
.star-btn { border: 0; background: none; font-size: 22px; color: var(--muted); padding: 0 4px; }
.star-btn.on { color: #e0a800; }
.big { font: 400 min(38vw, 180px)/1.1 var(--jp-serif); text-align: center; margin: auto 0; }
.card.back .big { font-size: 84px; margin: 8px 0 4px; }
.meaning { text-align: center; font-size: 20px; font-weight: 600; margin: 0 0 12px; }
.readings { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 15px; margin-bottom: 14px; }
.readings dt { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .06em; padding-top: 3px; }
.readings dd { margin: 0; font-family: var(--jp); }
.readings small { font-size: inherit; color: var(--muted); }
.empty { color: var(--muted); }

/* Example sentences */
.ex { border-top: 1px solid var(--line); padding: 12px 0 4px; }
.ex .jp { font: 19px/2.1 var(--jp); }
.ex .jp mark { background: none; color: var(--accent); font-weight: 700; }
.ex rt { font-size: 10px; color: var(--muted); }
.no-furigana .ex rt { display: none; }
.ex .tr-btn { border: 1px solid var(--line); background: none; border-radius: 99px; font-size: 13px; padding: 2px 12px; color: var(--muted); }
.ex .en { font-size: 15px; color: var(--muted); margin: 4px 0 0; }

.answer-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 14px; }
.answer-row[hidden] { display: none; }
.btn { border: 0; border-radius: 14px; padding: 14px; font-weight: 600; font-size: 16px; }
.btn.again { background: color-mix(in srgb, var(--again) 16%, transparent); color: var(--again); }
.btn.good { background: var(--good); color: #fff; }
.btn.ghost { background: none; border: 1px solid var(--line); color: var(--accent); margin-top: 8px; }
.hint { text-align: center; font-size: 13px; color: var(--muted); }
.done { text-align: center; margin: auto; }
.done .big { font-size: 64px; margin: 0; }

/* ---------- Kanji list ---------- */
#search {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); font-size: 16px; margin: 4px 0 8px;
}
.legend { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; margin: 0 0 10px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-left: 6px; }
.dot.known { background: var(--good); } .dot.learning { background: var(--again); } .dot.new { background: var(--line); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 8px; }
.tile {
  aspect-ratio: 1; background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  font: 30px var(--jp-serif); position: relative; display: grid; place-items: center;
}
.tile::after { content: ""; position: absolute; bottom: 6px; left: 50%; width: 18px; height: 3px; margin-left: -9px;
  border-radius: 2px; background: var(--line); }
.tile.known::after { background: var(--good); } .tile.learning::after { background: var(--again); }

/* ---------- Detail sheet ---------- */
.sheet { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: flex-end; z-index: 20; }
.sheet[hidden] { display: none; }
.sheet-body { background: var(--surface); width: 100%; max-height: 88vh; overflow-y: auto; border-radius: 20px 20px 0 0;
  padding: 18px 18px calc(24px + env(safe-area-inset-bottom)); position: relative; max-width: 560px; margin: 0 auto; }
.sheet .card { box-shadow: none; border: 0; padding: 0; min-height: 0; }
.close { position: absolute; top: 12px; right: 12px; border: 0; background: var(--line); width: 32px; height: 32px; border-radius: 50%; z-index: 1; }

/* ---------- More / settings ---------- */
h2 { font-size: 15px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 22px 0 8px; }
.toggle { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stats div { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.stats b { display: block; font-size: 22px; }
.stats span { font-size: 12px; color: var(--muted); }
.small { font-size: 13px; color: var(--muted); }

/* ---------- Bottom tabs ---------- */
.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; display: flex; justify-content: center;
  background: var(--surface); border-top: 1px solid var(--line);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.tabs button { flex: 1; max-width: 180px; border: 0; background: none; font-size: 12px; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 0; padding: 4px; }
.tabs b { font: 400 20px var(--jp-serif); }
.tabs button.on { color: var(--accent); }
.sheet .card-head { padding-right: 44px; }

/* ---------- Stage 2: readings, words, quizzes, Today ---------- */
.main-reading { text-align: center; font: 20px var(--jp); margin: 0 0 2px; }
.main-reading small { font-size: inherit; color: var(--muted); }
.words { list-style: none; padding: 0; margin: 0 0 14px; border: 1px solid var(--line); border-radius: 12px; }
.words li { padding: 8px 12px; border-top: 1px solid var(--line); font-size: 15px; }
.words li:first-child { border-top: 0; }
.words b { font: 600 17px var(--jp); }
.words .kana { font-family: var(--jp); color: var(--accent); }

.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 0 0 8px; }
.eyebrow.center { text-align: center; }
.today-card { min-height: 0; margin-bottom: 14px; }
.today-nums { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.today-nums div { text-align: center; }
.today-nums b { display: block; font-size: 40px; line-height: 1.1; }
.today-nums span { font-size: 13px; color: var(--muted); }
.btn.wide { width: 100%; margin-top: 10px; }
.all-done { text-align: center; color: var(--muted); }

.quiz { min-height: 0; gap: 12px; }
.q-head { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); }
.q-label { font-weight: 600; color: var(--ink); }
.q-prompt { text-align: center; padding: 6px 0; }
.q-big { font: 400 96px/1.15 var(--jp-serif); }
.q-big.word { font-size: 64px; }
.q-sub { margin: 0; color: var(--muted); font-size: 14px; }
.q-meaning { font-size: 26px; font-weight: 600; padding: 18px 0; }
.q-context { font: 18px/2.1 var(--jp); background: var(--bg); border-radius: 12px; padding: 4px 12px; }
.q-context mark { background: none; color: var(--accent); font-weight: 700; }
.q-context rt { font-size: 10px; color: var(--muted); }
.options { display: grid; grid-template-columns: 1fr; gap: 8px; }
.options.kanji-opts { grid-template-columns: 1fr 1fr; }
.kanji-opts .opt { font: 44px/1.2 var(--jp-serif); padding: 10px; }
.opt { border: 1.5px solid var(--line); background: var(--surface); border-radius: 12px; padding: 13px 14px;
  font: 17px var(--jp); text-align: left; transition: background .15s; }
.kanji-opts .opt { text-align: center; }
.opt.right { border-color: var(--good); background: color-mix(in srgb, var(--good) 14%, transparent); }
.opt.wrong { border-color: var(--again); background: color-mix(in srgb, var(--again) 14%, transparent); }
.opt.locked:not(.right):not(.wrong) { opacity: .55; }
.feedback { border-top: 1px solid var(--line); padding-top: 10px; }
.verdict { font-weight: 700; margin: 0 0 8px; }
.verdict.ok { color: var(--good); } .verdict.no { color: var(--again); }
.mini { display: flex; gap: 14px; align-items: center; margin-bottom: 10px; }
.mini-k { font: 56px/1 var(--jp-serif); }
.mini .main-reading { text-align: left; }
.mini-m { margin: 0; font-weight: 600; }
.done-card { min-height: 0; text-align: center; align-items: center; }
.link { border: 0; background: none; color: var(--accent); text-decoration: underline; padding: 0 0 0 8px; font-size: 13px; }
select { font: inherit; padding: 4px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); margin-left: auto; }
.toggle small { color: var(--muted); }

/* ---------- Stage 3: stroke order + writing pad ---------- */
.so { display: flex; gap: 14px; align-items: center; margin: 4px 0 14px; }
.so-svg { width: 132px; height: 132px; flex: none; background: var(--bg); border-radius: 12px; }
.so-btns { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.guide rect { fill: none; stroke: var(--line); stroke-width: .8; }
.guide line { stroke: var(--line); stroke-width: .6; stroke-dasharray: 3 3; }
.so-svg .strokes path, .pad-svg path { fill: none; stroke-linecap: round; stroke-linejoin: round; }
.so-svg .strokes path { stroke: var(--ink); stroke-width: 3.2; }
.so-svg .nums text { font: 7px system-ui; fill: var(--accent); }
.so-svg.playing .nums { opacity: .25; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 6px 12px; font-size: 14px; }

.pad { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.pad-svg { width: min(100%, 340px); aspect-ratio: 1; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; touch-action: none; user-select: none; -webkit-user-select: none; }
.pad-svg .measure { opacity: 0; }
.pad-svg .outline path { stroke: var(--line); stroke-width: 4; }
.pad-svg .done-strokes path { stroke: var(--ink); stroke-width: 4.2; }
.pad-svg .done-strokes path.shown { stroke: var(--accent); }
.pad-svg .live { fill: none; stroke: var(--ink); stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; opacity: .8; }
.pad-svg .bad-stroke { stroke: var(--again); stroke-width: 4; opacity: .9; }
.pad-svg .hint-stroke { stroke: var(--accent); stroke-width: 5; opacity: .75; }
.pad-msg { margin: 0; font-size: 14px; color: var(--muted); min-height: 1.5em; text-align: center; }
.pad-msg.no { color: var(--again); } .pad-msg.ok { color: var(--good); font-weight: 600; }
.pad-tools { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.pad-tools[hidden] { display: none; }
.q-meaning.small { font-size: 22px; padding: 6px 0 0; }
.verdict small { font-weight: 400; color: var(--muted); }
.write-head { display: flex; gap: 14px; align-items: center; margin: 4px 40px 12px 0; }
.write-head .main-reading { text-align: left; }
.pad-svg .outline[hidden] { display: none; }
.q-meaning { color: var(--ink); }

/* ---------- Stage 4: grammar ---------- */
#g-search { width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); font-size: 16px; margin: 4px 0 8px; }
.g-cat { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 0 6px; }
.g-cat small { font-weight: 400; }
.g-row-item { width: 100%; display: flex; gap: 12px; align-items: center; text-align: left; background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 6px; position: relative; }
.g-row-item::before { content: ""; position: absolute; left: 0; top: 10px; bottom: 10px; width: 3px; border-radius: 2px; background: var(--line); }
.g-row-item.learning::before { background: var(--again); } .g-row-item.known::before { background: var(--good); }
.g-n { font-size: 12px; color: var(--muted); min-width: 22px; text-align: right; }
.g-t { display: flex; flex-direction: column; }
.g-t b { font: 600 17px var(--jp); } .g-t small { color: var(--muted); font-size: 13px; }

.g-lesson .eyebrow { margin-bottom: 2px; }
.g-title { font: 700 26px var(--jp); margin: 0; text-transform: none; letter-spacing: 0; color: var(--ink); }
.g-meaning { margin: 2px 0 12px; color: var(--accent); font-weight: 600; }
.g-box { background: var(--bg); border-radius: 12px; padding: 10px 12px; margin-bottom: 12px; }
.g-label { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 0 0 4px; }
.g-form { margin: 2px 0; font: 15px/1.6 var(--jp); }
.g-explain { margin: 0 0 10px; }
.g-note { background: color-mix(in srgb, var(--again) 10%, transparent); border-radius: 10px; padding: 8px 12px; font-size: 14px; margin: 0 0 14px; }
.g-actions { margin-top: 14px; }

.g-point { margin: 0; font: 600 15px var(--jp); color: var(--accent); }
.g-point small { color: var(--muted); font-weight: 400; font-family: system-ui; }
.g-sentence, .g-full { font: 20px/2.2 var(--jp); }
.g-sentence rt, .g-full rt, .tile-w rt, .opt rt, .g-hint rt { font-size: 10px; color: var(--muted); }
.blank { display: inline-block; min-width: 3.2em; border-bottom: 2px solid var(--accent); margin: 0 3px; line-height: 1.2; }
.blank.filled { color: var(--good); font-weight: 700; min-width: 0; padding: 0 2px; }
.g-hint { color: var(--muted); font-size: 14px; margin: 0; }
.g-input { width: 100%; font: 20px var(--jp); padding: 12px 14px; border-radius: 12px; border: 1.5px solid var(--line);
  background: var(--surface); color: var(--ink); }
.g-input.right { border-color: var(--good); } .g-input.wrong { border-color: var(--again); }
.g-preview { margin: 4px 0 0; color: var(--muted); font: 15px var(--jp); min-height: 1.4em; }
.g-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 8px; }
.g-row[hidden] { display: none; }
.g-row .btn { padding: 10px 22px; }
.btn:disabled { opacity: .45; }
.g-answer { margin: 0 0 6px; }
.g-built { min-height: 56px; border: 1.5px dashed var(--line); border-radius: 12px; padding: 6px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.g-built.right { border-style: solid; border-color: var(--good); } .g-built.wrong { border-style: solid; border-color: var(--again); }
.g-placeholder { color: var(--muted); font-size: 14px; padding: 0 6px; }
.g-tiles { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.g-tiles[hidden] { display: none; }
.tile-w { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 6px 10px; font: 18px/1.9 var(--jp); }
.today-nums.three { grid-template-columns: 1fr 1fr 1fr; }
.today-nums.three b { font-size: 34px; }
.more-row { display: flex; gap: 8px; justify-content: center; margin-top: 10px; flex-wrap: wrap; }
.tabs button { min-width: 0; }
.sheet .q-head { padding-right: 44px; }

/* ---------- ⚑ flags ---------- */
.flag-btn { border: 0; background: none; font-size: 20px; color: var(--muted); padding: 0 6px; opacity: .55; }
.flag-btn.on { color: var(--accent); opacity: 1; }
.card-head > span { display: flex; align-items: center; }
.g-top { display: flex; justify-content: space-between; align-items: center; padding-right: 40px; }
.q-head > span { display: flex; align-items: center; gap: 4px; }
.flag-item { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; }
.flag-top { display: flex; justify-content: space-between; align-items: center; }
.flag-text { white-space: pre-wrap; font: 14px/1.6 var(--jp); margin: 6px 0; color: var(--ink); }
.flag-note { width: 100%; min-height: 54px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); font: 14px system-ui; padding: 8px; }
#toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(20px); background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 99px; font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; z-index: 50; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%); }

/* ---------- Stage 5: streak, weakest, chart ---------- */
.streak-row { display: flex; justify-content: space-between; align-items: center; margin: 2px 2px 10px; }
.streak { font-weight: 700; }
.weak-list { display: flex; flex-wrap: wrap; gap: 6px; }
.weak { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 4px 10px; font: 18px var(--jp);
  display: flex; flex-direction: column; align-items: center; line-height: 1.3; }
.weak.k { font: 26px var(--jp-serif); }
.weak small { font: 11px system-ui; color: var(--muted); }
.chart-tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 8px; }
.chart-tiles div { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.chart-tiles b { display: block; font-size: 22px; } .chart-tiles span { font-size: 12px; color: var(--muted); }
.chart { width: 100%; height: auto; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.chart .bar { fill: var(--good); }
.chart .hit { fill: transparent; }
.chart .bar-g:hover .bar { opacity: .8; }
.chart .base { stroke: var(--line); stroke-width: 1; }
.chart .grid { stroke: var(--line); stroke-width: .5; stroke-dasharray: 2 3; }
.chart .ax { font: 8px system-ui; fill: var(--muted); text-anchor: middle; }
.chart-tip { margin: 4px 0 0; }
.acct { display: flex; justify-content: space-between; align-items: center; gap: 10px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.acct > div { display: flex; flex-direction: column; }

/* ---------- app name: N4 the fun way ---------- */
.top h1.brand { display: flex; align-items: baseline; gap: 6px; }
.brand .b-n4 { font: 800 24px system-ui; color: #fff; background: var(--accent); border: 0; border-radius: 8px; padding: 1px 8px; margin: 0; }
.brand .b-rest { font: italic 600 17px var(--jp-serif); color: var(--ink); border: 0; padding: 0; margin: 0; letter-spacing: .01em; }
