/* Sokoban (js/sokoban.js). Tokens on :root, redefined under body.dark-theme. */
:root {
    --so-accent: #b45309;
    --so-accent-2: #92400e;
    --so-accent-soft: #fdf0dc;
    --so-page-bg: #f7f3ec;
    --so-card-bg: #ffffff;
    --so-card-border: #e6dccb;
    --so-muted: #6b6052;
    --so-shadow: 0 10px 30px rgba(80, 50, 10, .10);
    --so-frame: #d9cdb8;
    --so-wall: #6b7280;
    --so-wall-hi: #9ca3af;
    --so-wall-lo: #4b5563;
    --so-floor: #ece4d4;
    --so-floor-2: #e4dac6;
    --so-goal: #dc2626;
    --so-box: #c27c2c;
    --so-box-2: #9a5b19;
    --so-box-line: #7a4510;
    --so-box-on: #16a34a;
    --so-box-on-2: #0f7a37;
    --so-dead: #dc2626;
    --so-gold: #f59e0b;
    --so-good: #15803d;
    --so-bad: #dc2626;
    --so-warn: #b45309;
    --so-player: #2563eb;
    --so-skin: #f5c9a0;
}

body.dark-theme {
    --so-accent: #f59e0b;
    --so-accent-2: #fbbf24;
    --so-accent-soft: #2b2010;
    --so-page-bg: #12100c;
    --so-card-bg: #1c1914;
    --so-card-border: #352e22;
    --so-muted: #b5a993;
    --so-shadow: 0 10px 30px rgba(0, 0, 0, .45);
    --so-frame: #2a251c;
    --so-wall: #4b5160;
    --so-wall-hi: #6b7383;
    --so-wall-lo: #30343e;
    --so-floor: #2b2721;
    --so-floor-2: #26221d;
    --so-goal: #f87171;
    --so-box: #b7742a;
    --so-box-2: #8b5418;
    --so-box-line: #5e3509;
    --so-box-on: #22a556;
    --so-box-on-2: #137a3c;
    --so-good: #4ade80;
    --so-bad: #f87171;
    --so-warn: #fbbf24;
    --so-player: #60a5fa;
}

/* clip, not hidden: the site-wide `body { overflow-x: hidden }` would kill position: sticky. */
.so-body, body.so-body#page-top {
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    background: var(--so-page-bg);
    color: var(--text-color);
    min-height: 100vh;
    overflow-x: clip;
}
.so-main { padding-top: 92px; padding-bottom: 40px; }
.so-game .container, .so-hero .container { max-width: 980px; }
[hidden] { display: none !important; }

.so-hero { text-align: center; padding: 4px 0 8px; }
.so-hero h1 { font-weight: 800; font-size: clamp(1.6rem, 5vw, 2.2rem); margin-bottom: 4px; }
.so-hero-icon { color: var(--so-accent); }
.so-lead { margin: 0 auto; max-width: 560px; color: var(--so-muted); font-size: .98rem; line-height: 1.45; }
.so-loading { text-align: center; color: var(--so-muted); font-weight: 700; padding: 40px 0; }

