:root {
    --bg-top: #f4f7ff;
    --bg-bottom: #fff4d8;
    --panel: rgba(255, 255, 255, 0.92);
    --surface: rgba(255, 255, 255, 0.94);
    --line: rgba(116, 141, 191, 0.24);
    --text: #24324d;
    --muted: #617392;
    --snake: linear-gradient(135deg, #9cf36a 0%, #52d886 48%, #28b7a6 100%);
    --snake-head: linear-gradient(135deg, #47d47d 0%, #1bb39b 52%, #138a89 100%);
    --food: linear-gradient(135deg, rgba(255, 238, 227, 0.95) 0%, rgba(255, 248, 240, 0.98) 100%);
    --cell: rgba(219, 228, 240, 0.9);
    --accent: linear-gradient(135deg, #4d7fe0 0%, #6a55e6 100%);
    --gold: #ffce54;
    --blue: #2563eb;
    --shadow-soft: 0 18px 40px rgba(72, 92, 140, 0.12);
    --shadow-strong: 0 16px 30px rgba(83, 104, 171, 0.22);
}

* { box-sizing: border-box; }

/* body는 .game-shell-body (공유 CSS)에서 100dvh flex column 처리. 여기서는 배경만. */
body {
    font-family: 'Noto Sans KR', sans-serif;
    color: var(--text);
    background:
        radial-gradient(circle at top left, rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0) 34%),
        linear-gradient(180deg, var(--bg-top) 0%, var(--bg-bottom) 100%);
}

button { font: inherit; }

/* ───── 메인 레이아웃: 슬림 HUD + 보드 영역 ───── */
.game-main {
    flex: 1 1 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 0.5rem 0.75rem 0.75rem;
    gap: 0.5rem;
}

/* ───── 슬림 HUD ───── */
.game-hud {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 0.5rem 1rem 0.5rem 6rem; /* 좌측 back-pill 공간 확보 */
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 1rem;
    flex-shrink: 0;
}
.game-hud .hud-group:first-of-type {
    grid-column: 2;
    justify-self: center;
}
.game-hud .hud-group.right {
    grid-column: 3;
    justify-self: end;
}
.hud-group {
    display: flex;
    align-items: center;
    gap: 1.25rem;
}
.hud-stat {
    display: inline-flex;
    align-items: baseline;
    gap: 0.4rem;
    font-size: 0.85rem;
}
.hud-label {
    color: var(--muted);
    font-weight: 700;
}
.hud-stat strong {
    font-family: 'Jua', sans-serif;
    font-size: 1.4rem;
    line-height: 1;
    font-weight: 400;
    color: var(--text);
}
.hud-msg {
    font-size: 0.82rem;
    color: var(--muted);
    min-height: 1em;
}

/* 점수 변화 애니메이션 */
.value.score-bump {
    animation: scoreBump 0.45s ease;
}

/* ───── 보드 영역: viewport 남는 공간을 정사각형으로 ───── */
.board-area {
    flex: 1 1 0;
    min-height: 0;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.board-frame {
    position: relative;
    width: var(--board-size, 100%);
    height: var(--board-size, 100%);
    padding: 12px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 22px;
    box-shadow: var(--shadow-soft);
    backdrop-filter: blur(14px);
    overflow: hidden;
}

.board-frame::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.52), rgba(255, 255, 255, 0));
    pointer-events: none;
    border-radius: 22px;
}

.board-frame.game-over-flash {
    animation: gameOverFlash 0.7s ease;
}

.board-frame.food-burst {
    animation: boardPop 0.35s ease;
}

.board {
    position: relative;
    z-index: 1;
    display: grid;
    grid-template-columns: repeat(16, 1fr);
    grid-template-rows: repeat(16, 1fr);
    gap: 4px;
    width: 100%;
    height: 100%;
}

.cell {
    position: relative;
    overflow: hidden;
    border-radius: 8px;
    background: var(--cell);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.55);
    transition: transform 0.12s ease, filter 0.12s ease, background 0.18s ease;
}

.cell::before,
.cell::after {
    position: absolute;
    pointer-events: none;
}

.cell.snake {
    background: var(--snake);
    box-shadow: inset 0 -3px 0 rgba(18, 135, 117, 0.22), 0 8px 16px rgba(37, 184, 138, 0.22);
}

.cell.snake::before {
    content: '';
    inset: 10% 14% 48% 14%;
    border-radius: 999px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.72), rgba(255, 255, 255, 0));
}

.cell.body-alt {
    filter: saturate(1.06) brightness(1.02);
}

.cell.tail {
    border-radius: 10px 10px 14px 14px;
    transform: scale(0.94);
}

.cell.head {
    background: var(--snake-head);
    transform: scale(1.08);
    border-radius: 11px;
    box-shadow: inset 0 -4px 0 rgba(11, 111, 105, 0.24), 0 10px 22px rgba(19, 112, 92, 0.28);
    animation: headBounce 0.45s ease-in-out infinite alternate;
}

.cell.head::before {
    content: '';
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: #17324a;
    top: 30%;
    left: 28%;
    box-shadow: 14px 0 0 #17324a;
}

