:root {
  --bg: #f3efe7;
  --ink: #23201b;
  --muted: #7d766a;
  --line: rgba(35, 32, 27, 0.12);
  --card: #fffdf8;
  --slot: rgba(35, 32, 27, 0.07);
  --slot-edge: rgba(35, 32, 27, 0.16);
  --chip-edge: rgba(255, 255, 255, 0.92);
  --accent: #2f5bd3;
  --good: #3b7de0;
  --lost: #e8833a;
  --shadow: 0 1px 1.5px rgba(30, 25, 15, 0.16), 0 3px 8px rgba(30, 25, 15, 0.08);
  --lift: 0 10px 28px rgba(30, 25, 15, 0.28), 0 3px 6px rgba(30, 25, 15, 0.16);
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --top-h: 56px;
  --bar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17171a;
    --ink: #f0ece4;
    --muted: #9a958c;
    --line: rgba(240, 236, 228, 0.12);
    --card: #222226;
    --slot: rgba(240, 236, 228, 0.05);
    --slot-edge: rgba(240, 236, 228, 0.16);
    --chip-edge: rgba(23, 23, 26, 0.55);
    --accent: #7ea2ff;
    --shadow: 0 1px 1.5px rgba(0, 0, 0, 0.4), 0 3px 8px rgba(0, 0, 0, 0.25);
    --lift: 0 12px 30px rgba(0, 0, 0, 0.55), 0 3px 6px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 500 15px/1.4 "DM Sans", system-ui, -apple-system, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
svg { display: block; }
.hidden { display: none !important; }

/* ---------- header ---------- */
#top {
  position: fixed; inset: 0 0 auto 0; z-index: 20; max-width: 760px; margin: 0 auto;
  height: calc(var(--top-h) + var(--safe-t)); padding: var(--safe-t) max(14px, env(safe-area-inset-right)) 0 max(14px, env(safe-area-inset-left));
  display: flex; align-items: center; justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1 { margin: 0; font: 800 21px/1 Fraunces, Georgia, serif; letter-spacing: -0.01em; }
.brand p { margin: 3px 0 0; color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.logo { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; width: 26px; height: 26px; flex: none; }
.logo i { border-radius: 3px; }
.logo i:nth-child(1) { background: var(--p0, oklch(0.72 0.15 30)); }
.logo i:nth-child(2) { background: var(--p1, oklch(0.82 0.14 82)); }
.logo i:nth-child(3) { background: var(--p2, oklch(0.6 0.1 195)); }
.logo i:nth-child(4) { background: var(--p3, oklch(0.45 0.15 278)); }
nav { display: flex; gap: 2px; }
nav button {
  width: 42px; height: 44px; border: 0; border-radius: 12px; background: transparent;
  display: grid; place-items: center;
}
nav button:hover { background: var(--slot); }
nav svg, #bar svg, .close svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
nav svg .dot { fill: currentColor; }
#b-mute .x { display: none; }
#b-mute.off .x { display: block; }
#b-mute.off .wave { display: none; }

/* ---------- board ---------- */
#board {
  position: fixed; left: 0; right: 0; top: calc(var(--top-h) + var(--safe-t)); bottom: calc(var(--bar-h) + var(--safe-b));
  touch-action: none;
}
#board .slot, #board .chip, #board .seg, #board .strip-lbl, #board .counter { position: absolute; left: 0; top: 0; }

.slot {
  border-radius: var(--r, 8px);
  background: var(--slot);
  box-shadow: inset 0 0 0 1.5px var(--slot-edge);
  transition: box-shadow 0.15s, background 0.15s;
}
.slot.cell::after {
  content: ""; position: absolute; inset: 30%; border-radius: 50%;
  box-shadow: inset 0 0 0 1.5px var(--slot-edge);
}
.slot.can, .chip.can { box-shadow: inset 0 0 0 2px var(--accent); }
.slot.over { background: color-mix(in srgb, var(--accent) 16%, transparent); box-shadow: inset 0 0 0 2.5px var(--accent); }

.strip-lbl { display: grid; place-items: center; color: var(--muted); }
.strip-lbl svg { width: 62%; height: 62%; fill: currentColor; }
.seg { border-radius: 3px; opacity: 0.24; transition: opacity 0.3s; }
.strip-row { position: absolute; left: 0; top: 0; border-radius: 6px; pointer-events: none; transition: box-shadow 0.15s, background 0.15s; }
.strip-row.can { box-shadow: 0 0 0 2px var(--accent); }
.strip-row.over { box-shadow: 0 0 0 2.5px var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }

.counter { text-align: right; pointer-events: none; display: flex; flex-direction: column; justify-content: center; align-items: flex-end; }
.counter .n { font: 800 var(--n, 34px)/1 Fraunces, Georgia, serif; font-variant-numeric: tabular-nums; transition: transform 0.18s var(--ease); }
.counter .n.bump { transform: scale(1.12); }
.counter .lbl { color: var(--muted); font-size: 12px; margin-top: 4px; white-space: nowrap; }
.counter .lbl b { color: var(--ink); font-weight: 700; }

.chip {
  width: var(--cw); height: var(--ch);
  border-radius: var(--r, 8px);
  background: var(--c);
  color: var(--t);
  --ew: 1.5px; --ec: var(--chip-edge);
  box-shadow: var(--shadow), inset 0 0 0 var(--ew) var(--ec);
  transform-origin: 0 0;
  transition: transform 0.24s var(--ease), border-radius 0.24s, box-shadow 0.2s;
  will-change: transform;
  cursor: grab;
  z-index: 1;
}
.chip .lab {
  position: absolute; left: 0.14em; top: 0.06em; display: flex; align-items: center; gap: 0.12em;
  font: 800 var(--fs) / 1 Fraunces, Georgia, serif; font-variant-numeric: tabular-nums;
  transition: opacity 0.2s;
}
.chip .lab svg { width: 0.62em; height: 0.62em; fill: currentColor; }
.chip .name {
  position: absolute; left: 0.75em; bottom: 0.55em; font: 700 var(--fs2) / 1 "DM Sans", sans-serif;
  letter-spacing: 0.08em; text-transform: uppercase; opacity: 0.8; transition: opacity 0.2s;
}
.chip .big {
  position: absolute; right: 0.5em; bottom: 0.35em; font: 800 var(--fs3) / 1 Fraunces, Georgia, serif; opacity: 0.9;
  transition: opacity 0.2s;
}
.chip.covered .name, .chip.covered .big { opacity: 0; }
.compact .chip .name { display: none; }
.nonum #board .chip .lab, .nonum #board .chip .name, .nonum #board .chip .big, .nonum #board .strip-lbl { display: none; }

.toggle { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin: 14px 0; padding: 12px 14px; border-radius: 14px; background: var(--slot); cursor: pointer; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle i { flex: none; position: relative; width: 50px; height: 30px; border-radius: 999px; background: var(--slot-edge); transition: background 0.2s; }
.toggle i::after { content: ""; position: absolute; left: 3px; top: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform 0.2s var(--ease); }
.toggle input:checked + i { background: var(--ink); }
.toggle input:checked + i::after { transform: translateX(20px); }
.toggle input:focus-visible + i { outline: 2px solid var(--accent); outline-offset: 2px; }
.chip.f { box-shadow: none; cursor: default; }
.chip.f .lab, .chip.f .name, .chip.f .big { opacity: 0; }
.chip.flying { z-index: 400 !important; }
.chip.drag { transition: none; z-index: 1000 !important; box-shadow: var(--lift), inset 0 0 0 var(--ew) var(--ec); cursor: grabbing; }
.chip.deal { transition: transform 0.5s var(--ease); }
.chip.shake { animation: shake 0.32s; }
@keyframes shake {
  0%, 100% { translate: 0 0; } 20% { translate: -5px 0; } 40% { translate: 5px 0; } 60% { translate: -3px 0; } 80% { translate: 3px 0; }
}
.chip.glow { animation: glow 0.9s var(--ease) both; animation-delay: var(--d, 0s); }
@keyframes glow { 0% { filter: none; } 35% { filter: brightness(1.25) saturate(1.2); } 100% { filter: none; } }
.chip.replay-hl { box-shadow: var(--lift), inset 0 0 0 2.5px var(--accent); }

/* ---------- hint + toast ---------- */
#hint {
  position: fixed; left: 50%; bottom: calc(var(--bar-h) + var(--safe-b) + 10px); transform: translateX(-50%);
  max-width: min(92vw, 460px); padding: 9px 14px; border-radius: 12px;
  background: var(--card); box-shadow: var(--shadow), 0 0 0 1px var(--line);
  font-size: 13.5px; text-align: center; z-index: 30; transition: opacity 0.4s; pointer-events: none;
}
#hint.fade { opacity: 0; }
#toast {
  position: fixed; left: 50%; top: calc(var(--top-h) + var(--safe-t) + 8px); transform: translate(-50%, -8px);
  padding: 8px 14px; border-radius: 999px; background: var(--ink); color: var(--bg);
  font-size: 13.5px; font-weight: 600; z-index: 60; opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s var(--ease); white-space: nowrap; max-width: 92vw; overflow: hidden; text-overflow: ellipsis;
}
#toast.on { opacity: 1; transform: translate(-50%, 0); }

