:root {
    --bg-color: #0f0f13;
    --panel-bg: #1a1a24;
    --text-main: #e0e0e0;
    --neon-blue: #00f3ff;
    --neon-red: #ff0055;
    --neon-green: #39ff14;
    --neon-yellow: #fdf500;
    --grid-bg: rgba(255, 255, 255, 0.05);
    --pixel-empty: rgba(255, 255, 255, 0.1);
    --pixel-filled: #ffffff;
    --transition-speed: 0.3s;
}

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

body {
    font-family: 'Noto Sans KR', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    overflow: hidden;
    user-select: none;
    touch-action: none; /* Prevent scroll on touch */
}

h1, h2, h3, .glitch {
    font-family: 'Orbitron', sans-serif;
}

#game-container {
    width: 100vw;
    height: 100vh;
    display: flex;
    flex-direction: column;
    position: relative;
}

/* Screens */
.screen {
    display: none;
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}

.screen.active {
    display: flex;
}

/* Menu */
#screen-menu {
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at center, #1a1a2e 0%, #0f0f13 100%);
}

.glitch {
    font-size: clamp(3rem, 8vw, 5rem);
    font-weight: 900;
    font-family: 'Orbitron', sans-serif;
    text-transform: uppercase;
    text-align: center;
    line-height: 1.2;
    position: relative;
    text-shadow: 0.05em 0 0 rgba(255, 0, 85, 0.75),
                -0.025em -0.05em 0 rgba(0, 243, 255, 0.75),
                0.025em 0.05em 0 rgba(57, 255, 20, 0.75);
    animation: glitch 500ms infinite;
    margin-bottom: 10px;
    color: white;
}

@keyframes glitch {
    0% { text-shadow: 0.05em 0 0 rgba(255, 0, 85, 0.75), -0.05em -0.025em 0 rgba(0, 243, 255, 0.75), -0.025em 0.05em 0 rgba(57, 255, 20, 0.75); }
    14% { text-shadow: 0.05em 0 0 rgba(255, 0, 85, 0.75), -0.05em -0.025em 0 rgba(0, 243, 255, 0.75), -0.025em 0.05em 0 rgba(57, 255, 20, 0.75); }
    15% { text-shadow: -0.05em -0.025em 0 rgba(255, 0, 85, 0.75), 0.025em 0.025em 0 rgba(0, 243, 255, 0.75), -0.05em -0.05em 0 rgba(57, 255, 20, 0.75); }
    49% { text-shadow: -0.05em -0.025em 0 rgba(255, 0, 85, 0.75), 0.025em 0.025em 0 rgba(0, 243, 255, 0.75), -0.05em -0.05em 0 rgba(57, 255, 20, 0.75); }
    50% { text-shadow: 0.025em 0.05em 0 rgba(255, 0, 85, 0.75), 0.05em 0 0 rgba(0, 243, 255, 0.75), 0 -0.05em 0 rgba(57, 255, 20, 0.75); }
    99% { text-shadow: 0.025em 0.05em 0 rgba(255, 0, 85, 0.75), 0.05em 0 0 rgba(0, 243, 255, 0.75), 0 -0.05em 0 rgba(57, 255, 20, 0.75); }
    100% { text-shadow: -0.025em 0 0 rgba(255, 0, 85, 0.75), -0.025em -0.025em 0 rgba(0, 243, 255, 0.75), -0.025em -0.05em 0 rgba(57, 255, 20, 0.75); }
}

.subtitle {
    font-size: 1.2rem;
    color: var(--neon-blue);
    margin-bottom: 40px;
    letter-spacing: 2px;
    text-align: center;
    word-break: keep-all;
}

.menu-buttons {
    display: flex;
    gap: 20px;
    margin-bottom: 40px;
}

