:root {
  --bg: #071422;
  --surface: rgba(11, 24, 40, 0.88);
  --surface-solid: #0c1a2c;
  --border: rgba(94, 234, 212, 0.22);
  --border-soft: rgba(148, 163, 184, 0.18);
  --text: #e6f4f1;
  --muted: #93b0bd;
  --teal: #2dd4bf;
  --teal-bright: #5eead4;
  --gold: #fde047;
  --amber: #fb923c;
  --sky: #38bdf8;
  --crimson: #fb7185;
  --purple: #c084fc;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  touch-action: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.masthead {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: rgba(7, 20, 34, 0.94);
  border-bottom: 1px solid var(--border-soft);
  z-index: 30;
}

.masthead h1 {
  margin: 0;
  font-size: 16px;
  letter-spacing: 0.08em;
  color: var(--teal-bright);
  white-space: nowrap;
}

.masthead nav {
  display: flex;
  align-items: center;
  gap: 6px;
}

.masthead nav button {
  appearance: none;
  border: 1px solid var(--border);
  background: rgba(15, 34, 54, 0.85);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 999px;
  cursor: pointer;
}

.masthead nav button[aria-pressed="true"] {
  border-color: var(--teal);
  color: var(--teal-bright);
  background: rgba(45, 212, 191, 0.16);
}

.stage-root {
  position: relative;
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  overflow: hidden;
}

#galaxy-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
}

#galaxy-canvas:active {
  cursor: grabbing;
}

/* Floating Top HUD */
.hud-top {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: min(560px, calc(100% - 16px));
  display: flex;
  flex-direction: column;
  gap: 5px;
  pointer-events: none;
  z-index: 15;
}

.hud-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 11px;
  border-radius: 999px;
  background: rgba(8, 20, 34, 0.78);
  border: 1px solid var(--border-soft);
  backdrop-filter: blur(8px);
  font-size: 12px;
}

.hud-mission {
  font-weight: 700;
  color: #f1f5f9;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hud-clock {
  font-variant-numeric: tabular-nums;
  color: var(--teal-bright);
  font-weight: 700;
}

.hud-pop {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 11px;
}

.balance-bar {
  display: flex;
  width: 100%;
  height: 6px;
  border-radius: 999px;
  overflow: hidden;
  background: rgba(15, 23, 42, 0.72);
  border: 1px solid rgba(148, 163, 184, 0.18);
}

.balance-seg {
  height: 100%;
  transition: width 0.25s ease;
}

.hud-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 12px;
  border-radius: 12px;
  background: rgba(15, 37, 56, 0.92);
  border: 1px solid rgba(94, 234, 212, 0.45);
  font-size: 12px;
  color: #ecfeff;
  pointer-events: auto;
}

.mini-btn {
  border: 1px solid rgba(248, 113, 113, 0.5);
  background: rgba(127, 29, 29, 0.5);
  color: #fecaca;
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

/* Edge-clamped Radial Menu */
.radial-menu {
  position: absolute;
  z-index: 22;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  width: min(296px, calc(100vw - 20px));
  padding: 8px;
  border-radius: 18px;
  background: rgba(9, 22, 37, 0.92);
  border: 1px solid rgba(94, 234, 212, 0.36);
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.52);
  backdrop-filter: blur(10px);
}

.rad-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 82px;
  min-height: 48px;
  padding: 6px 8px;
  border-radius: 12px;
  border: 1px solid rgba(94, 234, 212, 0.28);
  background: rgba(17, 42, 62, 0.85);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.15s ease;
}

.rad-btn:active {
  transform: scale(0.96);
}

.rad-btn.primary {
  border-color: rgba(45, 212, 191, 0.65);
  background: rgba(17, 94, 89, 0.45);
}

.rad-btn.gold {
  border-color: rgba(253, 224, 71, 0.65);
  background: rgba(113, 63, 18, 0.45);
}

.rad-btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.rad-ico {
  font-size: 15px;
  line-height: 1.1;
}

.rad-lbl {
  font-size: 11px;
  font-weight: 700;
  margin-top: 2px;
  white-space: nowrap;
}

.rad-cost {
  font-size: 10px;
  color: var(--muted);
}

