/* ── Reset & Base ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #08080f;
  --surface: #0f1020;
  --surface2: #161828;
  --border: #252540;
  --text: #dde0f0;
  --text-dim: #7a7d9a;
  --accent: #4a9eff;
  --danger: #ff4a4a;
  --success: #4aff9a;
  --header-h: 48px;
  --footer-h: 36px;
  --sidebar-w: 270px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: 'Courier New', Courier, monospace;
  -webkit-font-smoothing: antialiased;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button:disabled { opacity: 0.38; cursor: default; }

.hidden { display: none !important; }

/* ── Modals ───────────────────────────────────────────────── */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-top: calc(16px + var(--safe-top));
  padding-bottom: calc(16px + var(--safe-bottom));
  overflow-y: auto;
}

.modal.active { display: flex; }

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 24px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 0 60px rgba(74,158,255,0.15);
}

.modal-title {
  font-size: clamp(20px, 5vw, 28px);
  font-weight: bold;
  letter-spacing: 4px;
  color: var(--accent);
  text-align: center;
  text-shadow: 0 0 20px rgba(74,158,255,0.5);
  margin-bottom: 6px;
}

.modal-subtitle {
  font-size: 11px;
  color: var(--text-dim);
  text-align: center;
  letter-spacing: 1px;
  margin-bottom: 28px;
}

.setup-section { margin-bottom: 20px; }

.setup-label {
  font-size: 10px;
  letter-spacing: 2px;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.option-group {
  display: flex;
  gap: 8px;
}

.option-btn {
  flex: 1;
  padding: 10px 4px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-dim);
  font-size: 12px;
  letter-spacing: 0.5px;
  text-align: center;
  line-height: 1.4;
  min-height: 48px;
  transition: all 0.15s;
}

.option-btn span { font-size: 9px; color: var(--text-dim); display: block; }

.option-btn.selected {
  background: rgba(74,158,255,0.15);
  border-color: var(--accent);
  color: var(--accent);
}

.option-btn.selected span { color: rgba(74,158,255,0.7); }

.btn-start {
  display: block;
  width: 100%;
  padding: 16px;
  margin-top: 8px;
  background: var(--accent);
  color: #000;
  font-size: 14px;
  font-weight: bold;
  letter-spacing: 3px;
  border-radius: 8px;
  min-height: 52px;
  transition: opacity 0.15s;
}

.btn-start:active { opacity: 0.8; }

.how-to-play {
  margin-top: 16px;
  padding: 12px;
  background: rgba(255,255,255,0.04);
  border-radius: 8px;
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.6;
}

.how-to-play strong { color: var(--text); }

/* ── Game Over Modal ──────────────────────────────────────── */
.gameover-box { text-align: center; }

.gameover-title {
  font-size: clamp(24px, 8vw, 40px);
  font-weight: bold;
  letter-spacing: 4px;
  margin-bottom: 12px;
  color: var(--success);
  text-shadow: 0 0 30px currentColor;
}

.gameover-title.defeat { color: var(--danger); }

.gameover-message {
  font-size: 13px;
  color: var(--text);
  line-height: 1.6;
  margin-bottom: 8px;
}

.gameover-turn {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 24px;
}

/* ── Game Layout ─────────────────────────────────────────── */
#game-ui {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: calc(100vh - var(--safe-top) - var(--safe-bottom));
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  overflow: hidden;
}

/* ── Header ──────────────────────────────────────────────── */
#game-header {
  flex-shrink: 0;
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow: hidden;
}

#header-left {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.game-title-small {
  font-size: 10px;
  letter-spacing: 2px;
  color: var(--accent);
  display: none;
}

.turn-display {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}

#empire-stats {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  overflow: hidden;
  justify-content: center;
}

.stat-chip {
  font-size: 11px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 3px 7px;
  white-space: nowrap;
  color: var(--text);
}

.tech-pct { color: var(--text-dim); font-size: 9px; }

