:root {
  --bg: #f6f4f1;
  --surface: #ffffff;
  --surface-2: #f0ece6;
  --text: #1d1b19;
  --muted: #6d6760;
  --line: #e3ddd5;
  --accent: #c8102e;
  --accent-ink: #ffffff;
  --accent-soft: #fbe6e9;
  --indigo: #2f3e8f;
  --indigo-soft: #e6e9f7;
  --ok: #1f8a4c;
  --ok-soft: #dff3e7;
  --bad: #c62828;
  --bad-soft: #fde4e4;
  --warn: #b26a00;
  --warn-soft: #fff1d6;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
  --radius: 16px;
  --jp: "Noto Sans JP", "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
  --th: "Noto Sans Thai", "Sarabun", "Tahoma", sans-serif;
  --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121212; --surface: #1c1c1e; --surface-2: #26262a; --text: #f1eeea; --muted: #a29b93;
    --line: #34343a; --accent: #ff4d5e; --accent-soft: #3a1b20; --indigo: #8fa0ff; --indigo-soft: #222746;
    --ok: #4cc685; --ok-soft: #173426; --bad: #ff6b6b; --bad-soft: #3d1c1c; --warn: #ffb547; --warn-soft: #3a2c12;
    --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121212; --surface: #1c1c1e; --surface-2: #26262a; --text: #f1eeea; --muted: #a29b93;
  --line: #34343a; --accent: #ff4d5e; --accent-soft: #3a1b20; --indigo: #8fa0ff; --indigo-soft: #222746;
  --ok: #4cc685; --ok-soft: #173426; --bad: #ff6b6b; --bad-soft: #3d1c1c; --warn: #ffb547; --warn-soft: #3a2c12;
  --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: var(--th); font-size: 16px; line-height: 1.6;
  padding-top: calc(56px + env(safe-area-inset-top));
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.jp, [lang="ja"] { font-family: var(--jp); }
ruby rt { font-size: .55em; color: var(--muted); }
u.tgt { text-decoration: underline 2px var(--accent); text-underline-offset: 4px; }
.blank { display: inline-block; min-width: 2.8em; height: 1.3em; border-bottom: 2px solid var(--muted); margin: 0 3px; vertical-align: -0.15em; text-align: center; line-height: 1.3em; }
.blank.star { color: var(--accent); border-color: var(--accent); font-size: .85em; }

/* ---------- top bar ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 20;
  height: calc(56px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 8px 0;
  display: flex; align-items: center; gap: 4px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; flex: 1; min-width: 0; padding-left: 6px; }
.brand #pageTitle { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 10px; flex: none;
  display: grid; place-items: center; background: var(--accent); color: var(--accent-ink);
  font-family: var(--jp); font-size: 18px;
}
.topbar-actions { display: flex; gap: 2px; }
.icon-btn {
  min-width: 40px; height: 40px; border-radius: 12px; border: 0; background: transparent;
  display: inline-grid; place-items: center; font-size: 20px; cursor: pointer; padding: 0 6px;
}
.icon-btn:active { background: var(--surface-2); }
#backBtn { font-size: 32px; line-height: 1; }

/* ---------- bottom nav ---------- */
.bottomnav {
  position: fixed; inset: auto 0 0 0; z-index: 20;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.bottomnav a {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 11.5px; color: var(--muted); gap: 1px;
}
.bottomnav a span { font-size: 21px; line-height: 1.2; filter: grayscale(1); opacity: .7; }
.bottomnav a.active { color: var(--accent); font-weight: 700; }
.bottomnav a.active span { filter: none; opacity: 1; }
.bottomnav b {
  position: absolute; top: 6px; left: calc(50% + 6px);
  background: var(--accent); color: var(--accent-ink); font-size: 10px; border-radius: 99px; padding: 0 6px; line-height: 16px;
}

/* ---------- layout ---------- */
.view { max-width: 760px; margin: 0 auto; padding: 16px 16px 32px; outline: none; }
.stack > * + * { margin-top: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.spread { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 10px; }
@media (min-width: 600px) { .grid2.md3 { grid-template-columns: repeat(3, minmax(0,1fr)); } }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.tiny { font-size: 11.5px; }
.center { text-align: center; }
.mt0 { margin-top: 0 !important; }
.mt { margin-top: 16px; }
.mb { margin-bottom: 16px; }
h1, h2, h3 { line-height: 1.3; margin: 0; }
h1 { font-size: 24px; }
h2 { font-size: 19px; }
h3 { font-size: 16px; }
.section-title { display: flex; align-items: baseline; justify-content: space-between; margin: 22px 0 10px; }
.section-title h2 { font-size: 17px; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.card.flat { box-shadow: none; }
.card.tap { cursor: pointer; transition: transform .08s; display: block; }
.card.tap:active { transform: scale(.98); }
.card.tap.row { display: flex; gap: 14px; align-items: center; }
.card.hero {
  background: linear-gradient(135deg, var(--accent), #8f0a20); color: #fff; border: 0;
}
.card.hero .muted { color: rgba(255,255,255,.8); }
.card.indigo { background: linear-gradient(135deg, #2f3e8f, #1b2560); color: #fff; border: 0; }
.card.indigo .muted { color: rgba(255,255,255,.8); }

.tile { display: flex; flex-direction: column; gap: 4px; min-height: 104px; }
.tile .ico { font-size: 26px; }
.tile .t { font-weight: 700; }
.tile .s { font-size: 12.5px; color: var(--muted); }

.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.list > .item { display: flex; gap: 12px; align-items: center; padding: 12px 14px; border-top: 1px solid var(--line); cursor: pointer; }
.list > .item:first-child { border-top: 0; }
.list > .item:active { background: var(--surface-2); }
.item .main { flex: 1; min-width: 0; }
.item .w { font-family: var(--jp); font-size: 19px; font-weight: 500; }
.item .r { font-family: var(--jp); color: var(--muted); font-size: 13px; }
.item .m { font-size: 14px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .chev { color: var(--muted); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--line); flex: none; }
.dot.learning { background: var(--warn); }
.dot.young { background: var(--indigo); }
.dot.mature { background: var(--ok); }

/* ---------- buttons / inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 600; cursor: pointer; text-align: center;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.indigo { background: var(--indigo); color: #fff; border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn.sm { min-height: 36px; padding: 0 12px; border-radius: 10px; font-size: 14px; }
.btn[disabled] { opacity: .45; pointer-events: none; }
.btn.white { background: #fff; color: #8f0a20; border: 0; }

.input, select.input {
  width: 100%; min-height: 46px; padding: 0 14px; border-radius: 14px;
  border: 1px solid var(--line); background: var(--surface); outline: none;
}
.input:focus { border-color: var(--indigo); }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 7px 14px; border-radius: 99px; border: 1px solid var(--line);
  background: var(--surface); font-size: 14px; cursor: pointer; white-space: nowrap;
}
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); padding: 4px; border-radius: 14px; gap: 4px; }
.seg button { border: 0; background: transparent; padding: 8px 4px; border-radius: 10px; font-weight: 600; cursor: pointer; font-size: 14px; }
.seg button.on { background: var(--surface); box-shadow: var(--shadow); color: var(--accent); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.red { background: var(--accent-soft); color: var(--accent); }
.badge.indigo { background: var(--indigo-soft); color: var(--indigo); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }

.progress { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; transition: width .3s; }
.card.hero .progress, .card.indigo .progress { background: rgba(255,255,255,.25); }
.card.hero .progress > i, .card.indigo .progress > i { background: #fff; }

.stat { display: flex; flex-direction: column; }
.stat b { font-size: 24px; line-height: 1.2; }
.stat span { font-size: 12px; color: var(--muted); }
.card.hero .stat span, .card.indigo .stat span { color: rgba(255,255,255,.8); }

/* ---------- level tabs ---------- */
.levels { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.levels a, .levels button {
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 8px 0;
  text-align: center; font-weight: 700; cursor: pointer; font-size: 14px;
}
.levels .on { background: var(--accent); color: #fff; border-color: transparent; }

/* ---------- flashcard ---------- */
.fc-wrap { perspective: 1200px; }
.fc {
  position: relative; min-height: 340px; border-radius: 22px; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px 18px; text-align: center;
}
.fc .big { font-family: var(--jp); font-size: 48px; font-weight: 500; line-height: 1.25; word-break: keep-all; }
.fc .big.kanji { font-size: 110px; line-height: 1.1; }
.fc .reading { font-family: var(--jp); font-size: 20px; color: var(--muted); }
.fc .meaning { font-size: 20px; font-weight: 600; margin-top: 8px; }
.fc .hint { position: absolute; bottom: 12px; font-size: 12px; color: var(--muted); }
.fc .ex { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--line); width: 100%; text-align: left; font-size: 14px; }
.grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 12px; }
.grades button { border: 0; border-radius: 14px; padding: 10px 2px; font-weight: 700; cursor: pointer; display: flex; flex-direction: column; align-items: center; font-size: 14px; }
.grades button small { font-weight: 400; font-size: 11px; opacity: .8; }
.g0 { background: var(--bad-soft); color: var(--bad); }
.g1 { background: var(--warn-soft); color: var(--warn); }
.g2 { background: var(--ok-soft); color: var(--ok); }
.g3 { background: var(--indigo-soft); color: var(--indigo); }

/* ---------- quiz ---------- */
.q-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.q-stem { font-family: var(--jp); font-size: 20px; line-height: 1.9; word-break: break-word; }
.q-stem.big { font-size: 40px; text-align: center; padding: 12px 0; }
.q-prompt { font-size: 13.5px; color: var(--muted); margin-bottom: 8px; }
.opts { display: grid; gap: 10px; margin-top: 16px; }
.opt {
  text-align: left; padding: 13px 14px; border-radius: 14px; border: 1.5px solid var(--line);
  background: var(--surface); cursor: pointer; font-family: var(--jp); font-size: 17px; display: flex; gap: 10px; align-items: flex-start;
}
.opt .n { flex: none; width: 24px; height: 24px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; font-size: 13px; font-family: var(--th); margin-top: 2px; }
.opt.sel { border-color: var(--indigo); background: var(--indigo-soft); }
.opt.right { border-color: var(--ok); background: var(--ok-soft); }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.opt[disabled] { cursor: default; }
.explain { margin-top: 14px; padding: 14px; border-radius: 14px; background: var(--surface-2); font-size: 14.5px; }
.explain.ok { background: var(--ok-soft); }
.explain.bad { background: var(--bad-soft); }
.passage {
  font-family: var(--jp); font-size: 17px; line-height: 2; white-space: pre-wrap; word-break: break-word;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px;
}
.passage.sm { font-size: 15.5px; }
.qnav { display: flex; gap: 6px; flex-wrap: wrap; }
.qnav button { width: 34px; height: 34px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-size: 13px; }
.qnav button.done { background: var(--indigo-soft); border-color: var(--indigo); color: var(--indigo); }
.qnav button.cur { outline: 2px solid var(--accent); }
.qnav button.right { background: var(--ok-soft); border-color: var(--ok); color: var(--ok); }
.qnav button.wrong { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; padding: 4px 10px; border-radius: 10px; background: var(--surface-2); }
.timer.low { background: var(--bad-soft); color: var(--bad); }

/* ---------- kana ---------- */
.kana-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.kana-grid.y3 { grid-template-columns: repeat(3, 1fr); }
.kana {
  aspect-ratio: 1 / 1; border-radius: 12px; background: var(--surface); border: 1px solid var(--line);
  display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: pointer;
}
.kana.empty { visibility: hidden; }
.kana b { font-family: var(--jp); font-size: 26px; font-weight: 500; line-height: 1.1; }
.kana small { font-size: 11px; color: var(--muted); }
.kana:active { background: var(--accent-soft); }

/* ---------- kanji ---------- */
.kanji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 8px; }
.kj {
  aspect-ratio: 1/1; border-radius: 14px; background: var(--surface); border: 1px solid var(--line);
  display: grid; place-items: center; font-family: var(--jp); font-size: 30px; cursor: pointer; position: relative;
}
.kj .dot { position: absolute; top: 6px; right: 6px; width: 7px; height: 7px; }
.kanji-hero { font-family: var(--jp); font-size: 120px; line-height: 1; text-align: center; }
dl.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; }
dl.kv dt { color: var(--muted); font-size: 13px; padding-top: 3px; }
dl.kv dd { margin: 0; font-family: var(--jp); }

/* ---------- grammar ---------- */
.g-pattern { font-family: var(--jp); font-size: 26px; font-weight: 700; }
.formula { font-family: var(--jp); background: var(--indigo-soft); color: var(--indigo); padding: 10px 14px; border-radius: 12px; font-weight: 500; }
.ex-item { padding: 12px 0; border-top: 1px solid var(--line); }
.ex-item:first-child { border-top: 0; padding-top: 0; }
.ex-item .j { font-family: var(--jp); font-size: 17px; }
.ex-item .rd { font-family: var(--jp); font-size: 13px; color: var(--muted); }
.ex-item .t { font-size: 14.5px; }
.notes { margin: 0; padding-left: 20px; }
.notes li { margin: 4px 0; }

/* ---------- listening ---------- */
.script .line { display: flex; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.script .line:first-child { border-top: 0; }
.script .sp { flex: none; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 700; }
.sp.M { background: var(--indigo-soft); color: var(--indigo); }
.sp.F { background: var(--accent-soft); color: var(--accent); }
.sp.N { background: var(--surface-2); color: var(--muted); }
.script .tx { font-family: var(--jp); }
.player { display: flex; align-items: center; gap: 14px; }
.play-btn {
  width: 64px; height: 64px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; font-size: 26px; cursor: pointer; flex: none;
  display: grid; place-items: center; box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 40%, transparent);
}
.play-btn.playing { animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { transform: scale(1.06); } }

/* ---------- misc ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .25s; z-index: 50; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.empty-state { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty-state .ico { font-size: 44px; }
.loading { text-align: center; padding: 48px 0; color: var(--muted); }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 0 auto 10px; }
@keyframes spin { to { transform: rotate(360deg); } }
.ring { --p: 0; width: 64px; height: 64px; border-radius: 50%; flex: none;
  background: conic-gradient(var(--accent) calc(var(--p) * 1%), var(--surface-2) 0);
  display: grid; place-items: center; }
.ring::before { content: attr(data-label); width: 50px; height: 50px; border-radius: 50%; background: var(--surface); display: grid; place-items: center; font-weight: 700; font-size: 13px; }
.heat { display: grid; grid-template-columns: repeat(15, 1fr); gap: 3px; }
.heat i { aspect-ratio: 1/1; border-radius: 3px; background: var(--surface-2); }
.heat i.l1 { background: color-mix(in srgb, var(--ok) 35%, var(--surface-2)); }
.heat i.l2 { background: color-mix(in srgb, var(--ok) 65%, var(--surface-2)); }
.heat i.l3 { background: var(--ok); }
.bar-row { display: grid; grid-template-columns: 84px 1fr 44px; gap: 8px; align-items: center; font-size: 13px; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
table.tbl th, table.tbl td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
table.tbl th { color: var(--muted); font-weight: 500; font-size: 12.5px; }
.result-big { font-size: 46px; font-weight: 800; line-height: 1; }
.pass { color: var(--ok); }
.fail { color: var(--bad); }
.sticky-bottom {
  position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 8px); z-index: 5;
  background: var(--bg); padding: 8px 0 0;
}
details.card > summary { cursor: pointer; font-weight: 600; list-style: none; }
details.card > summary::-webkit-details-marker { display: none; }
details.card > summary::after { content: "▾"; float: right; color: var(--muted); }
details.card[open] > summary { margin-bottom: 10px; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); }
.switch:first-child { border-top: 0; }
.switch input[type="checkbox"] { width: 44px; height: 26px; accent-color: var(--accent); }
.kbd-hide { display: none; }
@media (min-width: 760px) {
  .view { padding-top: 24px; }
}
