:root {
  --bg: #05080f;
  --panel: rgba(8, 14, 26, 0.92);
  --line: #1c2a44;
  --text: #d7e3f4;
  --dim: #7f93b0;
  --accent: #22d3ee;
  --good: #4ade80;
  --warn: #facc15;
  --bad: #f87171;
  --focus: #fde047;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--mono);
}

#game {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
}

/* ---------- HUD ---------- */
.hud {
  position: fixed;
  inset: 0 0 auto 0;
  padding: 12px 16px;
  pointer-events: none;
  font-size: 14px;
  text-shadow: 0 0 6px rgba(34, 211, 238, 0.35);
}
.hud[hidden] { display: none; }
.hud-row { display: flex; align-items: center; gap: 16px; margin-bottom: 8px; }
.hud-top { justify-content: space-between; }
.hud-center { text-align: center; }
.hud-right { text-align: right; }
.hud-dim { color: var(--dim); font-size: 12px; }
.hud-test { color: var(--warn); font-size: 11px; margin-top: 2px; }
.meter { display: flex; align-items: center; gap: 8px; min-width: 260px; }
.meter-wide { flex: 1; }
.meter-label { color: var(--dim); min-width: 56px; }
.meter-value { min-width: 48px; text-align: right; }
.bar {
  flex: 1;
  height: 10px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
}
.bar-fill { height: 100%; width: 0; }
.bar-uptime { background: var(--good); box-shadow: 0 0 8px var(--good); }
.bar-uptime.low { background: var(--bad); box-shadow: 0 0 8px var(--bad); }
.bar-telemetry { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.loadout { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.loadout li {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border: 1px solid var(--line);
  background: rgba(8, 14, 26, 0.7);
  padding: 2px 8px;
  font-size: 12px;
}
.loadout-icon { display: inline-flex; }
.loadout-icon .icon { display: block; }

/* ---------- Banner ---------- */
.notices {
  position: fixed;
  top: max(22%, calc(var(--hud-bottom, 0px) + 10px));
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100vw - 32px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
.banner {
  padding: 8px 18px;
  border: 1px solid var(--accent);
  background: var(--panel);
  color: var(--accent);
  font-size: 18px;
  pointer-events: none;
  text-align: center;
  max-width: calc(100vw - 32px);
}
.banner.threat { border-color: var(--bad); color: var(--bad); }
.banner[hidden] { display: none; }

/* ---------- Overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(2, 4, 9, 0.6);
}
.overlay.open { display: flex; }
.panel {
  width: min(640px, 100%);
  max-height: calc(100vh - 32px);
  overflow: auto;
  border: 1px solid var(--accent);
  background: var(--panel);
  padding: 24px;
  box-shadow: 0 0 24px rgba(34, 211, 238, 0.2);
}
.panel h1, .panel h2 { margin: 0 0 12px; color: var(--accent); font-weight: 600; }
.panel h1 { font-size: 32px; letter-spacing: 1px; }
.panel h2 { font-size: 22px; }
.panel p { color: var(--dim); line-height: 1.5; }
.panel.error { border-color: var(--bad); }
.panel.error h2 { color: var(--bad); }
.prompt::before { content: "$ "; color: var(--good); }

.btn-row { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 16px; }
button {
  font: inherit;
  color: var(--text);
  background: #0b1626;
  border: 1px solid var(--accent);
  padding: 10px 18px;
  cursor: pointer;
}
button:hover { background: #10223a; }
button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.choices { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.choice {
  width: 100%;
  text-align: left;
  display: grid;
  gap: 4px;
  padding: 12px 14px;
}
.choice .key { color: var(--warn); }
.choice .name { color: var(--accent); font-weight: 600; }
.choice .rank { color: var(--good); }
.choice .desc { color: var(--dim); font-size: 13px; }
.choice .changes { font-size: 13px; }

.stats { display: grid; grid-template-columns: auto 1fr; gap: 4px 16px; margin: 12px 0; }
.stats dt { color: var(--dim); }
.stats dd { margin: 0; }
.errors { color: var(--bad); font-size: 13px; padding-left: 18px; }
.controls { color: var(--dim); font-size: 13px; padding-left: 18px; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

@media (max-width: 640px) {
  .meter { min-width: 0; }
  .hud { font-size: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* Sound (feature 002) */
.hud-sound { color: var(--dim); font-size: 12px; margin-top: 2px; }
.hud-sound.off { color: var(--warn); }
.sound-controls { display: grid; grid-template-columns: auto 1fr auto; gap: 10px 12px; align-items: center; margin-top: 16px; }
.sound-controls label { color: var(--dim); font-size: 14px; }
.sound-controls output { min-width: 3.5em; text-align: right; font-variant-numeric: tabular-nums; }
.sound-controls input[type=range] { width: 100%; accent-color: var(--accent); }
.sound-controls input[type=range]:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------- Touch and mobile (feature 003) ---------- */
#game { touch-action: none; }

/* Page lock: touch mode only, so desktop keeps every default (003 FR-013, FR-020). */
body.touch {
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
html:has(body.touch) { overscroll-behavior: none; }
body.touch .overlay { touch-action: manipulation; }
body.touch .overlay input[type=range] { touch-action: none; }

/* Fill the visible viewport on phones (dynamic toolbars) and keep clear of notches. */
#game { width: 100dvw; height: 100dvh; }
.overlay { height: 100dvh; padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
  max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
.panel { max-height: calc(100dvh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
.hud {
  padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
}

/* On-screen Pause / Sound buttons: touch mode, during a run only (HUD is shown only then). */
.touch-buttons { display: none; }
body.touch .touch-buttons {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 4px;
}
.touch-btn {
  pointer-events: auto; /* the HUD itself stays pass-through so touches on its text steer */
  min-width: 48px;
  min-height: 48px;
  padding: 0;
  font-size: 20px;
  line-height: 1;
  border-radius: 10px;
  background: rgba(11, 22, 38, 0.8);
}
.touch-btn[aria-pressed="false"] { color: var(--warn); border-color: var(--warn); }

/* Floating joystick (visual only). */
.joystick {
  position: fixed;
  left: 0;
  top: 0;
  width: 112px;
  height: 112px;
  margin: -56px 0 0 -56px;
  border: 2px solid rgba(34, 211, 238, 0.45);
  border-radius: 50%;
  background: rgba(34, 211, 238, 0.06);
  pointer-events: none;
  display: none;
}
.joystick.active { display: block; }
.joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 44px;
  height: 44px;
  margin: -22px 0 0 -22px;
  border-radius: 50%;
  background: rgba(34, 211, 238, 0.35);
  border: 1px solid var(--accent);
}

/* 44 px touch targets on screens (003 FR-010). */
body.touch .overlay button { min-height: 44px; }
body.touch .sound-controls input[type=range] { min-height: 44px; }

/* Compact HUD on narrow screens (003 FR-011). */
@media (max-width: 600px) {
  .hud { font-size: 12px; }
  .hud-row { gap: 8px; }
  .meter-label { font-size: 11px; min-width: 40px; }
  .meter { min-width: 0; flex: 1 1 120px; }
  .meter-value { min-width: 36px; }
  /* Uptime meter gets its own full-width line; difficulty and time share the next one. */
  .hud-top { align-items: flex-start; flex-wrap: wrap; row-gap: 4px; }
  .hud-top > .meter { flex: 1 1 100%; }
  .hud-center { text-align: left; }
  .hud-center, .hud-right { flex: 0 0 auto; }
  .hud-test { max-width: 40vw; }
  .panel { padding: 16px; }
  .panel h1 { font-size: 24px; }
  .panel h2 { font-size: 18px; }
  .choice .desc { font-size: 12px; }
}
@media (max-height: 500px) {
  .panel { padding: 12px 16px; }
  .panel h1 { font-size: 22px; margin-bottom: 6px; }
  .panel h2 { font-size: 17px; margin-bottom: 6px; }
  .panel p { margin: 6px 0; }
  .choices { gap: 6px; }
  .btn-row { margin-top: 10px; }
  .stats { margin: 6px 0; }
}

/* Below the supported minimum (360 px wide, portrait): a hint, the game still runs. */
.rotate-hint { display: none; }
@media (max-width: 359px) and (orientation: portrait) {
  .rotate-hint {
    display: block;
    position: fixed;
    left: 0;
    right: 0;
    bottom: max(8px, env(safe-area-inset-bottom));
    text-align: center;
    color: var(--warn);
    font-size: 11px;
    pointer-events: none;
  }
}

/* Tap guard: while a guarded screen cannot accept a touch yet, its panel ignores pointers (003 FR-008). */
.overlay.guard-wait .panel { pointer-events: none; }

/* Level-up on small screens (003 FR-011): fit without scrolling. */
@media (max-width: 600px) {
  .choice { padding: 8px 10px; gap: 2px; }
  .choice .desc, .choice .changes { font-size: 11px; line-height: 1.3; }
}
@media (max-height: 500px) and (orientation: landscape) {
  .panel:has(.choices) { width: min(960px, 100%); }
  .choices { grid-template-columns: repeat(3, 1fr); }
  .choice { padding: 8px 10px; gap: 2px; align-content: start; height: 100%; }
  .choice .desc, .choice .changes { font-size: 11px; line-height: 1.3; }
  .panel:has(.choices) p { display: none; }
}

/* Major Incidents (feature 004) */
.hud-incidents { display: grid; gap: 4px; margin: 2px 0 6px; max-width: min(520px, 100%); }
.hud-incidents[hidden], .hud-link-warning[hidden] { display: none; }
.incident-row { display: flex; align-items: center; gap: 8px; }
.incident-name { color: var(--bad); font-weight: 600; white-space: nowrap; font-size: 13px; }
.incident-bar { flex: 1; height: 8px; border-color: var(--bad); }
.hud-link-warning { color: var(--warn); font-size: 13px; margin-bottom: 4px; }
.loadout li.offline { color: var(--bad); border-color: var(--bad); opacity: 0.75; }

/* Compact loadout on phones (portrait or landscape): icon + rank digit, so every chip fits on
   screen instead of being clipped. Names stay in each chip's aria-label and in the Field Guide. */
@media (max-width: 600px), (max-height: 500px) {
  .loadout { gap: 4px; }
  .loadout li { padding: 1px 5px; font-size: 11px; gap: 3px; }
  .loadout-name, .loadout-rank, .loadout-offline { display: none; }
  .loadout li::after { content: attr(data-rank); font-variant-numeric: tabular-nums; }
}
@media (max-width: 600px) {
  .incident-name { font-size: 11px; }
}

/* Safe-area insets exposed to scripts (edge pointers, feature 004). */
:root {
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
}

/* End screen on small phones: the Incidents row (feature 004) must still fit without scrolling. */
@media (max-width: 420px), (max-height: 700px) {
  .stats { font-size: 12px; gap: 2px 10px; margin: 6px 0; line-height: 1.35; }
  .panel:has(.stats) h2 { font-size: 17px; margin-bottom: 6px; }
  .panel:has(.stats) .btn-row { margin-top: 10px; }
}

/* ---------- Field Guide (feature 005, contracts/ui-guide.md) ---------- */
.panel.guide {
  width: min(720px, 100%);
  height: calc(100dvh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 16px 20px;
}
.guide-header { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; flex: none; }
.guide-header h2 { margin: 0; flex: 1; }
.guide-tabs { display: flex; gap: 6px; width: 100%; }
.guide-tabs [role=tab] { flex: 1; padding: 8px 10px; border-color: #1e3a5f; color: var(--dim); }
.guide-tabs [role=tab][aria-selected=true] { border-color: var(--accent); color: var(--accent); background: #10223a; }
.guide-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  margin-top: 12px;
  padding-right: 4px;
}
body.touch .overlay .guide-list { touch-action: pan-y; }
.guide-list h3 { margin: 14px 0 8px; font-size: 14px; letter-spacing: 1px; text-transform: uppercase; color: var(--warn); }
.guide-list h3:first-child { margin-top: 0; }
.guide-entry {
  display: grid;
  grid-template-columns: 40px 1fr;
  gap: 12px;
  padding: 10px;
  margin-bottom: 8px;
  border: 1px solid #13233c;
  background: #07101d;
}
.guide-entry:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; }
.guide-icon .icon { display: block; }
.guide-text { min-width: 0; overflow-wrap: anywhere; }
.guide-name { margin: 0 0 2px; font-size: 16px; color: var(--accent); }
.guide-text p { margin: 2px 0 0; line-height: 1.4; }
.guide-tag { color: var(--good); font-size: 13px; }
.guide-tag.offline { color: var(--bad); font-weight: 600; }
.guide-concept { color: var(--warn); font-size: 13px; }
.guide-desc { color: var(--text); font-size: 14px; }
.guide-stats, .guide-next { font-size: 13px; color: var(--dim); }
.guide-next { color: var(--good); }
.guide-entry.locked .guide-name { color: var(--dim); }
.guide-locked { color: var(--dim); font-style: italic; font-size: 13px; }
.guide-entry.offline { border-color: var(--bad); }
.guide * { transition: none; animation: none; }
body.touch .guide-tabs [role=tab], body.touch #guide-back { min-height: 44px; min-width: 44px; }
@media (max-width: 600px) {
  .panel.guide { padding: 12px; }
  .guide-entry { grid-template-columns: 32px 1fr; gap: 10px; padding: 8px; }
  .guide-icon .icon { width: 32px; height: 32px; }
  .guide-name { font-size: 15px; }
  .guide-desc { font-size: 13px; }
}
/* Landscape phones: header on one row so the list keeps most of the height. */
@media (max-height: 500px) {
  .panel.guide { padding: 8px 14px; }
  .guide-header { flex-wrap: nowrap; }
  .guide-header h2 { font-size: 17px; flex: none; }
  .guide-tabs { width: auto; flex: 1; }
  .guide-list { margin-top: 8px; }
  /* Pause screen with three buttons and the sound controls. */
  .panel-pause { padding: 12px 16px; }
  .panel-pause h2 { font-size: 18px; margin-bottom: 4px; }
  .panel-pause p { margin: 4px 0; }
  .panel-pause .btn-row { margin-top: 8px; gap: 8px; }
}

/* Late-game upgrades (feature 006): tool-floor note on the level-up screen. */
.tool-floor { color: var(--warn) !important; margin: 0 0 6px; }

/* Major Incident alerts (feature 006): one line per active incident, pulsing red until resolved;
   green "RESOLVED" for 3 s afterwards. Below the regular banner spot. */
.incident-alerts {
  display: grid;
  gap: 6px;
  justify-items: center;
  pointer-events: none;
  text-align: center;
}
.incident-alerts:empty { display: none; }
.incident-alert {
  padding: 6px 14px;
  background: var(--panel);
  border: 1px solid currentColor;
  font-size: 16px;
  font-weight: 600;
}
.incident-alert.active { color: var(--bad); animation: incident-pulse 1.2s ease-in-out infinite; }
.incident-alert.resolved { color: var(--good); }
@keyframes incident-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }
@media (prefers-reduced-motion: reduce) { .incident-alert.active { animation: none; } }
@media (max-width: 600px), (max-height: 500px) {
  .incident-alert { font-size: 12px; padding: 4px 10px; }
  .incident-alerts { gap: 4px; }
  .notices { gap: 6px; }
}
