/* Mancala (Kalah) — vs the phone or two players on one phone (js/mancala.js).
   Theme-aware like the other game pages: tokens on :root, redefined under body.dark-theme.
   The board is a CSS grid: 8 columns x 2 rows on wide screens, 2 columns x 8 rows on phones.
   Every cell carries its own grid position for both layouts (--hc/--hr and --vc/--vr, set by JS). */
:root {
    --mk-accent: #9a4f16;
    --mk-accent-soft: #fbead6;
    --mk-page-bg: #f7f1e8;
    --mk-card-bg: #ffffff;
    --mk-card-border: #e6dccd;
    --mk-muted: #6b6154;
    --mk-shadow: 0 10px 30px rgba(90, 50, 15, .12);
    --mk-p0: #f59e0b;
    --mk-p1: #0d9488;
    --mk-wood-1: #c98a4b;
    --mk-wood-2: #a8642c;
    --mk-wood-3: #8a4b1c;
    --mk-grain: rgba(70, 30, 5, .10);
    --mk-pit-1: #5e3214;
    --mk-pit-2: #7a4419;
    --mk-pit-rim: rgba(255, 226, 180, .35);
    --mk-target: #22c55e;
    --mk-capture: #ef4444;
    --mk-seed: 15px;
}

body.dark-theme {
    --mk-accent: #f0a65a;
    --mk-accent-soft: #3a2614;
    --mk-page-bg: #111827;
    --mk-card-bg: #1f2937;
    --mk-card-border: #374151;
    --mk-muted: #9ca3af;
    --mk-shadow: 0 10px 30px rgba(0, 0, 0, .4);
    --mk-p0: #fbbf24;
    --mk-p1: #2dd4bf;
    --mk-wood-1: #8a5a30;
    --mk-wood-2: #6b4322;
    --mk-wood-3: #4e2f16;
    --mk-grain: rgba(0, 0, 0, .16);
    --mk-pit-1: #2a160a;
    --mk-pit-2: #3d2210;
    --mk-pit-rim: rgba(255, 210, 150, .22);
    --mk-target: #4ade80;
    --mk-capture: #f87171;
}

/* clip, not hidden: the site-wide `body { overflow-x: hidden }` makes <body> a scroll
   container that never scrolls, which silently kills any position: sticky inside. */
.mk-body, body.mk-body#page-top {
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    background: var(--mk-page-bg);
    color: var(--text-color);
    min-height: 100vh;
    overflow-x: clip;
}
.mk-main { padding-top: 100px; padding-bottom: 40px; }
.mk-game .container, .mk-hero .container { max-width: 820px; }

/* ---- hero ---- */
.mk-hero { text-align: center; padding: 4px 0 10px; }
.mk-hero h1 { font-weight: 800; font-size: clamp(1.7rem, 5vw, 2.3rem); margin-bottom: 4px; }
.mk-lead { margin: 0 auto; max-width: 560px; color: var(--mk-muted); font-size: 1rem; line-height: 1.5; }

