/* ============================================================
   Design tokens
   ============================================================ */
:root {
    --bg: #101720;
    --bg-alt: #0b1017;
    --panel: #1b2530;
    --panel-light: #263241;
    --border: #34424f;
    --text: #eef2f6;
    --text-dim: #93a4b3;

    --player: #3fa9f5;
    --player-dark: #1f6fb0;
    --enemy: #ef5566;
    --enemy-dark: #a4303c;
    --neutral: #5b6b7a;
    --neutral-dark: #404d59;

    --gold: #f4c542;
    --success: #34d399;
    --danger: #ef4444;

    --radius: 10px;
    --radius-lg: 16px;
    --shadow: 0 6px 18px rgba(0, 0, 0, 0.4);

    --hex-w: clamp(38px, 9vw, 58px);
    --hex-h: calc(var(--hex-w) * 1.1547);

    --slot-size: clamp(46px, 12vw, 60px);
    --slot-gap: clamp(4px, 1vw, 8px);
}

* { box-sizing: border-box; }

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: radial-gradient(circle at 50% -10%, #1a2734 0%, var(--bg) 55%, var(--bg-alt) 100%);
    color: var(--text);
    margin: 0;
    min-height: 100vh;
}

#app-container { padding: clamp(12px, 3vw, 24px); max-width: 720px; margin: 0 auto; }

.hidden { display: none !important; }

h1, h2, h3 { margin: 0 0 0.5em; letter-spacing: 0.02em; }
h1 { font-size: clamp(1.2em, 4vw, 1.6em); }

.hint { color: var(--text-dim); font-size: 0.9em; margin: 0 0 14px; text-align: center; }

/* ============================================================
   Header
   ============================================================ */
header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px clamp(12px, 3vw, 24px);
    background: linear-gradient(180deg, var(--panel) 0%, var(--bg-alt) 100%);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
}

#currency-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--panel-light);
    border: 1px solid var(--border);
    padding: 6px 14px;
    border-radius: 999px;
    font-weight: bold;
    color: var(--gold);
}
.gold-icon { font-size: 1.1em; }

/* ============================================================
   Views & buttons
   ============================================================ */
.view { animation: fadeIn 0.25s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.home-panel { display: flex; flex-direction: column; gap: 10px; margin-top: 12vh; }

.big-btn, .secondary-btn, .attack-btn {
    display: block;
    width: 100%;
    padding: 16px;
    margin: 8px 0;
    font-size: 1.05em;
    font-weight: 600;
    border: none;
    cursor: pointer;
    border-radius: var(--radius);
    transition: transform 0.12s ease, filter 0.15s ease, opacity 0.15s ease;
}
.big-btn:active, .secondary-btn:active, .attack-btn:active { transform: scale(0.97); }

.big-btn {
    background: linear-gradient(180deg, var(--player) 0%, var(--player-dark) 100%);
    color: white;
    box-shadow: var(--shadow);
}
.big-btn:hover { filter: brightness(1.1); }

.secondary-btn {
    background: var(--panel-light);
    color: var(--text);
    border: 1px solid var(--border);
}
.secondary-btn:hover { background: var(--border); }

.attack-btn {
    background: linear-gradient(180deg, var(--enemy) 0%, var(--enemy-dark) 100%);
    color: white;
    box-shadow: var(--shadow);
}
.attack-btn:hover:not(:disabled) { filter: brightness(1.1); }
.attack-btn:disabled { background: var(--panel-light); color: var(--text-dim); cursor: not-allowed; box-shadow: none; }

/* ============================================================
   Hex map
   ============================================================ */
.hex-grid {
    display: flex;
    flex-direction: column;
    width: calc(var(--hex-w) * 5.5 + 24px);
    margin: 0 auto 14px;
}
.hex-row {
    display: flex;
    gap: 4px;
    margin-top: calc(var(--hex-h) * -0.25);
}
.hex-row:first-child { margin-top: 0; }
.hex-row.offset { margin-left: calc(var(--hex-w) / 2 + 2px); }

.sector {
    width: var(--hex-w);
    height: var(--hex-h);
    flex-shrink: 0;
    clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    position: relative;
    transition: transform 0.15s ease, filter 0.15s ease;
    font-size: clamp(0.6em, 1.6vw, 0.75em);
    font-weight: bold;
    color: rgba(255, 255, 255, 0.9);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}
.sector:hover { transform: scale(1.07); z-index: 3; }

.sector.owner-player { background: linear-gradient(160deg, var(--player) 0%, var(--player-dark) 100%); }
.sector.owner-enemy { background: linear-gradient(160deg, var(--enemy) 0%, var(--enemy-dark) 100%); }
.sector.owner-neutral { background: linear-gradient(160deg, var(--neutral) 0%, var(--neutral-dark) 100%); }

.sector.reachable { filter: brightness(1.35); }
.sector.is-source { filter: drop-shadow(0 0 8px var(--player)) brightness(1.2); transform: scale(1.1); z-index: 4; }
.sector.is-target { filter: drop-shadow(0 0 8px var(--enemy)) brightness(1.2); transform: scale(1.1); z-index: 4; }

.sector .garrison { opacity: 0.85; }

.map-legend {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 14px;
    font-size: 0.85em;
    color: var(--text-dim);
    margin-bottom: 16px;
}
.map-legend span { display: flex; align-items: center; gap: 6px; }
.legend-swatch {
    width: 14px; height: 14px; border-radius: 3px; display: inline-block;
    clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
}
.legend-swatch.player { background: var(--player); }
.legend-swatch.enemy { background: var(--enemy); }
.legend-swatch.neutral { background: var(--neutral); }

.map-controls { display: flex; flex-direction: column; }

/* ============================================================
   Kaserne / upgrades
   ============================================================ */
#upgrades { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }
.upgrade-card {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 14px;
}
.upgrade-icon { font-size: 1.8em; }
.upgrade-info { flex: 1; display: flex; flex-direction: column; }
.upgrade-info span { color: var(--text-dim); font-size: 0.85em; }
.buy-btn {
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius);
    background: var(--gold);
    color: #3a2c00;
    font-weight: bold;
    cursor: pointer;
    transition: filter 0.15s ease, transform 0.1s ease;
}
.buy-btn:hover { filter: brightness(1.08); }
.buy-btn:active { transform: scale(0.95); }

