/* RESET & BASE */
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #2b2b2b;
    color: #f0f0f0;
    height: 100vh;
    overflow: hidden;
    transition: background-color 0.5s ease;
}

/* LAYOUT STRUCTURE */
.game-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* 1. TOP HEADER */
.world-header {
    background: rgba(0,0,0,0.3);
    padding: 10px 20px;
    height: 200px;
    display: flex;
    align-items: center;
    gap: 30px;
    border-bottom: 1px solid #444;
}

.era-container { display: flex; gap: 15px; align-items: center; }

.era-pile { 
    width: 70px; height: 98px; 
    border: 2px dashed #555; 
    border-radius: 6px;
    display: flex; align-items: center; justify-content: center; 
    font-size: 10px; color: #888;
    background: rgba(0,0,0,0.2);
    cursor: zoom-in;
}
.era-pile.completed { 
    border: 2px solid #e53935; 
    background: rgba(229, 57, 53, 0.2); 
    color: #ffcccc;
}

.age-deck .card-back {
    width: 100%; height: 100%;
    background: repeating-linear-gradient(45deg, #444, #444 10px, #555 10px, #555 20px);
    border-radius: 6px;
    display: flex; align-items: center; justify-content: center;
    color: #aaa; font-weight: bold; font-size: 12px;
}

/* 2. MIDDLE (Play Area) */
.play-area {
    flex: 1;
    padding: 20px;
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    overflow-y: auto;
    justify-content: center;
    align-content: flex-start;
}

.player-board {
    background: #333;
    border: 1px solid #555;
    border-radius: 8px;
    padding: 10px;
    width: 280px;
    min-height: 180px;
    display: flex;
    flex-direction: column;
    position: relative;
    transition: box-shadow 0.3s;
}

.player-board.active-turn {
    border: 2px solid #4caf50;
    box-shadow: 0 0 15px rgba(76, 175, 80, 0.2);
}

.player-name { 
    font-size: 1.1rem; 
    border-bottom: 1px solid #555; 
    padding-bottom: 5px; 
    margin-bottom: 10px; 
    display: flex; justify-content: space-between;
}

.trait-pile { 
    display: flex; 
    flex-wrap: wrap; 
    gap: 4px; 
    align-content: flex-start;
}

/* 3. BOTTOM (Hand) */
.player-hand-section {
    height: 220px;
    background: #1f1f1f;
    border-top: 2px solid #444;
    padding: 10px 20px;
    display: flex;
    flex-direction: column;
}

.hand-header { 
    margin-bottom: 10px; 
    display: flex; 
    align-items: center; 
    justify-content: space-between;
}

.hand-cards {
    display: flex;
    gap: 15px;
    overflow-x: auto;
    padding: 20px 0;
    height: 100%;
    align-items: center;
}

/* CARD GRAPHICS */
.card {
    width: 100px;
    height: 140px;
    background: #222;
    border-radius: 8px;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.5);
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s;
    user-select: none;
    position: relative;
    overflow: hidden;
    border: 2px solid #555;
}

.card:hover { 
    transform: translateY(-10px) scale(1.05); 
    box-shadow: 0 10px 20px rgba(0,0,0,0.6);
    z-index: 100;
}

.card.selected {
    border: 3px solid #ffeb3b !important;
    box-shadow: 0 0 20px rgba(255, 235, 59, 0.6);
    transform: translateY(-20px) scale(1.1);
    z-index: 500;
}

.card-img {
    width: 100%; height: 100%; object-fit: cover; display: block;
}

.card-fallback-text {
    position: absolute; bottom: 0; left: 0; width: 100%;
    background: rgba(0,0,0,0.85); color: white;
    font-size: 10px; text-align: center; padding: 4px 2px;
    pointer-events: none;
}

