/* --- 1. CORE LAYOUT & RESET --- */
@import url('https://fonts.googleapis.com/css2?family=Kanit:ital,wght@0,400;0,600;0,700;1,900&family=Nunito:wght@700;800;900&display=swap');

* { box-sizing: border-box; touch-action: none; user-select: none; -webkit-user-select: none; }

:root {
    /* --- VIBRANT ROYAL BLUE THEME --- */
    --bg-friendly-top: #566CD5; 
    --bg-friendly-bot: #3E50A4; 
    
    /* --- DARK ARENA COLORS --- */
    --bg-grid-center: #1A1D24; 
    --bg-grid-edge:   #111318; 
    
    --bg-panel: #1A1D24;        
    --bg-cell:  #262A35;        
    --text-main: #E2E8F0;       
    --text-muted: #8FA3BF; 
    
    /* --- TRAY BAYS --- */
    --bg-tray: transparent; 
    
    /* Radius Standards */
    --rad-grid: 12px;
    --rad-block: 6px;
    --rad-cell: 4px;

    /* DYNAMIC HEXAGON STATE */
    --hex-opacity: 0;        
    --hex-start: #2980b9;    
    --hex-end: #3498db;      
}

/* Apply background gradient to HTML to ensure uniformity */
html {
    height: 100%;
    background: linear-gradient(180deg, var(--bg-friendly-top) 0%, var(--bg-friendly-bot) 100%);
    background-attachment: fixed; 
}

body { 
    background: transparent;
    color: var(--text-main); 
    font-family: 'Nunito', sans-serif; 
    text-align: center; 
    margin: 0; 
    display: flex; 
    justify-content: center; 
    min-height: 100vh; 
    overflow: hidden; 
    padding-top: 60px;
    overscroll-behavior: none;
}

.game-column { 
    width: 404px; 
    min-height: 800px; 
    display: flex; 
    flex-direction: column; 
    align-items: center;
    transform-origin: top center;
    transition: transform 0.1s linear; 
    position: relative;
}
.hidden { display: none !important; }

/* =====================================================
   HEADER RESTRUCTURING (ABSOLUTE HUD)
   ===================================================== */
.top-bar { 
    width: 100%; 
    height: 100px; 
    position: relative; 
    margin-bottom: 20px; 
    transition: opacity 0.2s; 
}

/* Hide Top Bar when Game Over is active */
.top-bar.fade-out {
    opacity: 0;
}

/* 1. MAIN SCORE (CENTER, FLOATING) */
.score-box:first-child {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    
    background: transparent;
    border: none;
    box-shadow: none;
    padding: 0;
    min-width: auto;
    
    display: flex;
    justify-content: center;
    align-items: center;
    
    overflow: visible; 
    z-index: 10;
    
    transition: transform 0.1s;
}

/* IMPACT POP EFFECT */
.score-box:first-child.pulse-trigger {
    animation: impact-pop 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes impact-pop {
    0% { transform: translate(-50%, -50%) scale(1); }
    50% { transform: translate(-50%, -50%) scale(1.3); } 
    100% { transform: translate(-50%, -50%) scale(1); }
}

.score-box:first-child .label { display: none; }

/* FONT: Nunito (76px) */
#score { 
    font-family: 'Nunito', sans-serif;
    font-size: 76px; 
    font-weight: 900; 
    color: #ffffff;
    letter-spacing: -2px;
    line-height: 1;
    z-index: 2;
    text-shadow: 0 4px 15px rgba(0,0,0,0.2);
    font-variant-numeric: tabular-nums;
}

/* 2. BEST SCORE (TOP LEFT - CAPSULE CONTAINER) */
.score-box:last-child {
    position: absolute;
    /* MOVED UP: Adjusted from -15px to -30px to prevent overlap with high scores */
    top: -30px; 
    left: 0; 
    
    /* THE CAPSULE LOOK */
    background: rgba(0, 0, 0, 0.25); 
    border-radius: 30px;             
    padding: 6px 16px;               
    
    border: none;
    box-shadow: none;
    min-width: auto;
    
    display: flex;
    flex-direction: row; 
    align-items: center;
    gap: 10px; 
}

/* (The rest of the CSS for .label and #best-score remains the same) */
.score-box:last-child .label { font-size: 0; } 

.score-box:last-child .label::before {
    content: '👑'; 
    font-size: 22px; 
    filter: drop-shadow(0 2px 2px rgba(0,0,0,0.3));
    display: block;
    line-height: 1;
}

#best-score { 
    font-family: 'Nunito', sans-serif;
    font-size: 20px; 
    font-weight: 800; 
    color: #ffffff; 
    text-shadow: none; 
    line-height: 1;
    margin-top: 2px; 
}

/* =====================================================
   THE PULSATING HEXAGON
   ===================================================== */