/* ============================================================
   Arena
   ============================================================ */
#view-arena {
    display: grid;
    grid-template-rows: auto minmax(220px, 1fr) auto auto auto;
    min-height: 80vh;
    background: linear-gradient(180deg, #1c5c38 0%, #16452a 100%);
    border-radius: var(--radius-lg);
    overflow: hidden;
    padding: clamp(8px, 2vw, 14px);
    gap: 10px;
    box-shadow: var(--shadow);
}

#arena-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 4px 12px;
    font-weight: bold;
}
#sector-label { font-size: 1em; color: var(--text); opacity: 0.9; }
#battle-timer { font-size: 1.6em; color: var(--gold); }

.arena-battle-area {
    position: relative;
    display: flex;
    flex-direction: row;
    background: rgba(0, 0, 0, 0.22);
    border-radius: var(--radius);
    overflow: hidden;
}

.battle-divider {
    position: absolute;
    left: 50%;
    top: 0;
    bottom: 0;
    width: 3px;
    background: rgba(255, 255, 255, 0.25);
    z-index: 5;
}

.units-side { flex: 1; height: 100%; position: relative; }

.castle-icon {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    font-size: clamp(46px, 11vw, 110px);
    filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.4));
}
.player .castle-icon { left: 2%; }
.ai .castle-icon { right: 2%; }

.spawn-grid {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    display: grid;
    grid-template-columns: repeat(3, var(--slot-size));
    grid-template-rows: repeat(2, var(--slot-size));
    gap: var(--slot-gap);
    z-index: 1;
}
#player-spawn-grid, #player-troops { left: 32%; }
#ai-spawn-grid, #ai-troops { right: 32%; }

/* Battlefield troop containers sit exactly on top of the spawn grid,
   sharing its geometry so units drop into visible slots. */
#player-troops, #ai-troops {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: auto;
    height: auto;
    display: grid;
    grid-template-columns: repeat(3, var(--slot-size));
    grid-template-rows: repeat(2, var(--slot-size));
    gap: var(--slot-gap);
    justify-items: center;
    align-items: center;
    z-index: 2;
}

.spawn-point {
    width: 100%;
    height: 100%;
    border: 2px dashed rgba(255, 255, 255, 0.25);
    border-radius: 4px;
}

.vs-container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center; z-index: 10; }
#arena-timer { font-size: clamp(2em, 6vw, 3em); font-weight: bold; color: var(--gold); text-shadow: 0 2px 6px rgba(0,0,0,0.5); }
.vs { font-size: 1.4em; font-weight: bold; color: var(--text); opacity: 0.9; }

