:root {
    color-scheme: light;
    --page: #f4f7fb;
    --panel: #ffffff;
    --panel-raised: #edf3fa;
    --line: #d5deea;
    --text: #1c2b3a;
    --muted: #667789;
    --blue: #2d70d8;
    --blue-strong: #2466ce;
    --red: #e85b5b;
}

* { box-sizing: border-box; }

html, body {
    width: 100%;
    height: 100%;
    min-height: 100%;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
    background: var(--page);
}

body.snake-page {
    color: var(--text);
    font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
    -webkit-font-smoothing: antialiased;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

button, select, input { font: inherit; }
button { color: inherit; }
button:focus-visible, select:focus-visible, input:focus-visible {
    outline: 2px solid var(--blue);
    outline-offset: 3px;
}

.game-screen {
    position: relative;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    background: var(--page);
}

.play-layout { display: contents; }

.board-stage {
    position: fixed;
    inset: 0;
    z-index: 0;
    min-width: 0;
    min-height: 0;
    pointer-events: none;
}

.board-frame {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    border: 0;
    border-radius: 0;
    background: #edf3fa;
    box-shadow: none;
}

#gameCanvas {
    position: absolute;
    inset: 0;
    display: block;
    width: 100%;
    height: 100%;
}

.state-overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    inset: auto;
    z-index: 2;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: max-content;
    max-width: min(92vw, 460px);
    padding: 13px 20px;
    transform: translate(-50%, -50%);
    border: 1px solid #d5deea;
    border-radius: 12px;
    background: #ffffffdf;
    box-shadow: 0 8px 28px #18314b18;
    text-align: center;
    pointer-events: none;
    transition: opacity .18s ease;
}
.state-overlay.is-hidden { opacity: 0; visibility: hidden; }
.overlay-kicker, .panel-kicker {
    display: block;
    color: var(--blue);
    font-size: 9px;
    font-weight: 800;
    letter-spacing: 2px;
}
.overlay-kicker { margin-bottom: 5px; }
.state-overlay h2 {
    margin: 0;
    color: var(--text);
    font-size: clamp(20px, 3vw, 30px);
    line-height: 1.2;
    letter-spacing: 1px;
}
.state-overlay p {
    max-width: 360px;
    margin: 6px 0 0;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.5;
}

