* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  background-color: #020617;
  color: #00f0ff;
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

#canvas-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

#atmosphere-glow {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
  background: radial-gradient(ellipse at 50% 55%, rgba(0, 240, 255, 0.06) 0%, transparent 65%);
  transition: background 0.4s ease;
}

@keyframes scanline-scroll {
  0% { background-position: 0 0, 0 0, 0 0, 0 0; }
  100% { background-position: 0 100px, 100px 0, 0 0, 0 0; }
}

#scanlines {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 3;
  background: 
    linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.3) 50%),
    linear-gradient(90deg, rgba(255, 0, 0, 0.02), rgba(0, 255, 0, 0.01), rgba(0, 0, 255, 0.02)),
    repeating-linear-gradient(transparent, transparent 38px, rgba(0, 240, 255, 0.03) 38px, rgba(0, 240, 255, 0.03) 39px),
    repeating-linear-gradient(90deg, transparent, transparent 38px, rgba(0, 240, 255, 0.03) 38px, rgba(0, 240, 255, 0.03) 39px);
  background-size: 100% 4px, 6px 100%, 39px 39px, 39px 39px;
  animation: scanline-scroll 6s linear infinite;
  opacity: 0.85;
}

/* 核心修復：適配 iOS Safe Area Insets (避開瀏海/動態島與底部小白條) */
#ui-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding-top: max(10px, env(safe-area-inset-top));
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
}

/* ========================================================
   頂部微型常駐列與折疊抽屜
======================================================== */
#top-bar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  max-width: 1240px;
  margin: 0 auto;
  width: 100%;
}

#top-header-row {
  display: flex;
  gap: 6px;
  width: 100%;
  align-items: center;
  flex-wrap: nowrap;
}

#integrity-container {
  flex: 1.4;
  min-width: 120px;
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid rgba(0, 240, 255, 0.4);
  box-shadow: inset 0 0 25px rgba(0, 240, 255, 0.05), 0 0 15px rgba(0, 240, 255, 0.1);
  backdrop-filter: blur(12px);
  border-radius: 6px;
  padding: 4px 8px;
  pointer-events: auto;
  overflow: hidden;
}

#career-badge {
  flex: 1.1;
  min-width: 110px;
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid rgba(0, 240, 255, 0.4);
  box-shadow: inset 0 0 15px rgba(0, 240, 255, 0.06);
  border-radius: 6px;
  padding: 4px 8px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  pointer-events: auto;
  white-space: nowrap;
  overflow: hidden;
}

.career-title {
  font-size: 10px;
  font-weight: 800;
  color: #4ade80;
  letter-spacing: 0.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.career-stat {
  font-size: 8.5px;
  font-family: monospace;
  color: #94a3b8;
}

/* 頂部功能抽屜 */
#top-menu-drawer {
  display: flex;
  gap: 6px;
  width: 100%;
  justify-content: center;
  align-items: center;
  background: rgba(15, 23, 42, 0.96);
  border: 1px solid rgba(0, 240, 255, 0.35);
  box-shadow: inset 0 0 15px rgba(0, 240, 255, 0.05), 0 4px 15px rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(14px);
  padding: 6px 10px;
  border-radius: 8px;
  pointer-events: auto;
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

#top-menu-drawer.top-drawer-closed {
  display: none !important;
}

.mode-select-wrap {
  position: relative;
  pointer-events: auto;
}

.select-mode-dropdown {
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid #00f0ff;
  box-shadow: inset 0 0 12px rgba(0, 240, 255, 0.15);
  color: #00f0ff;
  padding: 5px 8px;
  border-radius: 6px;
  font-size: 10px;
  font-weight: bold;
  letter-spacing: 0.5px;
  outline: none;
  cursor: pointer;
}

.select-mode-dropdown option {
  background: #0f172a;
  color: #fff;
  font-weight: bold;
}

.btn-top-util {
  background: rgba(15, 23, 42, 0.88);
  border: 1px solid rgba(0, 240, 255, 0.5);
  color: #00f0ff;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 10px;
  font-weight: bold;
  cursor: pointer;
  pointer-events: auto;
  white-space: nowrap;
  min-height: 30px; /* 保證觸控高度 */
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-top-util:hover {
  background: #00f0ff;
  color: #020617;
}

.bar-header {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  font-size: 9.5px;
  font-weight: bold;
  color: #94a3b8;
  margin-bottom: 2px;
  white-space: nowrap;
}

.bar-bg {
  position: relative;
  width: 100%;
  height: 4px;
  background: #090d16;
  border-radius: 2px;
  overflow: hidden;
  border: 1px solid #1e293b;
}

#integrity-max-bar {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  background: #1e293b;
  width: 100%;
}

