:root {
    --bg-graphite: #121316;
    --bg-surface: #1a1c23;
    --bg-surface-trans: rgba(26, 28, 35, 0.92);
    --accent-cyan: #3fe0ff;
    --accent-cyan-dim: rgba(63, 224, 255, 0.25);
    --accent-cyan-hover: #75ebff;
    --accent-red: #ff3b69;
    --accent-amber: #ffaa00;
    --accent-green: #2ecc71;
    --text-main: #e8eaee;
    --text-muted: #9aa0a6;
    --font-stack: system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    /* 1% of #game-container height (16:9 box capped at 960px wide and at the viewport height).
       Modal sizes below scale with it and reach their original values at 540px. */
    --u: min(1vh, 0.5625vw, 5.4px);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

html, body {
    width: 100%;
    height: 100%;
    background-color: var(--bg-graphite);
    color: var(--text-main);
    font-family: var(--font-stack);
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

#game-container {
    position: relative;
    width: min(100%, calc(100vh * 16 / 9));
    max-width: 960px;
    aspect-ratio: 16 / 9;
    background: #0d0e11;
    border: 2px solid var(--accent-cyan);
    border-radius: 10px;
    box-shadow: 0 0 25px var(--accent-cyan-dim), 0 10px 40px rgba(0, 0, 0, 0.7);
    overflow: hidden;
}

/* Canvas styling */
#gameCanvas {
    display: block;
    width: 100%;
    height: 100%;
    background: #121316;
    cursor: pointer;
    outline: none;
}

#gameCanvas:focus-visible {
    outline: 2px solid var(--accent-cyan);
    outline-offset: -2px;
}

/* HUD Overlay */
#hud {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    padding: 12px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    pointer-events: none;
    z-index: 10;
    background: linear-gradient(to bottom, rgba(18, 19, 22, 0.85) 0%, rgba(18, 19, 22, 0) 100%);
}

.hud-group {
    display: flex;
    align-items: center;
    gap: 16px;
    pointer-events: auto;
}

.hud-item {
    display: flex;
    flex-direction: column;
    background: rgba(18, 19, 22, 0.75);
    border: 1px solid rgba(63, 224, 255, 0.3);
    border-radius: 6px;
    padding: 4px 12px;
    min-width: 75px;
    backdrop-filter: blur(4px);
}

.hud-label {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 1px;
    color: var(--accent-cyan);
}

.hud-num {
    font-size: 18px;
    font-weight: 800;
    letter-spacing: 0.5px;
    color: var(--text-main);
    font-variant-numeric: tabular-nums;
}

/* Lives icon bar */
.lives-icons {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 3px;
    height: 18px;
}

.life-pip {
    display: inline-block;
    width: 14px;
    height: 14px;
    background: var(--accent-cyan);
    box-shadow: 0 0 6px var(--accent-cyan);
    border-radius: 3px;
    transition: all 0.2s ease;
}

.life-pip.empty {
    background: #2b2e38;
    box-shadow: none;
    border: 1px solid #454a59;
}

/* HUD Action buttons */
.hud-btn {
    width: 38px;
    height: 38px;
    border-radius: 8px;
    border: 1px solid var(--accent-cyan);
    background: rgba(26, 28, 35, 0.85);
    color: var(--accent-cyan);
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
}

.hud-btn:hover {
    background: var(--accent-cyan-dim);
    box-shadow: 0 0 10px var(--accent-cyan);
}

.hud-btn:active {
    transform: scale(0.92);
}

.hud-btn:focus-visible {
    outline: 2px solid var(--accent-cyan);
    outline-offset: 2px;
}

/* Overlays */
.overlay {
    position: absolute;
    inset: 0;
    background: rgba(18, 19, 22, 0.88);
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 25;
    padding: clamp(4px, calc(3.7 * var(--u)), 20px);
    overflow-y: auto;
    overscroll-behavior: contain;
    transition: opacity 0.25s ease;
}

.overlay.hidden {
    opacity: 0;
    pointer-events: none;
    display: none;
}

.modal-card {
    background: var(--bg-surface-trans);
    border: 2px solid var(--accent-cyan);
    box-shadow: 0 0 35px var(--accent-cyan-dim);
    border-radius: 14px;
    width: 100%;
    max-width: 580px;
    padding: clamp(8px, calc(5.2 * var(--u)), 28px) clamp(10px, calc(5.9 * var(--u)), 32px);
    /* auto margins centre the card but keep its top reachable when it overflows */
    margin: auto;
    text-align: center;
    position: relative;
}

