/* Glass Tetris - Final UI v47 (Floating Card) */
:root { 
    --bg-main: #121212;
    --accent-blue: #0b6cff;
    --accent-orange: #ffab40;
    --text: #ffffff;
    --text-muted: rgba(255, 255, 255, 0.5);
    --font: 'Quicksand', sans-serif;
    --board-opacity: 0.6;
    --dim-opacity: 0.4;
    --border-subtle: rgba(255, 255, 255, 0.15);
    --card-bg: rgba(30, 30, 30, var(--board-opacity));
    --btn-bg: rgba(30, 30, 30, 0.6);
    --btn-hover: rgba(255, 255, 255, 0.1);
    --menu-header-bg: rgba(255, 255, 255, 0.05);
    --input-bg: rgba(255, 255, 255, 0.1);
    --dpad-bg: #fff;
    --dpad-border: rgba(255, 255, 255, 0.8);
}

@media (prefers-color-scheme: light) {
    :root {
        --bg-main: #f4f4f4;
        --text: #1a1a1a;
        --text-muted: rgba(0, 0, 0, 0.6);
        --border-subtle: rgba(0, 0, 0, 0.15); 
        --card-bg: rgba(255, 255, 255, var(--board-opacity)); 
        --btn-bg: rgba(255, 255, 255, 0.9);
        --btn-hover: rgba(0, 0, 0, 0.05);
        --menu-header-bg: rgba(0, 0, 0, 0.05);
        --input-bg: rgba(0, 0, 0, 0.05);
        --dpad-bg: rgba(255, 255, 255, 0.9);
        --dpad-border: rgba(0, 0, 0, 0.2);
        --accent-blue: #0056b3;
        --accent-orange: #d97706; 
    }
}

html, body { 
    margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden; 
    font-family: var(--font); color: var(--text); background: var(--bg-main); 
    transition: background 0.3s, color 0.3s;
    touch-action: none; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none;
}

#game-wrapper { width:100%; height:100%; position:relative; overflow:hidden; }
.bg-layer { position:absolute; inset:0; background-size:cover; background-position:center; transition: opacity 2s ease-in-out; z-index:0; }
.kenburns { animation: kb-move 20s infinite alternate linear; }
@keyframes kb-move { 0% { transform: scale(1); } 100% { transform: scale(1.15); } }
#dimmer { position:absolute; inset:0; background:rgba(0,0,0,var(--dim-opacity)); z-index:0; pointer-events:none; transition: background 0.2s; }
#game-stage { position:relative; z-index:1; display:flex; width:100%; height:100%; align-items:center; justify-content:center; }

#tetris-container { 
    position:relative; height:90%; aspect-ratio:10/20; 
    border: 1px solid color-mix(in srgb, var(--border-subtle), transparent 50%);
    border-radius:4px; box-shadow:0 20px 60px rgba(0,0,0,0.6); 
    backdrop-filter:blur(6px); transition: background 0.2s;
}
#tetris-canvas { width:100%; height:100%; display:block; image-rendering:pixelated; }

/* Level Display (Desktop Default) */
#level-container {
    position: absolute; top: 5px; left: 50%; transform: translateX(-50%);
    display: flex; flex-direction: column; align-items: center; z-index: 10; pointer-events: none;
}
.lvl-label { font-size: 0.7rem; font-weight: bold; text-transform: uppercase; letter-spacing: 1px; color: rgba(255,255,255,0.8); text-shadow: 0 1px 3px rgba(0,0,0,0.8); }
.lvl-val { font-size: 1.8rem; font-weight: 700; color: #fff; text-shadow: 0 2px 4px rgba(0,0,0,0.8); line-height: 1; }

#toast {
    position:absolute; top:30px; left:50%; transform:translateX(-50%) translateY(-50px);
    background: var(--accent-blue); color: #fff; padding:10px 25px;
    border-radius:30px; font-weight:bold; font-size:0.9rem; z-index:100;
    box-shadow:0 10px 20px rgba(0,0,0,0.3); pointer-events:none; opacity:0; transition:0.3s;
}
#toast.show { transform:translateX(-50%) translateY(0); opacity:1; }