#integrity-bar {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  background: linear-gradient(90deg, #38bdf8, #00f0ff);
  transition: width 0.08s linear;
  box-shadow: 0 0 8px #00f0ff;
}

#alert-msg {
  font-size: 11px;
  font-weight: bold;
  color: #ef4444;
  min-height: 12px;
  text-shadow: 0 0 15px rgba(239, 68, 68, 0.9);
  text-align: center;
}

/* ========================================================
   左右抽屜式折疊 HUD
======================================================== */
.drawer-tab {
  position: absolute;
  top: 6px;
  background: rgba(15, 23, 42, 0.96);
  border: 1px solid #38bdf8;
  color: #38bdf8;
  font-size: 9.5px;
  font-weight: bold;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  pointer-events: auto;
  box-shadow: 0 0 10px rgba(56, 189, 248, 0.3);
  z-index: 30;
  white-space: nowrap;
}

#flux-monitor {
  position: absolute;
  top: calc(max(10px, env(safe-area-inset-top)) + 45px);
  left: 10px;
  background: rgba(15, 23, 42, 0.94);
  border: 1px solid rgba(0, 240, 255, 0.45);
  box-shadow: inset 0 0 25px rgba(0, 240, 255, 0.08), 0 0 20px rgba(0, 240, 255, 0.15);
  backdrop-filter: blur(14px);
  border-radius: 8px;
  padding: 6px;
  pointer-events: auto;
  z-index: 25;
  transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.left-tab {
  right: -56px;
  border-left: none;
}

#flux-monitor.drawer-closed {
  transform: translateX(calc(-100% - 10px));
}

#flux-monitor.drawer-open {
  transform: translateX(0);
}

.monitor-title {
  font-size: 8.5px;
  font-weight: 800;
  color: #38bdf8;
  margin-bottom: 2px;
  text-align: center;
}

#fluxCanvas {
  background: #020617;
  border-radius: 4px;
  border: 1px solid #334155;
  display: block;
}

.monitor-foot {
  font-size: 8.5px;
  font-family: 'Courier New', monospace;
  font-weight: 700;
  color: #38bdf8;
  background: rgba(2, 6, 23, 0.7);
  padding: 2px 4px;
  border-radius: 3px;
  border: 1px solid #1e293b;
  margin-top: 2px;
  width: 100%;
  text-align: center;
}

#mission-hud {
  position: absolute;
  top: calc(max(10px, env(safe-area-inset-top)) + 45px);
  right: 10px;
  width: 210px;
  background: rgba(15, 23, 42, 0.94);
  border: 1px solid rgba(0, 240, 255, 0.45);
  box-shadow: inset 0 0 20px rgba(0, 240, 255, 0.06), 0 0 15px rgba(0, 240, 255, 0.15);
  backdrop-filter: blur(14px);
  border-radius: 8px;
  padding: 6px 10px;
  pointer-events: auto;
  z-index: 25;
  transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.right-tab {
  left: -54px;
  border-right: none;
}

#mission-hud.drawer-closed {
  transform: translateX(calc(100% + 10px));
}

#mission-hud.drawer-open {
  transform: translateX(0);
}

.mission-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2px;
}

.mission-tag {
  font-size: 8px;
  font-weight: 800;
  color: #38bdf8;
}

#mission-timer {
  font-size: 9.5px;
  font-family: monospace;
  font-weight: bold;
  color: #f59e0b;
}

.mission-name {
  font-size: 9.5px;
  font-weight: 800;
  color: #ffffff;
  margin-bottom: 2px;
  line-height: 1.2;
}

.mission-desc {
  font-size: 8px;
  color: #94a3b8;
  margin-bottom: 3px;
  line-height: 1.2;
}

.mission-progress-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

.mission-progress-bar-bg {
  flex: 1;
  height: 3px;
  background: #090d16;
  border-radius: 2px;
  overflow: hidden;
  border: 1px solid #1e293b;
}

#mission-progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #38bdf8, #4ade80);
  box-shadow: 0 0 6px #4ade80;
  transition: width 0.1s linear;
}

#mission-pct-label {
  font-size: 8px;
  font-family: monospace;
  font-weight: bold;
  color: #4ade80;
  min-width: 20px;
}

