@font-face {
    font-family: 'VT323';
    src: url('../fonts/VT323-Regular.ttf') format('truetype');
    font-display: swap;
}

:root {
    --bg: #11202e;
    --pill: rgba(241, 239, 232, 0.94);
    --pill-ink: #2c2c2a;
    --pill-muted: #5f5e5a;
    --glass: rgba(20, 20, 20, 0.55);
    --panel: #1c2b38;
    --ink: #eef2f5;
    --muted: #9aa5ad;
    --blue: #378add;
    --red: #e24b4a;
    --teal: #5dcaa5;
    --air: #ff8c1a;
    --radius: 8px;
}

* { box-sizing: border-box; }
html, body {
    margin: 0; height: 100%; overflow: hidden;
    background: var(--bg); color: var(--ink);
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
    touch-action: none;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
kbd {
    font-family: inherit; font-size: 0.7em; opacity: 0.55; padding: 0 3px;
    border: 1px solid currentColor; border-radius: 4px; margin-left: 2px;
}
@media (pointer: coarse) { kbd { display: none; } }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.tiny { font-size: 12px; }
.rotate-hint { display: none; }
@media (orientation: portrait) and (pointer: coarse) { .rotate-hint { display: block; } }

#app { position: fixed; inset: 0; }
#stage { position: absolute; inset: 0; }
#stage canvas { display: block; width: 100%; height: 100%; }
#labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }

.hud { position: absolute; left: 0; right: 0; pointer-events: none; }
.hud button, .hud .pill, .hud #az-track { pointer-events: auto; }

/* ---- Top ---- */
#topbar {
    top: 0; padding: max(10px, env(safe-area-inset-top)) 12px 0;
    display: flex; align-items: center; gap: 8px;
}
#turn-banner {
    flex: 1; background: var(--glass); border-radius: var(--radius); padding: 8px 14px;
    font-size: 15px; font-weight: 600; color: var(--teal);
}
#turn-banner.red { color: #ff8a89; }
#turn-banner.blue { color: #8cc4ff; }
#corner-btns { display: flex; gap: 6px; }
.chip {
    background: var(--glass); border: 0; border-radius: var(--radius);
    padding: 8px 12px; min-height: 36px; color: var(--ink);
}
.chip:hover { background: rgba(40, 40, 40, 0.7); }

/* ---- Bottom ---- */
#bottom {
    bottom: 0; padding: 0 12px max(10px, env(safe-area-inset-bottom));
    display: flex; flex-direction: column; gap: 8px;
}
#pills-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: stretch; }
.pill {
    flex: 1; min-width: 118px; background: var(--pill); color: var(--pill-ink);
    border-radius: var(--radius); padding: 6px 8px;
}
.pill.wide { flex: 2; min-width: 220px; padding: 6px 12px; }
.pill-label { font-size: 11px; color: var(--pill-muted); padding-left: 4px; }
.pill-label.row { display: flex; justify-content: space-between; align-items: baseline; padding: 0; }
.pill-body { display: flex; align-items: center; justify-content: space-between; }
.pill-value { font-size: 19px; font-weight: 600; min-width: 3.2em; text-align: center; font-variant-numeric: tabular-nums; }
.pill-value.small { font-size: 13px; min-width: 0; }
.step {
    width: 44px; height: 36px; border: 0; border-radius: 6px;
    background: rgba(0, 0, 0, 0.08); color: var(--pill-ink); font-size: 14px;
}
.step:active { background: rgba(0, 0, 0, 0.18); }

#az-track {
    position: relative; height: 30px; margin-top: 6px; background: #d3d1c7;
    border-radius: 15px; cursor: ew-resize; touch-action: none;
}
#az-center { position: absolute; left: 50%; top: 4px; bottom: 4px; width: 2px; background: #888780; }
#az-thumb {
    position: absolute; left: 50%; top: 3px; width: 24px; height: 24px; border-radius: 50%;
    background: var(--blue); transform: translateX(-50%); box-shadow: 0 1px 3px rgba(0,0,0,0.35);
}

#wind-badge {
    display: flex; align-items: center; gap: 6px; background: var(--glass);
    border-radius: var(--radius); padding: 4px 8px 4px 10px; font-size: 13px; color: #d3d1c7;
}
#wind-badge-host { width: 44px; height: 44px; }

