/* ===== CSS Variables ===== */
:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --accent: #e94560;
    --accent-secondary: #f5a623;
    --text-primary: #ffffff;
    --text-secondary: #a0a0b0;
    --text-muted: rgba(255, 255, 255, 0.4);
    --surface: #252542;
    --surface-light: #2e2e52;
    --border: rgba(255, 255, 255, 0.08);
    --correct: #22c55e;
    --correct-bg: rgba(34, 197, 94, 0.15);
    --wrong: #ef4444;
    --wrong-bg: rgba(239, 68, 68, 0.15);
    --heart-red: #ef4444;
    --heart-grey: #4a4a5a;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    --space-xs: 6px;
    --space-sm: 12px;
    --space-md: 16px;
    --space-lg: 24px;
    --space-xl: 32px;
    --font: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: var(--font);
    background: var(--bg-secondary);
    color: var(--text-primary);
    min-height: 100dvh;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ===== Loading Screen ===== */
#loading-screen {
    position: fixed;
    inset: 0;
    background: var(--bg-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.loading-content { text-align: center; }

.loading-title {
    font-size: 28px;
    font-weight: 800;
    color: var(--accent);
    margin-bottom: 20px;
}

.loading-spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 12px;
}

.loading-spinner.small {
    width: 24px;
    height: 24px;
    border-width: 2px;
}

.loading-text {
    font-size: 14px;
    color: var(--text-secondary);
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Landing Screen ===== */
#landing-screen {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    background: linear-gradient(160deg, var(--bg-secondary) 0%, var(--bg-primary) 50%, #1e0a1e 100%);
}

.landing-header {
    padding: var(--space-md) var(--space-lg);
    padding-top: env(safe-area-inset-top, var(--space-md));
}

.landing-header-row {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
}

.header-spacer { width: 40px; }

.landing-titles { text-align: center; }

.landing-title {
    font-size: 28px;
    font-weight: 800;
    color: var(--accent);
}

.landing-subtitle {
    font-size: 14px;
    color: var(--text-secondary);
    font-weight: 500;
    margin-top: 2px;
}

.landing-date {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
}

.header-right { width: 40px; text-align: right; }

.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 22px;
    cursor: pointer;
    padding: 4px;
}

.landing-body {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--space-lg);
    gap: var(--space-lg);
}

.landing-streak {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 8px 20px;
    font-size: 15px;
    font-weight: 600;
}

.streak-icon { font-size: 20px; }

/* Tower Hero */
.tower-hero {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.tower-icon {
    animation: towerFloat 3s ease-in-out infinite;
}

.tower-ascii {
    font-family: 'Courier New', monospace;
    font-size: 14px;
    line-height: 1.3;
    color: var(--accent);
    text-shadow: 0 0 20px rgba(233, 69, 96, 0.4), 0 0 40px rgba(233, 69, 96, 0.2);
    white-space: pre;
    text-align: center;
    user-select: none;
}

@keyframes towerFloat {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

.play-button {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius-lg);
    padding: 16px 64px;
    font-size: 18px;
    font-weight: 700;
    font-family: var(--font);
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s;
    box-shadow: 0 4px 16px rgba(233, 69, 96, 0.4);
}

.play-button:active {
    transform: scale(0.97);
}

.landing-best {
    font-size: 13px;
    color: var(--text-muted);
}

/* ===== Game Screen ===== */
#game-screen {
    position: fixed;
    inset: 0;
    background: var(--bg-primary);
    overflow: hidden;
    touch-action: none;
}

#game-canvas {
    display: block;
    width: 100%;
    height: 100%;
}

/* HUD Overlay */
.game-hud {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    padding-top: calc(env(safe-area-inset-top, 10px) + 4px);
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 10;
    user-select: none;
}

.hud-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

.hud-label {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-secondary);
}

.hud-value {
    font-size: 20px;
    font-weight: 800;
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
}

.hud-hearts {
    flex-direction: row;
    gap: 4px;
}

