:root {
  --bg: #f6f1e7; --paper: #fffdf8; --ink: #2b2620; --muted: #7a7064; --line: #e4dccd;
  --accent: #9a3b2e; --accent-soft: #f3e0d8; --grammar: #2f5d8a; --grammar-soft: #dde8f3;
  --ok: #2f6b3a; --ok-soft: #dcebdc; --bad: #9a2e2e; --bad-soft: #f2dcdc; --warn-soft: #f4ead0;
  --jp: "Noto Serif CJK JP", "Noto Serif JP", "Noto Sans CJK JP", "Hiragino Mincho ProN", serif;
  --ui: system-ui, "Noto Sans CJK JP", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #1b1916; --paper: #24211d; --ink: #ece5d8; --muted: #a39887; --line: #3a352e;
    --accent: #e08a74; --accent-soft: #43302a; --grammar: #8db8e0; --grammar-soft: #26364a;
    --ok: #8fcf98; --ok-soft: #233a27; --bad: #ec9a9a; --bad-soft: #432626; --warn-soft: #3d3522; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--ui); line-height: 1.5; }
a { color: var(--accent); }
header.top { max-width: 880px; margin: 0 auto; padding: 20px 24px 8px; display: flex; align-items: baseline; gap: 16px; flex-wrap: wrap; }
header.top h1 { font-family: var(--jp); font-size: 1.5rem; margin: 0; font-weight: 600; }
header.top h1 a { color: inherit; text-decoration: none; }
header.top .spacer { flex: 1; }
main { max-width: 880px; margin: 0 auto; padding: 8px 24px 80px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
button, .btn { font: inherit; font-size: .95rem; border: 1px solid var(--line); background: var(--paper); color: var(--ink);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; text-decoration: none; display: inline-block; }
button:hover, .btn:hover { border-color: var(--muted); }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button:disabled { opacity: .5; cursor: not-allowed; }
.chip { display: inline-block; font-size: .75rem; padding: 2px 8px; border-radius: 99px; background: var(--line); color: var(--ink); white-space: nowrap; }
.chip.weak-words, .chip.daily { background: var(--accent-soft); }
.chip.daily { outline: 1px solid var(--accent); }
.chip.grammar { background: var(--grammar-soft); }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
.chip.warn { background: var(--warn-soft); }

/* Home */
.tabs { display: flex; gap: 6px; margin: 12px 0 16px; flex-wrap: wrap; }
.tabs button[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.cards { display: grid; gap: 12px; }
.card { display: grid; grid-template-columns: 1fr auto; gap: 4px 16px; padding: 16px 18px; background: var(--paper); border: 1px solid var(--line);
  border-radius: 12px; text-decoration: none; color: inherit; }
.card:hover { border-color: var(--muted); }
.card .title { font-family: var(--jp); font-size: 1.35rem; font-weight: 600; }
.card .meta { grid-column: 1; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.card .status { grid-row: 1 / span 2; grid-column: 2; align-self: center; text-align: right; }
.empty, .howto { padding: 20px; border: 1px dashed var(--line); border-radius: 12px; }
.howto code, code { background: var(--line); padding: 1px 5px; border-radius: 4px; font-size: .9em; }

/* Reader */
.story-head h2 { font-family: var(--jp); font-size: 2rem; margin: 8px 0 4px; font-weight: 600; }
.reader { background: var(--paper); border: 1px solid var(--line); border-radius: 14px; padding: 28px clamp(18px, 5vw, 48px); margin: 16px 0; }
.reader p { font-family: var(--jp); font-size: 1.35rem; line-height: 2.3; margin: 0 0 .9em; text-align: justify; }
ruby { ruby-position: over; }
rt { font-size: .5em; color: var(--accent); visibility: hidden; user-select: none; }
.w.show rt { visibility: visible; }
.w { cursor: pointer; border-bottom: 2px dotted color-mix(in srgb, var(--accent) 55%, transparent); border-radius: 2px; }
.w:hover { background: var(--accent-soft); }
.w.m-know { border-bottom-style: solid; border-bottom-color: color-mix(in srgb, var(--ok) 60%, transparent); }
.w.m-guess { border-bottom-style: solid; border-bottom-color: #c9a227; }
.w.m-none { border-bottom-style: solid; border-bottom-color: var(--bad); }
.g { border-bottom: 2px solid color-mix(in srgb, var(--grammar) 45%, transparent); }
.phase-done .g { cursor: pointer; }
.phase-done .g:hover { background: var(--grammar-soft); }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: .85rem; color: var(--muted); }
.legend span::before { content: ""; display: inline-block; width: 16px; height: 0; border-bottom: 2px dotted var(--accent); margin-right: 6px; vertical-align: middle; }
.legend .lk::before { border-bottom: 2px solid var(--ok); } .legend .lg::before { border-bottom: 2px solid #c9a227; } .legend .ln::before { border-bottom: 2px solid var(--bad); }
.legend .lgr::before { border-bottom: 2px solid var(--grammar); }

.pop { position: absolute; z-index: 10; background: var(--paper); border: 1px solid var(--muted); border-radius: 10px; padding: 12px; width: min(320px, 90vw);
  box-shadow: 0 8px 30px rgba(0,0,0,.18); font-size: .95rem; }
.pop .q { margin-bottom: 8px; }
.pop .row { display: flex; gap: 6px; flex-wrap: wrap; }
.pop input { width: 100%; font: inherit; padding: 6px 8px; margin: 8px 0 0; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--ink); }
.pop .big { font-family: var(--jp); font-size: 1.4rem; }

.bar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.bar .spacer { flex: 1; }

/* Quiz */
.quiz { display: grid; gap: 14px; margin: 16px 0; }
.qcard { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px; }
.qcard .qt { font-family: var(--jp); font-size: 1.15rem; margin-bottom: 10px; }
.qcard .ctx { font-family: var(--jp); font-size: 1.1rem; background: var(--bg); padding: 8px 12px; border-radius: 8px; margin-bottom: 10px; line-height: 1.9; }
.qcard .ctx b { color: var(--accent); }
.choices { display: grid; gap: 6px; }
.choices label { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.choices label:hover { border-color: var(--muted); }
.choices input { margin-top: 4px; }
.choices .jp { font-family: var(--jp); }
.choices label.right { background: var(--ok-soft); border-color: var(--ok); }
.choices label.wrong { background: var(--bad-soft); border-color: var(--bad); }
.qcard .expl { margin-top: 8px; font-size: .9rem; color: var(--muted); }
h3.section { margin: 28px 0 8px; font-size: 1.05rem; }

/* Results */
.score { font-size: 1.6rem; font-weight: 700; }
.score.pass { color: var(--ok); } .score.fail { color: var(--bad); }
table.words { width: 100%; border-collapse: collapse; background: var(--paper); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
table.words th, table.words td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; font-size: .92rem; }
table.words td.jp { font-family: var(--jp); font-size: 1.15rem; white-space: nowrap; }
.locked { color: var(--muted); font-style: italic; }
.gram { background: var(--paper); border: 1px solid var(--line); border-left: 4px solid var(--grammar); border-radius: 10px; padding: 14px 16px; margin: 10px 0; }
.gram .gt { font-family: var(--jp); font-size: 1.25rem; font-weight: 600; }
.gram .ex { font-family: var(--jp); }
details.translation { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 12px 16px; margin: 10px 0; }
details.translation p { margin: .5em 0; }

/* Upload */
form.upload { display: grid; gap: 12px; background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 20px; }
form.upload label { display: grid; gap: 4px; font-weight: 600; font-size: .9rem; }
form.upload input[type=text], form.upload textarea { font: inherit; font-weight: 400; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
form.upload textarea { font-family: var(--jp); font-size: 1.1rem; min-height: 280px; line-height: 1.8; }
.msg { padding: 10px 14px; border-radius: 8px; }
.msg.err { background: var(--bad-soft); } .msg.ok { background: var(--ok-soft); }
.choices label.idk { border-style: dashed; color: var(--muted); }
.choices label.idk.wrong { color: var(--bad); }
.qcard .deduce { display: flex; gap: 8px; align-items: center; margin-top: 8px; font-size: .85rem; color: var(--muted); cursor: pointer; }
.qcard .deduce input { accent-color: var(--accent); }

/* New story panel + job banners */
.newstory { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px; margin: 12px 0; display: grid; gap: 12px; }
.newstory .types { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 8px; }
.newstory .type { text-align: left; display: grid; gap: 4px; padding: 12px 14px; border-radius: 10px; }
.newstory .type[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.newstory .type:disabled { opacity: .5; }
.newstory .type b { font-size: 1rem; }
.newstory .type span { font-size: .85rem; color: var(--muted); }
.newstory .opts { display: grid; gap: 10px; }
.newstory .opts label { display: grid; gap: 4px; font-weight: 600; font-size: .9rem; }
.newstory .opts input { font: inherit; font-weight: 400; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
.job { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 10px 14px; border-radius: 10px; margin: 8px 0; background: var(--paper); border: 1px solid var(--line); }
.job.running { border-color: var(--accent); }
.job.done { background: var(--ok-soft); border-color: transparent; }
.job.failed, .job.interrupted { background: var(--bad-soft); border-color: transparent; }
.job .spin { width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .job .spin { animation: none; } }
.job .x { margin-left: auto; padding: 2px 8px; }

/* Keep the hidden attribute authoritative over display rules above */
[hidden] { display: none !important; }

/* Series */
.chip.series { background: var(--ink); color: var(--bg); font-family: var(--jp); }
.series-card .premise { grid-column: 1 / -1; }
.series-card .status .btn, .series-card .status button { white-space: nowrap; }
.series-line { font-family: var(--jp); font-size: .95rem; margin-bottom: 6px; color: var(--muted); }
.series-line a { text-decoration: none; }
details.previously { background: var(--paper); border: 1px solid var(--line); border-left: 4px solid var(--ink); border-radius: 10px; padding: 8px 14px; margin: 8px 0 12px; font-size: .92rem; }
details.previously summary { cursor: pointer; color: var(--muted); }
details.previously p { margin: 6px 0 2px; }
.nextch { margin-top: 20px; border-color: var(--accent); }
button.linkish { background: none; border: none; padding: 0; color: var(--muted); text-decoration: underline; cursor: pointer; }
.newstory .opts label.check { display: flex; gap: 8px; align-items: center; font-weight: 400; }
.newstory .opts select { font: inherit; font-weight: 400; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
.scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* Phones */
@media (max-width: 640px) {
  header.top { padding: 12px 14px 6px; gap: 8px; padding-top: max(12px, env(safe-area-inset-top)); }
  header.top h1 { font-size: 1.25rem; width: 100%; }
  header.top h1 .muted, header.top #status { display: none; }
  header.top .spacer { display: none; }
  header.top .btn, header.top button { padding: 6px 10px; font-size: .85rem; }
  main { padding: 6px 12px 60px; padding-bottom: max(60px, env(safe-area-inset-bottom)); }
  .story-head h2 { font-size: 1.6rem; }
  .reader { padding: 18px 16px; border-radius: 10px; margin: 12px -4px; }
  .reader p { font-size: 1.25rem; line-height: 2.2; text-align: left; }
  .card { grid-template-columns: 1fr; padding: 14px; }
  .card .status { grid-row: auto; grid-column: 1; text-align: left; }
  .card .title { font-size: 1.2rem; }
  .pop { width: calc(100vw - 16px); }
  .choices label { padding: 10px; }
  table.words th, table.words td { padding: 6px 8px; }
  .howto { display: none; }
}

/* Read-only devices (not trusted): hide actions, show notes */
.only-readonly { display: none; }
.readonly .only-readonly { display: block; }
.readonly span.only-readonly { display: inline; }
.readonly .needs-trust { display: none !important; }