#end-turn-btn {
  flex-shrink: 0;
  padding: 8px 14px;
  background: rgba(74,158,255,0.15);
  border: 1px solid var(--accent);
  border-radius: 6px;
  color: var(--accent);
  font-size: 12px;
  letter-spacing: 1px;
  font-weight: bold;
  min-height: 36px;
  min-width: 80px;
  transition: background 0.15s;
}

#end-turn-btn:not(:disabled):active { background: rgba(74,158,255,0.3); }

/* ── Main area ───────────────────────────────────────────── */
#game-main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: row;
  overflow: hidden;
}

/* ── Canvas Container ────────────────────────────────────── */
#canvas-container {
  flex: 1;
  min-width: 0;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background: #08080f;
}

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

/* ── Floating Send Bar ───────────────────────────────────── */
#send-float {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15,16,32,0.95);
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 10px 12px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 4px 24px rgba(0,0,0,0.6);
  min-width: min(340px, 90vw);
  z-index: 10;
}

#send-float-info {
  font-size: 11px;
  color: var(--text-dim);
  text-align: center;
  margin-bottom: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.float-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;
}

.float-btn {
  padding: 8px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 12px;
  min-height: 40px;
  min-width: 40px;
}

.float-btn:active { background: var(--surface); }

.float-count {
  font-size: 20px;
  font-weight: bold;
  color: var(--accent);
  min-width: 40px;
  text-align: center;
}

.float-launch {
  background: rgba(74,158,255,0.2);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: bold;
  padding: 8px 14px;
}

.float-launch:active { background: rgba(74,158,255,0.4); }

.float-cancel {
  background: rgba(255,74,74,0.15);
  border-color: rgba(255,74,74,0.5);
  color: var(--danger);
}

/* ── Sidebar ─────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
}

#planet-panel {
  padding: 12px;
  flex: 1;
}

.no-selection {
  color: var(--text-dim);
  font-size: 11px;
  line-height: 1.6;
  padding: 16px 0;
  text-align: center;
}

.planet-title {
  font-size: 13px;
  font-weight: bold;
  color: var(--text);
  letter-spacing: 0.5px;
  margin-bottom: 2px;
  word-break: break-word;
}

.planet-owner {
  font-size: 11px;
  letter-spacing: 1px;
  margin-bottom: 12px;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 12px;
}

.stat-item {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
}

.stat-label {
  font-size: 9px;
  color: var(--text-dim);
  letter-spacing: 1px;
  margin-bottom: 3px;
}

.stat-num {
  font-size: 16px;
  font-weight: bold;
  color: var(--text);
}

.action-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 10px 12px;
  border-radius: 7px;
  font-size: 12px;
  min-height: 44px;
  transition: background 0.15s;
  letter-spacing: 0.3px;
}

.btn-cost {
  font-size: 10px;
  color: var(--text-dim);
}

.btn-build {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-build:not(:disabled):active { background: rgba(255,255,255,0.08); }

.btn-send {
  background: rgba(74,158,255,0.15);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-weight: bold;
  justify-content: center;
}

.btn-send:not(:disabled):active { background: rgba(74,158,255,0.3); }

/* ── Send Panel (sidebar) ────────────────────────────────── */
.send-panel {
  background: rgba(74,158,255,0.06);
  border: 1px solid rgba(74,158,255,0.3);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 8px;
}

.send-from, .send-dest {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.send-from strong, .send-dest strong { color: var(--text); }

.send-hint {
  font-size: 11px;
  color: var(--text-dim);
  text-align: center;
  padding: 12px 0;
  font-style: italic;
}

.stepper-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 10px 0 6px;
}

.stepper-btn {
  flex: 1;
  padding: 8px 4px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 12px;
  min-height: 40px;
  text-align: center;
}

.stepper-btn:active { background: var(--surface); }

.stepper-val {
  font-size: 22px;
  font-weight: bold;
  color: var(--accent);
  min-width: 44px;
  text-align: center;
}

