/* ---------- Themes (semantic state colours keep their meaning in every theme) ---------- */
:root, [data-theme="midnight"] {
  --page-bg: #0b1730; --page-bg2: #0a1426;
  --surface: #12223e; --surface2: #17294a;
  --board-bg: #060d1b; --border: #274066;
  --text: #f1f5fb; --text-muted: #9fb0cc;
  --accent: #2f6df6; --accent-text: #fff;
  --selection: #4da3ff; --btn-bg: #17294a;
  --cell-correct: #5fb383; --cell-valid: #d2bf78; --cell-conflict: #cc8d96;
  --cell-text: #101624; --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --radius: 14px; --cell-radius: 5px;
}
[data-theme="classic"] {
  --page-bg: #eef1f6; --page-bg2: #e6eaf2; --surface: #fff; --surface2: #f4f6fa;
  --board-bg: #2b3445; --border: #cfd6e3; --text: #1d2433; --text-muted: #5d6a82;
  --accent: #2f6df6; --selection: #1c5be0; --btn-bg: #fff;
  --cell-correct: #c9e6d1; --cell-valid: #efe2b0; --cell-conflict: #eec3c3; --cell-text: #1d2433;
}
[data-theme="neon"] {
  --page-bg: #05050b; --page-bg2: #05050b; --surface: #0e0e1a; --surface2: #151527;
  --board-bg: #000; --border: #3b2a6e; --text: #f4f0ff; --text-muted: #9a8fc7;
  --accent: #b026ff; --selection: #19e3ff; --btn-bg: #151527;
  --cell-correct: #1fc26a; --cell-valid: #d6c21f; --cell-conflict: #d13b5e; --cell-text: #000;
}
[data-theme="paper"] {
  --page-bg: #f3ead7; --page-bg2: #ece1c9; --surface: #fbf6ea; --surface2: #f5eedc;
  --board-bg: #5a4a36; --border: #d8c9a8; --text: #3a2f22; --text-muted: #7b6a52;
  --accent: #a4562b; --selection: #a4562b; --btn-bg: #fbf6ea;
  --cell-correct: #c9dcbd; --cell-valid: #e5d7a4; --cell-conflict: #dfb2a8; --cell-text: #3a2f22;
  --font: Georgia, "Times New Roman", serif;
}
[data-theme="terminal"] {
  --page-bg: #020a04; --page-bg2: #020a04; --surface: #04140a; --surface2: #07200f;
  --board-bg: #000; --border: #146b32; --text: #7dffa0; --text-muted: #3fae62;
  --accent: #19c752; --accent-text: #000; --selection: #e8ffee; --btn-bg: #07200f;
  --cell-correct: #14522a; --cell-valid: #574c0a; --cell-conflict: #5e1c22; --cell-text: #eaffef;
  --font: "Cascadia Mono", Consolas, "Courier New", monospace; --radius: 4px; --cell-radius: 2px;
}
[data-theme="slate"] {
  --page-bg: #20262e; --page-bg2: #1a1f26; --surface: #2b323c; --surface2: #343c48;
  --board-bg: #14181d; --border: #4a5462; --text: #e8edf3; --text-muted: #9ba7b6;
  --accent: #5b8def; --selection: #8fb3ff; --btn-bg: #343c48;
  --cell-correct: #6fb08a; --cell-valid: #c9b878; --cell-conflict: #c08a92; --cell-text: #14181d;
}
[data-theme="retro"] {
  --page-bg: #2a1740; --page-bg2: #1f1030; --surface: #3a2159; --surface2: #47296c;
  --board-bg: #120720; --border: #ff9f1c; --text: #fff3d6; --text-muted: #d6b8f5;
  --accent: #ff6b35; --selection: #00e5ff; --btn-bg: #47296c;
  --cell-correct: #58b66a; --cell-valid: #d9b84a; --cell-conflict: #d0666a; --cell-text: #1a0b2b;
  --font: "Courier New", ui-monospace, monospace; --radius: 2px; --cell-radius: 0;
}
[data-theme="minimal"] {
  --page-bg: #fff; --page-bg2: #fff; --surface: #fafafa; --surface2: #f2f2f2;
  --board-bg: #d9d9d9; --border: #e3e3e3; --text: #111; --text-muted: #777;
  --accent: #111; --selection: #111; --btn-bg: #fff;
  --cell-correct: #dcece1; --cell-valid: #f3ebcf; --cell-conflict: #f1dcdc; --cell-text: #111;
  --radius: 8px; --cell-radius: 2px;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; }