.card.blue { border-color: #4fc3f7; }
.card.green { border-color: #81c784; }
.card.red { border-color: #e57373; }
.card.purple { border-color: #ba68c8; }
.card.colorless { border-color: #cfd8dc; }
.card.catastrophe { border-color: #ff1744; box-shadow: 0 0 8px #ff1744; }
.card.age { border-color: #fdd835; }

.card.mini {
    width: 50px; height: 70px; border-width: 1px; cursor: zoom-in;
}
.card.mini:hover { transform: scale(1.5); z-index: 200; }
.card.mini .card-fallback-text { display: none; }

/* MODALS & OVERLAYS */
.overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.85);
    display: flex; justify-content: center; align-items: center;
    z-index: 1000; /* Standard Overlay Layer */
}
.modal {
    background: #333; padding: 30px; border-radius: 10px; text-align: center;
    border: 1px solid #555; box-shadow: 0 0 20px rgba(0,0,0,0.8);
    /* Ensure modal content handles scrolling if needed */
    max-height: 90vh;
    overflow-y: auto;
}
.hidden { display: none !important; }

/* FIX: INSPECTION MODAL ON TOP */
#inspectionModal {
    z-index: 2000; /* ALWAYS ON TOP of other overlays */
}

/* BUTTONS */
.btn-action {
    padding: 8px 20px; border: none; border-radius: 4px;
    font-weight: bold; cursor: pointer; color: white;
    transition: background 0.2s, transform 0.1s; font-size: 0.9rem;
}
.btn-action:active { transform: scale(0.95); }

.btn-play { background: #4caf50; } 
.btn-play:hover { background: #388e3c; }

.btn-inspect { background: #2196f3; } 
.btn-inspect:hover { background: #1976d2; }

.btn-stabilize {
    padding: 10px 20px; background: #e91e63;
    color: white; border: none; border-radius: 5px;
    cursor: pointer; font-weight: bold;
}
.btn-stabilize:hover { background: #c2185b; }

.btn-play:disabled, .btn-inspect:disabled {
    background: #555; cursor: not-allowed; opacity: 0.5;
}

/* --- EXPANDED HAND MODAL ADJUSTMENTS --- */

/* 1. Make the modal take up 90% of the screen */
#handModal .modal-content {
    width: 90vw;         /* 90% of viewport width */
    height: 90vh;        /* 90% of viewport height */
    max-width: none;     /* Remove any previous width limits */
    display: flex;       /* Use flexbox to manage vertical space */
    flex-direction: column;
}

/* Ensure the grid of cards scrolls within the larger modal */
#handList {
    flex-grow: 1;        /* Take up remaining vertical space */
    overflow-y: auto;    /* Scroll if cards exceed the height */
    padding: 20px;       /* Add some padding around the grid */
}

/* 2. Hide the black bottom part (fallback text) ONLY in this modal */
#handList .card .card-fallback-text {
    display: none !important;
}

/* Optional: Make cards slightly larger in this view since we have more space */
#handList .card {
    width: 160px;  /* Increased from default */
    height: 224px; /* Increased from default (maintaining aspect ratio) */
}

/* --- FULL SCREEN HAND MODAL FIX --- */

/* Target the specific modal box inside the overlay */
#handModal .modal {
    width: 95vw !important;      /* 95% of screen width */
    height: 90vh !important;     /* 90% of screen height */
    max-width: none !important;  /* CRITICAL: Overrides the 900px limit in HTML */
    max-height: none !important; /* Removes height limits */
    
    /* Layout: Stacks header, grid, and button vertically */
    display: flex;
    flex-direction: column;
    padding: 20px;
}

/* Make the grid fill the empty space */
#handList {
    flex-grow: 1;          /* Grow to fill height */
    overflow-y: auto;      /* Scroll if needed */
    width: 100%;
    
    /* Center the cards nicely */
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); /* Bigger columns */
    gap: 20px;
    padding: 10px;
}

/* Make the cards much bigger in this view */
#handList .card {
    width: 100%;           /* Fill the grid cell */
    height: 280px;         /* Much taller */
}

/* Hide the black bottom text box */
#handList .card .card-fallback-text {
    display: none !important;
}

/* --- SHINY CARD EFFECTS (NEW) --- */

/* Wrapper to ensure overlays sit correctly */
.card-inner-wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden; /* Keeps the sparkles inside the rounded corners */
    border-radius: 6px; 
}

.shiny-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none; /* Let clicks pass through to the card */
    z-index: 10;
}

/* 1. The Sheen (Moving bar of light) */
.shiny-sheen {
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: linear-gradient(
        45deg, 
        rgba(255,255,255,0) 40%, 
        rgba(255,255,255,0.4) 50%, 
        rgba(255,255,255,0) 60%
    );
    /* Set to 60s for slow frequency */
    animation: sheen-sweep 60s infinite linear;
}

/* 2. The Sparkle (Rotating Star) */
.shiny-sparkle {
    position: absolute;
    top: 10%;
    right: 10%;
    width: 40px;
    height: 40px;
    /* This creates a 4-point star using pure CSS gradients */
    background: radial-gradient(circle, white 10%, transparent 10%),
                radial-gradient(circle, white 10%, transparent 10%);
    background-size: 100% 100%;
    filter: drop-shadow(0 0 5px gold);
    
    /* Using a pseudo-element to make the cross shape */
    display: flex;
    align-items: center;
    justify-content: center;
    
    /* Set to 60s to match sheen, with delay handled via JS/inline styles */
    animation: sparkle-pop 60s infinite ease-in-out;
}

