:root {
  --bg: #f6f8fb;
  --panel: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --accent: #2563eb;
  --border: #d6dde6;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
  background: radial-gradient(circle at 20% 20%, #ffffff, var(--bg) 60%);
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* Full-viewport 3D stage with every UI element floating on top as a glass panel.
   Applies to both editor mode (uploads) and snapshot mode (deep links). */
.app {
  position: relative;
  height: 100vh;
  height: 100dvh;
  width: 100vw;
  padding: 0;
  margin: 0;
  max-width: none;
  overflow: hidden;
}

header {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 20;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 14px;
  backdrop-filter: blur(10px) saturate(1.2);
  -webkit-backdrop-filter: blur(10px) saturate(1.2);
  box-shadow: 0 2px 12px rgba(15, 23, 42, 0.06);
}

header h1 {
  margin: 0;
  font-size: 15px;
  letter-spacing: -0.01em;
}

header p {
  display: none;
}

.controls {
  position: absolute;
  top: 62px;
  left: 12px;
  z-index: 15;
  width: 320px;
  max-width: calc(100vw - 24px);
  max-height: calc(100vh - 90px);
  overflow-y: auto;
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid var(--border);
  border-radius: 12px;
  backdrop-filter: blur(10px) saturate(1.2);
  -webkit-backdrop-filter: blur(10px) saturate(1.2);
  box-shadow: 0 4px 20px rgba(15, 23, 42, 0.08);
}

.controls label {
  display: grid;
  gap: 6px;
  font-size: 12px;
  text-transform: uppercase;
  color: var(--muted);
}

.controls label.wide {
  grid-column: 1 / -1;
}

.controls label.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.controls details.debug {
  grid-column: 1 / -1;
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  background: #f3f6fb;
}

.controls details.debug > summary {
  cursor: pointer;
  list-style: none;
  color: var(--muted);
  text-transform: uppercase;
  font-size: 12px;
  letter-spacing: 0.04em;
}

.controls details.debug > summary::-webkit-details-marker {
  display: none;
}

.controls .debug-body {
  margin-top: 12px;
  display: grid;
  gap: 12px;
}

.controls input {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--text);
}

.controls select {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--text);
}

.controls input[type="checkbox"] {
  width: 18px;
  height: 18px;
  padding: 0;
  accent-color: var(--accent);
}

.controls textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--text);
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 12px;
  line-height: 1.4;
}

.buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.controls button {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--text);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
  line-height: 1.2;
}
.controls button:hover {
  background: #f4f6fa;
  border-color: #b7c0cc;
}
.controls button:active {
  background: #eef2f7;
}
/* Save Online is the primary action once files are loaded. */
.controls #uploadSnapshotBtn {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}
.controls #uploadSnapshotBtn:hover {
  background: #1d4fd8;
  border-color: #1d4fd8;
}

.scan-list-panel {
  grid-column: 1 / -1;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  background: #ffffff;
}
.scan-list-panel > summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  color: var(--text);
  list-style: none;
}
.scan-list-panel > summary::-webkit-details-marker { display: none; }
.scan-list-panel > summary::before {
  content: '▸ ';
  display: inline-block;
  transition: transform 0.15s ease;
}
.scan-list-panel[open] > summary::before { content: '▾ '; }
.scan-list-panel textarea {
  margin-top: 8px;
}
.scan-list-transport {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}
.scan-list-transport button {
  min-width: 40px;
  padding: 6px 10px;
}
.scan-list-transport .scan-list-counter {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--muted);
  min-width: 70px;
}
.scan-list-speed {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}
.scan-list-speed input {
  width: 70px;
  padding: 4px 6px;
}


.status {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 25;
  padding: 8px 16px;
  background: rgba(17, 24, 39, 0.85);
  color: #f9fafb;
  border-radius: 999px;
  font-size: 12px;
  max-width: min(80vw, 640px);
  text-align: center;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.status:empty { opacity: 0; }
.status[data-tone="error"] { background: rgba(220, 38, 38, 0.9); }

.viewer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border-radius: 0;
  border: none;
  overflow: hidden;
  background: transparent;
  z-index: 0;
}

canvas#scene {
  width: 100%;
  height: 100%;
  display: block;
}

.viewer-corner-label {
  position: absolute;
  left: 16px;
  top: 16px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text);
  z-index: 10;
  max-width: 38%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.legend {
  position: absolute;
  right: 16px;
  top: 16px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  min-width: 180px;
  font-size: 12px;
}

