/* =========================================================
   Snake — Retro Nokia-style theme
   All CSS variables are theme-aware via [data-theme=...]
   ========================================================= */

/* ---- Reset & base ---- */
*, *::before, *::after { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    min-height: 100%;
    background: #1a1a1a;
    color: var(--fg);
    font-family: 'VT323', 'Courier New', monospace;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
    touch-action: manipulation;
}

body {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background:
        radial-gradient(ellipse at top, #2a2a2a 0%, #111 70%),
        #111;
}

/* ---- Themes ---- */
body[data-theme="nokia"] {
    --bg-phone: #6b6b5a;
    --bg-phone-edge: #3d3d32;
    --bg-screen: #9ba87e;
    --bg-screen-shade: #7d8a66;
    --fg: #1b2a13;
    --fg-dim: #445934;
    --accent: #2d4a1f;
    --danger: #6e1a1a;
    --grid-line: rgba(27, 42, 19, 0.18);
    --snake: #1b2a13;
    --snake-head: #0c1808;
    --food: #1b2a13;
    --bonus: #6e1a1a;
    --obstacle: #445934;
    --shadow: rgba(0, 0, 0, 0.35);
    --font-display: 'VT323', 'Courier New', monospace;
}

body[data-theme="dark"] {
    --bg-phone: #202028;
    --bg-phone-edge: #0e0e14;
    --bg-screen: #0d0d12;
    --bg-screen-shade: #050507;
    --fg: #d8e0d4;
    --fg-dim: #6c7468;
    --accent: #88c070;
    --danger: #d96a6a;
    --grid-line: rgba(216, 224, 212, 0.07);
    --snake: #88c070;
    --snake-head: #b6e89c;
    --food: #d8e0d4;
    --bonus: #f0c060;
    --obstacle: #4a5249;
    --shadow: rgba(0, 0, 0, 0.6);
    --font-display: 'VT323', 'Courier New', monospace;
}

body[data-theme="neon"] {
    --bg-phone: #1a0a2e;
    --bg-phone-edge: #08020f;
    --bg-screen: #0a0220;
    --bg-screen-shade: #050010;
    --fg: #f6c453;
    --fg-dim: #b07030;
    --accent: #f6c453;
    --danger: #ff5577;
    --grid-line: rgba(246, 196, 83, 0.08);
    --snake: #39ff88;
    --snake-head: #b6ffd6;
    --food: #f6c453;
    --bonus: #ff5577;
    --obstacle: #b07030;
    --shadow: 0 0 18px rgba(57, 255, 136, 0.25);
    --font-display: 'Press Start 2P', 'VT323', monospace;
}

/* ---- Accessibility ---- */
.skip-link {
    position: absolute;
    left: -9999px;
    top: 0;
    background: var(--accent);
    color: var(--bg-screen);
    padding: 8px 12px;
    z-index: 1000;
}
.skip-link:focus { left: 8px; top: 8px; }

button { font-family: inherit; }

/* ---- Phone "case" ---- */
.phone {
    width: 100%;
    max-width: 420px;
    background: linear-gradient(180deg, var(--bg-phone) 0%, var(--bg-phone-edge) 100%);
    border-radius: 28px;
    padding: 18px 14px 22px;
    box-shadow:
        0 30px 60px rgba(0, 0, 0, 0.55),
        inset 0 1px 0 rgba(255, 255, 255, 0.08),
        inset 0 -2px 0 rgba(0, 0, 0, 0.4);
    position: relative;
}

.phone::before {
    content: "";
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 60px;
    height: 4px;
    background: rgba(0, 0, 0, 0.4);
    border-radius: 4px;
}

.phone__brand {
    text-align: center;
    color: rgba(255, 255, 255, 0.7);
    margin: 8px 0 14px;
    letter-spacing: 4px;
    text-transform: uppercase;
    font-size: 14px;
    font-family: 'Press Start 2P', 'VT323', monospace;
}
.phone__brand-name { font-weight: bold; margin-right: 8px; }
.phone__brand-sub { font-size: 9px; opacity: 0.7; }

.phone__footer {
    text-align: center;
    color: rgba(255, 255, 255, 0.35);
    font-size: 12px;
    margin-top: 14px;
    letter-spacing: 1px;
}

/* ---- Screen ---- */
.screen {
    background: linear-gradient(180deg, var(--bg-screen) 0%, var(--bg-screen-shade) 100%);
    border-radius: 10px;
    padding: 10px;
    box-shadow:
        inset 0 2px 6px rgba(0, 0, 0, 0.5),
        inset 0 -1px 0 rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(0, 0, 0, 0.5);
}

.screen__inner {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* ---- HUD ---- */
.hud {
    display: flex;
    justify-content: space-between;
    padding: 4px 8px;
    color: var(--fg);
    font-family: var(--font-display);
    font-size: 22px;
    line-height: 1;
    letter-spacing: 1px;
}
.hud__item { display: flex; flex-direction: column; gap: 2px; }
.hud__item--right { text-align: right; }
.hud__label { font-size: 12px; opacity: 0.7; letter-spacing: 2px; }
.hud__value { font-size: 26px; font-weight: bold; }

/* ---- Canvas ---- */
.canvas-wrap {
    position: relative;
    width: 100%;
    aspect-ratio: 1 / 1;
    background: var(--bg-screen-shade);
    border-radius: 6px;
    overflow: hidden;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.4);
}

#game-canvas {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
}

