/* Theme in three states, matching how the instructions page already works.
   Light is defined on bare :root so it is the fallback that always exists;
   dark is layered on top by preference and by explicit choice. A colour
   whose ONLY definition lives inside a media query has no value when the
   query does not match, which is how half-themed pages happen. */
:root {
  --bg: #f4f6f9;
  --panel-bg: #ffffff;
  --border: #d6dbe4;
  --text: #1b1f27;
  --text-dim: #5c6675;
  --accent: #1a72c4;
  --accent-dim: #cfe3f7;
  --warn: #a06a05;
  --scene-bg: #eef1f6;
  --grid: #d2d8e2;
  --overlay-bg: rgba(255, 255, 255, 0.78);
  --overlay-border: rgba(0, 0, 0, 0.10);
  --advice-bg: #fdf6e3;
  --row-hover: #eef2f8;
  color-scheme: light;
}

/* System preference, unless the user explicitly chose light */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161a;
    --panel-bg: #1c1f26;
    --border: #2c313c;
    --text: #e6e8ec;
    --text-dim: #9aa2af;
    --accent: #5eb1ff;
    --accent-dim: #2b4a68;
    --warn: #e8b34c;
    --scene-bg: #14161a;
    --grid: #2c313c;
    --overlay-bg: rgba(12, 14, 18, 0.72);
    --overlay-border: rgba(255, 255, 255, 0.09);
    --advice-bg: #241f14;
    --row-hover: #232833;
    color-scheme: dark;
  }
}

/* Explicit choice wins in both directions */
:root[data-theme="dark"] {
  --bg: #14161a;
  --panel-bg: #1c1f26;
  --border: #2c313c;
  --text: #e6e8ec;
  --text-dim: #9aa2af;
  --accent: #5eb1ff;
  --accent-dim: #2b4a68;
  --warn: #e8b34c;
  --scene-bg: #14161a;
  --grid: #2c313c;
  --overlay-bg: rgba(12, 14, 18, 0.72);
  --overlay-border: rgba(255, 255, 255, 0.09);
  --advice-bg: #241f14;
  --row-hover: #232833;
  color-scheme: dark;
}

/* --- theme toggle ------------------------------------------------------ */

.panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

#theme-toggle {
  flex-shrink: 0;
  margin-top: 2px;
  width: 34px;
  height: 34px;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
#theme-toggle:hover { border-color: var(--accent); color: var(--accent); }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}

#app {
  display: flex;
  height: 100vh;
}

#panel {
  width: 340px;
  flex-shrink: 0;
  background: var(--panel-bg);
  border-right: 1px solid var(--border);
  padding: 20px;
  overflow-y: auto;
}

#panel h1 {
  font-size: 1.25rem;
  margin: 0 0 2px;
}

.subtitle {
  color: var(--text-dim);
  font-size: 0.8rem;
  margin: 0 0 20px;
}

label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 14px;
}

select, input[type="range"] {
  display: block;
  width: 100%;
  margin-top: 6px;
}

select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
}

output {
  color: var(--text);
  font-variant-numeric: tabular-nums;
  float: right;
}

fieldset {
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 0 0 16px;
  padding: 12px 14px 2px;
}

legend {
  color: var(--text-dim);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0 4px;
}

.ref-table {
  width: 100%;
  font-size: 0.78rem;
  border-collapse: collapse;
  margin-bottom: 6px;
}

.ref-table td {
  padding: 3px 0;
  color: var(--text);
}

.ref-table td:first-child {
  color: var(--text-dim);
  width: 45%;
}

.hint {
  color: var(--text-dim);
  font-size: 0.72rem;
  line-height: 1.4;
  margin: 4px 0 10px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.checkbox-row em {
  color: var(--text-dim);
  font-style: normal;
  font-size: 0.72rem;
}

.checkbox-row input {
  width: auto;
  margin: 0;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  margin-top: 2px;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  padding: 2px 6px 2px 2px;
  border-radius: 5px;
  border: 1px solid transparent;
}

.legend-item:hover {
  border-color: var(--border);
}

.legend-item--off {
  color: var(--text-dim);
  opacity: 0.5;
  text-decoration: line-through;
}

.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
  flex-shrink: 0;
}

.size-advice {
  margin-top: 8px;
  padding: 7px 9px;
  border-left: 3px solid var(--warn);
  background: var(--advice-bg);
  color: var(--warn);
  font-size: 0.75rem;
  line-height: 1.45;
  font-variant-numeric: normal;
}