#ui-left { position:absolute; top:20px; right:100%; margin-right:20px; text-align:right; width:150px; display:flex; flex-direction:column; gap:15px; }
.stat-item {
    background: var(--card-bg); border: 1px solid var(--border-subtle);
    backdrop-filter: blur(4px); padding: 10px 15px; border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.05); display: flex; flex-direction: column; align-items: center; min-width: 80px; position: relative;
}
.stat-label { font-size:0.75rem; opacity:0.8; letter-spacing:1px; text-transform:uppercase; margin-bottom:2px; font-weight: bold; }
.stat-val { font-size:1.8rem; font-weight:700; line-height:1; }

/* Score Popup Animation */
.score-pop {
    position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
    font-size: 1.5rem; color: var(--accent-orange); text-shadow: 0 2px 4px rgba(0,0,0,0.8);
    font-weight: bold; opacity: 0; pointer-events: none; z-index: 20; white-space: nowrap;
}
.score-pop.active { animation: popFade 0.8s ease-out forwards; }
@keyframes popFade {
    0% { opacity: 0; transform: translate(-50%, -20%) scale(0.5); }
    20% { opacity: 1; transform: translate(-50%, -50%) scale(1.2); }
    50% { opacity: 1; transform: translate(-50%, -60%) scale(1); }
    100% { opacity: 0; transform: translate(-50%, -100%) scale(0.9); }
}

#ui-right { position:absolute; top:20px; left:100%; margin-left:20px; width:100px; }
#next-box {
    background: var(--card-bg); border: 1px solid var(--border-subtle);
    border-radius:8px; width:80px; height:80px; display:flex; flex-direction:column; 
    align-items:center; justify-content:center; position:relative;
    backdrop-filter: blur(4px); box-shadow: 0 4px 6px rgba(0,0,0,0.05);
}
#next-box .stat-label {
    position: absolute;
    top: -20px;
    width: 100%;
    text-align: center;
    color: white; 
    text-shadow: 1px 1px 2px black;
}
#next-canvas { width:60px; height:60px; display:block; }

#ui-controls { position:absolute; bottom:20px; left:100%; margin-left:20px; display:flex; flex-direction:column; gap:12px; }
.icon-btn { 
    width:44px; height:44px; border-radius:50%; background: var(--btn-bg);
    border: 1px solid var(--border-subtle); color: var(--text); cursor:pointer; 
    display:flex; align-items:center; justify-content:center; transition:0.2s; position: relative;
}
.icon-btn:hover { background: var(--btn-hover); transform:scale(1.1); }
.icon-btn svg { width:20px; height:20px; fill:currentColor; }
.icon-btn:hover::after {
    content: attr(title); position: absolute; bottom: 125%; left: 50%; transform: translateX(-50%);
    background: rgba(0,0,0,0.85); color: #fff; padding: 6px 10px; border-radius: 6px;
    font-size: 0.75rem; font-weight: 500; white-space: nowrap; pointer-events: none; opacity: 0;
    animation: fadeInTooltip 0.2s forwards; z-index: 100; border: 1px solid rgba(255,255,255,0.1);
}
@keyframes fadeInTooltip { to { opacity: 1; transform: translateX(-50%) translateY(-5px); } }
.btn-badge { 
    position: absolute; top: -5px; right: -5px; background: var(--accent-blue); 
    color: #fff; font-size: 0.7rem; font-weight: bold; width: 18px; height: 18px; border-radius: 50%; 
    display: flex; align-items: center; justify-content: center; border: 1px solid var(--border-subtle); pointer-events: none;
}
.btn-orange-glow { border-color: rgba(255, 171, 64, 0.5) !important; color: var(--accent-orange) !important; }
.btn-pulse { animation: orange-pulse 2s infinite; }
@keyframes orange-pulse { 
    0% { box-shadow: 0 0 0 0 rgba(255, 171, 64, 0.4); } 
    70% { box-shadow: 0 0 0 8px rgba(255, 171, 64, 0); } 
    100% { box-shadow: 0 0 0 0 rgba(255, 171, 64, 0); } 
}
.btn-muted { opacity: 0.5; position:relative; }
.btn-muted::after { content:''; position:absolute; width:100%; height:2px; background:#ff4444; transform:rotate(45deg); }
#mobile-controls { display:none; }

#btn-close-menu { color: var(--text) !important; }

/* REVISED OVERLAY FOR FLOATING CARD */
#overlay-modal { 
    position:absolute; inset:0; 
    z-index:20; 
    display:none; 
    flex-direction:column; align-items:center; justify-content:center; text-align:center; padding:20px;
    /* Transparent and pass-through so buttons below are clickable */
    background: transparent; 
    pointer-events: none; 
}
/* The actual card logic */
.modal-inner {
    background: rgba(0, 0, 0, 0.95);
    padding: 30px;
    border-radius: 16px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
    border: 1px solid var(--border-subtle);
    backdrop-filter: blur(4px);
    pointer-events: auto; /* Capture clicks inside the box */
    display: flex;
    flex-direction: column;
    align-items: center;
    max-width: 85%;
    width: auto;
}