.score-box:first-child::before {
    content: '';
    position: absolute;
    top: 52%; left: 50%;
    
    /* Size: 50px */
    width: 50px; height: 50px;
    
    transform: translate(-50%, -50%) scale(0);
    
    clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
    
    background: linear-gradient(135deg, var(--hex-start), var(--hex-end));
    box-shadow: 0 0 25px var(--hex-start); 
    
    z-index: -1; 
    opacity: 0;
    
    transition: opacity 0.4s ease, transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

/* --- STATE 0: FULL COMBO --- */
body.combo-active[data-grace="0"] .score-box:first-child::before {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1; 
    animation: hex-pulse 0.8s ease-in-out infinite;
}

/* --- STATE 1: MISS 1 (Dimmed) --- */
body.combo-active[data-grace="1"] .score-box:first-child::before {
    transform: translate(-50%, -50%) scale(0.9);
    opacity: 0.6; 
    animation: hex-pulse 1.2s ease-in-out infinite; 
    filter: saturate(0.8);
}

/* --- STATE 2: MISS 2 (Dimmest) --- */
body.combo-active[data-grace="2"] .score-box:first-child::before {
    transform: translate(-50%, -50%) scale(0.8);
    opacity: 0.3; 
    animation: hex-pulse 2s ease-in-out infinite;
    filter: saturate(0.5);
}

@keyframes hex-pulse {
    0% { transform: translate(-50%, -50%) scale(1.0); }
    50% { transform: translate(-50%, -50%) scale(1.15); }
    100% { transform: translate(-50%, -50%) scale(1.0); }
}

/* =====================================================
   COLOR TIERS
   ===================================================== */

body.combo-active { 
    --hex-start: #00f2fe; 
    --hex-end: #4facfe; 
}

body:has(.combo-popup[style*="feca57"]),
body:has(.combo-popup[style*="5f27cd"]),
body:has(.combo-popup[style*="ff6b6b"]),
body:has(.combo-popup[style*="f1c40f"]),
body:has(.combo-popup[style*="00ffcc"]) { 
    --hex-start: #FFD700; /* Pure Gold */
    --hex-end: #FFA500;   /* Orange Gold */
}


/* --- GRID (STAGE LIGHTING) --- */
#grid-wrapper { 
    background: radial-gradient(circle at center, var(--bg-grid-center) 0%, var(--bg-grid-edge) 85%);
    padding: 12px; 
    border-radius: var(--rad-grid); 
    width: 404px; 
    height: 404px; 
    position: relative; 
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 25px 50px -12px rgba(0,0,0,0.55);
    margin-top: 0px; 
    overflow: visible; 
    z-index: 1; 
}

#grid { display: grid; grid-template-columns: repeat(8, 44px); gap: 4px; }

/* EMPTY CELLS */
.cell { 
    width: 44px; 
    height: 44px; 
    background: var(--bg-cell); 
    border-radius: var(--rad-cell); 
    position: relative;
    z-index: 1; 
}

/* OCCUPIED BLOCKS */
.cell.occupied { 
    background: var(--block-color); 
    border-radius: var(--rad-block);
    opacity: 1 !important; 
    z-index: 2; 
    box-shadow: 
        inset 3px 3px 0px 0px rgba(255,255,255,0.4),
        inset -3px -3px 0px 0px rgba(0,0,0,0.2);
}

.clear-resolve {
    z-index: 50; 
    background: var(--block-color);
    border-radius: var(--rad-block);
}

.placed-impact { 
    animation: place-pop 0.3s ease-out forwards; 
    z-index: 15; 
}

@keyframes place-pop {
    0% { transform: scale(0.8); filter: brightness(1.3); }
    60% { transform: scale(1.05); }
    100% { transform: scale(1); filter: brightness(1); }
}

/* =====================================================
   GHOST PREVIEWS
   ===================================================== */
@keyframes preview-pulse {
    0% { opacity: 0.5; transform: scale(0.95); filter: brightness(1); }
    100% { opacity: 0.8; transform: scale(1); filter: brightness(1.3); }
}

.cell.ghost { 
    background: var(--block-color); 
    border-radius: var(--rad-block);
    z-index: 100;
    opacity: 0.5 !important; 
    border: 1px solid rgba(255,255,255,0.3); 
    animation: preview-pulse 1.2s ease-in-out alternate infinite;
    box-shadow: none; 
}

.cell.ghost-clear {
    background: var(--preview-color) !important;
    border-radius: var(--rad-block);
    z-index: 100;
    border: 1px solid rgba(255,255,255,0.6); 
    animation: clear-pulse 0.6s ease-in-out alternate infinite;
}

@keyframes clear-pulse {
    0% { opacity: 0.6; filter: brightness(1.3); transform: scale(0.96); }
    100% { opacity: 1.0; filter: brightness(1.6); transform: scale(1); }
}

/* --- TRAY --- */
#tray { 
    width: 100%; 
    margin-top: 30px; 
    display: flex; 
    justify-content: space-between; 
    height: 120px; 
    transition: opacity 0.3s;
}

.tray-slot { 
    width: 128px; 
    height: 120px; 
    background: transparent; 
    border-radius: 12px; 
    display: flex; 
    justify-content: center; 
    align-items: center; 
}

.piece { position: relative; pointer-events: none; }

.tray-slot .piece {
    filter: saturate(0.9) brightness(0.95) drop-shadow(0 6px 6px rgba(0,0,0,0.15));
    transition: filter 0.2s;
}

.block { 
    position: absolute; 
    width: 26px; 
    height: 26px; 
    background: var(--block-color); 
    border-radius: 4px; 
    opacity: 1 !important; 
    box-shadow: 
        inset 2px 2px 0px 0px rgba(255,255,255,0.4),
        inset -2px -2px 0px 0px rgba(0,0,0,0.2);
}

.piece.dragging { position: fixed; z-index: 1000; pointer-events: none; }
.piece.dragging .block {
    width: 44px; height: 44px; 
    border-radius: var(--rad-block);
    opacity: 1 !important; 
    animation: none; transition: none;
    filter: none;
    box-shadow: 
        inset 3px 3px 0px 0px rgba(255,255,255,0.5),
        inset -3px -3px 0px 0px rgba(0,0,0,0.2),
        0 8px 15px rgba(0,0,0,0.3);
}
@media (pointer: coarse) {
    .piece.dragging .block { transform: scale(1.1); }
}

/* =====================================================
   FLOATING SCORE
   ===================================================== */
.floating-score {
    position: fixed;
    left: var(--start-x);
    top: var(--start-y);
    color: #ffffff;
    font-family: 'Kanit', sans-serif;
    font-weight: 700;
    font-size: 32px;
    pointer-events: none;
    z-index: 20006;
    text-shadow: 0 2px 4px rgba(0,0,0,0.5); 
    transform: translate(-50%, -50%);
    will-change: transform;
}