.btn {
    padding: 15px 30px;
    font-size: 1.2rem;
    font-weight: 700;
    font-family: 'Noto Sans KR', sans-serif;
    cursor: pointer;
    border: none;
    border-radius: 8px;
    text-transform: uppercase;
    transition: all var(--transition-speed);
}

.btn.primary {
    background: transparent;
    color: var(--neon-blue);
    border: 2px solid var(--neon-blue);
    box-shadow: 0 0 10px rgba(0, 243, 255, 0.2), inset 0 0 10px rgba(0, 243, 255, 0.2);
}
.btn.primary:hover, .btn.primary:active {
    background: var(--neon-blue);
    color: #000;
    box-shadow: 0 0 20px rgba(0, 243, 255, 0.6), inset 0 0 20px rgba(0, 243, 255, 0.6);
}

.btn.secondary {
    background: transparent;
    color: var(--neon-red);
    border: 2px solid var(--neon-red);
    box-shadow: 0 0 10px rgba(255, 0, 85, 0.2), inset 0 0 10px rgba(255, 0, 85, 0.2);
}
.btn.secondary:hover, .btn.secondary:active {
    background: var(--neon-red);
    color: #000;
    box-shadow: 0 0 20px rgba(255, 0, 85, 0.6), inset 0 0 20px rgba(255, 0, 85, 0.6);
}

.rules {
    background: rgba(255, 255, 255, 0.05);
    padding: 20px;
    border-radius: 12px;
    max-width: 600px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}
.rules h3 { color: var(--neon-yellow); margin-bottom: 10px; }
.rules ul { list-style-type: none; }
.rules li { margin-bottom: 8px; font-size: 0.95rem; color: #ccc; position: relative; padding-left: 20px; }
.rules li::before { content: "►"; position: absolute; left: 0; color: var(--neon-blue); font-size: 0.8rem; top: 2px; }

/* Game Screen Layout (Landscape by default) */
#screen-game {
    flex-direction: row; /* Landscape mode */
    background: var(--bg-color);
}

.side-panel {
    width: 250px;
    height: 100%;
    background: var(--panel-bg);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    position: relative;
    border-right: 1px solid rgba(255, 255, 255, 0.1);
    border-left: 1px solid rgba(255, 255, 255, 0.1);
}

.side-panel h2 { font-size: 2rem; margin-bottom: 10px; }
.side-panel.blue h2 { color: var(--neon-blue); text-shadow: 0 0 10px rgba(0,243,255,0.5); }
.side-panel.red h2 { color: var(--neon-red); text-shadow: 0 0 10px rgba(255,0,85,0.5); }

.status-indicator {
    font-family: 'Orbitron', sans-serif;
    font-size: 1.5rem;
    margin-bottom: 40px;
}

.controls-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

.challenge-btn {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    font-size: 2rem;
    background: #fff;
    color: #000;
}
.side-panel.blue .challenge-btn {
    background: var(--neon-blue);
    box-shadow: 0 0 30px rgba(0, 243, 255, 0.6);
}
.side-panel.red .challenge-btn {
    background: var(--neon-red);
    box-shadow: 0 0 30px rgba(255, 0, 85, 0.6);
}
.challenge-btn:disabled {
    background: #555 !important;
    color: #888 !important;
    box-shadow: none !important;
    cursor: not-allowed;
}

.action-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}
.action-area p {
    font-size: 1rem;
    color: var(--neon-yellow);
    text-align: center;
    margin-bottom: 10px;
}
.action-area .btn-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
}
.submit-btn { background: var(--neon-green); color: #000; font-size: 1.5rem; padding: 15px; }
.cancel-btn { background: transparent; color: #aaa; border: 1px solid #aaa; font-size: 1rem; padding: 10px; }

.cooldown-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 5rem;
    font-family: 'Orbitron', sans-serif;
    color: var(--neon-red);
    z-index: 10;
    backdrop-filter: blur(5px);
}
.hidden { display: none !important; }

/* Center Board */
.center-board {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
}

.top-bar {
    height: 150px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.02);
    border-bottom: 1px solid rgba(255,255,255,0.1);
}
.top-bar h3 {
    color: var(--neon-yellow);
    letter-spacing: 4px;
    margin-bottom: 10px;
}
.slots {
    display: flex;
    gap: 40px;
}

