/* Camera Rigs viewer. The WebGL canvas sits BEHIND the page: every element that contains a
   view (the 3D view, the thumbnail boxes, the modal stage) and all of its ancestors must keep a
   transparent background, or they hide the canvas. */

:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --panel-2: #f7f8fa;
  --text: #1a1e24;
  --muted: #5c6672;
  --line: #d9dde3;
  --accent: #2563eb;
  --accent-soft: rgba(37, 99, 235, 0.12);
  --sel: #111418;
  --view-bg: #e3e6eb;
  --thumb-bg: #d6dae0;
  --stage-bg: #ffffff;
  --grid-major: #8e98a4;
  --grid-minor: #c0c6ce;
  --room: #7a8593;
  --wall: #c9ccd1;
  --dim: rgba(243, 244, 246, 0.66);
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 8px 24px rgba(16, 24, 40, 0.08);
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101317;
    --panel: #171b20;
    --panel-2: #1c2127;
    --text: #e5e8ec;
    --muted: #97a1ad;
    --line: #2a3038;
    --accent: #6ea8ff;
    --accent-soft: rgba(110, 168, 255, 0.16);
    --sel: #ffffff;
    --view-bg: #181c21;
    --thumb-bg: #21262d;
    --stage-bg: #171b20;
    --grid-major: #5a6470;
    --grid-minor: #30363e;
    --room: #8592a1;
    --wall: #4a525c;
    --dim: rgba(16, 19, 23, 0.7);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

h1, h2, h3 { margin: 0; font-weight: 600; }
button, input, select { font: inherit; color: inherit; }

#gl {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 0;
  pointer-events: none;
}
#gl.front { z-index: 30; }       /* modal open: above the backdrop, below the modal panel */

#app {
  position: relative;
  z-index: 1;
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

/* ------------------------------------------------------------------ header */
.top {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 10px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 16px; min-width: 0; flex: 1; }
.brand h1 { font-size: 18px; white-space: nowrap; letter-spacing: -0.01em; }
.lede { margin: 0; color: var(--muted); font-size: 13px; }
.dataset { color: var(--muted); font-size: 12px; text-align: right; white-space: nowrap; }
.dataset b { color: var(--text); font-weight: 600; }
.dataset .warn { color: #c2410c; }

/* ------------------------------------------------------------------ layout */
.layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1.15fr) minmax(0, 1fr);
  grid-template-areas: "side viewer cams";
  min-height: 0;
}

