/* CAD Peek theme: "inspection bench".
   Graphite ink on a pale studio-grey bench, one signal orange for cuts, picks and dimensions.
   Signature: the dimension tag (.dim), a monospace number held between two orange tick marks,
   like a dimension line on a drawing. Every size and distance on the page wears it.
   Loaded after styles.css; the builder links it and does not edit it. */

:root {
  color-scheme: light dark;

  /* Palette (light). Contrast ratios are against the background named. */
  --bg: #eef0f2;            /* the bench */
  --surface: #ffffff;       /* rail, cards */
  --raised: #f6f7f8;        /* readouts, inputs */
  --stage: #e3e6e9;         /* 3D viewport clear colour */
  --model: #b9c0c7;         /* neutral model shade (three.js material) */
  --text: #1d2125;          /* 15.6:1 on surface, 13.9:1 on stage */
  --muted: #4f5761;         /* 7.3:1 on surface, 5.8:1 on stage */
  --border: #d5d9de;
  --border-strong: #8a939d; /* 3.1:1 on surface: control outlines */
  --accent: #c2410c;        /* white on it 5.2:1; 4.3:1 on stage as a line */
  --accent-hover: #9a3412;  /* white on it 7.3:1 */
  --accent-text: #9a3412;   /* 7.3:1 on surface, 6.3:1 on bg */
  --accent-tint: #fdebe3;   /* accent-text on it 6.4:1 */
  --on-accent: #ffffff;
  --success: #166534;       /* 7.1:1 on surface */
  --success-tint: #e8f5ec;
  --warning: #854d0e;       /* 6.9:1 on surface, 6.2:1 on tint */
  --warning-tint: #fdf3dc;
  --danger: #b91c1c;        /* 6.5:1 on surface, 5.7:1 on tint */
  --danger-tint: #fdecea;

  /* Type */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --text-xs: 0.8125rem;   /* 13px caps labels */
  --text-sm: 0.9375rem;   /* 15px rail text */
  --text-md: 1.0625rem;   /* 17px body */
  --text-lg: 1.25rem;     /* 20px h2 */
  --text-xl: 1.75rem;     /* 28px measurement readout */
  --text-2xl: clamp(1.75rem, 4.5vw, 2.5rem); /* h1 */

  /* Space */
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px; --s7: 48px; --s8: 64px;

  /* Shape and depth */
  --r-s: 6px; --r-m: 10px; --r-l: 14px;
  --shadow-1: 0 1px 2px rgb(20 24 28 / 0.06), 0 2px 8px rgb(20 24 28 / 0.06);
  --shadow-2: 0 8px 28px rgb(20 24 28 / 0.18);
  --fast: 160ms;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);

  /* Legacy names the template uses */
  --line: var(--border);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #1d2024;
    --raised: #252a2f;
    --stage: #101215;
    --model: #8d969f;
    --text: #e6e8ea;          /* 13.6:1 on surface */
    --muted: #a3acb6;         /* 7.0:1 on surface */
    --border: #30363d;
    --border-strong: #69737d; /* 3.2:1 on surface */
    --accent: #ff7a4d;        /* on-accent on it 7.7:1; 6.6:1 on stage as a line */
    --accent-hover: #ff9a75;
    --accent-text: #ff9a75;   /* 7.9:1 on surface */
    --accent-tint: #3a2219;   /* accent-text on it 6.9:1 */
    --on-accent: #1a0a03;
    --success: #5fd08f;       /* 7.9:1 on surface */
    --success-tint: #14271c;
    --warning: #f0b44c;       /* 8.6:1 on surface */
    --warning-tint: #2c230f;
    --danger: #ff8a80;        /* 7.0:1 on surface */
    --danger-tint: #2a1612;
    --shadow-1: 0 1px 2px rgb(0 0 0 / 0.4), 0 2px 8px rgb(0 0 0 / 0.3);
    --shadow-2: 0 8px 28px rgb(0 0 0 / 0.55);
  }
}

/* ---------- Page ---------- */

