:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #efebe4;
  --ink: #1f1d1a;
  --ink-2: #5d5850;
  --ink-3: #8f887d;
  --line: #e3ddd3;
  --accent: #b5442c;        /* vermilion */
  --accent-ink: #ffffff;
  --accent-soft: #f6e3dc;
  --indigo: #2f4a7a;
  --indigo-soft: #e2e8f3;
  --good: #2f7a4f;
  --good-soft: #dcefe3;
  --bad: #b23a3a;
  --bad-soft: #f7e0e0;
  --warn: #9a6a12;
  --warn-soft: #f6ebd3;
  --shadow: 0 1px 2px rgba(40, 30, 20, .06), 0 4px 14px rgba(40, 30, 20, .05);
  --radius: 16px;
  --jp: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", system-ui, sans-serif;
  --jp-serif: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
  --ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Hiragino Sans", system-ui, sans-serif;
  --tabbar-h: 58px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171b; --surface: #202227; --surface-2: #2a2c32; --ink: #eceae6; --ink-2: #b5b0a8; --ink-3: #8a857d;
    --line: #34363d; --accent: #e0694f; --accent-ink: #1a0d09; --accent-soft: #3a2420; --indigo: #8fa8d8; --indigo-soft: #26304a;
    --good: #6cc492; --good-soft: #1f3529; --bad: #ec7f7f; --bad-soft: #3d2324; --warn: #e2b45a; --warn-soft: #3a2f1a;
    --shadow: 0 1px 2px rgba(0,0,0,.3); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16171b; --surface: #202227; --surface-2: #2a2c32; --ink: #eceae6; --ink-2: #b5b0a8; --ink-3: #8a857d;
  --line: #34363d; --accent: #e0694f; --accent-ink: #1a0d09; --accent-soft: #3a2420; --indigo: #8fa8d8; --indigo-soft: #26304a;
  --good: #6cc492; --good-soft: #1f3529; --bad: #ec7f7f; --bad-soft: #3d2324; --warn: #e2b45a; --warn-soft: #3a2f1a;
  --shadow: 0 1px 2px rgba(0,0,0,.3); color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--ink); font-family: var(--ui); font-size: 16px; line-height: 1.5;
  -webkit-text-size-adjust: 100%; overflow-x: hidden;
  padding-top: env(safe-area-inset-top);
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 4px;
  height: 52px; padding: 0 8px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(14px); -webkit-backdrop-filter: saturate(1.4) blur(14px);
}
.topbar h1 { flex: 1; margin: 0 8px; font-size: 19px; font-weight: 700; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border: 0; background: none; border-radius: 12px; color: var(--ink-2); cursor: pointer; }
.icon-btn:active { background: var(--surface-2); }

main { padding: 4px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 28px); max-width: 680px; margin: 0 auto; outline: none; }
body.in-session main { padding-bottom: calc(env(safe-area-inset-bottom) + 120px); }
body.in-session .tabbar { display: none; }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: flex;
  padding: 4px 6px calc(env(safe-area-inset-bottom) + 2px); background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(16px); -webkit-backdrop-filter: saturate(1.4) blur(16px);
  border-top: 1px solid var(--line);
}
.tabbar button { position: relative; flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; height: var(--tabbar-h); justify-content: center; border: 0; background: none; color: var(--ink-3); font-size: 11px; font-weight: 600; cursor: pointer; }
.tabbar svg { width: 26px; height: 26px; }
.tabbar button.active { color: var(--accent); }
.badge { position: absolute; top: 6px; left: calc(50% + 6px); min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--accent); color: var(--accent-ink); font-size: 11px; font-style: normal; font-weight: 700; line-height: 18px; text-align: center; }

/* ---------- common blocks ---------- */
.section-label { margin: 22px 4px 8px; font-size: 13px; font-weight: 700; color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px; }
.card.flat { box-shadow: none; border: 1px solid var(--line); }
.muted { color: var(--ink-2); }
.small { font-size: 14px; }
.tiny { font-size: 12px; color: var(--ink-3); }
.row { display: flex; align-items: center; gap: 12px; }
.grow { flex: 1; min-width: 0; }
.jp { font-family: var(--jp); }

