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

:root {
    --accent: #e8c547;
    --accent2: #7ec850;
    --bg-dark: #0c0c14;
    --panel-bg: rgba(232, 197, 71, 0.06);
    --border: #e8c547;
    --text-muted: #b8b0a0;
    --danger: #e74c3c;
}

html, body {
    height: 100%;
}

body {
    font-family: 'Press Start 2P', cursive;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 16px 12px 32px;
    transition: background 0.35s ease;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    background: linear-gradient(160deg, #1a1a2e 0%, #0f0f1a 50%, #16213e 100%);
    color: var(--accent);
}

body.theme-classic {
    --accent: #e8c547;
    --accent2: #7ec850;
    --bg-dark: #0c0c14;
    --panel-bg: rgba(232, 197, 71, 0.06);
    --border: #e8c547;
    background: linear-gradient(160deg, #1a1a2e 0%, #0f0f1a 50%, #16213e 100%);
}

body.theme-gray {
    --accent: #00d4ff;
    --accent2: #00ff88;
    --bg-dark: #0a0e27;
    --panel-bg: rgba(0, 212, 255, 0.05);
    --border: #00d4ff;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

body.theme-green {
    --accent: #00ff9d;
    --accent2: #39ff14;
    --bg-dark: #0a1a14;
    --panel-bg: rgba(0, 255, 157, 0.06);
    --border: #00ff9d;
    background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
}

body.theme-blue {
    --accent: #00bfff;
    --accent2: #00ffff;
    --bg-dark: #0a1220;
    --panel-bg: rgba(0, 191, 255, 0.06);
    --border: #00bfff;
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

body.theme-wheat {
    --accent: #ff6ec7;
    --accent2: #ff8c00;
    --bg-dark: #1a0a14;
    --panel-bg: rgba(255, 110, 199, 0.06);
    --border: #ff6ec7;
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

body.theme-arcade {
    --accent: #ffdd00;
    --accent2: #ff6b00;
    --bg-dark: #1a0a0a;
    --panel-bg: rgba(255, 221, 0, 0.06);
    --border: #ffdd00;
    background: linear-gradient(135deg, #3a0a0a 0%, #1a0505 100%);
}

body.theme-dark {
    --accent: #00ffcc;
    --accent2: #00ffaa;
    --bg-dark: #0d0d1a;
    --panel-bg: rgba(0, 255, 204, 0.05);
    --border: #00ffcc;
    background: linear-gradient(135deg, #0f0f23 0%, #1a1a2e 100%);
}

.game-container {
    background: rgba(8, 8, 16, 0.94);
    border: 3px solid var(--border);
    border-radius: 16px;
    padding: 20px 22px 16px;
    box-shadow:
        0 0 0 1px rgba(0, 0, 0, 0.4),
        0 12px 40px rgba(0, 0, 0, 0.45),
        0 0 24px color-mix(in srgb, var(--accent) 25%, transparent);
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 720px;
    backdrop-filter: blur(8px);
}

.game-header {
    text-align: center;
    margin-bottom: 16px;
}

.game-header h1 {
    font-size: 22px;
    letter-spacing: 6px;
    color: var(--accent);
    text-shadow: 0 0 12px color-mix(in srgb, var(--accent) 55%, transparent);
    line-height: 1.3;
}

.game-header .subtitle {
    margin-top: 8px;
    font-size: 8px;
    letter-spacing: 2px;
    color: var(--text-muted);
    opacity: 0.85;
}

.game-main {
    display: flex;
    gap: 22px;
    align-items: flex-start;
    flex-wrap: wrap;
    justify-content: center;
    width: 100%;
}

.canvas-wrapper {
    border: 3px solid var(--border);
    border-radius: 8px;
    background: #0a0a12;
    line-height: 0;
    box-shadow:
        inset 0 0 20px rgba(0, 0, 0, 0.85),
        0 0 16px color-mix(in srgb, var(--accent) 18%, transparent);
    position: relative;
    overflow: hidden;
    flex-shrink: 0;
}

.canvas-wrapper.clickable-board {
    cursor: pointer;
}

.canvas-wrapper::after {
    content: '';
    position: absolute;
    inset: 0;
    background: repeating-linear-gradient(
        to bottom,
        transparent 0,
        transparent 2px,
        rgba(0, 0, 0, 0.12) 2px,
        rgba(0, 0, 0, 0.12) 3px
    );
    pointer-events: none;
    z-index: 1;
    opacity: 0.55;
}

#gameCanvas {
    display: block;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

.side-panel {
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-width: 168px;
    max-width: 220px;
    flex: 1 1 168px;
    align-items: stretch;
}

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.panel-box {
    background: var(--panel-bg);
    border: 2px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
    text-align: center;
    box-shadow: inset 0 0 10px color-mix(in srgb, var(--accent) 8%, transparent);
    transition: box-shadow 0.2s ease;
}

.panel-box h3 {
    font-size: 8px;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    color: var(--accent);
    margin-bottom: 6px;
    opacity: 0.95;
}

.panel-box p {
    font-size: 14px;
    color: var(--accent2);
    text-shadow: 0 0 8px color-mix(in srgb, var(--accent2) 50%, transparent);
    word-break: break-all;
}

#nextCanvas {
    background: #0a0a12;
    border: 2px solid var(--border);
    border-radius: 6px;
    display: block;
    margin: 0 auto;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

.color-swatches {
    display: flex;
    gap: 8px;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 4px;
}

.swatch {
    width: 22px;
    height: 22px;
    border: 2px solid var(--border);
    border-radius: 5px;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s, outline 0.15s;
}

.swatch:hover {
    box-shadow: 0 0 10px color-mix(in srgb, var(--accent) 45%, transparent);
}

.swatch:active {
    transform: scale(0.88);
}

.board-swatch.active {
    outline: 2px solid var(--accent2);
    outline-offset: 2px;
    box-shadow: 0 0 10px color-mix(in srgb, var(--accent2) 45%, transparent);
}

.speed-controls {
    display: flex;
    gap: 6px;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 6px;
}

.speed-btn {
    font-family: 'Press Start 2P', cursive;
    font-size: 7px;
    padding: 6px 8px;
    background: rgba(0, 0, 0, 0.35);
    border: 2px solid var(--border);
    border-radius: 6px;
    color: var(--text-muted);
    cursor: pointer;
    transition: background 0.15s, color 0.15s, box-shadow 0.15s, border-color 0.15s;
    letter-spacing: 0.5px;
}

.speed-btn:hover {
    color: var(--accent);
}

.speed-btn:active {
    transform: translate(1px, 1px);
}

.speed-btn.active {
    background: color-mix(in srgb, var(--accent2) 28%, transparent);
    border-color: var(--accent2);
    color: var(--accent2);
    box-shadow: 0 0 12px color-mix(in srgb, var(--accent2) 35%, transparent);
}

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
}

#startButton,
#pauseButton {
    font-family: 'Press Start 2P', cursive;
    font-size: 11px;
    border: 2px solid var(--border);
    border-radius: 10px;
    padding: 12px 14px;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: transform 0.12s, box-shadow 0.2s, filter 0.2s;
    width: 100%;
}

#startButton {
    background: linear-gradient(180deg, var(--accent), color-mix(in srgb, var(--accent) 70%, #000));
    color: #0c0c14;
    box-shadow: 0 4px 14px color-mix(in srgb, var(--accent) 40%, transparent);
    font-weight: bold;
}

#pauseButton {
    background: linear-gradient(180deg, var(--accent2), color-mix(in srgb, var(--accent2) 65%, #000));
    color: #0c0c14;
    box-shadow: 0 4px 14px color-mix(in srgb, var(--accent2) 35%, transparent);
}

#startButton:hover,
#pauseButton:hover {
    filter: brightness(1.08);
}

#startButton:active,
#pauseButton:active {
    transform: translateY(2px);
}

.hidden {
    display: none !important;
}

.touch-controls {
    display: none;
    flex-direction: column;
    gap: 10px;
    margin-top: 18px;
    align-items: center;
    width: 100%;
}

body.touch-device .touch-controls {
    display: flex;
}

body.touch-device .hint-desktop {
    display: none;
}

body:not(.touch-device) .hint-touch {
    display: none;
}

.control-row {
    display: flex;
    gap: 10px;
    justify-content: center;
    width: 100%;
    max-width: 320px;
}

.ctrl-btn {
    font-family: 'Press Start 2P', cursive;
    font-size: 16px;
    width: 64px;
    height: 64px;
    border: 2px solid var(--border);
    background: linear-gradient(180deg, color-mix(in srgb, var(--accent) 22%, transparent), rgba(0, 0, 0, 0.35));
    color: var(--accent);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow:
        0 4px 0 color-mix(in srgb, var(--border) 55%, #000),
        0 6px 16px rgba(0, 0, 0, 0.35);
    touch-action: manipulation;
    cursor: pointer;
    transition: transform 0.08s, background 0.15s, color 0.15s;
    flex-shrink: 0;
}

.ctrl-btn.wide {
    flex: 1;
    max-width: 120px;
}

.ctrl-btn:active {
    transform: translateY(3px);
    box-shadow:
        0 1px 0 color-mix(in srgb, var(--border) 55%, #000),
        0 2px 8px rgba(0, 0, 0, 0.3);
    background: linear-gradient(180deg, color-mix(in srgb, var(--accent2) 30%, transparent), rgba(0, 0, 0, 0.3));
    color: var(--accent2);
}

.controls-hint {
    margin-top: 14px;
    text-align: center;
    opacity: 0.75;
    max-width: 100%;
}

.controls-hint small {
    font-size: 7px;
    color: var(--text-muted);
    letter-spacing: 0.5px;
    line-height: 1.6;
    display: block;
}

.toast {
    position: fixed;
    left: 50%;
    bottom: 28px;
    transform: translateX(-50%) translateY(20px);
    max-width: min(90vw, 360px);
    padding: 14px 18px;
    background: rgba(12, 12, 20, 0.96);
    border: 2px solid var(--danger);
    border-radius: 10px;
    color: #fff;
    font-family: 'Press Start 2P', cursive;
    font-size: 8px;
    line-height: 1.7;
    text-align: center;
    letter-spacing: 0.5px;
    box-shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
    z-index: 1000;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

@media (max-width: 640px) {
    body {
        padding: 10px 8px 24px;
        align-items: flex-start;
    }

    .game-container {
        padding: 14px 12px 12px;
        border-radius: 12px;
        max-width: 100%;
    }

    .game-header h1 {
        font-size: 16px;
        letter-spacing: 4px;
    }

    .game-header .subtitle {
        font-size: 7px;
    }

    .game-main {
        flex-direction: column;
        align-items: center;
        gap: 14px;
    }

    .side-panel {
        max-width: 100%;
        width: 100%;
        min-width: 0;
    }

    .stats-grid {
        grid-template-columns: 1fr 1fr;
    }

    .ctrl-btn {
        width: 58px;
        height: 58px;
        font-size: 14px;
    }

    .panel-box p {
        font-size: 12px;
    }
}

@media (max-width: 380px) {
    .game-header h1 {
        font-size: 14px;
        letter-spacing: 2px;
    }

    .ctrl-btn {
        width: 52px;
        height: 52px;
        font-size: 12px;
    }

    .speed-btn {
        font-size: 6px;
        padding: 5px 6px;
    }
}

@media (min-width: 641px) and (max-height: 780px) {
    body {
        align-items: flex-start;
        padding-top: 10px;
    }

    .game-header h1 {
        font-size: 18px;
    }
}