body {
  background: radial-gradient(120% 80% at 50% 0%, var(--page-bg), var(--page-bg2));
  background-attachment: fixed; background-color: var(--page-bg);
  color: var(--text); font-family: var(--font);
  -webkit-user-select: none; user-select: none; touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; }
#app {
  width: min(100%, 460px); margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 14px max(16px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 10px;
}

.top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.top h1 { min-width: 0; margin: 0; font-size: clamp(24px, 7.4vw, 34px); letter-spacing: .3px; flex: 1; text-align: left; padding-left: 10px; }
.icon-btn, .chip {
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  border-radius: var(--radius);
}
.icon-btn { width: 48px; height: 48px; display: grid; place-items: center; }
.chip { display: flex; align-items: center; gap: 6px; height: 44px; padding: 0 10px; font-weight: 600; font-size: 15px; white-space: nowrap; flex: none; }

/* ---------- Board ---------- */
.boardwrap { position: relative; }
.board {
  container-type: inline-size;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: .9cqw;
  background: var(--board-bg); padding: 1.1cqw; border-radius: var(--radius);
  aspect-ratio: 1; width: 100%;
  border: 2px solid var(--board-bg);
}
.box { display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); gap: .5cqw; }
.cell {
  position: relative; border: 0; padding: 0; margin: 0; border-radius: var(--cell-radius);
  color: var(--cell-text); font-family: inherit; font-weight: 500;
  font-size: 6.4cqw; line-height: 1; display: grid; place-items: center;
  transition: background-color .15s, opacity .15s, transform .15s;
  min-width: 0;
}
.board.emoji .cell { font-size: 5.6cqw; font-weight: 400; }
.cell.s-g { background: var(--cell-correct); }
.cell.s-y { background: var(--cell-valid); }
.cell.s-r { background: var(--cell-conflict); }
.cell .sym { pointer-events: none; }
.board.emoji .cell .sym { text-shadow: 0 .2cqw .5cqw rgba(0,0,0,.28); }
.cell.sel { outline: .8cqw solid var(--selection); outline-offset: -.4cqw; box-shadow: 0 0 2.4cqw var(--selection); z-index: 2; border-radius: calc(var(--cell-radius) + 2px); }
.board.has-focus .cell:not(.foc) { opacity: .38; }
.board.has-focus .cell.foc { transform: scale(1.06); z-index: 1; box-shadow: 0 0 0 .5cqw rgba(255,255,255,.55); }
.cell .mark { position: absolute; top: .5cqw; right: .8cqw; font-size: 2.6cqw; font-weight: 700; display: none; }
.board.markers .cell .mark { display: block; }
.board.markers .cell.s-r { background-image: repeating-linear-gradient(135deg, transparent 0 .9cqw, rgba(0,0,0,.12) .9cqw 1.4cqw); }
.board.markers .cell.s-g .mark::before { content: "✓"; }
.board.markers .cell.s-y .mark::before { content: "•"; font-size: 4cqw; }
.board.markers .cell.s-r .mark::before { content: "!"; }

