/* Kana Drop */

*, *::before, *::after { box-sizing: border-box; }

:root {
  color-scheme: light;
  --bg-a: #fde2ee;
  --bg-b: #f4eefb;
  --bg-c: #d9f2f7;
  --bokeh: rgba(255, 255, 255, .6);
  --ink: #3a2840;
  --muted: #9a859e;
  --line: rgba(110, 70, 110, .15);
  --panel: rgba(255, 255, 255, .5);
  --panel-edge: rgba(255, 255, 255, .9);
  --panel-alt: rgba(255, 249, 214, .62);
  --cell: rgba(255, 255, 255, .88);
  --cell-edge: rgba(120, 80, 120, .14);
  --blank: rgba(255, 255, 255, .28);
  --pill: rgba(255, 255, 255, .72);
  --dock: rgba(255, 255, 255, .62);
  --sheet: #fffafd;
  --sheet-alt: #f5edf5;
  --hira: #d94f87;
  --hira-fill: #f9cadd;
  --hira-fill-2: #f2a2c3;
  --kata: #1d93a8;
  --kata-fill: #bce8f0;
  --kata-fill-2: #86d2e0;
  --violet: #8a5cd0;
  --good: #1f9d63;
  --bad: #e0434d;
  --bad-wash: rgba(224, 67, 77, .16);
  --tile-bg: #fff;
  --tile-edge: rgba(110, 70, 110, .17);
  --shadow: 0 8px 24px rgba(90, 40, 90, .1);
  --shadow-lg: 0 30px 80px rgba(60, 20, 60, .3);

  --ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Sans", "Noto Sans JP", sans-serif;
  --f-gothic: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Noto Sans CJK JP", "Yu Gothic UI", "Yu Gothic", YuGothic, Meiryo, sans-serif;
  --f-mincho: "Hiragino Mincho ProN", "Yu Mincho", YuMincho, "Noto Serif JP", "Noto Serif CJK JP", "MS Mincho", serif;
  --f-textbook: "Klee One", Klee, "UD Digi Kyokasho N-R", "UD デジタル 教科書体 N-R", var(--f-gothic);
  --kana: var(--f-gothic);

  --accent: var(--hira);
  --cell-h: 44px;
  --lw: 30px;
  --gap: 4px;
  --tile: clamp(56px, 17vw, 72px);
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg-a: #2b1a2a;
  --bg-b: #1b1925;
  --bg-c: #122329;
  --bokeh: rgba(255, 255, 255, .035);
  --ink: #f4e9f3;
  --muted: #a092a8;
  --line: rgba(255, 255, 255, .1);
  --panel: rgba(255, 255, 255, .045);
  --panel-edge: rgba(255, 255, 255, .08);
  --panel-alt: rgba(255, 226, 130, .055);
  --cell: rgba(255, 255, 255, .07);
  --cell-edge: rgba(255, 255, 255, .08);
  --blank: rgba(255, 255, 255, .02);
  --pill: rgba(255, 255, 255, .08);
  --dock: rgba(26, 23, 34, .72);
  --sheet: #221f2b;
  --sheet-alt: #2d2937;
  --hira: #f27aa9;
  --hira-fill: rgba(242, 122, 169, .26);
  --hira-fill-2: rgba(242, 122, 169, .5);
  --kata: #4fc6d9;
  --kata-fill: rgba(79, 198, 217, .22);
  --kata-fill-2: rgba(79, 198, 217, .45);
  --violet: #b08cf0;
  --good: #3fcf8e;
  --bad: #ff6b72;
  --bad-wash: rgba(255, 107, 114, .18);
  --tile-bg: #2d2938;
  --tile-edge: rgba(255, 255, 255, .13);
  --shadow: 0 8px 24px rgba(0, 0, 0, .3);
  --shadow-lg: 0 30px 80px rgba(0, 0, 0, .6);
}

[data-font="mincho"] { --kana: var(--f-mincho); }
[data-font="textbook"] { --kana: var(--f-textbook); }

body[data-script="k"] { --accent: var(--kata); }
body[data-script="both"] { --accent: var(--violet); }

