* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; background: #000; }
body {
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: #e8eefc;
  -webkit-tap-highlight-color: transparent;
}
#scene { position: fixed; inset: 0; display: block; touch-action: none; }

/* safe-area aware insets (notched phones) */
:root {
  --gap: 14px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

/* ---- Loading overlay ---- */
#loader {
  position: fixed; inset: 0; z-index: 50;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 40%, #0b1530 0%, #02030a 70%);
  transition: opacity .6s ease; gap: 18px;
}
#loader.hidden { opacity: 0; pointer-events: none; }
.spinner {
  width: 46px; height: 46px; border-radius: 50%;
  border: 3px solid rgba(120,160,255,.25); border-top-color: #7aa2ff;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#loader .msg { font-size: 14px; letter-spacing: .04em; color: #9fb4e6; }
#loader .err { color: #ff8a8a; max-width: 80%; text-align: center; font-size: 13px; }

/* ---- Shared panel chrome ---- */
.panel {
  position: fixed; z-index: 20;
  background: rgba(10, 16, 34, .66);
  border: 1px solid rgba(120,160,255,.22);
  border-radius: 14px;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 8px 40px rgba(0,0,0,.5);
}

/* ---- HUD (top-left) ---- */
#hud {
  top: calc(var(--gap) + var(--safe-t)); left: calc(var(--gap) + var(--safe-l));
  padding: 13px 15px; width: 290px; max-width: calc(100vw - 2*var(--gap));
  max-height: calc(100vh - 2*var(--gap) - var(--safe-t) - var(--safe-b));
  overflow: hidden;
}
#hud .hudhead { display: flex; align-items: baseline; gap: 8px; cursor: pointer; }
#hud h1 { font-size: 15px; font-weight: 650; letter-spacing: .01em; flex: 1; }
#hud .toggle {
  font-size: 13px; color: #9fb4e6; background: rgba(120,160,255,.14);
  border: 0; border-radius: 7px; padding: 2px 8px; cursor: pointer; line-height: 1.4;
}
#hud .sub { font-size: 11.5px; color: #8ea4d6; margin: 4px 0 10px; }
.hudbody { overflow-y: auto; max-height: calc(100vh - 130px); }
#hud.collapsed .hudbody { display: none; }
#hud.collapsed .sub { display: none; }
#hud .row { display: flex; justify-content: space-between; gap: 12px; font-size: 12.5px; padding: 3px 0; }
#hud .row .k { color: #93a7d6; white-space: nowrap; }
#hud .row .v { color: #f1f5ff; font-variant-numeric: tabular-nums; text-align: right; }
#hud .clock { letter-spacing: -.01em; }
#hud .below { color: #ff9d6e; font-weight: 600; }
#hud .above { color: #7df0a0; font-weight: 600; }
#hud .moonchip {
  display: inline-flex; align-items: center; gap: 7px;
  margin-top: 10px; padding: 5px 10px; border-radius: 999px;
  background: rgba(255,255,255,.06); font-size: 12px;
}
#hud .moonchip .dot {
  width: 14px; height: 14px; border-radius: 50%;
  background: radial-gradient(circle at 32% 35%, #fff, #c9cdd6 60%, #8b8f99);
  box-shadow: 0 0 8px rgba(255,255,255,.35);
}

/* ---- Contextual right panel: GR + ladder share the slot (one at a time) ---- */
#grPanel, #ladder {
  top: calc(var(--gap) + var(--safe-t)); right: calc(var(--gap) + var(--safe-r));
  width: 300px; max-width: calc(100vw - 2*var(--gap));
  max-height: calc(100vh - 160px - var(--safe-t)); overflow-y: auto;
  padding: 14px 16px;
  opacity: 0; transform: translateY(-6px); pointer-events: none;
  transition: opacity .28s, transform .28s;
}
#grPanel.show, #ladder.show { opacity: 1; transform: none; pointer-events: auto; }
#grPanel h2, #ladder h2 {
  font-size: 14.5px; font-weight: 650; margin-bottom: 4px;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
