/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: #1a1a2e;
    color: #eee;
    min-height: 100vh;
    padding: 0 1rem 2rem;
}
h1, h2, h3 { font-weight: 600; }
header {
    display: flex; align-items: center; justify-content: space-between;
    flex-wrap: wrap; gap: 0.4rem;
    padding: 0.5rem 0;
}
.header-left { display: flex; align-items: baseline; gap: 0.6rem; }
.header-right { display: flex; align-items: center; gap: 0.5rem; }
header h1 { font-size: 1.2rem; color: #e94560; }
header .subtitle { font-size: 0.78rem; color: #888; }
.github-link { color: #888; transition: color 0.2s; display: flex; align-items: center; gap: 0.4rem; text-decoration: none; font-size: 0.85rem; }
.github-link:hover { color: #fff; }

/* === View Tabs === */
.view-tabs {
    display: flex; gap: 0.4rem;
}
.view-tab {
    background: #16213e; border: 1px solid #334; color: #999;
    padding: 0.3rem 1rem; border-radius: 4px; cursor: pointer;
    font-size: 0.82rem; transition: background 0.15s, color 0.15s;
}
.view-tab:hover { background: #1a2a4a; color: #ddd; }
.view-tab.active { background: #e94560; border-color: #e94560; color: #fff; font-weight: 600; }

/* === Info Pages === */
.info-page { padding: 0 0.5rem; }
.info-content {
    max-width: 800px; margin: 0 auto;
    background: #16213e; border-radius: 6px; padding: 1.5rem 2rem;
    border: 1px solid #223; line-height: 1.6;
}
.back-link { color: #888; text-decoration: none; font-size: 0.85rem; display: inline-block; margin-bottom: 0.75rem; transition: color 0.2s; }
.back-link:hover { color: #e94560; }
.info-content h2 { font-size: 1.3rem; color: #e94560; margin-bottom: 0.75rem; }
.info-content h3 {
    font-size: 1rem; color: #ccc; margin-top: 1.5rem; margin-bottom: 0.5rem;
    padding-bottom: 0.3rem; border-bottom: 1px solid #334;
}
.info-lead { color: #bbb; margin-bottom: 1rem; font-size: 0.9rem; }
.info-list { margin: 0.5rem 0 0.75rem 1.5rem; font-size: 0.85rem; color: #ccc; }
.info-list li { margin-bottom: 0.35rem; }
.info-list-muted { color: #999; }
.info-note { font-size: 0.8rem; color: #888; margin-top: 0.4rem; font-style: italic; }
.info-refs {
    margin: 0.3rem 0 0.5rem 1.5rem; font-size: 0.78rem; list-style: none; padding: 0;
}
.info-refs li { margin-bottom: 0.2rem; }
.info-refs li::before { content: '\2192 '; color: #666; }
.info-refs a { color: #5b9bd5; text-decoration: none; }
.info-refs a:hover { text-decoration: underline; color: #7bb8e8; }
.info-section-refs { font-size: 0.75rem; color: #777; margin-top: 0.3rem; }
.info-section-refs a { color: #5b9bd5; text-decoration: none; }
.info-section-refs a:hover { text-decoration: underline; }

.info-table {
    width: 100%; border-collapse: collapse; margin: 0.5rem 0 0.75rem;
    font-size: 0.82rem;
}
.info-table th {
    background: #1a2540; color: #999; font-weight: 600;
    padding: 0.4rem 0.6rem; text-align: left; border-bottom: 1px solid #334;
}
.info-table td {
    padding: 0.35rem 0.6rem; border-bottom: 1px solid #1a1a2e; color: #ccc;
}
.info-table tbody tr:hover { background: #1a2540; }

.info-diagram {
    background: #0f1629; border: 1px solid #334; border-radius: 4px;
    padding: 0.75rem 1rem; margin: 0.5rem 0 0.75rem;
    font-family: 'Consolas', 'Courier New', monospace; font-size: 0.82rem;
    color: #bbb; text-align: center; line-height: 1.8;
}
.info-chains {
    background: #0f1629; border: 1px solid #334; border-radius: 4px;
    padding: 0.5rem 1rem; margin: 0.5rem 0 0.75rem;
    font-size: 0.82rem; color: #ccc; line-height: 1.8;
}
.info-formula {
    background: #0f1629; border: 1px solid #445; border-radius: 4px;
    padding: 0.75rem 1rem; margin: 0.5rem 0 0.75rem;
    font-family: 'Consolas', 'Courier New', monospace; font-size: 0.88rem;
    color: #e94560; text-align: center; line-height: 2;
}

/* === Buttons === */
.btn {
    background: #16213e; border: 1px solid #334; color: #ddd;
    padding: 0.35rem 0.75rem; border-radius: 4px; cursor: pointer;
    font-size: 0.82rem; transition: background 0.15s;
}
.btn:hover { background: #1a2a4a; }
.btn-primary { background: #e94560; border-color: #e94560; color: #fff; font-weight: 600; }
.btn-primary:hover { background: #c93550; }
.btn-danger { border-color: #a33; color: #f88; }
.btn-danger:hover { background: #2a1010; }

/* === Inputs === */
input[type="number"], select {
    background: #0f1629; border: 1px solid #334; color: #ddd;
    padding: 0.4rem 0.6rem; border-radius: 3px; font-size: 0.8rem;
    width: 70px; text-align: right;
    -moz-appearance: textfield;
}
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
input[type="number"]:focus, select:focus { border-color: #e94560; outline: none; }
select { width: auto; text-align: left; }

/* === Army Setup === */
.army-setup { margin-bottom: 1rem; }
.army-row {
    display: flex; gap: 0.75rem; align-items: flex-start;
}
.army-col { flex: 1; }
.army-panel {
    background: #16213e; border-radius: 6px; padding: 0.75rem;
    border: 1px solid #223;
}
.army-panel h2 { font-size: 1rem; margin-bottom: 0.5rem; color: #ccc; }
.battle-view-btn-row { display: flex; justify-content: center; margin-bottom: 0.5rem; }

/* === Section Dividers === */
.section-divider {
    font-size: 0.82rem; font-weight: 600; color: #999;
    text-transform: uppercase; letter-spacing: 0.06em;
    margin: 0.75rem 0 0.5rem;
    padding-bottom: 0.25rem;
    border-bottom: 1px solid #334;
}

/* === Grid Controls === */
.grid-controls {
    display: flex; align-items: center; gap: 0.5rem;
    margin-bottom: 0.5rem;
}
.grid-controls-label { font-size: 0.78rem; font-weight: 600; color: #999; }
.default-count-input { width: 80px; }
.btn-sm { font-size: 0.72rem; padding: 0.2rem 0.5rem; }

/* === Buff Section === */
.buff-section {
    margin-bottom: 0;
}
.buff-row {
    display: flex; align-items: center; gap: 0.75rem;
    margin-bottom: 0.6rem; font-size: 0.78rem;
}
.buff-label { width: 65px; font-weight: 600; }
.buff-stat-label { color: #999; font-size: 0.72rem; }
.buff-row input[type="number"] { width: 55px; font-size: 0.75rem; }

/* === Troop Grid === */
.troop-grid { font-size: 0.78rem; }
.tier-group-body { overflow: hidden; }

.grid-header {
    display: grid; grid-template-columns: 40px repeat(4, 1fr);
    gap: 0.2rem; padding: 0.2rem 0; font-weight: 600; color: #999;
}
.grid-header span { text-align: center; cursor: pointer; }
.grid-header span:hover { color: #e94560; }
.grid-row {
    display: grid; grid-template-columns: 40px repeat(4, 1fr);
    gap: 0.2rem; margin-bottom: 0.15rem;
}
.grid-row .tier-label {
    font-weight: 600; color: #888; display: flex; align-items: center;
    cursor: pointer;
}
.grid-row .tier-label:hover { color: #e94560; }
.grid-row input[type="number"] { width: 100%; }

/* === Troop Type Colors === */
.color-ground { color: #d4a843; }
.color-ranged { color: #4caf6e; }
.color-mounted { color: #5b9bd5; }
.color-siege { color: #e07040; }
.bg-ground { background: #3a2e10; }
.bg-ranged { background: #1a3320; }
.bg-mounted { background: #1a2a3e; }
.bg-siege { background: #3a1a10; }
.border-ground { border-color: #8B6914 !important; }
.border-ranged { border-color: #2d8a4e !important; }
.border-mounted { border-color: #4a7fb5 !important; }
.border-siege { border-color: #c44e2e !important; }

/* === Controls Bar === */
.controls-bar {
    display: none; align-items: center; justify-content: center;
    gap: 1rem; padding: 0.75rem; background: #16213e;
    border-radius: 6px; margin-bottom: 0.75rem; border: 1px solid #223;
    position: relative;
}
.battle-view-active .controls-bar { display: flex; }
.playback-buttons { display: flex; gap: 0.4rem; }
.speed-control { display: flex; align-items: center; gap: 0.4rem; font-size: 0.8rem; color: #999; }
#speed-slider { width: 100px; }

/* === Phase Indicator === */
.phase-indicator {
    display: none; justify-content: center; gap: 1.5rem;
    margin-bottom: 0.75rem; font-size: 0.82rem;
}
.battle-view-active .phase-indicator { display: flex; }
.phase-dot {
    padding: 0.25rem 0.75rem; border-radius: 12px;
    background: #16213e; color: #666; border: 1px solid #334;
    transition: all 0.2s;
}
.round-label {
    font-weight: 600; color: #ccc; margin-right: 0.5rem;
}
.phase-dot.active { background: #e94560; color: #fff; border-color: #e94560; }
.phase-dot.done { background: #223; color: #888; border-color: #555; }

/* === Battlefield === */
.battlefield-area { margin-bottom: 0.75rem; display: none; }
.battle-view-active .battlefield-area { display: block; }
.battlefield {
    background: linear-gradient(180deg, #0d1a0d 0%, #1a2612 40%, #1a2612 60%, #0d1a0d 100%);
    border-radius: 6px; padding: 0;
    width: 100%; aspect-ratio: 16 / 9; max-height: 420px;
    border: 1px solid #2a3a1a;
    position: relative; overflow: visible;
}

/* Center line */
.bf-center-line {
    position: absolute; top: 0; bottom: 0; left: 50%;
    width: 1px; background: rgba(255,255,255,0.08);
    z-index: 1;
}

/* Unit markers — positioned absolutely on the map */
.unit-marker {
    position: absolute; cursor: pointer; z-index: 3;
    display: flex; flex-direction: column; align-items: center;
    transition: left 0.4s ease-out, top 0.4s ease-out, opacity 0.3s;
    user-select: none;
}
.unit-marker:hover { z-index: 4; }
.unit-marker:hover .unit-icon { transform: scale(1.12); }

.unit-icon {
    width: 52px; height: 52px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-weight: 800; font-size: 1.1rem; color: #fff;
    border: 2px solid rgba(255,255,255,0.25);
    transition: transform 0.15s, box-shadow 0.2s;
    text-shadow: 0 1px 3px rgba(0,0,0,0.6);
}
.unit-icon.icon-ground  { background: radial-gradient(circle, #d4a843, #8B6914); }
.unit-icon.icon-ranged  { background: radial-gradient(circle, #4caf6e, #2d8a4e); }
.unit-icon.icon-mounted { background: radial-gradient(circle, #5b9bd5, #4a7fb5); }
.unit-icon.icon-siege   { background: radial-gradient(circle, #e07040, #c44e2e); }

.unit-count {
    font-size: 0.72rem; font-weight: 700; color: #ddd;
    margin-top: 2px; text-shadow: 0 1px 4px rgba(0,0,0,0.8);
    white-space: nowrap;
}
.unit-label {
    font-size: 0.62rem; color: #999;
    text-shadow: 0 1px 4px rgba(0,0,0,0.8);
}

.unit-marker.eliminated { opacity: 0.15; pointer-events: none; }
.unit-marker.eliminated .unit-icon { border-style: dashed; }
.unit-marker.highlighted .unit-icon {
    box-shadow: 0 0 16px rgba(233, 69, 96, 0.7);
    border-color: #e94560;
}
.unit-marker.damaged .unit-icon { animation: damage-flash-icon 0.35s ease-out; }

/* Range & speed indicators (persistent, brightened on hover) */
.bf-range-indicator {
    position: absolute; height: 4px;
    opacity: 0.12; pointer-events: none;
    z-index: 2; transform: translateY(-50%);
    border-radius: 2px; transition: opacity 0.15s;
}
.bf-speed-projection {
    position: absolute; width: 20px; height: 20px;
    border-radius: 50%; opacity: 0.20;
    border: 2px dashed; pointer-events: none;
    z-index: 2; transform: translate(-50%, -50%);
    transition: opacity 0.15s;
}

/* X-axis scale */
.bf-axis-tick {
    position: absolute; bottom: 12px; width: 1px; height: 8px;
    background: rgba(255,255,255,0.25); pointer-events: none;
    z-index: 1; transform: translateX(-50%);
}
.bf-axis-label {
    position: absolute; bottom: 1px;
    font-size: 10px; color: rgba(255,255,255,0.3);
    pointer-events: none; z-index: 1;
    transform: translateX(-50%);
    font-family: 'Consolas', 'Courier New', monospace;
}

/* SVG attack arrow overlay */
.bf-svg-overlay {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    pointer-events: none; z-index: 5;
}
.attack-line {
    stroke: #e94560; stroke-width: 2; fill: none;
}
.attack-label-bg {
    fill: rgba(17, 24, 39, 0.85); rx: 3; ry: 3;
}
.attack-label {
    fill: #e94560; font-size: 11px; font-weight: 600;
    text-anchor: middle; dominant-baseline: central;
}

/* Detail panel (click on unit) */
.detail-panel {
    position: absolute; background: #1a1a2e; border: 1px solid #445;
    border-radius: 6px; padding: 0.6rem; font-size: 0.78rem;
    z-index: 20; min-width: 180px; max-width: 250px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.6);
    display: none;
}
.detail-panel .detail-title { font-weight: 700; margin-bottom: 0.3rem; }
.detail-panel .detail-tiers { margin-bottom: 0.3rem; }
.detail-panel .detail-tier-row {
    display: flex; justify-content: space-between; font-size: 0.75rem;
    padding: 0.05rem 0; color: #bbb; gap: 1rem;
}
.detail-panel .detail-tier-row .tier-lost { color: #e94560; font-size: 0.7rem; }
.detail-panel .detail-total {
    border-top: 1px solid #334; padding-top: 0.25rem; font-weight: 600; color: #ccc;
}
.detail-panel .detail-targets {
    margin-top: 0.25rem; color: #888; font-size: 0.72rem;
}

@keyframes damage-flash-icon {
    0% { box-shadow: 0 0 20px rgba(233, 69, 96, 0.8); border-color: #e94560; }
    100% { box-shadow: none; border-color: rgba(255,255,255,0.25); }
}

/* === Summary Bar === */
.summary-bar {
    background: #16213e; border-radius: 6px; padding: 0.75rem;
    border: 1px solid #223; margin-bottom: 0.75rem;
    display: none;
}
.summary-bar.visible { display: block; }
.summary-content {
    display: flex; justify-content: space-between; gap: 1rem;
}
.summary-side { flex: 1; }
.summary-side h4 { font-size: 0.82rem; margin-bottom: 0.3rem; color: #999; }
.summary-row {
    display: flex; justify-content: space-between; align-items: center;
    font-size: 0.75rem; margin-bottom: 0.15rem;
}
.summary-row .loss { color: #e94560; }
.summary-row .gain { color: #4caf6e; }
.health-bar {
    height: 8px; background: #333; border-radius: 4px;
    overflow: hidden; margin-top: 0.3rem;
}
.health-bar .fill {
    height: 100%; border-radius: 4px; transition: width 0.3s;
    background: linear-gradient(90deg, #e94560, #4caf6e);
}
.summary-meta { text-align: center; font-size: 0.75rem; color: #888; margin-top: 0.4rem; }

/* === Winner banner === */
.winner-banner {
    text-align: center; padding: 0.5rem; font-size: 1.1rem;
    font-weight: 700; color: #ffd700; margin-bottom: 0.5rem;
}

/* === Bottom Panels === */
.bottom-panels { display: none; gap: 0.75rem; }
.battle-view-active .bottom-panels { display: flex; }
.battle-log-panel {
    flex: 2; background: #16213e; border-radius: 6px;
    padding: 0.75rem; border: 1px solid #223;
}
.battle-log-panel h3 { font-size: 0.9rem; margin-bottom: 0.4rem; }
.log-header { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 0.4rem; margin-bottom: 0.4rem; }
.log-filters { display: flex; gap: 0.4rem; align-items: center; font-size: 0.75rem; }
.log-filters label { display: flex; align-items: center; gap: 0.2rem; color: #999; }
.battle-log {
    max-height: 300px; overflow-y: auto; font-family: 'Consolas', 'Courier New', monospace;
    font-size: 0.73rem; line-height: 1.4;
}
.log-entry { padding: 0.15rem 0.3rem; border-bottom: 1px solid #1a1a2e; }
.log-entry:hover { background: #1a2540; }
.log-entry .log-round { color: #888; }
.log-entry .log-damage { color: #e94560; }
.log-entry .log-kills { color: #ffd700; }
.log-entry.hidden { display: none; }
.log-entry.log-counter { opacity: 0.7; font-size: 0.85em; padding-left: 1.2rem; }
.log-counter-label { color: #e94560; }

/* Comparison */
.comparison-panel {
    flex: 1; background: #16213e; border-radius: 6px;
    padding: 0.75rem; border: 1px solid #223;
    display: none;
}
.comparison-panel.visible { display: block; }
.comparison-panel h3 { font-size: 0.9rem; margin-bottom: 0.4rem; }
.comp-row { display: flex; justify-content: space-between; font-size: 0.78rem; margin-bottom: 0.2rem; }
.comp-delta.positive { color: #4caf6e; }
.comp-delta.negative { color: #e94560; }

/* === Tooltip === */
.tooltip {
    position: absolute; background: #1a1a2e; border: 1px solid #445;
    border-radius: 5px; padding: 0.5rem 0.7rem; font-size: 0.75rem;
    z-index: 100; pointer-events: none; white-space: nowrap;
    box-shadow: 0 4px 12px rgba(0,0,0,0.5);
}
.tooltip .tt-title { font-weight: 700; margin-bottom: 0.2rem; }
.tooltip .tt-row { color: #bbb; }
.tooltip .tt-buff { color: #4caf6e; }

/* === Battle View Mode === */
.battle-view-active .army-setup { display: none; }

/* === Scrollbar === */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: #111; }
::-webkit-scrollbar-thumb { background: #444; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #666; }

/* === Overflow Prevention === */
main { max-width: 100%; overflow-x: hidden; }
.info-table { display: block; overflow-x: auto; }
.info-diagram, .info-formula, .info-chains { overflow-x: auto; word-break: break-word; }

/* === Tablet Breakpoint (768px) === */
@media (max-width: 768px) {
    body { padding: 0 0.5rem 1.5rem; }

    /* Stack army panels vertically */
    .army-row { flex-direction: column; }
    .army-col { flex: none; width: 100%; }

    /* View tabs in header */
    .view-tabs { flex-wrap: wrap; }

    /* Controls bar: wrap children, touch-friendly */
    .controls-bar {
        flex-wrap: wrap; justify-content: center; gap: 0.5rem;
        padding: 0.5rem;
    }
    .controls-bar > #btn-battle-view { position: static; }
    .playback-buttons { flex-wrap: wrap; justify-content: center; }
    .speed-control { flex-wrap: wrap; justify-content: center; }
    #speed-slider { width: 140px; }

    /* Phase indicator: wrap */
    .phase-indicator { flex-wrap: wrap; gap: 0.5rem; }

    /* Stack bottom panels vertically */
    .battle-view-active .bottom-panels { flex-direction: column; }
    .battle-log-panel { flex: none; }
    .comparison-panel { flex: none; }

    /* Summary bar */
    .summary-content { flex-direction: column; gap: 0.5rem; }

    /* Touch target minimums */
    .btn { min-height: 44px; padding: 0.5rem 0.75rem; }
    .btn-sm { min-height: 36px; }
    .view-tab { min-height: 44px; padding: 0.4rem 0.75rem; }
    select { min-height: 44px; }

    /* Info pages */
    .info-content { padding: 1rem; }
}

/* === Mobile Breakpoint (480px) === */
@media (max-width: 480px) {
    body { padding: 0 0.35rem 1rem; font-size: 0.85rem; }

    /* Header */
    header { flex-wrap: wrap; gap: 0.3rem; }
    .header-left { flex-wrap: wrap; gap: 0.3rem; }
    header h1 { font-size: 1rem; }
    header .subtitle { font-size: 0.7rem; }

    /* Smaller army panels */
    .army-panel { padding: 0.5rem; }
    .army-panel h2 { font-size: 0.88rem; }

    /* Tighter troop grid */
    .grid-header { grid-template-columns: 30px repeat(4, 1fr); gap: 0.1rem; font-size: 0.68rem; }
    .grid-row { grid-template-columns: 30px repeat(4, 1fr); gap: 0.1rem; }
    .grid-row .tier-label { font-size: 0.68rem; }
    input[type="number"] { width: 100%; font-size: 0.72rem; padding: 0.3rem 0.2rem; }

    /* Tighter buff inputs */
    .buff-row { gap: 0.35rem; font-size: 0.72rem; }
    .buff-label { width: 50px; font-size: 0.68rem; }
    .buff-row input[type="number"] { width: 45px; font-size: 0.68rem; }

    /* Smaller buttons and controls */
    .btn { font-size: 0.75rem; padding: 0.4rem 0.5rem; }
    .view-tab { font-size: 0.72rem; padding: 0.35rem 0.5rem; }
    .section-divider { font-size: 0.72rem; }

    /* Phase indicator tighter */
    .phase-indicator { gap: 0.3rem; font-size: 0.72rem; }
    .phase-dot { padding: 0.2rem 0.5rem; font-size: 0.72rem; }
    .round-label { font-size: 0.72rem; }

    /* Battle log */
    .battle-log { font-size: 0.65rem; max-height: 200px; }
    .log-filters { flex-wrap: wrap; gap: 0.3rem; }

    /* Info pages */
    .info-content { padding: 0.75rem; }
    .info-content h2 { font-size: 1.1rem; }
    .info-list { margin-left: 1rem; font-size: 0.78rem; }
}
