/* ════════════════════════════════════════════════════════════════════════════
   NUMBER SUMS — game styles, layered on top of washi.css
   ════════════════════════════════════════════════════════════════════════════ */

:root {
  /* Region tints — the four washi accents plus indigo and rose, so adjoining
     regions always have a distinguishable neighbour to pick from. */
  --rg-0: #c0392b;   /* shu — vermilion */
  --rg-1: #2e7d5e;   /* jade            */
  --rg-2: #7c5cbf;   /* murasaki        */
  --rg-3: #b07d2e;   /* gold            */
  --rg-4: #2f6f9e;   /* ai — indigo     */
  --rg-5: #b0567f;   /* rose            */

  --tint: 13%;       /* how strongly a region washes its cells */
}

[data-theme="sumi"] {
  --rg-0: #e0594b;
  --rg-1: #5cb78f;
  --rg-2: #a487e0;
  --rg-3: #d6a24e;
  --rg-4: #61a3d0;
  --rg-5: #dd85ac;

  --tint: 20%;
}

html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  display: flex; flex-direction: column;
  -webkit-touch-callout: none;
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ── top bar ──────────────────────────────────────────────────────────────── */
.game-header {
  position: relative; z-index: 60; flex: 0 0 auto;
  display: flex; align-items: center; gap: var(--sp-4);
  padding: var(--sp-3) var(--sp-5);
  padding-top: calc(var(--sp-3) + env(safe-area-inset-top));
  padding-left: calc(var(--sp-5) + env(safe-area-inset-left));
  padding-right: calc(var(--sp-5) + env(safe-area-inset-right));
  border-bottom: var(--bw-strong) solid var(--ink);
  background: color-mix(in srgb, var(--paper) 90%, transparent);
  backdrop-filter: blur(6px);
}
.game-header .brand { display: flex; align-items: baseline; gap: var(--sp-3); flex-wrap: wrap; }
.game-header .mark { font-family: var(--font-display); font-size: var(--fs-lg); font-weight: 700; }
.game-header .mark-en {
  font-size: 0.6rem; letter-spacing: var(--tracking-wide);
  text-transform: uppercase; color: var(--muted);
}

.hud { display: flex; align-items: center; gap: var(--sp-5); margin-left: auto; }
.hud .stat { display: flex; flex-direction: column; align-items: center; gap: 0; }
.hud .stat-num { font-size: var(--fs-lg); font-variant-numeric: tabular-nums; line-height: 1.1; }
.hud .stat-label {
  font-size: 0.58rem; letter-spacing: var(--tracking-wide);
  text-transform: uppercase; color: var(--muted);
}

/* ── control strip ────────────────────────────────────────────────────────── */
.controls {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-5);
  padding-left: calc(var(--sp-5) + env(safe-area-inset-left));
  padding-right: calc(var(--sp-5) + env(safe-area-inset-right));
  border-bottom: 1px solid var(--border);
  background: var(--paper-2);
  overflow-x: auto; scrollbar-width: none;
}
.controls::-webkit-scrollbar { display: none; }
.controls .btn, .controls .select { white-space: nowrap; }
.controls .segmented .btn { font-size: 0.6rem; }
.controls .spacer { flex: 1 1 auto; min-width: var(--sp-2); }
.ctl-label {
  font-size: var(--fs-xs); letter-spacing: var(--tracking-wide);
  text-transform: uppercase; color: var(--muted);
}
/* washi styles selects as full-width form fields with a drawn arrow. Here it is
   a compact toolbar control instead: shrink to content, and leave room on the
   right for the arrow without using the `background` shorthand, which would
   throw the arrow's gradients away. */
.select {
  flex: 0 0 auto;
  width: auto;
  font-family: var(--font-mono); font-size: 0.7rem;
  padding: var(--sp-1) 1.8em var(--sp-1) var(--sp-2);
  background-position: calc(100% - 12px) center, calc(100% - 7px) center;
  border: var(--bw) solid var(--border-strong); border-radius: var(--r);
  background-color: var(--card); color: var(--text);
}

/* ── board area ───────────────────────────────────────────────────────────── */
#board-wrap {
  position: relative; flex: 1 1 auto; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  padding: var(--sp-3);
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 30%, color-mix(in srgb, var(--jade) 8%, transparent), transparent 62%),
    radial-gradient(circle at 10% 92%, var(--accent-soft), transparent 55%),
    radial-gradient(circle at 90% 10%, var(--murasaki-soft), transparent 55%);
}

#board {
  --cell: 44px;
  --clue: calc(var(--cell) * 0.86);
  display: grid;
  grid-template-columns: var(--clue) repeat(var(--n), var(--cell));
  grid-auto-rows: var(--cell);
  gap: 0;
}
#board > .corner { grid-row: 1; height: var(--clue); }
#board > .clue-col { height: var(--clue); }