/* ---- Overlays ---- */
.overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 16px;
    background: rgba(0, 0, 0, 0.55);
    color: var(--fg);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    animation: fadeIn 0.2s ease-out;
}
.overlay[hidden] { display: none; }

@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

.overlay__title {
    font-family: var(--font-display);
    font-size: 32px;
    letter-spacing: 4px;
    margin: 0 0 16px;
    color: var(--fg);
}
.overlay__title--danger { color: var(--danger); }

.title {
    font-family: var(--font-display);
    font-size: 56px;
    margin: 0 0 4px;
    letter-spacing: 8px;
    color: var(--fg);
    text-shadow: 0 0 8px var(--shadow);
}
.subtitle {
    font-size: 16px;
    opacity: 0.7;
    margin: 0 0 22px;
    letter-spacing: 2px;
}

.start-info {
    width: 80%;
    max-width: 280px;
    margin-bottom: 22px;
    border-top: 1px dashed var(--fg-dim);
    border-bottom: 1px dashed var(--fg-dim);
    padding: 10px 0;
    font-size: 18px;
    letter-spacing: 1px;
}
.start-info__row {
    display: flex;
    justify-content: space-between;
    padding: 2px 0;
}
.start-info__row span:first-child { opacity: 0.7; }

.gameover-stats {
    width: 80%;
    max-width: 280px;
    margin-bottom: 20px;
    font-size: 22px;
    letter-spacing: 2px;
}
.gameover-stats__row {
    display: flex;
    justify-content: space-between;
    padding: 4px 0;
    border-bottom: 1px dashed var(--fg-dim);
}
.gameover-stats__row:last-of-type { border-bottom: none; }
.gameover-stats__row span:first-child { opacity: 0.7; }
#new-record-row {
    justify-content: center;
    color: var(--bonus);
    font-weight: bold;
    margin-top: 8px;
    animation: pulse 0.8s ease-in-out infinite alternate;
}
@keyframes pulse {
    from { opacity: 0.5; transform: scale(1); }
    to   { opacity: 1; transform: scale(1.05); }
}

.overlay--countdown {
    background: rgba(0, 0, 0, 0.3);
}
.countdown__text {
    font-family: var(--font-display);
    font-size: 96px;
    color: var(--fg);
    text-shadow: 0 0 12px var(--shadow);
    animation: countPop 0.9s ease-out;
}
@keyframes countPop {
    0%   { transform: scale(0.4); opacity: 0; }
    40%  { transform: scale(1.15); opacity: 1; }
    100% { transform: scale(1); opacity: 1; }
}