.heart {
    font-size: 20px;
    line-height: 1;
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.heart.full {
    opacity: 1;
}

.heart.empty {
    opacity: 0.3;
    filter: grayscale(1);
    transform: scale(0.85);
}

/* Virtual Controls */
.virtual-controls {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    padding: 20px;
    padding-bottom: calc(env(safe-area-inset-bottom, 20px) + 12px);
    z-index: 10;
    pointer-events: none;
    user-select: none;
}

.controls-left {
    display: flex;
    gap: 16px;
    pointer-events: auto;
}

.controls-right {
    pointer-events: auto;
}

.control-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid rgba(255, 255, 255, 0.3);
    background: rgba(255, 255, 255, 0.15);
    color: rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    font-size: 20px;
    cursor: pointer;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.1s;
}

.control-btn:active,
.control-btn.active {
    background: rgba(255, 255, 255, 0.35);
    border-color: rgba(255, 255, 255, 0.5);
}

.control-arrow {
    width: 56px;
    height: 56px;
    font-size: 18px;
}

.control-jump {
    width: 64px;
    height: 64px;
    font-size: 22px;
}

/* ===== End Screen ===== */
.end-screen-modal {
    position: fixed;
    inset: 0;
    z-index: 500;
    background: linear-gradient(135deg, var(--accent) 0%, #8b1a2e 30%, var(--bg-primary) 70%, var(--bg-secondary) 100%);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    animation: fadeIn 0.3s ease-out;
}

.end-screen-modal::before {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at 30% 20%, rgba(255,255,255,0.1) 0%, transparent 60%),
                radial-gradient(circle at 80% 80%, rgba(233,69,96,0.2) 0%, transparent 50%);
    pointer-events: none;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.end-screen-content {
    position: relative;
    max-width: 480px;
    margin: 0 auto;
    padding: var(--space-sm) var(--space-md);
    padding-top: env(safe-area-inset-top, var(--space-sm));
    padding-bottom: env(safe-area-inset-bottom, var(--space-lg));
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
}

/* Hero */
.end-hero {
    text-align: center;
    padding: var(--space-md) 0 var(--space-xs);
}

.end-hero-title {
    font-size: 13px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 2.5px;
    color: rgba(255, 255, 255, 0.7);
    margin-bottom: 6px;
}

.end-hero-score {
    font-size: 64px;
    font-weight: 800;
    color: white;
    line-height: 1;
    text-shadow: 0 2px 16px rgba(0,0,0,0.2);
    animation: scoreIn 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) 0.2s both;
    font-variant-numeric: tabular-nums;
}

@keyframes scoreIn {
    0% { opacity: 0; transform: scale(0.5); }
    100% { opacity: 1; transform: scale(1); }
}

.end-hero-sublabel {
    font-size: 14px;
    color: rgba(255, 255, 255, 0.7);
    font-weight: 600;
    margin-top: 2px;
}

/* Stats Row */
.end-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    width: 100%;
    padding: 4px 0;
}

.end-stat {
    padding: 12px 8px;
    text-align: center;
    position: relative;
}

.end-stat:not(:last-child)::after {
    content: '';
    position: absolute;
    right: 0;
    top: 12px;
    bottom: 12px;
    width: 1px;
    background: rgba(255, 255, 255, 0.15);
}

.end-stat-value {
    font-size: 22px;
    font-weight: 800;
    color: white;
}

.end-stat-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: rgba(255, 255, 255, 0.5);
    margin-top: 2px;
}

/* Score Breakdown Table */
.score-breakdown {
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--radius-md);
    overflow: hidden;
}

.score-breakdown-header {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: rgba(255, 255, 255, 0.5);
    padding: 10px 14px 6px;
}

.score-breakdown-table {
    width: 100%;
    border-collapse: collapse;
}

.score-breakdown-table td {
    padding: 8px 14px;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.8);
    border-top: 1px solid rgba(255, 255, 255, 0.05);
}

.score-breakdown-table td:last-child {
    text-align: right;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--accent-secondary);
}

.score-breakdown-table tr.total-row td {
    font-weight: 800;
    font-size: 14px;
    color: white;
    border-top: 1px solid rgba(255, 255, 255, 0.15);
}