.cell.head::after {
    content: '';
    width: 62%;
    height: 34%;
    left: 19%;
    top: 12%;
    border-radius: 999px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0));
}

.cell.head-up::before { top: 26%; }
.cell.head-down::before { top: 38%; }
.cell.head-left::before {
    top: 28%;
    left: 20%;
    box-shadow: 10px 0 0 #17324a;
}
.cell.head-right::before {
    top: 28%;
    left: 34%;
    box-shadow: 10px 0 0 #17324a;
}

.cell.food {
    background: var(--food);
    box-shadow: inset 0 0 0 1px rgba(255, 189, 189, 0.35), 0 12px 24px rgba(255, 115, 129, 0.18);
}

.cell.food::before {
    content: '\1F34E';
    display: grid;
    place-items: center;
    inset: 0;
    font-size: clamp(12px, 2vw, 24px);
    line-height: 1;
    filter: drop-shadow(0 3px 2px rgba(222, 70, 77, 0.18));
}

/* ───── 오버레이 ───── */
.overlay {
    position: absolute;
    inset: 12px;
    display: grid;
    place-content: center;
    gap: 0.55rem;
    text-align: center;
    background: rgba(245, 248, 255, 0.85);
    border-radius: 18px;
    padding: 1.5rem;
    z-index: 2;
}

.overlay.hidden { display: none; }

.overlay h1 {
    margin: 0;
    font-family: 'Jua', sans-serif;
    font-size: clamp(1.8rem, 4vw, 2.9rem);
    color: var(--text);
    animation: overlayBounce 0.7s ease;
}

.overlay p {
    margin: 0;
    color: var(--muted);
}

/* ───── 이펙트 레이어 ───── */
.fx-layer {
    position: absolute;
    inset: 12px;
    pointer-events: none;
    z-index: 3;
}

.spark {
    position: absolute;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    opacity: 0;
    display: grid;
    place-items: center;
    font-size: 14px;
    line-height: 1;
    animation: sparkle 0.7s ease-out forwards;
}

.spark-star {
    background: transparent !important;
    width: 54px;
    height: 54px;
    font-size: 48px;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.7);
}

.spark-apple {
    background: transparent !important;
    width: 60px;
    height: 60px;
    font-size: 51px;
    filter: drop-shadow(0 4px 6px rgba(255, 107, 107, 0.25));
}

/* ───── 모바일 터치 컨트롤 (기본 숨김, 좁은 화면에서만 표시) ───── */
.touch-controls {
    display: none;
    flex-shrink: 0;
}

.control-btn {
    border: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.88);
    color: var(--text);
    border-radius: 14px;
    cursor: pointer;
    box-shadow: 0 6px 12px rgba(84, 107, 165, 0.08);
    transition: transform 0.14s ease, box-shadow 0.14s ease, background 0.14s ease;
    font-size: 1.6rem;
    line-height: 1;
}

.control-btn:hover,
.control-btn:active {
    transform: translateY(-2px) scale(1.02);
    box-shadow: var(--shadow-strong);
}

.up { grid-area: up; }
.left { grid-area: left; }
.down { grid-area: down; }
.right { grid-area: right; }

/* ───── 애니메이션 ───── */
@keyframes headBounce {
    from { transform: scale(1.04) translateY(0); }
    to { transform: scale(1.1) translateY(-2px); }
}

@keyframes scoreBump {
    0% { transform: scale(1); }
    40% { transform: scale(1.25); color: #ff6f61; }
    100% { transform: scale(1); }
}

@keyframes boardPop {
    0% { transform: scale(1); }
    45% { transform: scale(1.01); }
    100% { transform: scale(1); }
}

@keyframes overlayBounce {
    0% { transform: translateY(10px) scale(0.96); opacity: 0; }
    100% { transform: translateY(0) scale(1); opacity: 1; }
}

@keyframes sparkle {
    0% { transform: translate(0, 0) scale(0.6); opacity: 1; }
    100% { transform: translate(var(--dx), var(--dy)) scale(1.3); opacity: 0; }
}

@keyframes gameOverFlash {
    0%, 100% { box-shadow: var(--shadow-soft); }
    30% { box-shadow: 0 0 0 10px rgba(255, 106, 132, 0.15), var(--shadow-strong); }
}

/* ───── 반응형 ───── */
@media (max-width: 640px) {
    .game-main { padding: 0.4rem 0.5rem 0.5rem; gap: 0.4rem; }
    .game-hud {
        padding: 0.45rem 0.7rem 0.45rem 5rem;
        gap: 0.6rem;
    }
    .hud-group { gap: 0.8rem; }
    .hud-stat strong { font-size: 1.15rem; }
    .hud-stat { font-size: 0.78rem; }
    .hud-msg { display: none; }
}

@media (max-width: 520px) {
    .board { gap: 3px; }
    .board-frame { padding: 8px; border-radius: 16px; }
    .overlay,
    .fx-layer { inset: 8px; }

    .touch-controls {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        grid-template-areas:
            '. up .'
            'left down right';
        gap: 0.5rem;
        width: 100%;
        max-width: 280px;
        margin: 0 auto;
    }

    .control-btn {
        width: 100%;
        height: 56px;
        font-size: 1.4rem;
    }
}