/* ---- level select ---- */
.so-resume { display: flex; justify-content: center; margin: 8px 0 10px; }
.so-chapters { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; padding: 5px; border-radius: 16px; background: var(--so-card-bg); border: 1px solid var(--so-card-border); box-shadow: var(--so-shadow); }
.so-chap { min-height: 56px; padding: 4px; border: 0; border-radius: 12px; background: transparent; color: inherit; font-weight: 800; font-size: .92rem; line-height: 1.15; cursor: pointer; touch-action: manipulation; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.so-chap small { font-weight: 700; font-size: .72rem; opacity: .75; white-space: nowrap; }
.so-chap[aria-checked="true"] { background: var(--so-accent); color: #fff; }
body.dark-theme .so-chap[aria-checked="true"] { color: #1d1203; }
.so-levels-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin: 16px 2px 8px; }
.so-h2 { font-size: 1.1rem; font-weight: 800; margin: 0; }
.so-chapter-done { color: var(--so-muted); font-weight: 700; font-size: .9rem; }
.so-levels { display: grid; grid-template-columns: repeat(10, 1fr); gap: 6px; }
.so-lvl { position: relative; aspect-ratio: 1 / 1; min-height: 44px; border-radius: 12px; border: 1px solid var(--so-card-border); background: var(--so-card-bg); color: inherit; font-weight: 900; font-size: 1rem; cursor: pointer; touch-action: manipulation; display: grid; place-items: center; padding: 0; }
.so-lvl:hover { border-color: var(--so-accent); }
.so-lvl.is-done { background: var(--so-accent-soft); border-color: var(--so-accent); }
.so-lvl.is-next { box-shadow: 0 0 0 3px var(--so-gold); }
.so-lvl .so-badge { position: absolute; top: 3px; right: 4px; font-size: .62rem; line-height: 1; }
.so-lvl .so-badge.is-done { color: var(--so-good); }
.so-lvl .so-badge.is-opt { color: var(--so-gold); }
.so-legend { color: var(--so-muted); font-size: .82rem; margin: 10px 2px 0; }
.so-mini { display: inline-grid; place-items: center; width: 18px; height: 18px; border-radius: 5px; font-size: .62rem; background: var(--so-card-bg); border: 1px solid var(--so-card-border); vertical-align: -3px; }
.so-mini.is-done { color: var(--so-good); }
.so-mini.is-opt { color: var(--so-gold); }

/* ---- play ---- */
.so-top { display: grid; grid-template-columns: auto auto 1fr auto auto; align-items: center; gap: 6px; max-width: 620px; margin: 6px auto 8px; }
.so-title { text-align: center; line-height: 1.15; min-width: 0; }
.so-title b { display: block; font-size: 1.12rem; font-weight: 900; }
.so-title small { display: block; color: var(--so-muted); font-weight: 700; font-size: .8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.so-round { width: 46px; height: 46px; border-radius: 14px; border: 1px solid var(--so-card-border); background: var(--so-card-bg); color: inherit; font-size: 1.05rem; display: grid; place-items: center; cursor: pointer; touch-action: manipulation; }
.so-round:hover:not(:disabled) { border-color: var(--so-accent); }
.so-round:disabled { opacity: .35; cursor: not-allowed; }

.so-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; max-width: 620px; margin: 0 auto 8px; }
.so-stat { display: flex; flex-direction: column; align-items: center; padding: 4px; border-radius: 12px; background: var(--so-card-bg); border: 1px solid var(--so-card-border); font-variant-numeric: tabular-nums; line-height: 1.2; }
.so-stat small { color: var(--so-muted); font-weight: 700; font-size: .7rem; text-transform: uppercase; letter-spacing: .03em; }
.so-stat b { font-size: 1.15rem; font-weight: 900; }
.so-stat em { font-style: normal; color: var(--so-muted); font-size: .7rem; font-weight: 700; }

.so-stage { display: flex; justify-content: center; align-items: center; min-height: 160px; }
.so-board { position: relative; flex: none; border-radius: 10px; background: transparent; filter: drop-shadow(0 6px 14px rgba(60, 40, 10, .18)); touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; outline: none; }
.so-board:focus-visible { outline: 3px solid var(--so-gold); outline-offset: 4px; }
.so-t { position: absolute; width: var(--so-cell); height: var(--so-cell); }
/* stone bricks: mortar across the middle, joints offset between the two rows */
.so-wall { background-color: var(--so-wall); background-image:
    linear-gradient(rgba(0, 0, 0, .22), rgba(0, 0, 0, .22)),
    linear-gradient(rgba(0, 0, 0, .22), rgba(0, 0, 0, .22)),
    linear-gradient(rgba(0, 0, 0, .22), rgba(0, 0, 0, .22)),
    linear-gradient(160deg, var(--so-wall-hi), var(--so-wall) 55%, var(--so-wall-lo));
    background-size: 100% 2px, 2px 50%, 2px 50%, 100% 100%;
    background-position: 0 50%, 50% 0, 0 100%, 0 0;
    background-repeat: no-repeat;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .14); }
.so-floor { background: var(--so-floor); }
.so-floor.is-alt { background: var(--so-floor-2); }
.so-goal::after { content: ""; position: absolute; inset: 30%; border-radius: 50%; border: calc(var(--so-cell) * .07) solid var(--so-goal); box-shadow: 0 0 calc(var(--so-cell) * .18) var(--so-goal); opacity: .85; }
.so-mark { position: absolute; width: var(--so-cell); height: var(--so-cell); pointer-events: none; display: grid; place-items: center; }
.so-mark::after { content: ""; width: 28%; height: 28%; border-radius: 50%; background: var(--so-gold); box-shadow: 0 0 8px var(--so-gold); animation: so-blink 1s ease-in-out infinite; }
@keyframes so-blink { 50% { opacity: .45; transform: scale(.8); } }
.so-sprite { position: absolute; left: 0; top: 0; width: var(--so-cell); height: var(--so-cell); transition: transform var(--so-speed, 90ms) linear; will-change: transform; pointer-events: none; }
.so-box::before { content: ""; position: absolute; inset: 7%; border-radius: 14%; background:
    linear-gradient(45deg, transparent 44%, var(--so-box-line) 44% 56%, transparent 56%),
    linear-gradient(-45deg, transparent 44%, var(--so-box-line) 44% 56%, transparent 56%),
    linear-gradient(160deg, var(--so-box), var(--so-box-2));
    box-shadow: inset 0 0 0 calc(var(--so-cell) * .07) var(--so-box-line), 0 2px 4px rgba(0, 0, 0, .3); transition: filter .2s, background .2s; }
.so-box.is-on::before { background:
    linear-gradient(45deg, transparent 44%, rgba(0, 0, 0, .25) 44% 56%, transparent 56%),
    linear-gradient(-45deg, transparent 44%, rgba(0, 0, 0, .25) 44% 56%, transparent 56%),
    linear-gradient(160deg, var(--so-box-on), var(--so-box-on-2));
    box-shadow: inset 0 0 0 calc(var(--so-cell) * .07) rgba(0, 0, 0, .25), 0 0 calc(var(--so-cell) * .3) rgba(34, 197, 94, .6); }
.so-box.is-dead::before { background:
    linear-gradient(45deg, transparent 44%, rgba(0, 0, 0, .3) 44% 56%, transparent 56%),
    linear-gradient(-45deg, transparent 44%, rgba(0, 0, 0, .3) 44% 56%, transparent 56%),
    linear-gradient(160deg, #ef4444, #991b1b); animation: so-shake .4s ease-out; }
.so-box.is-sel::after { content: ""; position: absolute; inset: 0; border-radius: 18%; box-shadow: 0 0 0 calc(var(--so-cell) * .08) var(--so-gold); animation: so-pulse 1s ease-in-out infinite; }
@keyframes so-pulse { 50% { opacity: .4; } }
@keyframes so-shake { 25% { translate: -3px 0; } 50% { translate: 3px 0; } 75% { translate: -2px 0; } }
.so-player svg { width: 100%; height: 100%; display: block; transition: transform .12s; }
.so-player.is-left svg { transform: scaleX(-1); }
.so-board.is-won .so-box::before { animation: so-win .7s ease-out both; }
@keyframes so-win { 40% { transform: scale(1.15); } }

.so-status { margin: 8px 2px 0; min-height: 1.5em; font-weight: 700; text-align: center; color: var(--so-muted); line-height: 1.4; }
.so-status.is-good { color: var(--so-good); }
.so-status.is-bad { color: var(--so-bad); }
.so-status.is-warn { color: var(--so-warn); }

.so-controls { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 10px 16px; max-width: 620px; margin: 8px auto 0; }
.so-pad { display: grid; grid-template-columns: repeat(3, 54px); grid-template-rows: repeat(2, 50px); gap: 5px; }
.so-arrow { border-radius: 14px; border: 1px solid var(--so-card-border); background: var(--so-card-bg); color: inherit; font-size: 1.6rem; display: grid; place-items: center; cursor: pointer; touch-action: manipulation; box-shadow: 0 2px 0 var(--so-card-border); }
.so-arrow:active { transform: translateY(2px); box-shadow: none; }
.so-arrow[data-dir="0"] { grid-column: 2; grid-row: 1; }
.so-arrow[data-dir="3"] { grid-column: 1; grid-row: 2; }
.so-arrow[data-dir="2"] { grid-column: 2; grid-row: 2; }
.so-arrow[data-dir="1"] { grid-column: 3; grid-row: 2; }
.so-tools { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; flex: 1 1 300px; max-width: 400px; }
.so-tool { min-height: 56px; border-radius: 14px; border: 1px solid var(--so-card-border); background: var(--so-card-bg); color: inherit; font-weight: 800; font-size: .72rem; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; cursor: pointer; touch-action: manipulation; padding: 2px; }
.so-tool svg, .so-tool i { font-size: 1.1rem; }
.so-tool:hover:not(:disabled), .so-arrow:hover { border-color: var(--so-accent); }
.so-tool:disabled { opacity: .4; cursor: not-allowed; }
.so-record { text-align: center; color: var(--so-muted); font-size: .88rem; margin: 10px 0 0; min-height: 1.3em; }
.so-record b { color: var(--text-color); }

.so-btn { min-height: 48px; padding: 8px 16px; border: 1px solid var(--so-card-border); border-radius: 14px; background: var(--so-card-bg); color: inherit; font-weight: 800; font-size: .95rem; cursor: pointer; touch-action: manipulation; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.so-btn:hover { border-color: var(--so-accent); }
.so-btn-primary { background: var(--so-accent); border-color: var(--so-accent); color: #fff; }
body.dark-theme .so-btn-primary { color: #1d1203; }
.so-chap:focus-visible, .so-lvl:focus-visible, .so-btn:focus-visible, .so-round:focus-visible, .so-tool:focus-visible, .so-arrow:focus-visible { outline: 3px solid var(--so-gold); outline-offset: 2px; }

.so-help { max-width: 760px; margin: 18px auto 0; padding: 12px 16px; border-radius: 14px; background: var(--so-card-bg); border: 1px solid var(--so-card-border); }
.so-help summary { cursor: pointer; font-weight: 700; }
.so-help ul { margin: 10px 0 0; padding-left: 20px; color: var(--so-muted); line-height: 1.55; }
.so-help li { margin-bottom: 4px; }
.so-help strong { color: var(--text-color); }
.so-help p { color: var(--so-muted); margin: 10px 0 0; line-height: 1.55; font-size: .93rem; }
.so-help p.so-credit { font-size: .8rem; }
.so-help a { color: var(--so-accent); }
.so-powered { text-align: center; color: var(--so-muted); font-size: .85rem; margin: 26px 0 0; }
.so-powered a { color: var(--so-accent); }

/* ---- dialog ---- */
.so-overlay { position: fixed; inset: 0; z-index: 2000; display: grid; place-items: center; padding: 16px; background: rgba(0, 0, 0, .5); animation: so-fade .2s ease-out; }
@keyframes so-fade { from { opacity: 0; } }
.so-dialog { width: 100%; max-width: 400px; padding: 20px 16px 16px; border-radius: 22px; background: var(--so-card-bg); color: var(--text-color); text-align: center; box-shadow: 0 20px 50px rgba(0, 0, 0, .35); animation: so-rise .25s ease-out; }
@keyframes so-rise { from { transform: translateY(16px) scale(.97); opacity: 0; } }
.so-dialog h2 { font-weight: 900; font-size: 1.45rem; margin: 4px 0 6px; }
.so-dialog-icon { font-size: 36px; color: var(--so-gold); }
.so-dialog-sub { color: var(--so-muted); margin: 0 0 6px; line-height: 1.45; }
.so-dialog-sub b { color: var(--text-color); }
.so-dialog-actions { display: grid; grid-template-columns: 1fr 1.3fr; gap: 8px; margin-top: 14px; }

@media (max-width: 760px) {
    .so-levels { grid-template-columns: repeat(6, 1fr); }
}
/* while playing, the board matters more than the intro line */
.so-main.is-playing .so-lead { display: none; }
.so-main.is-playing .so-hero { padding-bottom: 0; }
@media (max-width: 480px) {
    .so-chap { font-size: .8rem; }
    .so-chap small { font-size: .66rem; }
    .so-levels { grid-template-columns: repeat(5, 1fr); }
    .so-top { gap: 4px; }
    /* d-pad and tools side by side, so the board, the arrows and the tools fit on one screen */
    .so-controls { flex-wrap: nowrap; gap: 8px; align-items: stretch; }
    .so-pad { grid-template-columns: repeat(3, 50px); grid-template-rows: repeat(2, 50px); flex: none; }
    .so-tools { grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 50px); flex: 1 1 auto; min-width: 0; }
    .so-tool { min-height: 0; font-size: .66rem; gap: 2px; border-radius: 12px; }
    .so-tool svg, .so-tool i { font-size: 1rem; }
}

@media (prefers-reduced-motion: reduce) {
    .so-sprite, .so-box::before, .so-mark::after, .so-dialog, .so-overlay { transition: none !important; animation: none !important; }
}
