/* Nunito (SIL OFL 1.1, see fonts/OFL.txt), self-hosted, latin subset, variable weight */
@font-face { font-family: 'Nunito'; font-style: normal; font-weight: 200 1000; font-display: swap; src: url('fonts/nunito.woff2') format('woff2'); }
@font-face { font-family: 'Nunito'; font-style: italic; font-weight: 200 1000; font-display: swap; src: url('fonts/nunito-italic.woff2') format('woff2'); }

:root {
  --bg: #16283f;
  --bg2: #1d3350;
  --line: rgba(255, 255, 255, 0.05);
  --frame: #e8572a;
  --bad: #ff4d4d;
  --well: #57534f;
  --empty: #9aa0a8;
  --empty-edge: #7a8088;
  --gold: #f2ac2b;
  --gold-edge: #c9831a;
  --gold-hi: #ffd06a;
  --ink: #f3ead8;
  --ink-dim: #9fb0c4;
  --accent: #f5c24a;
  --cell: min(17vw, 76px); /* 17vw keeps clues + board inside a 375 px phone */
  --gap: calc(var(--cell) * 0.1);
  --mini: calc(var(--cell) * 0.52); /* tray pieces at about half board scale */
  font-family: 'Nunito', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  color: var(--ink);
  background:
    radial-gradient(ellipse at 50% 20%, var(--bg2), transparent 70%),
    linear-gradient(var(--line) 1px, transparent 1px) 0 0 / 64px 64px,
    linear-gradient(90deg, var(--line) 1px, transparent 1px) 0 0 / 64px 64px,
    var(--bg);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.app {
  max-width: 560px; margin: 0 auto; min-height: 100%;
  padding: max(12px, env(safe-area-inset-top)) 14px 20px;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
}

/* top bar */
.bar { width: 100%; display: grid; grid-template-columns: 1fr auto 1fr; align-items: start; }
.bar > .chip { justify-self: start; }
.chip {
  display: inline-flex; flex-direction: column; align-items: center; gap: 2px;
  min-width: 60px; padding: 8px 10px; border: 0; border-radius: 12px;
  background: rgba(255, 255, 255, 0.08); font-size: 11px; font-weight: 800; letter-spacing: 0.04em;
}
.chip span { font-size: 16px; line-height: 1; }
.chip:hover { background: rgba(255, 255, 255, 0.14); }
.chip:disabled { opacity: 0.35; cursor: default; }
.title { text-align: center; }
.timer { font-size: 40px; font-weight: 900; font-style: italic; letter-spacing: 0.04em; font-variant-numeric: tabular-nums; text-shadow: 0 3px 0 rgba(0, 0, 0, 0.35); }
.sub { font-size: 13px; font-weight: 800; font-style: italic; color: var(--accent); letter-spacing: 0.08em; }

/* clues + board */
.puzzle {
  position: relative;
  --edge: calc(var(--cell) * 0.13 + var(--gap) * 2); /* frame + padding, so clues line up with cells */
  --clue: calc(var(--cell) * 0.7);
  display: grid; grid-template-columns: var(--clue) auto var(--clue); grid-template-areas: '. cols .' 'rows board .';
  column-gap: 6px; row-gap: 6px;
}
.clues { display: grid; gap: var(--gap); }
.clues.cols { grid-area: cols; grid-template-columns: repeat(var(--n, 3), var(--cell)); padding: 0 var(--edge); }
.clues.rows { grid-area: rows; grid-template-rows: repeat(var(--n, 3), var(--cell)); padding: var(--edge) 0; }
.board { grid-area: board; }
.clue {
  display: grid; place-items: center; font-size: calc(var(--cell) * 0.46); font-weight: 900;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.35); transition: color 0.2s, transform 0.2s;
}
.clues.rows .clue { justify-items: end; }
.clue { position: relative; }
.clue.ok { color: var(--gold); }
.clue.over { color: var(--bad); }
/* ✓ when a row/column is done, red "+N" when it has too many squares */
.clue[data-badge]:not([data-badge=''])::after {
  content: attr(data-badge); position: absolute; top: -2px; right: -6px;
  font-size: 13px; font-style: normal; line-height: 1; padding: 3px 5px; border-radius: 999px;
  background: var(--gold); color: #3b2405; text-shadow: none; z-index: 1;
}
.clue.over::after { background: var(--bad) !important; color: #fff !important; }
.puzzle.stuck .clue:not(.ok) { color: var(--bad); animation: pulse 0.9s 3; }
@keyframes pulse { 50% { transform: scale(1.18); } }
.clue.pre { opacity: 0.75; font-style: italic; }
.clue.tick { animation: tick 0.25s; }
@keyframes tick { 50% { transform: scale(1.25); } }
/* board */
.board-wrap { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.board {
  display: grid; gap: var(--gap); padding: calc(var(--gap) * 2);
  grid-template-columns: repeat(var(--n, 3), var(--cell));
  background: var(--well); border: calc(var(--cell) * 0.13) solid var(--frame); border-radius: calc(var(--cell) * 0.42);
  box-shadow: 0 10px 0 rgba(0, 0, 0, 0.3), 0 20px 40px rgba(0, 0, 0, 0.3);
  touch-action: none;
}
.board.shake { animation: shake 0.45s; }
.board.win { animation: pop 0.6s; }
.cell {
  position: relative; width: var(--cell); height: var(--cell); padding: 0; border: 0;
  border-radius: calc(var(--cell) * 0.14);
  background: var(--empty); box-shadow: inset 0 calc(var(--cell) * -0.08) 0 var(--empty-edge);
  transition: background 0.15s, box-shadow 0.15s, transform 0.15s;
}
.cell.on { background: var(--gold); box-shadow: inset 0 calc(var(--cell) * -0.08) 0 var(--gold-edge), inset 0 2px 0 var(--gold-hi); }
/* a stud on gold cells, so filled vs empty doesn't rely on colour alone */
.cell.on::before {
  content: ''; position: absolute; left: 33%; top: 30%; width: 34%; height: 34%; border-radius: 30%;
  border: calc(var(--cell) * 0.035) solid rgba(255, 236, 190, 0.6); box-shadow: 0 2px 0 rgba(150, 90, 10, 0.35);
}
.cell.flip { animation: flip 0.28s; }
.board.win .cell { animation: bounce 0.5s calc(var(--i) * 40ms); }
.cell.target { transform: scale(0.94); }

/* preview markers */
.mk { display: inline-block; vertical-align: middle; width: 18px; height: 18px; position: relative; margin: 0 4px 0 10px; }
.cell.fill::after, .mk.ring::after {
  content: ''; position: absolute; inset: 22%; border-radius: 50%;
  border: calc(var(--cell) * 0.07) solid var(--gold-hi); opacity: 0.9;
}
.mk.ring::after { inset: 0; border-width: 3px; }
.cell.unfill::after, .mk.slash::after {
  content: ''; position: absolute; left: 50%; top: 18%; height: 64%; width: calc(var(--cell) * 0.11);
  background: #3a3734; border-radius: 4px; transform: translateX(-50%) rotate(45deg); z-index: 1;
}
.mk.slash::after { width: 3px; top: 0; height: 100%; background: var(--frame); }
.cell.bad::after { content: ''; position: absolute; inset: 0; border-radius: inherit; background: rgba(232, 87, 42, 0.35); }

.msg { min-height: 1.3em; margin: 4px 0 0; font-weight: 800; color: var(--accent); text-align: center; }
.msg.stuck { padding: 8px 14px; border-radius: 12px; background: rgba(255, 77, 77, 0.14); color: #ffb3b3; font-size: 16px; }

/* bottom action bar: within thumb reach */
.actions { display: flex; gap: 10px; width: min(100%, 340px); }
.act {
  flex: 1; min-height: 48px; border: 2px solid rgba(255, 255, 255, 0.22); border-radius: 14px;
  background: rgba(255, 255, 255, 0.06); font-weight: 900; font-size: 15px; letter-spacing: 0.02em;
}
.act:hover:not(:disabled) { background: rgba(255, 255, 255, 0.12); }
.act:disabled { cursor: default; background: transparent; border-style: dashed; color: var(--ink-dim); }
.actions.stuck .act { background: var(--frame); border-color: transparent; color: #fff; box-shadow: 0 4px 0 #a83a16; }
.actions.stuck #undoBtn { background: var(--gold); color: #3b2405; box-shadow: 0 4px 0 var(--gold-edge); }
.actions.done #restartBtn { background: var(--frame); border-color: transparent; color: #fff; box-shadow: 0 4px 0 #a83a16; }

/* tray */
.tray { display: grid; grid-template-columns: repeat(2, auto); gap: 10px; }
.slot {
  width: calc(var(--mini) * 3 + 24px); height: calc(var(--mini) * 3 + 24px);
  display: grid; place-items: center; padding: 0;
  border: 3px dashed rgba(255, 255, 255, 0.28); border-radius: 18px; background: rgba(0, 0, 0, 0.12);
  touch-action: none; transition: transform 0.15s, opacity 0.2s, border-color 0.15s;
}
.slot:hover:not(.used) { transform: translateY(-2px); border-color: rgba(255, 255, 255, 0.5); }
.slot.sel { border-color: var(--accent); border-style: solid; background: rgba(245, 194, 74, 0.1); }
.slot.used { opacity: 0.25; cursor: default; }
.slot.dragging .piece { opacity: 0.25; }
.piece { display: grid; gap: 3px; }
.blk { width: var(--mini); height: var(--mini); border-radius: 5px; background: var(--gold); box-shadow: inset 0 -3px 0 var(--gold-edge), inset 0 1px 0 var(--gold-hi); }
.drag-ghost { position: fixed; left: 0; top: 0; pointer-events: none; z-index: 50; gap: var(--gap); filter: drop-shadow(0 10px 10px rgba(0, 0, 0, 0.4)); }
.drag-ghost .blk { width: var(--cell); height: var(--cell); border-radius: calc(var(--cell) * 0.14); opacity: 0.85; }


/* dialogs */
.dlg {
  width: min(92vw, 420px); max-height: 92vh; border: 0; border-radius: 22px; padding: 22px;
  background: var(--bg2); color: var(--ink); box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
  border-top: 6px solid var(--frame);
}
.dlg::backdrop { background: rgba(6, 12, 22, 0.7); backdrop-filter: blur(3px); }
.dlg h2 { margin: 0 0 10px; font-size: 26px; font-weight: 900; font-style: italic; }
.dlg ol { padding-left: 20px; line-height: 1.5; }
.dlg form { margin: 0; text-align: center; }
.dim { color: var(--ink-dim); font-size: 14px; text-align: center; }
.legend { font-size: 14px; }
/* How to play: three illustrated steps */
.how { display: grid; gap: 14px; margin: 6px 0 4px; }
.step { display: grid; grid-template-columns: 84px 1fr; gap: 14px; align-items: center; }
.step p { margin: 0; line-height: 1.4; font-size: 15px; }
.step .bad { color: var(--bad); }
.demo { position: relative; justify-self: center; display: grid; gap: 3px; padding: 5px; border-radius: 9px; background: var(--well); border: 3px solid var(--frame); }
.demo .d { width: 18px; height: 18px; border-radius: 4px; background: var(--empty); }
.demo .d.on { background: var(--gold); }
.demo.place { grid-template-columns: repeat(3, 18px); }
.demo .drop { position: absolute; left: 8px; top: 8px; display: flex; gap: 3px; animation: drop 2.4s infinite; }
.demo .drop .d { box-shadow: 0 0 0 2px var(--accent); }
.demo.flip { grid-template-columns: repeat(3, 18px); }
.demo .cover { position: absolute; left: 3px; top: 3px; width: 45px; height: 24px; border: 2px solid var(--accent); border-radius: 6px; animation: cover 2.4s infinite; }
.demo .to-gold { animation: to-gold 2.4s infinite; }
.demo .to-grey { animation: to-grey 2.4s infinite; }
.demo.nums { display: flex; align-items: center; gap: 6px; padding: 6px 8px; background: none; border: 0; font-weight: 900; font-size: 24px; }
.demo .arrow { font-size: 16px; color: var(--ink-dim); }
.demo .num.ok { color: var(--gold); }
.demo .num.ok::after { content: '✓'; font-size: 13px; vertical-align: super; margin-left: 1px; }
@keyframes drop { 0%, 15% { transform: translateY(-26px); opacity: 0; } 45%, 80% { transform: none; opacity: 1; } 100% { opacity: 0; } }
@keyframes cover { 0%, 30% { opacity: 0; } 40%, 90% { opacity: 1; } 100% { opacity: 0; } }
@keyframes to-gold { 0%, 45% { background: var(--empty); } 55%, 100% { background: var(--gold); } }
@keyframes to-grey { 0%, 45% { background: var(--gold); } 55%, 100% { background: var(--empty); } }

/* first-game hint pointing at the clue numbers */
.hint {
  position: absolute; z-index: 5; left: 50%; top: calc(var(--cell) * 0.85); transform: translateX(-50%); width: max-content; max-width: 260px;
  padding: 10px 14px; border-radius: 12px; background: var(--ink); color: var(--bg); font-weight: 800; font-size: 14px; line-height: 1.35; text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4); cursor: pointer;
}
.hint::before { content: ''; position: absolute; left: 50%; top: -7px; margin-left: -7px; border: 7px solid transparent; border-top: 0; border-bottom-color: var(--ink); }

/* win burst */
.spark { position: fixed; z-index: 60; width: 12px; height: 12px; border-radius: 3px; pointer-events: none; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.board > [role='row'] { display: contents; }
.done-head { text-align: center; }
.badge { display: inline-block; padding: 4px 12px; border-radius: 999px; background: var(--gold); color: #3b2405; font-weight: 900; letter-spacing: 0.12em; font-size: 12px; margin-bottom: 6px; }
.beats { text-align: center; font-weight: 800; color: var(--accent); margin: 8px 0; }
.card { display: block; width: min(78%, 300px); aspect-ratio: 1; border-radius: 14px; margin: 6px auto 14px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35); }
.btn.wide { display: block; width: 100%; padding: 14px; font-size: 18px; }
.links { display: flex; justify-content: center; align-items: center; gap: 10px; margin: 12px 0 4px; color: var(--ink-dim); }
.link { border: 0; background: none; padding: 6px 4px; font-weight: 800; color: var(--ink); text-decoration: underline; text-underline-offset: 3px; }
.link:hover { color: var(--accent); }
.btn { border: 0; border-radius: 12px; padding: 11px 16px; font-weight: 900; background: rgba(255, 255, 255, 0.1); }
.btn:hover { background: rgba(255, 255, 255, 0.18); }
.btn.primary { background: var(--frame); color: #fff; box-shadow: 0 4px 0 #a83a16; }
.btn.primary:active { transform: translateY(2px); box-shadow: 0 2px 0 #a83a16; }
.btn.ghost { background: none; color: var(--ink-dim); margin-top: 4px; }

.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 100;
  background: var(--ink); color: var(--bg); font-weight: 800; padding: 10px 16px; border-radius: 12px;
}

@keyframes flip { 50% { transform: scale(0.82) rotate(-4deg); } }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
@keyframes pop { 40% { transform: scale(1.06); } }
@keyframes bounce { 40% { transform: translateY(-10px); } }
@media (max-width: 400px) { .timer { font-size: 30px; } }
/* short phones (e.g. 375 x 667): smaller cells, and the buttons stay pinned within thumb reach */
@media (orientation: portrait) and (max-height: 740px) {
  :root { --cell: min(17vw, 76px, 8.5vh); }
  .actions { position: sticky; bottom: 0; z-index: 4; width: 100%; padding: 16px 14px max(10px, env(safe-area-inset-bottom)); background: linear-gradient(transparent, var(--bg) 14px); }
}
/* phones on their side: board on the left, pieces and buttons on the right */
@media (orientation: landscape) and (max-height: 520px) {
  :root { --cell: min(13vh, 60px); }
  .app {
    max-width: none; padding-bottom: 8px; display: grid; justify-content: center; align-items: center; column-gap: 36px; row-gap: 8px;
    grid-template-columns: auto auto; grid-template-areas: 'bar bar' 'board tray' 'board actions';
  }
  .bar { grid-area: bar; } .board-wrap { grid-area: board; } .tray { grid-area: tray; } .actions { grid-area: actions; width: 100%; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
