:root {
    /* Dynamic theme variables updated via JS per selected team */
    --bg-app: #dbeafe;
    --bg-header: #1e40af;
    --bg-road: #1e3a8a;
    --road-border: #60a5fa;
    --theme-primary: #2563eb;
    --theme-secondary: #60a5fa;
    --boss-color: #e11d48;
    --text-dark: #0f172a;
    --text-light: #f8fafc;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-app);
    color: var(--text-dark);
    height: 100dvh;
    overflow: hidden;
    display: flex;
    justify-content: center;
    transition: background-color 0.4s ease;
}

#app {
    width: 100%;
    max-width: 480px;
    height: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
    background: rgba(255, 255, 255, 0.35);
    box-shadow: 0 0 25px rgba(0, 0, 0, 0.08);
}

/* Header */
.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 12px;
    background: var(--bg-header);
    color: var(--text-light);
    height: 50px;
    flex-shrink: 0;
    z-index: 20;
    transition: background-color 0.4s ease;
}

.nav-left {
    display: flex;
    gap: 8px;
    align-items: center;
}

.btn-icon {
    position: relative;
    background: rgba(255, 255, 255, 0.18);
    color: var(--text-light);
    border: 2px solid rgba(255, 255, 255, 0.4);
    padding: 5px 11px;
    border-radius: 10px;
    font-size: 0.85rem;
    font-weight: 800;
    cursor: pointer;
    transition: transform 0.15s, background-color 0.2s;
}

.btn-icon:active {
    transform: scale(0.95);
}

.btn-sound {
    padding: 5px 9px;
}

/* Attention badge when player can afford a new team */
.btn-icon.can-unlock {
    background: #fbbf24;
    color: #0f172a;
    border-color: #ffffff;
    animation: bounce-attention 1.4s infinite;
}

.notify-dot {
    position: absolute;
    top: -6px;
    right: -6px;
    background: #ef4444;
    color: white;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    font-size: 0.72rem;
    font-weight: 900;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid white;
}

.notify-dot.hidden {
    display: none;
}

.stats {
    display: flex;
    gap: 8px;
}

.badge {
    background: rgba(15, 23, 42, 0.35);
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.85rem;
    color: white;
}

.badge.gold {
    background: rgba(15, 23, 42, 0.55);
    color: #fde047;
    border: 1px solid rgba(253, 224, 71, 0.5);
}

/* Rank Bar */
.rank-bar {
    background: rgba(255, 255, 255, 0.85);
    padding: 5px 14px 7px 14px;
    border-bottom: 2px solid rgba(0, 0, 0, 0.08);
    flex-shrink: 0;
}

.rank-info {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: #334155;
    font-weight: 800;
    margin-bottom: 3px;
}

.progress-track {
    width: 100%;
    height: 7px;
    background: #cbd5e1;
    border-radius: 4px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--theme-primary), #f59e0b);
    width: 10%;
    transition: width 0.4s ease;
}

/* Screens */
.screen {
    display: none;
    flex: 1;
    flex-direction: column;
    overflow-y: auto;
}

.screen.active {
    display: flex;
}

/* Theme Selection Screen */
#screen-theme {
    padding: 12px 14px;
    align-items: center;
}

#screen-theme h1 {
    font-size: 1.45rem;
    color: #0f172a;
    margin-bottom: 2px;
}

.subtitle {
    color: #475569;
    font-size: 0.82rem;
    margin-bottom: 12px;
    text-align: center;
    font-weight: 500;
}

.theme-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    width: 100%;
    padding-bottom: 12px;
}

.theme-card {
    background: #ffffff;
    border: 3px solid transparent;
    border-radius: 16px;
    padding: 10px 12px;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.06);
    transition: transform 0.15s, box-shadow 0.15s;
}

.theme-card:active {
    transform: scale(0.98);
}

.theme-card.locked {
    background: rgba(255, 255, 255, 0.65);
}

.theme-card.affordable {
    border-color: #f59e0b !important;
    background: #fffbeb;
    box-shadow: 0 4px 14px rgba(245, 158, 11, 0.25);
}

.theme-card-icon {
    width: 48px;
    height: 48px;
    flex-shrink: 0;
    background: #f1f5f9;
    border-radius: 12px;
    padding: 4px;
}

.theme-card-info {
    flex: 1;
    text-align: left;
}

.theme-card-info h3 {
    font-size: 1.02rem;
    margin-bottom: 2px;
}

.theme-card-info p {
    color: #475569;
    font-size: 0.78rem;
}

.btn-unlock {
    background: #fbbf24;
    color: #0f172a;
    border: 2px solid #f59e0b;
    padding: 7px 11px;
    border-radius: 10px;
    font-weight: 900;
    font-size: 0.8rem;
    cursor: pointer;
    flex-shrink: 0;
    box-shadow: 0 3px 0 #d97706;
}

.btn-unlock:disabled {
    background: #e2e8f0;
    color: #64748b;
    border-color: #cbd5e1;
    box-shadow: none;
    cursor: not-allowed;
}