/* ========================================================
   底部超薄微縮 HUD (Micro HUD)
======================================================== */
#bottom-dock {
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-width: 1080px;
  margin: 0 auto;
  width: 100%;
  pointer-events: auto;
}

#hud {
  display: grid;
  grid-template-columns: 1.1fr 1.1fr 1.1fr 0.9fr;
  gap: 4px;
  width: 100%;
}

.hud-card {
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid rgba(0, 240, 255, 0.3);
  backdrop-filter: blur(12px);
  border-radius: 5px;
  padding: 2px 5px;
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.hud-card-core {
  border-color: rgba(0, 240, 255, 0.45);
}

.hud-title {
  font-size: 7px;
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 0px;
  white-space: nowrap;
}

.hud-val-large {
  font-size: 15px;
  font-weight: 800;
  font-family: 'Courier New', monospace;
  color: #38bdf8;
  text-shadow: 0 0 8px rgba(56, 189, 248, 0.5);
  line-height: 1.1;
}

.hud-val-mid {
  font-size: 10.5px;
  font-weight: 700;
  font-family: 'Courier New', monospace;
  color: #38bdf8;
  white-space: nowrap;
  line-height: 1.1;
}

.hud-val-sub {
  font-size: 10.5px;
  font-weight: 700;
  font-family: 'Courier New', monospace;
  color: #cbd5e1;
  white-space: nowrap;
  line-height: 1.1;
}

.hud-unit {
  font-size: 7.5px;
  color: #94a3b8;
}

.temp-e { color: #f43f5e; }
.temp-i { color: #00f0ff; }
.temp-slash { color: #475569; margin: 0 1px; }

.hud-ignition {
  border-color: #4ade80 !important;
}
.hud-ignition #val-q {
  color: #4ade80 !important;
  text-shadow: 0 0 12px #4ade80 !important;
}

#dock-toggle-bar {
  display: flex;
  justify-content: center;
}

.btn-dock-collapse {
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid rgba(0, 240, 255, 0.35);
  color: #38bdf8;
  padding: 3px 12px;
  border-radius: 4px;
  font-size: 8.5px;
  font-weight: bold;
  cursor: pointer;
  min-width: unset;
}

#controls-panel {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 3px 6px;
  width: 100%;
  background: rgba(15, 23, 42, 0.94);
  border: 1px solid rgba(0, 240, 255, 0.35);
  backdrop-filter: blur(12px);
  padding: 4px 6px;
  border-radius: 6px;
  transition: all 0.25s ease;
}

#controls-panel.collapsed {
  display: none !important;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

label {
  font-size: 8px;
  color: #cbd5e1;
  display: flex;
  justify-content: space-between;
  font-weight: 600;
}

input[type=range] {
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  cursor: pointer;
  height: 4px;
  background: linear-gradient(90deg, #1e293b, rgba(0, 240, 255, 0.6));
  border-radius: 2px;
  outline: none;
}

input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #00f0ff;
  border: 2px solid #ffffff;
  box-shadow: 0 0 6px #00f0ff;
}

.btn-row {
  grid-column: 1 / -1;
  display: flex;
  gap: 4px;
  margin-top: 1px;
  overflow-x: auto;
}

button {
  flex: 1;
  min-width: 80px;
  padding: 5px 6px;
  font-weight: 700;
  font-size: 9px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  color: #fff;
  white-space: nowrap;
}

.btn-power { background: linear-gradient(135deg, #10b981, #047857); }
.btn-fuel { background: linear-gradient(135deg, #0284c7, #0369a1); }
.btn-divertor { background: linear-gradient(135deg, #475569, #334155); }
.btn-stl { background: linear-gradient(135deg, #8b5cf6, #6d28d9); }

#repair-hud {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transform: translate(-50%, -50%);
}

.hidden { display: none !important; }

.progress-ring__circle {
  transition: stroke-dashoffset 0.05s linear;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
  stroke-dasharray: 226;
  stroke-dashoffset: 226;
  filter: drop-shadow(0 0 8px #00f0ff);
}

#repair-label {
  font-size: 10px;
  font-weight: 800;
  color: #00f0ff;
  margin-top: -54px;
}

#achievement-toast {
  position: fixed;
  top: calc(max(15px, env(safe-area-inset-top)));
  right: 15px;
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid #f59e0b;
  border-radius: 6px;
  padding: 6px 10px;
  z-index: 300;
  pointer-events: none;
}
.achieve-show { opacity: 1; transform: translateY(0); }
.achieve-hide { opacity: 0; transform: translateY(-20px); }
.achieve-title { font-size: 9.5px; font-weight: 800; color: #fbbf24; }

#tutorial-guide {
  position: fixed;
  bottom: 140px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 250;
  pointer-events: auto;
}

.guide-card {
  background: rgba(15, 23, 42, 0.98);
  border: 1.5px solid #38bdf8;
  border-radius: 8px;
  padding: 8px 10px;
  width: 280px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.guide-header {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: #fff;
}
.guide-header b { color: #38bdf8; }
.guide-header span { font-size: 8.5px; color: #94a3b8; cursor: pointer; }
.guide-desc { font-size: 9px; color: #e2e8f0; line-height: 1.3; }
.btn-guide-action {
  background: linear-gradient(135deg, #0284c7, #0369a1);
  padding: 3px 8px;
  border-radius: 3px;
  font-size: 9px;
  font-weight: bold;
  border: none;
  color: #fff;
  align-self: flex-end;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(2, 6, 23, 0.88);
  backdrop-filter: blur(10px);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  padding: 12px;
}

.modal-card {
  background: rgba(15, 23, 42, 0.96);
  border: 1px solid #ef4444;
  border-radius: 8px;
  width: 100%;
  max-width: 420px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.victory-card { border-color: #4ade80; }
.modal-header { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 800; color: #fff; }
.modal-body { display: flex; flex-direction: column; gap: 4px; }
.report-section { display: flex; flex-direction: column; gap: 2px; }
.report-label { font-size: 8px; color: #94a3b8; }
.report-value { font-size: 10.5px; font-weight: bold; color: #ef4444; }

.report-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  background: rgba(2, 6, 23, 0.6);
  padding: 4px 6px;
  border-radius: 4px;
}
.grid-item { font-size: 8px; color: #94a3b8; }
.grid-item b { font-size: 9.5px; color: #38bdf8; }

#telemetryCanvas { background: #020617; border-radius: 4px; width: 100%; height: auto; }
.advice-box { background: rgba(30, 41, 59, 0.5); border-left: 3px solid #38bdf8; padding: 4px 6px; font-size: 9px; color: #cbd5e1; }

.btn-restart { background: linear-gradient(135deg, #ef4444, #991b1b); padding: 6px; font-weight: bold; border: none; border-radius: 4px; color: #fff; font-size: 9.5px; }
.btn-next-mission { background: linear-gradient(135deg, #4ade80, #15803d); padding: 6px; font-weight: bold; border: none; border-radius: 4px; color: #fff; font-size: 9.5px; }

.cert-card { max-width: 500px; border-color: #38bdf8; }
#certCanvas { width: 100%; height: auto; border-radius: 6px; background: #020617; }
.cert-btn-row { display: flex; gap: 6px; width: 100%; }
.btn-download-cert { flex: 2; background: linear-gradient(135deg, #0284c7, #0369a1); padding: 6px; font-weight: bold; border: none; border-radius: 4px; color: #fff; font-size: 9.5px; }
.btn-close-cert { flex: 1; background: linear-gradient(135deg, #475569, #334155); padding: 6px; font-weight: bold; border: none; border-radius: 4px; color: #fff; font-size: 9.5px; }

/* ========================================================
   📱 手機直向 (Portrait)
======================================================== */
@media screen and (max-width: 768px) and (orientation: portrait) {
  #hud { grid-template-columns: 1fr 1fr; gap: 3px; }
  #controls-panel { grid-template-columns: 1fr 1fr; }
}

/* ========================================================
   📱 手機橫向 (Landscape)
======================================================== */
@media screen and (orientation: landscape) and (max-height: 500px) {
  #top-header-row { height: 24px; }
  #integrity-container, #career-badge { height: 24px; padding: 1px 6px; }
  .btn-top-util { height: 24px; padding: 1px 6px; font-size: 9px; }

  #flux-monitor { top: calc(max(6px, env(safe-area-inset-top)) + 26px); left: 6px; }
  #mission-hud { top: calc(max(6px, env(safe-area-inset-top)) + 26px); right: 6px; width: 175px; }

  #hud {
    grid-template-columns: repeat(4, 1fr);
    gap: 3px;
  }
  .hud-card { padding: 1px 3px; }
  .hud-title { font-size: 6.5px; }
  .hud-val-large { font-size: 13px; }
  .hud-val-mid, .hud-val-sub { font-size: 8.5px; }

  #controls-panel {
    grid-template-columns: repeat(4, 1fr);
    padding: 3px 6px;
    gap: 2px 4px;
  }
  button { padding: 2px 4px; font-size: 8px; min-width: 60px; }
}
