:root {
    --bg-darker: #020617;
    --glass-bg: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(255, 255, 255, 0.1);
    --neon-blue:  #38bdf8;
    --neon-pink:  #f472b6;
    --neon-green: #4ade80;
    --text-main:  #f8fafc;
    --text-muted: #94a3b8;
    --color-z: #f00000;
    --color-o: #f0f000;
    /* Fixed touch-bar height — used in JS too (FIXED_BAR_H = 113) */
    --touch-bar-h: 113px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html { overflow: hidden; height: 100%; }

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-darker);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

/* Animated Background */
.background-animation {
    position: fixed; top: 0; left: 0;
    width: 100vw; height: 100vh; z-index: -1;
    background: radial-gradient(circle at center, #1e1b4b 0%, var(--bg-darker) 100%);
}
.background-animation::before {
    content: '';
    position: absolute;
    width: 200%; height: 200%; top: -50%; left: -50%;
    background-image:
        linear-gradient(rgba(255,255,255,0.03) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255,255,255,0.03) 1px, transparent 1px);
    background-size: 50px 50px;
    animation: gridMove 20s linear infinite;
}
@keyframes gridMove {
    from { transform: perspective(500px) rotateX(60deg) translateY(0); }
    to   { transform: perspective(500px) rotateX(60deg) translateY(50px); }
}

/* ════════════════════════════
   DESKTOP LAYOUT
════════════════════════════ */
.game-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    padding: 20px;
    max-width: 900px;
    width: 100%;
}

header { text-align: center; margin-bottom: 10px; }

h1.neon-text {
    font-family: 'Orbitron', sans-serif;
    font-size: 3rem; font-weight: 900; color: #fff;
    text-shadow: 0 0 5px #fff, 0 0 10px #fff,
        0 0 20px var(--neon-blue), 0 0 40px var(--neon-blue), 0 0 80px var(--neon-blue);
    letter-spacing: 4px;
}

/* Hidden on desktop */
.mobile-stats-bar  { display: none; }
.mobile-start-btn  { display: none; }

.container {
    display: flex;
    flex-direction: row;
    gap: 30px;
    align-items: flex-start;
    justify-content: center;
}

.glass-panel {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.37);
    padding: 20px;
}

.game-board-container {
    position: relative;
    padding: 10px;
    box-shadow: 0 0 10px rgba(56,189,248,0.2), inset 0 0 20px rgba(0,0,0,0.8);
    background: rgba(0,0,0,0.6);
}

canvas#tetris-canvas {
    background-color: rgba(0,0,0,0.8);
    display: block; border-radius: 4px;
    box-shadow: inset 0 0 10px #000;
}

/* Overlays */
.overlay {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.85); backdrop-filter: blur(4px);
    display: flex; flex-direction: column;
    justify-content: center; align-items: center;
    border-radius: 16px; z-index: 10;
}
.overlay.hidden { display: none; }
.overlay h2 { font-family: 'Orbitron', sans-serif; font-size: 2.5rem; margin-bottom: 10px; }
.overlay h2.error {
    color: #fff;
    text-shadow: 0 0 10px var(--color-z), 0 0 20px var(--color-z), 0 0 40px var(--color-z);
    font-size: 2.2rem;
}
.overlay h2.paused-text {
    color: #fff;
    text-shadow: 0 0 10px var(--color-o), 0 0 20px var(--color-o);
}
.overlay p { font-size: 1.2rem; margin-bottom: 20px; color: var(--text-muted); }
.overlay #final-score { color: var(--neon-blue); font-weight: 700; font-size: 1.5rem; }