.footer-actions {
    margin-top: auto;
    padding: 8px 0 4px 0;
}

.btn-reset {
    background: rgba(255, 255, 255, 0.6);
    color: #64748b;
    border: 1px solid #cbd5e1;
    padding: 5px 12px;
    border-radius: 8px;
    font-size: 0.72rem;
    font-weight: 600;
    cursor: pointer;
}

/* Battlefield (Themed Arena Track) */
.battlefield {
    position: relative;
    flex: 1;
    margin: 6px 10px 4px 10px;
    background: var(--bg-road);
    border-left: 6px solid var(--road-border);
    border-right: 6px solid var(--road-border);
    border-radius: 18px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 8px 10px 10px 10px;
    overflow: hidden;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15), inset 0 0 25px rgba(0, 0, 0, 0.25);
    transition: background-color 0.4s ease, border-color 0.4s ease;
}

.road-lanes {
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    pointer-events: none;
    z-index: 1;
}

.lane-divider {
    width: 4px;
    height: 100%;
    background: repeating-linear-gradient(
            to bottom,
            transparent 0px,
            transparent 14px,
            rgba(255, 255, 255, 0.25) 14px,
            rgba(255, 255, 255, 0.25) 28px
    );
}

/* Boss Zone */
.boss-zone {
    position: relative;
    z-index: 5;
    background: rgba(15, 23, 42, 0.82);
    border: 2px solid #fda4af;
    border-radius: 14px;
    padding: 6px 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    min-height: 66px;
    color: white;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

.boss-avatar {
    width: 46px;
    height: 46px;
    color: #fb7185;
    animation: float 2s ease-in-out infinite;
    flex-shrink: 0;
}

.boss-info {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
}

#boss-name {
    font-size: 0.72rem;
    color: #fecdd3;
    font-weight: 800;
    text-transform: uppercase;
}

.boss-stats-row {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.power-badge {
    font-size: 1.15rem;
    font-weight: 900;
    padding: 2px 10px;
    border-radius: 8px;
}

.boss-power {
    background: var(--boss-color);
    color: white;
}

.condition-badge {
    background: #fbbf24;
    color: #0f172a;
    font-size: 0.72rem;
    font-weight: 800;
    padding: 3px 8px;
    border-radius: 6px;
}

.condition-badge.hidden {
    display: none;
}

/* Gates & Monsters Track */
.gates-track {
    position: relative;
    z-index: 5;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    padding: 4px 0;
}

.gate-row-wrapper {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.center-barrier {
    position: absolute;
    bottom: -18px;
    width: 24px;
    height: 24px;
    background: repeating-linear-gradient(
            45deg,
            #f59e0b,
            #f59e0b 5px,
            #1e293b 5px,
            #1e293b 10px
    );
    border: 2px solid #fde047;
    border-radius: 6px;
    z-index: 6;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.5);
}

.gate-row {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
    transition: opacity 0.3s, transform 0.3s;
}

/* Portal Gate & Monster Nodes */
.node-btn {
    position: relative;
    border-radius: 12px;
    height: 52px;
    font-size: 1.3rem;
    font-weight: 900;
    color: #ffffff;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 6px;
    pointer-events: none;
    opacity: 0.75;
}

.node-btn.type-gate {
    background: linear-gradient(180deg, rgba(125, 211, 252, 0.4) 0%, rgba(2, 132, 199, 0.75) 100%);
    border: 2px solid #bae6fd;
    border-top: 3px solid #ffffff;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25), inset 0 0 10px rgba(255, 255, 255, 0.3);
}

.node-btn.type-monster {
    background: linear-gradient(180deg, rgba(244, 63, 94, 0.65) 0%, rgba(136, 19, 55, 0.9) 100%);
    border: 2px solid #fda4af;
    color: #fff1f2;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.monster-mini-svg {
    width: 30px;
    height: 30px;
    color: #fecdd3;
    flex-shrink: 0;
    animation: float 1.8s ease-in-out infinite;
}

.monster-power-label {
    font-size: 1.15rem;
    font-weight: 900;
    background: rgba(0, 0, 0, 0.4);
    padding: 2px 8px;
    border-radius: 8px;
    border: 1px solid rgba(253, 164, 175, 0.5);
}

.node-btn.reachable {
    pointer-events: auto;
    opacity: 1;
    animation: portal-pulse 1.5s infinite;
}

.node-btn.unreachable {
    opacity: 0.22;
    filter: grayscale(0.85);
}

.node-btn.reachable:active {
    transform: scale(0.95);
}

.gate-row.passed-row .node-btn {
    opacity: 0.08;
    transform: scale(0.9);
    pointer-events: none;
}

/* Start Pad */
.start-pad {
    position: relative;
    z-index: 2;
    height: 56px;
    border: 2px dashed rgba(255, 255, 255, 0.4);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.45);
    font-weight: 800;
    letter-spacing: 3px;
    font-size: 0.8rem;
}

