:root {
  --bg: #14171c;
  --panel: #1b1f26;
  --panel-2: #232833;
  --line: #2f3641;
  --text: #e6e9ef;
  --muted: #8b93a1;

  /* Theme colours. The 3D view and the section canvas read these through
     core/theme.js, so every colour still lives only here. */
  --accent: #10d154;      /* logo green: toolbar icons, active tiles, selection box */
  --accent-2: #ffcc47;    /* landmark markers in the 3D view */
  --section: #6ee7a8;     /* measurement readouts */
  --xsec: #10d154;        /* cross-section contour, 3D and 2D panel alike — the logo green */
  --xsec-dim: #077737;    /* the unselected contours behind it */
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

#app {
  display: grid;
  grid-template-columns: 1fr clamp(520px, 30vw, 760px);   /* panel scales with the screen */
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "toolbar toolbar"
    "viewport side";
  height: 100vh;
}

/* Top bar: menu bar + icon toolbar (MeshLab-style) */
#topbar {
  grid-area: toolbar;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}
#menubar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px 8px;
  border-bottom: 1px solid var(--line);
}
/* The logo carries the app name, so no text accompanies it. Light ink, for a
   dark ground. */
.brand-logo {
  background: url("assets/Logo.png") center / contain no-repeat;
  flex: 0 0 auto;
}
/* Below ~40px tall the wordmark inside the logo stops being readable. */
.app-title { margin: 0 12px 0 2px; height: 52px; width: 156px; }

.menu { position: relative; }
.menu-title {
  padding: 5px 12px; background: none; border: none; color: var(--text);
  cursor: pointer; font: inherit; font-size: 13px; border-radius: 6px;
}
.menu-title:hover, .menu.open > .menu-title { background: var(--panel-2); }
.menu-dd {
  display: none; position: absolute; top: 100%; left: 0; margin-top: 5px;
  min-width: 200px; padding: 5px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 12px 34px rgba(0, 0, 0, .55); z-index: 300;
}
.menu.open > .menu-dd { display: block; }
.menu-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 7px 10px; background: none; border: none; color: var(--text);
  cursor: pointer; font: inherit; font-size: 13px; text-align: left;
  border-radius: 6px; white-space: nowrap;
}
.menu-item:hover { background: var(--panel-2); }
.menu-item:disabled { opacity: .4; cursor: default; }
.menu-item.active { color: var(--accent); }
.menu-item.active::after { content: '●'; margin-left: auto; font-size: 8px; }
.mi-ic { width: 18px; text-align: center; }
.mi-key { margin-left: auto; color: var(--muted); font-size: 11px; }
.mi-check { width: 14px; color: var(--accent); visibility: hidden; }
/* Every row reserves the SAME leading slot, so labels line up whether the item is a
   plain command, a checkable toggle (.mi-check), or carries an emoji (.mi-ic). */
.menu-item > .mi-check, .menu-item > .mi-ic { flex: 0 0 16px; width: 16px; text-align: center; }
.menu-item:not(:has(> .mi-check)):not(:has(> .mi-ic))::before { content: ''; flex: 0 0 16px; }
.menu-item.check:has(input:checked) .mi-check { visibility: visible; }
.menu-sep { height: 1px; background: var(--line); margin: 5px 6px; }
/* A labelled control row in the Settings panel — a segmented choice that reads better
   than one menu item per option, so the selection can be seen changing. */
.menu-row {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 6px 10px; color: var(--muted); font-size: 13px; white-space: nowrap;
}
/* What the option above actually means — a setting whose effect is invisible in the viewer needs
   its definition spelled out where it is chosen. Wraps, unlike the row it explains. */