/* Create the star shape visually */
.shiny-sparkle::before, .shiny-sparkle::after {
    content: "";
    position: absolute;
    background: white;
    box-shadow: 0 0 10px gold;
}
.shiny-sparkle::before { width: 4px; height: 100%; border-radius: 50%; }
.shiny-sparkle::after { width: 100%; height: 4px; border-radius: 50%; }


/* --- SHINY ANIMATIONS (Keyframes) --- */

@keyframes sheen-sweep {
    /* Active Phase: 0% to 3% (approx 2 seconds out of 60) */
    0% { transform: translate(-100%, -100%) rotate(0deg); }
    3% { transform: translate(100%, 100%) rotate(0deg); }
    
    /* Cooldown Phase: 3% to 100% (hidden for the rest) */
    100% { transform: translate(100%, 100%) rotate(0deg); }
}

@keyframes sparkle-pop {
    /* Wait */
    0% { transform: scale(0) rotate(0deg); opacity: 0; }
    
    /* Pop Up quickly (at ~1.5% of timeline) */
    1.5% { transform: scale(1.2) rotate(90deg); opacity: 1; }
    
    /* Fade Out quickly (by ~3% of timeline) */
    3% { transform: scale(0) rotate(180deg); opacity: 0; }
    
    /* Stay hidden */
    100% { transform: scale(0) rotate(180deg); opacity: 0; }
}

/* ========================================= */
/* MOBILE LANDSCAPE ADAPTATION          */
/* ========================================= */

@media only screen and (max-height: 500px) and (orientation: landscape) {
    
    /* 1. Switch from Flex Column to CSS Grid */
    .game-container {
        display: grid;
        height: 100vh;
        width: 100vw;
        /* Layout: 
           Left Column (140px) | Right Column (Auto)
           Row 1 (Auto)        | Main Board
           Row 2 (100px)       | Hand
        */
        grid-template-columns: 140px 1fr;
        grid-template-rows: 1fr 100px;
        grid-template-areas: 
            "sidebar board"
            "sidebar hand";
        
        /* Handle iPhone Notch/Safe Areas */
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }

    /* 2. Convert Top Header to Left Sidebar */
    .world-header {
        grid-area: sidebar;
        height: 100%;
        width: 100%;
        flex-direction: column; /* Stack items vertically */
        padding: 10px 5px;
        gap: 15px;
        border-bottom: none;
        border-right: 1px solid #444;
        overflow-y: auto; /* Allow scrolling if controls overflow */
        justify-content: flex-start;
    }

    /* Stack the Age Deck components vertically */
    .era-container {
        flex-direction: column;
        gap: 8px;
    }

    /* Hide Era History on mobile to save space */
    #eraHistory { display: none !important; }

    /* Shrink the Age Card size */
    .card.age, .age-deck .card-back {
        width: 60px;
        height: 84px;
    }

    /* Stack the Stats & Controls */
    .stats-panel {
        width: 100%;
        font-size: 0.75rem;
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    /* Stack the buttons vertically and make them full width */
    .stats-panel > div:last-child {
        display: flex;
        flex-direction: column;
        gap: 5px;
    }
    
    .sb-btn, #btnMultiToggle, .btn-stabilize {
        width: 100%;
        padding: 6px;
        font-size: 0.8rem;
    }

    /* 3. Adjust the Play Area (Board) */
    .play-area {
        grid-area: board;
        padding: 10px;
        align-content: flex-start; /* Items start at top */
    }

    /* Make Player Boards smaller */
    .player-board {
        width: 220px; /* Narrower */
        min-height: 140px;
    }
    
    /* Make Mini Cards on board smaller */
    .card.mini {
        width: 35px;
        height: 49px;
    }
    .card.mini div { font-size: 8px !important; } /* Shrink value text */

    /* 4. Adjust the Hand Section (Bottom Right) */
    .player-hand-section {
        grid-area: hand;
        height: 100%; /* Fill the 100px row */
        border-top: 1px solid #444;
        padding: 5px 10px;
    }

    .hand-header {
        margin-bottom: 2px;
    }
    .hand-header span { font-size: 0.8rem; }
    .hand-header button { padding: 2px 6px; font-size: 0.75rem; }

    /* Shrink cards in hand to fit the short row */
    .hand-cards {
        padding: 5px 0;
    }
    .hand-cards .card {
        width: 50px;
        height: 70px;
    }
    
    /* Hide the text box on tiny hand cards to see art */
    .hand-cards .card-fallback-text {
        display: none;
    }
    /* Show text only on hover/active if you want, or rely on Inspect */
}