#controls-row { display: flex; gap: 8px; align-items: center; }
.ctl {
    min-height: 44px; border: 0; border-radius: var(--radius); padding: 8px 16px;
    background: #fff; color: #222; font-size: 14px; font-weight: 500; pointer-events: auto;
}
.ctl:hover { background: #f1efe8; }
.ctl.active { background: var(--blue); color: #fff; }
#weapon-box {
    flex: 1; background: var(--panel); border-radius: var(--radius); padding: 4px 6px 4px 12px;
    display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 44px;
    pointer-events: auto;
}
.ctl.light { min-height: 36px; padding: 4px 10px; }
/* Weapon selector: twice its natural width, name on the left and the
   caret/shortcut on the right; never wider than its section. */
#btn-weapon {
    width: 290px; max-width: 100%;
    display: flex; align-items: center; gap: 6px; text-align: left;
}
#btn-weapon #weapon-name { flex: 1; }
.ctl.fire { background: var(--red); color: #fcebeb; padding: 8px 22px; }
.ctl.fire:hover { background: #cf3c3b; }
.ctl.air { background: var(--air); color: #2a1400; }
.ctl.recall { background: #8a9a4a; color: #151a08; }
.ctl.recall:hover { background: #7b8a40; }
.unit-label {
    position: absolute; transform: translate(-50%, -100%); white-space: nowrap; pointer-events: none;
    font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 6px;
    background: rgba(20, 20, 20, 0.6); text-shadow: 0 1px 1px #000;
}
.unit-label.blue { color: #b9dcff; }
.unit-label.red { color: #ffc2c1; }
.unit-label.empty { color: #ffd36b; }
@media (min-width: 641px) and (min-height: 560px) and (pointer: coarse) { .unit-label { font-size: 12px; } }
.ctl:disabled, .step:disabled { opacity: 0.45; cursor: default; }
#move-left {
    display: flex; justify-content: space-between; font-size: 11px; color: var(--muted);
    padding: 0 2px;
}

/* ---- World-anchored labels ---- */
.tank-label {
    position: absolute; transform: translate(-50%, -100%); text-align: center;
    font-size: 11px; font-weight: 600; text-shadow: 0 1px 2px #000; white-space: nowrap;
}
.tank-label .hp { width: 64px; height: 6px; background: #b22; border-radius: 3px; margin: 2px auto 0; overflow: hidden; }
.tank-label .hp i { display: block; height: 100%; background: #2fd35a; }
.tank-label .buffs { font-size: 10px; color: #ffe39a; }
.tank-label .bubble {
    background: #fff; color: #111; border-radius: 10px; padding: 2px 8px; margin-bottom: 4px;
    font-size: 13px; text-shadow: none;
}
.float-text {
    position: absolute; transform: translate(-50%, -50%); font-family: 'VT323', monospace;
    font-size: 26px; font-weight: 700; text-shadow: 0 2px 3px #000; white-space: nowrap;
}

/* New-version banner + post-update toast (js/update.js). */
#update-banner {
    position: fixed; top: 0; left: 50%; transform: translateX(-50%); z-index: 50;
    max-width: calc(100% - 16px); margin-top: max(0px, env(safe-area-inset-top));
    background: rgba(20, 49, 74, 0.97); color: #fff; border: 2px solid #7ad0ff; border-top: 0;
    border-radius: 0 0 10px 10px; padding: 8px 16px; min-height: 44px;
    font-size: 14px; cursor: pointer; box-shadow: 0 3px 12px rgba(0,0,0,0.5); white-space: nowrap;
}
#update-banner u { color: #9fdcff; margin-left: 6px; }
@media (max-width: 520px) { #update-banner .ub-long { display: none; } }
#update-toast {
    position: fixed; top: 64px; left: 50%; transform: translateX(-50%); z-index: 50;
    background: rgba(20, 90, 60, 0.95); color: #fff; border-radius: 10px; padding: 8px 16px;
    font-size: 14px; font-weight: 600; box-shadow: 0 3px 12px rgba(0,0,0,0.5);
    transition: opacity 0.8s; pointer-events: none;
}
#update-toast.fade { opacity: 0; }

/* Drag-to-aim guide (drawn in screen space over the battlefield). */
#drag-guide { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; overflow: visible; }
#drag-guide .dg-line { stroke: rgba(255,255,255,0.85); stroke-width: 3; stroke-dasharray: 8 6; stroke-linecap: round; }
#drag-guide .dg-start { fill: rgba(255,255,255,0.9); }
#drag-guide .dg-end { fill: none; stroke-width: 3; }
#drag-guide[data-side="blue"] .dg-end { stroke: #8cc4ff; }
#drag-guide[data-side="red"] .dg-end { stroke: #ff9a99; }
#drag-guide-label {
    position: absolute; z-index: 3; pointer-events: none; white-space: nowrap;
    background: rgba(20,20,20,0.75); color: #fff; border-radius: 8px; padding: 5px 10px;
    font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums;
}

#win-banner {
    position: absolute; left: 0; right: 0; top: 34%; text-align: center; pointer-events: none;
    font-family: 'VT323', monospace; font-size: clamp(34px, 7vw, 72px); letter-spacing: 2px;
    text-shadow: 0 4px 10px #000; opacity: 0; transition: opacity 0.3s;
}

/* ---- Overlays ---- */
.overlay {
    position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
    background: rgba(8, 16, 24, 0.62); padding: 16px; z-index: 5;
}
.panel {
    background: rgba(17, 32, 46, 0.96); border: 1px solid rgba(255,255,255,0.08);
    border-radius: 14px; padding: 22px 22px 16px; width: 100%; max-width: 420px;
    max-height: 100%; overflow-y: auto; text-align: center;
}
.panel.wide { max-width: 640px; text-align: left; }
.panel h1 { font-family: 'VT323', monospace; font-size: 54px; margin: 0 0 6px; letter-spacing: 3px; font-weight: 400; }
.panel h1 span { color: var(--red); }
.panel h2 { margin: 4px 0 10px; }
.panel h3 { margin: 16px 0 6px; font-size: 15px; color: var(--teal); }
.panel p, .panel li { font-size: 14px; line-height: 1.45; }
.panel ul { padding-left: 0; list-style: none; margin: 0; }
.panel li { display: flex; gap: 10px; align-items: center; margin: 6px 0; }
.panel li img { width: 34px; height: 34px; flex: none; }
.keys { line-height: 2; }
.keys kbd { opacity: 0.8; font-size: 12px; }
button.big {
    display: block; width: 100%; min-height: 48px; margin: 8px 0; border: 0; border-radius: 10px;
    background: #fff; color: #1a1a1a; font-size: 16px; font-weight: 600; text-align: left; padding: 10px 16px;
}
button.big b { display: inline-block; width: 26px; color: var(--blue); }
button.big:hover { background: #e8eef4; }
button.big.ghost { background: transparent; color: var(--ink); border: 1px solid rgba(255,255,255,0.25); }
.row-btns { display: flex; gap: 8px; }
.row-btns button.big { text-align: center; }
button.link { background: none; border: 0; color: var(--muted); padding: 8px; }
#results-stats { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: 14px; }
#results-stats th, #results-stats td { padding: 6px 4px; border-bottom: 1px solid rgba(255,255,255,0.08); }
#results-stats th { color: var(--muted); font-weight: 500; }

.short-label { display: none; }
.ctl { white-space: nowrap; }

/* ---- Tablets (touch screens wider than a phone) ----
   Same arrangement as desktop, scaled up for fingers: ≥48px tap targets,
   larger text, a taller azimuth slider. Mouse/trackpad desktops never match
   (pointer: fine), so the desktop layout is untouched. */
@media (min-width: 641px) and (min-height: 560px) and (pointer: coarse) {
    #topbar { padding-left: 16px; padding-right: 16px; }
    #turn-banner { font-size: 17px; padding: 10px 16px; }
    .chip { min-height: 48px; padding: 10px 18px; font-size: 16px; }
    #bottom { padding-left: 16px; padding-right: 16px; gap: 10px; }
    #pills-row { gap: 10px; }
    .pill { padding: 8px 10px; min-width: 200px; }
    .pill.wide { min-width: 260px; padding: 8px 14px; }
    .pill-label { font-size: 13px; }
    .pill-value { font-size: 24px; }
    .pill-value.small { font-size: 16px; }
    .step { width: 56px; height: 48px; font-size: 18px; }
    #az-track { height: 44px; border-radius: 22px; margin-top: 8px; }
    #az-thumb { width: 36px; height: 36px; top: 4px; }
    #wind-badge { font-size: 15px; padding: 6px 10px 6px 14px; }
    #wind-badge-host, #wind-badge-host canvas { width: 56px !important; height: 56px !important; }
    #controls-row { gap: 10px; }
    .ctl { min-height: 56px; font-size: 17px; padding: 10px 22px; }
    .ctl.light { min-height: 48px; }
    #weapon-box { min-height: 60px; padding: 6px 8px 6px 16px; }
    #btn-weapon { width: 380px; }
    .ctl.fire { padding: 10px 32px; }
    #move-left { font-size: 13px; }
    .tank-label { font-size: 13px; }
    .tank-label .hp { width: 80px; height: 8px; }
    .tank-label .buffs { font-size: 12px; }
    .float-text { font-size: 32px; }
}

/* Portrait tablets: the bigger pills don't fit four across, so Power +
   Elevation take the first row and Azimuth + wind the second (like phones). */
@media (min-width: 641px) and (max-width: 900px) and (min-height: 560px) and (pointer: coarse) {
    .pill { min-width: 0; }
    #pill-power, #pill-angle { flex: 1 1 calc(50% - 5px); order: 1; }
    .pill.wide { flex: 1 1 0; min-width: 0; order: 2; }
    #wind-badge { order: 3; flex: none; }
    #btn-weapon { width: 100%; }
}

/* ---- Phone widths ----
   Pills: Power + Elevation split the first row; Azimuth + a compact wind
   badge share the second, so nothing gets squeezed past its content. */
@media (max-width: 640px) {
    #bottom { padding-left: 8px; padding-right: 8px; gap: 6px; }
    #pills-row { gap: 6px; }
    .pill { min-width: 0; padding: 4px; }
    #pill-power, #pill-angle { flex: 1 1 calc(50% - 3px); order: 1; }
    .pill.wide { flex: 1 1 0; min-width: 0; order: 2; padding: 4px 10px; }
    #wind-badge { order: 3; flex: none; flex-direction: column; justify-content: center; gap: 0; padding: 2px 6px; font-size: 11px; }
    #wind-badge-host { width: 36px; height: 36px; }
    #wind-badge-host canvas { width: 36px !important; height: 36px !important; }
    .step { width: 40px; flex: none; }
    .pill-value { font-size: 17px; min-width: 0; flex: 1; }
    #turn-banner { font-size: 14px; }
    .az-long { display: none; }

    /* Control row on phones: fixed-size Move / Air strike / Fire so they
       never get squeezed, and the weapon selector takes whatever is left
       (truncating a long weapon name instead of pushing Fire off-screen). */
    #controls-row { gap: 6px; }
    .ctl { padding: 8px 10px; font-size: 14px; }
    #btn-move { flex: none; min-width: 64px; }
    #btn-air, #btn-recall { flex: none; min-width: 64px; padding: 8px; }
    #btn-fire { flex: none; min-width: 68px; padding: 8px 12px; }
    #weapon-box { flex: 1 1 0; min-width: 0; padding: 4px; }
    #weapon-box .muted { display: none; }
    #btn-weapon { width: 100%; min-width: 0; padding: 4px 8px; }
    #weapon-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}

/* Small phones: short labels ("Air", "Big ×2"), no caret, tighter buttons. */
@media (max-width: 400px) {
    .long-label { display: none; }
    .short-label { display: inline; }
    #btn-weapon .caret { display: none; }
    #controls-row { gap: 5px; }
    #btn-move, #btn-air, #btn-recall { min-width: 54px; padding: 8px 6px; }
    #btn-fire { min-width: 58px; padding: 8px 8px; }
    #btn-weapon { padding: 4px 6px; justify-content: center; }
    #weapon-name { flex: 0 1 auto; }
}

/* Menu title stays on one line on phones. */
@media (max-width: 480px) {
    .panel { padding: 18px 14px 12px; }
    .panel h1 { font-size: clamp(30px, 11.5vw, 54px); letter-spacing: 1px; white-space: nowrap; }
}

/* ---- Landscape phones (short screens, e.g. iPhone sideways ~390–430px tall) ----
   These are wider than 640px, so without this they'd get the desktop HUD
   (and, before 00.10.00, the tablet one, which filled the whole screen).
   Two slim rows: all four pills side by side, then the control row.
   Padding respects the notch / home indicator (safe-area insets). */
@media (max-height: 500px) and (orientation: landscape) {
    #topbar {
        padding: max(4px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
        gap: 6px;
    }
    #turn-banner { font-size: 13px; padding: 5px 10px; }
    .chip { min-height: 32px; padding: 5px 10px; font-size: 13px; }
    #bottom {
        padding: 0 max(8px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
        gap: 5px;
    }
    #pills-row { gap: 6px; flex-wrap: nowrap; }
    .pill { min-width: 0; padding: 2px 6px; }
    .pill.wide { flex: 1.6; min-width: 0; padding: 2px 8px; }
    .pill-label { font-size: 10px; }
    .az-long { display: none; }
    .step { width: 44px; height: 34px; flex: none; }
    .pill-value { font-size: 16px; min-width: 2.4em; }
    #az-track { height: 26px; margin-top: 3px; }
    #az-thumb { width: 20px; height: 20px; top: 3px; }
    #wind-badge { flex: none; flex-direction: column; justify-content: center; gap: 0; padding: 2px 6px; font-size: 11px; }
    #wind-badge-host, #wind-badge-host canvas { width: 32px !important; height: 32px !important; }
    #controls-row { gap: 6px; }
    .ctl { min-height: 40px; padding: 6px 12px; font-size: 14px; }
    .ctl.fire { padding: 6px 18px; }
    .ctl.light { min-height: 34px; }
    #weapon-box { min-height: 40px; padding: 3px 4px 3px 10px; }
    #btn-weapon { width: 260px; }
    #move-left { display: none; }
    .tank-label { font-size: 10px; }
    .float-text { font-size: 22px; }
}
