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

:root {
    --purple: #5533E8;
    --yellow: #F5C200;
    --coral: #E84530;
    --teal: #00A889;
    --green: #22C55E;
    --bg: #111827;
    --bg2: #1F2937;
    --bg3: #374151;
    --border: #4B5563;
    --dim: #6B7280;
    --cell: 60px;
    --sidebar: 196px;
}

html, body {
    height: 100%;
    overflow: hidden;
    font-family: 'Inter', sans-serif;
    background: var(--bg);
    color: #fff;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.back-link {
    position: fixed;
    top: 7px; left: 10px;
    z-index: 300;
    color: var(--dim);
    font-size: 0.7rem;
    font-weight: 600;
    text-decoration: none;
    letter-spacing: 0.04em;
    transition: color 0.15s;
}
.back-link:hover { color: #fff; }

/* ── APP ─────────────────────────────────────── */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
}

/* ── TOP ROW (wave bar + sidebar spacer) ──────── */
#top-row {
    display: flex;
    flex-shrink: 0;
}
#wave-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 5px 12px 5px 100px;
    background: var(--bg2);
    border-bottom: 2px solid var(--border);
    flex: 1;
    min-height: 38px;
}
#wave-label {
    font-size: 0.78rem;
    font-weight: 900;
    letter-spacing: 0.08em;
    color: var(--dim);
}
#wave-num {
    color: #fff;
    font-size: 1rem;
}
#wave-status {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--yellow);
    letter-spacing: 0.03em;
}
#sidebar-top-spacer {
    width: var(--sidebar);
    flex-shrink: 0;
    background: var(--bg2);
    border-bottom: 2px solid var(--border);
    border-left: 2px solid var(--border);
}

/* ── GAME LAYOUT ─────────────────────────────── */
#game-layout {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ── BOARD WRAP ──────────────────────────────── */
#board-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    padding: 8px;
}

#board {
    position: relative;
    flex-shrink: 0;
}

/* ── GRID ────────────────────────────────────── */
#grid {
    display: grid;
    position: absolute;
    inset: 0;
}

.cell {
    width: var(--cell);
    height: var(--cell);
    border: 1px solid transparent;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background 0.1s;
}
.cell.path      { background: #2D3748; border-color: #4A5568; }
.cell.empty     { background: #1A2535; border-color: #2D3748; cursor: pointer; }
.cell.empty:hover { background: #243045; border-color: var(--purple); }
.cell.drop-valid   { background: rgba(85,51,232,0.25); border-color: var(--purple); }
.cell.drop-invalid { background: rgba(232,69,48,0.18); border-color: var(--coral); }

/* ── TOWERS ──────────────────────────────────── */
.tower {
    width: 52px;
    height: 52px;
    border-radius: 10px;
    background: var(--purple);
    border: 3px solid #7C5CEA;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: 900;
    color: #fff;
    cursor: grab;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4);
    position: relative;
    transition: transform 0.1s;
    z-index: 10;
}
.tower:active { transform: scale(0.95); }
.tower.firing { animation: fire-pulse 0.3s ease-out; }
@keyframes fire-pulse {
    0%   { transform: scale(1);    box-shadow: 0 0 0 0   rgba(245,194,0,0.8); }
    50%  { transform: scale(1.12); box-shadow: 0 0 0 8px rgba(245,194,0,0);   }
    100% { transform: scale(1); }
}
.tower-cooldown {
    position: absolute;
    bottom: 2px; left: 0; right: 0;
    height: 3px;
    background: rgba(0,0,0,0.4);
    border-radius: 2px;
    overflow: hidden;
}
.tower-cooldown-bar {
    height: 100%;
    background: var(--yellow);
    transition: width 0.1s linear;
}

/* ── ENEMIES ─────────────────────────────────── */
#enemies-layer {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 20;
}
.enemy {
    position: absolute;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--coral);
    border: 3px solid #ff6b57;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
    font-weight: 900;
    color: #fff;
    transform: translate(-50%, -50%);
    box-shadow: 0 2px 10px rgba(0,0,0,0.5);
    z-index: 20;
}
.enemy-hp {
    position: absolute;
    bottom: -6px; left: 0; right: 0;
    height: 4px;
    background: rgba(0,0,0,0.5);
    border-radius: 2px;
    overflow: hidden;
}
.enemy-hp-bar {
    height: 100%;
    background: var(--green);
    transition: width 0.15s;
}