/* Grid & Pixels */
.grid-3x5 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(5, 1fr);
    gap: 2px;
    background: var(--grid-bg);
    padding: 4px;
    border-radius: 4px;
    width: 48px;
    height: 80px;
}
.pixel {
    width: 12px;
    height: 12px;
    background: var(--pixel-empty);
    border-radius: 2px;
}
.pixel.filled {
    background: var(--pixel-filled);
    box-shadow: 0 0 8px rgba(255, 255, 255, 0.8);
}

.mission-target {
    position: relative;
    cursor: pointer;
    transition: transform 0.2s;
    border: 2px solid transparent;
    padding: 5px;
    border-radius: 8px;
    background: #000;
}
.mission-target.cleared { opacity: 0.3; pointer-events: none; }
.mission-target.cleared-p1 { border-color: var(--neon-blue); box-shadow: 0 0 20px var(--neon-blue); }
.mission-target.cleared-p2 { border-color: var(--neon-red); box-shadow: 0 0 20px var(--neon-red); }

.mission-target.selectable:hover {
    transform: scale(1.1);
    border-color: var(--neon-yellow);
}
.mission-target.selected-target {
    border-color: var(--neon-green);
    box-shadow: 0 0 20px rgba(57, 255, 20, 0.5);
    transform: scale(1.1);
}

/* Tile Pool */
.main-board {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px;
}
.tile-pool {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 15px;
}

.tile {
    position: relative;
    cursor: pointer;
    transition: all 0.2s;
    border: 2px solid rgba(255, 255, 255, 0.15);
    padding: 6px;
    border-radius: 8px;
    background: rgba(0,0,0,0.5);
}
.tile-number {
    position: absolute;
    top: 2px;
    left: 4px;
    font-size: 10px;
    font-weight: bold;
    color: rgba(255, 255, 255, 0.4);
    pointer-events: none;
}
.tile .grid-3x5 {
    width: 66px;
    height: 110px;
    margin-top: 10px;
}
.tile .pixel {
    width: 18px;
    height: 18px;
}
.tile .pixel.filled { background: var(--neon-blue); box-shadow: 0 0 10px var(--neon-blue); }

.tile.selectable:hover { transform: translateY(-5px); border-color: rgba(255,255,255,0.5); }
.tile.selected {
    border-color: var(--neon-green);
    background: rgba(57, 255, 20, 0.15);
    transform: translateY(-5px);
    box-shadow: 0 0 20px rgba(57, 255, 20, 0.4);
}
.tile.selected .pixel.filled { background: var(--neon-green); box-shadow: 0 0 10px var(--neon-green); }

.tile.used { visibility: hidden; opacity: 0; }

.submit-feedback {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 1.5rem;
    font-weight: 700;
    text-shadow: none;
    background: rgba(0,0,0,0.7);
    color: white;
    padding: 8px 16px;
    border-radius: 8px;
    z-index: 10;
    white-space: nowrap;
    animation: fadePop 1.5s ease-out forwards;
}
.feedback-correct { color: var(--neon-green); }
.feedback-wrong { color: var(--neon-pink); }
@keyframes fadePop {
    0% { transform: translate(-50%, -50%) scale(0.8); opacity: 0; }
    20% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
    80% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
    100% { transform: translate(-50%, -50%) scale(1.1); opacity: 0; }
}

/* Turn Announcer */
.center-overlay {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.85);
    padding: 30px 60px;
    border-radius: 20px;
    border: 2px solid var(--neon-yellow);
    box-shadow: 0 0 30px var(--neon-yellow);
    z-index: 100;
    text-align: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}
