:root {
  /* A tailor's cutting mat: deep green with a faint printed grid. */
  --mat: #102520;
  --stage: #0b1a16;
  --panel: #15302a;
  --panel-2: #1c3a33;
  --rule: #2a4a41;
  --grid: rgba(173, 214, 196, 0.05);
  --grid-major: rgba(173, 214, 196, 0.09);
  --chalk: #eef1e9;
  --muted: #a3b8af;
  --faint: #6f8a80;
  --tape: #f5c73b;
  --tape-ink: #2a2106;
  --blue: #7cc0ff;
  --red: #ff6b5b;
  --text: "Archivo", system-ui, -apple-system, sans-serif;
  --r-lg: 14px;
  --r-md: 9px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }

body {
  margin: 0;
  font: 400 15px/1.45 var(--text);
  font-stretch: 100%;
  color: var(--chalk);
  background-color: var(--mat);
  background-image:
    linear-gradient(var(--grid-major) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-major) 1px, transparent 1px),
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 120px 120px, 120px 120px, 24px 24px, 24px 24px;
  -webkit-font-smoothing: antialiased;
}

button, select, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--tape); outline-offset: 2px; }
h1, h2 { margin: 0; }

/* Numbers and headings use Archivo's condensed cut, like the print on a tape measure. */
.num, .wordmark, .empty h1, .guide-title, .bf-value, .results-head h1 {
  font-stretch: 62%;
  font-variant-numeric: tabular-nums;
}

/* ---------- Layout ---------- */

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 14px 18px;
  height: 100dvh;
  padding: 14px 18px 18px;
}

.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: baseline; gap: 14px; min-width: 0; }
.wordmark {
  font-size: 32px;
  font-weight: 800;
  line-height: 1;
  letter-spacing: 0.01em;
  padding: 2px 10px 3px;
  background: var(--tape);
  color: var(--tape-ink);
  border-radius: 3px;
  /* Tape-measure ticks along the bottom edge. */
  background-image: repeating-linear-gradient(90deg, var(--tape-ink) 0 1px, transparent 1px 7px);
  background-size: 100% 5px;
  background-repeat: no-repeat;
  background-position: bottom left;
  background-color: var(--tape);
}
.model-status { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.model-status[data-state="error"] { color: var(--red); }
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.main { display: flex; flex-direction: column; gap: 12px; min-height: 0; min-width: 0; }

/* ---------- Controls ---------- */

.segmented {
  display: inline-flex;
  padding: 3px;
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: 999px;
}
.segmented.wide { display: flex; }
.segmented.wide button { flex: 1; }
.segmented button {
  border: 0;
  background: none;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
}
.segmented button[aria-checked="true"] { background: var(--chalk); color: var(--stage); }

select {
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 14px;
  max-width: 240px;
}

.icon-btn {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 1px solid var(--rule);
  background: var(--panel);
  cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }

.primary-btn, .ghost-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
  white-space: nowrap;
}
.primary-btn { background: var(--tape); color: var(--tape-ink); border: 0; }
.primary-btn:hover { background: #ffd65a; }
.primary-btn:disabled { opacity: 0.4; cursor: default; }
.ghost-btn { background: transparent; border: 1px solid var(--rule); }
.ghost-btn:hover { border-color: var(--muted); }
.ghost-btn:disabled { opacity: 0.4; cursor: default; }
.link-btn { background: none; border: 0; padding: 4px 2px; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; cursor: pointer; font-size: 14px; }

.controls { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.control-meta { margin: 0 0 0 auto; font-size: 13px; color: var(--faint); }

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

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  border-radius: var(--r-lg);
  background: var(--stage);
  border: 1px solid var(--rule);
  overflow: hidden;
}
#stageCanvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.src-video { position: absolute; width: 2px; height: 2px; opacity: 0; pointer-events: none; }

.empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  gap: 14px;
  overflow-y: auto;
}
.empty h1 { font-size: clamp(34px, 4.4vw, 56px); font-weight: 800; line-height: 0.95; max-width: 16ch; }
.empty p { max-width: 54ch; margin: 0; color: var(--muted); }
.empty-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin-top: 6px; }
.empty-error { color: var(--red) !important; }