/* ───────────────────────── Layout ───────────────────────── */

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation; /* no double-tap zoom, pinch still works */
}

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: var(--ui);
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 14%, var(--bokeh) 0, transparent 140px),
    radial-gradient(circle at 92% 6%, var(--bokeh) 0, transparent 170px),
    radial-gradient(circle at 80% 88%, var(--bokeh) 0, transparent 200px),
    radial-gradient(circle at 16% 94%, var(--bokeh) 0, transparent 160px),
    radial-gradient(circle at 50% 50%, var(--bokeh) 0, transparent 240px),
    linear-gradient(115deg, var(--bg-a), var(--bg-b) 50%, var(--bg-c));
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas: "top" "bar" "board" "dock";
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

body.dragging, body.dragging * { cursor: grabbing !important; }

.sr {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}
.jp { font-family: var(--f-gothic); }

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ───────────────────────── Header ───────────────────────── */

#top {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
}

.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.logo {
  display: grid; place-items: center;
  width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, #f38db6, #5cc6d9);
  color: #fff; font: 700 20px/1 var(--f-gothic);
  box-shadow: 0 4px 12px rgba(200, 80, 140, .25);
}
.brand h1 {
  margin: 0; font-size: 18px; font-weight: 800; letter-spacing: -.01em; white-space: nowrap;
  background: linear-gradient(90deg, var(--hira), var(--kata));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}

.hud { display: flex; gap: 6px; margin-inline: auto; }
.pill {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 6px 10px; border-radius: 999px;
  background: var(--pill); border: 1px solid var(--line);
  font: 600 14px/1 var(--ui); font-variant-numeric: tabular-nums;
}
.pill svg { width: 14px; height: 14px; opacity: .55; }
.pill.miss.bad { color: var(--bad); }
.pill.miss.bad svg { opacity: 1; }

.actions { display: flex; gap: 6px; }
.icon-btn {
  appearance: none; -webkit-appearance: none;
  display: grid; place-items: center;
  width: 40px; height: 40px; padding: 0;
  border-radius: 12px; border: 1px solid var(--line);
  background: var(--pill); color: var(--ink);
  cursor: pointer;
  transition: transform .12s ease, background .12s ease;
}
.icon-btn:active { transform: scale(.94); }
.icon-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
@media (hover: hover) { .icon-btn:hover { background: var(--sheet); } }

#progress {
  grid-area: bar;
  height: 4px;
  margin: 0 max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
  border-radius: 4px;
  background: var(--line);
  overflow: hidden;
}
#progress i {
  display: block; height: 100%;
  background: linear-gradient(90deg, var(--hira), var(--kata));
  transform-origin: left center;
  transform: scaleX(0);
  transition: transform .5s cubic-bezier(.3, .7, .3, 1);
}

/* Review button: icon + count badge; gets a text label when there's room */
.review-btn { position: relative; }
.review-btn .lbl { display: none; font: 600 14px/1 var(--ui); white-space: nowrap; }
.review-btn[aria-pressed="true"] { background: var(--accent); border-color: transparent; color: #fff; }
.badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 19px;
  height: 19px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--hira);
  color: #fff;
  font: 700 11px/19px var(--ui);
  text-align: center;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 0 0 2px var(--bg-b);
  pointer-events: none;
}
.badge[hidden] { display: none; }
@media (min-width: 720px) {
  .review-btn { display: inline-flex; align-items: center; gap: 6px; width: auto; padding: 0 12px; }
  .review-btn .lbl { display: inline; }
}

@media (max-width: 760px) {
  .brand h1 { display: none; }
}
@media (max-width: 440px) {
  .brand { display: none; }
  .pill { padding: 6px 8px; font-size: 13px; }
  .icon-btn { width: 36px; height: 36px; border-radius: 11px; }
  .hud { gap: 4px; margin-inline: 0 auto; }
  .actions { gap: 5px; }
}
@media (max-width: 370px) {
  .pill { padding: 6px 6px; font-size: 12.5px; gap: 2px; }
  .pill:not(.miss) svg { display: none; }
  .icon-btn { width: 34px; height: 34px; }
}

/* ───────────────────────── Board ───────────────────────── */

#board {
  grid-area: board;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 12px max(10px, env(safe-area-inset-right)) 18px max(10px, env(safe-area-inset-left));
}

