:root {
    --bg-dark: #121212;
    --gold: #F1C40F;
    --crimson: #D32F2F;
    --green: #2ECC71;
    --blue: #3498DB;
    --text-main: #E0E0E0;
    --card-ratio: 500 / 726;
    --max-width: 600px;
    --google-blue: #4285F4;
}

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

body {
    background-color: var(--bg-dark);
    font-family: 'Lato', sans-serif;
    color: var(--text-main);
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    width: 100vw;
}

.bg-texture {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background-image: url('assets/bg_game.webp');
    background-size: 500px; opacity: 0.3; z-index: -1;
}

/* === SCREENS === */
.screen {
    width: 100%; height: 100%;
    max-width: var(--max-width);
    position: relative;
    display: flex; flex-direction: column;
}
.hidden { display: none !important; }

/* === HOME SCREEN === */
#home-screen { justify-content: center; align-items: center; z-index: 50; }
.home-content { text-align: center; width: 80%; max-width: 300px; }
.main-logo { width: 100%; margin-bottom: 30px; animation: breathe 3s infinite ease-in-out; cursor: pointer; }
.menu-buttons { display: flex; flex-direction: column; gap: 12px; }

/* BUTTONS */
.dungeon-btn {
    background: linear-gradient(to bottom, #3E3E3E, #222222);
    border: 2px solid #555; border-radius: 6px; color: #ccc;
    font-family: 'Cinzel', serif; font-weight: 700; font-size: 1rem;
    padding: 14px; text-transform: uppercase; cursor: pointer;
    box-shadow: 0 4px 0 #111; transition: transform 0.1s, box-shadow 0.1s;
    position: relative;
}
.dungeon-btn:disabled, .dungeon-btn.loading { 
    opacity: 0.6; cursor: not-allowed; transform: none; box-shadow: 0 4px 0 #111; 
}
.dungeon-btn:active:not(:disabled):not(.loading) { 
    transform: translateY(4px); box-shadow: none; 
}
.btn-red { border-color: var(--crimson); color: var(--crimson); }
.btn-secondary { background: transparent; }

/* === GAME LAYOUT === */
#game-container {
    justify-content: space-between;
    padding: 10px;
    padding-bottom: 20px;
}
#animation-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 2500; }

