:root {
  --bg: #f6f3ee;
  --panel: #fffdf9;
  --ink: #23201c;
  --muted: #6f675d;
  --line: #e4ddd2;
  --accent: #c2410c;
  --accent-ink: #fff;
  --accent-soft: #fbe8dc;
  --track: #e8e1d6;
  --warn-bg: #fff4d6;
  --warn-ink: #7a5300;
  --err-bg: #fde4e1;
  --err-ink: #9b1c12;
  --viewer: #ece7df;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171513; --panel: #201d1a; --ink: #eee8e0; --muted: #a39a8e; --line: #36312b;
    --accent: #f08a4b; --accent-ink: #1b120c; --accent-soft: #3a2619; --track: #3a342e;
    --warn-bg: #3a2f12; --warn-ink: #f3d27a; --err-bg: #3d1c18; --err-ink: #ffb4aa;
    --viewer: #1d1a17; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #171513; --panel: #201d1a; --ink: #eee8e0; --muted: #a39a8e; --line: #36312b;
  --accent: #f08a4b; --accent-ink: #1b120c; --accent-soft: #3a2619; --track: #3a342e;
  --warn-bg: #3a2f12; --warn-ink: #f3d27a; --err-bg: #3d1c18; --err-ink: #ffb4aa;
  --viewer: #1d1a17; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
}
button, input, select { font: inherit; color: inherit; }

.topbar {
  display: flex; align-items: baseline; gap: 16px; flex-wrap: wrap;
  padding: 12px 20px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand svg { width: 26px; height: 26px; fill: var(--accent); align-self: center; }
.brand svg .hole { fill: var(--panel); }
.brand h1 { font-size: 18px; margin: 0; letter-spacing: -0.01em; }
.tagline { color: var(--muted); font-size: 14px; }

.layout { flex: 1; display: grid; grid-template-columns: 360px 1fr; min-height: 0; }
.panel { background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; padding: 8px 20px 24px; }
.panel section { padding: 14px 0 10px; border-bottom: 1px solid var(--line); }
.panel h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 0 0 10px; }

.field, .slider { margin-bottom: 14px; }
.field label, .slider label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 6px; }
label small, .hint { font-weight: 400; color: var(--muted); font-size: 12.5px; }
.hint { display: block; margin-top: 4px; }

.slider .row { display: flex; align-items: center; gap: 10px; }
.slider input[type=range] { flex: 1; min-width: 0; accent-color: var(--accent); height: 22px; }
.slider .num { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 7px; background: var(--bg); }
.slider .num input { width: 64px; border: 0; background: transparent; padding: 5px 4px 5px 8px; text-align: right; }
.slider .num input:focus { outline: none; }
.slider .num:focus-within { border-color: var(--accent); }
.slider .num span { color: var(--muted); font-size: 12.5px; padding-right: 8px; }

input[type=text], select {
  width: 100%; border: 1px solid var(--line); border-radius: 7px; background: var(--bg); padding: 7px 10px;
}
input[type=text]:focus, select:focus { outline: none; border-color: var(--accent); }

.seg { display: flex; background: var(--track); border-radius: 8px; padding: 3px; gap: 2px; }
.seg button {
  flex: 1; border: 0; background: transparent; padding: 6px 8px; border-radius: 6px; cursor: pointer;
  font-size: 13.5px; color: var(--muted); white-space: nowrap;
}
.seg button.on { background: var(--panel); color: var(--ink); font-weight: 600; box-shadow: 0 1px 2px rgb(0 0 0 / .12); }
.seg.small button { padding: 4px 10px; font-size: 13px; }

.measure-note { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
.measure-note svg { width: 52px; flex: none; }
.measure-note .hx { fill: none; stroke: var(--muted); stroke-width: 2; }
.measure-note .hx.in { stroke-width: 1.5; }
.measure-note .arrow { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.measure-note p { margin: 0; font-size: 13px; color: var(--muted); }
.measure-note b { color: var(--ink); font-weight: 600; }
.derived { margin: -4px 0 14px; font-size: 13px; color: var(--muted); }
.derived b { color: var(--ink); font-weight: 600; }

.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 6px 0; font-size: 14px; }
.link:hover { text-decoration: underline; }

.stage { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.viewer { position: relative; flex: 1; min-height: 320px; background: var(--viewer); }
.viewer canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.toolbar { position: absolute; top: 12px; left: 12px; right: 12px; display: flex; gap: 8px; flex-wrap: wrap; pointer-events: none; }
.toolbar > * { pointer-events: auto; }
.toolbar .seg { background: color-mix(in srgb, var(--panel) 80%, transparent); backdrop-filter: blur(6px); box-shadow: 0 1px 3px rgb(0 0 0 / .1); }
.toolbar #camera { margin-left: auto; }
.toolbar select.small { width: auto; padding: 4px 8px; font-size: 13px; background: var(--panel); }
.status {
  position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%);
  background: var(--panel); color: var(--muted); font-size: 13px; padding: 4px 12px; border-radius: 99px;
  box-shadow: 0 1px 3px rgb(0 0 0 / .15); opacity: 0; transition: opacity .15s; pointer-events: none;
}
.status.show { opacity: 1; }

.results {
  display: grid; grid-template-columns: 1fr auto; gap: 16px 24px; padding: 16px 20px;
  background: var(--panel); border-top: 1px solid var(--line);
}
.summary p { margin: 0; }
#summary b { font-weight: 600; }
.warnings { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 6px; }
.warnings li, .error { background: var(--warn-bg); color: var(--warn-ink); padding: 6px 10px; border-radius: 7px; font-size: 13.5px; }
.error { background: var(--err-bg); color: var(--err-ink); margin-top: 8px !important; }

.downloads { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; min-width: 220px; }
.primary {
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 8px; padding: 10px 18px;
  font-weight: 600; cursor: pointer; width: 100%;
}
.primary:disabled { opacity: .5; cursor: default; }
.primary:not(:disabled):hover { filter: brightness(1.08); }
.piecelist { display: flex; flex-wrap: wrap; gap: 4px; justify-content: flex-end; max-width: 340px; }
.piecelist button {
  border: 1px solid var(--line); background: var(--bg); border-radius: 6px; padding: 3px 8px; font-size: 12.5px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 5px;
}
.piecelist button:hover { border-color: var(--accent); }
.piecelist i { width: 9px; height: 9px; border-radius: 2px; display: inline-block; }

@media (max-width: 860px) {
  body { height: auto; }
  .layout { grid-template-columns: 1fr; }
  .stage { order: -1; }
  .viewer { height: 60vh; flex: none; }
  .panel { border-right: 0; padding: 4px 16px 24px; }
  .results { grid-template-columns: 1fr; padding: 14px 16px; }
  .downloads { align-items: stretch; }
  .piecelist { justify-content: flex-start; max-width: none; }
  .topbar { padding: 10px 16px; }
  .toolbar #camera { margin-left: 0; }
}
