:root {
  --bg: #f6f3ee;
  --fg: #1d1b18;
  --muted: #7a746b;
  --line: #ddd6cb;
  --accent: #b5452f;
  --accent-soft: #f3e1dc;
  --btn: #ebe6de;
  --serif: "Noto Serif JP", "Hiragino Mincho ProN", "Yu Mincho", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16161a;
    --fg: #ece8e1;
    --muted: #8d887f;
    --line: #2e2d33;
    --accent: #e0765f;
    --accent-soft: #3a2420;
    --btn: #25242a;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
  -webkit-tap-highlight-color: transparent;
  max-width: 560px;
  margin-inline: auto;
}
button {
  font: inherit;
  color: inherit;
  background: var(--btn);
  border: 0;
  border-radius: 12px;
  min-height: 48px;
  padding: 0 16px;
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { opacity: .35; }

header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 12px 0;
}
header .side { display: flex; gap: 8px; }
header .side.end { justify-content: flex-end; }
header button { min-width: 48px; font-size: 1.2rem; padding: 0 12px; white-space: nowrap; }
header #missed { font-size: 1rem; color: var(--accent); }
#score {
  grid-column: 2;
  background: none;
  min-height: 44px;
  padding: 0 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.25;
  font-size: .9rem;
  white-space: nowrap;
}
#score[hidden] { display: none; }
#streak { font-weight: 600; }
#pts { color: var(--muted); font-size: .8rem; }

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 16px 0;
}
#via { color: var(--muted); font-size: .9rem; min-height: 1.4em; margin: 0 0 12px; }
#via b { font-family: var(--serif); font-weight: 500; color: var(--fg); }

#word {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-end;
  font-family: var(--serif);
  font-weight: 700;
  line-height: 1;
}
.seg { display: flex; flex-direction: column; align-items: center; }
.note {
  font-family: var(--sans);
  font-weight: 500;
  font-size: 1rem;
  color: var(--accent);
  min-height: 2.6em;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  visibility: hidden;
}
.note small { color: var(--muted); font-size: .75rem; font-weight: 400; }
.show-readings .note, .revealed .note { visibility: visible; }
/* Kanji and kana share one size, like printed Japanese; it shrinks with
   the word length (--n characters) so long words stay on one line. */
