:root {
    --bg-dark: #0b132b;
    --bg-panel: #1c2541;
    --ice-light: #e0fbfc;
    --ice-border: #98c1d9;
    --accent-gold: #ffb703;
    --accent-gold-dark: #fb8500;
    --danger: #ef233c;
    --text-main: #ffffff;
    --text-muted: #8d99ae;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body {
    background-color: var(--bg-dark);
    background-image: radial-gradient(circle at top, #1c2541 0%, #0b132b 80%);
    color: var(--text-main);
    height: 100vh;
    height: 100dvh;
    width: 100vw;
    overflow: hidden;
    position: fixed;
}

/* Screen Management */
.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    flex-direction: column;
    z-index: 10;
}

.screen.active {
    display: flex;
}

/* Headers & HUDs */
.top-hud, .game-hud, .league-hud {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: calc(12px + var(--safe-top)) 20px 12px;
    background: rgba(11, 19, 43, 0.85);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    z-index: 20;
}

.icon-btn, .quit-btn {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-main);
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 1.1rem;
    cursor: pointer;
    min-height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:active, .quit-btn:active {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(0.95);
}

button:disabled {
    opacity: 0.45;
    cursor: default;
    transform: none !important;
    filter: grayscale(0.35);
}

.quit-btn {
    background: rgba(239, 35, 60, 0.15);
    border-color: rgba(239, 35, 60, 0.4);
    color: #ff4d6d;
    font-weight: 600;
    font-size: 0.9rem;
}

.stat-badge {
    background: rgba(0, 0, 0, 0.3);
    padding: 8px 16px;
    border-radius: 20px;
    font-weight: 700;
    border: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--accent-gold);
}

/* Personal Branding */
.branding-header {
    text-align: center;
    padding: 24px 20px 10px;
}

.branding-header h1 {
    font-size: 2rem;
    color: var(--ice-light);
    text-shadow: 0 2px 12px rgba(152, 193, 217, 0.4);
    margin-bottom: 6px;
    letter-spacing: 0.5px;
}

.brand-subtitle {
    font-size: 1rem;
    color: var(--text-main);
    font-weight: 500;
    margin-bottom: 8px;
}

.brand-signature {
    font-size: 0.9rem;
    color: var(--accent-gold);
    font-style: italic;
    opacity: 0.9;
}

/* Map Screen */
.map-scroll-area {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 40px 0 calc(100px + var(--safe-bottom));
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
    align-items: center;
}

#map-levels {
    display: flex;
    flex-direction: column-reverse;
    align-items: center;
    gap: 60px;
    width: 100%;
    position: relative;
}

/* Winding path effect for dynamically injected map nodes */
#map-levels .level-node:nth-child(even) { transform: translateX(45px); }
#map-levels .level-node:nth-child(odd) { transform: translateX(-45px); }

.level-node {
    width: 68px;
    height: 68px;
    border-radius: 50%;
    background: var(--bg-panel);
    border: 3px solid rgba(152, 193, 217, 0.4);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.4rem;
    font-weight: bold;
    color: var(--text-muted);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.4), inset 0 2px 6px rgba(255, 255, 255, 0.1);
    position: relative;
    z-index: 2;
}

.level-node.completed {
    background: linear-gradient(135deg, #2b2d42, #1c2541);
    border-color: #48bb78;
    color: #48bb78;
}

.level-node.current {
    background: linear-gradient(135deg, var(--accent-gold), var(--accent-gold-dark));
    border-color: #fff;
    color: #000;
    box-shadow: 0 0 20px rgba(255, 183, 3, 0.5), inset 0 2px 6px rgba(255, 255, 255, 0.5);
    animation: node-pulse 2s infinite alternate;
}

.level-node.locked {
    opacity: 0.5;
}

/* Floating League Button */
.floating-btn {
    position: absolute;
    bottom: calc(24px + var(--safe-bottom));
    right: 24px;
    background: linear-gradient(135deg, var(--accent-gold), var(--accent-gold-dark));
    color: #0b132b;
    border: none;
    padding: 14px 24px;
    border-radius: 30px;
    font-weight: 800;
    font-size: 1rem;
    box-shadow: 0 8px 20px rgba(251, 133, 0, 0.4);
    z-index: 30;
    min-height: 44px;
}

/* Game Screen */
.level-indicator {
    font-size: 1.2rem;
    font-weight: bold;
    color: var(--ice-light);
}

.spacer { width: 44px; }

.board-viewport {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
}

#board {
    width: 320px;
    height: 350px;
    position: relative;
    background: rgba(28, 37, 65, 0.4);
    border-radius: 16px;
    border: 1px solid rgba(255, 255, 255, 0.05);
}