/* ── FX ──────────────────────────────────────── */
#fx-layer {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 30;
}
.projectile {
    position: absolute;
    width: 12px; height: 12px;
    border-radius: 50%;
    background: var(--yellow);
    transform: translate(-50%, -50%);
    box-shadow: 0 0 8px 2px rgba(245,194,0,0.7);
    pointer-events: none;
    z-index: 30;
}
.coin-drop {
    position: absolute;
    font-size: 1.1rem;
    font-weight: 900;
    color: var(--yellow);
    pointer-events: none;
    animation: coin-float 0.9s ease-out forwards;
    z-index: 40;
    transform: translate(-50%, -50%);
}
@keyframes coin-float {
    0%   { opacity: 1; transform: translate(-50%, -50%); }
    100% { opacity: 0; transform: translate(-50%, -160%); }
}
.explosion {
    position: absolute;
    width: 50px; height: 50px;
    border-radius: 50%;
    background: radial-gradient(circle, #fff 0%, var(--yellow) 40%, transparent 70%);
    transform: translate(-50%, -50%) scale(0);
    animation: explode 0.4s ease-out forwards;
    pointer-events: none;
    z-index: 35;
}
@keyframes explode {
    0%   { transform: translate(-50%, -50%) scale(0);   opacity: 1; }
    60%  { transform: translate(-50%, -50%) scale(1.4); opacity: 0.8; }
    100% { transform: translate(-50%, -50%) scale(2);   opacity: 0; }
}

/* ── CASTLE ──────────────────────────────────── */
#base-marker {
    position: absolute;
    transform: translate(-50%, -50%);
    z-index: 25;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
}
#base-emoji {
    font-size: 2rem;
    filter: drop-shadow(0 0 6px rgba(255,200,0,0.5));
    line-height: 1;
}
#castle-hp {
    width: 44px;
    height: 6px;
    background: rgba(0,0,0,0.5);
    border-radius: 3px;
    overflow: hidden;
    border: 1px solid rgba(255,255,255,0.15);
}
#castle-hp-bar {
    height: 100%;
    width: 100%;
    background: var(--green);
    border-radius: 3px;
    transition: width 0.3s, background 0.3s;
}

/* ── SIDEBAR ─────────────────────────────────── */
#sidebar {
    width: var(--sidebar);
    flex-shrink: 0;
    background: var(--bg2);
    border-left: 2px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 10px 8px;
    overflow-y: auto;
}
#sidebar::-webkit-scrollbar { width: 3px; }
#sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── COINS ───────────────────────────────────── */
#coins-display {
    display: flex;
    align-items: center;
    gap: 5px;
    background: var(--bg3);
    border-radius: 8px;
    padding: 7px 10px;
    border: 1px solid var(--border);
}
.coin-icon { font-size: 1.1rem; }
#coin-count {
    font-size: 1.3rem;
    font-weight: 900;
    line-height: 1;
}
.coins-label {
    font-size: 0.62rem;
    font-weight: 700;
    color: var(--dim);
    letter-spacing: 0.06em;
    margin-top: 2px;
}

/* ── SIDEBAR SECTIONS ────────────────────────── */
.sidebar-section {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.sidebar-label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.6rem;
    font-weight: 800;
    letter-spacing: 0.08em;
    color: var(--dim);
}
.gen-timer {
    color: var(--teal);
    font-weight: 700;
}

/* ── HAND (grid) ─────────────────────────────── */
#hand {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 5px;
}

/* ── HAND TILES ──────────────────────────────── */
.hand-tile {
    aspect-ratio: 1;
    border-radius: 9px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-weight: 900;
    cursor: grab;
    border: 2px solid transparent;
    box-shadow: 0 2px 6px rgba(0,0,0,0.4);
    position: relative;
    transition: transform 0.1s, box-shadow 0.1s;
    padding: 2px;
}
.hand-tile:active { transform: scale(0.92); }
.hand-tile.type-tower {
    background: var(--purple);
    border-color: #7C5CEA;
    font-size: 1.35rem;
    color: #fff;
}
.hand-tile.type-modifier {
    background: var(--teal);
    border-color: #00D4AC;
    font-size: 1rem;
    color: #fff;
}
.tile-type-label {
    font-size: 0.46rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    opacity: 0.75;
    margin-top: 1px;
}
.tile-price {
    font-size: 0.5rem;
    font-weight: 700;
    color: rgba(255,255,255,0.5);
    line-height: 1;
}

/* ── TRASH ───────────────────────────────────── */
#trash-zone {
    border: 2px dashed var(--border);
    border-radius: 8px;
    padding: 7px 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--dim);
    cursor: default;
    transition: border-color 0.15s, background 0.15s;
    flex-shrink: 0;
}
.trash-cost { font-size: 0.58rem; opacity: 0.7; }
#trash-zone.drag-over {
    border-color: var(--coral);
    background: rgba(232,69,48,0.15);
    color: var(--coral);
}

/* ── UPGRADE SECTION ─────────────────────────── */
#upgrade-section {
    display: flex;
    flex-direction: column;
    gap: 6px;
    border-top: 1px solid var(--border);
    padding-top: 8px;
}
#upgrade-label {
    display: flex;
    flex-direction: column;
    gap: 1px;
}
#upgrade-label > span:first-child {
    font-size: 0.64rem;
    font-weight: 800;
    letter-spacing: 0.06em;
    color: var(--teal);
}
.upgrade-hint {
    font-size: 0.56rem;
    color: var(--dim);
    font-weight: 600;
}
.eq-row {
    display: flex;
    align-items: center;
    gap: 6px;
}
.eq-op-space {
    width: 34px;
    flex-shrink: 0;
}
.eq-divider {
    height: 2px;
    background: var(--border);
    border-radius: 1px;
    margin: 2px 0;
}
.eq-sign {
    font-size: 1.1rem;
    font-weight: 900;
    color: var(--dim);
    flex-shrink: 0;
    width: 34px;
    text-align: center;
    line-height: 1;
}
.drag-hint {
    font-size: 0.52rem;
    color: var(--dim);
    font-weight: 600;
    text-align: center;
}