.legend h3 {
  margin: 0 0 8px 0;
  font-size: 12px;
  text-transform: uppercase;
  color: var(--muted);
}

.legend ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.legend li {
  display: flex;
  align-items: center;
  gap: 8px;
}

.legend .swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  border: 1px solid #c7d2de;
}

/* When running an exported snapshot, the upload-related controls + Export button become
   meaningless (data is baked in), so hide the whole "load files" section. The scan box
   and "Clear Highlights" button stay so operators can still scan + reset. */
/* Snapshot / deep-link view is minimal on purpose — the workflow is: scan a QR, land
   on this page, see the scanned part. Editor chrome (title, upload/scan controls,
   Module View) is all hidden. Corner label stays so operators know which cabinet
   they're looking at. */
.snapshot-mode header,
.snapshot-mode .controls,
.snapshot-mode .viewer-controls,
.snapshot-mode .legend-mini,
.snapshot-mode .viewer-corner-label { display: none; }
/* Also hide the corner label in editor mode when empty so it doesn't render as a naked pill. */
.viewer-corner-label:empty { display: none; }

/* Single-line micro-chip Cabinet Layers legend, pinned top-left below the corner label. */
.legend.legend-mini {
  top: 56px;
  left: 12px;
  right: auto;
  bottom: auto;
  padding: 3px 8px;
  min-width: 0;
  font-size: 9px;
  background: rgba(255, 255, 255, 0.92);
}
.legend.legend-mini h3 { display: none; }
.legend.legend-mini ul {
  display: flex;
  gap: 8px;
  grid-auto-flow: unset;
  grid-template-rows: unset;
}
.legend.legend-mini li { gap: 3px; }
.legend.legend-mini .swatch { width: 7px; height: 7px; border-radius: 2px; }

/* Scan info card — collapses to just the barcode; tap to reveal the rest.
   Sits in the top-right slot the Cabinet Layers legend used to occupy. */
.scan-info {
  position: absolute;
  right: 16px;
  top: 16px;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  min-width: 180px;
  max-width: 260px;
  font-size: 12px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.1);
  backdrop-filter: blur(10px) saturate(1.2);
  -webkit-backdrop-filter: blur(10px) saturate(1.2);
}
.scan-info[hidden] { display: none; }
.scan-info > summary {
  list-style: none;
  cursor: pointer;
  display: grid;
  gap: 4px;
  user-select: none;
}
.scan-info > summary::-webkit-details-marker { display: none; }
.scan-info > summary::after {
  content: '▾';
  position: absolute;
  right: 12px;
  top: 12px;
  color: var(--muted);
  font-size: 10px;
  transition: transform 0.15s ease;
}
.scan-info[open] > summary::after { transform: rotate(180deg); }
.scan-info .scan-title {
  font-weight: 600;
  color: #ef4444;
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.scan-info .scan-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 15px;
  font-weight: 600;
  color: #111;
  word-break: break-all;
  padding-right: 18px; /* room for the chevron */
}
.scan-info dl {
  margin: 10px 0 0 0;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 12px;
}
.scan-info dt { color: var(--muted); text-transform: uppercase; font-size: 10px; align-self: center; }
.scan-info dd { margin: 0; color: #111; word-break: break-word; }

/* Snapshot / deep-link view: the scan card is the whole feedback surface —
   the bottom status toast is redundant clutter. */
.snapshot-mode .status { display: none; }

.viewer-controls {
  top: 16px;
  min-width: 180px;
  max-width: 240px;
  width: 240px;
  padding: 8px 10px;
  display: grid;
  gap: 6px;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  overflow-x: hidden;
  font-size: 11px;
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(10px) saturate(1.2);
  -webkit-backdrop-filter: blur(10px) saturate(1.2);
  box-shadow: 0 4px 20px rgba(15, 23, 42, 0.08);
}
/* When a scan card is visible in the top-right, push the Module View panel below it. */
.viewer:has(.scan-info:not([hidden])) .viewer-controls {
  top: 168px;
  max-height: calc(100vh - 192px);
}

.viewer-controls h3 { font-size: 10px; margin: 0 0 4px 0; }

.viewer-controls label {
  display: grid;
  gap: 3px;
  font-size: 10px;
  text-transform: uppercase;
  color: var(--muted);
}

.viewer-controls label.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 10px;
  line-height: 1.2;
}
.viewer-controls label.toggle > input[type="checkbox"] { flex-shrink: 0; }

.viewer-controls select,
.viewer-controls input[type="text"] {
  padding: 4px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--text);
  font-size: 12px;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
}

