/* Nyoka look — forest night, leaf green, mango. */
:root {
    --night: #0e1c15;
    --night-2: #13261c;
    --panel: #172d21;
    --line: #264636;
    --text: #e6f3e1;
    --muted: #8fb09c;
    --leaf: #58b947;
    --mango: #ffa62b;
    --mango-ink: #2a1700;
    color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

/* The game screen is a fixed surface: it never scrolls, so swipes only steer. */
html { overflow: hidden; overscroll-behavior: none; }
body { position: fixed; inset: 0; width: 100%; overflow: hidden; overscroll-behavior: none; }

body {
    margin: 0;
    font-family: "Bricolage Grotesque", system-ui, -apple-system, "Segoe UI", sans-serif;
    font-variation-settings: "opsz" 14;
    color: var(--text);
    background: radial-gradient(900px 600px at 50% 35%, #1b3a29 0%, transparent 70%), var(--night);
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
}

/* ---------- layout ---------- */
.shell {
    --extra: 0px;
    --board: min(calc(100vw - 32px), 560px, calc(100dvh - 250px - var(--extra)));
    height: 100%;
    overflow: hidden;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "top" "stage" "runs";
    justify-items: center;
    align-content: center;
    gap: 14px;
    padding: max(14px, env(safe-area-inset-top)) 16px max(18px, env(safe-area-inset-bottom));
}
.has-dpad .shell { --extra: 130px; }
.top, .stage { touch-action: none; }
.top { grid-area: top; width: var(--board); min-width: 280px; display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.stage { grid-area: stage; width: var(--board); min-width: 280px; display: flex; flex-direction: column; gap: 12px; }
.runs { grid-area: runs; width: var(--board); min-width: 280px; }

@media (min-width: 900px) {
    .shell {
        --board: min(560px, calc(100dvh - 190px - var(--extra)));
        grid-template-columns: auto 260px;
        grid-template-areas: "top runs" "stage runs";
        justify-content: center;
        align-content: center;
        column-gap: 36px;
    }
    .top, .stage { justify-self: end; }
    .runs { width: 100%; min-width: 0; align-self: start; margin-top: 6px; }
    .runs-close { display: none; }
}

/* ---------- header ---------- */
.brand { margin: 0; font-size: clamp(28px, 5vw, 38px); line-height: 1; font-weight: 800; letter-spacing: -0.03em; font-variation-settings: "opsz" 72; }
.stats { margin: 0; display: flex; gap: 6px; }
.stat { min-width: 62px; padding: 6px 10px 7px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line); text-align: left; }
.stat dt { font-size: 12px; color: var(--muted); line-height: 1.2; }
.stat dd { margin: 0; font-size: 20px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
#score { display: inline-block; transform-origin: left center; }
#score.pop { animation: pop 0.22s ease-out; }
@keyframes pop { 0% { transform: scale(1.35); color: var(--mango); } 100% { transform: scale(1); } }

/* ---------- board ---------- */
.board {
    position: relative;
    width: 100%;
    aspect-ratio: 1;
    border-radius: 16px;
    overflow: hidden;
    background: var(--night-2);
    border: 3px solid var(--line);
    box-shadow: 0 20px 50px -20px rgba(0, 0, 0, 0.7);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}
.board[data-mode="walls"] { border-color: #3b6a4f; }
.board[data-mode="wrap"] { border-style: dashed; border-color: #2d5640; }
#canvas { display: block; width: 100%; height: 100%; }

/* ---------- overlay ---------- */
.overlay {
    position: absolute; inset: 0; display: grid; place-items: center; padding: 16px;
    background: rgba(9, 20, 14, 0.5);
    backdrop-filter: blur(1.5px);
    -webkit-backdrop-filter: blur(1.5px);
}
.overlay[hidden] { display: none; }
.card { width: min(100%, 340px); display: flex; flex-direction: column; align-items: stretch; gap: 10px; text-align: center; animation: rise 0.28s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.card h2 { margin: 0; font-size: clamp(26px, 6vw, 34px); line-height: 1.05; font-weight: 800; letter-spacing: -0.02em; font-variation-settings: "opsz" 72; }
.card p { margin: 0; font-size: 15px; line-height: 1.4; color: #cfe4c9; }
.card .hint { font-size: 13px; color: var(--muted); }
.card .keys { margin-top: 2px; }
.result { display: flex; flex-direction: column; align-items: center; margin: 2px 0 4px; }
.result .big { font-size: 56px; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -0.03em; font-variation-settings: "opsz" 72; }
.result .sub { font-size: 14px; color: var(--mango); font-weight: 600; }

.seg { display: grid; grid-template-columns: 1fr 1fr; padding: 3px; gap: 3px; border-radius: 12px; background: rgba(0, 0, 0, 0.35); border: 1px solid var(--line); }
.seg button { font: inherit; font-size: 14px; font-weight: 600; padding: 8px 0; border: 0; border-radius: 9px; background: transparent; color: var(--muted); cursor: pointer; }
.seg button[aria-checked="true"] { background: var(--leaf); color: #0b1a10; }
.seg button:not([aria-checked="true"]):hover { color: var(--text); }

.btn { font: inherit; font-weight: 700; font-size: 16px; padding: 12px 18px; border-radius: 12px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.06); color: var(--text); cursor: pointer; transition: transform 0.08s ease, background 0.15s ease; }
.btn:hover { background: rgba(255, 255, 255, 0.12); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--mango); border-color: var(--mango); color: var(--mango-ink); }
.btn.primary:hover { background: #ffb64d; }

/* ---------- toolbar + d-pad ---------- */
.toolbar { display: flex; gap: 8px; justify-content: flex-end; }
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); color: var(--text); cursor: pointer; }
.icon-btn:hover:not(:disabled) { background: #1f3b2c; }
.icon-btn:disabled { opacity: 0.4; cursor: default; }
.icon-btn svg, .dpad svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn[aria-pressed="true"]#btn-dpad { background: #24432f; border-color: var(--leaf); }
#btn-pause .i-play { display: none; }
#btn-pause[data-state="paused"] .i-play { display: block; fill: currentColor; }
#btn-pause[data-state="paused"] .i-pause { display: none; }
#btn-sound .i-sound-off { display: none; }
#btn-sound[data-state="off"] .i-sound-on { display: none; }
#btn-sound[data-state="off"] .i-sound-off { display: block; color: var(--muted); }

.dpad { align-self: center; display: grid; grid-template-columns: repeat(3, 62px); grid-template-rows: repeat(2, 56px); grid-template-areas: ". up ." "left down right"; gap: 6px; touch-action: manipulation; }
.dpad[hidden] { display: none; }
.dpad button { display: grid; place-items: center; border-radius: 14px; border: 1px solid var(--line); background: var(--panel); color: var(--text); cursor: pointer; }
.dpad button:active { background: var(--leaf); color: #0b1a10; }
.dpad [data-dir="up"] { grid-area: up; }
.dpad [data-dir="left"] { grid-area: left; }
.dpad [data-dir="down"] { grid-area: down; }
.dpad [data-dir="right"] { grid-area: right; }
.dpad svg { width: 26px; height: 26px; stroke-width: 2.4; }

/* ---------- best runs ---------- */
.runs-card { padding: 14px 16px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line); }
.runs-actions { display: flex; align-items: center; gap: 4px; }
.runs-close { background: none; border: 0; color: var(--muted); font-size: 28px; line-height: 1; cursor: pointer; padding: 0 4px; }
.runs-close:hover { color: var(--text); }
.icon-btn.only-narrow { display: none; }

/* On phones the scores live in a sheet that opens on demand, so nothing
   sits below the board to make the page scroll. */
@media (max-width: 899px) {
    .icon-btn.only-narrow { display: grid; }
    .runs { display: none; position: fixed; inset: 0; z-index: 30; padding: 16px; width: auto; min-width: 0; background: rgba(4, 12, 8, 0.7); place-items: center; touch-action: none; }
    .runs.open { display: grid; }
    .runs-card { width: min(100%, 420px); max-height: 100%; overflow: auto; touch-action: pan-y; overscroll-behavior: contain; }
}
.runs-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 6px; }
.runs h2 { margin: 0; font-size: 18px; font-weight: 700; }
.link-btn { font: inherit; font-size: 13px; color: var(--muted); background: none; border: 0; padding: 4px; cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }
.link-btn:hover { color: var(--text); }
.runs ol { list-style: none; margin: 0; padding: 0; }
.runs li { display: grid; grid-template-columns: 22px auto 1fr; align-items: baseline; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.runs li:first-child { border-top: 0; }
.runs li.empty { display: block; color: var(--muted); font-size: 14px; line-height: 1.4; }
.rank { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 14px; }
.run-score { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.run-meta { font-size: 13px; color: var(--muted); text-align: right; }
.runs li:first-child .run-score { color: var(--mango); }
.note { margin: 8px 0 4px; font-size: 12px; color: var(--muted); }
.switch-link { font-size: 13px; color: var(--muted); text-underline-offset: 3px; }
.switch-link:hover { color: var(--text); }

/* ---------- help dialog ---------- */
.help { width: min(480px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); padding: 0; border: 1px solid var(--line); border-radius: 18px; background: var(--panel); color: var(--text); overflow: hidden; }
.help[open] { display: flex; flex-direction: column; }
.help::backdrop { background: rgba(4, 12, 8, 0.7); }
.help-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 8px; }
.help-head h2 { margin: 0; font-size: 24px; font-weight: 800; letter-spacing: -0.02em; font-variation-settings: "opsz" 72; }
.help-close { background: none; border: 0; color: var(--muted); font-size: 28px; line-height: 1; cursor: pointer; padding: 0 4px; }
.help-close:hover { color: var(--text); }
.help-body { padding: 4px 18px 12px; overflow: auto; overscroll-behavior: contain; font-size: 15px; line-height: 1.5; }
.help-body h3 { margin: 14px 0 6px; font-size: 16px; font-weight: 700; }
.help-body h4 { margin: 0 0 4px; font-size: 14px; color: var(--muted); font-weight: 600; }
.help-body ul { margin: 0; padding-left: 18px; color: #cfe4c9; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.help-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 18px 16px; border-top: 1px solid var(--line); }
.help-foot .btn { padding: 9px 18px; font-size: 15px; }

.install-copy { margin: 0 0 8px; color: #cfe4c9; }
.install[hidden], [data-install-tip][hidden], [data-install-section][hidden] { display: none; }
.help-body .install { padding: 8px 16px; font-size: 14px; margin: 6px 0 4px; }

/* ---------- focus + motion ---------- */
:focus-visible { outline: 3px solid var(--mango); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { .card, #score.pop { animation: none; } .btn { transition: none; } }