.size-note {
  margin-top: 6px;
  color: var(--text-dim);
  font-size: 0.72rem;
  line-height: 1.4;
  font-variant-numeric: normal;
}

#node-types-wrap {
  max-height: 280px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
}

#node-types-table {
  width: 100%;
  font-size: 0.75rem;
  border-collapse: collapse;
}

#node-types-table th {
  text-align: left;
  color: var(--text-dim);
  font-weight: 500;
  border-bottom: 1px solid var(--border);
  padding: 5px 6px;
  position: sticky;
  top: 0;
  background: var(--panel-bg);
}

#node-types-table td {
  padding: 4px 6px;
  border-bottom: 1px solid var(--border);
}

#node-types-table td:last-child {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#node-types-table tr.warn td {
  color: var(--warn);
}

#node-types-table tr.clickable {
  cursor: pointer;
}

#node-types-table tr.clickable:hover td {
  background: var(--row-hover);
}

#node-types-table tr.row-active td {
  background: var(--accent-dim);
  color: var(--text);
}

.warn-text { color: var(--warn); }

.kind-table { width: 100%; border-collapse: collapse; font-size: 0.76rem; }
.kind-table th {
  text-align: left; color: var(--text-dim); font-weight: 500;
  font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.03em;
  padding-bottom: 4px;
}
.kind-table td { padding: 4px 4px 4px 0; border-bottom: 1px solid var(--border); }
.kind-table td.kind-name { color: var(--text); width: 42%; }
.kind-table em { color: var(--text-dim); font-style: normal; font-size: 0.7rem; }
.kind-table label { display: flex; align-items: center; gap: 5px; margin: 0; color: var(--text); }
.kind-table input { width: auto; margin: 0; }
.too-short { color: var(--text-dim); opacity: 0.5; font-size: 0.7rem; }

#advanced > summary { cursor: pointer; font-size: 0.85rem; margin-bottom: 10px; }
#advanced > summary.summary-active { color: var(--warn); font-weight: 600; }

/* left-to-right orientation cue under a slider label */
.scale-hint {
  display: block;
  font-size: 0.68rem;
  color: var(--text-dim);
  letter-spacing: 0.02em;
  margin-top: 2px;
}

#stl-upload {
  width: 100%;
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-bottom: 6px;
}

#dimensions-readout {
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  margin-bottom: 10px;
}

.stub-tag {
  color: var(--warn);
  font-size: 0.7rem;
  font-weight: normal;
  text-transform: none;
  letter-spacing: 0;
}

#splice-btn {
  width: 100%;
  padding: 10px;
  background: var(--accent);
  color: var(--panel-bg);
  border: none;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
}

#splice-btn:hover { filter: brightness(1.08); }
#splice-btn:disabled { opacity: 0.5; cursor: default; }

#status, #comb-status {
  min-height: 1.4em;
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-top: 8px;
}

#results h2 {
  font-size: 0.9rem;
  margin: 18px 0 8px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

#stats-table, #comb-legend {
  width: 100%;
  font-size: 0.8rem;
  border-collapse: collapse;
}

#stats-table td, #comb-legend td {
  padding: 3px 0;
  border-bottom: 1px solid var(--border);
}

#stats-table td:first-child, #comb-legend td:first-child {
  color: var(--text-dim);
}

#stats-table td:last-child, #comb-legend td:last-child {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#stats-table tr.warn td, #comb-legend tr.warn td {
  color: var(--warn);
}

.downloads {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.downloads button {
  flex: 1;
  padding: 8px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}

.downloads button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

#viewer-wrap {
  position: relative;
  flex: 1;
}

#viewer {
  width: 100%;
  height: 100%;
  display: block;
}

#viewer-hint {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: var(--text-dim);
  font-size: 0.9rem;
  pointer-events: none;
}

#envelope-readout {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  /* must not swallow drags — it sits directly over the orbit control area */
  pointer-events: none;
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--overlay-bg);
  border: 1px solid var(--overlay-border);
  backdrop-filter: blur(6px);
  text-align: center;
  line-height: 1.35;
  max-width: calc(100% - 24px);
}
#envelope-readout .mm {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
#envelope-readout .imperial {
  font-size: 0.85rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}
#envelope-readout .axes {
  font-size: 0.7rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
  opacity: 0.75;
}