* { box-sizing: border-box; }
html { background: var(--bg); }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: var(--text-md)/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
}

header, main, footer { max-width: 76rem; margin: 0 auto; padding: var(--s4) var(--s5); }
header { border-bottom: 1px solid var(--border); }
.brand {
  display: inline-flex; align-items: center; gap: var(--s2);
  min-height: 44px; line-height: 44px;
  font-weight: 700; letter-spacing: -0.01em; color: var(--text); text-decoration: none;
}
.brand::before {
  content: ""; width: 24px; height: 24px; flex: none;
  background: url("icons/favicon.svg") center / contain no-repeat;
}
header nav a { color: var(--muted); text-decoration: none; font-size: var(--text-sm); padding: 0 var(--s2); border-radius: var(--r-s); }
header nav a:hover { color: var(--text); background: var(--raised); }

/* Reading width for prose; the tool uses the full column. */
main > h1, main > p, main > h2, main > h3, main > ol, main > ul { max-width: 68ch; }
h1 { font-size: var(--text-2xl); line-height: 1.1; letter-spacing: -0.02em; margin: var(--s5) 0 var(--s2); }
.lede { font-size: var(--text-md); color: var(--muted); margin: 0 0 var(--s5); }
h2 { font-size: var(--text-lg); line-height: 1.3; margin: var(--s7) 0 var(--s3); }
h3 { font-size: var(--text-md); margin: var(--s5) 0 var(--s1); }
main ol, main ul { padding-left: 1.4em; }
main li + li { margin-top: var(--s2); }
a { color: var(--accent-text); text-underline-offset: 3px; }
a:hover { text-decoration-thickness: 2px; }
code, kbd { font-family: var(--mono); font-size: 0.9em; }
kbd {
  padding: 1px 6px; border: 1px solid var(--border-strong); border-bottom-width: 2px;
  border-radius: var(--r-s); background: var(--surface);
}

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- Buttons ---------- */

.btn, .tool button, .consent button {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  min-height: 44px; padding: var(--s2) var(--s4);
  border: 1px solid var(--border-strong); border-radius: var(--r-m);
  background: var(--surface); color: var(--text);
  font: 600 var(--text-sm)/1.2 var(--font); text-decoration: none; cursor: pointer;
  transition: background-color var(--fast) var(--ease), border-color var(--fast) var(--ease),
              color var(--fast) var(--ease), transform var(--fast) var(--ease);
}
.btn:hover, .tool button:hover { background: var(--raised); border-color: var(--text); }
.btn:active, .tool button:active { transform: translateY(1px); }
.tool button:disabled, .btn[aria-disabled="true"] {
  opacity: 0.45; cursor: not-allowed; transform: none; background: var(--surface); border-color: var(--border-strong);
}
.btn svg, .tool button svg { width: 18px; height: 18px; flex: none; }

/* The one filled button: "Choose a model file" (a <label> styled as a button). */
.btn-primary, .tool .btn-primary {
  background: var(--accent); border-color: var(--accent); color: var(--on-accent);
  min-height: 48px; padding: var(--s2) var(--s5); font-size: var(--text-md);
}
.btn-primary:hover, .tool .btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--on-accent); }

/* Quiet: text-only, for Clear, Show all, Hide all. */
.tool .btn-quiet { border-color: transparent; background: transparent; color: var(--accent-text); padding-inline: var(--s3); }
.tool .btn-quiet:hover { background: var(--accent-tint); border-color: transparent; }

/* Toggle buttons (X-ray, Measure, section axes). Pressed = tinted, not filled. */
.tool button[aria-pressed="true"] {
  background: var(--accent-tint); border-color: var(--accent); color: var(--accent-text);
  box-shadow: inset 0 0 0 1px var(--accent);
}

/* The template's .actions rule fills every button; here they are secondary by default. */
.actions { display: flex; flex-wrap: wrap; gap: var(--s2); margin: 0; }
.actions button { background: var(--surface); color: var(--text); border: 1px solid var(--border-strong); }

/* ---------- Viewer: rail + stage ---------- */