.menu-note {
  padding: 0 10px 8px;
  color: var(--muted); opacity: .75; font-size: 11.5px; line-height: 1.45; white-space: normal;
}
/* Move / Rotate segmented control inside the plane-cut confirm pop */
.cut-modes { display: flex; gap: 6px; margin: 9px 0 4px; }
.cut-modes .mode-btn { flex: 1; }
.cut-modes .mode-btn.active { background: var(--accent); border-color: var(--accent); color: #08213b; }

/* The keep-side row: explanation on the left, Flip on the right */
.cut-opt.cut-side { justify-content: space-between; gap: 10px; }
.cut-opt.cut-side b { color: var(--accent-2); font-weight: 600; }

/* Option row inside the plane-cut confirm pop */
.cut-opt { display: flex; align-items: center; gap: 7px; margin: 8px 0 2px; font-size: 12px; color: var(--muted); }
.cut-opt input { margin: 0; }

/* Nested flyout submenu (Fill holes → flat / curved) */
.menu-sub { position: relative; }
.menu-item.has-sub .mi-arrow { margin-left: auto; padding-left: 12px; color: var(--muted); font-size: 10px; }
.menu-sub > .submenu {
  display: none; position: absolute; top: -6px; left: 100%; margin-left: 3px;
  min-width: 170px; padding: 5px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 12px 34px rgba(0, 0, 0, .55); z-index: 310;
}
.menu-sub:hover > .submenu { display: block; }
.menu-sub:hover > .has-sub { background: var(--panel-2); }

/* Toolbar: labelled tiles, accent-coloured glyphs. Each button is a chip with the icon above its
   name, so the bar reads at a glance instead of relying on 20px monochrome shapes. */
#toolbar { display: flex; align-items: center; gap: 6px; padding: 5px 10px; }
.tbtn {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; width: 58px; height: 52px; padding: 0;
  background: var(--panel-2); border: 1px solid transparent;
  border-radius: 10px; color: var(--accent); cursor: pointer;
  font: inherit; font-size: 10px; letter-spacing: .2px;
}
.tbtn svg { width: 24px; height: 24px; }
/* Two-tone glyphs: what is being MEASURED is accent-coloured, the scaffolding that holds it is
   muted. On the active tile everything goes dark, so the shape still reads on the accent fill. */
.tbtn svg .dim { stroke: var(--text); }
.tbtn svg .dim-fill { fill: var(--text); }
.tbtn.active svg .dim { stroke: rgba(8,33,59,.72); }
.tbtn.active svg .dim-fill { fill: rgba(8,33,59,.72); }
.tbtn:disabled svg .dim { stroke: var(--text); }
.tbtn .tb-label { color: var(--text); font-size: 10px; line-height: 1; }
.tbtn:hover { background: #232b36; }
.tbtn:hover .tb-label { color: var(--text); }
.tbtn.active { background: var(--accent); color: #08213b; }
.tbtn.active .tb-label { color: #08213b; }
.tbtn:disabled { opacity: .38; cursor: default; }
.tbtn:disabled:hover { background: var(--panel-2); }
/* Auto is the primary action — a tinted, glowing ground so it reads as the
   button to press, without recolouring its icon or label. */
.tbtn.primary {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 20%, transparent);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--accent) 35%, transparent), 0 0 14px color-mix(in srgb, var(--accent) 22%, transparent);
}
.tbtn.primary .tb-label { color: var(--text); font-weight: 600; }
.tbtn.primary:hover {
  background: color-mix(in srgb, var(--accent) 34%, transparent);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--accent) 50%, transparent), 0 0 18px color-mix(in srgb, var(--accent) 34%, transparent);
}
.tsep { width: 1px; height: 34px; background: var(--line); margin: 0 5px; }

.sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
.hint { color: var(--muted); font-size: 12px; margin-left: 8px; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  cursor: pointer;
  user-select: none;
  font-size: 13px;
}
.btn:hover { border-color: #3d4757; }
.btn.active { background: var(--accent); border-color: var(--accent); color: #08213b; font-weight: 600; }
.btn.small { padding: 3px 9px; font-size: 12px; }
.btn.cut { border-color: color-mix(in srgb, var(--section) 42%, var(--panel)); color: var(--section); }
.btn.cut:hover { background: color-mix(in srgb, var(--section) 16%, var(--panel)); border-color: var(--section); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #08213b; font-weight: 600; }
.btn.primary:hover { background: #63b1ff; border-color: #63b1ff; }
.btn:disabled { opacity: .5; cursor: default; pointer-events: none; }
.btn.select {
  -webkit-appearance: none;
  appearance: none;
  padding-right: 12px;
  font-family: inherit;
}
.btn.select option { background: var(--panel-2); color: var(--text); }
.group { display: inline-flex; gap: 4px; }

.chk { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); cursor: pointer; }
.chk input { accent-color: var(--accent); }

/* Viewport */
#viewport { grid-area: viewport; position: relative; min-width: 0; overflow: hidden; }
/* Pin the WebGL canvas to the viewport box. The renderer uses setSize(w,h,false), so
   three.js sizes only the drawing BUFFER (w × devicePixelRatio) and never the canvas's
   CSS size — on a fractional-DPI display (Windows at 125%/150% scaling) that made the
   canvas DISPLAY wider than the viewport and spill over the right panel's 1st column.
   width/height:100% keeps display = viewport size at any DPI; the buffer still carries
   the extra resolution, so it stays crisp. */
#viewport canvas { display: block; width: 100%; height: 100%; }

/* Auto-measure progress overlay */
#progress {
  position: fixed;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: 340px;
  padding: 18px 20px 20px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, .55);
  z-index: 100;
}
#progress.hidden { display: none; }

/* Same panel treatment as #progress, so a message reads as part of the app. */
#notice {
  position: fixed;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: min(420px, 86vw);
  padding: 18px 20px 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, .55);
  z-index: 101;
}
#notice.hidden { display: none; }
#notice-title { color: var(--text); font-size: 14px; font-weight: 600; margin-bottom: 10px; }
#notice-body { color: var(--muted); font-size: 13px; line-height: 1.55; white-space: pre-line; }
.notice-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
#notice-extra { display: contents; }

/* Prominent step-by-step guide while picking the 3 landmark points */
#lm-guide {
  position: fixed;
  left: 50%; top: 64px;
  transform: translateX(-50%);
  width: min(440px, 90vw);
  padding: 14px 20px 16px;
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: 12px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, .5), 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
  z-index: 120;
  text-align: center;
  pointer-events: none;
  animation: lm-pop .18s ease-out;
}
#lm-guide.hidden { display: none; }
@keyframes lm-pop { from { opacity: 0; transform: translateX(-50%) translateY(-6px); } }
#lm-guide-head { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 8px; }
#lm-guide-step { font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted, #8a94a6); }
#lm-guide-dots { display: inline-flex; gap: 6px; }
#lm-guide-dots i { width: 8px; height: 8px; border-radius: 999px; background: var(--panel-2, #2a3140); border: 1px solid var(--line, #3a4557); }
#lm-guide-dots i.done { background: var(--accent); border-color: var(--accent); }
#lm-guide-dots i.active { background: var(--accent-2); border-color: var(--accent-2); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent-2) 25%, transparent); }
#lm-guide-name { font-size: 22px; font-weight: 700; color: var(--text); }
#lm-guide-desc { font-size: 13px; color: var(--text); opacity: .8; margin-top: 2px; }
#lm-guide-hint { font-size: 12px; color: var(--muted, #8a94a6); margin-top: 8px; }

/* Rubber-band rectangle for Box select (drawn over the viewport) */
.select-box {
  position: fixed;
  border: 1px solid var(--accent);
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  pointer-events: none;
  z-index: 250;
}
.select-box.hidden { display: none; }

/* Manual-adjustment confirm popup */
.confirm-pop {
  position: fixed;
  left: 50%; top: 74px;
  transform: translateX(-50%);
  z-index: 210;
  min-width: 300px; max-width: 420px;
  padding: 14px 16px;
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .55);
}
.confirm-pop.hidden { display: none; }
#adjust-confirm-msg { color: var(--text); font-size: 13px; line-height: 1.4; }
#adjust-confirm-msg b { color: var(--accent); }
#adjust-confirm-msg .confirm-val { color: var(--accent-2); font-weight: 600; font-variant-numeric: tabular-nums; }
.confirm-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
/* Scale-mesh dialog */
.scale-msg { color: var(--text); font-size: 13px; font-weight: 600; }
.scale-hint { color: var(--muted); font-size: 11px; margin-top: 3px; }
#scale-input {
  width: 100%; margin-top: 10px; box-sizing: border-box;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  color: var(--text); font: inherit; font-size: 15px; padding: 8px 10px;
  font-variant-numeric: tabular-nums;
}
#scale-input:focus { outline: none; border-color: var(--accent); }