.cell.pop { animation: pop .22s ease-out; }
.cell.good { animation: good .5s ease-out; }
.cell.perfect { animation: perfect .9s ease-out; z-index: 3; }
@keyframes pop { 0% { transform: scale(.82); } 100% { transform: scale(1); } }
@keyframes good { 0% { box-shadow: 0 0 0 0 rgba(255,255,255,.9); } 100% { box-shadow: 0 0 0 2.4cqw rgba(255,255,255,0); } }
@keyframes perfect { 0% { transform: scale(.8); box-shadow: 0 0 0 0 #fff; } 35% { transform: scale(1.22); box-shadow: 0 0 4cqw 1cqw #fff; } 100% { transform: scale(1); box-shadow: 0 0 0 0 transparent; } }
.board.valid-pulse { animation: validPulse .9s ease-out; }
@keyframes validPulse { 0% { box-shadow: 0 0 0 0 var(--cell-correct); } 40% { box-shadow: 0 0 0 5px var(--cell-correct), 0 0 30px var(--cell-correct); } 100% { box-shadow: 0 0 0 0 transparent; } }
.board.complete .cell { animation: wave 1s ease-in-out; animation-delay: calc(var(--i) * 14ms); }
@keyframes wave { 0%,100% { transform: scale(1); } 40% { transform: scale(1.2) rotate(-4deg); } }
.board.finished .cell { cursor: default; }

.toast {
  position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%) scale(.9);
  background: rgba(8, 14, 28, .92); color: #fff; padding: 10px 20px; border-radius: 999px;
  font-weight: 800; letter-spacing: .5px; font-size: 18px; white-space: nowrap;
  border: 2px solid var(--cell-correct); box-shadow: 0 6px 30px rgba(0,0,0,.4);
  opacity: 0; pointer-events: none; transition: opacity .18s, transform .18s; z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }

/* ---------- Symbol focus ---------- */
.focusbar { display: grid; grid-template-columns: repeat(9, minmax(0, 1fr)); gap: 5px; }
.focusbar button {
  aspect-ratio: .78; min-height: 52px; background: var(--btn-bg); border: 1px solid var(--border);
  border-radius: 10px; font-size: clamp(18px, 6vw, 28px); display: grid; place-items: center; padding: 0;
  box-shadow: 0 2px 0 rgba(0,0,0,.25);
}
.focusbar.emoji button { font-size: clamp(16px, 5.4vw, 26px); }
.focusbar button.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text, #fff); transform: translateY(-2px); }

/* ---------- Stats ---------- */
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.tile, .status, .legend {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.tile { display: flex; align-items: center; gap: 5px; padding: 9px 6px; min-width: 0; overflow: hidden; }
.tile > div { min-width: 0; }
.tile svg { width: 22px; height: 22px; }
.tile svg, .tile .emo { flex: none; color: var(--text); }
.tile .emo { font-size: 20px; }
.tile small, .status small { display: block; color: var(--text-muted); font-size: 12px; }
.tile b { font-size: clamp(15px, 4.6vw, 21px); font-weight: 600; font-variant-numeric: tabular-nums; }

.status { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; }
.status-l { display: flex; align-items: center; gap: 12px; padding-right: 12px; border-right: 1px solid var(--border); }
.status b { font-size: 21px; font-weight: 600; font-variant-numeric: tabular-nums; }
.status-ico { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font-weight: 900; color: #3a0b12; background: var(--cell-conflict); flex: none; }
.status-ico.yellow { background: var(--cell-valid); color: #3d3200; }
.status-ico.done { background: var(--cell-correct); color: #07361a; }
.status-r { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; color: var(--text-muted); }
.prog { flex: 1; min-width: 0; }
.bar { height: 10px; border-radius: 999px; background: var(--board-bg); overflow: hidden; margin-top: 6px; }
.bar i { display: block; height: 100%; width: 0; background: var(--cell-correct); border-radius: 999px; transition: width .3s; }
#progPct { color: var(--text); font-size: 15px; font-variant-numeric: tabular-nums; }
.status.valid { box-shadow: 0 0 0 2px var(--cell-correct) inset; }

/* ---------- Buttons ---------- */
.actions { display: grid; grid-template-columns: minmax(0,1.4fr) minmax(0,1fr) minmax(0,1fr); gap: 6px; }
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px; height: 54px;
  background: var(--btn-bg); border: 1px solid var(--border); border-radius: var(--radius);
  font-weight: 600; font-size: 15px; white-space: nowrap; padding: 0 6px; min-width: 0;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text, #fff); }
.btn:disabled { opacity: .4; cursor: default; }
.btn:active:not(:disabled), .focusbar button:active, .chip:active, .icon-btn:active { filter: brightness(1.2); }

/* ---------- Legend ---------- */
.legend summary { list-style: none; display: flex; align-items: center; gap: 12px; padding: 14px; cursor: pointer; }
.legend summary::-webkit-details-marker { display: none; }
.legend summary .chev { margin-left: auto; transition: transform .2s; }
.legend:not([open]) .chev { transform: rotate(180deg); }
.legend-body { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; padding: 2px 14px 16px; font-size: 13px; color: var(--text-muted); }
.legend-body > div { display: flex; align-items: center; gap: 8px; }
.sw { width: 28px; height: 28px; border-radius: 6px; flex: none; }
.sw.s-g { background: var(--cell-correct); } .sw.s-y { background: var(--cell-valid); } .sw.s-r { background: var(--cell-conflict); }

/* ---------- Modal / sheets ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: flex-end; justify-content: center; z-index: 50; }
.modal[hidden] { display: none; }
.sheet {
  width: min(100%, 520px); max-height: 92vh; overflow: auto; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 22px 22px 0 0; padding: 18px 16px max(22px, env(safe-area-inset-bottom));
  animation: slideUp .22s ease-out;
}
@media (min-width: 560px) { .modal { align-items: center; } .sheet { border-radius: 22px; } }
@keyframes slideUp { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet h2 { margin: 0 0 4px; font-size: 24px; }
.sheet h3 { margin: 18px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--text-muted); }
.sheet p { line-height: 1.45; margin: 8px 0; }
.sheet .sub { color: var(--text-muted); margin: 0 0 8px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.x { background: var(--surface2); border: 1px solid var(--border); border-radius: 50%; width: 38px; height: 38px; font-size: 18px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > * { flex: 1; }
.opt {
  background: var(--surface2); border: 2px solid var(--border); border-radius: 12px; padding: 10px 12px;
  text-align: left; font-weight: 600; min-height: 46px;
}
.opt.on { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.opt small { display: block; font-weight: 400; color: var(--text-muted); margin-top: 2px; }
.opt .prev { display: block; font-size: 15px; letter-spacing: 1px; margin-top: 4px; font-weight: 400; }
.opt:disabled { opacity: .5; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.themes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.theme-opt { padding: 8px; text-align: center; font-size: 13px; }
.theme-opt .sws { display: flex; height: 16px; border-radius: 4px; overflow: hidden; margin-bottom: 6px; }
.theme-opt .sws i { flex: 1; }
.toggle { display: flex; justify-content: space-between; align-items: center; width: 100%; background: var(--surface2); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; font-weight: 600; margin-bottom: 8px; }
.toggle .sw-track { width: 46px; height: 26px; border-radius: 999px; background: var(--board-bg); position: relative; transition: background .15s; flex: none; }
.toggle .sw-track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform .15s; }
.toggle.on .sw-track { background: var(--accent); }
.toggle.on .sw-track::after { transform: translateX(20px); }
.statgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.statgrid div { background: var(--surface2); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; }
.statgrid small { color: var(--text-muted); display: block; font-size: 12px; }
.statgrid b { font-size: 20px; font-variant-numeric: tabular-nums; }
.report { text-align: center; }
.report .big { font-size: 30px; font-weight: 800; color: var(--cell-correct); letter-spacing: 1px; margin-top: 4px; }
.report .statgrid { text-align: left; }
.btnrow { display: flex; gap: 8px; margin-top: 16px; }
.btnrow .btn { flex: 1; }
.lg-line { display: flex; gap: 10px; align-items: center; margin: 8px 0; }
.legend-ex { width: 34px; height: 34px; border-radius: 6px; flex: none; }

/* ---------- Reduced motion ---------- */
body.reduce-motion *, body.reduce-motion *::before, body.reduce-motion *::after { animation: none !important; transition: none !important; }
body.reduce-motion .cell.perfect, body.reduce-motion .cell.good { box-shadow: 0 0 0 .7cqw #fff, 0 0 3cqw #fff; }
@media (max-height: 740px) { #app { gap: 7px; } .btn { height: 46px; } .focusbar button { min-height: 44px; } }