.downloads button.primary-action {
  background: var(--accent);
  color: var(--panel-bg);
  border-color: var(--accent);
  font-weight: 600;
}
.downloads button.primary-action:hover { filter: brightness(1.08); color: var(--panel-bg); }


/* --- mobile -----------------------------------------------------------
   The desktop layout is a fixed 340px sidebar beside a filling canvas,
   which on a phone leaves the viewer a slot too narrow to see anything.
   Stack instead: viewer on top at a workable height, controls scrolling
   beneath it. */
@media (max-width: 820px) {
  #app {
    flex-direction: column-reverse;   /* viewer above the controls */
    height: auto;
    /* dvh, not vh: vh is the LARGEST viewport, so with browser chrome
       showing it overflows, and installed to the home screen it misreads
       the standalone viewport entirely */
    min-height: 100dvh;
  }

  #viewer-wrap {
    flex: none;
    /* smaller than before — a half-screen model pushed every control below
       the fold, so using the app meant scrolling before you could touch
       anything */
    height: 42dvh;
    min-height: 240px;
    position: sticky;      /* keep the model in view while scrolling */
    top: 0;
    z-index: 5;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
    /* clear the notch when installed and painting under the status bar */
    padding-top: env(safe-area-inset-top, 0px);
  }

  #panel {
    width: 100%;
    border-right: none;
    overflow: visible;     /* the page scrolls, not the panel */
    padding:
      16px
      calc(16px + env(safe-area-inset-right, 0px))
      calc(40px + env(safe-area-inset-bottom, 0px))
      calc(16px + env(safe-area-inset-left, 0px));
  }

  /* touch targets: the 12 member checkboxes are the fiddliest control */
  .checkbox-row, .kind-table label { min-height: 34px; }
  .checkbox-row input, .kind-table input { width: 20px; height: 20px; }
  input[type="range"] { height: 30px; }

  .kind-table { font-size: 0.8rem; }
  .kind-table td.kind-name { width: 38%; }

  #splice-btn {
    position: sticky;
    /* sit above the home indicator, not under it */
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    padding: 14px;
    font-size: 1rem;
    box-shadow: 0 3px 14px rgba(0, 0, 0, 0.45);
  }

  .downloads { flex-wrap: wrap; }
  .downloads button { flex: 1 1 45%; padding: 11px; }
  #node-types-wrap { max-height: 340px; }
}

@media (max-width: 420px) {
  #panel { padding: 12px 12px 40px; }
  h1 { font-size: 1.1rem; }
  .kind-table em { display: block; }
}


/* --- instructions overlay ---------------------------------------------
   Shown in-page rather than a new tab: an installed PWA blocks
   window.open, and the download fallback hands a phone a 1.5MB HTML file
   it will not render — so the button appeared to do nothing. */
#instr-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: var(--panel-bg);
}

#instr-overlay[hidden] { display: none; }

.instr-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding:
    calc(10px + env(safe-area-inset-top, 0px))
    calc(12px + env(safe-area-inset-right, 0px))
    10px
    calc(12px + env(safe-area-inset-left, 0px));
  background: var(--panel-bg);
  color: var(--text);
  border-bottom: 1px solid var(--border);
  flex: none;
}

.instr-bar .spacer { flex: 1; }

.instr-bar button {
  padding: 9px 13px;
  min-height: 38px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 0.82rem;
  cursor: pointer;
}

.instr-bar button:hover { border-color: var(--accent); color: var(--accent); }

#instr-close { background: var(--accent); color: var(--panel-bg); border-color: var(--accent); font-weight: 600; }

/* matches the themed instructions document inside it; a fixed white
   here flashes on open and edges a dark page */
#instr-frame {
  flex: 1;
  width: 100%;
  border: 0;
  background: var(--bg);
}

.jump {
  width: 100%;
  margin-top: 10px;
  padding: 11px;
  background: transparent;
  color: var(--accent);
  border: 1px dashed var(--accent-dim);
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
}
.jump:hover { border-style: solid; }

.build-stamp { opacity: 0.45; font-size: 0.68rem; margin-top: 18px; }

#preset-name {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 8px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.85rem;
}

/* comb legend shares the stats table look; its header row needs the dim
   label colour the stats table gets from its first column */
#comb-legend th {
  text-align: left;
  padding: 4px 8px;
  color: var(--text-dim);
  font-weight: 600;
  font-size: 0.8rem;
}