.topbar {
    position: absolute;
    top: max(8px, env(safe-area-inset-top));
    left: max(14px, env(safe-area-inset-left));
    right: max(14px, env(safe-area-inset-right));
    z-index: 4;
    min-height: 42px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    align-items: center;
    gap: 12px;
    pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.brand {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    width: max-content;
    color: var(--blue);
    text-decoration: none;
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 1px;
}
.brand svg { width: 30px; height: 30px; }
.brand b { color: var(--text); }
.page-title { text-align: center; }
.eyebrow {
    display: block;
    color: var(--blue);
    font-size: 8px;
    font-weight: 800;
    letter-spacing: 2px;
}
.page-title h1 {
    margin: 2px 0 0;
    font-size: 17px;
    line-height: 1.1;
    letter-spacing: 1px;
}
.top-actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 7px;
    padding-right: 48px;
}
.icon-button, .top-button {
    min-height: 38px;
    border: 1px solid #cdd8e5;
    border-radius: 9px;
    background: #ffffffed;
    color: var(--text);
    cursor: pointer;
}
.icon-button { width: 40px; display: inline-grid; place-items: center; }
.icon-button svg, .top-button svg {
    width: 18px;
    height: 18px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.top-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    padding: 0 12px;
    font-size: 11px;
}
.icon-button:hover, .top-button:hover { border-color: #8ca8c9; background: #fff; }

.scorebar {
    position: absolute;
    top: calc(max(8px, env(safe-area-inset-top)) + 50px);
    left: max(14px, env(safe-area-inset-left));
    right: max(14px, env(safe-area-inset-right));
    z-index: 3;
    min-height: 47px;
    display: flex;
    align-items: center;
    gap: clamp(9px, 2vw, 24px);
    padding: 4px 12px;
    border-top: 1px solid #ccd7e4;
    border-bottom: 1px solid #ccd7e4;
    background: #f7f9fcdf;
    backdrop-filter: blur(8px);
}
.score-card { min-width: 60px; display: flex; flex-direction: column; gap: 1px; }
.score-card span { color: var(--muted); font-size: 9px; letter-spacing: .7px; }
.score-card strong { color: var(--text); font-size: 17px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.score-card:first-child strong { color: var(--blue); }
.score-secondary strong { font-size: 14px; }
.time-card strong { font-size: 12px; }
.state-pill {
    margin-left: auto;
    padding: 6px 10px;
    border: 1px solid #c6d7eb;
    border-radius: 8px;
    background: #fff;
    color: var(--blue);
    font-size: 10px;
    white-space: nowrap;
}
.state-pill[data-state="paused"] { color: #9a6500; border-color: #ead6a5; }
.state-pill[data-state="over"], .state-pill[data-state="won"] { color: #c84545; border-color: #edc4c4; }

.left-panel { display: none; }

.right-panel {
    position: fixed;
    left: 50%;
    bottom: max(12px, env(safe-area-inset-bottom));
    z-index: 5;
    width: min(460px, calc(100vw - 28px));
    max-width: none;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-items: center;
    gap: 0;
    padding: 10px;
    transform: translateX(-50%);
    border: 1px solid #d1dce8;
    border-radius: 13px;
    background: #ffffffed;
    box-shadow: 0 7px 24px #18314b1c;
    backdrop-filter: blur(8px);
}
.right-panel > .panel-kicker,
.right-panel > h2,
.right-panel > .desktop-help,
.right-panel > .mobile-help,
.speed-readout,
.joystick { display: none; }

.game-actions {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 9px;
    margin: 0;
}
.action-button {
    min-height: 43px;
    padding: 0 12px;
    border: 1px solid #c9d6e5;
    border-radius: 9px;
    background: #f8fafd;
    color: var(--text);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: .2px;
    cursor: pointer;
    transition: background .15s ease, border-color .15s ease, transform .15s ease;
}
.action-button:hover:not(:disabled) {
    transform: translateY(-1px);
    border-color: #8ca8c9;
    background: #edf4fc;
}
.action-button:disabled { opacity: .48; cursor: not-allowed; }
.action-button.primary { border-color: var(--blue-strong); background: var(--blue-strong); color: #fff; }
.action-button.primary:hover:not(:disabled) { border-color: #185bbd; background: #185bbd; }
.action-button.quiet { background: #fff; color: #53677c; }

.orientation-hint {
    position: absolute;
    top: calc(max(8px, env(safe-area-inset-top)) + 105px);
    left: max(14px, env(safe-area-inset-left));
    z-index: 4;
    align-items: center;
    gap: 9px;
    padding: 7px 9px 7px 12px;
    border: 1px solid #d1dce8;
    border-radius: 9px;
    background: #ffffffed;
    color: #53677c;
    font-size: 10px;
    box-shadow: 0 3px 12px #18314b12;
}
.orientation-hint[hidden] { display: none !important; }
.orientation-hint button {
    min-height: 30px;
    padding: 0 10px;
    border: 1px solid #b9cde5;
    border-radius: 7px;
    background: #eef5ff;
    color: var(--blue);
    font-size: 10px;
    font-weight: 700;
}

.settings-dialog {
    width: min(92vw, 430px);
    max-width: none;
    max-height: min(90dvh, 720px);
    padding: 0;
    overflow: auto;
    border: 1px solid #cbd7e5;
    border-radius: 13px;
    background: #fff;
    color: var(--text);
    box-shadow: 0 24px 80px #18314b38;
}
.settings-dialog::backdrop { background: #23354d66; backdrop-filter: blur(3px); }
.settings-content { display: grid; gap: 0; padding: 20px; }
.settings-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.settings-heading h2 { margin: 5px 0 0; font-size: 22px; }
.close-settings { flex: 0 0 auto; }
.setting-row {
    min-height: 64px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    border-top: 1px solid #e0e6ee;
}
.setting-row strong, .setting-row small, .radio-row strong, .radio-row small { display: block; }
.setting-row strong, .radio-row strong { font-size: 13px; }
.setting-row small, .radio-row small { margin-top: 4px; color: var(--muted); font-size: 11px; line-height: 1.45; }
.setting-row select {
    width: 112px;
    padding: 9px 10px;
    border: 1px solid #cbd7e5;
    border-radius: 8px;
    background: var(--panel-raised);
    color: var(--text);
}
.setting-fieldset { margin: 0; padding: 13px 0 8px; border: 0; border-top: 1px solid #e0e6ee; }
.setting-fieldset legend { padding: 0; color: var(--text); font-size: 13px; font-weight: 700; }
.radio-row { min-height: 52px; display: flex; align-items: center; gap: 11px; padding: 5px 0; cursor: pointer; }
.radio-row input, .sound-row input { width: 18px; height: 18px; accent-color: var(--blue-strong); }
.sound-row input { flex: 0 0 auto; }
.settings-done { width: 100%; margin-top: 12px; }

.snake-page #gm-fab {
    top: calc(max(8px, env(safe-area-inset-top)) + 5px) !important;
    right: max(12px, env(safe-area-inset-right)) !important;
    bottom: auto !important;
    left: auto !important;
    box-shadow: 0 4px 18px #18314b20;
}

@media (max-width: 1000px), (pointer: coarse) {
    .topbar {
        left: max(10px, env(safe-area-inset-left));
        right: max(10px, env(safe-area-inset-right));
        min-height: 38px;
        gap: 6px;
    }
    .brand { gap: 5px; font-size: 10px; letter-spacing: .4px; }
    .brand svg { width: 26px; height: 26px; }
    .page-title .eyebrow { font-size: 7px; letter-spacing: 1.2px; }
    .page-title h1 { font-size: 15px; }
    .top-actions { gap: 5px; padding-right: 45px; }
    .icon-button { width: 35px; min-height: 35px; }
    .top-button { min-height: 35px; gap: 5px; padding: 0 8px; font-size: 9px; }
    .top-button svg { width: 15px; height: 15px; }
    .scorebar {
        top: calc(max(8px, env(safe-area-inset-top)) + 44px);
        left: max(10px, env(safe-area-inset-left));
        right: max(10px, env(safe-area-inset-right));
        min-height: 43px;
        gap: clamp(5px, 1.5vw, 13px);
        padding: 3px 6px;
    }
    .score-card { min-width: clamp(40px, 8vw, 60px); }
    .score-card span { font-size: 8px; letter-spacing: .3px; }
    .score-card strong { font-size: 14px; }
    .score-secondary strong { font-size: 12px; }
    .time-card strong { font-size: 11px; }
    .state-pill { padding: 5px 7px; font-size: 9px; }
    .state-overlay { padding: 10px 15px; }
    .state-overlay h2 { font-size: clamp(18px, 4vw, 25px); }
    .state-overlay p { font-size: 10px; }
}

@media (pointer: coarse) {
    .right-panel {
        left: max(10px, env(safe-area-inset-left));
        right: max(10px, env(safe-area-inset-right));
        bottom: max(9px, env(safe-area-inset-bottom));
        width: auto;
        grid-template-columns: min(122px, 27vw) minmax(0, 1fr);
        grid-template-rows: auto minmax(0, 1fr);
        gap: 3px 11px;
        padding: 8px 10px;
        transform: none;
        border-radius: 12px;
    }
    .right-panel > .mobile-help {
        grid-column: 1;
        grid-row: 1;
        display: block;
        align-self: end;
        padding-left: 5px;
        color: var(--muted);
        font-size: 9px;
        line-height: 1.25;
    }
    .mobile-help span { display: block; }
    .mobile-help span + span { margin-top: 1px; opacity: .8; }
    .joystick {
        position: relative;
        grid-column: 1;
        grid-row: 2;
        justify-self: center;
        width: min(108px, 27vw);
        aspect-ratio: 1;
        display: grid;
        place-items: center;
        border-radius: 50%;
        touch-action: none;
        -webkit-tap-highlight-color: transparent;
        user-select: none;
    }
    .joystick-ring {
        position: absolute;
        inset: 3px;
        border: 1px solid #aebfd2;
        border-radius: 50%;
        background: #edf3fa;
        box-shadow: inset 0 0 0 8px #f9fbfd;
    }
    .joystick-center { position: absolute; width: 5px; height: 5px; border-radius: 50%; background: #8298b0; }
    .joystick-knob {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 43%;
        height: 43%;
        border: 1px solid #1c5db9;
        border-radius: 50%;
        background: #347de0;
        box-shadow: 0 4px 12px #18314b35;
        transform: translate(-50%, -50%);
        pointer-events: none;
    }
    .game-actions {
        grid-column: 2;
        grid-row: 1 / span 2;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        align-content: center;
        gap: 6px;
    }
    .action-button { min-height: 43px; padding: 0 5px; font-size: 10px; }
    .orientation-hint { font-size: 9px; }
    .orientation-hint button { min-height: 28px; padding: 0 8px; }
}

@media (orientation: landscape) and (pointer: coarse) {
    .right-panel {
        left: 50%;
        right: auto;
        width: min(760px, calc(100vw - 20px));
        transform: translateX(-50%);
    }
}

@media (orientation: portrait) and (pointer: coarse) {
    .orientation-hint:not([hidden]) { display: flex; }
    .right-panel {
        grid-template-columns: min(104px, 27vw) minmax(0, 1fr);
        gap: 2px 7px;
        padding: 7px;
    }
    .joystick { width: min(96px, 26vw); }
    .action-button { min-height: 40px; font-size: 9px; }
    .state-overlay { max-width: 88vw; }
}

@media (orientation: landscape) and (max-height: 560px) and (pointer: coarse) {
    .topbar { min-height: 32px; }
    .brand svg { width: 22px; height: 22px; }
    .page-title h1 { font-size: 13px; }
    .scorebar { top: calc(max(5px, env(safe-area-inset-top)) + 37px); min-height: 36px; }
    .right-panel {
        grid-template-columns: 92px minmax(0, 1fr);
        grid-template-rows: minmax(0, 1fr);
        gap: 0 8px;
        padding: 6px 8px;
    }
    .right-panel > .mobile-help { display: none; }
    .joystick { grid-row: 1; width: min(86px, 22vw); }
    .game-actions { grid-row: 1; }
    .action-button { min-height: 34px; font-size: 9px; }
    .orientation-hint { top: calc(max(5px, env(safe-area-inset-top)) + 78px); }
}

@media (max-width: 390px) and (orientation: portrait) {
    .topbar { left: max(7px, env(safe-area-inset-left)); right: max(7px, env(safe-area-inset-right)); }
    .brand { font-size: 9px; }
    .scorebar { left: max(7px, env(safe-area-inset-left)); right: max(7px, env(safe-area-inset-right)); gap: 4px; padding-inline: 3px; }
    .score-card { min-width: 39px; }
    .state-pill { padding: 4px 5px; font-size: 8px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; }
}