.board-inner {
  position: relative;
  min-height: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
  align-content: center;
  gap: 14px;
}

.board-col { display: flex; flex-direction: column; gap: 10px; }
.board-col.main { width: min(100%, 560px); }
.board-col.side { width: min(100%, 400px); }
.two .board-col.main { width: min(100%, 640px); }
.two .board-col.side { width: min(100%, 460px); }

.kt {
  display: grid;
  grid-template-columns: var(--lw) repeat(var(--cols), minmax(0, 1fr));
  grid-auto-rows: var(--cell-h);
  gap: var(--gap);
  padding: 8px;
  border-radius: 18px;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  box-shadow: var(--shadow);
}
.kt.has-head { grid-template-rows: 22px; }
.nolabels .kt { grid-template-columns: repeat(var(--cols), minmax(0, 1fr)); }
.kt[data-section="dakuten"] { background: var(--panel-alt); }

.kt-colhead, .kt-rowhead {
  display: grid; place-items: center;
  font: 500 13px/1 var(--ui);
  letter-spacing: .06em;
  color: var(--muted);
}
.kt-rowhead { font-size: 12px; letter-spacing: 0; }

.cell {
  appearance: none; -webkit-appearance: none;
  margin: 0; padding: 0;
  font: inherit; color: inherit;
  position: relative;
  display: flex;
  min-width: 0;
  border-radius: 10px;
  border: 1px solid var(--cell-edge);
  background: var(--cell);
  overflow: hidden;
  touch-action: manipulation;
  transition: transform .12s ease, box-shadow .12s ease;
}
.cell.blank { background: var(--blank); border-color: transparent; }
.cell.inactive { background: var(--blank); border-style: dashed; opacity: .6; }
.has-selection[data-mode="place"] .cell.target:not(.full),
.has-selection[data-mode="place"] .hide-placed .cell.target { cursor: pointer; }
@media (hover: hover) {
  .has-selection[data-mode="place"] .cell.target:not(.full):hover,
  .has-selection[data-mode="place"] .hide-placed .cell.target:hover { box-shadow: inset 0 0 0 2px var(--accent); }
}
.cell:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; z-index: 3; }

.cell.over {
  transform: scale(1.06);
  box-shadow: 0 0 0 3px var(--accent), 0 8px 18px rgba(60, 20, 60, .22);
  z-index: 2;
}
.cell.hint { animation: hintPulse 1.1s ease-in-out infinite; z-index: 1; }

.slot {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  display: grid;
  place-items: center;
  font-family: var(--kana);
  font-size: calc(var(--cell-h) * .56);
  line-height: 1;
}
.two .slot { font-size: calc(var(--cell-h) * .46); }
.cell.combo .slot { font-size: calc(var(--cell-h) * .52); letter-spacing: -.04em; }
.two .cell.combo .slot { font-size: calc(var(--cell-h) * .48); }
.slot + .slot { border-left: 1px dashed var(--cell-edge); }

/* the "liquid" that fills a cell when a kana lands */
.slot::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(to top, var(--fill-2), var(--fill) 75%);
  transform: scaleY(0);
  transform-origin: 50% 100%;
}
.slot.h { --fill: var(--hira-fill); --fill-2: var(--hira-fill-2); }
.slot.k { --fill: var(--kata-fill); --fill-2: var(--kata-fill-2); }
.slot.filled::before { transform: scaleY(1); }
.slot.fresh::before { animation: fillUp 1s cubic-bezier(.35, .6, .2, 1) both; }
.glyph { position: relative; }
.slot.fresh .glyph { animation: glyphIn .5s .38s cubic-bezier(.2, .9, .3, 1.3) both; }
.slot.off {
  background: repeating-linear-gradient(135deg, transparent 0 5px, var(--line) 5px 6px);
  opacity: .7;
}

.cell.wrong, .cell.wrong-plain { animation: shake .4s ease; }
.cell.wrong::after, .cell.wrong-plain::after {
  content: attr(data-romaji);
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: var(--bad-wash);
  color: var(--bad);
  font: 700 calc(var(--cell-h) * .36)/1 var(--ui);
  animation: fadeOut 1s ease forwards;
}
.cell.wrong-plain::after { content: ""; }