.center-overlay.show { opacity: 1; animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275); }
@keyframes popIn { 0% { transform: translate(-50%, -50%) scale(0.5); } 100% { transform: translate(-50%, -50%) scale(1); } }
#turn-text { color: var(--neon-yellow); font-size: 2.5rem; }

/* Result Screen */
#screen-result {
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at center, #2a0a18 0%, #0f0f13 100%);
}
#result-title { font-size: clamp(3rem, 10vw, 6rem); color: var(--neon-yellow); margin-bottom: 30px; text-shadow: 0 0 30px rgba(253, 245, 0, 0.5); }

/* Responsive (Portrait) */
@media (max-aspect-ratio: 1/1), (max-width: 768px) {
    #screen-game {
        flex-direction: row;
        flex-wrap: wrap;
        align-content: flex-start;
        padding-top: 70px; /* Fix top UI overlap */
        overflow-y: auto;
    }
    .center-board {
        width: 100%;
        order: 1;
        flex: 0 0 auto;
        padding: 5px;
    }
    .side-panel {
        flex: 1 1 50%;
        width: 50%;
        height: auto;
        min-height: 160px;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;
        padding: 10px 5px;
        border-left: none; border-right: none;
        border-top: 1px solid rgba(255,255,255,0.1);
        border-bottom: none;
    }
    #player-1-panel {
        order: 2;
    }
    #player-2-panel {
        order: 3;
        transform: none; /* Player 2 faces same way as Player 1 */
        border-left: 1px solid rgba(255,255,255,0.1);
    }
    .side-panel h2 { font-size: 1.1rem; margin-bottom: 5px; flex-shrink: 0; }
    .status-indicator { margin-bottom: 10px; font-size: 0.9rem; flex-shrink: 0; }
    
    .controls-wrapper {
        flex-direction: column;
        align-items: center;
        justify-content: center;
        width: 100%;
        flex-shrink: 0;
    }
    .challenge-btn { 
        width: 70px; 
        height: 70px; 
        font-size: 1rem; 
        flex-shrink: 0;
        white-space: nowrap;
        border-radius: 50%;
    }
    .action-area { flex-direction: column; align-items: center; width: 100%; }
    .action-area p { display: none; }
    .action-area .btn-group { flex-direction: row; gap: 5px; justify-content: center; width: 100%; }
    .btn-group .btn { padding: 6px 8px; font-size: 0.8rem; flex: 1; }
    
    /* Smaller previews for side-by-side */
    .preview-grid { width: auto; height: auto; padding: 5px; margin-bottom: 10px; }
    .preview-grid .grid-3x5 { width: 45px; height: 75px; }
    .preview-grid .pixel { width: 12px; height: 12px; }

    /* Smaller tile pool */
    .tile-pool {
        grid-template-columns: repeat(5, 1fr);
        gap: 5px;
    }
    .tile .grid-3x5 { width: 40px; height: 66px; margin-top: 5px; }
    .tile .pixel { width: 10px; height: 10px; }
    
    /* Smaller slots */
    .slots { gap: 10px; }
    .mission-target { width: auto; height: auto; padding: 5px; }
    .mission-target .pixel { width: 10px; height: 10px; }

    /* Fix back button and top controls wrapping on mobile */
    .back-to-lab-btn { top: 10px; left: 10px; padding: 6px 8px; }
    .back-to-lab-btn .icon-container { width: 1.5rem; height: 1.5rem; border-radius: 6px; }
    .back-to-lab-btn .icon-container img { width: 0.9rem; height: 0.9rem; }
    .back-to-lab-btn .subtitle { font-size: 0.5rem; }
    .back-to-lab-btn .title { font-size: 0.7rem; }
    
    .top-right-controls { top: 10px; right: 10px; gap: 8px; }
    .compact-btn { padding: 6px 10px; font-size: 0.8rem; }
    .icon-btn { width: 36px; height: 36px; }

    /* Mobile Menu adjustments */
    #screen-menu { padding: 20px; }
    .menu-buttons { flex-direction: column; width: 100%; max-width: 250px; align-items: center; gap: 15px; }
    .menu-buttons .btn { width: 100%; font-size: 1.1rem; padding: 12px 20px; white-space: nowrap; }
}