.score-animate { animation: score-travel 1s linear forwards; }

@keyframes score-travel {
    0% { transform: translate(-50%, -50%) scale(0.5); opacity: 1; }
    10% { transform: translate(-50%, -80%) scale(1.2); opacity: 1; }
    100% { transform: translate(calc(-50% + var(--tx)), calc(-50% + var(--ty))) scale(1); opacity: 0; }
}

/* =====================================================
   COMBO & REINFORCEMENT SYSTEM
   ===================================================== */

@keyframes shake-1 { 0%, 100% { transform: translate(0, 0); } 25% { transform: translate(-2px, 2px); } 75% { transform: translate(2px, -2px); } }
@keyframes shake-2 { 0%, 100% { transform: translate(0, 0); } 20% { transform: translate(-4px, 0); } 40% { transform: translate(4px, 0); } 60% { transform: translate(-4px, 0); } 80% { transform: translate(4px, 0); } }
@keyframes shake-3 { 0%, 100% { transform: translate(0, 0); } 10%, 90% { transform: translate(-5px, -2px); } 30%, 70% { transform: translate(5px, 3px); } 50% { transform: translate(-5px, 5px); } }

.shake-1 { animation: shake-1 0.1s linear; }
.shake-2 { animation: shake-2 0.12s linear; }
.shake-3 { animation: shake-3 0.15s linear; }

.combo-popup {
    position: absolute;
    top: 52%; 
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 20010;
    pointer-events: none;
    animation: combo-pop 0.98s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
    will-change: transform;
}

.combo-label {
    font-family: 'Kanit', sans-serif;
    font-weight: 900;
    font-style: italic;
    font-size: 55px;
    color: #fff;
    -webkit-text-stroke: 2px #fff;
    text-shadow: 0 4px 8px rgba(0,0,0,0.3);
    line-height: 1;
}

.combo-value {
    font-family: 'Kanit', sans-serif;
    font-weight: 900;
    font-style: italic;
    font-size: 88px;
    line-height: 1;
    margin-top: 8px;
    background: -webkit-linear-gradient(#fff, var(--combo-color));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    -webkit-text-stroke: 3px #fff;
    filter: drop-shadow(0 6px 0 var(--combo-color));
}

@keyframes combo-pop {
    0% { transform: translate(-50%, -20%) scale(0.5); opacity: 0; }
    40% { transform: translate(-50%, -60%) scale(1.2); opacity: 1; }
    100% { transform: translate(-50%, -80%) scale(1); opacity: 0; }
}

.reinforcement-banner {
    position: absolute;
    top: 38%;
    left: 0;
    width: 100%;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 20020;
    pointer-events: none;
    
    background: linear-gradient(90deg, transparent, var(--combo-color), transparent);
    border-top: 2px solid var(--combo-color);
    border-bottom: 2px solid var(--combo-color);
    
    animation: banner-wipe 0.85s ease-out forwards;
    transform-origin: center;
    will-change: transform;
}

.banner-text {
    font-family: 'Rubik Mono One', sans-serif;
    font-size: 26px;
    color: #fff;
    letter-spacing: 5px;
    text-transform: uppercase;
    text-shadow: 0 2px 4px rgba(0,0,0,0.5);
    white-space: nowrap;
}

@keyframes banner-wipe {
    0% { transform: scaleX(0); opacity: 0; }
    20% { transform: scaleX(1); opacity: 1; }
    80% { transform: scaleX(1); opacity: 1; }
    100% { transform: scaleX(1.2); opacity: 0; }
}

/* =====================================================
   FULL SCREEN GAME OVER OVERLAY (FIXED DELAY)
   ==================================================== */

#game-over-overlay {
    position: absolute;
    top: 0; left: 0;
    width: 100%;
    height: 100%;
    z-index: 5000;

    /* UNIFORMITY FIX: Exact same gradient as HTML */
    background: linear-gradient(180deg, var(--bg-friendly-top) 0%, var(--bg-friendly-bot) 100%);

    display: none;
    opacity: 0; /* Start invisible */

    align-items: center;
    justify-content: center;
    /* iPad-portrait fit (#79): clip the scale(1.25) pop-in animation so it
       cannot push past the .game-column rect when the column does not scale
       up to the viewport (resizeGame caps scale at 1.0, so on 834×1194
       and 1024×1366 iPads the 404×800 column sits inside a much larger
       viewport and any 1.25× content overflow gets visible-clipped against
       body { overflow: hidden }). Companion media-query below restores
       the content to fit-on-iPad sizing. Phone portrait is untouched. */
    overflow: hidden;
}

/* Keyframe for robust delaying */
@keyframes overlayFadeIn {
    0% { opacity: 0; }
    100% { opacity: 1; }
}

/* ACTIVE STATE */
#game-over-overlay:not(.hidden) {
    display: flex;
    
    /* USE ANIMATION INSTEAD OF TRANSITION */
    /* This forces the delay to be respected even after display:none switch */
    animation: overlayFadeIn 0.4s ease forwards;
    
    /* TIMING CALCULATION:
       - Grid Animation Ends: ~920ms
       - JS Triggers Overlay: 800ms
       - Desired Wait: 400ms after grid
       - Target Appearance: 1320ms (920 + 400)
       - Required Delay: 1320 - 800 = 520ms
       - Set to 0.55s (550ms) for safety
    */
    animation-delay: 0.55s;
}

/* WHEN ACTIVE: TRIGGER CONTENT ANIMATION */
#game-over-overlay:not(.hidden) .game-over-content {
    animation: pop-in-content 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
    
    /* Content appears slightly after the blue screen starts */
    /* 0.55s (bg start) + 0.15s offset = 0.7s */
    animation-delay: 0.7s; 
}