.modal-view.hidden {
    display: none;
}

/* Badges & Titles */
.badge-tag {
    display: inline-block;
    font-size: clamp(9px, calc(2.04 * var(--u)), 11px);
    font-weight: 800;
    letter-spacing: 2px;
    padding: 3px 12px;
    border-radius: 20px;
    background: var(--accent-cyan-dim);
    color: var(--accent-cyan);
    border: 1px solid var(--accent-cyan);
    margin-bottom: clamp(4px, calc(2.2 * var(--u)), 12px);
}

.badge-tag.danger-tag {
    background: rgba(255, 59, 105, 0.2);
    color: var(--accent-red);
    border-color: var(--accent-red);
}

.game-title {
    font-size: clamp(18px, calc(7 * var(--u)), 38px);
    font-weight: 900;
    letter-spacing: 2px;
    margin-bottom: clamp(2px, calc(1.1 * var(--u)), 6px);
    text-transform: uppercase;
}

.cyan-accent {
    color: var(--accent-cyan);
    text-shadow: 0 0 16px var(--accent-cyan);
}

.tagline {
    font-size: clamp(10px, calc(2.6 * var(--u)), 14px);
    color: var(--text-muted);
    margin-bottom: clamp(6px, calc(3.7 * var(--u)), 20px);
}

.modal-title {
    font-size: clamp(18px, calc(5.9 * var(--u)), 32px);
    font-weight: 900;
    letter-spacing: 1px;
    margin-bottom: clamp(4px, calc(1.85 * var(--u)), 10px);
}

.gameover-title {
    color: var(--accent-red);
    text-shadow: 0 0 16px rgba(255, 59, 105, 0.4);
}

.modal-sub {
    font-size: clamp(10px, calc(2.6 * var(--u)), 14px);
    color: var(--text-muted);
    margin-bottom: clamp(6px, calc(4.4 * var(--u)), 24px);
}

/* Legend cards */
.legend-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: clamp(4px, calc(2.2 * var(--u)), 12px);
    margin-bottom: clamp(4px, calc(3 * var(--u)), 16px);
    text-align: left;
}

.legend-card {
    font-size: clamp(9.5px, calc(2.2 * var(--u)), 12px);
    line-height: 1.4;
    padding: clamp(3px, calc(1.85 * var(--u)), 10px) clamp(4px, calc(2.2 * var(--u)), 12px);
    border-radius: 8px;
    background: #14161c;
}

.legend-card.hazard {
    border-left: 3px solid var(--accent-red);
    color: #ffd8df;
}

.legend-card.bonus {
    border-left: 3px solid var(--accent-cyan);
    color: #e0f8ff;
}

.controls-guide {
    display: flex;
    flex-direction: column;
    gap: clamp(2px, calc(1.1 * var(--u)), 6px);
    background: #13141a;
    border: 1px dashed rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    padding: clamp(3px, calc(1.85 * var(--u)), 10px);
    font-size: clamp(9.5px, calc(2.2 * var(--u)), 12px);
    color: var(--text-muted);
    margin-bottom: clamp(4px, calc(4.4 * var(--u)), 24px);
}

.controls-guide strong {
    color: var(--text-main);
}

/* Stats summary */
.stats-summary {
    display: flex;
    justify-content: center;
    gap: clamp(8px, calc(3.7 * var(--u)), 20px);
    margin-bottom: clamp(6px, calc(4.4 * var(--u)), 24px);
}

.stat-box {
    background: #131419;
    border: 1px solid rgba(63, 224, 255, 0.3);
    border-radius: 8px;
    padding: clamp(4px, calc(2.6 * var(--u)), 14px) clamp(8px, calc(3.7 * var(--u)), 20px);
    min-width: min(140px, calc(26 * var(--u)));
}

.stat-label {
    display: block;
    font-size: clamp(9px, calc(2.04 * var(--u)), 11px);
    font-weight: 700;
    letter-spacing: 1px;
    color: var(--accent-cyan);
    margin-bottom: clamp(2px, calc(0.75 * var(--u)), 4px);
}

.stat-number {
    font-size: clamp(16px, calc(5.2 * var(--u)), 28px);
    font-weight: 800;
    color: #ffffff;
    font-variant-numeric: tabular-nums;
}