#word { --size: max(28px, min(22vw, 112px, calc((min(100vw, 560px) - 56px) / var(--n, 2)))); }
.k {
  font: inherit;
  font-size: var(--size);
  background: none;
  border-radius: 16px;
  padding: 4px 2px;
  min-height: 0;
  line-height: 1.1;
}
.k:active { background: var(--btn); }
.k { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
.kana { font-size: var(--size); line-height: 1.1; padding: 4px 1px; color: var(--muted); font-weight: 500; }
.count { font-family: var(--sans); font-weight: 400; font-size: .75rem; color: var(--muted); height: 1.2em; visibility: hidden; }
.revealed .count { visibility: visible; }
.count.dead { color: var(--accent); }

#answer { margin-top: 20px; }
#reading { font-size: 1.5rem; margin: 0 0 8px; }
#meanings { list-style: none; padding: 0; margin: 0; font-size: 1.1rem; line-height: 1.5; }
#meanings li + li { color: var(--muted); font-size: 1rem; }
.tag { display: inline-block; font-size: .75rem; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; margin-top: 10px; }

footer { padding: 12px 0 20px; min-height: 100px; }
footer button { width: 100%; min-height: 56px; font-size: 1.1rem; }
.primary { background: var(--fg); color: var(--bg); }
#mark[aria-pressed="true"] { background: var(--accent); color: #fff; }
.hint { text-align: center; color: var(--muted); font-size: .9rem; margin: 10px 0 0; }

dialog {
  border: 0;
  border-radius: 16px;
  background: var(--bg);
  color: var(--fg);
  width: min(92vw, 420px);
  max-height: 90dvh;
  overflow-y: auto;
  padding: 20px;
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog h2 { margin: 0 0 16px; font-size: 1.2rem; }
dialog label { display: flex; gap: 10px; align-items: center; min-height: 48px; }
dialog input { width: 22px; height: 22px; accent-color: var(--accent); }
dialog button { width: 100%; margin-top: 12px; }
.danger { color: var(--accent); }
#stats, .credits { color: var(--muted); font-size: .85rem; line-height: 1.4; }
.credits a { color: inherit; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(130px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: .9rem;
  opacity: 0;
  transition: opacity .2s;
  pointer-events: none;
  max-width: calc(100vw - 32px);
}
#toast.on { opacity: .92; }

.row { display: flex; gap: 8px; }
.row button { flex: 1; }
#add[aria-pressed="true"] { background: var(--fg); color: var(--bg); }
.note.hard { visibility: visible; }
.note.hard::after { content: ""; display: block; height: 2px; margin: 2px auto 0; width: 60%; background: var(--accent); border-radius: 1px; opacity: .5; }
.field { justify-content: space-between; }
dialog select {
  font: inherit;
  color: inherit;
  background: var(--btn);
  border: 0;
  border-radius: 10px;
  min-height: 44px;
  padding: 0 10px;
  max-width: 60%;
}
dialog select:disabled { opacity: .4; }
dialog h2 + .row, dialog .row + .credits { margin-top: 0; }
dialog .row button { margin-top: 0; }
dialog h2:not(:first-child) { margin: 20px 0 10px; font-size: 1rem; }

.lvl { font-size: .55rem; letter-spacing: 1px; color: var(--accent); height: 1.2em; visibility: hidden; opacity: .8; }
.revealed .lvl { visibility: visible; }

/* details button under each kanji (after Show) */
.det {
  min-height: 28px;
  padding: 0 6px;
  margin-top: 4px;
  font: 400 .75rem var(--sans);
  border-radius: 8px;
  color: var(--muted);
  visibility: hidden;
}
.revealed .det { visibility: visible; }
.det.ph { visibility: hidden !important; pointer-events: none; width: 0; padding: 0; overflow: hidden; }
#via button { min-height: 28px; padding: 0 10px; font-size: .8rem; margin-left: 4px; }

/* bottom sheets: details & collection */
dialog.sheet {
  width: min(100vw, 560px);
  max-width: 100vw;
  max-height: 88dvh;
  margin: auto auto 0;
  border-radius: 18px 18px 0 0;
  padding: 8px 16px calc(20px + env(safe-area-inset-bottom));
}
.sheet button { width: auto; margin-top: 0; }
.sheet .row { margin: 0; }
.sheet-head { display: flex; justify-content: flex-end; align-items: center; position: sticky; top: 0; background: var(--bg); z-index: 1; }
.sheet-head h2 { margin: 0 auto 0 0 !important; font-size: 1.1rem; }
.sheet-head button { width: auto; min-width: 44px; margin: 0; background: none; font-size: 1.1rem; }
.sheet h3 { font-size: 1rem; margin: 18px 0 8px; }
.sheet h3 small, .sum small { color: var(--muted); font-weight: 400; }
.loading { color: var(--muted); }

.det-top { display: flex; gap: 16px; align-items: center; }
.det-k { font-family: var(--serif); font-weight: 700; font-size: 84px; line-height: 1; }
.det-meta { margin: 0; color: var(--muted); font-size: .85rem; }
.det-m { margin: 4px 0 0; font-size: 1.05rem; line-height: 1.35; }
.readings p { margin: 10px 0 0; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.readings .lab { color: var(--muted); font-size: .75rem; width: 30px; }
.chip { border: 1px solid var(--line); border-radius: 8px; padding: 3px 8px; font-size: .95rem; color: var(--muted); }
.chip small { opacity: .6; }
.chip.met { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

.strokes { margin-top: 14px; }
.pad { position: relative; width: min(60vw, 220px); aspect-ratio: 1; margin: 0 auto 10px; border: 1px solid var(--line); border-radius: 12px; background:
  linear-gradient(var(--line), var(--line)) center / 1px 100% no-repeat,
  linear-gradient(var(--line), var(--line)) center / 100% 1px no-repeat; }
#stroke-svg { width: 100%; height: 100%; display: block; }
#stroke-svg path { fill: none; stroke-linecap: round; stroke-linejoin: round; stroke-width: 3.5; }
#stroke-svg .ghost path { stroke: var(--line); }
#stroke-svg .ink path { stroke: var(--fg); }
#stroke-svg.tracing .ghost path { stroke: var(--muted); opacity: .35; }
#trace { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.strokes .row button, .sheet > div > .row button { min-height: 44px; font-size: .9rem; }
#trace-btn[aria-pressed="true"] { background: var(--fg); color: var(--bg); }

.record { color: var(--muted); font-size: .9rem; margin: 14px 0 10px; }
.badge { border-radius: 6px; padding: 1px 7px; font-size: .8rem; background: var(--btn); color: var(--fg); }

.words { list-style: none; padding: 0; margin: 0; }
.words button {
  width: 100%;
  text-align: left;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px;
  background: none;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  padding: 10px 4px;
  min-height: 48px;
}
.words b { font-family: var(--serif); font-size: 1.2rem; }
.words .r { color: var(--muted); font-size: .9rem; }
.words .mn { flex-basis: 100%; color: var(--muted); font-size: .85rem; padding-left: 22px; }
.st { width: 16px; text-align: center; color: var(--muted); }
.st.right { color: #3f8f4f; }
.st.missed { color: var(--accent); }

/* mastery colours */
.m1 { --m: #b0804a; }
.m2 { --m: #8e99a6; }
.m3 { --m: #c9a227; }
.badge.m1, .badge.m2, .badge.m3 { background: var(--m); color: #fff; }
.sum span { color: var(--m); font-weight: 600; }
.grid { display: flex; flex-wrap: wrap; gap: 4px; }
.cell {
  width: 38px;
  height: 38px;
  min-height: 0;
  padding: 0;
  font-family: var(--serif);
  font-size: 1.2rem;
  border-radius: 8px;
  color: var(--muted);
  opacity: .55;
}
.cell.m1, .cell.m2, .cell.m3 { background: var(--m); color: #fff; opacity: 1; }
.badges { list-style: none; padding: 0; margin: 0; }
.badges li { padding: 6px 0; border-bottom: 1px solid var(--line); }
.badges li small { color: var(--muted); margin-left: 6px; }
.badges .locked { color: var(--muted); opacity: .6; }
.badges .locked::before { content: "🔒 "; }

/* word category chips (after Show) */
#pos { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; margin: 0 0 12px; }
#pos:empty { display: none; }
.pos { border: 1px solid var(--line); border-radius: 8px; padding: 2px 8px; font-size: .8rem; color: var(--muted); }
.pos b { color: var(--fg); font-weight: 500; font-size: .9rem; }

/* search */
#search-input {
  width: 100%;
  font: inherit;
  font-size: 1.1rem;
  color: var(--fg);
  background: var(--btn);
  border: 0;
  border-radius: 12px;
  padding: 12px 14px;
  margin: 4px 0 10px;
}
#random-in-search { width: 100%; min-height: 44px; font-size: .95rem; margin-bottom: 6px; }
.kanji-hits { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.kanji-hits button { display: flex; flex-direction: column; align-items: center; min-height: 0; padding: 8px 12px; max-width: 46%; }
.kanji-hits b { font-family: var(--serif); font-size: 2rem; line-height: 1.1; }
.kanji-hits span { font-size: .75rem; color: var(--muted); text-align: center; }
#search-results .none { color: var(--muted); font-size: .9rem; }
