:root {
  --bg: #1a0e22;
  --bg-2: #24132f;
  --bg-3: #321b42;
  --line: #4a2a5e;
  --text: #fff1f5;
  --soft: #ecd9ea;
  --muted: #c4a8c8;
  --rose: #ff4f8b;
  --rose-2: #ff76a5;
  --rose-deep: #5a1838;
  --mint: #5ee6c1;
  --pixel: "Pixelify Sans", ui-monospace, monospace;
  --sans: "Outfit", system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font: 400 15px/1.45 var(--sans); -webkit-font-smoothing: antialiased; overflow: hidden; }
button { font: inherit; color: inherit; }

/* One screen: header, HUD, board (fills what's left), pad on touch screens. */
.app { height: 100vh; height: 100dvh; max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; padding: max(10px, env(safe-area-inset-top)) 16px max(10px, env(safe-area-inset-bottom)); }
.topbar { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.topbar h1 { margin: 0; font: 700 clamp(22px, 5.5vw, 30px)/1 var(--pixel); color: var(--rose); text-shadow: 2px 2px 0 var(--rose-deep); white-space: nowrap; }
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.home { flex: none; display: grid; place-items: center; width: 30px; height: 30px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg-2); color: var(--rose); font: 700 22px/1 var(--pixel); text-decoration: none; }
.home:hover { background: var(--bg-3); }
.home:focus-visible { outline: 2px solid var(--mint); outline-offset: 2px; }
.tools { display: flex; gap: 6px; }
.tool { appearance: none; border: 1px solid var(--line); background: var(--bg-2); border-radius: 8px; padding: 6px 10px; font-size: 13px; font-weight: 500; color: var(--soft); cursor: pointer; white-space: nowrap; text-decoration: none; }
.tool:hover { background: var(--bg-3); }
.tool .short { display: none; }
@media (max-width: 480px) {
  .topbar h1 { font-size: 17px; }
  .tools { gap: 4px; }
  .tool .long { display: none; }
  .tool .short { display: inline; }
}