/* ---------- bottom bar ---------- */
#bar {
  position: fixed; inset: auto 0 0 0; z-index: 20; max-width: 760px; margin: 0 auto;
  height: calc(var(--bar-h) + var(--safe-b)); padding: 0 max(14px, env(safe-area-inset-right)) var(--safe-b) max(14px, env(safe-area-inset-left));
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
#bar button, .btn {
  height: 46px; padding: 0 18px; border-radius: 14px; border: 0;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-weight: 700; font-size: 15px;
  transition: transform 0.1s, background 0.15s, opacity 0.15s;
}
#bar button:active, .btn:active { transform: scale(0.96); }
#bar button:disabled { opacity: 0.35; }
.solid { background: var(--ink); color: var(--bg); }
.ghost { background: var(--slot); color: var(--ink); box-shadow: inset 0 0 0 1px var(--line); }
.ghost.armed { background: var(--lost); color: #fff; box-shadow: none; }
#b-undo { min-width: 120px; }
#mode { color: var(--muted); font-size: 12.5px; text-align: center; flex: 1; min-width: 0; }
#mode b { color: var(--ink); }
.small { height: 38px !important; padding: 0 14px !important; font-size: 14px !important; }

#replaybar {
  position: fixed; inset: auto 0 0 0; z-index: 25; height: calc(var(--bar-h) + var(--safe-b));
  padding: 0 16px var(--safe-b); display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--bg);
}
#replay-txt { font-weight: 700; }
#replaybar button { height: 46px; border-radius: 14px; border: 0; padding: 0 20px; font-weight: 700; }