/* Tiles */
.tile {
    width: 36px;
    height: 40px;
    position: absolute;
    background: linear-gradient(180deg, var(--ice-light) 0%, #a4ebf3 100%);
    border: 1.5px solid #48cae4;
    border-radius: 6px;
    box-shadow: 0 4px 0 #0077b6, 0 5px 8px rgba(0,0,0,0.3), inset 0 2px 2px rgba(255,255,255,0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 22px;
    z-index: 10;
    transition: transform 0.15s ease, filter 0.2s, top 0.3s ease, left 0.3s ease;
}

.tile:active {
    transform: translateY(3px);
    box-shadow: 0 1px 0 #0077b6, 0 2px 4px rgba(0,0,0,0.3);
}

.tile.blocked {
    filter: brightness(0.6) sepia(0.2) hue-rotate(180deg);
}

.tile.highlight {
    animation: highlight-glow 1s infinite alternate;
    border-color: var(--accent-gold);
    box-shadow: 0 0 15px var(--accent-gold), 0 4px 0 #0077b6;
    z-index: 20;
}

.tile.shake {
    animation: shake-error 0.4s cubic-bezier(.36,.07,.19,.97) both;
}

/* Tray Area */
.tray-area {
    width: 100%;
    height: 80px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    margin-bottom: 10px;
}

.tray-slots {
    display: flex;
    gap: 4px;
    background: rgba(0, 0, 0, 0.4);
    padding: 8px 6px;
    border-radius: 12px;
    border: 1px solid rgba(152, 193, 217, 0.1);
}

.tray-slot {
    width: 36px;
    height: 40px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

#tray {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 288px;
    height: 56px;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 6px;
    pointer-events: none;
}

.tile.tray-tile {
    position: relative;
    top: auto;
    left: auto;
    flex: 0 0 36px;
    pointer-events: auto;
    box-shadow: 0 2px 0 #0077b6, 0 3px 5px rgba(0,0,0,0.4);
    z-index: 30;
}

/* Boosters */
.booster-bar {
    display: flex;
    justify-content: center;
    gap: 20px;
    padding: 12px 16px calc(20px + var(--safe-bottom));
    background: rgba(11, 19, 43, 0.95);
    border-top: 1px solid rgba(255, 255, 255, 0.05);
}

.booster-btn {
    background: var(--bg-panel);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 14px;
    min-width: 68px;
    min-height: 60px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    color: var(--text-main);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.booster-btn:active {
    transform: scale(0.94);
}

.booster-icon { font-size: 1.4rem; }
.booster-label { font-size: 0.75rem; margin-top: 4px; color: var(--text-muted); }

.badge {
    position: absolute;
    top: -6px;
    right: -6px;
    background: var(--danger);
    color: white;
    font-size: 0.75rem;
    font-weight: 800;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 2px solid var(--bg-panel);
}

/* League Screen */
.league-content {
    flex: 1;
    overflow-y: auto;
    padding: 20px 16px calc(20px + var(--safe-bottom));
    display: flex;
    flex-direction: column;
}

.league-subtitle {
    text-align: center;
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--ice-light);
    margin-bottom: 6px;
}

#league-timer {
    text-align: center;
    font-size: 0.9rem;
    color: var(--accent-gold);
    margin-bottom: 24px;
}

#league-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.league-row {
    display: flex;
    justify-content: space-between;
    padding: 16px 20px;
    background: var(--bg-panel);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 14px;
    font-size: 1rem;
    align-items: center;
}

.league-row.is-player {
    background: rgba(255, 183, 3, 0.1);
    border-color: var(--accent-gold);
    color: var(--accent-gold);
    font-weight: 700;
    box-shadow: 0 4px 15px rgba(255, 183, 3, 0.15);
}

/* Modals */
#modals-container {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(11, 19, 43, 0.85);
    backdrop-filter: blur(8px);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 100;
    padding: 20px;
}