.new-record {
    background: rgba(255, 170, 0, 0.15);
    border: 1px solid var(--accent-amber);
    color: var(--accent-amber);
    padding: clamp(3px, calc(1.1 * var(--u)), 6px) clamp(10px, calc(3 * var(--u)), 16px);
    border-radius: 20px;
    font-weight: 800;
    font-size: clamp(10px, calc(2.4 * var(--u)), 13px);
    letter-spacing: 1px;
    display: inline-block;
    margin-bottom: clamp(4px, calc(3 * var(--u)), 16px);
    animation: pulseRecord 1.5s infinite;
}

.new-record.hidden {
    display: none;
}

@keyframes pulseRecord {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.04); opacity: 0.85; }
}

/* Buttons */
.btn-cluster {
    display: flex;
    flex-direction: column;
    gap: clamp(4px, calc(2.2 * var(--u)), 12px);
    width: 100%;
}

.cyber-btn {
    width: 100%;
    padding: clamp(5px, calc(2.6 * var(--u)), 14px) clamp(12px, calc(4.4 * var(--u)), 24px);
    border-radius: 8px;
    font-size: clamp(11px, calc(3 * var(--u)), 16px);
    font-weight: 800;
    letter-spacing: 1.5px;
    text-transform: uppercase;
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.2s, background 0.2s;
    outline: none;
}

.cyber-btn.primary-btn {
    background: var(--accent-cyan);
    color: #0b0c0f;
    border: 1px solid var(--accent-cyan);
    box-shadow: 0 4px 20px var(--accent-cyan-dim);
}

.cyber-btn.primary-btn:hover {
    background: var(--accent-cyan-hover);
    transform: translateY(-2px);
    box-shadow: 0 6px 24px rgba(63, 224, 255, 0.45);
}

.cyber-btn.primary-btn:active {
    transform: translateY(1px);
}

.cyber-btn.secondary-btn {
    background: rgba(26, 28, 35, 0.8);
    color: var(--text-main);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.cyber-btn.secondary-btn:hover {
    border-color: var(--accent-cyan);
    color: var(--accent-cyan);
    background: rgba(63, 224, 255, 0.08);
}

/* Focus Visible for Keyboard Accessibility */
.cyber-btn:focus-visible {
    outline: 3px solid var(--accent-cyan);
    outline-offset: 3px;
    box-shadow: 0 0 15px var(--accent-cyan);
}

/* Reduced Motion preference */
@media (prefers-reduced-motion: reduce) {
    *, .cyber-btn, .hud-btn, .new-record {
        animation: none !important;
        transition: none !important;
    }
}

/* Mobile & Small screens */
@media (max-width: 640px) {
    #game-container {
        border-radius: 0;
        border-left: none;
        border-right: none;
    }

    /* Modal card, title, legend and stats now scale with --u (see base rules);
       the old single-column legend made the menu ~410px tall in a ~190px box. */

    .hud-label {
        font-size: 9px;
    }

    .hud-num {
        font-size: 15px;
    }
}

/* Short game box (16/10 host iframe below ~600x380, or narrow standalone):
   drop decorative lines and put modal buttons side by side so the menu fits */
@media (max-height: 380px), (max-width: 600px) {
    .badge-tag,
    .tagline {
        display: none;
    }

    .btn-cluster {
        flex-direction: row;
        flex-wrap: wrap;
    }

    .cyber-btn {
        width: auto;
        flex: 1 1 auto;
    }
}

/* Smallest frames (about 320x180 to 375x210) */
@media (max-height: 210px), (max-width: 375px) {
    .overlay {
        padding: 4px;
    }

    .game-title,
    .modal-title {
        font-size: 16px;
    }

    .legend-card,
    .controls-guide {
        font-size: 9px;
        line-height: 1.2;
    }
}

/* Compact HUD so the pause / sound buttons are not pushed out of the box */
@media (max-width: 440px) {
    #hud {
        padding: 4px 8px;
    }

    .hud-group {
        gap: 6px;
    }

    .hud-item {
        min-width: 0;
        padding: 1px 6px;
    }

    .hud-label {
        font-size: 8px;
    }

    .hud-num {
        font-size: 12px;
    }

    .lives-icons {
        gap: 4px;
        margin-top: 1px;
        height: 12px;
    }

    .life-pip {
        width: 10px;
        height: 10px;
    }

    .hud-btn {
        width: 28px;
        height: 28px;
        font-size: 13px;
    }
}