/* Side Panels */
.left-panel, .right-panel { display: flex; flex-direction: column; gap: 20px; width: 200px; }
.panel-section h3 {
    font-family: 'Orbitron', sans-serif; font-size: 1.2rem;
    margin-bottom: 15px; text-align: center; color: var(--text-muted); letter-spacing: 2px;
}
.next-piece-section { display: flex; flex-direction: column; align-items: center; }
canvas#next-canvas { background-color: rgba(0,0,0,0.5); border-radius: 8px; }
.stats-section { display: flex; flex-direction: column; gap: 15px; }
.stat-box {
    display: flex; flex-direction: column; align-items: center;
    background: rgba(0,0,0,0.3); padding: 10px; border-radius: 8px;
    border: 1px solid rgba(255,255,255,0.05);
}
.stat-label { font-size: 0.8rem; color: var(--text-muted); margin-bottom: 5px; letter-spacing: 1px; }
.stat-value { font-family: 'Orbitron', sans-serif; font-size: 1.8rem; font-weight: 700; }
.neon-blue  { color: var(--neon-blue);  text-shadow: 0 0 10px var(--neon-blue); }
.neon-pink  { color: var(--neon-pink);  text-shadow: 0 0 10px var(--neon-pink); }
.neon-green { color: var(--neon-green); text-shadow: 0 0 10px var(--neon-green); }