.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  line-height: 1.3;
}
.btn:hover { border-color: var(--muted); }
.btn:focus-visible, .tile:focus-visible, .rig:focus-visible, .chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ------------------------------------------------------------------ rigs (left) */
.side {
  grid-area: side;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow: auto;
  padding: 14px 12px 20px;
  min-height: 0;
}
.side h2, .cams h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.side-hint { margin: 4px 0 10px; font-size: 12px; color: var(--muted); }
.rig-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.rig {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 2px 10px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  background: var(--panel);
}
.rig:hover { border-color: var(--muted); }
.rig.active { border-color: var(--accent); background: var(--accent-soft); }
.rig.failed { cursor: default; opacity: 0.75; }
.rig input { margin: 2px 0 0; grid-row: span 2; accent-color: var(--accent); }
.rig .t { font-weight: 600; font-size: 13px; line-height: 1.3; }
.rig .o { font-size: 12px; color: var(--muted); }
.rig .err { grid-column: 1 / -1; color: #dc2626; font-size: 12px; }
.rig-info { grid-column: 1 / -1; margin-top: 8px; font-size: 12px; cursor: auto; }
.rig-info .src { font-family: var(--mono); font-size: 11px; color: var(--muted); word-break: break-word; margin: 0 0 6px; }
.rig-info .facts { color: var(--muted); margin: 0 0 6px; }
.rig-info details { border-top: 1px solid var(--line); padding-top: 6px; }
.rig-info summary { cursor: pointer; font-weight: 600; }
.rig-info pre, .details pre {
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.45;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  margin: 6px 0 0;
  overflow: auto;
  max-height: 46vh;
  white-space: pre;
}
.rig-info a { color: var(--accent); }

/* ------------------------------------------------------------------ 3D view (middle) */
.viewer { grid-area: viewer; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.view3d {
  position: relative;
  flex: 1;
  min-height: 0;
  touch-action: none;
  cursor: grab;
  container-type: size;
}
.view3d:active { cursor: grabbing; }
.legend {
  position: absolute;
  left: 8px;
  top: 8px;
  max-width: calc(100% - 110px);
  max-height: 34%;
  overflow: hidden;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 3px 4px;
  pointer-events: none;
}
.legend span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 0 6px 0 4px;
  border-radius: 9px;
  font-size: 10.5px;
  line-height: 17px;
  background: color-mix(in srgb, var(--panel) 80%, transparent);
  border: 1px solid var(--line);
}
.legend i { width: 8px; height: 8px; }
@container (max-height: 300px) { .legend, .hint { display: none; } }
@container (max-width: 480px) { .hint, .legend { display: none; } }
.legend i, .chip i, .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; flex: none; }
.fit { position: absolute; right: 10px; top: 10px; font-size: 12px; }
.hint {
  position: absolute;
  left: 10px;
  bottom: 8px;
  font-size: 11px;
  color: var(--muted);
  pointer-events: none;
  text-shadow: 0 0 3px var(--view-bg);
}
.timeline {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.timeline input[type=range], .m-foot input[type=range] { flex: 1; min-width: 60px; accent-color: var(--accent); }
.play { width: 38px; padding: 4px 0; text-align: center; }
.time { font-variant-numeric: tabular-nums; font-size: 12px; color: var(--muted); white-space: nowrap; min-width: 150px; text-align: right; }

/* ------------------------------------------------------------------ camera grid (right) */
.cams { grid-area: cams; display: flex; flex-direction: column; min-height: 0; min-width: 0; border-left: 1px solid var(--line); }
.cams-head { padding: 12px 12px 8px; background: var(--panel); border-bottom: 1px solid var(--line); }
.cams-head h2 .count { text-transform: none; letter-spacing: 0; font-weight: 500; }
.filters { margin-top: 8px; display: grid; gap: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; max-height: 60px; overflow: auto; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 12px;
  padding: 1px 8px 1px 6px;
  font-size: 12px;
  cursor: pointer;
}
.chip b { font-weight: 500; color: var(--muted); }
.chip.off { opacity: 0.45; }
.chip.off i { background: transparent !important; box-shadow: inset 0 0 0 1px var(--muted); }
.filter-row { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; font-size: 12px; color: var(--muted); }
.filter-row select { margin-left: 4px; border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 2px 4px; color: var(--text); }
.filter-row .size { display: inline-flex; align-items: center; gap: 6px; }
.filter-row .size input { width: 110px; accent-color: var(--accent); }

.grid-scroll { flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain; }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--tw, 170px), 1fr));
  gap: 10px;
  padding: 10px 12px 18px;
  align-items: start;
}
.empty { color: var(--muted); padding: 0 14px; }
.tile {
  border: 1px solid var(--line);
  border-radius: 3px;
  cursor: pointer;
  min-width: 0;
}
.tile:hover { border-color: var(--muted); }
.tile.sel { outline: 2px solid var(--sel); outline-offset: 1px; }
.tile .view { position: relative; aspect-ratio: var(--ar, 1.75); }
.tile .offtag, .stage-view .offtag { display: none; }
.tile.off .view::after, .stage-view.off::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--dim);
}
.tile.off .offtag, .stage-view.off .offtag {
  display: block;
  position: absolute;
  z-index: 1;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  white-space: nowrap;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--muted);
}
.tile .cap { padding: 4px 7px 5px; background: var(--panel); border-top: 1px solid var(--line); min-width: 0; }
.tile .nm { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; min-width: 0; }
.tile .nm span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile .sub { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ------------------------------------------------------------------ modal */
.backdrop { position: fixed; inset: 0; z-index: 20; background: rgba(8, 10, 14, 0.55); }
.modal {
  position: fixed;
  z-index: 40;
  inset: 4vh 4vw;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.m-head, .m-foot { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel); }
.m-head { border-bottom: 1px solid var(--line); }
.m-foot { border-top: 1px solid var(--line); }
.m-titles { min-width: 0; flex: 1; }
.m-titles h3 { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-sub { font-size: 12px; color: var(--muted); }
.m-nav { display: flex; gap: 6px; }
.m-body { display: grid; grid-template-columns: minmax(0, 1fr) 360px; min-height: 0; }
.stage { position: relative; min-height: 0; display: flex; align-items: center; justify-content: center; }
.stage-view { position: relative; }
.details { background: var(--panel); border-left: 1px solid var(--line); overflow: auto; padding: 12px 14px; font-size: 12px; }
.details h4 { margin: 12px 0 4px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.details h4:first-child { margin-top: 0; }
.details table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.details td { padding: 2px 0; vertical-align: top; }
.details td:first-child { color: var(--muted); width: 42%; padding-right: 8px; }
.details pre { max-height: none; }

/* ------------------------------------------------------------------ loading / errors */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: var(--bg);
}
.overlay-box { width: min(560px, calc(100vw - 32px)); }
.overlay-msg { font-size: 14px; color: var(--muted); white-space: pre-wrap; }
.overlay.error .overlay-msg { color: var(--text); }
.overlay.error .overlay-msg h2 { font-size: 17px; margin-bottom: 8px; color: #dc2626; }
.overlay.error .overlay-msg code { font-family: var(--mono); font-size: 12px; }
.overlay.error .bar { display: none; }
.bar { height: 4px; margin-top: 12px; background: var(--line); border-radius: 2px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.15s; }

/* ------------------------------------------------------------------ smaller screens */
@media (max-width: 1180px) {
  .lede { display: none; }
  .layout {
    grid-template-columns: 250px minmax(0, 1fr);
    grid-template-rows: minmax(0, 1.1fr) minmax(0, 1fr);
    grid-template-areas: "side viewer" "side cams";
  }
  .cams { border-left: 0; border-top: 1px solid var(--line); }
  .m-body { grid-template-columns: minmax(0, 1fr) 300px; }
}

/* short windows: let the page scroll instead of squeezing the 3D view and the grid */
@media (max-height: 640px) and (min-width: 761px) {
  #app { height: auto; display: block; }
  .layout { grid-template-rows: auto auto; }
  .viewer { height: max(300px, calc(100vh - 70px)); }
  .cams { height: max(360px, calc(100vh - 20px)); }
  .side { overflow: visible; }
}

@media (max-width: 760px) {
  #app { height: auto; display: block; }
  .top { padding: 10px 14px; }
  .dataset { display: none; }
  .layout { display: flex; flex-direction: column; }
  .viewer { order: 1; height: 62vh; min-height: 320px; }
  .side { order: 2; border-right: 0; border-bottom: 1px solid var(--line); overflow: visible; padding: 12px 14px; }
  .cams { order: 3; }
  .side-hint { display: none; }
  .rig { padding: 6px 9px; }
  .hint { display: none; }
  .time { min-width: 0; }
  .cams { border-top: 1px solid var(--line); }
  .grid-scroll { overflow: visible; }
  .grid { padding: 10px 14px 24px; }
  .modal { inset: 0; border-radius: 0; border: 0; }
  .m-body { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1.2fr) minmax(0, 1fr); }
  .details { border-left: 0; border-top: 1px solid var(--line); }
  .m-nav .btn { padding: 4px 8px; }
}

/* ------------------------------------------------------------------ scene note + scene fit */
.scene-box { margin: 0 0 14px; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel-2); font-size: 12px; line-height: 1.45; color: var(--muted); }
.scene-box b { color: var(--text); font-weight: 600; }
.scene-box a { color: var(--accent); }
.scene-note { margin: 0; }
.demo-pick { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; color: var(--text); font-weight: 600; }
.demo-pick select { flex: 1; min-width: 0; font: inherit; font-weight: 400; padding: 3px 6px;
  color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 6px; }
.fit-badge { margin-left: 6px; padding: 0 6px; border-radius: 9px; font-size: 11px;
  color: #b45309; background: rgba(217, 119, 6, 0.12); white-space: nowrap; }
.scene-fit { margin: 8px 0 4px; padding: 7px 9px; border-radius: 6px; font-size: 12px; line-height: 1.45;
  background: var(--panel-2); border-left: 3px solid var(--line); }
.scene-fit.changed { border-left-color: #d97706; }
.scene-fit .sf-head { color: var(--text); font-weight: 600; margin-bottom: 3px; }
.scene-fit ul { margin: 0; padding-left: 16px; }
.scene-fit li { margin: 2px 0; }
.scene-fit .sf-none { color: var(--muted); }
@media (prefers-color-scheme: dark) {
  .fit-badge { color: #fbbf24; background: rgba(251, 191, 36, 0.14); }
  .scene-fit.changed { border-left-color: #f59e0b; }
}
