@layer base, components, utilities;

@layer base {
    :root {
        --bg-color: #fff9f5;
        --primary-red: #ff5e5e;
        --secondary-red: #ff8c8c;
        --text-color: #4a4a4a;
        --label-color: #8e8e8e;
        --board-bg: #ffffff;
        --shadow-color: rgba(0, 0, 0, 0.1);
        --glow-color: rgba(255, 94, 94, 0.4);
        --selection-bg: rgba(255, 94, 94, 0.2);
        --selection-border: rgba(255, 94, 94, 0.5);
        
        --apple-size: 40px;
        --grid-gap: 4px;
        --board-cols: 17;
        --board-rows: 10;
    }

    * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
        -webkit-tap-highlight-color: transparent;
    }

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

@layer components {
    #game-container {
        width: 100%;
        max-width: 800px;
        padding: 20px;
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    header {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        padding: 0 10px;
    }

    .stats {
        display: flex;
        gap: 20px;
    }

    .stat-item {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
    }

    .label {
        font-size: 12px;
        font-weight: 700;
        color: var(--label-color);
        letter-spacing: 0.05em;
    }

    #score, #timer {
        font-size: 24px;
        font-weight: 800;
        color: var(--primary-red);
    }

    h1 {
        font-size: 20px;
        font-weight: 900;
        color: var(--primary-red);
        margin: 0;
    }

    #reset-btn {
        padding: 8px 16px;
        background: white;
        border: 2px solid var(--primary-red);
        color: var(--primary-red);
        border-radius: 20px;
        font-weight: 700;
        cursor: pointer;
        transition: all 0.2s ease;
    }

    #reset-btn:hover {
        background: var(--primary-red);
        color: white;
    }

    #game-board-container {
        position: relative;
        background: var(--board-bg);
        padding: 10px;
        border-radius: 16px;
        box-shadow: 0 10px 30px var(--shadow-color);
        user-select: none;
        overflow: auto;
        max-width: 100%;
        scrollbar-width: none; /* Firefox */
        -ms-overflow-style: none; /* IE and Edge */
    }

    #game-board-container::-webkit-scrollbar {
        display: none; /* Chrome, Safari, Opera */
    }

    #game-board {
        display: grid;
        grid-template-columns: repeat(var(--board-cols), var(--apple-size));
        grid-template-rows: repeat(var(--board-rows), var(--apple-size));
        gap: var(--grid-gap);
    }

    .apple {
        position: relative;
        width: var(--apple-size);
        height: var(--apple-size);
        display: flex;
        justify-content: center;
        align-items: center;
        font-weight: 800;
        font-size: 18px;
        color: white;
        cursor: pointer;
        transition: transform 0.1s ease, opacity 0.3s ease;
        z-index: 1;
    }

    .apple::before {
        content: '';
        position: absolute;
        width: 85%;
        height: 85%;
        background: var(--primary-red);
        border-radius: 50% 50% 45% 45%;
        box-shadow: inset -3px -3px 6px rgba(0,0,0,0.1), 
                    2px 2px 5px var(--shadow-color);
        z-index: -1;
    }

    .apple::after {
        content: '';
        position: absolute;
        top: 10%;
        left: 50%;
        width: 4px;
        height: 8px;
        background: #8b4513;
        border-radius: 2px;
        transform: translateX(-50%) rotate(10deg);
        z-index: -1;
    }

    .apple.selected::before {
        background: var(--secondary-red);
        box-shadow: 0 0 10px var(--glow-color);
        transform: scale(1.1);
    }

    .apple.removed {
        opacity: 0;
        pointer-events: none;
        transform: scale(0.5);
    }

    #selection-box {
        position: absolute;
        background: var(--selection-bg);
        border: 2px solid var(--selection-border);
        border-radius: 4px;
        pointer-events: none;
        display: none;
        z-index: 10;
    }

    #overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        backdrop-filter: blur(5px);
        display: flex;
        justify-content: center;
        align-items: center;
        z-index: 100;
        opacity: 1;
        transition: opacity 0.3s ease;
    }

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

    .modal {
        background: white;
        padding: 40px;
        border-radius: 24px;
        text-align: center;
        box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
        max-width: 90%;
        width: 320px;
    }

    .modal h2 {
        margin-bottom: 10px;
        color: var(--primary-red);
        font-weight: 900;
    }

    .modal p {
        font-size: 16px;
        margin-bottom: 10px;
        color: var(--label-color);
    }

    #final-score {
        display: block;
        font-size: 64px;
        font-weight: 900;
        color: var(--primary-red);
        margin: 10px 0 30px 0;
        text-shadow: 2px 2px 0px rgba(0,0,0,0.05);
    }

    #start-btn {
        width: 100%;
        padding: 15px;
        background: var(--primary-red);
        color: white;
        border: none;
        border-radius: 12px;
        font-size: 18px;
        font-weight: 800;
        cursor: pointer;
        transition: transform 0.2s ease, background 0.2s ease;
    }

    #start-btn:hover {
        background: #e65555;
        transform: translateY(-2px);
    }
}

@layer utilities {
    .hidden {
        display: none !important;
    }
}

/* Mobile Adjustments */
@media (max-width: 600px) {
    :root {
        --apple-size: 30px;
        --grid-gap: 2px;
    }
    
    #game-container {
        padding: 10px;
    }
    
    header {
        flex-direction: column;
        gap: 10px;
    }
    
    .stats {
        width: 100%;
        justify-content: space-around;
    }
}