.controls-section { display: flex; flex-direction: column; gap: 15px; }
.neon-btn {
    font-family: 'Orbitron', sans-serif; font-size: 1rem; font-weight: 700;
    padding: 12px 20px; border: none; border-radius: 8px; cursor: pointer;
    transition: all 0.2s ease; text-transform: uppercase; letter-spacing: 1px;
    touch-action: manipulation;
}
.neon-btn:disabled { opacity: 0.5; cursor: not-allowed; box-shadow: none !important; }
.neon-btn.primary {
    background: transparent; color: var(--neon-blue);
    border: 2px solid var(--neon-blue);
    box-shadow: inset 0 0 10px rgba(56,189,248,0.2), 0 0 10px rgba(56,189,248,0.2);
}
.neon-btn.primary:hover:not(:disabled) { background: var(--neon-blue); color: #000; box-shadow: 0 0 20px var(--neon-blue); }
.neon-btn.secondary { background: transparent; color: var(--text-main); border: 2px solid var(--text-muted); }
.neon-btn.secondary:hover:not(:disabled) { border-color: #fff; box-shadow: 0 0 10px rgba(255,255,255,0.5); }
#btn-restart { background: var(--neon-blue); color: #000; border: none; box-shadow: 0 0 15px var(--neon-blue); }
#btn-restart:hover { box-shadow: 0 0 30px var(--neon-blue); }
.help-text { margin-top: 15px; font-size: 0.85rem; color: var(--text-muted); }
.help-text p { margin-bottom: 8px; display: flex; align-items: center; gap: 6px; }
kbd {
    background: rgba(255,255,255,0.1); border: 1px solid rgba(255,255,255,0.2);
    border-radius: 4px; padding: 2px 6px;
    font-family: monospace; font-size: 0.8rem; color: #fff;
    box-shadow: 0 2px 0 rgba(0,0,0,0.5);
}

/* Touch Controls — hidden on desktop */
.touch-controls { display: none; }
.touch-row { display: flex; gap: 6px; width: 100%; }

.touch-btn {
    flex: 1;
    height: 52px;
    background: rgba(56,189,248,0.08);
    border: 2px solid rgba(56,189,248,0.35);
    border-radius: 10px;
    color: var(--neon-blue);
    font-family: 'Orbitron', sans-serif;
    font-size: 1.5rem;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    user-select: none; -webkit-user-select: none;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.08s, box-shadow 0.08s, transform 0.06s;
}
.touch-btn:hover { background: rgba(56,189,248,0.2); box-shadow: 0 0 12px rgba(56,189,248,0.4); }
.touch-btn:active { background: rgba(56,189,248,0.32); transform: scale(0.93); box-shadow: 0 0 16px rgba(56,189,248,0.6); }

.touch-btn.touch-rotate { background: rgba(164,0,240,0.1); border-color: rgba(164,0,240,0.5); color: var(--neon-pink); font-size: 1.9rem; }
.touch-btn.touch-rotate:hover { background: rgba(164,0,240,0.22); }
.touch-btn.touch-rotate:active { background: rgba(164,0,240,0.35); box-shadow: 0 0 16px rgba(164,0,240,0.6); }

.touch-btn.touch-hard { background: rgba(240,50,50,0.08); border-color: rgba(240,50,50,0.4); color: #f87171; }
.touch-btn.touch-hard:hover { background: rgba(240,50,50,0.2); }
.touch-btn.touch-hard:active { background: rgba(240,50,50,0.32); box-shadow: 0 0 16px rgba(240,50,50,0.5); }

.touch-btn.touch-soft {
    height: 43px; font-size: 0.8rem; letter-spacing: 1px;
    background: rgba(0,240,0,0.07); border-color: rgba(0,240,0,0.32); color: var(--neon-green);
}
.touch-btn.touch-soft:hover { background: rgba(0,240,0,0.18); }
.touch-btn.touch-soft:active { background: rgba(0,240,0,0.28); box-shadow: 0 0 14px rgba(0,240,0,0.4); }

/* ════════════════════════════════════════════
   MOBILE  ≤ 640px

   Layout (top → bottom):
     header      ~17px
     stats-bar   ~70px  (includes 48px next-canvas-m)
     start-btn   ~36px
     canvas      ← JS sizes to fill remaining height
     ── fixed touch-bar ──  height: 113px (var(--touch-bar-h))
════════════════════════════════════════════ */
@media (max-width: 640px) {
    :root { --touch-bar-h: 64px; }
    /* No body scroll — everything sized to fit viewport */
    body {
        overflow: hidden;
        align-items: flex-start;
        height: 100dvh;
        height: 100vh;
    }

    .game-wrapper {
        padding: 4px 4px 0;
        gap: 4px;
        align-items: stretch;
        width: 100%;
        /* Height = viewport minus the fixed touch bar */
        height: calc(100dvh - var(--touch-bar-h));
        height: calc(100vh - var(--touch-bar-h));
        overflow: hidden;
    }

    header { margin: 0; text-align: center; }
    h1.neon-text { font-size: 0.90rem; letter-spacing: 2px; line-height: 1.2; }

    /* Stats strip — single compact line: SCORE 0 | LEVEL 1 | LINES 0 */
    .mobile-stats-bar {
        display: flex;
        align-items: center;
        justify-content: center;
        background: var(--glass-bg);
        border: 1px solid var(--glass-border);
        border-radius: 8px;
        padding: 3px 8px;
        flex-shrink: 0;
        gap: 12px;
    }
    .msb-stats {
        display: flex;
        flex: 1;
        justify-content: space-around;
        align-items: center;
    }
    /* Each item: label + value in a single horizontal row */
    .msb-item { display: flex; flex-direction: row; align-items: center; gap: 4px; }
    .msb-label { font-size: 0.55rem; color: var(--text-muted); letter-spacing: 0.5px; font-family: 'Orbitron', sans-serif; }
    .msb-value { font-family: 'Orbitron', sans-serif; font-size: 0.85rem; font-weight: 700; line-height: 1; }
    .msb-next {
        display: flex;
        flex-direction: column;
        align-items: center;
        background: rgba(0, 0, 0, 0.4);
        padding: 4px 8px;
        border-radius: 8px;
        border: 1px solid rgba(255, 255, 255, 0.05);
        gap: 2px;
    }
    canvas#next-canvas-m {
        background-color: transparent;
        display: block;
        width: 48px;
        height: 48px;
    }

    /* START / PAUSE button — above canvas, always visible */
    .mobile-start-btn {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 36px;
        flex-shrink: 0;
        font-family: 'Orbitron', sans-serif;
        font-size: 0.85rem;
        font-weight: 700;
        letter-spacing: 2px;
        color: var(--neon-blue);
        background: rgba(56,189,248,0.12);
        border: 2px solid var(--neon-blue);
        border-radius: 8px;
        cursor: pointer;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
        box-shadow: inset 0 0 10px rgba(56,189,248,0.1), 0 0 10px rgba(56,189,248,0.15);
        transition: background 0.1s, box-shadow 0.1s;
    }
    .mobile-start-btn:hover { background: rgba(56,189,248,0.25); box-shadow: 0 0 20px rgba(56,189,248,0.4); }
    .mobile-start-btn:active { background: rgba(56,189,248,0.38); transform: scale(0.97); }
    .mobile-start-btn.running {
        border-color: var(--neon-pink); color: var(--neon-pink);
        background: rgba(244,114,182,0.1);
        box-shadow: 0 0 10px rgba(244,114,182,0.2);
    }
    .mobile-start-btn.paused {
        border-color: var(--neon-green); color: var(--neon-green);
        background: rgba(74,222,128,0.1);
        box-shadow: 0 0 10px rgba(74,222,128,0.2);
    }

    /* Hide desktop-only elements */
    .desktop-only { display: none !important; }

    /* Game area row */
    .container {
        display: flex;
        flex-direction: row;
        gap: 4px;
        align-items: flex-start;
        justify-content: center;
        width: 100%;
        position: relative;
        padding-right: 76px; /* 64px width + 12px gap */
        flex: 1;          /* fill remaining height in game-wrapper */
        overflow: hidden;
        min-height: 0;
    }

    .game-board-container {
        padding: 3px;
        border-radius: 8px;
        flex-shrink: 0;
        background: rgba(0,0,0,0.7);
        box-shadow: 0 0 8px rgba(56,189,248,0.15);
    }

    /* Side panels */
    .left-panel {
        display: none !important;
    }
    .right-panel {
        display: flex !important;
        flex-direction: column;
        gap: 8px;
        width: 64px;
        flex-shrink: 0;
        position: absolute;
        right: 4px;
        top: 0;
    }

    .glass-panel { padding: 5px; border-radius: 8px; }
    .panel-section h3 { font-size: 0.55rem; margin-bottom: 4px; letter-spacing: 1px; }
    canvas#next-canvas { border-radius: 5px; width: 100%; height: auto; display: block; }

    /* Touch controls: FIXED at bottom of viewport, always visible */
    .touch-controls {
        display: flex;
        flex-direction: column;
        gap: 5px;
        position: fixed;
        bottom: 0; left: 0; right: 0;
        z-index: 1000;
        padding: 6px 5px;
        background: rgba(2, 6, 23, 0.97);
        border-top: 1px solid rgba(56,189,248,0.15);
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        /* height = 6+52+5+43+6 = 112px ≈ var(--touch-bar-h: 113px) */
    }
    .touch-row { gap: 5px; }
}

/* Extra-small phones ≤ 390px
   JS uses SIDE_CSS_W=64px for these */
@media (max-width: 390px) {
    .game-wrapper { gap: 3px; padding: 3px 3px 0; }
    h1.neon-text { font-size: 0.78rem; letter-spacing: 1.5px; }
    .mobile-stats-bar { padding: 2px 6px; }
    .msb-value { font-size: 0.76rem; }
    .msb-label { font-size: 0.50rem; }
    .mobile-start-btn { height: 38px; font-size: 0.78rem; }
    .container { padding-right: 64px; }
    .glass-panel { padding: 4px; }
    .touch-controls { gap: 4px; padding: 5px 4px; }
    .touch-row { gap: 4px; }
    .touch-btn { height: 48px; font-size: 1.35rem; }
    .touch-btn.touch-rotate { font-size: 1.65rem; }
    .right-panel { width: 56px !important; }
    /* Recalculate --touch-bar-h for smaller buttons: 5+48+5 = 58px */
    :root { --touch-bar-h: 58px; }
    .game-wrapper {
        height: calc(100dvh - 58px);
        height: calc(100vh - 58px);
    }
}

/* Landscape mobile */
@media (max-height: 480px) and (orientation: landscape) {
    body { overflow: hidden; align-items: flex-start; }
    .game-wrapper {
        flex-direction: row;
        flex-wrap: wrap;
        padding: 4px;
        gap: 4px;
        height: auto;
        align-items: flex-start;
    }
    header { width: 100%; }
    h1.neon-text { font-size: 0.9rem; letter-spacing: 1px; }
    .mobile-stats-bar {
        display: flex;
        width: 100%;
        padding: 3px 8px;
    }
    .mobile-start-btn {
        display: flex;
        width: 100%;
        height: 34px;
        font-size: 0.7rem;
    }
    .container { flex-shrink: 0; }
    .touch-controls {
        display: flex;
        position: static;
        flex-direction: column;
        width: 160px;
        gap: 4px;
        padding: 0;
        background: transparent;
        border: none;
        backdrop-filter: none;
    }
    .touch-row { flex-direction: column; }
    .touch-btn { height: 42px; font-size: 1.2rem; }
    .touch-btn.touch-soft { height: 36px; font-size: 0.7rem; }
}

/* ════════════════════════════
   RANKINGS MODAL & RECORD FORM
════════════════════════════ */
.modal {
    position: fixed;
    top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    opacity: 1;
    transition: opacity 0.3s;
}

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

.ranking-content {
    width: 90vw;
    max-width: 500px;
    padding: 2rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
}

.title-center {
    text-align: center;
    font-family: 'Orbitron', sans-serif;
    letter-spacing: 1px;
    font-size: 1.8rem !important;
}

.rankings-table-wrapper {
    width: 100%;
    max-height: 280px;
    overflow-y: auto;
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.4);
}

.rankings-table {
    width: 100%;
    border-collapse: collapse;
    font-family: 'Orbitron', sans-serif;
    color: var(--text-main);
}

.rankings-table th, .rankings-table td {
    padding: 10px 8px;
    text-align: center;
    font-size: 0.85rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.rankings-table th {
    font-size: 0.75rem;
    color: var(--neon-blue);
    background: rgba(56, 189, 248, 0.08);
    letter-spacing: 1px;
    position: sticky;
    top: 0;
}

.rankings-table tr:hover td {
    background: rgba(255, 255, 255, 0.02);
}

.rankings-table tr.highlight td {
    color: var(--neon-pink);
    font-weight: bold;
    text-shadow: 0 0 5px var(--neon-pink);
}

/* Record Form in GameOver Overlay */
#record-form-container {
    margin-top: 0.8rem;
    margin-bottom: 0.8rem;
    width: 100%;
    max-width: 240px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.4rem;
}

#record-form-container.hidden {
    display: none !important;
}