.score-breakdown-table tr.total-row td:last-child {
    color: white;
}

/* Ranking */
.end-ranking {
    text-align: center;
    font-size: 14px;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.8);
    padding: 4px 0;
}

.ranking-medal { font-size: 18px; }

/* Hearts Display */
.end-hearts {
    display: flex;
    justify-content: center;
    gap: 8px;
    padding: 4px 0;
}

.end-heart {
    font-size: 28px;
    line-height: 1;
}

.end-heart.lost {
    filter: grayscale(1);
    opacity: 0.3;
}

/* Buttons */
.end-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: var(--space-xs) 0;
}

.end-btn {
    display: block;
    width: 100%;
    padding: 14px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: var(--radius-md);
    background: rgba(255, 255, 255, 0.08);
    color: white;
    font-family: var(--font);
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.15s;
}

.end-btn:hover { background: rgba(255, 255, 255, 0.12); }

.end-btn-primary {
    background: rgba(255, 255, 255, 0.18);
    border-color: rgba(255, 255, 255, 0.25);
}

/* Score Breakdown (end screen) */
.end-breakdown {
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--radius-md);
    overflow: hidden;
    padding: 4px 0;
}

.breakdown-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 14px;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.8);
}

.breakdown-row:not(:first-child) {
    border-top: 1px solid rgba(255, 255, 255, 0.05);
}

.breakdown-label { font-weight: 500; }

.breakdown-value {
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--accent-secondary);
}

.breakdown-total {
    border-top: 1px solid rgba(255, 255, 255, 0.15) !important;
}

.breakdown-total .breakdown-label,
.breakdown-total .breakdown-value {
    font-weight: 800;
    font-size: 14px;
    color: white;
}

/* Hearts (end screen) */
.heart { font-size: 24px; line-height: 1; }
.heart-full { opacity: 1; }
.heart-empty { opacity: 0.35; filter: grayscale(1); }

/* Come back message */
.end-comeback {
    text-align: center;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.4);
    padding: var(--space-sm) 0 var(--space-lg);
    font-weight: 500;
}

/* ===== Game Chooser ===== */
.game-chooser {
    position: fixed;
    inset: 0;
    z-index: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-lg);
}

.game-chooser-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
}

.game-chooser-content {
    position: relative;
    background: #ffffff;
    border-radius: var(--radius-xl);
    padding: var(--space-lg);
    width: 100%;
    max-width: 340px;
    border: 1px solid #e2e8f0;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
}

.game-chooser-content h2 {
    font-size: 18px;
    font-weight: 700;
    margin-bottom: var(--space-md);
    text-align: center;
    color: #0f172a;
}

.game-chooser-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: var(--radius-md);
    margin-bottom: 8px;
    text-decoration: none;
    color: #0f172a;
    transition: background 0.15s;
}

.game-chooser-item:hover { background: #f1f5f9; }
.game-chooser-icon { font-size: 24px; }
.game-chooser-info { display: flex; flex-direction: column; }
.game-chooser-name { font-weight: 700; font-size: 14px; }
.game-chooser-desc { font-size: 12px; color: #64748b; }

.game-chooser-close {
    display: block;
    width: 100%;
    margin-top: var(--space-sm);
    padding: 12px;
    background: none;
    border: 1px solid #e2e8f0;
    border-radius: var(--radius-md);
    font-family: var(--font);
    font-size: 14px;
    font-weight: 600;
    color: #64748b;
    cursor: pointer;
}

/* ===== Side Menu ===== */
.side-menu-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 899;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

.side-menu-overlay:not(.hidden) {
    opacity: 1;
    pointer-events: auto;
}

.side-menu {
    position: fixed;
    top: 0;
    right: -280px;
    bottom: 0;
    width: 280px;
    background: var(--bg-primary);
    z-index: 900;
    display: flex;
    flex-direction: column;
    transition: right 0.3s ease;
    border-left: 1px solid var(--border);
}

.side-menu.open { right: 0; }

.side-menu-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-md) var(--space-md);
    padding-top: env(safe-area-inset-top, var(--space-md));
    border-bottom: 1px solid var(--border);
}