/* ---------- sheet ---------- */
#sheet { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
#sheet .scrim { position: absolute; inset: 0; background: rgba(15, 12, 8, 0.38); animation: fade 0.25s both; }
#sheet .panel {
  position: relative; width: min(100%, 480px); max-height: calc(100% - var(--safe-t) - 24px); overflow: auto;
  background: var(--card); border-radius: 22px 22px 0 0; padding: 22px 20px calc(20px + var(--safe-b));
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.2); animation: rise 0.34s var(--ease) both;
  -webkit-user-select: text; user-select: text; overscroll-behavior: contain;
}
@media (min-width: 640px) and (min-height: 560px) {
  #sheet { align-items: center; }
  #sheet .panel { border-radius: 22px; padding-bottom: 22px; }
}
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
.close { position: absolute; right: 10px; top: 10px; width: 44px; height: 44px; border: 0; border-radius: 12px; background: transparent; display: grid; place-items: center; }
.close:hover { background: var(--slot); }
.panel h2 { margin: 0 44px 4px 0; font: 800 26px/1.1 Fraunces, Georgia, serif; letter-spacing: -0.01em; }
.panel h3 { margin: 22px 0 8px; font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.panel p { margin: 6px 0; }
.panel .muted { color: var(--muted); font-size: 13.5px; }

.score { display: flex; align-items: baseline; gap: 12px; margin: 10px 0 4px; flex-wrap: wrap; }
.score .n { font: 800 54px/1 Fraunces, Georgia, serif; font-variant-numeric: tabular-nums; }
.score .of { color: var(--muted); }
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 5px 11px; border-radius: 999px; background: var(--slot); font-weight: 700; }
.badge.perfect { background: linear-gradient(90deg, oklch(0.85 0.09 30), oklch(0.88 0.1 82), oklch(0.82 0.07 195), oklch(0.78 0.09 262)); color: #23201b; }

.strokes { display: flex; flex-wrap: wrap; gap: 4px; margin: 12px 0 4px; min-height: 18px; }
.strokes i { width: 16px; height: 16px; border-radius: 4px; background: var(--good); animation: pop 0.3s var(--ease) both; }
.strokes i.lost { background: var(--lost); }
.strokes i.wait { background: var(--slot); animation: pulse 1s infinite alternate; }
@keyframes pop { from { transform: scale(0.4); opacity: 0; } }
@keyframes pulse { to { opacity: 0.4; } }
.coach { font-size: 14.5px; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 16px; }
.actions .btn.wide { grid-column: 1 / -1; height: 52px; font-size: 16px; }
.actions .btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.stats b { display: block; font: 800 26px/1.1 Fraunces, Georgia, serif; }
.stats span { font-size: 11.5px; color: var(--muted); }
.dist { display: grid; gap: 5px; margin-top: 6px; }
.dist div { display: grid; grid-template-columns: 74px 1fr; align-items: center; gap: 8px; font-size: 13px; }
.dist i { display: block; height: 20px; border-radius: 5px; background: var(--slot); min-width: 26px; text-align: right; padding: 0 7px; font-style: normal; font-weight: 700; line-height: 20px; font-size: 12px; }
.dist i.me { background: var(--ink); color: var(--bg); }
.next { display: flex; justify-content: space-between; align-items: center; margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); }
.next b { font: 800 20px/1 Fraunces, Georgia, serif; font-variant-numeric: tabular-nums; }
.more { margin: 14px 0 0; font-size: 13px; text-align: center; color: var(--muted); }
.more a { color: inherit; font-weight: 700; }

