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

:root {
    --purple: #6D28D9;
    --yellow: #D97706;
    --coral: #DC2626;
    --teal: #059669;
    --green: #16A34A;
    --bg: #87CEEB;
    --bg2: rgba(255,255,255,0.88);
    --bg3: #DCFCE7;
    --border: #86EFAC;
    --dim: #166534;
    --cell: 60px;
    --sidebar: 196px;
}

html, body {
    height: 100%;
    overflow: hidden;
    font-family: 'Inter', sans-serif;
    background: linear-gradient(180deg, #87CEEB 0%, #A8D8A8 35%, #6DB86D 100%);
    color: #14532D;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

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

/* ── 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: rgba(255,255,255,0.82);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    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: #166534;
}
#wave-num {
    color: #052e16;
    font-size: 1rem;
}
#wave-status {
    font-size: 0.75rem;
    font-weight: 700;
    color: #059669;
    letter-spacing: 0.03em;
}
#sidebar-top-spacer {
    width: var(--sidebar);
    flex-shrink: 0;
    background: rgba(255,255,255,0.82);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    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: #C4A472; border-color: #A0845A; }
.cell.empty     { background: #52A852; border-color: #3D8C3D; cursor: pointer; }
.cell.empty:hover { background: #66BB66; border-color: var(--purple); }
.cell.drop-valid   { background: rgba(109,40,217,0.2); border-color: var(--purple); }
.cell.drop-invalid { background: rgba(220,38,38,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: rgba(255,255,255,0.88);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    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: #F0FFF4;
    border-radius: 8px;
    padding: 7px 10px;
    border: 1px solid #86EFAC;
}
.coin-icon { font-size: 1.1rem; }
#coin-count {
    font-size: 1.3rem;
    font-weight: 900;
    line-height: 1;
    color: #14532D;
}
.coins-label {
    font-size: 0.62rem;
    font-weight: 700;
    color: #166534;
    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: #166534;
}
.gen-timer {
    color: #059669;
    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 #86EFAC;
    border-radius: 8px;
    padding: 7px 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    font-size: 0.7rem;
    font-weight: 700;
    color: #166534;
    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(220,38,38,0.12);
    color: var(--coral);
}

/* ── UPGRADE SECTION ─────────────────────────── */
#upgrade-section {
    display: flex;
    flex-direction: column;
    gap: 6px;
    border-top: 1px solid #86EFAC;
    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: #059669;
}
.upgrade-hint {
    font-size: 0.56rem;
    color: #166534;
    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: #86EFAC;
    border-radius: 1px;
    margin: 2px 0;
}
.eq-sign {
    font-size: 1.1rem;
    font-weight: 900;
    color: #166534;
    flex-shrink: 0;
    width: 34px;
    text-align: center;
    line-height: 1;
}
.drag-hint {
    font-size: 0.52rem;
    color: #166534;
    font-weight: 600;
    text-align: center;
}

/* ── UPGRADE SLOTS ───────────────────────────── */
.upgrade-slot {
    flex: 1;
    height: 54px;
    border: 2px dashed #86EFAC;
    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: #059669;
    background: rgba(5,150,105,0.18);
}
.upgrade-slot.filled {
    border-style: solid;
    border-color: var(--purple);
    background: rgba(109,40,217,0.12);
}
.upgrade-slot.filled:hover { background: rgba(109,40,217,0.22); }

.slot-hint {
    font-size: 0.52rem;
    font-weight: 700;
    color: #166534;
    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: #DCFCE7;
    border: 2px solid #86EFAC;
    color: #14532D;
    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: #059669; border-color: #047857; color: #fff; }

#result-slot {
    flex: 1;
    height: 54px;
    border: 2px dashed #86EFAC;
    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: #D97706;
    background: rgba(217,119,6,0.1);
    animation: result-pulse 1.4s ease-in-out infinite;
}
@keyframes result-pulse {
    0%, 100% { box-shadow: 0 0 0 0   rgba(217,119,6,0.5); }
    50%       { box-shadow: 0 0 0 6px rgba(217,119,6,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(5,46,22,0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    backdrop-filter: blur(6px);
}
#overlay.hidden, #gameover-overlay.hidden { display: none; }

#overlay-box, #gameover-box {
    background: rgba(255,255,255,0.96);
    border: 2px solid #86EFAC;
    border-radius: 16px;
    padding: 28px 32px;
    text-align: center;
    max-width: 380px;
    width: 90%;
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
    box-shadow: 0 16px 48px rgba(5,46,22,0.25);
    color: #14532D;
}
#overlay-icon { font-size: 2.5rem; }
#overlay-box h2, #gameover-box h2 { font-size: 1.6rem; font-weight: 900; color: #14532D; }
#overlay-box p, #gameover-box p   { color: #166534; 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: #5B21B6; transform: scale(1.03); }