/* ── clues ────────────────────────────────────────────────────────────────── */
.clue {
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-mono); font-weight: 500;
  font-size: calc(var(--cell) * 0.34);
  font-variant-numeric: tabular-nums;
  color: var(--ink-2);
  transition: color var(--t), opacity var(--t);
}
.clue.is-done { color: var(--jade); opacity: 0.5; }
.clue.is-over { color: var(--accent); font-weight: 700; }

/* ── a cell ───────────────────────────────────────────────────────────────── */
.cell {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-mono); font-weight: 500;
  font-size: calc(var(--cell) * 0.40);
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  background: var(--card);
  border: 1px solid color-mix(in srgb, var(--line) 60%, transparent);
  cursor: pointer;
  touch-action: none;                       /* we drive drag marking ourselves */
  transition: background var(--t), color var(--t);
}
.cell.in-region { background: color-mix(in srgb, var(--rc) var(--tint), var(--card)); }

/* The number itself, so it can scale/fade independently of the tile. */
.cell .val {
  position: relative; z-index: 2;
  transition: opacity var(--t), transform var(--t), color var(--t);
}

/* Circled — a drawn ring, the way you would ring it in pencil. */
.cell .ring {
  position: absolute; z-index: 1;
  left: 50%; top: 50%;
  width: calc(var(--cell) * 0.76); height: calc(var(--cell) * 0.76);
  transform: translate(-50%, -50%) scale(0.4);
  border: calc(var(--cell) * 0.075) solid var(--accent);
  border-radius: 50%;
  opacity: 0;
  transition: opacity var(--t), transform var(--t-slow) cubic-bezier(.2,1.4,.4,1);
  pointer-events: none;
}
.cell.is-keep .ring { opacity: 1; transform: translate(-50%, -50%) scale(1); }
.cell.is-keep .val { font-weight: 700; }

/* Erased — the number is left as a ghost so a misclick is easy to spot. */
.cell.is-drop .val { opacity: 0.18; transform: scale(0.78); }
.cell.is-drop { background: color-mix(in srgb, var(--ink) 4%, var(--card)); }
.cell.is-drop.in-region {
  background: color-mix(in srgb, var(--rc) calc(var(--tint) / 3), var(--paper-2));
}

@media (hover: hover) {
  .cell:hover { background: color-mix(in srgb, var(--accent) 8%, var(--card)); }
  .cell.in-region:hover {
    background: color-mix(in srgb, var(--rc) calc(var(--tint) + 10%), var(--card));
  }
}

/* Region outline — drawn on an overlay so it sits above the neighbouring
   cell's own hairline border rather than fighting it. */
.cell::after {
  content: ''; position: absolute; inset: -1px; z-index: 3;
  pointer-events: none;
  border: 0 solid var(--rc);
}
.cell.r-t::after { border-top-width:    calc(var(--cell) * 0.055); }
.cell.r-b::after { border-bottom-width: calc(var(--cell) * 0.055); }
.cell.r-l::after { border-left-width:   calc(var(--cell) * 0.055); }
.cell.r-r::after { border-right-width:  calc(var(--cell) * 0.055); }

/* Region total, pinned to the region's top-left cell. */
.rbadge {
  position: absolute; z-index: 5;
  left: calc(var(--cell) * -0.06); top: calc(var(--cell) * -0.06);
  min-width: calc(var(--cell) * 0.36); height: calc(var(--cell) * 0.36);
  padding: 0 calc(var(--cell) * 0.05);
  display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--cell) * 0.24); font-weight: 500; line-height: 1;
  font-variant-numeric: tabular-nums;
  color: #fff; background: var(--rc);
  border-radius: var(--r-pill);
  box-shadow: var(--shadow-sm);
  pointer-events: none;
}
.rbadge.is-done { opacity: 0.4; }
.rbadge.is-over { box-shadow: 0 0 0 2px var(--accent); }

/* Feedback flashes */
.cell.flash-hint { animation: flash-hint 1.1s ease-out 2; }
.cell.flash-bad  { animation: flash-bad  0.4s ease-out 2; }
@keyframes flash-hint {
  0%, 100% { box-shadow: none; }
  40%      { box-shadow: inset 0 0 0 calc(var(--cell) * 0.09) var(--jade); }
}
@keyframes flash-bad {
  0%, 100% { box-shadow: none; }
  40%      { box-shadow: inset 0 0 0 calc(var(--cell) * 0.09) var(--accent); }
}

.cell.is-solved-flash { animation: pop 0.4s ease-out; }
@keyframes pop { 50% { transform: scale(1.14); } }

