:root {
  color-scheme: dark;
  --space: #020306;
  --surface: #0b0e13;
  --surface-raised: #11151c;
  --surface-hover: #1a202a;
  --ink: #f5f7fa;
  --muted: #aeb7c4;
  --line: #2c3440;
  --sun: #f2a13a;
  --focus: #8bc4ff;
  --danger: #ff9b93;
  --radius: 12px;
  --z-canvas: 0;
  --z-hud: 10;
  --z-panel: 20;
  --z-status: 30;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-width: 320px;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--space);
}

body {
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Noto Sans TC", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button,
a {
  font: inherit;
}

button:focus-visible,
a:focus-visible,
#solar-stage:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

button {
  touch-action: manipulation;
}

[hidden] {
  display: none !important;
}

.skip-link {
  position: fixed;
  top: 8px;
  left: 50%;
  z-index: 100;
  padding: 9px 13px;
  border-radius: 8px;
  background: #ffffff;
  color: #111111;
  transform: translate(-50%, -150%);
}

.skip-link:focus {
  transform: translate(-50%, 0);
}

.solar-app,
#solar-stage {
  position: fixed;
  inset: 0;
}

#solar-stage {
  z-index: var(--z-canvas);
  overflow: hidden;
  background: var(--space);
}

#solar-stage > canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

.scene-vignette {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  background:
    radial-gradient(circle at 50% 46%, transparent 0 36%, rgba(1, 2, 5, 0.2) 66%, rgba(1, 2, 5, 0.72) 100%),
    linear-gradient(to bottom, rgba(2, 3, 6, 0.34), transparent 24%, transparent 72%, rgba(2, 3, 6, 0.52));
}

.topbar {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: 14px;
  left: 118px;
  z-index: var(--z-hud);
  display: flex;
  min-height: 56px;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 8px 9px 8px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(11, 14, 19, 0.94);
}

.brand {
  min-width: 0;
}

.brand h1 {
  margin: 0;
  color: var(--ink);
  font-size: 1.02rem;
  font-weight: 780;
  letter-spacing: -0.02em;
  text-wrap: balance;
}

.scene-label {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.72rem;
}

.simulation-date {
  margin: 0 0 0 auto;
  color: #d9dee6;
  font-size: 0.76rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.control-cluster {
  display: flex;
  flex: 0 0 auto;
  gap: 5px;
  align-items: center;
}

.control-cluster,
.planet-dock {
  scrollbar-width: none;
}

.control-cluster::-webkit-scrollbar,
.planet-dock::-webkit-scrollbar {
  display: none;
}

.control-button {
  min-height: 38px;
  padding: 7px 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--surface-raised);
  color: #e7ebf0;
  font-size: 0.76rem;
  font-weight: 680;
  cursor: pointer;
  white-space: nowrap;
}

.control-button:hover {
  border-color: #3c4654;
  background: var(--surface-hover);
}

.control-button[aria-pressed="true"] {
  border-color: #cf8f3a;
  background: #2d2112;
  color: #ffd9a7;
}

.control-button:disabled {
  color: #727c89;
  cursor: wait;
}

.planet-panel {
  position: fixed;
  bottom: 94px;
  left: 16px;
  z-index: var(--z-panel);
  width: min(330px, calc(100vw - 32px));
  padding: 17px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(11, 14, 19, 0.96);
}

.planet-heading-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.planet-heading {
  min-width: 0;
}

.planet-name {
  margin: 0;
  color: #ffffff;
  font-size: 1.55rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.06;
  text-wrap: balance;
}

.planet-type {
  display: inline-block;
  margin-top: 7px;
  color: color-mix(in srgb, var(--planet-color) 65%, #ffffff);
  font-size: 0.74rem;
  font-weight: 720;
}

.panel-toggle {
  display: none;
  min-height: 36px;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-raised);
  color: var(--ink);
  font-size: 0.75rem;
}

.planet-description {
  margin: 13px 0 0;
  color: #c6cdd7;
  font-size: 0.86rem;
  line-height: 1.55;
  text-wrap: pretty;
}

.planet-facts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 11px 15px;
  margin: 16px 0 0;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.planet-facts div {
  min-width: 0;
}

.planet-facts dt {
  color: var(--muted);
  font-size: 0.7rem;
}

.planet-facts dd {
  margin: 3px 0 0;
  color: #ffffff;
  font-size: 0.84rem;
  font-variant-numeric: tabular-nums;
  font-weight: 690;
}

.panel-footer {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-top: 15px;
  padding-top: 13px;
  border-top: 1px solid var(--line);
}

.nasa-link {
  flex: 0 0 auto;
  color: #9dcaff;
  font-size: 0.76rem;
  font-weight: 680;
  text-underline-offset: 3px;
  white-space: nowrap;
}

.scale-note {
  flex: 1 1 0;
  max-width: 172px;
  margin: 0;
  color: #929dab;
  font-size: 0.65rem;
  line-height: 1.45;
  text-align: right;
}

.planet-dock {
  position: fixed;
  right: 14px;
  bottom: max(12px, env(safe-area-inset-bottom));
  left: 14px;
  z-index: var(--z-panel);
  display: flex;
  max-width: 920px;
  min-height: 58px;
  margin: 0 auto;
  gap: 3px;
  align-items: center;
  overflow-x: auto;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(11, 14, 19, 0.96);
}

.planet-tab {
  display: inline-flex;
  flex: 1 0 auto;
  min-width: 86px;
  min-height: 44px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 7px 9px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: #aeb7c4;
  font-size: 0.75rem;
  font-weight: 650;
  cursor: pointer;
}