.hud { flex: none; display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 8px; }
.hud-cell { background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; min-width: 0; }
.hud-cell .lbl { display: block; font-size: 10.5px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.hud-cell b { display: block; font: 500 18px/1.2 var(--pixel); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.hud-cell .days { color: var(--mint); }
.hud-cell .days.low { color: #ff8a7a; }

.board { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; container-type: size; }
.stage { position: relative; width: min(100cqw, 100cqh); height: min(100cqw, 100cqh); border-radius: 10px; overflow: hidden; border: 2px solid var(--line); background: #140a1a; touch-action: none; user-select: none; -webkit-user-select: none; }
#screen { display: block; width: 100%; height: 100%; }

.toast { position: absolute; left: 50%; top: 10px; transform: translate(-50%, -8px); max-width: calc(100% - 24px); width: max-content; padding: 6px 12px; border-radius: 8px; background: rgba(26, 14, 34, .94); border: 1px solid var(--rose); color: var(--text); font: 500 13px/1.3 var(--sans); text-align: center; opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; z-index: 3; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.bubble { position: absolute; transform: translate(-50%, calc(-100% - 14px)); max-width: 62%; width: max-content; padding: 6px 10px; border-radius: 10px; background: #fff7fa; color: #2a1230; font: 500 13px/1.25 var(--sans); opacity: 0; transition: opacity .12s; pointer-events: none; z-index: 3; }
.bubble::after { content: ""; position: absolute; left: 50%; bottom: -6px; margin-left: -6px; border: 6px solid transparent; border-bottom: 0; border-top-color: #fff7fa; }
.bubble.show { opacity: 1; }

.flag { position: absolute; right: 10px; bottom: 10px; padding: 4px 10px; border-radius: 8px; background: #123d36; color: var(--mint); font: 600 12px/1.3 var(--sans); opacity: 0; transition: opacity .15s; pointer-events: none; z-index: 3; }
.flag.show { opacity: 1; }

.overlay { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; padding: 16px; background: rgba(20, 10, 26, .82); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); overflow-y: auto; z-index: 4; }
.overlay.show { display: flex; }
#ov-ready { background: transparent; backdrop-filter: none; -webkit-backdrop-filter: none; pointer-events: none; }
.card { text-align: center; max-width: 420px; margin: auto; }
.card h2 { margin: 2px 0 8px; font: 700 clamp(24px, 6vw, 34px)/1.1 var(--pixel); }
.card .hi { color: var(--rose); }
.eyebrow { margin: 0; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.lede { margin: 8px auto 14px; max-width: 30ch; color: var(--soft); }

.ready { text-align: center; text-shadow: 0 2px 0 #000, 0 0 18px rgba(0,0,0,.8); }
.ready span { display: block; font: 500 13px/1.3 var(--sans); letter-spacing: .08em; text-transform: uppercase; color: var(--text); }
.ready b { display: block; font: 700 46px/1 var(--pixel); color: var(--mint); }

.btn { appearance: none; border: 1px solid var(--line); background: var(--bg-3); border-radius: 8px; padding: 10px 20px; font-weight: 600; font-size: 15px; cursor: pointer; transition: transform .08s, background .15s; }
.btn:hover { background: #3f2353; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--rose); border-color: var(--rose); color: var(--bg); box-shadow: 0 3px 0 #a82a5c; }
.btn.primary:hover { background: var(--rose-2); }
.btn.primary:active { box-shadow: 0 1px 0 #a82a5c; transform: translateY(2px); }
.btn.big { width: 100%; padding: 12px 20px; font-size: 17px; }
.btn:focus-visible, .tool:focus-visible, .pad button:focus-visible { outline: 2px solid var(--mint); outline-offset: 2px; }
.row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.score-line { margin: 4px 0 14px; color: var(--muted); }
.score-line b { color: var(--text); font: 700 22px/1 var(--pixel); font-variant-numeric: tabular-nums; }
.score-line .dot { margin: 0 4px; }
.soon-card { margin: 4px auto 12px; color: var(--mint); font: 500 16px/1.3 var(--pixel); max-width: 26ch; }

.bonus { display: grid; grid-template-columns: 1fr auto; gap: 2px 16px; max-width: 250px; margin: 0 auto 14px; text-align: left; font-size: 14px; }
.bonus dt { color: var(--muted); }
.bonus dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.bonus .total { color: var(--text); font-weight: 600; border-top: 1px solid var(--line); padding-top: 4px; }

.pad { display: none; flex: none; position: relative; width: 186px; height: 126px; margin: 8px auto 0; }
.pad button { position: absolute; width: 62px; height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg-2); font-size: 16px; color: var(--rose); touch-action: none; }
.pad button:active, .pad button.on { background: var(--bg-3); }
.pad [data-dir="0"] { left: 62px; top: 0; }
.pad [data-dir="3"] { left: 0; top: 41px; }
.pad [data-dir="1"] { right: 0; top: 41px; }
.pad [data-dir="2"] { left: 62px; top: 82px; }

.touch-only { display: none; }
@media (pointer: coarse) { .pad { display: block; } .touch-only { display: inline; } .kbd-only { display: none; } .board { align-items: flex-start; } }

/* Save-your-score form on the game-over and final screens. */
.save { margin: 0 auto 12px; max-width: 320px; text-align: left; }
.save label { display: block; margin-bottom: 5px; text-align: center; font-weight: 600; font-size: 14px; }
.field { display: flex; gap: 6px; }
.field input { flex: 1; min-width: 0; padding: 9px 12px; border-radius: 8px; border: 1px solid var(--line); background: #12091a; color: var(--text); font: 400 15px var(--sans); }
.field input::placeholder { color: #7d6884; }
.field input:focus-visible { outline: 2px solid var(--mint); outline-offset: 1px; }
.msg { min-height: 1.1em; margin: 4px 0 0; font-size: 13px; color: var(--mint); text-align: center; }
.msg.err { color: #ff8a7a; }
.saved { margin: 0 auto 12px; }
.saved p { margin: 0 0 8px; color: var(--mint); font: 500 17px/1.3 var(--pixel); }

/* Leaderboard pop-up */
.board-card { max-width: 440px; }
.lb-list { list-style: none; margin: 0 0 10px; padding: 0; max-height: min(52vh, 460px); overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; }
.lb-list li { display: grid; grid-template-columns: 34px 1fr auto; gap: 8px; align-items: center; padding: 7px 12px; font-size: 14.5px; }
.lb-list li:nth-child(odd) { background: rgba(255, 255, 255, .03); }
.lb-list li.me { background: rgba(94, 230, 193, .14); }
.lb-list .rk { font: 700 15px/1 var(--pixel); color: var(--muted); }
.lb-list li:nth-child(1) .rk { color: #ffd166; }
.lb-list li:nth-child(2) .rk { color: #d9dce6; }
.lb-list li:nth-child(3) .rk { color: #e3a079; }
.lb-list .nm { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-list .nm small { margin-left: 6px; color: var(--muted); font-size: 12px; }
.lb-list .sc { font: 700 16px/1 var(--pixel); font-variant-numeric: tabular-nums; }
.lb-status { min-height: 1.2em; margin: 0 0 10px; text-align: center; color: var(--muted); font-size: 13px; }
.lb-status:empty { display: none; }

/* Rules pop-up, shown before the first game and from "How to play". */
.rules { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; padding: 12px; background: rgba(15, 7, 20, .86); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); overflow-y: auto; z-index: 10; }
.rules.show { display: flex; }
.rules-card { width: 100%; max-width: 560px; margin: auto; padding: 18px 18px 16px; border-radius: 12px; background: var(--bg-2); border: 1px solid var(--line); box-shadow: 0 20px 60px rgba(0, 0, 0, .5); }
.rules-card h2 { margin: 0 0 6px; text-align: center; font: 700 clamp(30px, 8vw, 42px)/1 var(--pixel); color: var(--rose); text-shadow: 3px 3px 0 var(--rose-deep); }
.what { margin: 0 auto 12px; max-width: 46ch; text-align: center; color: var(--soft); font-size: 14.5px; }
.rules-card h3 { margin: 0 0 6px; font: 700 15px/1.2 var(--pixel); color: var(--mint); letter-spacing: .02em; }
.steps, .cast { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 6px 14px; }
.steps li, .cast li { display: grid; grid-template-columns: 30px 1fr; gap: 8px; align-items: center; font-size: 13.5px; line-height: 1.3; color: var(--soft); }
.steps b, .cast b { color: var(--text); font-weight: 600; }
.steps canvas, .cast canvas { width: 26px; height: auto; image-rendering: pixelated; justify-self: center; }
.steps li { counter-increment: step; }
@media (min-width: 560px) { .steps, .cast { grid-template-columns: 1fr 1fr; } }
.controls { margin: 0 0 12px; font-size: 12.5px; color: var(--muted); text-align: center; }
.beat { margin: 0 0 10px; text-align: center; color: var(--mint); font-weight: 500; }

@media (max-width: 559px) {
  .rules { padding: 8px; }
  .rules-card { padding: 14px 14px 12px; }
  .rules-card h2 { font-size: 30px; margin-bottom: 4px; }
  .what { font-size: 13.5px; margin-bottom: 8px; }
  .rules-card h3 { margin-bottom: 4px; }
  .steps, .cast { gap: 5px; margin-bottom: 9px; }
  .steps li, .cast li { font-size: 13px; line-height: 1.25; grid-template-columns: 26px 1fr; }
  .steps canvas, .cast canvas { width: 22px; }
  .controls { margin-bottom: 9px; }
  .btn.big { padding: 10px 20px; }
}
@media (max-height: 700px) {
  .rules-card { padding: 14px 14px 12px; }
  .what { margin-bottom: 8px; font-size: 13.5px; }
  .steps, .cast { gap: 4px 12px; margin-bottom: 8px; }
  .steps li, .cast li { font-size: 13px; }
}
@media (max-width: 420px) {
  .tool { padding: 5px 8px; font-size: 12px; }
  .hud-cell { padding: 3px 7px; }
  .hud-cell b { font-size: 16px; }
  .hud-cell .lbl { font-size: 9.5px; }
}
@media (prefers-reduced-motion: reduce) { .toast, .bubble { transition: none; } }