/* --- Content Wrapper (Transparent now) --- */
.game-over-content {
    background: transparent;
    box-shadow: none;
    border: none;
    
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    
    opacity: 0; /* Start hidden for animation */
    transform: scale(0.8);
    
    width: 100%;
    max-width: none;
}

/* THE POP ANIMATION */
@keyframes pop-in-content {
    0% { opacity: 0; transform: scale(0.8); }
    100% { opacity: 1; transform: scale(1.25); }
}

/* =====================================================
   iPad PORTRAIT FIT (#79)
   The 1.25× pop-in scale was tuned for phone portrait,
   where .game-column is itself sized to the viewport via
   resizeGame()'s scale(viewportWidth/404). On iPad portrait
   the column stays at its native 404×800 (resizeGame clamps
   scale to 1.0), so a 1.25× content transform pushes outside
   the column rect and gets clipped against body { overflow
   :hidden }. Targeting min-width 768px (smallest iPad short
   edge) AND orientation:portrait keeps phones (≤480 short
   edge) and iPad landscape untouched. The Best Score
   celebration (#new-record-overlay / .nr-content) is
   unaffected — its pop animation already ends at scale(1)
   and we only override the gameover overlay's animation
   below by name.
   ==================================================== */
@keyframes pop-in-content-ipad {
    0% { opacity: 0; transform: scale(0.8); }
    60% { opacity: 1; transform: scale(1.05); }
    100% { opacity: 1; transform: scale(1); }
}

@media (min-width: 768px) and (orientation: portrait) {
    /* Re-bind the gameover content's animation to the
       iPad-tuned variant (ends at scale 1, not 1.25). */
    #game-over-overlay:not(.hidden) .game-over-content {
        animation-name: pop-in-content-ipad;
    }

    /* #107 followup to #103/#106: 22vh landed the banner at
       ~27% from viewport top — operator wants the top edge
       in the upper sixth (~10–12%) on BOTH iPad 11"
       (820×1180) and iPad 13" (1024×1366) portrait. Overlay
       top sits at body padding-top:60px (≈5.08% of 1180 on
       iPad 11", ≈4.39% of 1366 on iPad 13" — structural,
       scales near-proportionally). padding-top:6vh adds 6%
       of viewport height on top of that offset:
         iPad 11":  60 + 0.06×1180 = 130.8px  → 11.08% top edge
         iPad 13":  60 + 0.06×1366 = 141.96px → 10.39% top edge
       Both inside the 10–12% target band; vh-based so the
       rule lands proportionally across both iPad sizes with
       no per-device tuning. Phone portrait (≤480px short
       edge) does not match this media query and remains
       byte-identical. iPad landscape does not match
       orientation:portrait and is also untouched. */
    #game-over-overlay {
        align-items: flex-start;
        padding-top: 6vh;
    }

    /* Belt-and-braces: cap content width so the title /
       score / button row never extend past the column.
       #103 followup to #79/#84: 404px (column-width) still
       read as oversized on iPad portrait — drop to 320px
       (~21% reduction, ~39% of an 820px iPad viewport,
       ~42px gutter on each side inside the 404px column).
       Phone portrait + landscape untouched (media query
       gates min-width:768px AND orientation:portrait). */
    .game-over-content {
        width: 100%;
        max-width: 260px;
    }

    /* #103 second-pass reduction: max-width:320px alone
       did not visually shrink the banner enough on real
       iPad portrait — Ripon flagged 2026-05-04 still
       oversized. Width-only caps don't reduce the inner
       type weight (title 2.5rem, score 3.5rem) or the
       button (padding 9px 100px, 38×38 SVG), so the
       banner still reads dominant. Reducing the inner
       sizes ~28% in lockstep brings the whole composition
       proportional to iPad portrait without re-tuning the
       animation or touching the wrapper transform (which
       the pop-in-content-ipad keyframe owns). max-width
       drops from 320px → 260px to match the new content
       width (button at padding 8px 70px + 30px SVG ≈
       208px, fits comfortably inside 260px wrapper).
       Phone + landscape gated out by the parent media
       query above. */
    #gameOverTitle {
        font-size: 1.8rem;
        margin: 0 0 28px 0;
    }
    #go-score-val {
        font-size: 2.5rem;
    }
    #go-best-val {
        font-size: 1.5rem;
    }
    .go-label {
        font-size: 0.85rem;
        letter-spacing: 2px;
    }
    .go-score-container, .go-best-container {
        margin-bottom: 18px;
    }
    .go-play-btn {
        padding: 8px 70px;
        margin-top: 28px;
    }
    .go-play-btn svg {
        width: 30px;
        height: 30px;
    }
}

/* --- Title --- */
#gameOverTitle {
    color: #ffffff;
    font-family: 'Verdana', 'Segoe UI', sans-serif;
    font-size: 2.5rem; 
    font-weight: 800; 
    margin: 0 0 40px 0; 
    text-transform: uppercase;
    letter-spacing: 2px;
    text-shadow: 0 4px 0 rgba(0,0,0,0.1);
    line-height: 1;
}

.highlight-char {
    font-size: 1.3em;
    vertical-align: -3px; 
}

/* --- Score Groups --- */
.go-score-container, .go-best-container {
    margin-bottom: 25px; 
    width: 100%;
}

.go-label {
    color: #bdfaff; 
    font-family: 'Verdana', sans-serif;
    font-size: 1rem;
    font-weight: 600; 
    margin: 0 0 5px 0;
    letter-spacing: 3px;
    text-transform: uppercase;
    opacity: 0.9;
}

#go-score-val {
    font-family: 'Verdana', sans-serif;
    font-size: 3.5rem; 
    font-weight: 700; 
    color: #ffffff;
    margin: 0;
    text-shadow: 0 4px 15px rgba(255,255,255,0.3);
}