/* Bottom Thumb-Friendly Dock */
.hud-bottom {
  position: absolute;
  left: 50%;
  bottom: max(10px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(520px, calc(100% - 16px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
  z-index: 16;
}

.planet-strip {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 12px;
  background: rgba(8, 20, 34, 0.86);
  border: 1px solid var(--border-soft);
  backdrop-filter: blur(8px);
  font-size: 11.5px;
  pointer-events: auto;
}

.gene-chips {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
}

.g-e { color: #34d399; }
.g-s { color: #fb923c; }
.g-sp { color: #38bdf8; }

.ratio-dock {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 999px;
  background: rgba(8, 20, 34, 0.86);
  border: 1px solid var(--border);
  backdrop-filter: blur(10px);
  pointer-events: auto;
}

.ratio-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  padding-left: 4px;
}

.ratio-pills {
  display: flex;
  gap: 4px;
}

.ratio-pills button,
.quick-pill {
  appearance: none;
  border: 1px solid rgba(148, 163, 184, 0.24);
  background: rgba(15, 34, 54, 0.82);
  color: var(--text);
  border-radius: 999px;
  min-height: 34px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.ratio-pills button.active,
.quick-pill[aria-pressed="true"] {
  border-color: var(--teal);
  background: rgba(45, 212, 191, 0.24);
  color: #ecfeff;
}

.rotate-banner {
  position: absolute;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  padding: 5px 12px;
  border-radius: 999px;
  background: rgba(120, 53, 15, 0.9);
  border: 1px solid rgba(251, 191, 36, 0.5);
  color: #fef3c7;
  font-size: 11px;
  font-weight: 600;
  z-index: 25;
  pointer-events: none;
}

/* Modal & Lobby Layer */
.overlay-root {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(14px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom));
  background: rgba(4, 11, 20, 0.74);
  backdrop-filter: blur(8px);
  z-index: 40;
}

.overlay-root[hidden] {
  display: none;
}

.overlay-card {
  width: min(720px, 100%);
  max-height: min(86dvh, 760px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  padding: 18px;
  border-radius: 20px;
  background: linear-gradient(165deg, rgba(12, 29, 48, 0.96), rgba(8, 18, 32, 0.98));
  border: 1px solid rgba(94, 234, 212, 0.3);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.modal-header h2 {
  margin: 0;
  font-size: 20px;
  color: var(--teal-bright);
  letter-spacing: 0.04em;
}

.sub-line {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.5;
}

.tab-row {
  display: flex;
  gap: 8px;
}

.tab-btn {
  flex: 1;
  min-height: 40px;
  border-radius: 12px;
  border: 1px solid var(--border-soft);
  background: rgba(15, 34, 54, 0.68);
  color: var(--muted);
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
}

.tab-btn.active {
  border-color: var(--teal);
  background: rgba(45, 212, 191, 0.18);
  color: #ecfeff;
}

.chapter-pills {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.ch-pill {
  flex: 0 0 auto;
  border: 1px solid var(--border-soft);
  background: rgba(15, 34, 54, 0.6);
  color: var(--muted);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.ch-pill.active {
  border-color: var(--teal);
  color: var(--teal-bright);
  background: rgba(45, 212, 191, 0.16);
}

.stage-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(126px, 1fr));
  gap: 8px;
}

.stage-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  padding: 10px 11px;
  border-radius: 14px;
  border: 1px solid var(--border-soft);
  background: rgba(15, 33, 52, 0.72);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.stage-card.selected {
  border-color: var(--teal);
  background: rgba(17, 94, 89, 0.35);
  box-shadow: 0 0 0 1px rgba(45, 212, 191, 0.35);
}

.stage-card.locked {
  opacity: 0.45;
  cursor: not-allowed;
}

.stage-num {
  font-size: 11px;
  color: var(--teal-bright);
  font-weight: 700;
}

.stage-name {
  font-size: 13px;
  font-weight: 700;
}

.stage-stars {
  font-size: 12px;
  color: var(--gold);
  letter-spacing: 0.08em;
}

.stage-preview {
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(9, 21, 35, 0.8);
  border: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.btn {
  flex: 1 1 140px;
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(18, 42, 64, 0.85);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}

.btn.primary {
  border-color: var(--teal);
  background: linear-gradient(135deg, #0d9488, #0f766e);
  color: #f0fdfa;
}

.skirmish-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}

.field-box {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(9, 21, 35, 0.72);
  border: 1px solid var(--border-soft);
}

.field-box label {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
}

.seg-group {
  display: flex;
  gap: 6px;
}

.seg-group button {
  flex: 1;
  min-height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border-soft);
  background: rgba(15, 34, 54, 0.75);
  color: var(--text);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.seg-group button.active {
  border-color: var(--teal);
  background: rgba(45, 212, 191, 0.22);
  color: #ecfeff;
}

.seed-row {
  display: flex;
  gap: 6px;
}

.seed-input {
  flex: 1;
  min-height: 36px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid var(--border-soft);
  background: rgba(7, 17, 29, 0.9);
  color: var(--text);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  -webkit-user-select: text;
  user-select: text;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stat-tile {
  padding: 10px;
  border-radius: 12px;
  background: rgba(9, 21, 35, 0.8);
  border: 1px solid var(--border-soft);
  text-align: center;
}

.stat-tile b {
  display: block;
  font-size: 18px;
  color: var(--teal-bright);
}

.stat-tile span {
  font-size: 11px;
  color: var(--muted);
}

.star-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
}

.star-list .met {
  color: var(--gold);
}

.star-list .miss {
  color: var(--muted);
}

.guide-block {
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(9, 21, 35, 0.72);
  border: 1px solid var(--border-soft);
}

.guide-block h3 {
  margin: 0 0 4px;
  font-size: 14px;
  color: var(--teal-bright);
}

.guide-block p {
  margin: 0;
  font-size: 13px;
  line-height: 1.55;
  color: #cbd5e1;
}

@media (max-width: 600px) {
  .overlay-card {
    padding: 14px;
  }
  .ratio-label {
    display: none;
  }
  .ratio-pills button,
  .quick-pill {
    padding: 0 8px;
    font-size: 11.5px;
  }
}