/* Settings panel */
.popover {
  position: fixed;
  z-index: 200;
  width: 380px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .5);
  overflow: hidden;
}
.popover.hidden { display: none; }
.mcfg-tabs {
  display: flex; gap: 2px;
  padding: 6px 8px 0;
  border-bottom: 1px solid var(--line);
}
.mcfg-tab {
  /* Size to the label, never wrap it: "Lower Body" is longer than the other tabs. */
  flex: 1 1 auto; white-space: nowrap; text-align: center;
  padding: 7px 6px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  cursor: pointer;
  font: inherit; font-size: 12px;
  border-radius: 6px 6px 0 0;
}
.mcfg-tab:hover { background: var(--panel-2); color: var(--text); }
.mcfg-tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.mcfg-radio { margin-right: 5px; font-size: 10px; opacity: .8; }
.mcfg-tab.active .mcfg-radio { color: var(--accent); opacity: 1; }
.mcfg-sides {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px; color: var(--muted); font-size: 12px;
}
.unit-toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.unit-btn {
  padding: 4px 12px; background: var(--panel-2); color: var(--muted);
  border: none; cursor: pointer; font: inherit; font-size: 12px;
}
.unit-btn + .unit-btn { border-left: 1px solid var(--line); }
.unit-btn:hover { color: var(--text); }
.unit-btn.active { background: var(--accent); color: #08213b; font-weight: 600; }
/* The side panel scrolls as a whole; the list does not need its own scrollbar. */
#meas-cfg-list { padding: 6px 0; }
.mcfg-empty { padding: 18px 12px; color: var(--muted); font-size: 12px; text-align: center; }
.mcfg-row {
  display: flex; align-items: flex-start; gap: 8px;
  padding: 8px 12px; cursor: pointer; user-select: none;
}
.mcfg-row:hover { background: var(--panel-2); }
.mcfg-row input { accent-color: var(--accent); margin-top: 2px; }
.mcfg-col { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.mcfg-nameline { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.mcfg-name { color: var(--text); font-size: 13px; }
.mcfg-type {
  font-size: 9px; color: var(--accent);
  border: 1px solid var(--line); border-radius: 4px;
  padding: 1px 5px; text-transform: uppercase; letter-spacing: .3px;
  white-space: nowrap;
}
.mcfg-desc { color: var(--muted); font-size: 11px; line-height: 1.35; }
.pop-foot {
  display: flex; gap: 6px; padding: 10px 12px;
  border-top: 1px solid var(--line);
}
.pop-foot .btn.small { flex: 0 0 auto; }

/* Custom-circumference: height tag + delete on a row, and the add form. */
.mcfg-h {
  font-size: 9px; color: var(--muted);
  border: 1px solid var(--line); border-radius: 4px;
  padding: 1px 5px; white-space: nowrap;
}
.mcfg-del {
  flex: 0 0 auto; align-self: center;
  width: 22px; height: 22px; padding: 0;
  background: none; border: 1px solid var(--line); border-radius: 6px;
  color: var(--muted); cursor: pointer; font-size: 12px; line-height: 1;
}
.mcfg-del:hover { color: #ff6b6b; border-color: #ff6b6b; }
.mcfg-add {
  display: flex; flex-direction: column; gap: 6px;
  padding: 10px 12px; border-bottom: 1px dashed var(--line);   /* sits above the list */
}
.mcfg-add-title {
  color: var(--muted); text-transform: uppercase; letter-spacing: .5px; font-size: 10px;
}
.mcfg-add-row { display: flex; align-items: center; gap: 6px; }
.mcfg-in {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  color: var(--text); font: inherit; font-size: 12px; padding: 5px 8px;
}
.mcfg-in.cc-name { width: 100%; }
.mcfg-in.cc-height { width: 90px; }
.mcfg-in:focus { outline: none; border-color: var(--accent); }
.mcfg-unit { color: var(--muted); font-size: 11px; white-space: nowrap; }
.mcfg-add .btn.small.cc-add { margin-left: auto; flex: 0 0 auto; }
#progress-label { color: var(--text); font-size: 13px; margin-bottom: 12px; }
.progress-track { height: 8px; background: var(--panel-2); border-radius: 999px; overflow: hidden; }
#progress-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--section), var(--accent));
  background-size: 200% 100%;
  border-radius: 999px;
  transition: width .3s ease;
  /* Flow the gradient continuously so a long stage (e.g. the ~60s inner-noise
     clean on a big mesh) never looks frozen even while the % is unchanged. */
  animation: prog-flow 1.3s linear infinite;
}
@keyframes prog-flow { from { background-position: 0 0; } to { background-position: -200% 0; } }
#progress-pct {
  margin-top: 8px; text-align: right;
  color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums;
}

/* Side panel */
/* Two columns above, one export row across the full width below — the reports
   cover both columns, so the buttons must not sit inside either one. */
#side {
  grid-area: side;
  background: var(--panel);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.side-cols {
  display: flex;
  flex-direction: row;          /* cross-section + manual | auto measures */
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
}
.side-col {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  flex: 1 1 0;
  /* Last resort on a short window: scroll rather than let content run past the
     column and hide under the export row. */
  overflow-y: auto;
}
.side-col.col-section { border-right: 1px solid var(--line); flex: 1 1 0; min-width: 220px; }
.side-col.col-meas { flex: 1.15 1 0; min-width: 240px; }
.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* Both columns' heads must be the same height or their rules — and everything
     below them — sit at different heights. Column 1's carries a button, so pin
     the height rather than letting content decide it. */
  min-height: 43px;
  box-sizing: border-box;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .5px;
  font-size: 11px;
}
#section {
  /* Grows into the space manual measures would occupy, so column 1 is never
     half-empty; never smaller than 210px once they appear. */
  flex: 0 0 210px;          /* fixed: the section sits at the top of column 1 */
  /* A flex item's default `min-height: auto` is its content minimum, and a
     canvas' content minimum follows its buffer's aspect ratio — which would
     override the 210 and creep as the buffer is re-sized. */
  min-height: 0;
  width: 100%;
  background:
    radial-gradient(circle at 50% 45%, #1d222b 0%, #171b22 100%);
  cursor: grab;
}
#section:active { cursor: grabbing; }

#foot-stats {
  margin: 0;
  padding: 12px;
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
  background: #1a2b22;
}
#foot-stats > div { display: flex; flex-direction: column; gap: 2px; }
#foot-stats dt { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .4px; }
#foot-stats dd { margin: 0; font-variant-numeric: tabular-nums; font-size: 20px; font-weight: 600; color: var(--text); }