#overlay-title { font-size:2.5rem; font-weight:bold; margin-bottom:10px; color: #fff; } 
#overlay-msg { opacity:0.8; margin-bottom:25px; max-width:300px; line-height:1.5; color: #fff; }
#menu-backdrop { position:fixed; inset:0; background:rgba(0,0,0,0.60); z-index:50; display:none; align-items:center; justify-content:center; }
#menu-window { 
    background: var(--bg-main); width:90%; max-width:450px; max-height:85vh; border-radius:12px; 
    border:1px solid var(--border-subtle); display:flex; flex-direction:column; 
}
#menu-header { padding:15px; background: var(--menu-header-bg); display:flex; justify-content:space-between; align-items:center; }
#menu-body { padding:20px; overflow-y:auto; flex:1; }
#menu-footer { padding:10px 15px; border-top:1px solid var(--border-subtle); font-size:0.65rem; text-align:center; opacity:0.5; line-height:1.4; }

.rule-block { margin-bottom: 35px; border-bottom: 1px solid var(--border-subtle); padding-bottom: 20px; }
.rule-block:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }
.rule-title { display: block; font-size: 1.2rem; color: var(--accent-orange); margin-bottom: 12px; font-weight: bold; }
.rule-text { line-height: 1.8; font-size: 1rem; opacity: 0.95; }
.rule-warning { color: var(--accent-orange); font-size: 0.9rem; display: block; margin-top: 10px; font-style: italic; line-height: 1.5; }
.help-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.help-item { font-size: 0.8rem; opacity: 0.8; }
.help-item strong { display:block; color:var(--accent-orange); margin-bottom:5px; font-size:0.9rem; }
.tab-nav { display:flex; gap:15px; margin-bottom:20px; border-bottom:1px solid var(--border-subtle); padding-bottom:10px; overflow-x:auto; }
.tab-btn { background:none; border:none; color: var(--text-muted); cursor:pointer; font-size:1rem; font-weight:bold; white-space:nowrap; }
.tab-btn.active { color: var(--text); border-bottom:2px solid var(--accent-orange); }
.tab-content { display:none; }
.tab-content.active { display:block; }
select, input { width:100%; padding:10px; border:1px solid var(--border-subtle); color: #838181; border-radius:6px; margin-bottom:10px; box-sizing:border-box; }
.range-row { margin-bottom:15px; }
.range-row label { display:flex; justify-content:space-between; font-size:0.9rem; margin-bottom:5px; opacity:0.8; }
input[type="range"] { width:100%; -webkit-appearance:none; background: var(--input-bg); height:6px; border-radius:3px; }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance:none; width:16px; height:16px; background:var(--accent-orange); border-radius:50%; cursor:pointer; }
.action-btn { background: var(--input-bg); border:1px solid var(--border-subtle); color: #fff; padding:12px 24px; font-size:1rem; border-radius:6px; cursor:pointer; font-weight:bold; width:100%; margin-bottom:10px; transition:0.2s; }
.action-btn:hover { background: var(--btn-hover); border-color: #fff; }
.text-btn { background:none; border:none; color: white; cursor:pointer; text-decoration:underline; font-size:0.85rem; }
.score-row { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--border-subtle); padding: 8px 0; }
.s-rank { width: 25px; opacity: 0.5; font-size: 0.9rem; }
.s-name { flex: 1; font-weight: bold; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-right: 10px; }
.s-date { font-size: 0.75rem; opacity: 0.5; margin-right: 15px; }
.s-val { color: var(--accent-orange); font-weight: bold; min-width: 50px; text-align: right; }
.key-group { display:flex; gap:2px; }
.key-btn { width:18px; height:18px; background: var(--input-bg); border:1px solid var(--border-subtle); border-radius:3px; display:flex; align-items:center; justify-content:center; font-size:10px; font-weight:bold; }
.key-btn.long { width:50px; }
.touch-icon { font-size:24px; }
.dpad { position:relative; width:40px; height:40px; }
.dpad div { position:absolute; background: var(--dpad-bg); border:1px solid var(--dpad-border); }
.d-up { width:12px; height:12px; top:4px; left:14px; border-radius:2px 2px 0 0; }
.d-lr { width:32px; height:12px; top:14px; left:4px; border-radius:2px; }
.d-down { width:12px; height:12px; bottom:4px; left:14px; border-radius:0 0 2px 2px; }

/* =========================================
   MOBILE GRID LAYOUT (FIXED LEVEL POS & BORDER)
   ========================================= */
@media (max-width: 900px) {
    #game-stage { padding: 0; align-items: stretch; justify-content: center; overflow: hidden; }
    
    #tetris-container {
        display: grid; grid-template-columns: 1fr auto 1fr; grid-template-rows: auto minmax(0, 1fr) auto; 
        gap: 8px; width: 92%; margin: 0 auto; height: 100%; max-width: 100%; padding: 10px 0;         
        box-sizing: border-box; background: transparent; border: none; box-shadow: none; backdrop-filter: none;
    }

    #ui-left { display: contents; }
    .stat-item { min-width: 0 !important; width: 100%; padding: 8px 4px; }
    
    /* Stats Layout */
    #ui-left .stat-item:nth-child(1) { grid-row: 1; grid-column: 1; justify-self: start; align-self: center; align-items: flex-start; margin-bottom: 0; }
    #timer-container { grid-row: 1; grid-column: 3; justify-self: end; align-self: center; align-items: flex-end; margin-bottom: 0; }
    
    .stat-val { font-size: 1.3rem; line-height: 1.2; } 
    .stat-label { font-size: 0.65rem; margin-bottom: 2px; }
    
    /* Next Box (Center) */
    #ui-right { grid-row: 1; grid-column: 2; display: flex !important; position: static; margin: 0; justify-content: center; align-items: center; }
    #next-box { width: 55px; height: 55px; } 
    #next-canvas { width: 35px; height: 35px; margin-top: 0; }
    #next-box .stat-label { position: static; top: auto; font-size: 0.5rem; margin-bottom: 1px; }
    
    /* Board */
    #tetris-canvas {
        grid-row: 2; grid-column: 1 / span 3; background: var(--card-bg);
        border: none;
        border-radius: 4px; box-shadow: 0 10px 30px rgba(0,0,0,0.5);
        backdrop-filter: blur(6px); width: auto; height: 100%; max-height: 100%; max-width: 100%; aspect-ratio: 10/20; justify-self: center;   
    }

    /* Level */
    #level-container {
        top: 85px; 
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
        backdrop-filter: none !important;
    }
    #level-container .lvl-val { font-size: 2rem; opacity: 0.5; }
    #level-container .lvl-label { display:none; }
    
    #ui-controls { grid-row: 3; grid-column: 1 / span 3; position: static; width: 100%; margin: 0; padding-bottom: 25px; display: flex; flex-direction: row; justify-content: center; gap: 25px; }
    #btn-fs { display: none !important; } #mobile-controls { display: none !important; }
    .help-grid { grid-template-columns: 1fr; gap: 20px; } 
}