.tool {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  height: clamp(520px, 74vh, 780px);
  margin: 0 0 var(--s6);
  border: 1px solid var(--border); border-radius: var(--r-l);
  background: var(--surface); box-shadow: var(--shadow-1);
  overflow: hidden;
}
.rail {
  display: flex; flex-direction: column; gap: var(--s4);
  padding: var(--s4); overflow-y: auto; overscroll-behavior: contain;
  border-right: 1px solid var(--border); background: var(--surface);
  font-size: var(--text-sm); line-height: 1.45;
}
.stage { position: relative; min-height: 0; background: var(--stage); }
.stage canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.stage:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; }

@media (max-width: 760px) {
  header, main, footer { padding-inline: var(--s4); }
  .tool { grid-template-columns: 1fr; height: auto; margin-inline: calc(-1 * var(--s2)); }
  .stage { order: -1; aspect-ratio: 1 / 1; max-height: 68vh; width: 100%; }
  .rail { border-right: 0; border-top: 1px solid var(--border); overflow: visible; }
}

/* Group label: small caps-style label, set apart by weight and tracking. */
.group { display: flex; flex-direction: column; gap: var(--s2); padding-top: var(--s4); border-top: 1px solid var(--border); }
.group-label, .group legend {
  margin: 0; padding: 0;
  font-size: var(--text-xs); font-weight: 700; letter-spacing: 0.07em; text-transform: uppercase; color: var(--text);
  display: flex; justify-content: space-between; align-items: baseline; gap: var(--s2); width: 100%;
}
.group-label .count, .group-label output { font-family: var(--mono); font-weight: 500; letter-spacing: 0; text-transform: none; color: var(--muted); font-variant-numeric: tabular-nums; }
fieldset.group { border: 0; border-top: 1px solid var(--border); margin: 0; padding: var(--s4) 0 0; min-width: 0; }
.hint { margin: 0; color: var(--muted); font-size: var(--text-xs); line-height: 1.5; }
.seg { display: flex; flex-wrap: wrap; gap: var(--s2); }
.seg button { flex: 1 1 0; min-width: 44px; padding-inline: var(--s3); }

/* ---------- Drop zone ---------- */

.drop {
  display: flex; flex-direction: column; align-items: center; gap: var(--s2);
  margin: 0; padding: var(--s5) var(--s4);
  border: 2px dashed var(--border-strong); border-radius: var(--r-l);
  background: var(--raised); text-align: center;
  transition: border-color var(--fast) var(--ease), background-color var(--fast) var(--ease);
}
.drop .drop-icon { width: 40px; height: 40px; color: var(--muted); }
.drop .drop-title { margin: 0; font-weight: 700; font-size: var(--text-md); color: var(--text); }
.drop .drop-hint { margin: 0 0 var(--s2); color: var(--muted); font-size: var(--text-xs); }
.drop.over, .stage.over { border-color: var(--accent); background: var(--accent-tint); }
.stage.over { box-shadow: inset 0 0 0 3px var(--accent); }
/* After a model opens the drop zone shrinks to one row. */
.drop.compact { flex-direction: row; justify-content: space-between; padding: var(--s3); border-width: 1px; border-style: solid; border-color: var(--border); }
.drop.compact .drop-icon, .drop.compact .drop-title, .drop.compact .drop-hint { display: none; }
.drop.compact .btn-primary { background: var(--surface); color: var(--text); border-color: var(--border-strong); min-height: 44px; font-size: var(--text-sm); padding-inline: var(--s4); flex: 1; }
.drop.compact .btn-primary:hover { background: var(--raised); border-color: var(--text); }
.visually-hidden, .drop input[type="file"] {
  position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
/* A focused hidden file input lights its label button. */
.drop input[type="file"]:focus-visible + .btn-primary, .drop .btn-primary:focus-within { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- Status ---------- */

#status {
  min-height: calc(3 * 1.45em + 2 * var(--s2));
  margin: 0; padding: var(--s2) var(--s3);
  border-left: 4px solid transparent; border-radius: var(--r-s);
  font-size: var(--text-sm); font-weight: 500; color: var(--text);
  overflow-wrap: anywhere;
}
#status:empty { min-height: 0; padding: 0; border: 0; }
#status.busy { display: flex; gap: var(--s2); align-items: baseline; color: var(--text); }
#status.busy::before {
  content: ""; flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--accent);
  transform: translateY(-1px); animation: cp-pulse 1s var(--ease) infinite alternate;
}
#status.error { color: var(--danger); background: var(--danger-tint); border-left-color: var(--danger); }
#status.ok { color: var(--success); background: var(--success-tint); border-left-color: var(--success); }
#status.note { color: var(--warning); background: var(--warning-tint); border-left-color: var(--warning); }
@keyframes cp-pulse { from { opacity: 0.35; transform: translateY(-1px) scale(0.8); } to { opacity: 1; transform: translateY(-1px) scale(1.1); } }