/* ---- mode switch + options ---- */
.mk-modes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 5px; margin: 6px auto 10px; max-width: 560px; border-radius: 16px; background: var(--mk-card-bg); border: 1px solid var(--mk-card-border); }
.mk-mode { min-height: 46px; padding: 6px 8px; border: 0; border-radius: 12px; background: transparent; color: inherit; font-weight: 700; font-size: .88rem; line-height: 1.2; cursor: pointer; touch-action: manipulation; display: flex; align-items: center; justify-content: center; gap: 7px; text-align: center; }
.mk-mode[aria-pressed="true"] { background: var(--mk-accent); color: #fff; }
body.dark-theme .mk-mode[aria-pressed="true"] { color: #1f1300; }
.mk-mode:focus-visible, .mk-chip:focus-visible { outline: 2px solid var(--mk-accent); outline-offset: 2px; }

.mk-options { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px 14px; margin: 0 auto 10px; max-width: 560px; }
.mk-chips { display: flex; align-items: center; gap: 5px; }
.mk-chips[hidden] { display: none; }
.mk-options-bottom { margin: 10px auto 0; }
.mk-chips-label { color: var(--mk-muted); font-size: .82rem; font-weight: 700; margin-right: 2px; }
.mk-chip { min-height: 40px; min-width: 44px; padding: 4px 12px; border: 1px solid var(--mk-card-border); border-radius: 999px; background: var(--mk-card-bg); color: inherit; font-weight: 700; font-size: .9rem; cursor: pointer; touch-action: manipulation; }
.mk-chip[aria-pressed="true"] { background: var(--mk-accent-soft); border-color: var(--mk-accent); color: var(--mk-accent); }

/* ---- turn banner ---- */
.mk-turn { display: flex; align-items: center; gap: 14px; padding: 10px 16px; margin: 0 auto 12px; max-width: 560px; min-height: 76px; border-radius: 18px; background: var(--mk-card-bg); border: 1px solid var(--mk-card-border); box-shadow: var(--mk-shadow); }
.mk-turn-text { min-width: 0; }
.mk-turn-title { font-weight: 800; font-size: clamp(1.25rem, 5.5vw, 1.6rem); line-height: 1.15; }
.mk-turn-hint { color: var(--mk-muted); font-size: .95rem; font-weight: 600; margin-top: 2px; line-height: 1.3; }
.mk-turn-dot { width: 34px; height: 34px; flex: none; border-radius: 50%; background: var(--mk-p0); box-shadow: inset 0 -4px 8px rgba(0, 0, 0, .25), 0 2px 5px rgba(0, 0, 0, .25); transition: background-color .25s; }
.mk-turn[data-player="1"] .mk-turn-dot { background: var(--mk-p1); }
.mk-turn.is-good { border-color: var(--mk-target); }
.mk-turn.is-capture { border-color: var(--mk-capture); }
.mk-turn.is-over { border-color: var(--mk-accent); }

/* ---- player tags around the board ---- */
.mk-table { max-width: 780px; margin: 0 auto 12px; }
.mk-tag { display: flex; align-items: center; gap: 8px; padding: 6px 12px; margin: 0 auto; width: fit-content; max-width: 100%; border-radius: 999px; background: var(--mk-card-bg); border: 2px solid var(--mk-card-border); font-weight: 700; transition: border-color .2s, box-shadow .2s; }
.mk-tag-far { margin-bottom: 8px; }
.mk-tag-near { margin-top: 8px; }
.mk-tag-dot { width: 14px; height: 14px; border-radius: 50%; flex: none; background: var(--mk-tag-color, var(--mk-p0)); }
.mk-tag-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mk-tag-store { color: var(--mk-muted); white-space: nowrap; }
.mk-tag-store b { color: var(--text-color); font-size: 1.05rem; }
.mk-tag.is-active { border-color: var(--mk-tag-color, var(--mk-p0)); box-shadow: 0 0 0 3px color-mix(in srgb, var(--mk-tag-color, var(--mk-p0)) 25%, transparent); }

/* ---- the board ---- */
.mk-board-wrap { position: relative; }
.mk-board {
    position: relative;
    display: grid;
    grid-template-columns: 1.3fr repeat(6, 1fr) 1.3fr;
    grid-template-rows: repeat(2, 100px);
    gap: 18px 10px;
    padding: 22px 18px;
    border-radius: 34px;
    background:
        repeating-linear-gradient(92deg, transparent 0 7px, var(--mk-grain) 7px 9px, transparent 9px 23px, var(--mk-grain) 23px 24px, transparent 24px 41px),
        radial-gradient(ellipse at 30% 20%, rgba(255, 240, 210, .25), transparent 60%),
        linear-gradient(180deg, var(--mk-wood-1), var(--mk-wood-2) 60%, var(--mk-wood-3));
    box-shadow: 0 14px 30px rgba(60, 30, 5, .35), inset 0 2px 0 rgba(255, 235, 200, .35), inset 0 -4px 0 rgba(0, 0, 0, .18);
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
.mk-board.is-turning { animation: mk-turn .65s cubic-bezier(.4, .1, .2, 1); }
@keyframes mk-turn { from { transform: rotate(180deg); } to { transform: rotate(0deg); } }

.mk-cell { position: relative; grid-column: var(--hc); grid-row: var(--hr); min-width: 0; }
.mk-hole {
    position: absolute; inset: 0;
    border-radius: 50%;
    background: radial-gradient(ellipse at 50% 40%, var(--mk-pit-1) 0%, var(--mk-pit-1) 45%, var(--mk-pit-2) 100%);
    box-shadow: inset 0 8px 14px rgba(0, 0, 0, .55), inset 0 -2px 3px var(--mk-pit-rim), 0 1px 0 rgba(255, 235, 200, .35);
}
.mk-store .mk-hole { border-radius: 999px; }
.mk-seeds { position: absolute; inset: 0; pointer-events: none; }

button.mk-pit { display: block; width: 100%; height: 100%; padding: 0; margin: 0; border: 0; background: transparent; color: inherit; font: inherit; cursor: default; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button.mk-pit:focus { outline: none; }
button.mk-pit:focus-visible .mk-hole { outline: 3px solid var(--mk-accent); outline-offset: 3px; }
.mk-pit.is-playable { cursor: pointer; }
.mk-pit.is-playable .mk-hole { box-shadow: inset 0 8px 14px rgba(0, 0, 0, .55), 0 0 0 3px color-mix(in srgb, var(--mk-side) 70%, transparent), 0 0 14px color-mix(in srgb, var(--mk-side) 45%, transparent); }
.mk-pit.is-playable:hover .mk-hole { transform: scale(1.03); }
.mk-pit .mk-hole { transition: transform .15s, box-shadow .2s; }
.mk-cell.is-from .mk-hole { outline: 2px dashed color-mix(in srgb, var(--mk-side) 85%, #fff); outline-offset: 3px; }
.mk-cell.is-to .mk-hole { box-shadow: inset 0 8px 14px rgba(0, 0, 0, .55), 0 0 0 3px rgba(255, 255, 255, .55); }
.mk-cell.is-preview .mk-hole { box-shadow: inset 0 8px 14px rgba(0, 0, 0, .55), 0 0 0 4px var(--mk-target), 0 0 18px var(--mk-target); }
.mk-cell.is-preview-cap .mk-hole { box-shadow: inset 0 8px 14px rgba(0, 0, 0, .55), 0 0 0 4px var(--mk-capture), 0 0 18px var(--mk-capture); }
.mk-cell.is-flash .mk-hole { animation: mk-flash .7s ease-out; }
@keyframes mk-flash { 0% { box-shadow: inset 0 8px 14px rgba(0, 0, 0, .55), 0 0 0 0 var(--mk-capture); } 40% { box-shadow: inset 0 8px 14px rgba(0, 0, 0, .55), 0 0 0 6px var(--mk-capture), 0 0 22px var(--mk-capture); } 100% { box-shadow: inset 0 8px 14px rgba(0, 0, 0, .55), 0 0 0 0 transparent; } }
.mk-cell.is-lift .mk-hole { transform: scale(1.06); }

/* seed counts sit on the rim, on the outer side of each cell */
.mk-count {
    position: absolute; z-index: 2;
    min-width: 26px; height: 22px; padding: 0 6px;
    border-radius: 999px;
    background: rgba(30, 14, 4, .78);
    color: #fff3df;
    font-size: .8rem; font-weight: 800; line-height: 22px; text-align: center;
    pointer-events: none;
    font-variant-numeric: tabular-nums;
}
.mk-cell[data-side="near"] .mk-count { bottom: -14px; left: 50%; transform: translateX(-50%); }
.mk-cell[data-side="far"] .mk-count { top: -14px; left: 50%; transform: translateX(-50%); }
.mk-store .mk-count { min-width: 34px; height: 26px; line-height: 26px; font-size: .95rem; }
.mk-store[data-side] .mk-count { top: auto; bottom: -14px; left: 50%; transform: translateX(-50%); }
.mk-count.is-bump { animation: mk-bump .25s ease-out; }
@keyframes mk-bump { 50% { transform: translateX(-50%) scale(1.25); } }
.mk-count.is-zero { opacity: .55; }

/* side colour stripe under each row */
.mk-cell { --mk-side: var(--mk-p0); }
.mk-cell[data-owner="1"] { --mk-side: var(--mk-p1); }
.mk-store .mk-hole::after { content: ""; position: absolute; inset: 6px; border-radius: inherit; border: 2px solid color-mix(in srgb, var(--mk-side) 55%, transparent); pointer-events: none; }

/* seeds: small glassy pebbles */
.mk-seed {
    position: absolute;
    width: var(--mk-seed);
    height: calc(var(--mk-seed) * .86);
    margin: calc(var(--mk-seed) * -.43) 0 0 calc(var(--mk-seed) * -.5);
    border-radius: 50%;
    background: radial-gradient(circle at 34% 30%, rgba(255, 255, 255, .9) 0 9%, var(--c1) 36%, var(--c2) 100%);
    box-shadow: 0 2px 2px rgba(0, 0, 0, .5), inset 0 -1px 2px rgba(0, 0, 0, .3);
    transform: rotate(var(--r, 0deg));
}
.mk-seed.is-drop { animation: mk-drop .3s ease-out; }
@keyframes mk-drop {
    from { transform: translateY(-16px) scale(1.3) rotate(var(--r, 0deg)); opacity: .3; }
    65% { transform: translateY(1px) scale(.95) rotate(var(--r, 0deg)); opacity: 1; }
    to { transform: rotate(var(--r, 0deg)); }
}

/* the hand carrying the seeds being sown */
.mk-hand {
    position: absolute; left: 0; top: 0; z-index: 6;
    width: 44px; height: 44px; margin: -30px 0 0 -22px;
    border-radius: 50%;
    background: radial-gradient(circle at 40% 35%, rgba(255, 248, 235, .96), rgba(244, 222, 190, .94));
    box-shadow: 0 8px 18px rgba(0, 0, 0, .4);
    pointer-events: none;
    transition: transform var(--mk-step, .2s) cubic-bezier(.3, .1, .3, 1);
}
.mk-hand[hidden] { display: none; }
.mk-hand-seeds { position: absolute; inset: 9px; }
.mk-hand-seeds .mk-seed { --mk-seed: 12px; }
.mk-hand-count { position: absolute; right: -8px; top: -8px; min-width: 22px; height: 22px; padding: 0 5px; border-radius: 999px; background: var(--mk-accent); color: #fff; font-size: .78rem; font-weight: 800; line-height: 22px; text-align: center; }
body.dark-theme .mk-hand-count { color: #1f1300; }

/* ---- actions ---- */
.mk-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; max-width: 560px; margin: 18px auto 0; }
.mk-btn { min-height: 48px; padding: 8px 12px; border: 1px solid var(--mk-card-border); border-radius: 14px; background: var(--mk-card-bg); color: inherit; font-weight: 700; font-size: .95rem; cursor: pointer; touch-action: manipulation; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.mk-btn:hover:not(:disabled) { border-color: var(--mk-accent); }
.mk-btn:disabled { opacity: .45; cursor: not-allowed; }
.mk-btn:focus-visible { outline: 2px solid var(--mk-accent); outline-offset: 2px; }
.mk-btn-primary { background: var(--mk-accent); border-color: var(--mk-accent); color: #fff; }
body.dark-theme .mk-btn-primary { color: #1f1300; }

.mk-record { max-width: 560px; margin: 12px auto 0; min-height: 1.5em; text-align: center; color: var(--mk-muted); font-size: .9rem; font-weight: 600; }
.mk-record b { color: var(--text-color); }

.mk-toggles { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 4px 14px; max-width: 560px; margin: 6px auto 0; }
.mk-link { border: 0; background: none; min-height: 40px; padding: 6px 8px; color: var(--mk-muted); font-size: .88rem; font-weight: 600; cursor: pointer; touch-action: manipulation; }
.mk-link:hover { color: var(--mk-accent); }
#mk-reset-score { text-decoration: underline; font-weight: 500; }
.mk-check { display: inline-flex; align-items: center; gap: 8px; min-height: 40px; color: var(--mk-muted); font-size: .88rem; font-weight: 600; cursor: pointer; margin: 0; }
.mk-check[hidden] { display: none; }
.mk-check input { width: 18px; height: 18px; accent-color: var(--mk-accent); }

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

/* ---- end overlay ---- */
.mk-overlay { position: fixed; inset: 0; z-index: 2000; display: grid; place-items: center; padding: 16px; background: rgba(0, 0, 0, .45); animation: mk-fade .25s ease-out; }
.mk-overlay[hidden] { display: none; }
@keyframes mk-fade { from { opacity: 0; } }
.mk-win { width: 100%; max-width: 340px; padding: 22px 18px 18px; border-radius: 22px; background: var(--mk-card-bg); color: var(--text-color); text-align: center; box-shadow: 0 20px 50px rgba(0, 0, 0, .35); animation: mk-rise .3s ease-out; }
@keyframes mk-rise { from { transform: translateY(20px) scale(.96); opacity: 0; } }
.mk-win-icon { font-size: 34px; color: var(--mk-p0); }
.mk-win.is-loss .mk-win-icon { color: var(--mk-muted); }
.mk-win h2 { font-weight: 800; font-size: 1.5rem; margin: 6px 0 6px; }
.mk-win-score { font-weight: 800; font-size: 1.2rem; margin: 0 0 4px; font-variant-numeric: tabular-nums; }
.mk-win-reason { color: var(--mk-muted); margin: 0 0 14px; }
.mk-win-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* ---- phones: the board stands up, 2 columns x 8 rows ---- */
@media (max-width: 599.98px) {
    .mk-board {
        grid-template-columns: 1fr 1fr;
        grid-template-rows: 52px repeat(6, minmax(48px, 1fr)) 52px;
        gap: 10px 30px;
        padding: 14px 30px;
        border-radius: 30px;
        background:
            repeating-linear-gradient(2deg, transparent 0 7px, var(--mk-grain) 7px 9px, transparent 9px 23px, var(--mk-grain) 23px 24px, transparent 24px 41px),
            radial-gradient(ellipse at 30% 15%, rgba(255, 240, 210, .25), transparent 60%),
            linear-gradient(90deg, var(--mk-wood-1), var(--mk-wood-2) 60%, var(--mk-wood-3));
    }
    .mk-cell { grid-column: var(--vc); grid-row: var(--vr); }
    .mk-cell[data-side="near"] .mk-count { top: 50%; bottom: auto; left: -15px; transform: translate(-50%, -50%); }
    .mk-cell[data-side="far"] .mk-count { top: 50%; bottom: auto; left: auto; right: -15px; transform: translate(50%, -50%); }
    .mk-hero { padding-bottom: 4px; }
    .mk-lead { display: none; }
    .mk-main { padding-top: 76px; }
    .mk-hero h1 { font-size: 1.6rem; margin-bottom: 0; }
    .mk-modes { margin: 4px auto 8px; }
    .mk-mode { min-height: 44px; }
    .mk-options { margin-bottom: 8px; }
    .mk-turn { min-height: 64px; padding: 8px 14px; margin-bottom: 8px; }
    .mk-tag { padding: 4px 12px; }
    .mk-tag-far { margin-bottom: 6px; }
    .mk-tag-near { margin-top: 6px; }
    .mk-count.is-bump { animation: none; }
    .mk-store[data-side] .mk-count { top: 50%; bottom: auto; left: auto; right: 14px; transform: translateY(-50%); }
}

@media (max-width: 400px) {
    .mk-mode { font-size: .8rem; gap: 5px; }
    .mk-chip { padding: 4px 10px; min-width: 42px; }
    .mk-turn { padding: 10px 12px; gap: 10px; }
    .mk-turn-dot { width: 28px; height: 28px; }
    .mk-btn { font-size: .88rem; padding: 8px; }
    .mk-board { gap: 9px 30px; padding: 12px 30px; }
}

@media (prefers-reduced-motion: reduce) {
    .mk-board.is-turning, .mk-seed.is-drop, .mk-count.is-bump, .mk-cell.is-flash .mk-hole { animation: none !important; }
    .mk-hand { transition: none; }
}