.stepper-hint {
  font-size: 9px;
  color: var(--text-dim);
  text-align: center;
  margin-bottom: 10px;
}

.btn-confirm {
  background: rgba(74,158,255,0.2);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-weight: bold;
  justify-content: center;
  margin-bottom: 6px;
}

.btn-confirm:not(:disabled):active { background: rgba(74,158,255,0.4); }

.btn-cancel {
  background: rgba(255,74,74,0.1);
  border: 1px solid rgba(255,74,74,0.4);
  color: var(--danger);
  justify-content: center;
  font-size: 11px;
}

.btn-cancel:active { background: rgba(255,74,74,0.2); }

/* ── Footer / Status Bar ─────────────────────────────────── */
#status-bar {
  flex-shrink: 0;
  height: var(--footer-h);
  display: flex;
  align-items: center;
  padding: 0 10px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  gap: 8px;
  overflow: hidden;
}

#status-message {
  flex: 1;
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#log-btn {
  flex-shrink: 0;
  font-size: 9px;
  letter-spacing: 1px;
  color: var(--text-dim);
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface2);
  min-height: 28px;
}

/* ── Log Overlay ─────────────────────────────────────────── */
#log-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  z-index: 50;
  display: flex;
  align-items: flex-end;
  padding-bottom: var(--safe-bottom);
}

#log-content {
  width: 100%;
  max-height: 60dvh;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.log-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  letter-spacing: 1px;
  color: var(--text-dim);
  flex-shrink: 0;
}

.log-header button {
  color: var(--text-dim);
  font-size: 16px;
  padding: 4px 8px;
}

#log-list {
  overflow-y: auto;
  padding: 12px 16px;
  -webkit-overflow-scrolling: touch;
}

.log-entry {
  font-size: 11px;
  color: var(--text-dim);
  padding: 5px 0;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  line-height: 1.5;
}

.log-entry:last-child { border-bottom: none; color: var(--text); }

/* ── Mobile Portrait ─────────────────────────────────────── */
@media (max-width: 680px), (orientation: portrait) {
  .game-title-small { display: block; }

  #empire-stats { justify-content: flex-end; }

  .stat-chip { padding: 3px 5px; font-size: 10px; }

  #game-main {
    flex-direction: column;
  }

  #canvas-container {
    flex: none;
    width: 100%;
    /* Use aspect ratio but cap height */
    aspect-ratio: 16 / 11;
    max-height: 52dvh;
  }

  #sidebar {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    flex: 1;
    min-height: 0;
  }

  #planet-panel { padding: 10px; }

  .stats-grid { gap: 5px; }

  .stat-item { padding: 6px 8px; }

  .stat-num { font-size: 14px; }

  /* Floating send bar positioned slightly higher on mobile for thumb reach */
  #send-float { bottom: 8px; }
}

/* ── Very narrow phones (SE etc.) ────────────────────────── */
@media (max-width: 380px) {
  #header-left { gap: 4px; }
  .game-title-small { display: none; }
  #end-turn-btn { padding: 8px 10px; min-width: 70px; font-size: 11px; }
  .stat-chip { font-size: 9px; }
  .float-btn { min-width: 34px; padding: 8px 6px; font-size: 11px; }
}

/* ── Landscape phones ────────────────────────────────────── */
@media (max-height: 500px) and (orientation: landscape) {
  :root { --header-h: 38px; --footer-h: 30px; }
  .stat-chip { font-size: 9px; }
  #sidebar { width: 220px; }
  #canvas-container { aspect-ratio: auto; }
}

/* ── Desktop ──────────────────────────────────────────────── */
@media (min-width: 681px) and (orientation: landscape) {
  .game-title-small { display: block; }
  #empire-stats { justify-content: center; }
  /* Floating bar: hide on desktop since sidebar is always visible */
  #send-float { display: none !important; }
}