/* ---------- Model readout + the dimension tag ---------- */

.readout {
  display: flex; flex-direction: column; gap: var(--s2);
  padding: var(--s3); border-radius: var(--r-m); background: var(--raised); border: 1px solid var(--border);
}
.readout .file-name {
  margin: 0; font-weight: 700; font-size: var(--text-sm); color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.readout .file-meta { margin: 0; color: var(--muted); font-size: var(--text-xs); font-variant-numeric: tabular-nums; }
.extents { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: var(--s1) var(--s3); margin: 0; align-items: center; }
.extents dt { font: 700 var(--text-xs)/1 var(--mono); color: var(--muted); }
.extents dd { margin: 0; }
.units { margin: 0; font-size: var(--text-xs); color: var(--muted); }
.units strong { color: var(--text); }

/* Signature: a number between two orange ticks, joined by a hairline. */
.dim {
  display: flex; align-items: center; gap: var(--s2);
  font-family: var(--mono); font-variant-numeric: tabular-nums; color: var(--text);
  white-space: nowrap;
}
.dim::before, .dim::after {
  content: ""; flex: 1 1 12px; min-width: 12px; height: 12px;
  background: linear-gradient(var(--accent), var(--accent)) center / 100% 1px no-repeat;
}
.dim::before { border-left: 2px solid var(--accent); }
.dim::after { border-right: 2px solid var(--accent); }
.dim .unit { color: var(--muted); font-size: 0.8em; }
.dim-lg { font-size: var(--text-xl); font-weight: 700; letter-spacing: -0.01em; }

/* ---------- Ranges, checkboxes, parts ---------- */

input[type="range"] { width: 100%; min-height: 44px; margin: 0; accent-color: var(--accent); background: transparent; cursor: pointer; }
input[type="checkbox"] { width: 20px; height: 20px; margin: 0; accent-color: var(--accent); cursor: pointer; flex: none; }
.range-row { display: flex; flex-direction: column; gap: 0; }
.range-row output { font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: var(--text-xs); color: var(--muted); }

.parts { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--border); }
.parts li { margin: 0; border-bottom: 1px solid var(--border); }
.parts label {
  display: flex; align-items: center; gap: var(--s3); min-height: 44px; padding: 0 var(--s2);
  cursor: pointer; border-radius: var(--r-s); transition: background-color var(--fast) var(--ease);
}
.parts label:hover { background: var(--raised); }
.parts .part-name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.parts .part-tris { font-family: var(--mono); font-size: var(--text-xs); color: var(--muted); font-variant-numeric: tabular-nums; }
.parts input:not(:checked) ~ .part-name { color: var(--muted); text-decoration: line-through; text-decoration-color: var(--border-strong); }

/* Measure panel */
.measure-result { display: flex; flex-direction: column; gap: var(--s2); padding: var(--s3); border-radius: var(--r-m); background: var(--accent-tint); border: 1px solid var(--accent); }
.measure-result .deltas { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--s2); margin: 0; font-family: var(--mono); font-size: var(--text-xs); font-variant-numeric: tabular-nums; color: var(--text); }
.measure-result .deltas dt { color: var(--muted); }
.measure-result .deltas dd { margin: 0; overflow-wrap: anywhere; }