#grPanel h2 { color: #c9b6ff; }
#ladder h2 { color: #ffd9a8; }
.tag {
  font-size: 9.5px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase;
  color: #9affd0; background: rgba(80,255,180,.12); padding: 2px 7px; border-radius: 999px;
}
.grsub { font-size: 11.5px; color: #9aa9d6; line-height: 1.5; margin-bottom: 8px; }
.cite { color: #6f7fae; font-style: italic; }
.grsec {
  font-size: 11px; font-weight: 600; color: #b9c6ee; margin: 10px 0 4px;
  border-top: 1px solid rgba(120,160,255,.16); padding-top: 7px;
}
.grsec .eq { color: #8fa6da; font-weight: 400; font-style: italic; }
#grPanel .row, #ladder .row { display: flex; justify-content: space-between; gap: 12px; font-size: 12.5px; padding: 2px 0; }
#grPanel .row .k { color: #93a7d6; }
#grPanel .row .v { color: #eaf1ff; font-variant-numeric: tabular-nums; text-align: right; }
.grnote { font-size: 10.5px; color: #8090ba; line-height: 1.5; margin-top: 9px; }
.grnote b { color: #cdb8ff; }
.cand { display: flex; gap: 9px; font-size: 11.5px; color: #cfd8ee; line-height: 1.45; margin: 7px 0; }
.cand b { color: #fff; } .cand i { color: #b9c6ee; font-style: italic; }
.cdot { flex: 0 0 11px; width: 11px; height: 11px; border-radius: 50%; margin-top: 3px; box-shadow: 0 0 9px currentColor; }

/* ---- Bottom dock: time strip + view controls (single column, centered) ---- */
#dock {
  position: fixed; z-index: 20;
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--gap) + var(--safe-b));
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  width: max-content; max-width: calc(100vw - 2*var(--gap));
}
.bar {
  display: flex; align-items: center; gap: 6px; padding: 7px 9px;
  background: rgba(10, 16, 34, .66);
  border: 1px solid rgba(120,160,255,.22);
  border-radius: 12px;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  max-width: 100%; overflow-x: auto; scrollbar-width: none;
}
.bar::-webkit-scrollbar { display: none; }
.bar .tlabel { font-size: 12px; color: #9fb4e6; padding: 0 3px; white-space: nowrap; }
.bar button {
  font: inherit; font-size: 12.5px; color: #dce6ff; white-space: nowrap;
  background: rgba(60, 90, 170, .25);
  border: 1px solid rgba(120,160,255,.28);
  padding: 7px 12px; border-radius: 10px; cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
}
.bar button:hover { background: rgba(90, 130, 230, .42); border-color: rgba(150,185,255,.55); }
.bar button:active { transform: translateY(1px); }
.bar button.active { background: rgba(120, 160, 255, .55); border-color: #aecbff; color: #fff; }
#controls { flex-wrap: wrap; justify-content: center; }

/* ---- Hint (desktop only) ---- */
#hint {
  position: fixed; right: calc(var(--gap) + var(--safe-r)); bottom: calc(var(--gap) + var(--safe-b));
  z-index: 19; padding: 10px 13px; font-size: 12px; color: #9fb4e6; max-width: 210px; line-height: 1.5;
  background: rgba(10,16,34,.5); border: 1px solid rgba(120,160,255,.18); border-radius: 12px;
}
#hint b { color: #d7e3ff; }

/* ---- Per-view caption ---- */
#viewinfo {
  left: calc(var(--gap) + var(--safe-l));
  bottom: calc(var(--gap) + var(--safe-b) + 104px);   /* sit above the bottom dock */
  z-index: 19; max-width: 320px; padding: 11px 14px;
  font-size: 12.5px; line-height: 1.55; color: #c5d3f0; cursor: default;
  opacity: 0; transform: translateY(8px); pointer-events: none;
  transition: opacity .3s, transform .3s;
}
#viewinfo.show { opacity: 1; transform: none; pointer-events: auto; }
#viewinfo b { color: #f0f5ff; }
#viewinfo .eq { color: #9fb6e6; font-style: italic; }

/* ---- Pro button + modal ---- */
.bar button.pro {
  color: #1a1206; font-weight: 650;
  background: linear-gradient(90deg, #ffd278, #ffb24d);
  border-color: rgba(255,200,110,.6);
}
.bar button.pro:hover { background: linear-gradient(90deg, #ffdc92, #ffbe66); }
.modal-backdrop {
  position: fixed; inset: 0; z-index: 60; display: none;
  align-items: center; justify-content: center; padding: 16px;
  background: rgba(2,4,12,.62); backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px);
}
.modal-backdrop.show { display: flex; }
.modal {
  position: relative; width: min(420px, 94vw);
  background: rgba(14,20,40,.97); border: 1px solid rgba(120,160,255,.28);
  border-radius: 18px; padding: 24px 22px; box-shadow: 0 24px 80px rgba(0,0,0,.65);
}
.modal h2 {
  font-size: 21px; margin-bottom: 7px;
  background: linear-gradient(90deg, #ffd278, #9b8cff);
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.modal p { font-size: 13px; color: #b9c6ee; line-height: 1.6; margin-bottom: 15px; }
.modal ul { list-style: none; display: grid; gap: 10px; margin-bottom: 19px; }
.modal li { font-size: 13px; color: #dce6ff; display: flex; gap: 10px; line-height: 1.45; }
.modal li b { color: #fff; }
.modal .cta {
  display: block; text-align: center; text-decoration: none; font-weight: 650; font-size: 14px;
  padding: 12px; border-radius: 12px; color: #1a1206;
  background: linear-gradient(90deg, #ffd278, #ffb24d);
  transition: filter .15s;
}
.modal .cta:hover { filter: brightness(1.06); }
.modal-note { font-size: 11px; color: #8090ba; text-align: center; margin-top: 11px; line-height: 1.5; }
.modal-close {
  position: absolute; top: 12px; right: 14px; background: none; border: 0;
  color: #9fb4e6; font-size: 17px; cursor: pointer; line-height: 1;
}

/* ---- Body label badges ---- */
.badge {
  position: fixed; z-index: 15; transform: translate(-50%, -140%);
  pointer-events: none; font-size: 11.5px; letter-spacing: .03em;
  color: #eaf1ff; text-shadow: 0 1px 4px #000, 0 0 10px rgba(0,0,0,.7);
  white-space: nowrap; opacity: 0; transition: opacity .25s;
}
.badge .tick { display:block; width:1px; height:14px; margin:3px auto 0; background: rgba(255,255,255,.5); }
.badge.gal { color: #ffe0b0; text-shadow: 0 0 12px rgba(255,200,120,.7), 0 1px 4px #000; }
.badge.cand { font-size: 10.5px; color: #d8e6ff; }

/* =====================  RESPONSIVE  ===================== */

/* Tablet: shrink panels, drop the hint */
@media (max-width: 900px) {
  #hint { display: none; }
  #hud, #grPanel, #ladder { width: 264px; }
}

/* Phone: HUD collapses to a chip; right panel becomes a bottom sheet */
@media (max-width: 640px) {
  :root { --gap: 10px; }
  #hud {
    width: auto; max-width: calc(100vw - 2*var(--gap));
    padding: 10px 13px;
  }
  #hud h1 { font-size: 14px; }
  .hudbody { max-height: 44vh; }

  /* GR / ladder dock to the bottom as a sheet, above the controls */
  #grPanel, #ladder {
    top: auto; right: 50%; transform: translate(50%, 8px);
    bottom: calc(118px + var(--safe-b));
    width: calc(100vw - 2*var(--gap)); max-width: 460px;
    max-height: 38vh;
  }
  #grPanel.show, #ladder.show { transform: translate(50%, 0); }

  .bar button { font-size: 12px; padding: 7px 10px; }
  .bar .tlabel { display: none; }
  #dock { width: calc(100vw - 2*var(--gap)); }
  #controls { flex-wrap: nowrap; justify-content: flex-start; }

  /* hide the small body labels on tiny screens to reduce clutter */
  .badge.cand { display: none; }

  /* caption moves to the top so it never fights the bottom dock */
  #viewinfo {
    left: var(--gap); right: var(--gap); bottom: auto;
    top: calc(var(--gap) + var(--safe-t) + 52px);
    max-width: none; font-size: 12px;
  }
}

/* Very short landscape phones: keep HUD collapsed-friendly */
@media (max-height: 430px) {
  .hudbody { max-height: 38vh; }
  #grPanel, #ladder { max-height: 50vh; }
}