/* Pick mode: the spot waiting to be filled */
.cell.goal-cell { z-index: 1; box-shadow: 0 0 0 2px var(--accent), 0 0 20px -2px var(--accent); }
.slot.goal { box-shadow: inset 0 0 0 2px var(--accent); }
.slot.goal::after {
  content: "?";
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: var(--accent);
  font: 700 calc(var(--cell-h) * .46)/1 var(--ui);
  animation: goalPulse 1.3s ease-in-out infinite;
}

/* "Keep placed kana visible" off: a placed kana fills in, lingers, then drains away */
.hide-placed .slot.filled:not(.fresh)::before { transform: scaleY(0); }
.hide-placed .slot.filled:not(.fresh) .glyph { opacity: 0; }
.hide-placed .slot.drain::before { animation: drain .7s ease-in both; }
.hide-placed .slot.drain .glyph { animation: glyphOut .5s ease-in both; }

/* end of round: the whole table flows back in as a wave */
.slot.reveal::before { animation: fillUp .6s var(--d, 0ms) cubic-bezier(.35, .6, .2, 1) both; }
.slot.reveal .glyph { animation: glyphIn .45s calc(var(--d, 0ms) + 150ms) cubic-bezier(.2, .9, .3, 1.3) both; }

.slot.party .glyph { animation: party .7s var(--d, 0ms) ease both; }
.slot.party::before { animation: partyFill .7s var(--d, 0ms) ease both; }

/* ───────────────────────── Dock / tray ───────────────────────── */

#dock {
  grid-area: dock;
  position: relative;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px max(12px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  background: var(--dock);
  border-top: 1px solid var(--line);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}

/* one fixed-height line, so showing/hiding it never shifts the table */
.dock-hint {
  margin: 0;
  max-width: 100%;
  min-height: 1.3em;
  font: 500 12.5px/1.3 var(--ui);
  color: var(--muted);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  transition: opacity .2s ease;
}
.dock-hint.show { opacity: 1; }

.dock-meta {
  position: absolute;
  top: -12px;
  right: max(12px, env(safe-area-inset-right));
  margin: 0;
  padding: 4px 9px;
  border-radius: 999px;
  background: var(--sheet);
  border: 1px solid var(--line);
  font: 600 11px/1 var(--ui);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.dock-meta:empty { display: none; }

.tray {
  display: grid;
  grid-template-columns: repeat(var(--tc, 3), var(--tile));
  gap: 10px;
  justify-content: center;
}
.tray[data-n="1"] { --tc: 1; --tile: clamp(64px, 20vw, 82px); }
.tray[data-n="3"] { --tc: 3; --tile: clamp(60px, 19vw, 76px); }
.tray[data-n="5"] { --tc: 5; --tile: clamp(52px, 15.5vw, 72px); }
.tray[data-n="8"] { --tc: 4; --tile: clamp(52px, 17vw, 68px); }
.tray.nudge { animation: nudge .45s ease; }

.tile-slot {
  width: var(--tile); height: var(--tile);
  border-radius: 16px;
  border: 2px dashed var(--line);
}

.tile {
  appearance: none; -webkit-appearance: none;
  margin: 0; padding: 0;
  font: inherit; color: var(--ink);
  position: relative;
  width: var(--tile); height: var(--tile);
  display: grid; place-items: center;
  border: 0;
  border-radius: 16px;
  background: var(--tile-bg);
  box-shadow: inset 0 0 0 2px var(--tile-edge), 0 3px 0 var(--tile-edge), 0 8px 18px rgba(80, 30, 80, .13);
  cursor: grab;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  transition: transform .15s ease, box-shadow .15s ease, opacity .15s ease;
}
.tile-kana, .ghost .tile-kana {
  font-family: var(--kana);
  font-size: calc(var(--tile) * .54);
  font-weight: 500;
  line-height: 1;
  pointer-events: none;
  transition: transform .15s ease;
}
.tile-kana.two { font-size: calc(var(--tile) * .38); letter-spacing: -.04em; }
.tile-romaji {
  position: absolute; left: 0; right: 0; bottom: 5px;
  text-align: center;
  font: 600 11px/1 var(--ui);
  color: var(--muted);
  opacity: 0;
  pointer-events: none;
}
.tile.show-romaji .tile-romaji { opacity: 1; }
.tile.show-romaji .tile-kana { transform: translateY(-5px); }
.tile-num {
  display: none;
  position: absolute; top: 5px; left: 7px;
  font: 600 10px/1 var(--ui);
  color: var(--muted);
  pointer-events: none;
}
@media (hover: hover) and (pointer: fine) {
  .tile-num { display: block; }
  .tile:hover { transform: translateY(-2px); }
}
.tile.pressed { transform: scale(.95); }
.tile.selected {
  transform: translateY(-5px);
  box-shadow: inset 0 0 0 3px var(--accent), 0 3px 0 var(--accent), 0 12px 24px rgba(80, 30, 80, .2);
}
.tile.lifted { opacity: .22; transform: scale(.9); box-shadow: inset 0 0 0 2px var(--tile-edge); }
.tile.out { opacity: .35; pointer-events: none; cursor: default; box-shadow: inset 0 0 0 2px var(--tile-edge); }
.tile.out::after {
  content: "";
  position: absolute; inset: 20%;
  background:
    linear-gradient(45deg, transparent 46%, var(--bad) 46% 54%, transparent 54%),
    linear-gradient(-45deg, transparent 46%, var(--bad) 46% 54%, transparent 54%);
}
.tile.hint { animation: tileHint 1.1s ease-in-out infinite; }
.tile.enter { animation: tileIn .38s cubic-bezier(.2, .9, .3, 1.25) both; }
.tile.nope { animation: shake .38s ease; }
.tile:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }

.ghost {
  position: fixed;
  left: 0; top: 0;
  z-index: 100;
  display: grid; place-items: center;
  border-radius: 16px;
  background: var(--tile-bg);
  color: var(--ink);
  box-shadow: inset 0 0 0 3px var(--accent), 0 18px 40px rgba(60, 20, 60, .32);
  pointer-events: none;
  will-change: transform;
}

/* Side dock on wide screens and landscape phones */
@media (min-width: 720px) and (min-aspect-ratio: 1/1), (orientation: landscape) and (max-height: 540px) and (min-width: 560px) {
  body {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas: "top top" "bar bar" "board dock";
  }
  #dock {
    flex-direction: column;
    justify-content: flex-start;
    gap: 14px;
    width: clamp(196px, 21vw, 270px);
    padding: 16px max(16px, env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) 16px;
    border-top: 0;
    border-left: 1px solid var(--line);
    overflow-y: auto;
  }
  .dock-hint {
    order: 2;
    min-height: 0;
    padding: 10px 12px;
    border-radius: 12px;
    background: var(--pill);
    color: var(--ink);
    border: 1px solid var(--line);
    white-space: normal;
  }
  .dock-hint:not(.show) { display: none; }
  .dock-meta { position: static; order: 1; align-self: center; }
  .tray[data-n] { --tc: 2; --tile: clamp(62px, 7.6vw, 86px); }
  .tray[data-n="1"] { --tc: 1; --tile: clamp(86px, 10vw, 116px); }
}

/* ───────────────────────── Dialogs ───────────────────────── */

dialog {
  border: 0;
  padding: 0;
  width: min(560px, calc(100% - 24px));
  max-width: none;
  max-height: min(88vh, 820px);
  max-height: min(88dvh, 820px);
  border-radius: 22px;
  background: var(--sheet);
  color: var(--ink);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
dialog[open] { display: flex; flex-direction: column; animation: sheetIn .22s ease-out; }
dialog::backdrop { background: rgba(30, 10, 30, .38); -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); }

.sheet { display: flex; flex-direction: column; min-height: 0; flex: 1 1 auto; }
.sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 10px 20px;
  border-bottom: 1px solid var(--line);
}
.sheet-head h2 { margin: 0; font-size: 19px; }
.sheet-body { overflow-y: auto; overscroll-behavior: contain; padding: 6px 20px 18px; flex: 1 1 auto; min-height: 0; }
.sheet-foot {
  display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  background: var(--sheet-alt);
}
.sheet-foot .note { margin: 0 auto 0 4px; flex: 1 1 180px; }