.planet-tab:hover {
  background: var(--surface-hover);
  color: #ffffff;
}

.planet-tab[aria-pressed="true"] {
  border-color: #485363;
  background: var(--surface-raised);
  color: #ffffff;
}

.planet-swatch {
  width: 10px;
  height: 10px;
  flex: 0 0 10px;
  border: 1px solid rgba(255, 255, 255, 0.45);
  border-radius: 50%;
  background: var(--planet-color);
}

.gesture-status,
.gesture-hint {
  position: fixed;
  z-index: var(--z-status);
  right: 18px;
  padding: 8px 11px;
  border: 1px solid #3b4655;
  border-radius: 8px;
  background: #10151d;
  color: #dfe6ee;
  font-size: 0.72rem;
}

.gesture-status {
  top: calc(max(12px, env(safe-area-inset-top)) + 68px);
}

.gesture-status.error {
  border-color: #7d3d38;
  color: var(--danger);
}

.gesture-hint {
  top: calc(max(12px, env(safe-area-inset-top)) + 108px);
  max-width: 320px;
  color: var(--muted);
}

.controls-hint {
  position: fixed;
  right: 18px;
  bottom: 83px;
  z-index: var(--z-hud);
  max-width: 48ch;
  margin: 0;
  color: #929dac;
  font-size: 0.68rem;
  text-align: right;
}

#gesture-video {
  position: fixed;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.loading-state,
.error-state {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 32px;
  background: var(--space);
  color: #dfe5ec;
  font-size: 0.95rem;
  text-align: center;
}

.loading-copy {
  display: grid;
  gap: 14px;
  justify-items: center;
}

.loading-orbit {
  position: relative;
  width: 52px;
  height: 52px;
  border: 1px solid #536071;
  border-radius: 50%;
}

.loading-orbit::before {
  position: absolute;
  top: 4px;
  left: 50%;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--sun);
  content: "";
  transform: translateX(-50%);
  transform-origin: 50% 22px;
  animation: loading-orbit 1.25s linear infinite;
}

@keyframes loading-orbit {
  to {
    transform: translateX(-50%) rotate(360deg);
  }
}

.error-state {
  color: var(--danger);
}

@media (max-width: 820px) {
  .topbar {
    right: 10px;
    left: 108px;
    display: grid;
    min-height: 92px;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 6px 10px;
    padding: 9px;
  }

  .scene-label {
    display: none;
  }

  .simulation-date {
    grid-column: 2;
    grid-row: 1;
    font-size: 0.67rem;
  }

  .control-cluster {
    grid-column: 1 / -1;
    overflow-x: auto;
  }

  .control-button {
    flex: 1 0 auto;
    min-height: 36px;
  }

  .planet-panel {
    right: 10px;
    bottom: 82px;
    left: 10px;
    width: auto;
    padding: 13px 14px;
  }

  .planet-name {
    font-size: 1.3rem;
  }

  .planet-description {
    display: -webkit-box;
    margin-top: 9px;
    overflow: hidden;
    font-size: 0.79rem;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  .panel-toggle {
    display: inline-block;
  }

  .planet-panel:not(.expanded) .planet-facts,
  .planet-panel:not(.expanded) .panel-footer {
    display: none;
  }

  .planet-panel.expanded {
    max-height: min(56vh, 430px);
    overflow-y: auto;
  }

  .planet-panel.expanded .planet-description {
    display: block;
    overflow: visible;
  }

  .planet-dock {
    right: 8px;
    bottom: max(7px, env(safe-area-inset-bottom));
    left: 8px;
    min-height: 64px;
    justify-content: flex-start;
  }

  .planet-tab {
    min-width: 76px;
    min-height: 48px;
    flex-direction: column;
    gap: 4px;
    padding: 5px 7px;
  }

  .controls-hint {
    display: none;
  }

  .gesture-status {
    top: calc(max(12px, env(safe-area-inset-top)) + 101px);
    right: 10px;
  }

  .gesture-hint {
    top: calc(max(12px, env(safe-area-inset-top)) + 139px);
    right: 10px;
    left: 10px;
    max-width: none;
    text-align: center;
  }
}

@media (max-width: 430px) {
  .brand h1 {
    font-size: 0.9rem;
  }

  .simulation-date {
    max-width: 148px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .control-button {
    padding-inline: 9px;
    font-size: 0.7rem;
  }
}

@media (max-width: 480px) {
  .topbar {
    grid-template-columns: minmax(0, 1fr);
  }

  .simulation-date {
    display: none;
  }
}

@media (max-height: 600px) and (orientation: landscape) {
  .topbar {
    min-height: 48px;
    grid-template-columns: auto 1fr;
  }

  .brand h1 {
    font-size: 0.9rem;
  }

  .simulation-date,
  .scene-label {
    display: none;
  }

  .control-cluster {
    grid-column: 2;
    grid-row: 1;
    justify-content: flex-end;
  }

  .planet-panel {
    top: 68px;
    right: 10px;
    bottom: auto;
    left: auto;
    width: min(280px, 42vw);
    max-height: calc(100vh - 142px);
    overflow-y: auto;
  }

  .planet-panel:not(.expanded) .planet-description {
    -webkit-line-clamp: 2;
  }

  .planet-dock {
    min-height: 54px;
  }

  .planet-tab {
    min-height: 40px;
    flex-direction: row;
  }

  .gesture-status {
    top: 66px;
    right: auto;
    left: 64px;
  }

  .gesture-hint {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

@media (forced-colors: active) {
  .scene-vignette {
    display: none;
  }

  .planet-swatch,
  .loading-orbit::before {
    border: 1px solid CanvasText;
  }
}