/* ---- Buttons ---- */
.btn {
    font-family: var(--font-display);
    font-size: 20px;
    letter-spacing: 2px;
    text-transform: uppercase;
    padding: 10px 22px;
    border: 2px solid var(--fg);
    background: transparent;
    color: var(--fg);
    border-radius: 4px;
    cursor: pointer;
    transition: transform 0.05s ease, background 0.15s ease, color 0.15s ease;
    min-width: 140px;
}
.btn:hover { background: var(--fg); color: var(--bg-screen); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible {
    outline: 2px dashed var(--accent);
    outline-offset: 3px;
}
.btn--primary {
    background: var(--fg);
    color: var(--bg-screen);
}
.btn--primary:hover { background: var(--accent); border-color: var(--accent); color: var(--bg-screen); }

/* ---- Controls (under screen) ---- */
.controls {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-top: 14px;
}
.ctrl-btn {
    font-family: var(--font-display);
    font-size: 18px;
    letter-spacing: 2px;
    text-transform: uppercase;
    padding: 12px 4px;
    background: linear-gradient(180deg, #3a3a3a 0%, #1c1c1c 100%);
    color: #d8d8d8;
    border: 1px solid #000;
    border-radius: 8px;
    cursor: pointer;
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, 0.08),
        0 2px 0 #000;
    transition: transform 0.05s ease, box-shadow 0.05s ease;
}
.ctrl-btn:hover { filter: brightness(1.15); }
.ctrl-btn:active {
    transform: translateY(2px);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), 0 0 0 #000;
}
.ctrl-btn:focus-visible {
    outline: 2px dashed var(--accent);
    outline-offset: 2px;
}

.ctrl-btn__label {
    display: block;
    line-height: 1;
}

.hotkey {
    display: inline-block;
    font-family: var(--font-display);
    font-size: 11px;
    letter-spacing: 1px;
    line-height: 1;
    padding: 2px 6px;
    min-width: 18px;
    text-align: center;
    color: #d8d8d8;
    background: linear-gradient(180deg, #4a4a4a 0%, #2a2a2a 100%);
    border: 1px solid #000;
    border-radius: 4px;
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, 0.12),
        0 1px 0 #000;
    vertical-align: middle;
    white-space: nowrap;
}

/* When hotkey sits inside the phone it should match case text */
.controls .hotkey {
    font-size: 10px;
    padding: 2px 5px;
    margin-top: 4px;
    opacity: 0.75;
}

/* ---- Hotkeys list (below controls) ---- */
.hotkeys {
    margin-top: 14px;
    padding: 10px 12px;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid rgba(0, 0, 0, 0.4);
    border-radius: 8px;
    color: rgba(255, 255, 255, 0.75);
}

.hotkeys__title {
    margin: 0 0 8px;
    font-family: 'Press Start 2P', 'VT323', monospace;
    font-size: 9px;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: rgba(255, 255, 255, 0.55);
    text-align: center;
}

.hotkeys__list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px 12px;
    font-size: 14px;
    letter-spacing: 1px;
}

.hotkeys__item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 3px 0;
}

.hotkeys__keys {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    flex-wrap: wrap;
}

.hotkeys__desc {
    opacity: 0.8;
    text-align: right;
}

@media (max-width: 380px) {
    .hotkeys__list {
        grid-template-columns: 1fr;
        gap: 4px;
        font-size: 13px;
    }
}