/* ── UPGRADE SLOTS ───────────────────────────── */
.upgrade-slot {
    flex: 1;
    height: 54px;
    border: 2px dashed var(--border);
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s;
    overflow: hidden;
}
.upgrade-slot.drag-over,
.upgrade-slot.filled.drag-over {
    border-color: var(--teal);
    background: rgba(0,168,137,0.22);
}
.upgrade-slot.filled {
    border-style: solid;
    border-color: var(--purple);
    background: rgba(85,51,232,0.15);
}
.upgrade-slot.filled:hover { background: rgba(85,51,232,0.26); }

.slot-hint {
    font-size: 0.52rem;
    font-weight: 700;
    color: var(--dim);
    text-align: center;
    line-height: 1.3;
    pointer-events: none;
}
.slot-tile {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-weight: 900;
    pointer-events: none;
}
.slot-tile.type-tower    { font-size: 1.3rem; color: #fff; }
.slot-tile.type-modifier { font-size: 0.95rem; color: #fff; }
.slot-tile-label {
    font-size: 0.44rem;
    font-weight: 700;
    opacity: 0.7;
    letter-spacing: 0.06em;
}

.op-btn {
    width: 34px;
    height: 34px;
    flex-shrink: 0;
    border-radius: 50%;
    background: var(--bg3);
    border: 2px solid var(--border);
    color: #fff;
    font-size: 1.2rem;
    font-weight: 900;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: inherit;
    line-height: 1;
    padding: 0;
    transition: background 0.15s, border-color 0.15s;
}
.op-btn:hover { background: var(--teal); border-color: #00D4AC; }

#result-slot {
    flex: 1;
    height: 54px;
    border: 2px dashed var(--border);
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    transition: border-color 0.2s, background 0.2s;
}
#result-slot.ready {
    border-style: solid;
    border-color: var(--yellow);
    background: rgba(245,194,0,0.1);
    animation: result-pulse 1.4s ease-in-out infinite;
}
@keyframes result-pulse {
    0%, 100% { box-shadow: 0 0 0 0   rgba(245,194,0,0.5); }
    50%       { box-shadow: 0 0 0 6px rgba(245,194,0,0);   }
}
.result-tile {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-weight: 900;
    cursor: grab;
}
.result-tile:active { cursor: grabbing; }
.result-tile.type-tower    { font-size: 1.3rem; color: #fff; }
.result-tile.type-modifier { font-size: 0.95rem; color: #fff; }
.result-tile-label {
    font-size: 0.44rem;
    font-weight: 700;
    opacity: 0.7;
    letter-spacing: 0.06em;
}

/* ── DRAG GHOST ──────────────────────────────── */
#drag-ghost {
    position: fixed;
    z-index: 500;
    pointer-events: none;
    border-radius: 10px;
    width: 54px;
    height: 54px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-weight: 900;
    opacity: 0.88;
    transform: translate(-50%, -50%);
    box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}
#drag-ghost.type-tower {
    background: var(--purple);
    border: 2px solid #7C5CEA;
    font-size: 1.5rem;
    color: #fff;
}
#drag-ghost.type-modifier {
    background: var(--teal);
    border: 2px solid #00D4AC;
    font-size: 1.1rem;
    color: #fff;
}

/* ── OVERLAYS ────────────────────────────────── */
#overlay, #gameover-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.75);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    backdrop-filter: blur(4px);
}
#overlay.hidden, #gameover-overlay.hidden { display: none; }

#overlay-box, #gameover-box {
    background: var(--bg2);
    border: 2px solid var(--border);
    border-radius: 16px;
    padding: 28px 32px;
    text-align: center;
    max-width: 380px;
    width: 90%;
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
}
#overlay-icon { font-size: 2.5rem; }
#overlay-box h2, #gameover-box h2 { font-size: 1.6rem; font-weight: 900; }
#overlay-box p, #gameover-box p   { color: var(--dim); font-size: 0.88rem; }
#overlay-preview {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: center;
    margin: 4px 0;
}
.preview-enemy {
    width: 42px; height: 42px;
    border-radius: 50%;
    background: var(--coral);
    border: 2px solid #ff6b57;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    font-weight: 900;
    color: #fff;
}
#overlay-btn, #restart-btn {
    margin-top: 4px;
    padding: 12px 28px;
    background: var(--purple);
    border: none;
    border-radius: 10px;
    color: #fff;
    font-size: 1rem;
    font-weight: 800;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s, transform 0.1s;
}
#overlay-btn:hover, #restart-btn:hover { background: #6644f0; transform: scale(1.03); }