#arena-log {
    text-align: center;
    font-size: 0.9em;
    color: var(--text);
    background: rgba(0, 0, 0, 0.25);
    border-radius: var(--radius);
    padding: 8px;
    min-height: 1.2em;
}

.castle-platform {
    background: var(--panel);
    padding: 10px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    display: flex;
    gap: 10px;
}
.platform-side { flex: 1; display: flex; flex-direction: column; align-items: center; min-width: 0; }
.castle-platform h3 { margin: 0 0 6px; font-size: 0.85em; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.05em; }
.platform-divider { width: 1px; background: var(--border); }

.troop-container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-content: center;
    gap: 6px;
    min-height: 50px;
}

/* ============================================================
   Troop units
   ============================================================ */
.troop-unit {
    width: 44px;
    text-align: center;
    transition: transform 0.15s ease, opacity 0.25s ease;
}
.troop-unit.selectable { cursor: pointer; }
.troop-unit.selectable:hover { transform: translateY(-3px) scale(1.08); }
.troop-unit.dying { opacity: 0; transform: scale(0.4) rotate(15deg); }
.troop-unit.hit { animation: hitFlash 0.35s ease; }
@keyframes hitFlash {
    0% { transform: scale(1); filter: none; }
    30% { transform: scale(1.25); filter: brightness(2) saturate(2) hue-rotate(-20deg); }
    100% { transform: scale(1); filter: none; }
}

.troop-icon { font-size: 22px; line-height: 1; }
.troop-hp-bar { height: 5px; background: rgba(0,0,0,0.4); width: 100%; margin-top: 3px; border-radius: 3px; overflow: hidden; }
.troop-hp-fill {
    height: 100%;
    width: 100%;
    background: var(--success);
    transition: width 0.3s ease, background-color 0.3s ease;
}
.troop-hp-fill.mid { background: var(--gold); }
.troop-hp-fill.low { background: var(--danger); }

/* ============================================================
   Action bar
   ============================================================ */
.arena-controls-right {
    display: flex;
    justify-content: center;
    gap: 10px;
    padding: 10px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: var(--radius);
}
.action-btn {
    flex: 1;
    max-width: 160px;
    padding: 10px 6px;
    border-radius: var(--radius);
    border: none;
    background: linear-gradient(180deg, #f4a12f 0%, #d9820f 100%);
    color: white;
    font-weight: bold;
    font-size: 0.85em;
    cursor: pointer;
    transition: transform 0.1s ease, filter 0.15s ease, opacity 0.15s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}
.action-btn span { font-size: 0.85em; }
.action-btn:hover:not(:disabled) { filter: brightness(1.08); }
.action-btn:active:not(:disabled) { transform: scale(0.95); }
.action-btn:disabled { opacity: 0.45; cursor: not-allowed; filter: grayscale(0.4); }

/* ============================================================
   Selection modal
   ============================================================ */
.modal {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--panel);
    border: 1px solid var(--border);
    padding: 20px;
    border-radius: var(--radius-lg);
    z-index: 100;
    text-align: center;
    box-shadow: var(--shadow);
    max-width: 90%;
}
#modal-timer { font-size: 2.2em; font-weight: bold; color: var(--gold); margin-bottom: 6px; }
#selection-options { display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0; justify-content: center; }

/* ============================================================
   Message overlay
   ============================================================ */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(6, 10, 15, 0.72);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
    animation: fadeIn 0.2s ease;
}
.message-box {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
    max-width: 380px;
    width: 100%;
    text-align: center;
    box-shadow: var(--shadow);
}
.message-box p { color: var(--text-dim); line-height: 1.5; }
.message-box .big-btn { margin-top: 12px; }

/* ============================================================
   Responsive tweaks
   ============================================================ */
@media (max-width: 520px) {
    .arena-battle-area { flex-direction: column; }
    .battle-divider { left: 0; right: 0; top: 50%; bottom: auto; width: auto; height: 3px; }
    .units-side { height: 50%; }
    .player .castle-icon { left: 50%; top: 8px; transform: translateX(-50%); }
    .ai .castle-icon { right: 50%; top: auto; bottom: 8px; transform: translateX(50%); }
    #player-spawn-grid, #player-troops, #ai-spawn-grid, #ai-troops { left: 50%; right: auto; transform: translate(-50%, -50%); }
    .arena-controls-right { flex-wrap: wrap; }
    .action-btn { min-width: 40%; }
}