.how { padding-left: 0; list-style: none; margin: 10px 0; display: grid; gap: 12px; }
.how li { display: grid; grid-template-columns: 74px 1fr; gap: 12px; align-items: center; }
.demo { position: relative; height: 62px; }
.demo .chip { position: absolute; cursor: default; transition: none; }
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; margin: 8px 0; font-size: 13.5px; }
.legend span { display: flex; align-items: center; gap: 7px; }
.legend svg { width: 14px; height: 14px; fill: currentColor; }

/* ---------- loading ---------- */
#loading { position: fixed; inset: 0; display: grid; place-content: center; justify-items: center; gap: 14px; background: var(--bg); z-index: 90; color: var(--muted); transition: opacity 0.3s; }
#loading.done { opacity: 0; pointer-events: none; }
#loading .logo { width: 40px; height: 40px; gap: 3px; }
.spin { animation: spin 1.4s var(--ease) infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .chip, .chip.deal { transition-duration: 0.01s; }
  .chip.glow, .spin { animation: none; }
}
.chip.noanim { transition: none !important; }

@media (max-width: 380px) { nav button { width: 38px; } .brand { gap: 8px; } }

.palettes { display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 8px; }
.pal { display: grid; gap: 3px; text-align: left; padding: 9px 9px 10px; border-radius: 14px; border: 0; background: var(--slot); box-shadow: inset 0 0 0 1px var(--line); transition: box-shadow 0.15s, transform 0.1s; }
.pal:active { transform: scale(0.97); }
.pal.on { box-shadow: inset 0 0 0 2.5px var(--ink); }
.pal b { margin-top: 5px; font-size: 14px; }
.pal span { font-size: 11.5px; color: var(--muted); line-height: 1.25; }
.pal-strips { display: grid; gap: 2px; }
.pal-strips div { display: grid; grid-template-columns: repeat(9, 1fr); gap: 1.5px; }
.pal-strips i { height: 9px; border-radius: 2px; }

/* ---------- side markers: what shows which side (warm or cool) a chip is on ---------- */
.mk-stock .chip { --ew: max(3px, calc(var(--cw) * 0.065)); }
.mk-stock .chip.warm { --ec: #fff6e6; }
.mk-stock .chip.cool { --ec: #2b2833; }
.chip .side { display: none; position: absolute; border-radius: 50%; }
.mk-dot .chip .side {
  display: block; width: calc(var(--cw) * 0.17); height: calc(var(--cw) * 0.17);
  right: calc(var(--cw) * 0.08); top: calc(var(--cw) * 0.08); box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.8);
}
.mk-dot .chip.warm .side { background: #ffc94d; }
.mk-dot .chip.cool .side { background: #1d3557; }
.mk-shape .chip.warm { border-radius: calc(var(--cw) * 0.44) calc(var(--cw) * 0.44) var(--r) var(--r); }
.mk-shape .chip.warm .lab { left: calc(var(--cw) * 0.15); top: calc(var(--cw) * 0.1); }
.mk-shape .chip.cool { border-radius: calc(var(--r) * 0.3); }
.chip.f .side { display: none; }

.markers { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.mk-pv { position: relative; height: 90px; --cw: 46px; --ch: 62px; --r: 6px; --fs: 14px; }
.mk-pv .chip { position: absolute; cursor: inherit; transition: none; }