#leg-meas {
  border-top: 1px solid var(--line);
  background: var(--panel);
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.lm-head {
  display: flex; align-items: baseline; justify-content: space-between;
  padding: 8px 12px 4px;
  color: var(--muted); text-transform: uppercase; letter-spacing: .5px; font-size: 11px;
}
.lm-hint { text-transform: none; letter-spacing: 0; font-size: 10px; opacity: .7; }
#lm-rows { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; overflow-y: auto; }
/* Landmark-points diagnostic readout (1st column) */
#lm-points.hidden { display: none; }
#lm-points { border-top: 1px solid #222834; }
#lm-pt-rows { display: flex; flex-direction: column; padding: 0 12px 8px; gap: 3px; }
.lm-pt {
  display: grid; grid-template-columns: 34px 1fr; align-items: baseline; gap: 6px;
  font-variant-numeric: tabular-nums;
}
.lm-pt-name { color: var(--text); font-size: 11px; font-weight: 600; }
.lm-pt-xyz { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; color: #c9d1dc; white-space: nowrap; text-align: right; }
.lm-pt-off .lm-pt-name { color: #e0564a; }   /* the point is not on the mesh */
#side-exports {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  background: var(--panel-2);
}
.pdf-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  width: 100%;
  margin: 0;
  border-color: color-mix(in srgb, var(--section) 42%, var(--panel)); color: var(--section);
}
/* An inline SVG with no intrinsic size falls back to 300x150 and drags the
   whole export row with it. */