.record-input-title {
    font-size: 0.75rem;
    color: var(--neon-blue);
    font-family: 'Orbitron', sans-serif;
    letter-spacing: 1px;
    text-shadow: 0 0 5px var(--neon-blue);
}

.input-wrapper {
    display: flex;
    gap: 8px;
    width: 100%;
    justify-content: center;
}

#player-name {
    background: rgba(56, 189, 248, 0.05);
    border: 1px solid rgba(56, 189, 248, 0.3);
    border-radius: 6px;
    color: #fff;
    padding: 6px 10px;
    font-family: 'Orbitron', sans-serif;
    font-weight: bold;
    font-size: 0.9rem;
    width: 130px;
    text-align: center;
    outline: none;
    text-transform: uppercase;
    transition: all 0.3s ease;
}

#player-name:focus {
    border-color: var(--neon-blue);
    box-shadow: 0 0 10px rgba(56, 189, 248, 0.3);
    background: rgba(56, 189, 248, 0.1);
}

/* Mobile Controls modifications */
.mobile-control-row {
    display: flex;
    gap: 8px;
    width: 100%;
    flex-shrink: 0;
}

.mobile-top10-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 36px;
    flex-shrink: 0;
    font-family: 'Orbitron', sans-serif;
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 1px;
    color: var(--neon-pink);
    background: rgba(244,114,182,0.12);
    border: 2px solid var(--neon-pink);
    border-radius: 8px;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    box-shadow: inset 0 0 10px rgba(244,114,182,0.1), 0 0 10px rgba(244,114,182,0.15);
    transition: background 0.1s, box-shadow 0.1s, transform 0.05s;
}

.mobile-top10-btn:hover { background: rgba(244,114,182,0.25); box-shadow: 0 0 20px rgba(244,114,182,0.4); }
.mobile-top10-btn:active { background: rgba(244,114,182,0.38); transform: scale(0.97); }

/* Override mobile start button to share horizontal row */
@media (max-width: 640px) {
    .mobile-start-btn {
        flex: 2 !important;
        width: auto !important;
    }
}