#go-best-val {
    font-family: 'Verdana', sans-serif;
    font-size: 2rem;
    font-weight: 700; 
    color: #ffeaa7; 
    margin: 0;
    text-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

/* --- Replay Button --- */
.go-play-btn {
    background-color: #55efc4; 
    border: none;
    border-radius: 20px;
    cursor: pointer;
    margin-top: 40px;
    
    /* Thinner Padding */
    padding: 9px 100px; 
    width: auto;
    
    /* Thinner Shadow */
    box-shadow: 0 5px 0 #00b894, 0 20px 30px rgba(0,0,0,0.2);
    transition: all 0.1s ease;
    
    opacity: 0; 
    transform: translateY(20px);
    pointer-events: none;
}

.go-play-btn.reveal {
    opacity: 1;
    transform: translateY(0);
    pointer-events: all;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.go-play-btn:active {
    transform: translateY(5px); 
    box-shadow: 0 0 0 #00b894;
}

.go-play-btn svg {
    filter: drop-shadow(0 4px 3px rgba(0,0,0,0.2));
    display: block;
    width: 38px;
    height: 38px;
}

/* =====================================================
   NEW BEST SCORE ANIMATION (TURBO PREMIUM VERSION)
   ===================================================== */

/* Container */
#new-best-overlay {
    position: absolute;
    inset: 0;
    z-index: 2000;
    pointer-events: none;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    background: radial-gradient(circle, rgba(255, 215, 0, 0.2) 0%, transparent 60%);
    animation: bg-flash 0.3s ease-out forwards;
}

@keyframes bg-flash {
    0% { opacity: 0; transform: scale(0.8); }
    100% { opacity: 1; transform: scale(1); }
}

.celebration-content {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
}

/* --- 1. THE CROWN (Optimized Duration) --- */