#modals-container:not(:empty) {
    display: flex;
    animation: fade-in 0.2s ease-out;
}

.modal {
    background: var(--bg-panel);
    border: 1px solid rgba(152, 193, 217, 0.2);
    border-radius: 24px;
    padding: 32px 24px;
    width: 100%;
    max-width: 350px;
    text-align: center;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
    display: flex;
    flex-direction: column;
    gap: 16px;
    animation: pop-in 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.modal h2 { color: var(--ice-light); font-size: 1.6rem; margin-bottom: 4px; }
.subtext { color: var(--text-muted); font-size: 0.95rem; line-height: 1.4; }

.danger { color: #ff4d6d; }
.danger-modal { border-color: rgba(239, 35, 60, 0.4); }
.danger-modal h2 { color: #ff4d6d; }

.modal button {
    padding: 14px;
    border-radius: 12px;
    border: none;
    font-weight: 700;
    font-size: 1.05rem;
    min-height: 48px;
    cursor: pointer;
    margin-top: 8px;
}

.modal button:active { transform: scale(0.96); }

.modal button:first-of-type:not(.danger-btn) {
    background: linear-gradient(135deg, var(--ice-light), #98c1d9);
    color: var(--bg-dark);
}

.modal button.danger-btn {
    background: var(--danger);
    color: white;
}

.modal button:nth-of-type(2) {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-main);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

/* Settings Modal Elements */
.settings-modal > div {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 12px;
}

.settings-modal > div:has(input[type="text"]) {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
}

.settings-modal label {
    color: var(--text-main);
    font-weight: 500;
    font-size: 1.05rem;
    text-align: left;
}

.settings-modal input[type="text"] {
    width: 100%;
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid rgba(152, 193, 217, 0.3);
    color: white;
    padding: 12px 14px;
    border-radius: 10px;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
}

.settings-modal input[type="text"]:focus {
    border-color: var(--ice-light);
}

.settings-modal input[type="checkbox"] {
    width: 24px;
    height: 24px;
    accent-color: var(--accent-gold);
    cursor: pointer;
}

/* Grand Prize & Chest Modals */
.grand-prize, .chest-modal {
    border-color: var(--accent-gold);
    background: linear-gradient(180deg, var(--bg-panel), #2a1f0a);
}

.grand-prize h2, .chest-modal h2 {
    color: var(--accent-gold);
    text-shadow: 0 0 10px rgba(255, 183, 3, 0.5);
}

.grand-prize button:first-of-type, .chest-modal button:first-of-type {
    background: linear-gradient(135deg, var(--accent-gold), var(--accent-gold-dark));
    color: #000;
}

/* Toast */
#toast {
    position: absolute;
    top: calc(80px + var(--safe-top));
    left: 50%;
    transform: translateX(-50%) translateY(-20px);
    background: rgba(28, 37, 65, 0.95);
    border: 1px solid var(--accent-gold);
    color: var(--ice-light);
    padding: 12px 24px;
    border-radius: 30px;
    font-weight: 600;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.6);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s, transform 0.3s;
    z-index: 200;
    width: max-content;
    max-width: calc(100vw - 32px);
    white-space: normal;
    text-align: center;
}

#toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* Animations */
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop-in { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes node-pulse {
    from { box-shadow: 0 0 10px rgba(255, 183, 3, 0.4); }
    to { box-shadow: 0 0 25px rgba(255, 183, 3, 0.8), inset 0 2px 6px rgba(255, 255, 255, 0.5); }
}
@keyframes highlight-glow {
    from { filter: brightness(1.1); }
    to { filter: brightness(1.4); }
}
@keyframes shake-error {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-4px); }
    40%, 80% { transform: translateX(4px); }
}

@media (max-width: 380px) {
    .top-hud, .game-hud, .league-hud {
        padding-left: 14px;
        padding-right: 14px;
    }

    .booster-bar {
        gap: 12px;
    }

    .branding-header h1 {
        font-size: 1.75rem;
    }
}

@media (max-height: 700px) {
    .branding-header {
        padding-top: 12px;
    }

    .tray-area {
        height: 68px;
        margin-bottom: 4px;
    }

    .booster-bar {
        padding-top: 8px;
        padding-bottom: calc(10px + var(--safe-bottom));
    }

    .booster-btn {
        min-height: 54px;
    }
}