/* countdown hero */
.hero { padding: 20px 18px; background: var(--ink); color: var(--bg); border-radius: 20px; margin: 8px 0 14px; position: relative; overflow: hidden; }
.hero .days { font-size: 56px; font-weight: 800; line-height: 1; letter-spacing: -.03em; }
.hero .days small { font-size: 18px; font-weight: 600; margin-left: 6px; letter-spacing: 0; }
.hero .sub { margin-top: 8px; font-size: 14px; opacity: .78; }
.hero .stamp { position: absolute; right: 16px; top: 16px; width: 64px; height: 64px; border-radius: 50%; border: 2.5px solid var(--accent); color: var(--accent); display: grid; place-items: center; font-family: var(--jp-serif); font-weight: 700; font-size: 22px; transform: rotate(-8deg); }
.hero .phase { display: inline-block; margin-top: 12px; padding: 4px 10px; border-radius: 99px; background: color-mix(in srgb, var(--bg) 16%, transparent); font-size: 13px; font-weight: 600; }

/* list items */
.list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 12px; }
.item { display: flex; align-items: center; gap: 14px; width: 100%; padding: 14px 16px; border: 0; background: none; text-align: left; cursor: pointer; min-height: 64px; }
.item + .item { border-top: 1px solid var(--line); }
.item:active { background: var(--surface-2); }
.item .ico { flex: none; width: 40px; height: 40px; border-radius: 11px; display: grid; place-items: center; font-family: var(--jp); font-weight: 700; font-size: 17px; background: var(--accent-soft); color: var(--accent); }
.item .ico.indigo { background: var(--indigo-soft); color: var(--indigo); }
.item .ico.good { background: var(--good-soft); color: var(--good); }
.item .ico.warn { background: var(--warn-soft); color: var(--warn); }
.item .t { font-weight: 650; }
.item .d { font-size: 13px; color: var(--ink-2); margin-top: 1px; }
.item .chev { color: var(--ink-3); flex: none; }
.item .count { flex: none; font-size: 14px; font-weight: 700; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.item.done .t { color: var(--ink-3); }
.check { flex: none; width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--line); display: grid; place-items: center; }
.item.done .check { background: var(--good); border-color: var(--good); color: #fff; }

/* meters */
.meter { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.meter > i { display: block; height: 100%; background: var(--indigo); border-radius: 4px; }
.meter.accent > i { background: var(--accent); }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); padding: 12px 14px; }
.stat .k { font-size: 12px; color: var(--ink-3); font-weight: 600; }
.stat .v { font-size: 22px; font-weight: 750; font-variant-numeric: tabular-nums; margin: 2px 0 6px; }
.stat .v small { font-size: 13px; color: var(--ink-3); font-weight: 600; }

/* buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 50px; padding: 0 20px; border-radius: 14px; border: 0; background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 17px; cursor: pointer; width: 100%; }
.btn:active { filter: brightness(.92); }
.btn.secondary { background: var(--surface-2); color: var(--ink); }
.btn.ghost { background: none; color: var(--accent); }
.btn.small { min-height: 40px; font-size: 15px; width: auto; padding: 0 14px; border-radius: 11px; }
.btn:disabled { opacity: .45; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }

/* ---------- session ---------- */
.progress-top { display: flex; align-items: center; gap: 10px; margin: 6px 0 16px; }
.progress-top .meter { flex: 1; }
.progress-top .tiny { font-variant-numeric: tabular-nums; }
.qtype { display: inline-block; font-size: 12px; font-weight: 700; color: var(--indigo); background: var(--indigo-soft); padding: 3px 9px; border-radius: 99px; margin-bottom: 10px; }
.prompt { font-family: var(--jp); font-size: 20px; line-height: 1.85; margin: 4px 0 18px; word-break: break-word; }
.prompt u { text-decoration: none; border-bottom: 2px solid var(--accent); padding-bottom: 1px; }
.prompt .blank { display: inline-block; min-width: 3.2em; border-bottom: 2px solid var(--ink-3); text-align: center; color: var(--ink-3); }
.prompt .slot { display: inline-block; min-width: 2.4em; border-bottom: 2px solid var(--ink-3); text-align: center; margin: 0 2px; color: var(--ink-3); font-size: 15px; }
.prompt .slot.star { border-color: var(--accent); color: var(--accent); }
.headword { font-family: var(--jp); font-size: 30px; font-weight: 700; margin: 6px 0 16px; }
.options { display: flex; flex-direction: column; gap: 10px; }
.opt { display: flex; align-items: flex-start; gap: 12px; width: 100%; min-height: 56px; padding: 14px 14px; border-radius: 14px; border: 1.5px solid var(--line); background: var(--surface); text-align: left; cursor: pointer; font-family: var(--jp); font-size: 17px; line-height: 1.6; }
.opt .n { flex: none; width: 26px; height: 26px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; font-family: var(--ui); font-size: 13px; font-weight: 700; color: var(--ink-2); margin-top: 1px; }
.opt:active { background: var(--surface-2); }
.opt.correct { border-color: var(--good); background: var(--good-soft); }
.opt.correct .n { background: var(--good); color: #fff; }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.opt.wrong .n { background: var(--bad); color: #fff; }
.opt.dim { opacity: .55; }
.opt[disabled] { cursor: default; }
.opt-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(64px, 1fr)); gap: 10px; }
.opt-grid .opt { justify-content: center; font-family: var(--ui); font-weight: 700; font-size: 20px; }

