/* Snake Game — styles.
   Palette follows the original: gray-700 page, blue-300 frame, gray-50 grid,
   gray snake, blue food. No frameworks. */
:root {
    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-400: #9ca3af;
    --gray-500: #6b7280;
    --gray-600: #4b5563;
    --gray-700: #374151;
    --gray-900: #111827;
    --blue-300: #93c5fd;
    --blue-500: #3b82f6;
    --blue-600: #2563eb;
    --primary: #0d6efd;
    --frame-w: 480px;
    --frame-h: 550px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    position: fixed;
    inset: 0;
    width: 100vw;
    overflow: hidden;
    background: var(--gray-700);
    font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    color: var(--gray-900);
    -webkit-tap-highlight-color: transparent;
}

button { font: inherit; }
svg { fill: currentColor; }

/* ---------- layout ---------- */
.layout {
    display: flex;
    flex-direction: row;
    gap: 8px;
    max-width: 80rem;
    height: 100%;
    width: 100%;
    margin: 0 auto;
}
.column {
    max-width: 80rem;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

@media (min-width: 320px) { :root { --frame-w: 300px; } }
@media (min-width: 360px) { :root { --frame-w: 360px; } }
@media (min-width: 640px) { :root { --frame-w: 480px; } }
:root { --frame-h: min(500px, calc(100dvh - 24px)); }
@media (min-width: 640px) { :root { --frame-h: min(550px, calc(100dvh - 24px)); } }

/* ---------- frame ---------- */
.frame {
    position: relative;
    width: var(--frame-w);
    height: var(--frame-h);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    padding: 4px 4px 0;
    overflow: hidden;
    cursor: pointer;
    background: var(--blue-300);
    border-radius: 6px;
    box-shadow: 0 4px 6px -1px rgb(0 0 0 / .1), 0 2px 4px -2px rgb(0 0 0 / .1);
}
.column:has(.controls:not([hidden])) .frame { border-bottom-left-radius: 0; border-bottom-right-radius: 0; }

.grid-area {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    background: var(--gray-50);
    overflow: hidden;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.hint {
    position: absolute;
    left: 50%;
    top: 22%;
    transform: translate(-50%, -50%);
    max-width: 88%;
    padding: 8px 14px;
    border-radius: 6px;
    background: rgb(255 255 255 / .94);
    box-shadow: 0 1px 3px rgb(0 0 0 / .2);
    color: var(--gray-600);
    font-size: 14px;
    line-height: 1.35;
    text-align: center;
    pointer-events: none;
}
.hint[hidden] { display: none; }
.hint strong { color: var(--gray-900); }

/* ---------- bottom bar ---------- */
.bar {
    flex: 0 0 auto;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 4px;
    background: var(--blue-300);
}
.btn-sm {
    height: 24px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 0;
    border-radius: 4px;
    cursor: pointer;
}
.btn-sm svg { width: 16px; height: 16px; }
.btn-light { background: var(--gray-100); color: #000; }
.btn-light:hover { background: var(--gray-200); }
.btn-light[aria-pressed="true"] { background: var(--gray-300); }
.btn-blue { background: var(--blue-500); color: #fff; }
.btn-blue:hover { background: var(--blue-600); }
.w24 { width: 24px; }
.w32 { width: 32px; }
.w40 { width: 40px; }

.score {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
    font-size: 20px;
    line-height: 28px;
    font-weight: 700;
    color: var(--gray-600);
    background: #fff;
    padding: 0 8px;
    font-variant-numeric: tabular-nums;
}

#score { display: inline-block; transform-origin: left center; }
#score.pop { animation: score-pop .2s ease-out; }
@keyframes score-pop { 0% { transform: scale(1.35); color: var(--blue-600); } 100% { transform: scale(1); } }
.lv {
    margin-left: 8px;
    padding-left: 8px;
    border-left: 2px solid var(--gray-200);
    font-size: 13px;
    font-weight: 600;
    color: var(--gray-400);
}
@media (max-width: 359px) { .lv { display: none; } }

.frame.shake { animation: shake .38s cubic-bezier(.36,.07,.19,.97); }
@keyframes shake {
    10%, 90% { transform: translateX(-1px); }
    20%, 80% { transform: translateX(2px); }
    30%, 50%, 70% { transform: translateX(-4px); }
    40%, 60% { transform: translateX(4px); }
}

.flash {
    position: absolute;
    left: 50%;
    top: 12%;
    transform: translateX(-50%);
    padding: 4px 12px;
    border-radius: 999px;
    background: var(--blue-600);
    color: #fff;
    font-size: 13px;
    font-weight: 700;
    white-space: nowrap;
    pointer-events: none;
    box-shadow: 0 1px 3px rgb(0 0 0 / .25);
}
.flash[hidden] { display: none; }
.flash.show { animation: flash-in 1.3s ease-out forwards; }
@keyframes flash-in {
    0% { opacity: 0; transform: translate(-50%, 6px); }
    12% { opacity: 1; transform: translate(-50%, 0); }
    75% { opacity: 1; }
    100% { opacity: 0; transform: translate(-50%, -4px); }
}

/* ---------- on-screen controls ---------- */
.controls {
    width: var(--frame-w);
    display: flex;
}
.controls[hidden] { display: none; }
.pad {
    margin-left: auto;
    width: 13rem;
    height: 5rem;
    padding: 6px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    gap: 4px;
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: 0 0 6px 6px;
}
.pad-row { display: flex; gap: 8px; justify-content: space-between; }
.pad button {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
    color: var(--gray-900);
    border: 1px solid var(--gray-200);
    border-radius: 4px;
    cursor: pointer;
    touch-action: manipulation;
}
.pad button:hover { background: var(--gray-100); color: #1d4ed8; }
.pad button:active { background: var(--gray-200); }
.pad svg { width: 16px; height: 16px; }

/* ---------- side panel ---------- */
.interactions {
    flex: 0 0 auto;
    align-self: stretch;
    width: 24rem;
    max-width: 40vw;
    margin: 0 0 0 auto;
    padding-bottom: 14px;
    background: var(--gray-50);
    border-radius: 6px;
    overflow: auto;
}
.interactions[hidden] { display: none; }
.tabs { display: flex; list-style: none; margin: 0 0 -1px; padding: 0; background: var(--gray-50); }
.tabs li { width: 49%; display: flex; align-items: flex-end; justify-content: center; }
.tab {
    width: 100%;
    height: 56px;
    padding: 16px;
    font-weight: 700;
    font-size: 16px;
    color: var(--gray-500);
    background: none;
    border: 0;
    border-bottom: 2px solid transparent;
    cursor: pointer;
}
.tab:hover { color: var(--gray-600); border-bottom-color: var(--gray-300); }
.tab.active { color: #2563eb; border-bottom-color: #2563eb; }
.tab-pane { padding: 20px 24px; }
.tab-pane[hidden] { display: none; }

.runs { list-style: none; margin: 0 0 12px; padding: 0; }
.runs li { display: grid; grid-template-columns: 24px auto 1fr; align-items: baseline; gap: 10px; padding: 9px 0; border-top: 1px solid var(--gray-200); }
.runs li:first-child { border-top: 0; }
.runs li.empty { display: block; font-size: 22px; font-weight: 700; color: var(--gray-400); line-height: 1.25; }
.rank { color: var(--gray-400); font-variant-numeric: tabular-nums; }
.run-score { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 20px; font-weight: 700; color: var(--gray-600); }
.runs li:first-child .run-score { color: #2563eb; }
.run-meta { font-size: 13px; color: var(--gray-500); text-align: right; }
.muted { margin: 0; font-size: 13px; line-height: 1.4; color: var(--gray-500); }

.stats { margin: 0 0 12px; }
.stats div { display: flex; justify-content: space-between; align-items: baseline; padding: 9px 0; border-top: 1px solid var(--gray-200); }
.stats div:first-child { border-top: 0; }
.stats dt { color: var(--gray-500); font-size: 14px; }
.stats dd { margin: 0; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 18px; font-weight: 700; color: var(--gray-600); }
.link { background: none; border: 0; padding: 0; color: var(--gray-500); font-size: 13px; text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
.link:hover { color: var(--gray-900); }

@media (max-width: 899px) {
    .interactions {
        position: fixed;
        top: 8px; right: 8px; bottom: 8px;
        width: min(24rem, calc(100vw - 16px));
        max-width: none;
        z-index: 20;
        box-shadow: 0 10px 30px rgb(0 0 0 / .45);
    }
}

/* ---------- toast (game over) ---------- */
.toast {
    position: fixed;
    right: 4px;
    bottom: 4px;
    z-index: 40;
    display: flex;
    align-items: center;
    gap: 12px;
    max-width: calc(100vw - 8px);
    padding: 12px 8px 12px 14px;
    background: var(--primary);
    color: #fff;
    border-radius: 6px;
    box-shadow: 0 .5rem 1rem rgb(0 0 0 / .25);
    animation: toast-in .2s ease-out;
}
.toast[hidden] { display: none; }
.toast-close { background: none; border: 0; color: rgb(255 255 255 / .85); font-size: 22px; line-height: 1; padding: 0 6px; cursor: pointer; }
.toast-close:hover { color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- how to play modal ---------- */
.modal {
    width: min(500px, calc(100vw - 24px));
    max-height: calc(100dvh - 24px);
    padding: 0;
    border: 1px solid rgb(0 0 0 / .175);
    border-radius: 8px;
    color: #212529;
    background: #fff;
    overflow: hidden;
}
.modal[open] { display: flex; flex-direction: column; }
.modal::backdrop { background: rgb(0 0 0 / .5); }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid #dee2e6; }
.modal-header h1 { margin: 0; font-size: 20px; font-weight: 500; line-height: 1.2; }
.modal-close { background: none; border: 0; font-size: 28px; line-height: 1; color: var(--gray-500); cursor: pointer; padding: 0 4px; }
.modal-close:hover { color: #000; }
.modal-body { padding: 16px; overflow: auto; font-size: 16px; line-height: 1.5; }
.modal-body h2 { margin: 18px 0 8px; font-size: 18px; font-weight: 700; }
.modal-body h2:first-child { margin-top: 0; }
.modal-body h3 { margin: 0 0 4px; font-size: 16px; font-weight: 600; }
.modal-body ul { margin: 0; padding-left: 16px; list-style: disc; }
.modal-body ul.plain { list-style: none; padding: 0; display: grid; gap: 8px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.inline-i { width: 1em; height: 1em; vertical-align: -0.125em; }
.modal-footer { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-top: 1px solid #dee2e6; }
.switch-link { font-size: 14px; color: var(--gray-500); text-underline-offset: 3px; }
.switch-link:hover { color: var(--gray-900); }
.btn-secondary { padding: 6px 12px; color: #fff; background: #6c757d; border: 1px solid #6c757d; border-radius: 6px; cursor: pointer; }
.btn-secondary:hover { background: #5c636a; }
.modal .muted { margin-top: 6px; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 0; border: 1px solid var(--gray-300); border-radius: 6px; overflow: hidden; }
.seg button { padding: 6px 0; border: 0; background: #fff; color: var(--gray-600); cursor: pointer; font-weight: 600; }
.seg button + button { border-left: 1px solid var(--gray-300); }
.seg button[aria-checked="true"] { background: var(--blue-500); color: #fff; }
.check { display: flex; align-items: center; gap: 8px; margin-top: 10px; cursor: pointer; }

.install[hidden], [data-install-tip][hidden], [data-install-section][hidden] { display: none; }
.modal-body .install { margin: 6px 0 4px; }

/* ---------- focus + motion ---------- */
:focus-visible { outline: 3px solid #1d4ed8; outline-offset: 2px; }
.bar :focus-visible, .frame :focus-visible { outline-color: #1e3a8a; }

@media (prefers-reduced-motion: reduce) {
    .toast, .frame.shake, #score.pop { animation: none; }
    .flash.show { animation: none; opacity: 1; }
}
