:root {
  --bg: #e8e8ec;
  --sheet: #fbfbfd;
  --ink: #1d1d24;
  --muted: #6a6a76;
  --line: #dcdce3;
  --chip: #eeeef3;
  --accent: #7f77dd;      /* purple: the full 4D object */
  --coral: #d85a30;       /* the slice */
  --teal: #1d9e75;
  --focus: #4b43b8;
  --radius: 14px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

#view {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;          /* one-finger orbit, pinch zoom go to OrbitControls */
}

#hud {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  left: 12px;
  right: 12px;
  display: flex;
  justify-content: space-between;
  pointer-events: none;
  font-size: 13px;
  color: var(--muted);
}
#hud-title { font-weight: 600; color: var(--ink); }

#loading {
  position: fixed;
  top: 40%;
  width: 100%;
  text-align: center;
  color: var(--muted);
}
#loading.hidden { display: none; }

/* ------------------------------------------------------------- bottom sheet */

#sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 auto;
  max-width: 560px;
  background: var(--sheet);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: 0 -4px 24px rgba(20, 20, 40, 0.14);
  display: flex;
  flex-direction: column;
  max-height: 64vh;
  max-height: 64dvh;
  padding-bottom: env(safe-area-inset-bottom);
  transition: max-height 0.22s ease;
}
#sheet.collapsed #sheet-body { display: none; }

#handle {
  all: unset;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  align-items: center;
  padding: 6px 16px 4px;
  cursor: pointer;
  touch-action: none;
}
#handle:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; border-radius: var(--radius); }
.grabber {
  grid-column: 1 / -1;
  justify-self: center;
  width: 40px;
  height: 5px;
  border-radius: 3px;
  background: #c9c9d2;
  margin-bottom: 6px;
}
#summary {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chevron {
  width: 10px;
  height: 10px;
  border-right: 2px solid var(--muted);
  border-top: 2px solid var(--muted);
  transform: rotate(-45deg);
  margin: 4px 4px 0 12px;
  transition: transform 0.2s;
}
#sheet:not(.collapsed) .chevron { transform: rotate(135deg); margin-top: -4px; }

#peek {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 2px 12px 10px 16px;
}
#peek .slider-row { flex: 1; margin: 0; }

#sheet-body {
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0 16px 16px;
  border-top: 1px solid var(--line);
}

.group { padding: 12px 0 4px; border-bottom: 1px solid var(--line); }
.group:last-of-type { border-bottom: 0; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 0 0 8px; }
h2 small { display: block; text-transform: none; letter-spacing: 0; font-weight: 400; margin-top: 2px; }

.field { display: grid; grid-template-columns: 64px 1fr; align-items: center; gap: 8px; margin-bottom: 10px; }
.field span { color: var(--muted); }

select, button {
  font: inherit;
  color: inherit;
}
select {
  width: 100%;
  min-height: 44px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--chip);
  padding: 0 10px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
button:active { filter: brightness(0.94); }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
button[aria-pressed="true"], .segmented button[aria-checked="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
button:disabled { opacity: 0.45; }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 8px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.grid2 .wide { grid-column: 1 / -1; }
.wide-btn { width: 100%; margin: 4px 0 10px; }

.slider-row {
  display: grid;
  grid-template-columns: 74px 1fr 58px;
  align-items: center;
  gap: 8px;
  margin: 6px 0;
  min-height: 44px;
}
.slider-row .lbl { color: var(--muted); font-size: 14px; }
output { font-variant-numeric: tabular-nums; text-align: right; font-size: 14px; }
input[type="range"] { width: 100%; min-height: 32px; accent-color: var(--accent); touch-action: pan-y; }
#peek input[type="range"] { accent-color: var(--coral); }

.rot-row {
  display: grid;
  grid-template-columns: 34px 1fr 50px 44px;
  align-items: center;
  gap: 8px;
  min-height: 46px;
}
.rot-row .plane { font-weight: 600; font-size: 14px; }
.rot-row .plane.w { color: var(--accent); }
.rot-row button { min-height: 40px; padding: 0; font-size: 18px; }
.speed-row {
  display: grid;
  grid-template-columns: 76px 1fr 66px;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  margin: -2px 0 4px 42px;   /* tucked under its plane's row */
  padding-left: 8px;
  border-left: 2px solid var(--accent);
}
.speed-row .lbl { color: var(--muted); font-size: 13px; }

.icon-btn { width: 48px; min-height: 44px; padding: 0; display: grid; place-items: center; }
.play {
  width: 0; height: 0;
  border-left: 13px solid currentColor;
  border-top: 8px solid transparent;
  border-bottom: 8px solid transparent;
  margin-left: 3px;
}
#sweep[aria-pressed="true"] .play {
  width: 12px; height: 15px;
  border: 0;
  border-left: 4px solid currentColor;
  border-right: 4px solid currentColor;
  margin-left: 0;
}
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.check { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }

#readout { font-size: 14px; font-variant-numeric: tabular-nums; padding-bottom: 8px; }
#readout div { padding: 1px 0; }
#readout .piece { padding-left: 14px; color: var(--muted); }
#readout .stat { color: var(--muted); font-size: 12px; margin-top: 4px; }