/* Back to lab button */
.back-to-lab-btn {
    position: absolute;
    top: 15px;
    left: 15px;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: rgba(0, 0, 0, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    text-decoration: none;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 100;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.5);
    white-space: nowrap;
}
.back-to-lab-btn:hover {
    transform: scale(1.05);
    border-color: var(--neon-blue);
    box-shadow: 0 0 15px rgba(0, 243, 255, 0.4);
}
.back-to-lab-btn .icon-container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    background: var(--neon-blue);
    border-radius: 8px;
    flex-shrink: 0;
    transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}
.back-to-lab-btn:hover .icon-container { transform: rotate(12deg); }
.back-to-lab-btn .icon-container img { width: 1.25rem; height: 1.25rem; filter: brightness(0) invert(1); }
.back-to-lab-btn .text-container { display: flex; flex-direction: column; text-align: left; flex-shrink: 0; }
.back-to-lab-btn .subtitle { font-size: 0.625rem; font-weight: 700; color: var(--neon-blue); text-transform: uppercase; line-height: 1; margin-bottom: 2px;}
.back-to-lab-btn .title { font-size: 0.875rem; font-weight: 800; color: white; line-height: 1;}

/* Top Right Controls */
.top-right-controls {
    position: absolute;
    top: 20px;
    right: 20px;
    display: flex;
    gap: 15px;
    z-index: 50;
    align-items: center;
}
.compact-btn {
    background: rgba(0, 0, 0, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #ccc;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 0.9rem;
    font-family: 'Noto Sans KR', sans-serif;
    cursor: pointer;
    transition: all var(--transition-speed);
}
.compact-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: var(--text-main);
    color: white;
}
.icon-btn {
    background: rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-main);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all var(--transition-speed);
}
.icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: var(--neon-blue);
    color: var(--neon-blue);
    transform: scale(1.1);
}
#btn-help {
    left: auto;
    right: 20px;
}

/* Help Modal */
.modal {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(5px);
}
.modal.active { display: flex; }
.modal-content {
    background: var(--panel-bg);
    border: 1px solid var(--neon-blue);
    padding: 30px;
    border-radius: 12px;
    max-width: 500px;
    position: relative;
    box-shadow: 0 0 30px rgba(0, 243, 255, 0.3);
}
.modal-close {
    position: absolute;
    top: 10px;
    right: 15px;
    background: none;
    border: none;
    color: white;
    font-size: 2rem;
    cursor: pointer;
}
.modal-content h3 { color: var(--neon-yellow); margin-bottom: 15px; font-size: 1.5rem; }
.modal-content ul { list-style: none; }
.modal-content li { margin-bottom: 10px; padding-left: 20px; position: relative; line-height: 1.4; }
.modal-content li::before { content: "►"; position: absolute; left: 0; color: var(--neon-blue); font-size: 0.8rem; top: 2px; }

/* Preview Grid */
.preview-grid {
    position: relative;
    width: 60px;
    height: 100px;
    margin-bottom: 15px;
    background: rgba(0,0,0,0.4);
    border: 1px dashed rgba(255, 255, 255, 0.3);
    border-radius: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
}
.preview-grid.active-preview {
    border: 1px solid var(--neon-yellow);
    box-shadow: 0 0 10px rgba(253, 245, 0, 0.2);
}
.preview-grid .grid-3x5 {
    width: 54px;
    height: 90px;
    background: transparent;
}
.preview-grid .pixel {
    width: 14px;
    height: 14px;
}