/* ---------- Stage overlays ---------- */

.stage-msg {
  position: absolute; inset: 0; display: grid; place-items: center; align-content: center; gap: var(--s3);
  padding: var(--s5); text-align: center; color: var(--muted); pointer-events: none;
}
.stage-msg svg { width: 120px; height: 120px; color: var(--border-strong); }
.stage-msg p { margin: 0; max-width: 32ch; font-size: var(--text-md); }
.stage-msg strong { color: var(--text); font-size: var(--text-lg); }
.stage-busy {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  min-width: 260px; padding: var(--s4) var(--s5);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-m); box-shadow: var(--shadow-2);
  font-size: var(--text-sm); color: var(--text);
}
.stage-busy p { margin: 0 0 var(--s3); }
.bar { height: 4px; border-radius: 2px; background: var(--border); overflow: hidden; }
.bar i { display: block; width: 40%; height: 100%; background: var(--accent); border-radius: 2px; animation: cp-slide 1.1s var(--ease) infinite; }
@keyframes cp-slide { from { transform: translateX(-100%); } to { transform: translateX(250%); } }

.hud {
  position: absolute; left: var(--s3); right: var(--s3); bottom: var(--s3);
  display: flex; justify-content: space-between; align-items: flex-end; gap: var(--s2);
  pointer-events: none; font-size: var(--text-xs); color: var(--muted);
}
.hud .keys { margin: 0; padding: var(--s1) var(--s2); background: var(--surface); border-radius: var(--r-s); opacity: 0.92; }
.hud-dim {
  min-width: 220px; padding: var(--s2) var(--s4);
  background: var(--surface); border: 1px solid var(--accent); border-radius: var(--r-m); box-shadow: var(--shadow-2);
}
.hud-dim .dim { font-size: var(--text-lg); font-weight: 700; }
.stage-tag {
  position: absolute; top: var(--s3); left: var(--s3);
  padding: var(--s1) var(--s2); border-radius: var(--r-s);
  background: var(--surface); border: 1px solid var(--border);
  font: 600 var(--text-xs)/1.4 var(--mono); color: var(--text); pointer-events: none;
}
.stage-tag.mode-measure { border-color: var(--accent); color: var(--accent-text); }
.reticle {
  position: absolute; width: 32px; height: 32px; margin: -16px 0 0 -16px; pointer-events: none;
  border: 2px solid var(--accent); border-radius: 50%;
}
.reticle::before, .reticle::after { content: ""; position: absolute; background: var(--accent); }
.reticle::before { left: 50%; top: -8px; bottom: -8px; width: 2px; margin-left: -1px; }
.reticle::after { top: 50%; left: -8px; right: -8px; height: 2px; margin-top: -1px; }

/* Results arriving */
.readout, .measure-result, .hud-dim { animation: cp-in 180ms var(--ease) both; }
@keyframes cp-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

[hidden] { display: none !important; }

/* ---------- Template parts ---------- */

.ad-slot { background: var(--raised); border: 1px solid var(--border); }
.ad-label { color: var(--muted); opacity: 1; letter-spacing: 0.06em; text-transform: uppercase; font-weight: 700; }
.house-card {
  padding: var(--s4); border-radius: var(--r-m); background: var(--surface);
  border: 1px solid var(--border); border-left: 4px solid var(--accent); color: var(--text);
  transition: border-color var(--fast) var(--ease);
}
.house-card:hover { border-color: var(--accent); }
.house-card strong { color: var(--accent-text); }
.house-card span { color: var(--muted); }
.consent { background: var(--surface); color: var(--text); border: 1px solid var(--border); box-shadow: var(--shadow-2); border-radius: var(--r-l); }
.consent button { flex: 1; }
footer { opacity: 1; color: var(--muted); font-size: var(--text-sm); border-top: 1px solid var(--border); margin-top: var(--s7); }
footer a { color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .bar i { width: 100%; opacity: 0.5; }
}
