:root {
  --bg: #0c0f0a;
  --panel: #141a11;
  --panel-2: #1a2216;
  --line: #2c3824;
  --line-2: #3d4d31;
  --text: #d9d2b8;
  --muted: #8d8a72;
  --hud: #b8f07a;
  --amber: #f2b33d;
  --hostile: #e2553b;
  --friendly: #7cc6e6;
  --sand: #c9b27c;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --chrome: 178px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--mono); }
body { overscroll-behavior: none; -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2, h3, p { margin: 0; }
[hidden] { display: none !important; }

#app {
  height: 100dvh;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  background:
    radial-gradient(ellipse at 50% -20%, rgba(184, 240, 122, 0.06), transparent 60%),
    var(--bg);
}

/* ------------------------------------------------------------ top bar */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: linear-gradient(#11160e, #0e120b);
}
.brand { display: flex; flex-direction: column; line-height: 1.1; letter-spacing: 0.14em; }
.brand b { color: var(--amber); font-size: 13px; }
.brand span { color: var(--muted); font-size: 10px; }
.readouts { display: flex; gap: 22px; margin: 0 0 0 auto; }
.readouts div { display: flex; flex-direction: column; align-items: flex-end; }
.readouts dt { font-size: 9px; color: var(--muted); letter-spacing: 0.16em; }
.readouts dd { margin: 0; font-size: 18px; font-weight: 700; color: var(--hud); font-variant-numeric: tabular-nums; }
.topbar-actions { display: flex; gap: 6px; }
.icon-button {
  min-width: 40px; height: 34px; padding: 0 8px;
  background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 3px;
  font-size: 11px; font-weight: 700; letter-spacing: 0.08em; color: var(--hud);
}
.icon-button[aria-pressed="true"] { color: var(--muted); text-decoration: line-through; }

/* ------------------------------------------------------------ cockpit */
.cockpit {
  display: grid;
  grid-template-columns: 236px minmax(0, 1fr) 270px;
  gap: 12px; padding: 12px 16px;
  min-height: 0;
}
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 4px;
  padding: 12px; display: flex; flex-direction: column; gap: 10px; min-height: 0;
}
.panel h2 { font-size: 10px; letter-spacing: 0.2em; color: var(--muted); font-weight: 700; border-bottom: 1px solid var(--line); padding-bottom: 6px; }