/* ---- D-pad (mobile) ---- */
.dpad {
    display: none; /* shown only on touch / small screens */
    margin-top: 12px;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 56px);
    gap: 6px;
    width: 100%;
    max-width: 240px;
    margin-left: auto;
    margin-right: auto;
}
.dpad__btn {
    font-size: 22px;
    background: linear-gradient(180deg, #3a3a3a 0%, #1c1c1c 100%);
    color: #d8d8d8;
    border: 1px solid #000;
    border-radius: 8px;
    cursor: pointer;
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, 0.08),
        0 2px 0 #000;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}
.dpad__btn:active {
    transform: translateY(2px);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), 0 0 0 #000;
    background: linear-gradient(180deg, #4a4a4a 0%, #2a2a2a 100%);
}
.dpad__btn--up    { grid-column: 2; grid-row: 1; }
.dpad__btn--left  { grid-column: 1; grid-row: 2; }
.dpad__btn--center {
    grid-column: 2; grid-row: 2;
    background: radial-gradient(circle, #555 0%, #222 80%);
    cursor: default;
    font-size: 14px;
    color: rgba(255,255,255,0.3);
}
.dpad__btn--right { grid-column: 3; grid-row: 2; }
.dpad__btn--down  { grid-column: 2; grid-row: 3; }

/* Show dpad on coarse pointers */
@media (pointer: coarse) {
    .dpad { display: grid; }
}

/* ---- Modal ---- */
.modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}
.modal[hidden] { display: none; }

.modal__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
}

.modal__panel {
    position: relative;
    width: 100%;
    max-width: 480px;
    max-height: 90vh;
    background: linear-gradient(180deg, #2a2a2a 0%, #181818 100%);
    color: #e8e8e8;
    border-radius: 12px;
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: modalIn 0.2s ease-out;
}
@keyframes modalIn {
    from { transform: translateY(20px) scale(0.96); opacity: 0; }
    to   { transform: translateY(0) scale(1); opacity: 1; }
}

.modal__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.modal__title {
    margin: 0;
    font-family: var(--font-display);
    font-size: 24px;
    letter-spacing: 4px;
    text-transform: uppercase;
}
.modal__close {
    background: transparent;
    border: none;
    color: #e8e8e8;
    font-size: 22px;
    cursor: pointer;
    width: 32px; height: 32px;
    border-radius: 4px;
}
.modal__close:hover { background: rgba(255, 255, 255, 0.08); }
.modal__close:focus-visible { outline: 2px dashed var(--accent); outline-offset: 2px; }

.modal__body {
    padding: 12px 18px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.modal__footer {
    padding: 12px 18px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}
.modal__hint {
    margin: 0;
    font-size: 14px;
    opacity: 0.7;
}

/* ---- Settings fields ---- */
.setting {
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 8px;
    padding: 10px 12px;
    margin: 0;
}
.setting legend {
    font-size: 14px;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: #b8b8b8;
    padding: 0 6px;
}
.setting__options {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 6px;
}

.chip {
    font-family: var(--font-display);
    font-size: 16px;
    letter-spacing: 1px;
    padding: 6px 12px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.12);
    color: #e8e8e8;
    border-radius: 16px;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease;
}
.chip:hover { background: rgba(255, 255, 255, 0.08); }
.chip:focus-visible { outline: 2px dashed var(--accent); outline-offset: 2px; }
.chip[aria-pressed="true"] {
    background: var(--accent);
    color: #111;
    border-color: var(--accent);
}

/* In dark theme accent is greenish; ensure readable */
body[data-theme="dark"] .chip[aria-pressed="true"] { color: #0d0d12; }
body[data-theme="neon"] .chip[aria-pressed="true"] { color: #050010; }

/* ---- Responsive ---- */
@media (max-width: 380px) {
    .hud { font-size: 18px; }
    .hud__value { font-size: 22px; }
    .title { font-size: 44px; }
    .btn { font-size: 16px; padding: 8px 16px; min-width: 120px; }
    .ctrl-btn { font-size: 14px; }
}

@media (max-height: 700px) {
    .phone { padding: 10px 10px 14px; }
    .canvas-wrap { max-height: 55vh; }
}

/* ---- Utility: prevent text selection on rapid clicking ---- */
.no-select {
    user-select: none;
    -webkit-user-select: none;
}