.side-menu-title {
    font-size: 18px;
    font-weight: 700;
    color: var(--accent);
}

.side-menu-nav {
    padding: var(--space-md);
    flex: 1;
}

.menu-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px var(--space-md);
    border-radius: var(--radius-md);
    text-decoration: none;
    color: var(--text-primary);
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.15s ease;
}

.menu-link:hover { background: rgba(255, 255, 255, 0.06); }
.menu-icon { font-size: 18px; width: 24px; text-align: center; }

.menu-divider {
    height: 1px;
    background: var(--border);
    margin: var(--space-xs) var(--space-md);
}

.side-menu-footer {
    padding: var(--space-md);
    border-top: 1px solid var(--border);
}

.menu-credit {
    font-size: 12px;
    color: var(--text-muted);
    text-align: center;
}

/* ===== Stats Page Styling ===== */
.stats-container {
    max-width: 480px;
    margin: 0 auto;
    padding: var(--space-md);
    padding-top: env(safe-area-inset-top, var(--space-md));
}

.stats-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-lg);
}

.stats-header h1 {
    font-size: 22px;
    font-weight: 800;
    color: var(--accent);
}

.stats-back-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-bottom: var(--space-lg);
}

.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 14px 8px;
    text-align: center;
}

.stat-card-value {
    font-size: 22px;
    font-weight: 800;
    color: var(--accent);
}

.stat-card-label {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    margin-top: 2px;
}

.stats-section {
    margin-top: var(--space-lg);
}

.stats-section h2 {
    font-size: 14px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-secondary);
    margin-bottom: var(--space-sm);
}

.stat-game-row {
    display: flex;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
}

.stat-game-date {
    flex: 1;
    color: var(--text-secondary);
}

.stat-game-score {
    font-weight: 700;
    margin-right: 12px;
}

.stat-game-floor {
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

/* ===== Share Button ===== */
.share-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 14px;
    border: none;
    border-radius: var(--radius-md);
    background: rgba(255, 255, 255, 0.18);
    color: white;
    font-family: var(--font);
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
}

.share-btn:hover {
    background: rgba(255, 255, 255, 0.24);
}

.share-btn:active {
    transform: scale(0.97);
}

.share-btn-icon {
    font-size: 18px;
}

.share-copied {
    background: var(--correct) !important;
}

/* ===== Group Leaderboards on End Screen ===== */
.end-group-leaderboards {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 16px;
}

.end-group-card {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 12px;
    overflow: hidden;
}

.end-group-header {
    font-size: 13px;
    font-weight: 600;
    padding: 10px 12px 4px;
    color: var(--text-primary);
}

.end-group-table {
    width: 100%;
    border-collapse: collapse;
}

.end-group-table th {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    color: var(--text-muted);
    padding: 4px 12px;
    text-align: left;
}

.end-group-table th:first-child { width: 32px; text-align: center; }

.end-group-table td {
    padding: 4px 12px;
    font-size: 12px;
    color: var(--text-secondary);
    border-top: 1px solid rgba(255, 255, 255, 0.04);
}

.end-group-table td:first-child {
    width: 32px;
    text-align: center;
    font-weight: 600;
    color: var(--text-muted);
}

.end-group-table td:last-child {
    text-align: right;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.end-group-table tr.leaderboard-me {
    background: rgba(233, 69, 96, 0.15);
}

.end-group-table tr.leaderboard-me td {
    color: var(--accent);
    font-weight: 600;
}

.end-group-empty {
    font-size: 12px;
    color: var(--text-muted);
    padding: 10px 12px;
    text-align: center;
}

/* ===== Responsive ===== */
@media (min-width: 768px) {
    .virtual-controls {
        padding: 32px 48px;
    }

    .control-arrow {
        width: 64px;
        height: 64px;
        font-size: 22px;
    }

    .control-jump {
        width: 72px;
        height: 72px;
        font-size: 26px;
    }

    .game-hud {
        padding: 12px 24px;
    }

    .hud-value {
        font-size: 24px;
    }
}