.positions { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.positions li { display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; font-size: 12px; }
.positions li b { color: var(--friendly); letter-spacing: 0.12em; }
.positions li span { font-variant-numeric: tabular-nums; }
.positions .meter { grid-column: 1 / -1; }
.positions li.lost b, .positions li.lost span { color: #59605a; text-decoration: line-through; }
.positions li.hit { animation: hit 0.6s; }
@keyframes hit { 0%, 60% { background: rgba(226, 85, 59, 0.25); } 100% { background: transparent; } }

.meter { height: 6px; background: #0a0d08; border: 1px solid var(--line); position: relative; overflow: hidden; }
.meter i { position: absolute; inset: 0 auto 0 0; width: 100%; background: var(--friendly); transition: width 0.25s, background 0.25s; }
.meter i.warn { background: var(--amber); }
.meter i.crit { background: var(--hostile); }

.tally { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 0; }
.tally div { background: var(--panel-2); border: 1px solid var(--line); padding: 6px 8px; }
.tally dt { font-size: 9px; letter-spacing: 0.16em; color: var(--muted); }
.tally dd { margin: 0; font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
#r-breaches, #r-friendly { color: var(--hostile); }
#r-kills { color: var(--hud); }
.legend { font-size: 10px; color: var(--muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-top: auto; }
.sw { display: inline-block; width: 9px; height: 9px; border: 1.5px solid; }
.sw.hostile { border-color: var(--hostile); } .sw.aaa { border-color: var(--amber); } .sw.friendly { border-color: var(--friendly); }

.scope { display: flex; flex-direction: column; align-items: center; gap: 8px; min-width: 0; min-height: 0; }
.scope-frame {
  position: relative;
  width: min(100%, calc((100dvh - var(--chrome)) * 4 / 3));
  aspect-ratio: 4 / 3;
  border: 1px solid var(--line-2); border-radius: 4px; overflow: hidden;
  box-shadow: 0 0 0 4px #0a0d08, 0 0 0 5px var(--line), 0 20px 50px rgba(0, 0, 0, 0.6);
  background: #3b3424;
}
#field { display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
.scanlines {
  position: absolute; inset: 0; pointer-events: none;
  background: repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0.12) 0 1px, transparent 1px 3px);
  mix-blend-mode: multiply;
}
.ticker {
  width: min(100%, calc((100dvh - var(--chrome)) * 4 / 3));
  min-height: 1.6em; font-size: 12px; color: var(--hud); letter-spacing: 0.04em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ticker.alert { color: var(--hostile); }

.airframe { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: center; font-size: 11px; }
.airframe .meter { height: 10px; }
.airframe b { color: var(--hud); font-variant-numeric: tabular-nums; }
.log { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 120px; font-size: 11px; line-height: 1.45; display: flex; flex-direction: column-reverse; gap: 4px; }
.log li { color: var(--text); border-left: 2px solid var(--line-2); padding-left: 6px; }
.log li time { color: var(--muted); margin-right: 6px; }
.log li.hot { border-color: var(--amber); }
.log li.alert { border-color: var(--hostile); color: #f0b3a6; }
.log li.good { border-color: var(--hud); }
.ghost { background: none; border: 1px solid var(--line-2); color: var(--muted); padding: 8px; border-radius: 3px; font-size: 11px; letter-spacing: 0.14em; }
.ghost:hover { color: var(--hostile); border-color: var(--hostile); }

/* ------------------------------------------------------------ weapons */
.weapons {
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px;
  padding: 10px 16px; padding-bottom: max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: #0e120b;
}
.weapon {
  position: relative; overflow: hidden;
  display: grid; grid-template-columns: auto 1fr auto; grid-template-rows: auto auto; gap: 0 10px; align-items: center;
  text-align: left; padding: 10px 12px; min-height: 58px;
  background: var(--panel); border: 1px solid var(--line-2); border-radius: 4px;
}
.weapon kbd { grid-row: 1 / 3; font-size: 11px; color: var(--muted); border: 1px solid var(--line-2); padding: 2px 6px; border-radius: 2px; }
.w-name { font-weight: 700; font-size: 13px; letter-spacing: 0.08em; }
.w-hint { grid-column: 2; font-size: 10px; color: var(--muted); }
.w-ammo { grid-row: 1 / 3; grid-column: 3; font-size: 22px; font-weight: 700; color: var(--hud); font-variant-numeric: tabular-nums; }
.w-cool { position: absolute; left: 0; bottom: 0; height: 3px; width: 0; background: var(--amber); }
.weapon[aria-pressed="true"] { border-color: var(--amber); background: linear-gradient(rgba(242, 179, 61, 0.14), rgba(242, 179, 61, 0.04)); box-shadow: inset 0 0 0 1px var(--amber); }
.weapon[aria-pressed="true"] .w-name { color: var(--amber); }
.weapon.cooling .w-ammo { color: var(--muted); }
.weapon.empty .w-ammo, .weapon.empty .w-name { color: #5c5f52; }

/* ------------------------------------------------------------ overlays */
.overlay {
  position: fixed; inset: 0; z-index: 10;
  display: grid; place-items: center; padding: 16px;
  background: radial-gradient(ellipse at center, rgba(12, 15, 10, 0.72), rgba(8, 10, 6, 0.94));
  backdrop-filter: blur(2px);
  overflow-y: auto;
}
.sheet {
  width: min(760px, 100%);
  background: rgba(20, 26, 17, 0.94); border: 1px solid var(--line-2); border-radius: 6px;
  padding: 28px; display: flex; flex-direction: column; gap: 16px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
}
.sheet.small { width: min(420px, 100%); align-items: center; text-align: center; }
.eyebrow { font-size: 10px; letter-spacing: 0.22em; color: var(--muted); }
h1 { display: flex; flex-direction: column; font-size: clamp(34px, 7vw, 64px); line-height: 0.95; letter-spacing: 0.02em; color: var(--sand); }
h1 span { font-size: 0.42em; letter-spacing: 0.3em; color: var(--amber); margin-bottom: 6px; }
.big { font-size: clamp(30px, 6vw, 48px); letter-spacing: 0.06em; color: var(--amber); }
.brief { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.brief h3 { font-size: 10px; letter-spacing: 0.22em; color: var(--hud); margin-bottom: 6px; }
.brief p { font-size: 13px; line-height: 1.55; color: var(--text); }
.brief b { color: var(--friendly); }
.orders { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.orders li { display: grid; grid-template-columns: auto 110px 1fr; gap: 12px; align-items: baseline; font-size: 12px; padding: 8px 10px; background: var(--panel-2); border-left: 2px solid var(--amber); }
.orders kbd { color: var(--muted); }
.orders b { color: var(--amber); letter-spacing: 0.1em; }
.note { font-size: 12px; color: var(--muted); line-height: 1.5; }
.note em { color: var(--hostile); font-style: normal; font-weight: 700; }
.primary {
  align-self: flex-start;
  background: var(--amber); color: #1b1606; border: 0; border-radius: 3px;
  padding: 14px 26px; font-weight: 800; letter-spacing: 0.16em; font-size: 14px;
  box-shadow: 0 0 0 1px #000, 0 0 24px rgba(242, 179, 61, 0.35);
}
.primary:hover { filter: brightness(1.08); }
.primary:focus-visible, .weapon:focus-visible, .ghost:focus-visible, .icon-button:focus-visible { outline: 2px solid var(--hud); outline-offset: 2px; }
.fine { font-size: 10px; color: var(--muted); line-height: 1.5; }
.row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.summary { font-size: 13px; line-height: 1.6; }
.score { border-collapse: collapse; width: 100%; font-size: 13px; font-variant-numeric: tabular-nums; }
.score th { text-align: left; font-weight: 400; color: var(--muted); padding: 6px 0; border-bottom: 1px solid var(--line); }
.score td { text-align: right; padding: 6px 0; border-bottom: 1px solid var(--line); }
.score td.neg { color: var(--hostile); }
.score tfoot th, .score tfoot td { color: var(--amber); font-weight: 800; font-size: 20px; border: 0; padding-top: 12px; }

#app[data-screen="title"] .cockpit, #app[data-screen="title"] .weapons { filter: saturate(0.6) brightness(0.8); }

/* ------------------------------------------------------------ narrow */
@media (max-width: 1100px) {
  .cockpit { grid-template-columns: 210px minmax(0, 1fr); }
  .aircraft { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; align-items: start; }
  .aircraft .log { min-height: 56px; max-height: 96px; }
  :root { --chrome: 360px; }
}

@media (max-width: 760px) {
  :root { --chrome: 0px; }
  .topbar { padding: 6px 12px; gap: 10px; padding-top: max(6px, env(safe-area-inset-top)); }
  .brand span { display: none; }
  .readouts { gap: 14px; }
  .readouts dd { font-size: 15px; }
  .cockpit { grid-template-columns: 1fr; padding: 8px; gap: 8px; align-content: start; }
  .scope { order: 1; }
  .scope-frame { width: 100%; }
  .ticker {
    width: 100%; font-size: 11px; line-height: 1.4; min-height: 2.8em;
    white-space: normal; display: -webkit-box; -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .intel { order: 2; padding: 8px; gap: 6px; }
  .intel h2, .intel .legend { display: none; }
  .positions { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .positions li { font-size: 10px; }
  .tally { grid-template-columns: repeat(4, 1fr); gap: 4px; }
  .tally div { padding: 4px 6px; }
  .tally dt { font-size: 8px; letter-spacing: 0.08em; }
  .tally dd { font-size: 14px; }
  .aircraft { order: 3; display: flex; padding: 8px; }
  .aircraft h2, .aircraft .log, .aircraft #abort { display: none; }
  .weapons { position: sticky; bottom: 0; gap: 6px; padding: 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .weapon { grid-template-columns: 1fr auto; padding: 8px; min-height: 54px; }
  .weapon kbd, .w-hint { display: none; }
  .w-name { font-size: 10px; letter-spacing: 0.04em; }
  .w-ammo { grid-column: 2; font-size: 17px; }
  .brief { grid-template-columns: 1fr; }
  .orders li { grid-template-columns: 90px 1fr; }
  .orders kbd { display: none; }
  .sheet { padding: 20px; }
  .primary { align-self: stretch; }
}

@media (max-width: 380px) {
  .readouts div:nth-child(2) { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .positions li.hit { animation: none; }
  .meter i { transition: none; }
}