.pdf-btn svg { width: 13px; height: 13px; flex: 0 0 auto; }
.pdf-btn:hover { background: color-mix(in srgb, var(--section) 16%, var(--panel)); border-color: var(--section); }
.pdf-btn:disabled { opacity: .5; pointer-events: none; }
.lm-row {
  display: grid;
  grid-template-columns: 20px 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  background: none;
  border: none;
  border-top: 1px solid #23272f;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font: inherit;
}
.lm-row:hover { background: var(--panel-2); }
.lm-row.active { background: #123; box-shadow: inset 3px 0 0 var(--accent); }
/* An ESTIMATED landmark: the engine found no feature and placed it from the corpus median. The
   number is a usable starting point, so it stays legible — but it is dimmed and dashed so it never
   passes for a measurement. Dragging the point clears the flag and the row returns to normal. */
.lm-row.estimated .lm-val { color: var(--muted); font-style: italic; }
.lm-row.estimated .lm-h { font-style: italic; }
.lm-row.estimated { border-left: 2px dashed var(--muted); }
.lm-row.unmeasured { cursor: default; }
.lm-row.unmeasured .lm-val { color: var(--muted); }
.lm-row.unmeasured:hover { background: none; }
.lm-key { font-weight: 700; color: var(--accent); }
.lm-desc { color: var(--muted); font-size: 12px; }
.lm-val { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--section); }
.lm-h { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 11px; min-width: 46px; text-align: right; }
/* Cross-section quality failure (open / self-intersecting contour): mark the row red. */
.lm-row.lm-bad { box-shadow: inset 3px 0 0 #e0736b; }
.lm-row.lm-bad .lm-desc, .lm-row.lm-bad .lm-val, .lm-row.lm-bad .lm-h { color: #e0736b; }
.lm-row.lm-bad.active { background: #311; }

/* Manual measurements — distance / angle / circumference. */
#manual-meas {
  border-top: 1px solid var(--line);
  background: var(--panel);
  /* Sits directly under the section stats, with the column's slack below it.
     Shrinkable to nothing so a long list scrolls inside itself instead of
     growing the column past its bounds — a percentage max-height would freeze
     it at a size the column may not have. */
  flex: 0 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
#manual-rows { overflow-y: auto; }
/* The panel is always present, so it needs something to say when it is empty. */
.manual-empty { padding: 8px 12px 10px; color: var(--muted); font-size: 11px; }
.manual-row {
  display: grid;
  grid-template-columns: auto 1fr auto auto auto;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-top: 1px solid #23272f;
}
.manual-eye {
  background: none; border: none; cursor: pointer;
  font-size: 13px; line-height: 1; padding: 2px 4px; border-radius: 5px;
  filter: grayscale(1);
}
.manual-eye:hover { background: var(--panel-2); }
.manual-eye.off { opacity: .35; }
.manual-kind {
  font-size: 9px; text-transform: uppercase; letter-spacing: .4px;
  font-weight: 700; opacity: .85;
}
.manual-name {
  background: none; border: 1px solid transparent; color: var(--text);
  font: inherit; font-size: 12px; padding: 3px 6px; border-radius: 5px; min-width: 0;
}
.manual-name:hover { border-color: #3d4757; }
.manual-name:focus { outline: none; border-color: var(--accent); background: var(--panel-2); }
.manual-val { font-variant-numeric: tabular-nums; font-weight: 600; }
.manual-del {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 14px; line-height: 1; padding: 3px 7px; border-radius: 5px;
}
.manual-del:hover { color: #e0736b; background: var(--panel-2); }

#stats {
  margin: 0;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
  background: var(--panel-2);
}
#stats > div { display: flex; flex-direction: column; gap: 2px; }
#stats dt { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .4px; }
#stats dd { margin: 0; font-variant-numeric: tabular-nums; font-size: 14px; color: var(--section); }

.app-ver {
  position: fixed; left: 10px; bottom: 8px; z-index: 50;
  font-size: 11px; color: var(--muted); opacity: 0.7;
  pointer-events: none; font-variant-numeric: tabular-nums;
}

/* ---- Login overlay (Cognito sign-in gate) -------------------------------- */
.login-overlay {
  position: fixed; inset: 0; z-index: 9999;
  display: flex; align-items: center; justify-content: center;
  background: #000;                 /* opaque — the main view is fully hidden */
}
.login-overlay.login-light { background: #ffffff; }
.login-card {
  width: 320px; max-width: 90vw; padding: 28px 26px;
  background: #1b1f27; border: 1px solid #2c333f; border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  display: flex; flex-direction: column; gap: 12px; color: #e6e9ef;
  font-family: system-ui, -apple-system, sans-serif;
}
/* The card stays dark even when the overlay behind it is white, so it always
   takes the dark ink. */
.login-logo { width: 100%; height: 89px; margin: 0 auto 8px; }
.login-sub { margin: 0 0 6px; color: #96a0b0; font-size: 13px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: #96a0b0; }
.login-card input {
  padding: 10px 12px; border-radius: 8px; border: 1px solid #333c4a;
  background: #12151b; color: #e6e9ef; font-size: 14px; outline: none;
}
.login-card input:focus { border-color: #4a9e6f; }
.login-card button {
  margin-top: 6px; padding: 11px; border: 0; border-radius: 8px; cursor: pointer;
  background: #3a8f63; color: #fff; font-size: 14px; font-weight: 600;
}
.login-card button:disabled { opacity: 0.6; cursor: default; }
.login-err { color: #ff7a7a; font-size: 12px; min-height: 14px; }

/* ---- Account chip (top-right, signed-in user) ---------------------------- */
#menubar { align-items: center; }
.account {
  margin-left: auto;            /* push to the far right of the menubar */
  display: flex; align-items: center; gap: 10px;
  padding: 0 6px 0 10px;
}
.account-text { display: flex; flex-direction: column; line-height: 1.15; text-align: right; }
.account-name { font-size: 12px; font-weight: 600; color: #e6e9ef; }
.account-lab { font-size: 11px; color: var(--section); }
.account-email { font-size: 11px; color: #96a0b0; }
.account-signout {
  border: 1px solid #333c4a; background: #1b1f27; color: #c9cfda;
  border-radius: 6px; padding: 4px 9px; font-size: 11px; cursor: pointer;
}
.account-signout:hover { background: #242a34; color: #fff; border-color: #3a8f63; }

/* ---- Pre-auth splash (covers the app until sign-in resolves) -------------- */
#preauth-splash {
  position: fixed; inset: 0; z-index: 9998;      /* just below the login overlay (9999) */
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  background: #000; color: #e6e9ef;
  font-family: system-ui, -apple-system, sans-serif;
}
.theme-light #preauth-splash { background: #ffffff; color: #1b1f27; }
.preauth-logo { width: 270px; height: 90px; }
.preauth-sub { font-size: 13px; color: #96a0b0; }
.theme-light .preauth-sub { color: #667085; }
/* Keep the login overlay consistent with the theme too. */
.theme-light .login-overlay:not(.login-light) { background: #ffffff; }

/* ============================================================================
   Responsive — phones / narrow windows. Desktop (the 2-column grid above) is
   unchanged; below ~900px the right panel (min 520px) can no longer fit beside
   the 3D view, so we stack: topbar · 3D viewport · panel (scrolls internally).
   ============================================================================ */
@media (max-width: 900px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 50vh 1fr;      /* topbar · 3D view · measurements panel */
    grid-template-areas:
      "toolbar"
      "viewport"
      "side";
  }
  /* Menubar + icon toolbar scroll sideways instead of clipping or squishing. */
  #menubar, #toolbar {
    overflow-x: auto; overflow-y: hidden; flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
  }
  #menubar::-webkit-scrollbar, #toolbar::-webkit-scrollbar { height: 0; }
  #menubar > *, #toolbar > * { flex: 0 0 auto; }
  /* Grid items default to min-width:auto → they grow to their content and overflow the
     single narrow column. Pin them to the track so content reflows/scrolls instead. */
  #topbar, #viewport, #side { min-width: 0; }
  /* Right panel: stack its two columns and scroll within the panel area. */
  #side {
    border-left: none;
    border-top: 1px solid var(--line);
    overflow-y: auto;
  }
  .side-cols { flex-direction: column; flex: 0 0 auto; }
  /* Both selectors must be as specific as the desktop `.side-col.col-*` rules,
     or those keep flex-basis:0 and the columns collapse inside the now
     content-sized wrapper. Here each column is its own height and #side scrolls. */
  .side-col.col-section, .side-col.col-meas { flex: 0 0 auto; min-width: 0; }
  .side-col.col-section {
    border-right: none;
    border-bottom: 1px solid var(--line);
  }
}

/* The file input is visually hidden but still rendered: `hidden`/display:none inputs are
   unreliable to activate on mobile Safari, both from the label and from tb-load's click(). */
.file-in { position: absolute; width: 1px; height: 1px; opacity: 0; overflow: hidden; }

/* Freehand lasso path, drawn over the whole window while dragging a selection. */
.select-lasso {
  position: fixed; inset: 0; width: 100vw; height: 100vh;
  pointer-events: none; z-index: 60;
}
.select-lasso.hidden { display: none; }
.select-lasso polyline {
  /* Same accent as the box marquee — both are the same act of selecting. The dashes
     are what tell freehand from marquee, not the colour. */
  fill: color-mix(in srgb, var(--accent) 10%, transparent);
  stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 4 3;
}

/* ============================================================================
   mobileMode — the same viewer embedded in a host app (iOS/Android/web) that
   has already uploaded a scan and run the pipeline. The URL carries the job id
   (?job=<id>&mode=mobile); everything the host app owns — menus, toolbar, the
   results panel, the sign-in gate — is hidden, leaving the model, its overlay
   and the progress bar. Hiding is done here rather than in the boot path so
   desktopMode keeps exactly one code path.
   ========================================================================= */
body.mobileMode #topbar,
body.mobileMode #toolbar,
body.mobileMode #side,
body.mobileMode #preauth-splash,
body.mobileMode .app-ver { display: none; }

body.mobileMode #app {
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  grid-template-areas: "viewport";
  height: 100dvh;                 /* dvh, so a phone's collapsing address bar doesn't crop the canvas */
}

/* The hint line is the only text mobileMode keeps — it carries "needs manual alignment" and load
   errors, which are the two things a host app's user must be told. */
body.mobileMode #hint {
  position: fixed; left: 0; right: 0; bottom: 0;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, .55); text-align: center; pointer-events: none;
}
/* Nothing to say → no strip at all, rather than an empty bar over the model. */
body.mobileMode #hint:empty { display: none; }

/* On a phone the canvas fills the screen, so every gesture on it belongs to the model: without
   this, a drag scrolls the page and a pinch zooms the document instead of orbiting the scan. */
body.mobileMode #viewport,
body.mobileMode #viewport canvas { touch-action: none; }

/* The guide bar was positioned under the desktop toolbar, which mobileMode hides — bring it up,
   clear of the notch. */
body.mobileMode #lm-guide { top: calc(12px + env(safe-area-inset-top)); }
/* The progress card is centred by a transform — only its width needs to fit a narrow screen. */
body.mobileMode #progress { width: min(340px, 86vw); }

/* One Settings panel (the "Setting" title): body type + side panel of measurement items, then general rows. */
.set-body { max-height: calc(100vh - 150px); overflow-y: auto; padding-bottom: 4px; }
.set-section {
  padding: 12px 12px 4px; color: var(--muted); font-size: 11px;
  text-transform: uppercase; letter-spacing: .5px;
}
.set-section + .menu-row { padding-top: 2px; }
/* No title bar: the first section heading carries the close button. */
.set-section-top { display: flex; align-items: center; justify-content: space-between; padding-top: 8px; }
.set-section-top .btn { text-transform: none; }
.set-count { color: var(--muted); font-size: 12px; font-weight: 400; }
/* Side panel with the measurement items, placed beside the Settings panel by placeItemsPanel().
   position:fixed lets it sit outside the Settings panel's overflow:hidden box. */
.set-flyout {
  position: fixed; z-index: 201; width: 340px; display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .5); overflow: hidden;
}
.set-flyout[hidden] { display: none; }
.set-flyout-head {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  border-bottom: 1px solid var(--line); font-size: 13px; font-weight: 600;
}
.set-items-actions { display: flex; gap: 6px; margin-left: auto; }
.set-flyout-body { overflow-y: auto; min-height: 0; }
.pop-foot #meas-cfg-apply { margin-left: auto; }
