/* iMeasure — boilerplate styling.
   Deliberately plain & token-driven so the UI/UX pass can restyle via these
   custom properties without touching layout logic. */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --ink: #1b1f24;
  --ink-soft: #5b6570;
  --line: #c8ced6;        /* ruler tick / hairline */
  --line-strong: #2b3138; /* major ruler ticks & labels */
  --grid: rgba(43, 49, 56, 0.07);       /* minor grid lines */
  --grid-strong: rgba(43, 49, 56, 0.16); /* major grid lines */
  --accent: #2f6df6;
  --accent-ink: #ffffff;
  --ref-fill: rgba(47, 109, 246, 0.06);
  --ref-stroke: #2f6df6;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.14);
  --radius: 12px;
  --toolbar-gap: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b;
    --surface: #1e232a;
    --ink: #eef1f4;
    --ink-soft: #9aa4af;
    --line: #3a424c;
    --line-strong: #d7dde3;
    --grid: rgba(215, 221, 227, 0.08);
    --grid-strong: rgba(215, 221, 227, 0.18);
    --accent: #5b8bff;
    --ref-fill: rgba(91, 139, 255, 0.10);
    --ref-stroke: #5b8bff;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;               /* the ruler owns the viewport */
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
}

/* ---- Ruler ------------------------------------------------------------- */
#ruler {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 1;
}
#ruler .grid { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
#ruler .grid.major { stroke: var(--grid-strong); }
#ruler .tick { stroke: var(--line); stroke-width: 1; shape-rendering: crispEdges; }
#ruler .tick.major { stroke: var(--line-strong); }
#ruler .label { fill: var(--line-strong); font-size: 11px; font-variant-numeric: tabular-nums; }
#ruler .baseline { stroke: var(--line-strong); stroke-width: 1.5; shape-rendering: crispEdges; }

/* ---- Reference objects ------------------------------------------------- */
#refs-layer { position: fixed; inset: 0; z-index: 2; pointer-events: none; }
.ref {
  position: absolute;
  pointer-events: auto;
  cursor: grab;
  background: var(--ref-fill);
  border: 1.5px solid var(--ref-stroke);
  border-radius: 2px;
  display: grid;
  place-items: center;
  touch-action: none;
  color: var(--ref-stroke);
}
.ref.dragging { cursor: grabbing; box-shadow: var(--shadow); }
.ref .ref-label {
  font-size: 11px;
  font-weight: 600;
  text-align: center;
  padding: 2px 4px;
  pointer-events: none;
}
.ref .ref-close {
  position: absolute;
  top: -10px; right: -10px;
  width: 20px; height: 20px;
  border-radius: 50%;
  border: none;
  background: var(--ref-stroke);
  color: #fff;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
}

/* ---- Measure caliper --------------------------------------------------- */
#measure {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  z-index: 3;
  pointer-events: none;          /* toggled to auto in measure mode */
}
#measure.active { pointer-events: auto; cursor: crosshair; }
#measure .m-line { stroke: var(--accent); stroke-width: 2; }
#measure .m-cap  { fill: var(--accent); }
#measure .m-label {
  fill: var(--accent);
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  paint-order: stroke;           /* halo for legibility over the ruler */
  stroke: var(--surface);
  stroke-width: 4px;
  stroke-linejoin: round;
}

/* ---- Toolbar ----------------------------------------------------------- */
#toolbar {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  z-index: 4;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-width: calc(100vw - 24px);
}
.tb-group { display: flex; align-items: center; gap: var(--toolbar-gap); }
.tb-readout { color: var(--ink-soft); font-variant-numeric: tabular-nums; padding: 0 4px; }

#toolbar button, .modal button, .modal select {
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
}
#toolbar button:hover, .modal button:hover { border-color: var(--accent); }
#toolbar .unit-btn.is-active,
#toolbar #measure-btn.is-active,
#toolbar #grid-btn.is-active,
#toolbar #objects-btn.is-active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
#readout { cursor: pointer; }

/* ---- Popovers (stats + object catalog) -------------------------------- */
#stats, #refs-popover {
  position: fixed;
  left: 50%;
  bottom: 74px;
  transform: translateX(-50%);
  z-index: 5;
  width: min(360px, calc(100vw - 24px));
  padding: 16px 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
#refs-popover { width: min(420px, calc(100vw - 24px)); }
#stats h2, #refs-popover h2 { margin: 0 0 10px; font-size: 15px; }
.stats-close, .pop-close { position: absolute; top: 8px; right: 8px; border: none; background: none; cursor: pointer; color: var(--ink-soft); font-size: 14px; }

/* Object catalog grid. */
#refs-catalog { display: grid; gap: 12px 16px; }
#refs-catalog .cat-group h3 {
  margin: 0 0 6px; font-size: 11px; letter-spacing: .06em; text-transform: uppercase;
  color: var(--ink-soft); font-weight: 700;
}
#refs-catalog .cat-items { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
#refs-catalog .cat-items button {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  padding: 8px 10px; text-align: left; line-height: 1.2;
}
#refs-catalog .cat-items button .obj-dim { font-size: 11px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
#stats dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; }
#stats dt { color: var(--ink-soft); }
#stats dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
#stats .fine-print { text-align: left; }

/* ---- Zoom warning ------------------------------------------------------ */
#zoom-warning {
  position: fixed;
  top: 12px; left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px;
  background: #b4520a; color: #fff;
  border-radius: 10px;
  box-shadow: var(--shadow);
}
#zoom-warning button { background: rgba(255,255,255,.15); color: #fff; border: none; border-radius: 6px; padding: 4px 8px; cursor: pointer; }

/* ---- Calibration overlay ---------------------------------------------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: rgba(10, 12, 16, 0.55);
}
.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
}
.modal-card h1 { margin: 0 0 8px; font-size: 20px; }
.modal-card .lede { margin: 0 0 16px; color: var(--ink-soft); }

/* Resizable card, pinned to the top-left corner so it can render at true
   physical size without being trapped in the panel; JS sets its dimensions. */
#calib-card {
  position: fixed;
  top: 40px;
  left: 40px;
  box-sizing: border-box;
  background: var(--ref-fill);
  border: 2px solid var(--accent);
  border-radius: 3.18mm; /* replaced in JS to scale with calibration */
  display: grid;
  place-items: center;
  padding: 6px;
  overflow: hidden;
  color: var(--accent);
}
.calib-card-label { font-size: 12px; font-weight: 600; opacity: .85; text-align: center; }

/* Compact control panel, floating clear of the card at the bottom. */
#calib-panel {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  overflow: auto;
}

.calib-controls { display: flex; align-items: center; gap: 10px; margin: 4px 0 14px; }
.calib-controls input[type=range] { flex: 1; }
.nudge { width: 34px; height: 34px; font-size: 18px; line-height: 1; }

.calib-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  color: var(--ink-soft);
  flex-wrap: wrap;
}
.calib-meta #calib-readout { font-variant-numeric: tabular-nums; }
button.primary { width: 100%; padding: 12px; font-size: 15px; }
.fine-print { margin: 10px 0 0; text-align: center; color: var(--ink-soft); font-size: 12px; }

[hidden] { display: none !important; }