.explain { margin-top: 16px; padding: 14px 16px; border-radius: 14px; background: var(--surface); border-left: 4px solid var(--indigo); box-shadow: var(--shadow); font-size: 15.5px; line-height: 1.65; }
.explain .verdict { font-weight: 800; font-size: 17px; margin-bottom: 4px; }
.explain .verdict.ok { color: var(--good); }
.explain .verdict.ng { color: var(--bad); }
.explain .jp-line { font-family: var(--jp); font-size: 16px; }
.explain hr { border: 0; border-top: 1px solid var(--line); margin: 10px 0; }

.session-footer { position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; padding: 12px 16px calc(env(safe-area-inset-bottom) + 12px); background: linear-gradient(to top, var(--bg) 70%, transparent); }
.session-footer .inner { max-width: 680px; margin: 0 auto; }

/* ordering */
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 16px; }
.chip { font-family: var(--jp); font-size: 17px; padding: 10px 14px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface); cursor: pointer; min-height: 46px; }
.chip.used { opacity: .3; pointer-events: none; }
.answer-line { min-height: 58px; padding: 10px; border-radius: 14px; border: 1.5px dashed var(--line); display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.answer-line .chip { background: var(--indigo-soft); border-color: transparent; position: relative; }
.answer-line .chip.star-pos::after { content: "★"; position: absolute; top: -9px; right: -6px; font-size: 13px; color: var(--accent); }
.answer-line .placeholder { color: var(--ink-3); font-size: 14px; padding: 0 6px; }

/* flashcards */
.flash { min-height: 260px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 28px 18px; cursor: pointer; }
.flash .front { font-family: var(--jp); font-size: 40px; font-weight: 700; line-height: 1.3; }
.flash .front.grammar { font-size: 28px; }
.flash .reading { font-family: var(--jp); font-size: 20px; color: var(--accent); margin-top: 12px; }
.flash .meaning { font-size: 18px; margin-top: 8px; }
.flash .ex { font-family: var(--jp); font-size: 16px; color: var(--ink-2); margin-top: 14px; line-height: 1.7; text-align: left; }
.flash .hint { margin-top: 18px; font-size: 13px; color: var(--ink-3); }
.grade { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.grade .btn { min-height: 56px; flex-direction: column; gap: 0; font-size: 16px; }
.grade .btn small { font-size: 11px; font-weight: 600; opacity: .75; }
.grade .again { background: var(--bad-soft); color: var(--bad); }
.grade .hard { background: var(--warn-soft); color: var(--warn); }
.grade .good { background: var(--good-soft); color: var(--good); }

/* reading */
.passage { font-family: var(--jp); font-size: 17px; line-height: 2; background: var(--surface); border-radius: var(--radius); padding: 18px 18px; box-shadow: var(--shadow); white-space: pre-wrap; word-break: break-word; }
body.serif .passage, body.serif .prompt { font-family: var(--jp-serif); }
.passage u { text-decoration: none; border-bottom: 2px solid var(--accent); }
.passage h3 { font-size: 16px; margin: 0 0 6px; }
.timer { display: inline-flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; font-weight: 700; font-size: 15px; padding: 5px 10px; border-radius: 99px; background: var(--surface-2); }
.timer.over { background: var(--bad-soft); color: var(--bad); }
.timer.near { background: var(--warn-soft); color: var(--warn); }
.qblock { margin-top: 18px; }
.qblock .qtext { font-family: var(--jp); font-size: 17px; font-weight: 600; margin-bottom: 10px; line-height: 1.7; }

/* listening */
.player { display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 26px 16px; }
.play-btn { width: 88px; height: 88px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; cursor: pointer; box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 35%, transparent); }
.play-btn svg { width: 38px; height: 38px; }
.play-btn.playing { background: var(--ink); color: var(--bg); }
.wave { display: flex; gap: 4px; height: 22px; align-items: center; }
.wave i { width: 4px; height: 6px; border-radius: 2px; background: var(--ink-3); }
.playing-now .wave i { animation: wv 1s ease-in-out infinite; background: var(--accent); }
.playing-now .wave i:nth-child(2) { animation-delay: .15s } .playing-now .wave i:nth-child(3) { animation-delay: .3s } .playing-now .wave i:nth-child(4) { animation-delay: .45s } .playing-now .wave i:nth-child(5) { animation-delay: .6s }
@keyframes wv { 0%,100% { height: 6px } 50% { height: 22px } }
.script { font-family: var(--jp); font-size: 16px; line-height: 1.85; }
.script p { margin: 0 0 8px; }
.script b { display: inline-block; min-width: 2.2em; color: var(--indigo); }
details.script-box { margin-top: 12px; }
details.script-box summary { cursor: pointer; font-weight: 700; color: var(--indigo); padding: 8px 0; }

/* library */
.search { width: 100%; height: 46px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); padding: 0 14px; font-size: 16px; color: var(--ink); margin-bottom: 12px; }
.gp { padding: 14px 16px; }
.gp + .gp { border-top: 1px solid var(--line); }
.gp .g { font-family: var(--jp); font-size: 18px; font-weight: 700; }
.gp .m { font-size: 15px; margin-top: 2px; }
.gp .c { font-family: var(--jp); font-size: 13px; color: var(--ink-3); margin-top: 4px; }
.gp .ex { font-family: var(--jp); font-size: 15px; color: var(--ink-2); margin-top: 6px; line-height: 1.7; }
.gp .n { font-size: 13.5px; color: var(--ink-2); margin-top: 6px; padding: 8px 10px; background: var(--surface-2); border-radius: 10px; }
.wl { display: grid; grid-template-columns: auto 1fr; gap: 2px 14px; padding: 12px 16px; align-items: baseline; }
.wl + .wl { border-top: 1px solid var(--line); }
.wl .w { font-family: var(--jp); font-size: 18px; font-weight: 700; }
.wl .r { font-family: var(--jp); font-size: 14px; color: var(--accent); }
.wl .m { grid-column: 1 / -1; font-size: 14.5px; color: var(--ink-2); }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; margin-bottom: 12px; }
.seg button { flex: 1; border: 0; background: none; height: 38px; border-radius: 9px; font-weight: 650; font-size: 14px; color: var(--ink-2); cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.prose { font-size: 15.5px; line-height: 1.7; }
.prose h3 { font-size: 17px; margin: 18px 0 6px; }
.prose h3:first-child { margin-top: 0; }
.prose p { margin: 0 0 10px; }
.prose ul { margin: 0 0 10px; padding-left: 20px; }
.prose li { margin-bottom: 4px; }
.tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 6px 0 12px; }
.tbl { border-collapse: collapse; width: 100%; font-size: 14px; min-width: 420px; }
.tbl th, .tbl td { border-bottom: 1px solid var(--line); padding: 8px 6px; text-align: left; vertical-align: top; }
.tbl th { font-size: 12px; color: var(--ink-3); text-transform: uppercase; letter-spacing: .04em; }
.tbl td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* settings */
.field { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; min-height: 56px; }
.field + .field { border-top: 1px solid var(--line); }
.field label { font-weight: 600; }
.field select, .field input[type="date"] { font: inherit; font-size: 15px; color: var(--ink); background: var(--surface-2); border: 0; border-radius: 10px; padding: 8px 10px; max-width: 55%; }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; border-radius: 15px; background: var(--line); transition: .2s; cursor: pointer; }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.switch input:checked + span { background: var(--good); }
.switch input:checked + span::after { transform: translateX(20px); }

/* result */
.result-score { text-align: center; padding: 26px 16px; }
.result-score .big { font-size: 60px; font-weight: 800; letter-spacing: -.03em; line-height: 1; }
.result-score .big small { font-size: 22px; color: var(--ink-3); }
.wrong-list .item { min-height: 0; align-items: flex-start; cursor: default; }
.wrong-list .jp { font-size: 15px; }

.empty { text-align: center; padding: 34px 20px; color: var(--ink-2); }
.empty .em { font-size: 40px; margin-bottom: 8px; }

.toast { position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 20px); transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 600; opacity: 0; pointer-events: none; transition: .25s; z-index: 50; max-width: calc(100% - 32px); text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.tip { display: flex; gap: 10px; padding: 12px 14px; border-radius: 14px; background: var(--warn-soft); color: var(--ink); font-size: 14.5px; line-height: 1.55; margin-bottom: 12px; }
.tip b { color: var(--warn); }