.crown-container {
    width: 110px;
    height: 110px;
    z-index: 10;
    transform-origin: center bottom;
    animation: crown-lifecycle 2.0s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

.crown-svg {
    width: 100%;
    height: 100%;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cdefs%3E%3ClinearGradient id='g' x1='0%25' y1='0%25' x2='100%25' y2='100%25'%3E%3Cstop offset='0%25' style='stop-color:%23FFD700'/%3E%3Cstop offset='50%25' style='stop-color:%23FDB931'/%3E%3Cstop offset='100%25' style='stop-color:%23FFD700'/%3E%3C/linearGradient%3E%3C/defs%3E%3Cpath fill='url(%23g)' stroke='%23B8860B' stroke-width='1' d='M50 20 L65 45 L85 25 L80 55 L95 40 L90 75 Q50 85 10 75 L5 40 L20 55 L15 25 L35 45 Z' /%3E%3Ccircle cx='5' cy='40' r='3' fill='%23FFD700' stroke='%23B8860B'/%3E%3Ccircle cx='15' cy='25' r='3' fill='%23FFD700' stroke='%23B8860B'/%3E%3Ccircle cx='50' cy='20' r='4' fill='%23FFD700' stroke='%23B8860B'/%3E%3Ccircle cx='85' cy='25' r='3' fill='%23FFD700' stroke='%23B8860B'/%3E%3Ccircle cx='95' cy='40' r='3' fill='%23FFD700' stroke='%23B8860B'/%3E%3C/svg%3E");
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center bottom;
    filter: drop-shadow(0 0 15px rgba(255, 215, 0, 0.9));
}

@keyframes crown-lifecycle {
    0% { transform: scale(2.0) translateY(30px); filter: drop-shadow(0 0 50px #FFF) brightness(3.0); opacity: 0; }
    15% { transform: scale(1.0) translateY(0); filter: drop-shadow(0 0 20px #FFD700) brightness(1.2); opacity: 1; }
    30% { transform: scale(1.0) translateY(-22px); filter: drop-shadow(0 4px 2px rgba(0,0,0,0.4)) brightness(1); opacity: 1; }
    60% { transform: scale(1.0) translateY(-22px); opacity: 1; }
    100% { transform: scale(0.95) translateY(-80px); opacity: 0; }
}

/* --- 2. THE BANNER (Synced Lifecycle) --- */
.best-banner {
    position: absolute;
    top: 75px; 
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: banner-lifecycle 2.0s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
    animation-delay: 0.2s; 
    opacity: 0;
}

@keyframes banner-lifecycle {
    0% { transform: scale(0.2) translateY(-20px); opacity: 0; }
    15% { transform: scale(1) translateY(0); opacity: 1; }
    60% { transform: scale(1) translateY(0); opacity: 1; }
    100% { transform: scale(0.9) translateY(10px); opacity: 0; }
}

.ribbon-center {
    background: linear-gradient(180deg, #BA68C8 0%, #7B1FA2 45%, #4A148C 55%, #4A148C 100%);
    color: #FFF;
    padding: 6px 28px;
    font-family: 'Kanit', sans-serif;
    font-weight: 900;
    font-size: 22px;
    letter-spacing: 1px;
    text-transform: uppercase;
    text-shadow: 0 1px 2px rgba(0,0,0,0.5);
    border-radius: 4px;
    border: 1px solid #BA68C8; 
    border-bottom: 3px solid #38006b; 
    box-shadow: 0 10px 25px rgba(0,0,0,0.5);
    transform: perspective(200px) rotateX(5deg);
}

.ribbon-tail-l, .ribbon-tail-r {
    position: absolute;
    top: 6px;
    z-index: -1;
    width: 24px; 
    height: 28px;
    background: #311B92; 
    box-shadow: inset 0 0 5px rgba(0,0,0,0.6);
    border-radius: 2px;
}

.ribbon-tail-l { left: -10px; transform: skewY(20deg) scale(0.9); }
.ribbon-tail-r { right: -10px; transform: skewY(-20deg) scale(0.9); }

.fade-out-best { animation: fade-overlay-out 1.0s ease-in forwards !important; }

@keyframes fade-overlay-out {
    0% { opacity: 1; transform: scale(1); }
    100% { opacity: 0; transform: scale(1.1); }
}

/* =====================================================
   STEP 2: GAME OVER SEQUENCE
   ===================================================== */

.visual-fill {
    background: var(--block-color);
    border-radius: var(--rad-block);
    box-shadow: 
        inset 3px 3px 0px 0px rgba(255,255,255,0.4),
        inset -3px -3px 0px 0px rgba(0,0,0,0.2);
    animation: fill-pop 0.15s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
    position: relative;
    z-index: 10;
}

@keyframes fill-pop {
    0% { transform: scale(0); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

.state-dimmed {
    filter: brightness(0.8) !important;
    transition: filter 0.4s ease;
}

/* "No Space Left" Toast */
#no-space-message {
    width: 100%;
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Kanit', sans-serif;
    font-weight: 900;
    font-size: 28px;
    color: #ffffff;
    text-transform: uppercase;
    letter-spacing: 1px;
    text-shadow: 0 2px 4px rgba(0,0,0,0.5);
    position: absolute;
    /* MOVED UP by 50px (was 70px, now 120px) */
    bottom: 120px; 
    left: 0;
    z-index: 100;
    background: rgba(26, 29, 36, 0.95);
    border-radius: 12px;
    animation: msg-slide-up 0.3s ease-out forwards;
}

@keyframes msg-slide-up {
    0% { transform: translateY(20px); opacity: 0; }
    100% { transform: translateY(0); opacity: 1; }
}

#no-space-message.hidden { display: none; }

/* ============================================
   NEON SNAP VFX & LIGHTING
   ============================================ */

.fx-dying-block {
    position: absolute;
    width: 44px;
    height: 44px;
    background: var(--color);
    border-radius: 6px;
    z-index: 500;
    pointer-events: none;
    will-change: transform, opacity;
}

.anim-implode { animation: implode 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards; }
@keyframes implode {
    0% { transform: scale(1); filter: brightness(1); opacity: 1; }
    40% { transform: scale(0.9); filter: brightness(2); opacity: 1; }
    100% { transform: scale(0.2); filter: brightness(5); opacity: 0; }
}

.fx-core-flash {
    position: absolute;
    width: 44px;
    height: 44px;
    background: #FFFFFF;
    box-shadow: 0 0 20px 5px rgba(255, 255, 255, 0.8);
    border-radius: 6px;
    z-index: 600;
    pointer-events: none;
    animation: core-flash-pop 0.3s ease-out forwards;
}

@keyframes core-flash-pop {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.3); opacity: 1; }
    100% { transform: scale(2); opacity: 0; }
}

.fx-laser-beam {
    position: absolute;
    background: #fff;
    box-shadow: 0 0 15px var(--color); 
    border-radius: 4px;
    z-index: 400;
    pointer-events: none;
    opacity: 0.8;
}

.laser-h { height: 2px; width: 0; animation: laser-wipe-h 0.3s ease-out forwards; }
.laser-v { width: 2px; height: 0; animation: laser-wipe-v 0.3s ease-out forwards; }
@keyframes laser-wipe-h { 0% { width: 0; opacity: 1; } 50% { width: 100%; opacity: 1; } 100% { width: 100%; opacity: 0; } }
@keyframes laser-wipe-v { 0% { height: 0; opacity: 1; } 50% { height: 100%; opacity: 1; } 100% { height: 100%; opacity: 0; } }

.fx-spark {
    position: absolute;
    width: 4px;
    height: 4px;
    background: var(--color);
    box-shadow: 0 0 4px var(--color);
    border-radius: 1px;
    z-index: 550;
    pointer-events: none;
    animation: spark-fly 0.5s ease-out forwards;
}
@keyframes spark-fly {
    0% { transform: translate(0, 0) scale(1.5); opacity: 1; }
    60% { opacity: 1; }
    100% { transform: translate(var(--tx), var(--ty)) scale(0); opacity: 0; }
}

@keyframes flashbang-dim { 0% { background: rgba(255,255,255,0.2); } 100% { background: transparent; } }
.flashbang-active { animation: flashbang-dim 0.15s linear; }

#grid-wrapper::after, #grid-wrapper::before {
    content: '';
    position: absolute;
    border-radius: var(--rad-grid);
    pointer-events: none;
    opacity: 0; 
}

#grid-wrapper::after {
    inset: 0;
    z-index: 5;
    background: var(--flash-color, #fff);
    mix-blend-mode: screen; 
}

#grid-wrapper::before {
    inset: -4px; 
    z-index: -1;
    border: 3px solid var(--flash-color, transparent);
    box-shadow: 0 0 20px var(--flash-color, transparent);
}

@keyframes gpu-flash { 0% { opacity: 1; } 100% { opacity: 0; } }

/* TRIGGERS */
#grid-wrapper:has(.combo-popup[style*="00d2d3"]) { --flash-color: #00d2d3; }
#grid-wrapper:has(.combo-popup[style*="00d2d3"])::after, #grid-wrapper:has(.combo-popup[style*="00d2d3"])::before { animation: gpu-flash 0.5s ease-out forwards; }

#grid-wrapper:has(.combo-popup[style*="ff9ff3"]) { --flash-color: #ff9ff3; }
#grid-wrapper:has(.combo-popup[style*="ff9ff3"])::after, #grid-wrapper:has(.combo-popup[style*="ff9ff3"])::before { animation: gpu-flash 0.5s ease-out forwards; }

#grid-wrapper:has(.combo-popup[style*="54a0ff"]) { --flash-color: #54a0ff; }
#grid-wrapper:has(.combo-popup[style*="54a0ff"])::after, #grid-wrapper:has(.combo-popup[style*="54a0ff"])::before { animation: gpu-flash 0.5s ease-out forwards; }

#grid-wrapper:has(.combo-popup[style*="feca57"]),
#grid-wrapper:has(.combo-popup[style*="5f27cd"]),
#grid-wrapper:has(.combo-popup[style*="ff6b6b"]),
#grid-wrapper:has(.combo-popup[style*="f1c40f"]),
#grid-wrapper:has(.combo-popup[style*="00ffcc"]) { --flash-color: #fff; }

#grid-wrapper:has(.combo-popup[style*="feca57"]) { --flash-color: #feca57; }
#grid-wrapper:has(.combo-popup[style*="5f27cd"]) { --flash-color: #5f27cd; }
#grid-wrapper:has(.combo-popup[style*="ff6b6b"]) { --flash-color: #ff6b6b; }
#grid-wrapper:has(.combo-popup[style*="f1c40f"]) { --flash-color: #f1c40f; }
#grid-wrapper:has(.combo-popup[style*="00ffcc"]) { --flash-color: #00ffcc; }

#grid-wrapper:has(.combo-popup[style*="feca57"])::after, #grid-wrapper:has(.combo-popup[style*="feca57"])::before,
#grid-wrapper:has(.combo-popup[style*="5f27cd"])::after, #grid-wrapper:has(.combo-popup[style*="5f27cd"])::before,
#grid-wrapper:has(.combo-popup[style*="ff6b6b"])::after, #grid-wrapper:has(.combo-popup[style*="ff6b6b"])::before,
#grid-wrapper:has(.combo-popup[style*="f1c40f"])::after, #grid-wrapper:has(.combo-popup[style*="f1c40f"])::before,
#grid-wrapper:has(.combo-popup[style*="00ffcc"])::after, #grid-wrapper:has(.combo-popup[style*="00ffcc"])::before {
    animation: gpu-flash 0.5s ease-out forwards;
}

/* =====================================================
   NEW RECORD OVERLAY (FLOATING CARD SYSTEM)
   ===================================================== */

/* 1. THE BACKDROP (Full Screen, Fixed, PURPLE) */
#new-record-overlay {
    position: fixed; /* BREAKS OUT OF GAME COLUMN */
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: 6000;
    
    /* THE FULL SCREEN PURPLE GRADIENT */
    background: linear-gradient(180deg, #8E2DE2 0%, #4A00E0 100%);
    
    display: none;
    opacity: 0;
    
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

#new-record-overlay:not(.hidden) {
    display: flex;
    animation: overlayFadeIn 0.5s ease forwards;
    animation-delay: 0.55s; 
}

/* 2. THE CONTENT CONTAINER (Transparent, Centered) */
.nr-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 10;
    
    position: relative;
    background: transparent; /* No Card Background */
    width: 100%;
    max-width: none; /* Allow full width if needed, but centering handles it */
    padding: 0;
    border-radius: 0;
    
    box-shadow: none;
    border: none;
}

/* --- 1. The Crown (Pop & Float) --- */
.nr-crown-wrapper {
    width: 140px;
    height: 100px;
    margin-bottom: 20px; 
    opacity: 0; 
}

#new-record-overlay:not(.hidden) .nr-crown-wrapper {
    animation: 
        crown-pop-enter 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.5) forwards,
        crown-float-idle 3s ease-in-out infinite 0.6s;
    animation-delay: 0.7s, 1.3s;
}