/* Header */
.game-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 10px 15px; background: rgba(0,0,0,0.6); border-radius: 12px;
    margin-bottom: 5px; height: 60px;
}
.stat-box { font-size: 1.4rem; font-weight: bold; display: flex; align-items: center; gap: 10px; }
.hp-box { color: var(--crimson); }
.stat-center { text-align: center; line-height: 1.1; }
.score-display { color: var(--gold); font-weight: 700; font-size: 1.1rem; }
.time-display { font-size: 0.8rem; color: #888; }
.menu-btn { background: #333; color: #ccc; border: 1px solid #555; padding: 8px 12px; border-radius: 4px; font-weight: bold; cursor: pointer; }
@keyframes beat { 0% { transform: scale(1); } 50% { transform: scale(1.2); } 100% { transform: scale(1); } }
.heart-icon { display: inline-block; animation-name: none; }

/* Room */
.room-container { display: flex; justify-content: space-between; width: 100%; margin: 10px 0; }
.card-slot { width: 23%; aspect-ratio: var(--card-ratio); border: 2px dashed #444; border-radius: 8px; position: relative; background: rgba(0,0,0,0.2); }
.card {
    position: absolute; top: 0; left: 0;
    width: 100%; height: 100%;
    background-size: 100% 100%;
    border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.5);
    cursor: grab; z-index: 10; touch-action: none; will-change: transform;
}
.card.dragging { z-index: 999 !important; cursor: grabbing; box-shadow: 0 15px 30px rgba(0,0,0,0.8); transform: scale(1.15); }
.card.flying { z-index: 100; transition: transform 0.3s ease-out, opacity 0.3s; position: fixed !important; }

/* Bottom Area */
.bottom-area { display: flex; justify-content: space-between; align-items: flex-end; height: auto; }
.pile-container { width: 30%; display: flex; flex-direction: column; align-items: center; position: relative; }
.card-back, .slot-border { width: 100%; aspect-ratio: var(--card-ratio); border-radius: 8px; position: relative; }
.card-back { background-size: cover; }
.slot-border { border: 2px solid #555; background: rgba(0,0,0,0.4); display: flex; align-items: center; justify-content: center; }
.slot-text { font-size: 0.8rem; color: #666; font-weight: bold; pointer-events: none;}
.weapon-slot { border-color: var(--gold); overflow: visible; }
.pile-label { font-size: 0.8rem; color: #777; margin-top: 5px; }

/* Stacks */
#weapon-stack-container, #discard-visual { position: absolute; top:0; left:0; width:100%; height:100%; pointer-events: none; }
.stacked-card { position: absolute; width: 100%; height: 100%; border-radius: 8px; box-shadow: 0 -2px 4px rgba(0,0,0,0.5); background-size: 100% 100%; }

/* WEAPON BONUS BADGE */
#weapon-bonus-badge {
    position: absolute; top: -12px; right: -8px;
    background: var(--crimson); color: white;
    font-weight: bold; border-radius: 50%; width: 26px; height: 26px;
    font-size: 0.9rem; display: flex; align-items: center; justify-content: center;
    border: 2px solid #fff; z-index: 100;
    box-shadow: 0 2px 4px rgba(0,0,0,0.5);
}

/* === OVERLAYS & DIALOGS === */
.overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.9); display: flex; justify-content: center; align-items: center; z-index: 1000; }
.dialog-frame {
    background: #1E1E1E; border: 2px solid var(--crimson); border-radius: 12px;
    padding: 4px; box-shadow: 0 0 30px rgba(0,0,0,0.8);
    width: 90%; max-width: 350px; max-height: 90vh;
    display: flex; flex-direction: column;
}
.dialog-content { background: linear-gradient(to bottom, #2A2A2A, #101010); padding: 15px; border: 1px solid #444; border-radius: 8px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.dialog-frame h2 { color: var(--text-main); text-align: center; margin: 10px 0; font-family: 'Cinzel', serif; }

/* Drain / Tally Overlay */
#drain-overlay { display: flex; justify-content: center; align-items: center; }
.drain-content { display: flex; flex-direction: column; align-items: center; text-align: center; }
#drain-overlay h2 { margin-bottom: 10px; font-size: 2.5rem; }
.drain-score { font-size: 5rem; font-weight: 900; color: var(--gold); text-shadow: 0 0 15px #000; }
.final-score-text { font-size: 1rem; color: #888; letter-spacing: 2px; margin-bottom: 25px; }
.drain-card-display { width: 140px; aspect-ratio: var(--card-ratio); background-size: cover; border-radius: 8px; box-shadow: 0 0 20px #000; margin-top: 20px; margin-bottom: 10px; animation: popIn 0.2s ease-out; }
.drain-text { font-size: 3rem; font-weight: 900; text-shadow: 0 0 10px #000; font-family: 'Cinzel', serif; }

/* Game Over Dialog */
.stats-container { display: flex; flex-direction: column; gap: 5px; }
.stats-row {
    display: flex; justify-content: space-between; font-size: 1rem;
    color: #aaa; padding: 4px 0; opacity: 0;
}
.stats-row span { color: #888; }
.stats-row b { color: var(--gold); font-weight: bold; }
@keyframes fadeInUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
.stats-row.visible { animation: fadeInUp 0.5s forwards ease-out; }


/* RULES SLIDE-IN PANEL */
.rules-panel-container { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 2000; pointer-events: none; }
.rules-overlay-bg { position: absolute; width: 100%; height: 100%; background: rgba(0,0,0,0.7); opacity: 0; transition: opacity 0.3s; }
.rules-panel {
    position: absolute; right: 0; top: 0; height: 100%;
    width: 90%; max-width: 500px;
    background: #121212;
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    pointer-events: all;
}
body.rules-visible .rules-panel-container { pointer-events: all; }
body.rules-visible .rules-overlay-bg { opacity: 1; }
body.rules-visible .rules-panel { transform: translateX(0); }
.rules-close-btn { position: absolute; top: 10px; right: 10px; font-size: 2rem; color: #888; background: none; border: none; cursor: pointer; }
.rules-fullscreen { width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; padding: 10px; overflow: hidden; }
.rules-logo { width: 60%; max-width: 250px; margin-top: 5px; }
.rules-main-title { color: var(--gold); font-size: 1.8rem; margin: 10px 0 15px 0; font-family: 'Cinzel', serif; text-shadow: 0 0 5px #000; text-align: center; }
.rules-scroll-container { width: 100%; overflow-y: auto; padding: 0 15px 40px 15px; }
.rule-container { background: #1E1E1E; border: 1px solid #333; border-radius: 12px; padding: 16px; margin-bottom: 24px; }
.rule-header { color: #FFF; font-size: 1.1rem; font-weight: bold; letter-spacing: 1px; text-transform: uppercase; font-family: 'Cinzel', serif; }
.rule-divider { height: 2px; width: 40px; background: var(--gold); margin: 12px 0; }
.white-bold { color: #FFF; font-weight: bold; font-size: 0.9rem; }
.gold-text { color: var(--gold); }
.rule-container p { color: #BBB; font-size: 0.9rem; line-height: 1.5; margin-bottom: 4px; }
.rule-feature { background: #1E1E1E; border: 1px solid #333; border-radius: 12px; padding: 12px; margin-bottom: 16px; display: flex; align-items: center; gap: 12px; }
.rf-left { padding: 8px; }
.rf-img { width: 80px; height: auto; border-radius: 6px; box-shadow: 0 4px 8px rgba(0,0,0,0.5); }
.rf-right { flex: 1; }
.rf-title { color: #FFF; font-size: 1rem; font-weight: bold; margin-bottom: 8px; font-family: 'Cinzel', serif; }
.rf-right p { margin-bottom: 4px; font-size: 0.9rem; line-height: 1.3; }
.math-box { background: rgba(0,0,0,0.5); border: 1px solid #444; border-radius: 4px; padding: 8px; margin-top: 8px; text-align: center; }
.math-title { color: #888; font-size: 0.7rem; font-weight: bold; margin-bottom: 4px; }
.chain-example { text-align: center; font-family: 'Cinzel', serif; }
.joker-row { display: flex; align-items: center; margin-top: 12px; }
.joker-img { width: 70px; height: auto; border-radius: 4px; margin-right: 16px; flex-shrink: 0; }
.joker-text p { font-size: 0.8rem; color: #BBB; line-height: 1.3; margin-top: 4px; }
.divider-line { height: 1px; background: #333; margin: 12px 0; }
.endgame-row { display: flex; gap: 8px; }
.endgame-box { flex: 1; background: rgba(255,255,255,0.05); border: 1px solid #444; border-radius: 8px; padding: 12px; font-size: 0.9rem; }
.endgame-box p { font-size: 0.8rem; }
.victory-box { border-color: var(--green); background: rgba(46, 204, 113, 0.1); }
.defeat-box { border-color: var(--crimson); background: rgba(183, 28, 28, 0.1); }
.eg-title { font-weight: 900; font-size: 1rem; margin-bottom: 4px; }
.victory-box .eg-title { color: var(--green); }
.defeat-box .eg-title { color: var(--crimson); }

/* CUSTOM SCROLLBAR */
.rules-scroll-container::-webkit-scrollbar,
#leaderboard-list::-webkit-scrollbar { width: 8px; }
.rules-scroll-container::-webkit-scrollbar-track,
#leaderboard-list::-webkit-scrollbar-track { background: #1E1E1E; }
.rules-scroll-container::-webkit-scrollbar-thumb,
#leaderboard-list::-webkit-scrollbar-thumb { background: #555; border-radius: 4px; }
.rules-scroll-container::-webkit-scrollbar-thumb:hover,
#leaderboard-list::-webkit-scrollbar-thumb:hover { background: var(--gold); }

/* SETTINGS */
#settings-modal { z-index: 10000; }

.setting-row { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; }
.switch { position: relative; display: inline-block; width: 50px; height: 28px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #555; transition: .4s; border-radius: 28px; }
.slider:before { position: absolute; content: ""; height: 20px; width: 20px; left: 4px; bottom: 4px; background-color: white; transition: .4s; border-radius: 50%; }
input:checked + .slider { background-color: var(--crimson); }
input:checked + .slider:before { transform: translateX(22px); }

/* PREVIEW MODAL */
#preview-card-container { display: flex; justify-content: center; gap: 10px; margin-bottom: 15px; }
.preview-card { width: 80px; aspect-ratio: var(--card-ratio); background-size: cover; border-radius: 6px; box-shadow: 0 2px 5px rgba(0,0,0,0.5); }

/* DECK SELECTION MODAL */
.deck-carousel { display: flex; align-items: center; justify-content: space-between; margin-bottom: 15px; }
.arrow-btn { background: none; border: none; color: var(--gold); font-size: 3rem; font-weight: 900; cursor: pointer; padding: 0 20px; }
.deck-preview-area { position: relative; width: 140px; height: 180px; display: flex; justify-content: center; align-items: center; }
.deck-preview-card {
    position: absolute; width: 100px; height: calc(100px / (var(--card-ratio)));
    background-size: cover; border-radius: 8px; box-shadow: 0 4px 10px #000;
    transition: transform 0.3s ease-in-out;
}
#deck-preview-card-1 { transform: translateX(-15px) rotateZ(-10deg); z-index: 1; }
#deck-preview-card-2 { transform: translateX(15px) rotateZ(10deg); z-index: 2; }
.deck-name-display { text-align: center; font-size: 1.2rem; font-weight: bold; color: var(--gold); margin-bottom: 20px; font-family: 'Cinzel', serif; }


/* FX & ANIMATIONS */
.float-text {
    position: fixed; left: 50%;
    transform: translateX(-50%);
    width: 100%; text-align: center; font-size: 2.5rem; font-weight: 900;
    text-shadow: 3px 3px 0 #000; animation: floatUp 1.2s forwards;
    pointer-events: none; z-index: 2000;
    top: 45%;
}
.chain-toast {
    top: 65%;
}

/* Fanfare Bonus Toast */
.bonus-toast {
    position: fixed;
    top: 20%;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    text-align: center;
    font-size: 3rem;
    font-weight: 900;
    color: var(--gold);
    text-shadow: 0 0 20px #FFD700, 3px 3px 0 #000;
    font-family: 'Cinzel', serif;
    animation: fanfareAnim 2s forwards ease-out;
    pointer-events: none;
    z-index: 2100;
}

@keyframes fanfareAnim {
    0% { transform: translate(-50%, 50px) scale(0.5); opacity: 0; }
    20% { transform: translate(-50%, 0) scale(1.2); opacity: 1; }
    80% { transform: translate(-50%, 0) scale(1); opacity: 1; }
    100% { transform: translate(-50%, -50px) scale(1); opacity: 0; }
}

@keyframes floatUp { 0% { opacity: 1; transform: translate(-50%, -50%) scale(1); } 100% { opacity: 0; transform: translate(-50%, -200%) scale(1.5); } }
@keyframes popIn { 0% { transform: scale(0); } 100% { transform: scale(1); } }
.shake { animation: shake 0.2s cubic-bezier(.36,.07,.19,.97) both; }
@keyframes shake { 10%, 90% { transform: translate3d(-2px, 0, 0); } 20%, 80% { transform: translate3d(4px, 0, 0); } 30%, 50%, 70% { transform: translate3d(-6px, 0, 0); } 40%, 60% { transform: translate3d(6px, 0, 0); } }

.swap-arrow {
    position: absolute; top: -10px; left: 50%;
    font-size: 2rem; color: var(--gold); text-shadow: 0 0 5px #000;
    animation: bounce 1s infinite;
}
@keyframes bounce { 0%, 100% { transform: translate(-50%, 0); } 50% { transform: translate(-50%, -10px); } }

.buff-anim-card {
    position: fixed; top: 50%; left: 50%;
    width: 120px; aspect-ratio: var(--card-ratio);
    background-size: cover; border-radius: 8px;
    z-index: 2000;
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
    transition: transform 0.6s, opacity 0.4s 0.2s;
}
@keyframes breathe { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.03); } }

/* NEW HIGH SCORE MESSAGE */
#new-highscore-msg {
    margin: 10px 0;
    font-size: 1.3rem;
    font-weight: 900;
    color: var(--gold);
    text-align: center;
    font-family: 'Cinzel', serif;
    animation: pulseGold 1.5s infinite;
}
@keyframes pulseGold {
    0% { transform: scale(1); text-shadow: 0 0 5px #FFD700; }
    50% { transform: scale(1.1); text-shadow: 0 0 15px #FFD700; }
    100% { transform: scale(1); text-shadow: 0 0 5px #FFD700; }
}

/* --- AUTH & USER STATUS STYLES --- */
.signin-status {
    margin-top: 25px;
    font-size: 0.9rem;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 50px; /* Reserve space */
}

.btn-google {
    background: var(--google-blue);
    color: white;
    border-color: #1a73e8;
    box-shadow: 0 4px 0 #0d47a1;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

#user-info {
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(0,0,0,0.3);
    padding: 8px 12px;
    border-radius: 20px;
}

#user-pic {
    width: 24px;
    height: 24px;
    border-radius: 50%;
}

.link-btn {
    background: none;
    border: none;
    color: #aaa;
    text-decoration: underline;
    cursor: pointer;
    font-size: 0.8rem;
}

/* --- LEADERBOARD STYLES --- */
#leaderboard-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 200px;
    max-height: 350px; /* Scrollable area */
    overflow-y: auto;
    padding-right: 5px;
}

#leaderboard-user-rank {
    margin-top: 10px;
    border-top: 1px solid #444;
    padding-top: 10px;
}

.leaderboard-row {
    display: flex;
    align-items: center;
    padding: 8px;
    background: rgba(0,0,0,0.2);
    border: 1px solid #333;
    border-radius: 4px;
    font-size: 1rem;
    transition: all 0.2s;
}

.leaderboard-rank {
    font-weight: bold;
    color: #888;
    width: 35px;
    flex-shrink: 0;
    font-size: 1.1rem;
    font-family: 'Cinzel', serif;
}

.leaderboard-name {
    flex-grow: 1;
    color: #aaa;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: normal;
}

.leaderboard-score {
    font-weight: bold;
    color: #ccc;
    font-family: 'Cinzel', serif;
}

.leaderboard-loading {
    text-align: center;
    color: #888;
    margin-top: 20px;
}

/* Leaderboard Rank & User Highlight Styles */
.leaderboard-row.is-user {
    background: rgba(255, 0, 255, 0.15);
    border-color: magenta;
}
.leaderboard-row.is-user .leaderboard-name,
.leaderboard-row.is-user .leaderboard-score {
    color: #ff80ff; /* Light magenta */
    font-weight: bold;
}

.leaderboard-row.rank-gold { background: rgba(255, 215, 0, 0.15); border-color: gold; }
.leaderboard-row.rank-gold .leaderboard-rank { color: gold; }
.leaderboard-row.rank-gold .leaderboard-name { color: #fff; font-weight: bold; }
.leaderboard-row.rank-gold .leaderboard-score { color: gold; }

.leaderboard-row.rank-silver { background: rgba(192, 192, 192, 0.15); border-color: silver; }
.leaderboard-row.rank-silver .leaderboard-rank { color: silver; }
.leaderboard-row.rank-silver .leaderboard-name { color: #eee; font-weight: bold; }
.leaderboard-row.rank-silver .leaderboard-score { color: silver; }

.leaderboard-row.rank-bronze { background: rgba(205, 127, 50, 0.15); border-color: #cd7f32; }
.leaderboard-row.rank-bronze .leaderboard-rank { color: #cd7f32; }
.leaderboard-row.rank-bronze .leaderboard-name { color: #ddd; font-weight: bold; }
.leaderboard-row.rank-bronze .leaderboard-score { color: #cd7f32; }

/* --- AD OVERLAY (FIXED) --- */
#ad-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    width: 100vw; height: 100vh;
    background: rgba(0, 0, 0, 0.95); /* Opaque dark background */
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(5px);
}

/* Ensure container fits mobile screens */
.ad-container-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 320px;
    padding: 10px;
}

.ad-label {
    color: #666;
    font-size: 0.7rem;
    margin-bottom: 5px;
    letter-spacing: 1px;
    font-family: sans-serif;
    text-transform: uppercase;
}

.ad-content-box {
    background: #000;
    width: 300px;
    height: 250px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #333;
    margin-bottom: 20px;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
}

#ad-slot-container {
    width: 100%;
    height: 100%;
}

#ad-close-btn {
    background: transparent;
    border: 2px solid #555;
    color: #888;
    padding: 12px 30px;
    font-family: 'Cinzel', serif;
    font-size: 1rem;
    font-weight: bold;
    cursor: not-allowed;
    border-radius: 4px;
    transition: all 0.3s;
    width: 100%;
    text-transform: uppercase;
}

#ad-close-btn:not(:disabled) {
    cursor: pointer;
    border-color: var(--gold);
    color: var(--gold);
    background: rgba(241, 196, 15, 0.1);
}

#ad-close-btn:not(:disabled):hover {
    background: rgba(241, 196, 15, 0.2);
    transform: translateY(-2px);
}

#ad-remove-link {
    margin-top: 15px;
    color: #666;
    font-size: 0.8rem;
    text-decoration: underline;
    cursor: pointer;
}
#ad-remove-link:hover { color: #fff; }