@media (max-width: 600px) {
  dialog {
    width: 100%;
    margin: auto 0 0;
    max-height: 92vh;
    max-height: 92dvh;
    border-radius: 22px 22px 0 0;
    animation-name: sheetUp;
  }
  dialog[open] { animation-name: sheetUp; }
  .sheet-foot { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
}

.field { border: 0; margin: 0; padding: 14px 0 4px; min-width: 0; }
.field + .field { border-top: 1px solid var(--line); }
/* floated so the legend sits inside the box instead of cutting through the divider line */
.field legend { float: left; width: 100%; padding: 0; margin-bottom: 8px; font-weight: 700; font-size: 14px; }
.field legend + * { clear: both; }
.sub { margin: 14px 0 8px; font-weight: 600; font-size: 13px; color: var(--muted); }
.note { margin: 8px 2px 4px; font-size: 12.5px; line-height: 1.4; color: var(--muted); }
.note.warn { color: var(--bad); }
h3 { margin: 20px 0 10px; font-size: 14px; }

.seg {
  display: flex;
  gap: 3px;
  padding: 3px;
  border-radius: 12px;
  background: var(--sheet-alt);
  border: 1px solid var(--line);
}
.seg label { flex: 1 1 0; min-width: 0; position: relative; }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; pointer-events: none; }
.seg span {
  display: block;
  padding: 9px 4px;
  border-radius: 9px;
  text-align: center;
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  color: var(--muted);
  transition: background .15s ease, color .15s ease;
}
.seg input:checked + span { background: var(--sheet); color: var(--ink); box-shadow: 0 1px 4px rgba(0, 0, 0, .12), inset 0 0 0 1.5px var(--accent); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }
.seg.small span { padding: 7px 4px; font-size: 13px; }

.switch {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 8px 0;
  cursor: pointer;
}
.switch > span { font-weight: 600; font-size: 14px; }
.switch small { display: block; margin-top: 2px; font-weight: 400; font-size: 12.5px; color: var(--muted); }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch i {
  position: relative; flex: none;
  width: 46px; height: 28px; border-radius: 999px;
  background: var(--line);
  transition: background .18s ease;
}
.switch i::after {
  content: "";
  position: absolute; top: 3px; left: 3px;
  width: 22px; height: 22px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .18s ease;
}
.switch input:checked + i { background: var(--accent); }
.switch input:checked + i::after { transform: translateX(18px); }
.switch input:focus-visible + i { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn {
  appearance: none; -webkit-appearance: none;
  padding: 11px 18px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--sheet);
  color: var(--ink);
  font: 600 15px/1 var(--ui);
  cursor: pointer;
  transition: transform .12s ease, filter .12s ease;
}
.btn:active { transform: scale(.97); }
.btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.danger { color: var(--bad); margin-top: 22px; }
.btn.small { padding: 8px 12px; font-size: 13px; border-radius: 10px; }
.btn[hidden] { display: none; }

.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }

.chip-group { margin-top: 10px; }
.chip-head {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 6px;
  font-size: 12.5px; font-weight: 600; color: var(--muted);
}
.chip-head span { margin-right: auto; }
.link {
  appearance: none; border: 0; background: none; padding: 4px 2px;
  color: var(--accent); font: 600 12.5px/1 var(--ui); cursor: pointer;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  appearance: none; -webkit-appearance: none;
  display: inline-flex; align-items: baseline; gap: 4px;
  min-width: 52px;
  justify-content: center;
  padding: 7px 10px;
  border-radius: 10px;
  border: 1.5px solid var(--line);
  background: transparent;
  color: var(--muted);
  font: 500 13px/1 var(--ui);
  cursor: pointer;
  transition: background .12s ease, color .12s ease, border-color .12s ease;
}
.chip-kana { font-family: var(--f-gothic); font-size: 16px; }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* results + stats */
.results { text-align: center; padding-top: 22px; }
.res-jp {
  font: 700 30px/1.1 var(--f-gothic);
  background: linear-gradient(90deg, var(--hira), var(--kata));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.res-hero h2 { margin: 6px 0 0; font-size: 20px; }
.numbers {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 16px 0 4px;
}
.numbers > div {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 12px 6px;
  border-radius: 14px;
  background: var(--sheet-alt);
}
.numbers b { font-size: 24px; font-variant-numeric: tabular-nums; }
.numbers span { font-size: 12px; color: var(--muted); }
.res-best { margin: 12px 0 0; font-weight: 600; font-size: 14px; color: var(--muted); }
.res-best.new { color: var(--good); }
.results h3 { text-align: left; }
.results .kchips { justify-content: flex-start; }

.kchips { display: flex; flex-wrap: wrap; gap: 6px; }
.kchip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 10px;
  border-radius: 10px;
  background: var(--sheet-alt);
  font-size: 13px;
}
.kchip b { font: 500 20px/1 var(--kana); }
.kchip small { color: var(--bad); font-weight: 700; }
.kchip .dots { font-style: normal; font-size: 10px; letter-spacing: 1px; color: var(--good); }
h3 small { font-weight: 500; color: var(--muted); }
.res-pile { text-align: center; margin-top: 16px; }
#st-weak-practice { margin-top: 10px; }