/* Moving Player Squad Token */
.player-token {
    position: absolute;
    left: 50%;
    top: 85%;
    width: 122px;
    transform: translate(-50%, -50%);
    z-index: 15;
    display: flex;
    flex-direction: column;
    align-items: center;
    pointer-events: none;
    transition: left 0.45s cubic-bezier(0.34, 1.56, 0.64, 1),
    top 0.45s cubic-bezier(0.25, 1, 0.5, 1),
    transform 0.3s ease;
}

.player-token.marching {
    animation: march-bounce 0.45s ease;
}

.token-badge {
    background: var(--theme-primary);
    color: white;
    font-weight: 900;
    font-size: 1.15rem;
    padding: 1px 10px;
    border-radius: 12px;
    border: 2px solid #ffffff;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.4);
    margin-bottom: 3px;
    z-index: 2;
    transition: transform 0.2s;
}

.token-badge.pop {
    transform: scale(1.35);
}

.token-units {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 2px;
    background: rgba(15, 23, 42, 0.85);
    border: 2px solid #ffffff;
    border-radius: 12px;
    padding: 4px 6px;
    min-height: 34px;
    max-height: 60px;
    overflow: hidden;
    box-shadow: 0 6px 14px rgba(0, 0, 0, 0.45);
}

.unit-icon {
    width: 20px;
    height: 20px;
    color: var(--theme-secondary);
    animation: pop-in 0.25s ease;
}

/* Subtle App Footer */
.app-footer {
    text-align: center;
    padding: 5px 10px;
    font-size: 0.74rem;
    color: #475569;
    background: rgba(255, 255, 255, 0.6);
    border-top: 1px solid rgba(0, 0, 0, 0.06);
    flex-shrink: 0;
}

.app-footer a {
    color: var(--bg-header);
    font-weight: 700;
    text-decoration: none;
}

.app-footer a:hover {
    text-decoration: underline;
}

/* Floating Math Feedback */
.floating-feedback {
    position: absolute;
    font-size: 1.5rem;
    font-weight: 900;
    color: #fde047;
    text-shadow: 0 2px 6px rgba(0, 0, 0, 0.9);
    z-index: 30;
    pointer-events: none;
    animation: float-up 0.85s ease-out forwards;
}

.shake {
    animation: shake 0.4s cubic-bezier(.36,.07,.19,.97) both;
}

/* Modal */
.modal-overlay {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(3px);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    z-index: 50;
}

.modal-overlay.hidden {
    display: none;
}

.modal-card {
    background: #ffffff;
    color: #0f172a;
    border: 3px solid var(--theme-primary);
    border-radius: 22px;
    padding: 22px;
    text-align: center;
    width: 100%;
    max-width: 350px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.35);
    animation: pop-in 0.3s ease;
}

.modal-icon {
    width: 66px;
    height: 66px;
}

.modal-stars {
    font-size: 1.7rem;
    letter-spacing: 4px;
}

.modal-card p {
    color: #334155;
    font-size: 0.98rem;
    line-height: 1.4;
}

.unlock-alert {
    background: #fef3c7;
    border: 2px solid #f59e0b;
    color: #92400e;
    font-weight: 800;
    font-size: 0.88rem;
    padding: 8px 12px;
    border-radius: 12px;
    width: 100%;
    animation: bounce-attention 1.2s infinite;
}

.unlock-alert.hidden {
    display: none;
}

.modal-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
    margin-top: 4px;
}

.btn-unlock-modal {
    background: #fbbf24;
    color: #0f172a;
    border: none;
    border-radius: 12px;
    padding: 12px 18px;
    font-size: 1rem;
    font-weight: 900;
    cursor: pointer;
    width: 100%;
    box-shadow: 0 4px 0 #d97706;
}

.btn-unlock-modal.hidden {
    display: none;
}

.btn-primary {
    background: var(--theme-primary);
    color: white;
    border: none;
    border-radius: 12px;
    padding: 13px 24px;
    font-size: 1.08rem;
    font-weight: 800;
    cursor: pointer;
    width: 100%;
    box-shadow: 0 4px 0 rgba(0, 0, 0, 0.25);
}

@keyframes pop-in {
    0% { transform: scale(0); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-4px); }
}

@keyframes portal-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.5); }
    50% { box-shadow: 0 0 0 5px rgba(255, 255, 255, 0); }
}

@keyframes bounce-attention {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

@keyframes march-bounce {
    0%, 100% { transform: translate(-50%, -50%) scale(1); }
    50% { transform: translate(-50%, -60%) scale(1.12); }
}

@keyframes float-up {
    0% { transform: translate(-50%, 0) scale(0.8); opacity: 1; }
    100% { transform: translate(-50%, -45px) scale(1.2); opacity: 0; }
}

@keyframes shake {
    10%, 90% { transform: translate3d(-3px, 0, 0); }
    20%, 80% { transform: translate3d(5px, 0, 0); }
    30%, 50%, 70% { transform: translate3d(-6px, 0, 0); }
    40%, 60% { transform: translate3d(6px, 0, 0); }
}