.hero-figure { width: min(210px, 42%); height: auto; overflow: visible; flex: none; }
.hero-figure .body { fill: none; stroke: var(--chalk); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.hero-figure .tape { fill: var(--tape); }
.hero-figure .tape-ticks { stroke: var(--tape-ink); stroke-width: 1.2; }
.hero-figure .ring path { fill: none; stroke: var(--tape); stroke-width: 4; }
.hero-figure .ring path.back { stroke-width: 1.5; stroke-dasharray: 3 3; opacity: 0.6; }

@media (prefers-reduced-motion: no-preference) {
  .hero-figure .ring { animation: breathe 3.2s ease-in-out infinite; transform-origin: 120px 120px; }
  .hero-figure .ring:nth-of-type(2) { animation-delay: 0.25s; }
  .hero-figure .ring:nth-of-type(3) { animation-delay: 0.5s; }
  @keyframes breathe { 50% { transform: scaleX(1.06); } }
}

.guide {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  width: min(720px, calc(100% - 32px));
  padding: 14px 20px 16px;
  border-radius: var(--r-lg);
  background: rgba(11, 26, 22, 0.86);
  text-align: center;
}
.guide-step { margin: 0; font-size: 14px; color: var(--tape); font-weight: 600; }
.guide-title { font-size: clamp(30px, 3.6vw, 48px); font-weight: 800; line-height: 1; margin-top: 4px; }
.guide-checks { list-style: none; margin: 12px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px 14px; justify-content: center; font-size: 14px; }
.guide-checks li { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.guide-checks li::before { content: ""; width: 9px; height: 9px; border-radius: 50%; border: 1.5px solid var(--faint); }
.guide-checks li[data-ok="true"] { color: var(--chalk); }
.guide-checks li[data-ok="true"]::before { background: var(--tape); border-color: var(--tape); }

.hold {
  position: absolute;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  width: 120px;
  height: 120px;
  display: grid;
  place-items: center;
}
.hold svg { position: absolute; inset: 0; transform: rotate(-90deg); }
.hold circle { fill: rgba(11, 26, 22, 0.8); stroke-width: 7; }
.hold-track { stroke: var(--rule); }
.hold-fill { fill: none !important; stroke: var(--tape); stroke-linecap: round; stroke-dasharray: 326.7; stroke-dashoffset: 326.7; }
.hold span { position: relative; font-size: 14px; font-weight: 600; text-align: center; line-height: 1.2; max-width: 80px; }

.hint {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  margin: 0;
  padding: 8px 16px;
  border-radius: 999px;
  background: rgba(11, 26, 22, 0.88);
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  max-width: calc(100% - 32px);
  overflow: hidden;
  text-overflow: ellipsis;
}

.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash[data-on="true"] { animation: shutter 0.6s ease-out; }
@keyframes shutter { from { opacity: 0.75; } to { opacity: 0; } }

/* ---------- Rail ---------- */

.rail { display: flex; flex-direction: column; gap: 12px; min-height: 0; overflow-y: auto; }
.panel {
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: var(--r-lg);
  padding: 16px;
}
.panel h2 { font-size: 15px; font-weight: 600; margin-bottom: 12px; }
.panel-note { margin: 10px 0 0; font-size: 13px; color: var(--faint); }
.panel-note.warn { color: var(--tape); }

.profile { display: grid; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.field-label { font-size: 13px; color: var(--muted); }
.field small { color: var(--faint); font-size: 12px; }
.field select { max-width: none; border-radius: var(--r-md); }
.input-unit { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.input-unit input {
  width: 84px;
  padding: 7px 10px;
  border-radius: var(--r-md);
  border: 1px solid var(--rule);
  background: var(--stage);
  color: var(--chalk);
  font-variant-numeric: tabular-nums;
}
.input-unit input[aria-invalid="true"] { border-color: var(--red); }
.field-error { margin: 0; color: var(--red); font-size: 13px; }

.insets { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.insets figure { margin: 0; min-width: 0; }
.insets canvas { width: 100%; aspect-ratio: 1; display: block; border-radius: var(--r-md); background: var(--stage); border: 1px solid var(--rule); }
.insets figcaption { margin-top: 5px; font-size: 12px; color: var(--muted); line-height: 1.3; }
.insets figcaption span { display: block; color: var(--chalk); font-variant-numeric: tabular-nums; }

.steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; counter-reset: step; }
.steps li { display: grid; grid-template-columns: 28px 1fr; grid-template-rows: auto auto; column-gap: 10px; counter-increment: step; }
.steps li::before {
  content: counter(step);
  grid-row: 1 / 3;
  width: 28px; height: 28px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 1.5px solid var(--rule);
  font-weight: 700;
  font-size: 14px;
  color: var(--muted);
}
.steps li[data-state="active"]::before { border-color: var(--tape); color: var(--tape); }
.steps li[data-state="done"]::before { content: "✓"; background: var(--tape); border-color: var(--tape); color: var(--tape-ink); }
.step-name { font-weight: 600; font-size: 14px; }
.step-desc { font-size: 13px; color: var(--muted); }

/* ---------- Results ---------- */

.results {
  position: fixed;
  inset: 0;
  overflow-y: auto;
  background: inherit;
  background-color: var(--mat);
  padding: 26px max(18px, calc((100vw - 1200px) / 2)) 56px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.results > * { flex-shrink: 0; }
.results-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 18px; flex-wrap: wrap; }
.results-date { margin: 0 0 4px; color: var(--muted); font-size: 14px; }
.results-head h1 { font-size: clamp(38px, 5vw, 64px); font-weight: 800; line-height: 0.95; }
.results-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.result-hero {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr);
  gap: 18px;
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: var(--r-lg);
  padding: 22px 24px;
}
.bf-label { margin: 0; color: var(--muted); font-size: 14px; }
.bf-value { margin: 2px 0 0; font-size: clamp(88px, 11vw, 140px); font-weight: 800; line-height: 0.85; color: var(--tape); }
.bf-value .pct { font-size: 0.42em; margin-left: 4px; color: var(--chalk); }
.bf-range { margin: 12px 0 0; font-size: 16px; }
.bf-range b { color: var(--tape); }

.bf-scale { position: relative; margin-top: 22px; height: 58px; }
.bf-bands { position: absolute; left: 0; right: 0; top: 18px; height: 16px; display: flex; gap: 2px; }
.bf-bands span { height: 100%; background: var(--panel-2); border-radius: 2px; }
.bf-bands span[data-active="true"] { background: rgba(245, 199, 59, 0.3); }
.bf-band-labels { position: absolute; left: 0; right: 0; top: 40px; font-size: 12px; color: var(--muted); }
.bf-band-labels span { position: absolute; transform: translateX(-50%); white-space: nowrap; }
.bf-band-labels span[data-active="true"] { color: var(--chalk); font-weight: 600; }
.bf-range-bar { position: absolute; top: 14px; height: 24px; border: 2px solid var(--tape); border-radius: 4px; }
.bf-marker { position: absolute; top: 0; width: 2px; height: 42px; background: var(--tape); }
.bf-marker::before { content: ""; position: absolute; top: -2px; left: -5px; border: 6px solid transparent; border-top-color: var(--tape); }

.composition { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 18px; margin: 0; align-content: center; }
.composition dt { font-size: 13px; color: var(--muted); }
.composition dd { margin: 2px 0 0; font-size: 32px; font-weight: 700; line-height: 1; font-stretch: 62%; font-variant-numeric: tabular-nums; }
.composition dd small { display: block; font-size: 13px; font-weight: 500; font-stretch: 100%; color: var(--muted); margin-top: 4px; }

.photos { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.photos figure { margin: 0; min-width: 0; }
.photos canvas { display: block; width: 100%; max-height: 78vh; object-fit: contain; background: var(--stage); border-radius: var(--r-lg); border: 1px solid var(--rule); }
.photos figcaption { margin-top: 8px; font-size: 13px; color: var(--muted); }

.results-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 18px; align-items: start; }

.measure-table, .history-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.measure-table th, .measure-table td, .history-table th, .history-table td { padding: 8px 0; border-bottom: 1px solid var(--rule); text-align: left; font-size: 15px; }
.measure-table th { font-weight: 500; color: var(--muted); }
.measure-table td { text-align: right; font-weight: 600; }
.measure-table td small, .history-table td small { font-weight: 500; color: var(--muted); margin-left: 6px; font-size: 13px; }
.measure-table tr:last-child > * { border-bottom: 0; }
.history-table th { font-size: 13px; font-weight: 500; color: var(--muted); }
.history-table td:not(:first-child), .history-table th:not(:first-child) { text-align: right; }

.methods { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.methods li { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; }
.methods b { font-weight: 600; }
.methods span { font-size: 13px; color: var(--muted); grid-column: 1; }
.methods strong { grid-row: 1 / 3; grid-column: 2; align-self: center; font-size: 30px; font-weight: 700; font-stretch: 62%; font-variant-numeric: tabular-nums; }

.calibrate { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--rule); display: grid; gap: 8px; font-size: 14px; }
.calibrate-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.history-head { display: flex; justify-content: space-between; align-items: baseline; }
.history-table[hidden] { display: none; }
.save-status { margin: 10px 0 0; font-size: 14px; color: var(--muted); }
.save-status[data-state="saved"] { color: var(--chalk); }
.save-status[data-state="error"] { color: var(--red); }
.save-status a, .field.github a { color: var(--tape); }

/* ---------- Dialogs ---------- */

.dialog {
  width: min(480px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: var(--r-lg);
  background: var(--panel);
  color: var(--chalk);
}
.dialog::backdrop { background: rgba(4, 12, 10, 0.65); }
.dialog form { display: flex; flex-direction: column; gap: 16px; padding: 22px; }
.dialog h2 { font-size: 30px; font-weight: 800; font-stretch: 62%; line-height: 1; }
.dialog p { margin: 0; color: var(--muted); font-size: 14px; }
.check { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.check input { width: 18px; height: 18px; accent-color: var(--tape); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; }
.field.github { padding-top: 14px; border-top: 1px solid var(--rule); }
.field.github code { font-size: 12px; color: var(--chalk); }
.gh-status { margin: 0 !important; color: var(--chalk) !important; }
.gh-connect { display: flex; gap: 8px; }
.gh-connect input { flex: 1; min-width: 0; padding: 8px 12px; border-radius: var(--r-md); border: 1px solid var(--rule); background: var(--stage); color: var(--chalk); }
.dialog-error { color: var(--red) !important; }

.photo-picks { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.photo-pick {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  border-radius: var(--r-md);
  border: 1px dashed var(--rule);
  background: var(--stage);
  text-align: left;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
}
.photo-pick b { color: var(--chalk); font-weight: 600; }
.photo-pick .thumb { aspect-ratio: 3 / 4; border-radius: 6px; background: var(--panel-2) center / cover no-repeat; display: grid; place-items: center; }
.photo-pick .thumb:not([data-set])::after { content: "+"; font-size: 32px; color: var(--faint); }

/* ---------- Narrow screens ---------- */

@media (max-width: 600px) {
  .bf-band-labels span:not([data-active="true"]) { display: none; }
}

@media (max-width: 960px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto auto auto; height: auto; min-height: 100dvh; }
  .stage { flex: none; height: 70vh; }
  .rail { overflow: visible; }
  .control-meta { display: none; }
  .result-hero, .results-grid, .photos { grid-template-columns: 1fr; }
}