/* ── mode bar ─────────────────────────────────────────────────────────────── */
.modebar {
  flex: 0 0 auto; z-index: 60;
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-5);
  padding-bottom: calc(var(--sp-3) + env(safe-area-inset-bottom));
  padding-left: calc(var(--sp-5) + env(safe-area-inset-left));
  padding-right: calc(var(--sp-5) + env(safe-area-inset-right));
  border-top: var(--bw-strong) solid var(--ink);
  background: var(--paper-2);
}
.modebar .spacer { flex: 1 1 auto; }
.mode-toggle .btn {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-4); font-size: 0.72rem;
}
.mode-ico {
  display: inline-flex; align-items: center; justify-content: center;
  width: 1.55em; height: 1.55em; font-size: 0.85em; line-height: 1;
  position: relative; flex: 0 0 auto;
}
.mode-ico.ring { border: 2px solid currentColor; border-radius: 50%; }
.mode-ico.strike { opacity: 0.45; }
.mode-ico.strike::after {
  content: ''; position: absolute; left: -10%; right: -10%; top: 50%;
  border-top: 2px solid currentColor; transform: rotate(-20deg);
}
.mode-toggle .btn[aria-pressed="true"] .mode-ico.strike { opacity: 1; }

/* ── status toast ─────────────────────────────────────────────────────────── */
#status {
  position: fixed; left: 50%; bottom: calc(5.5rem + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(0.5rem);
  z-index: 90; max-width: min(90vw, 34rem);
  padding: var(--sp-2) var(--sp-4);
  border: var(--bw) solid var(--ink); border-radius: var(--r-pill);
  background: var(--card); box-shadow: var(--shadow-lg);
  font-size: var(--fs-sm); text-align: center;
  opacity: 0; pointer-events: none; cursor: pointer;
  transition: opacity var(--t-slow), transform var(--t-slow);
}
#status.show { opacity: 1; pointer-events: auto; transform: translateX(-50%) translateY(0); }

/* ── overlays ─────────────────────────────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0; z-index: 100;
  display: none; align-items: center; justify-content: center;
  padding: var(--sp-4);
  background: color-mix(in srgb, var(--ink) 55%, transparent);
  backdrop-filter: blur(3px);
}
.overlay.show { display: flex; }
.modal {
  width: min(30rem, 100%); max-height: 88vh; overflow-y: auto;
  padding: var(--sp-6);
  background: var(--card);
  border: var(--bw-strong) solid var(--ink); border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  text-align: center;
}
.modal-wide { width: min(38rem, 100%); text-align: left; }
.modal h2 { margin: 0 0 var(--sp-2); font-family: var(--font-display); }
.modal h3 {
  margin: var(--sp-5) 0 var(--sp-2); font-family: var(--font-display);
  font-size: var(--fs-md);
}
.modal .lead { color: var(--ink-2); margin: 0 0 var(--sp-3); }
.win-kanji {
  font-family: var(--font-display); font-size: var(--fs-3xl);
  font-weight: 700;                    /* matches the one self-hosted 完 subset */
  color: var(--accent); line-height: 1;
}
#ov-sub { color: var(--muted); font-size: var(--fs-sm); margin: var(--sp-2) 0 0; }
#ov-buttons, #help-buttons {
  display: flex; gap: var(--sp-3); justify-content: center;
  flex-wrap: wrap; margin-top: var(--sp-5);
}
.rules { margin: 0; padding-left: 1.1rem; font-size: var(--fs-sm); line-height: var(--lh-base); }
.rules li { margin-bottom: var(--sp-2); }
.ink-jade { color: var(--jade); font-weight: 700; }
.ink-accent { color: var(--accent); font-weight: 700; }

/* ── phones ───────────────────────────────────────────────────────────────── */
@media (max-width: 560px) {
  .game-header { padding-left: var(--sp-3); padding-right: var(--sp-3); gap: var(--sp-2); }
  .game-header .mark { font-size: var(--fs-md); }
  .game-header .mark-en { display: none; }
  .hud { gap: var(--sp-4); }
  .hud .stat-num { font-size: var(--fs-md); }
  .hud .stat[data-opt] { display: none; }

  /* Everything in the top strip has to fit a 390px phone without scrolling,
     or Help ends up stranded off the right edge where nobody finds it. */
  .controls { padding-left: var(--sp-3); padding-right: var(--sp-3); gap: var(--sp-2); }
  .controls .btn { font-size: 0.58rem; padding: var(--sp-1) var(--sp-2); }
  .controls .segmented .btn { font-size: 0.55rem; }
  .controls .select { font-size: 0.62rem; padding: 2px 1.6em 2px var(--sp-1); }
  .ctl-label { display: none; }
  #btn-restart, #btn-theme { display: none; }

  .modebar { padding-left: var(--sp-3); padding-right: var(--sp-3); gap: var(--sp-2); }
  .modebar .btn { font-size: 0.64rem; padding: var(--sp-2) var(--sp-3); }
  .modal { padding: var(--sp-5); }
}

/* Short landscape phones: give the board every pixel we can spare. */
@media (max-height: 480px) {
  .game-header { padding-top: calc(var(--sp-1) + env(safe-area-inset-top)); padding-bottom: var(--sp-1); }
  .controls { padding-top: 2px; padding-bottom: 2px; }
  .modebar { padding-top: var(--sp-2); padding-bottom: calc(var(--sp-2) + env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001s !important; transition-duration: 0.001s !important; }
}