.heatmap { display: flex; flex-direction: column; gap: 12px; margin-top: 12px; }
.hm-sec h4 { margin: 0 0 6px; font-size: 12px; color: var(--muted); font-weight: 600; }
.hm { display: grid; grid-template-columns: repeat(var(--cols), minmax(0, 1fr)); gap: 3px; }
.hm-cell {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  height: 46px;
  border-radius: 8px;
  background: var(--sheet-alt);
  color: var(--muted);
}
.hm-cell b { font: 500 18px/1 var(--kana); }
.hm-cell small { font-size: 10px; font-weight: 700; }
.hm-cell.blank { background: transparent; }
.hm-cell.seen { background: hsl(calc(var(--acc) * 130) 70% 84%); color: #2c2030; }
[data-theme="dark"] .hm-cell.seen { background: hsl(calc(var(--acc) * 130) 45% 32%); color: #fff; }

.best { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; }
.best li {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--sheet-alt);
  font-size: 13px;
}
.best li b { font-variant-numeric: tabular-nums; font-size: 15px; }
.best li.note { background: none; padding: 0; }

#toast {
  position: fixed;
  left: 50%;
  top: calc(64px + env(safe-area-inset-top));
  z-index: 200;
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--sheet);
  font: 600 13px/1.2 var(--ui);
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translate(-50%, -8px);
  transition: opacity .2s ease, transform .2s ease;
  pointer-events: none;
  max-width: calc(100vw - 32px);
  text-align: center;
}
#toast.show { opacity: .94; transform: translate(-50%, 0); }

/* ───────────────────────── Animations ───────────────────────── */

@keyframes fillUp { from { transform: scaleY(0); } to { transform: scaleY(1); } }
@keyframes drain { from { transform: scaleY(1); } to { transform: scaleY(0); } }
@keyframes glyphOut { from { opacity: 1; } to { opacity: 0; transform: scale(.7); } }
@keyframes glyphIn {
  0% { opacity: 0; transform: scale(.4); }
  70% { opacity: 1; transform: scale(1.12); }
  100% { opacity: 1; transform: scale(1); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}
@keyframes fadeOut { 0%, 55% { opacity: 1; } 100% { opacity: 0; } }
@keyframes hintPulse {
  0%, 100% { box-shadow: 0 0 0 2px var(--good); }
  50% { box-shadow: 0 0 0 4px var(--good), 0 0 18px var(--good); }
}
@keyframes tileHint {
  0%, 100% { box-shadow: inset 0 0 0 2px var(--good), 0 0 0 0 var(--good); }
  50% { box-shadow: inset 0 0 0 3px var(--good), 0 0 16px 2px var(--good); }
}
@keyframes goalPulse { 0%, 100% { opacity: .45; } 50% { opacity: 1; } }
@keyframes tileIn {
  from { opacity: 0; transform: translateY(14px) scale(.8); }
  to { opacity: 1; transform: none; }
}
@keyframes party {
  0%, 100% { transform: none; }
  40% { transform: scale(1.18) rotate(-4deg); }
}
@keyframes partyFill {
  0%, 100% { filter: none; }
  40% { filter: brightness(1.12) saturate(1.4); }
}
@keyframes nudge {
  0%, 100% { transform: none; }
  30% { transform: translateY(-6px); }
  60% { transform: translateY(2px); }
}
@keyframes sheetIn { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes sheetUp { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