.notes:empty { display: none; }
.note {
  background: #f0effc;
  border-left: 3px solid var(--accent);
  padding: 8px 10px;
  border-radius: 6px;
  margin: 4px 0 8px;
  font-size: 14px;
}
.foot { font-size: 12px; color: var(--muted); margin: 12px 0 0; }
.hidden-row { display: none !important; }
.noscript { position: fixed; top: 40%; width: 100%; text-align: center; }

@media (min-width: 900px) {
  #sheet { left: auto; right: 16px; bottom: 16px; border-radius: var(--radius); max-height: calc(100vh - 32px); width: 380px; }
}
@media (prefers-reduced-motion: reduce) { #sheet, .chevron { transition: none; } }

/* ------------------------------------------------------------- panes (M7) */

#view { pointer-events: none; }            /* gestures go to the per-pane surfaces */
#surfaces, #overlays { position: fixed; inset: 0; pointer-events: none; }
.surface { position: absolute; pointer-events: auto; touch-action: none; }
.overlay-box { position: absolute; pointer-events: none; }

.pane-label {
  position: absolute;
  left: 12px;
  bottom: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  background: rgba(251, 251, 253, 0.82);
  padding: 2px 8px;
  border-radius: 6px;
}
.pane-divider { position: absolute; height: 1px; width: 100%; background: #c9c9d2; }
.pane-divider.vertical { width: 1px; height: 100%; }

.strip-band { position: absolute; border-top: 1px solid var(--line); }
.strip-caption {
  position: absolute;
  top: 2px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 12px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 6px;
}
.strip-labels { position: absolute; left: 0; right: 0; bottom: 2px; display: flex; }
.strip-labels span {
  flex: 1;
  text-align: center;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  white-space: nowrap;
}

.w-legend {
  position: absolute;
  right: 12px;
  bottom: 8px;
  display: grid;
  grid-template-columns: auto 72px auto;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  background: rgba(251, 251, 253, 0.82);
  padding: 3px 8px;
  border-radius: 6px;
}
.w-legend .bar { height: 8px; border-radius: 4px; background: linear-gradient(90deg, #2f6fdf, #e0402a); }

/* ----------------------------------------------------- corner controls (M7) */

#help-btn {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  left: 8px;
  width: 44px;
  height: 44px;
  min-height: 44px;
  padding: 0;
  border-radius: 50%;
  border: 1.5px solid var(--accent);
  background: rgba(251, 251, 253, 0.92);
  color: var(--accent);
  font: 700 20px/1 Georgia, "Times New Roman", serif;
  font-style: italic;
  display: grid;
  place-items: center;
  z-index: 5;
  box-shadow: 0 1px 6px rgba(20, 20, 40, 0.12);
}
#help-btn[hidden] { display: none; }
#hud { left: 64px; top: max(20px, calc(env(safe-area-inset-top) + 12px)); }

#fps-overlay {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  right: 8px;
  min-width: 64px;
  padding: 4px 8px;
  border-radius: 8px;
  background: rgba(20, 20, 28, 0.78);
  color: #fff;
  font: 600 13px/1.3 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  text-align: right;
  pointer-events: none;
  z-index: 5;
}
#fps-overlay[hidden] { display: none; }

/* ------------------------------------------------------------ help (M7) */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 30, 0.38);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 10;
}
.modal[hidden] { display: none; }
.modal-card {
  width: 100%;
  max-width: 560px;
  max-height: 86vh;
  max-height: 86dvh;
  background: var(--sheet);
  border-radius: var(--radius) var(--radius) 0 0;
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
}
@media (min-width: 900px) { .modal { align-items: center; } .modal-card { border-radius: var(--radius); } }
.modal-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px 8px; border-bottom: 1px solid var(--line); }
.tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; flex: 1; }
.tabs button { font-size: 14px; padding: 0 4px; }
.tabs button[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.close { width: 44px; padding: 0; font-size: 24px; flex: none; }
.modal-body { overflow-y: auto; padding: 4px 16px 16px; overscroll-behavior: contain; }
.modal-body h3 { font-size: 16px; margin: 14px 0 6px; }
.modal-body p { margin: 0 0 10px; }
.modal-body dt { font-weight: 600; margin-top: 10px; }
.modal-body dd { margin: 2px 0 0; }

.tour-card { max-height: 72vh; max-height: 72dvh; }
.tour-track {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
  overscroll-behavior: contain;
  flex: 1;
}
.tour-track::-webkit-scrollbar { display: none; }
.tour-slide { flex: 0 0 100%; scroll-snap-align: start; padding: 18px 20px 8px; overflow-y: auto; }
.tour-slide h3 { margin: 0 0 8px; font-size: 18px; }
.tour-slide p { margin: 0 0 10px; }
.tour-foot { display: grid; grid-template-columns: 88px 1fr 88px; align-items: center; gap: 8px; padding: 8px 12px 12px; }
.dots { display: flex; justify-content: center; gap: 6px; }
.dots span { width: 8px; height: 8px; border-radius: 50%; background: #cfcfd8; }
.dots span.on { background: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