.viewer-controls .quick-search {
  display: flex;
  align-items: stretch;
  gap: 0;
}
.viewer-controls .quick-search-prefix {
  font-family: ui-monospace, SFMono-Regular, monospace;
  background: #f4f6fa;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-right: none;
  border-radius: 6px 0 0 6px;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}
.viewer-controls .quick-search input[type="text"] {
  border-radius: 0 6px 6px 0;
  font-family: ui-monospace, SFMono-Regular, monospace;
  flex: 1;
  min-width: 0;
}

.viewer-controls select[multiple] {
  min-height: 112px;
}

.viewer-controls input[type="number"] {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--text);
}

.viewer-controls input[type="checkbox"] {
  width: 18px;
  height: 18px;
  padding: 0;
  accent-color: var(--accent);
}

.viewer-controls button {
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #eef3ff;
  color: #1f3f87;
  cursor: pointer;
  font-size: 11px;
}
.viewer-controls button:disabled {
  opacity: 0.5;
  cursor: default;
}

.truck-fit-dims {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.truck-fit-dims label {
  font-size: 10px;
}

.truck-fit-note {
  color: var(--muted);
  font-size: 11px;
  text-transform: none;
}

/* ---------- Mobile (≤768px) ---------- */
/* Goal: keep the 3D viewport as the dominant element — same overlay pattern as desktop,
   just tighter panels. Module View overlay collapses into a bottom-sheet the user
   expands by tapping its title bar. */
@media (max-width: 768px) {
  header {
    top: 8px;
    left: 8px;
    padding: 5px 10px;
    max-width: calc(100vw - 16px);
  }
  header h1 { font-size: 13px; line-height: 1.2; }

  .controls {
    top: 46px;
    left: 8px;
    right: 8px;
    width: auto;
    max-width: none;
    max-height: 40vh;
    padding: 8px;
    gap: 6px;
    grid-template-columns: 1fr;
    border-radius: 10px;
  }
  .controls label { gap: 2px; font-size: 10px; }
  .controls input { padding: 6px 10px; font-size: 13px; }
  .controls .buttons { gap: 6px; flex-wrap: wrap; }
  .controls .buttons button {
    padding: 6px 10px;
    font-size: 12px;
  }

  .status {
    font-size: 11px;
    line-height: 1.3;
    bottom: 12px;
    padding: 6px 12px;
  }

  .viewer-corner-label {
    left: 8px;
    top: 8px;
    padding: 4px 8px;
    font-size: 10px;
    max-width: 60%;
  }
  .legend.legend-mini {
    top: 36px;
    left: 8px;
    padding: 2px 6px;
    font-size: 8px;
  }
  .scan-info {
    right: 8px;
    top: 8px;
    padding: 8px 10px;
    min-width: 130px;
    max-width: 180px;
    font-size: 11px;
  }
  .scan-info dl { gap: 1px 8px; }

  /* Module View collapses to a bottom-sheet. The h3 title bar is the tap target.
     Default = collapsed; .open = expanded. Desktop ignores .open since this whole
     block is media-query-scoped. */
  .viewer-controls {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: auto;
    min-width: 0;
    max-width: none;
    max-height: 70%;
    border-radius: 12px 12px 0 0;
    background: rgba(255, 255, 255, 0.97);
    padding: 0;
    overflow: hidden;
    transform: translateY(calc(100% - 36px));
    transition: transform 0.22s ease;
  }
  .viewer-controls.open {
    transform: translateY(0);
    overflow-y: auto;
  }
  .viewer-controls h3 {
    margin: 0;
    padding: 10px 14px;
    font-size: 11px;
    text-align: center;
    background: #f4f6fa;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    user-select: none;
    position: sticky;
    top: 0;
    z-index: 1;
  }
  .viewer-controls h3::after {
    content: ' ▴';
    color: var(--muted);
  }
  .viewer-controls.open h3::after { content: ' ▾'; }

  .viewer-controls > *:not(h3) {
    padding-left: 12px;
    padding-right: 12px;
  }
  .viewer-controls > *:not(h3):last-child {
    padding-bottom: 12px;
  }
  .viewer-controls > h3 + * {
    padding-top: 8px;
  }

  /* Bigger touch targets inside the sheet */
  .viewer-controls label { font-size: 10px; }
  .viewer-controls select,
  .viewer-controls input[type="text"] {
    padding: 7px 9px;
    font-size: 13px;
  }
  .viewer-controls button {
    padding: 8px 12px;
    font-size: 12px;
  }
  .viewer-controls label.toggle { font-size: 12px; gap: 10px; }
}