.nr-crown-svg {
    width: 100%; height: 100%;
    filter: drop-shadow(0 0 20px rgba(255, 215, 0, 0.6));
}

@keyframes crown-pop-enter {
    0% { transform: scale(0) translateY(50px); opacity: 0; }
    60% { transform: scale(1.2) translateY(0); opacity: 1; }
    100% { transform: scale(1) translateY(0); opacity: 1; }
}

@keyframes crown-float-idle {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

/* --- 2. "Best Score!" Title --- */
.nr-title-container {
    overflow: hidden; 
    margin-bottom: 30px;
}

.nr-title {
    font-family: 'Verdana', 'Segoe UI', sans-serif;
    font-size: 2.5rem; 
    font-weight: 800; 
    color: #FFD700; 
    text-transform: uppercase;
    letter-spacing: 2px;
    text-shadow: 0 4px 0 rgba(0,0,0,0.2);
    margin: 0;
    line-height: 1;
    
    opacity: 0;
    transform: translateX(-50px);
}

#new-record-overlay:not(.hidden) .nr-title {
    animation: slide-in-snap 0.5s cubic-bezier(0.25, 1, 0.5, 1) forwards;
    animation-delay: 1.0s;
}

@keyframes slide-in-snap {
    0% { opacity: 0; transform: translateX(-50px); }
    100% { opacity: 1; transform: translateX(0); }
}

/* --- 3. Score Values --- */
.nr-score-label {
    font-family: 'Verdana', sans-serif;
    font-size: 1rem;
    font-weight: 600; 
    color: rgba(255,255,255,0.8);
    letter-spacing: 3px;
    text-transform: uppercase;
    margin: 0 0 5px 0;
    opacity: 0;
    animation: simple-fade 0.5s ease forwards 1.2s;
}

#nr-score-val {
    font-family: 'Verdana', sans-serif;
    font-size: 3.5rem; 
    font-weight: 700; 
    color: #ffffff;
    margin: 0 0 40px 0;
    text-shadow: 0 4px 15px rgba(255,255,255,0.3);
}

@keyframes simple-fade { to { opacity: 1; } }

/* --- 4. Premium Gold Button --- */
.nr-gold-btn {
    background: linear-gradient(180deg, #FDB931 0%, #F57F17 100%);
    box-shadow: 0 5px 0 #E65100, 0 20px 30px rgba(0,0,0,0.3);
}

.nr-gold-btn:active {
    box-shadow: 0 0 0 #E65100;
}

/* --- 5. Confetti Particles --- */
#confetti-container {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 1;
}

