:root {
  --bg: #050a14;
  --panel: #0c1628;
  --border: #1e3354;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --warn: #f59e0b;
  --danger: #ef4444;
  --ok: #22c55e;
}

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

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

.table-layout {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100dvh;
  padding: 8px; /* Safe buffer from Android bezel edge gestures */
  gap: 6px;
}

.rail-top,
.rail-bottom {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  height: 104px;
  flex-shrink: 0;
}

.middle-row {
  display: flex;
  flex: 1;
  min-height: 0;
  gap: 6px;
}

.rail-side {
  width: 108px;
  flex-shrink: 0;
}

.player-slot {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(12, 22, 40, 0.85);
  border: 2px solid var(--border);
  border-radius: 16px;
  padding: 6px 10px;
  position: relative;
  transition: border-color 0.15s ease, opacity 0.2s ease;
}

.player-slot.inactive {
  opacity: 0.2;
  border-style: dashed;
}

.player-slot.slot-P1 { --pcolor: #38bdf8; }
.player-slot.slot-P2 { --pcolor: #a78bfa; }
.player-slot.slot-P3 { --pcolor: #fb7185; }
.player-slot.slot-P4 { --pcolor: #f59e0b; }
.player-slot.slot-P5 { --pcolor: #22c55e; }
.player-slot.slot-P6 { --pcolor: #2dd4bf; }

.player-slot.has-active {
  border-color: var(--pcolor, var(--accent));
  box-shadow: 0 0 18px rgba(56, 189, 248, 0.2);
}

/* Rotate labels/buttons toward each side of the table */
.rot-180 {
  transform: rotate(180deg);
}

.rail-side.player-slot {
  flex-direction: column;
  padding: 10px 6px;
}

.rot-90 .slot-inner {
  transform: rotate(90deg);
}

.rot-neg-90 .slot-inner {
  transform: rotate(-90deg);
}

.slot-badge {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--pcolor, var(--accent));
  text-transform: uppercase;
  white-space: nowrap;
}

.rail-side .slot-badge {
  writing-mode: vertical-rl;
}
.rot-90 .slot-badge {
  transform: rotate(180deg);
}
.rot-neg-90 .slot-badge {
  transform: rotate(0deg);
}

.btn-group {
  display: flex;
  flex: 1;
  width: 100%;
  height: 100%;
  gap: 10px;
  align-items: center;
  justify-content: center;
}

.rail-side .btn-group {
  flex-direction: column;
}

.ctrl-btn {
  flex: 1;
  height: 100%;
  min-width: 84px;
  min-height: 72px;
  max-width: 220px;
  background: linear-gradient(180deg, #162642 0%, #0e192d 100%);
  border: 2px solid #2b4570;
  border-radius: 14px;
  color: #f8fafc;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  touch-action: none;
  cursor: pointer;
}

.rail-side .ctrl-btn {
  width: 100%;
  min-height: 86px;
  max-height: 180px;
}

.ctrl-btn .slot-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  pointer-events: none;
}

.ctrl-icon {
  font-size: 24px;
  line-height: 1;
}

.ctrl-label {
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-align: center;
}

.ctrl-sub {
  font-size: 10px;
  font-weight: 700;
  color: #7dd3fc;
  opacity: 0.92;
  margin-top: 1px;
  font-variant-numeric: tabular-nums;
}

.ctrl-btn.active .ctrl-sub {
  color: #082f49;
}

.ctrl-btn.active {
  background: var(--pcolor, var(--accent));
  color: #04101f;
  border-color: #ffffff;
  transform: scale(0.96);
}

.ctrl-btn.toggle-on {
  border-color: var(--ok);
  box-shadow: inset 0 0 12px rgba(34, 197, 94, 0.35);
}

.ctrl-btn.danger-btn {
  border-color: #92400e;
  background: linear-gradient(180deg, #3b1d0a 0%, #221005 100%);
}

.ctrl-btn.danger-btn.active {
  background: #f59e0b;
  color: #1a0b02;
}

@keyframes stagePulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(250, 204, 21, 0.7); border-color: #fde047; }
  50% { box-shadow: 0 0 18px 4px rgba(250, 204, 21, 0.85); border-color: #ffffff; }
}

.ctrl-btn.danger-btn.stage-ready {
  animation: stagePulse 1s infinite ease-in-out;
}

.hold-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 8px;
  width: 0%;
  background: #fde047;
  pointer-events: none;
}

/* Central Flight Viewport */
.flight-stage {
  flex: 1;
  position: relative;
  border-radius: 16px;
  border: 2px solid var(--border);
  overflow: hidden;
  background: radial-gradient(circle at center, #0b1730 0%, #030712 100%);
}

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

.hud-top {
  position: absolute;
  top: 8px;
  left: 8px;
  right: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.hud-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  background: rgba(6, 11, 22, 0.78);
  border: 1px solid rgba(56, 189, 248, 0.35);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
  color: var(--muted);
}

.chip strong {
  color: #f8fafc;
  font-variant-numeric: tabular-nums;
}

.chip-stage {
  color: #fbbf24;
  font-weight: 700;
}

.chip-touch {
  color: #34d399;
  font-weight: 700;
}

.hud-actions {
  display: flex;
  gap: 6px;
  pointer-events: auto;
}

.hud-btn {
  background: rgba(15, 23, 42, 0.88);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  touch-action: none;
}

.hud-btn.active {
  border-color: var(--accent);
  background: rgba(14, 116, 144, 0.9);
}

.teacher-hold-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 3px;
  width: 0%;
  background: #fde047;
}

.hud-coop-prompt {
  position: absolute;
  top: 68px;
  left: 8px;
  background: rgba(12, 74, 110, 0.88);
  border: 1px solid #38bdf8;
  color: #f0f9ff;
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 800;
  pointer-events: none;
  max-width: 58%;
}

.hud-coop-prompt-top {
  position: absolute;
  top: 40px;
  right: 8px;
  transform: rotate(180deg);
  background: rgba(12, 74, 110, 0.88);
  border: 1px solid #38bdf8;
  color: #f0f9ff;
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 800;
  pointer-events: none;
  max-width: 52%;
}

.hatch-card {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(440px, 88%);
  background: rgba(15, 23, 42, 0.96);
  border: 2px solid #38bdf8;
  border-radius: 18px;
  padding: 16px 18px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.78);
  z-index: 32;
}

.hatch-card.hidden {
  display: none;
}

.hatch-card h2 {
  margin: 0 0 6px;
  font-size: 18px;
  color: #38bdf8;
}

.hatch-card p {
  margin: 0 0 10px;
  font-size: 12px;
  color: #cbd5e1;
}

.hatch-players {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  margin-bottom: 10px;
}

.hatch-chip {
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 800;
  background: #1e293b;
  border: 1px solid #475569;
  color: #94a3b8;
}

.hatch-chip.ok {
  background: #166534;
  border-color: #4ade80;
  color: #dcfce7;
}

.hatch-bar-track {
  height: 12px;
  background: #1e293b;
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid #334155;
  margin-bottom: 10px;
}

.hatch-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #38bdf8, #4ade80);
  transition: width 0.06s linear;
}

.btn-hatch-skip {
  background: transparent;
  border: 1px solid #475569;
  color: #94a3b8;
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 11px;
  cursor: pointer;
}

.victory-science {
  background: rgba(14, 116, 144, 0.2);
  border: 1px solid rgba(56, 189, 248, 0.35);
  border-radius: 10px;
  padding: 7px 10px;
  font-size: 12px;
  color: #bae6fd;
  margin-bottom: 12px;
  line-height: 1.35;
}

.hud-target {
  position: absolute;
  bottom: 8px;
  left: 10px;
  background: rgba(6, 11, 22, 0.82);
  border: 1px solid rgba(56, 189, 248, 0.35);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 12px;
  color: #bae6fd;
  pointer-events: none;
}

.hud-target-switch {
  position: absolute;
  bottom: 8px;
  right: 10px;
  background: rgba(15, 23, 42, 0.88);
  border: 1px solid rgba(56, 189, 248, 0.45);
  color: #e0f2fe;
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.hud-checklist {
  position: absolute;
  top: 40px;
  left: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  pointer-events: none;
}

.hud-checklist span {
  background: rgba(6, 11, 22, 0.72);
  border: 1px solid rgba(148, 163, 184, 0.35);
  color: #94a3b8;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 700;
}

.hud-checklist span.done {
  background: rgba(22, 101, 52, 0.85);
  border-color: #4ade80;
  color: #dcfce7;
}

.mission-banner {
  position: absolute;
  top: 98px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(14, 116, 144, 0.92);
  border: 1px solid #67e8f9;
  color: #ffffff;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.04em;
  pointer-events: none;
  white-space: nowrap;
}

.mission-banner.hidden {
  display: none;
}

.victory-card {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(440px, 88%);
  background: rgba(15, 23, 42, 0.95);
  border: 2px solid #4ade80;
  border-radius: 18px;
  padding: 18px 20px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.75);
  z-index: 30;
}

.victory-card.hidden {
  display: none;
}

.victory-stars {
  font-size: 32px;
  letter-spacing: 4px;
  margin-bottom: 4px;
}

.victory-card h2 {
  margin: 0 0 6px;
  font-size: 20px;
  color: #4ade80;
}

.victory-card p {
  margin: 0 0 10px;
  font-size: 13px;
  color: #cbd5e1;
  line-height: 1.4;
}

.victory-stats {
  background: rgba(6, 11, 22, 0.75);
  border: 1px solid #334155;
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12px;
  color: #e2e8f0;
  margin-bottom: 12px;
}

.victory-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.best-record-label {
  margin-top: 4px;
  font-weight: 700;
  color: #fde047;
}

.touch-debug-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 40;
}

.touch-dot {
  position: fixed;
  width: 54px;
  height: 54px;
  margin-left: -27px;
  margin-top: -27px;
  border-radius: 50%;
  border: 3px solid #34d399;
  background: rgba(52, 211, 153, 0.22);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 800;
  color: #ecfdf5;
}

/* Teacher Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 15, 0.84);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  padding: 16px;
}

.modal-backdrop.hidden {
  display: none;
}

.modal-card {
  width: min(560px, 100%);
  background: #0f172a;
  border: 2px solid #334155;
  border-radius: 20px;
  padding: 20px 22px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
}

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

.modal-header h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0.05em;
  color: #38bdf8;
}

.badge-offline {
  font-size: 11px;
  font-weight: 800;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(34, 197, 94, 0.18);
  color: #4ade80;
  border: 1px solid rgba(34, 197, 94, 0.4);
}

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

.setting-group {
  margin-bottom: 12px;
}

.setting-group label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: #cbd5e1;
  margin-bottom: 6px;
}

.segmented {
  display: flex;
  gap: 8px;
}

.segmented button {
  flex: 1;
  padding: 9px 10px;
  border-radius: 10px;
  border: 1px solid #334155;
  background: #1e293b;
  color: #cbd5e1;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
}

.segmented button.selected {
  background: #0284c7;
  border-color: #38bdf8;
  color: #ffffff;
}

.setting-toggles {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 12px 0;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #e2e8f0;
}

.teacher-tip {
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.35);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12px;
  color: #fde68a;
  line-height: 1.35;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
}

.btn-primary,
.btn-secondary {
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 800;
  font-size: 14px;
  cursor: pointer;
  border: none;
}

.btn-primary {
  background: #22c55e;
  color: #052e16;
}

.btn-secondary {
  background: #334155;
  color: #f1f5f9;
}