.confetti-piece {
    position: absolute;
    width: 10px; height: 10px;
    background: #fff;
    opacity: 0;
}

@keyframes confetti-fall {
    0% { transform: translate(0, 0) rotate(0deg); opacity: 1; }
    25% { opacity: 1; }
    100% { transform: translate(var(--tx), var(--ty)) rotate(var(--rot)); opacity: 0; }
}

/* --- TUTORIAL HAND ANIMATION --- */
.tutorial-hand {
    position: fixed;
    z-index: 9999;
    font-size: 40px; /* Size of the hand */
    pointer-events: none;
    filter: drop-shadow(0 4px 6px rgba(0,0,0,0.4));
    will-change: transform, opacity;
    transition: opacity 0.3s;
}

/* The actual hand icon (using an emoji for instant compatibility) */
.tutorial-hand::before {
    content: '👆'; 
    display: block;
    transform: rotate(-30deg); /* Tilt it to look natural */
}

/* The "Press" Animation */
@keyframes hand-press {
    0% { transform: scale(1); }
    50% { transform: scale(0.8); }
    100% { transform: scale(1); }
}

/* The "Guide" Motion (Tray to Grid) */
@keyframes hand-guide {
    0% { transform: translate(0, 0); opacity: 0; }
    20% { opacity: 1; transform: translate(0, 0); } /* Start at Tray */
    30% { transform: scale(0.9); } /* Press */
    70% { transform: translate(var(--move-x), var(--move-y)); } /* Move to Grid */
    80% { transform: translate(var(--move-x), var(--move-y)) scale(0.9); } /* Release */
    100% { transform: translate(var(--move-x), var(--move-y)); opacity: 0; }
}

.hand-active {
    animation: hand-guide 2.5s infinite;
}

/* ============================================
   TUTORIAL GHOST DRAG SYSTEM (DOM-LOCKED)
   ============================================ */

.tutorial-wrapper {
    position: fixed;
    z-index: 9999;
    pointer-events: none;
    top: 0; left: 0;
    
    /* Variables set by JS */
    --start-x: 0px;
    --start-y: 0px;
    --move-x: 0px;
    --move-y: 0px;
    --scale: 1; 
    
    /* 3.5s Duration (Relaxed) */
    animation: guide-float-path 3.5s infinite cubic-bezier(0.2, 0.6, 0.3, 1);
    will-change: transform, opacity;
    
    transform-origin: top left;
}

/* 1. The Hand Icon - IMAGE BASED (LARGER) */
.tutorial-hand-img {
    /* INCREASED SIZE: From 90px to 160px */
    width: 160px; 
    height: 160px;
    
    position: absolute;
    z-index: 2147483647; /* Max Z-Index */
    
    top: 50%; 
    left: 50%;
    
    /* ALIGNMENT ADJUSTMENT:
       Since the image is bigger, we tweak the offset slightly to keep 
       the fingertip right on the target block.
    */
    transform: translate(-25%, -15%); 
    
    /* THE IMAGE SOURCE */
    background-image: url('hand.png');
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    
    /* Stronger shadow for the larger element */
    filter: drop-shadow(0 15px 15px rgba(0,0,0,0.4));
    
    pointer-events: none;
}

/* 2. KILL THE OLD EMOJI (Force Removal) */
.tutorial-hand-img::before {
    content: none !important;
    display: none !important;
}

/* 2. The Ghost Piece */
.tutorial-ghost-piece {
    position: relative; 
    opacity: 0.9; 
    filter: brightness(1.1) drop-shadow(0 10px 20px rgba(0,0,0,0.4));
}

.tutorial-ghost-piece .block {
    position: absolute;
    box-shadow: inset 3px 3px 0px 0px rgba(255,255,255,0.5),
                inset -3px -3px 0px 0px rgba(0,0,0,0.2);
    border-radius: 6px;
}

/* --- THE ANIMATION LOOP --- */
@keyframes guide-float-path {
    0% {
        opacity: 0;
        transform: translate(var(--start-x), var(--start-y)) scale(var(--scale));
    }
    15% {
        opacity: 1;
        transform: translate(var(--start-x), var(--start-y)) scale(var(--scale)); 
    }
    30% {
        transform: translate(var(--start-x), calc(var(--start-y) - 20px)) scale(calc(var(--scale) * 1.1)); 
    }
    60% {
        transform: translate(calc(var(--start-x) + var(--move-x)), calc(var(--start-y) + var(--move-y))) scale(var(--scale)); 
    }
    85% {
        opacity: 1;
        transform: translate(calc(var(--start-x) + var(--move-x)), calc(var(--start-y) + var(--move-y))) scale(var(--scale)); 
    }
    100% {
        opacity: 0;
        transform: translate(calc(var(--start-x) + var(--move-x)), calc(var(--start-y) + var(--move-y))) scale(var(--scale));
    }
}

/* --- TUTORIAL INSTRUCTION TEXT --- */
.tutorial-text {
    position: absolute;
    top: 40px; /* Sits inside the empty header space */
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    
    font-family: 'Nunito', sans-serif; /* Matches game aesthetic */
    font-size: 24px;
    font-weight: 800;
    color: #ffffff;
    text-align: center;
    line-height: 1.3;
    text-shadow: 0 2px 4px rgba(0,0,0,0.4);
    
    z-index: 2000;
    pointer-events: none;
    
    opacity: 0;
    animation: fade-in-text 0.6s ease-out forwards 0.5s;
}

@keyframes fade-in-text {
    0% { opacity: 0; transform: translate(-50%, 10px); }
    100% { opacity: 1; transform: translate(-50%, 0); }
}