/* ValveCore - concrete, cast iron and gas-marking yellow */

:root {
  --bg: #E2E5E1;
  --surface: #F5F6F3;
  --surface-2: #EBEEE9;
  --ink: #1B2529;
  --ink-2: #53616A;
  --ink-3: #7B878C;
  --line: #CCD2CC;
  --gas: #F2B705;          /* APWA gas-line marking yellow */
  --gas-soft: #F2B70526;
  --control: #1B2529;
  --focus: #1B6FD1;
  --danger: #B3261E;
  --ok: #2E8B57;

  --iron: #1A2124;         /* valve box lid */
  --iron-edge: #283236;
  --iron-ink: #DCE2DF;
  --iron-ink-2: #93A0A4;

  --r-lg: 14px;
  --r: 9px;
  --r-sm: 6px;

  --font: "Barlow", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-cond: "Barlow Condensed", "Arial Narrow", "Segoe UI", sans-serif;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #13191B;
  --surface: #1C2427;
  --surface-2: #232D31;
  --ink: #E6EAE7;
  --ink-2: #A0ACB0;
  --ink-3: #75828A;
  --line: #313D41;
  --control: #F2B705;
  --focus: #6FB0FF;
  --iron: #0F1416;
  --iron-edge: #222B2F;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #13191B;
    --surface: #1C2427;
    --surface-2: #232D31;
    --ink: #E6EAE7;
    --ink-2: #A0ACB0;
    --ink-3: #75828A;
    --line: #313D41;
    --control: #F2B705;
    --focus: #6FB0FF;
    --iron: #0F1416;
    --iron-edge: #222B2F;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .45; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
code { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: .92em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
h2 { margin: 0; font: 600 17px/1.2 var(--font-cond); letter-spacing: .01em; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------- top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; color: inherit; text-decoration: none; }
.brand-mark { width: 34px; height: 34px; flex: none; }
.brand-text { display: flex; flex-direction: column; }
.brand-name { font: 700 25px/1 var(--font-cond); letter-spacing: .005em; }
.brand-sub { font-size: 13px; color: var(--ink-2); }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.model-status {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--line);
  font-size: 13px; color: var(--ink-2); white-space: nowrap;
}
.model-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ink-3); }
.model-status[data-state="ready"] .dot { background: var(--ok); }
.model-status[data-state="error"] .dot { background: var(--danger); }
.model-status[data-state="error"] { color: var(--danger); }

.icon-btn {
  display: grid; place-items: center; width: 36px; height: 36px;
  border: 1px solid var(--line); border-radius: var(--r); background: var(--surface-2);
}
.icon-btn svg { width: 18px; height: 18px; }

/* ---------------------------------------------------------- layout */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr) 360px;
  gap: 16px;
  padding: 16px;
  align-items: start;
}
.panel {
  position: sticky; top: 16px;
  display: flex; flex-direction: column; gap: 20px;
  max-height: calc(100vh - 96px);
  overflow: auto;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
}

/* ---------------------------------------------------------- source panel */
.segmented {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px;
  padding: 4px; border-radius: var(--r); background: var(--surface-2); border: 1px solid var(--line);
}
.segmented button {
  padding: 7px 8px; border: 0; border-radius: var(--r-sm); background: transparent;
  font-weight: 500; color: var(--ink-2);
}
.segmented button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(20, 30, 34, .12); }

.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center;
  padding: 22px 14px;
  border: 1.5px dashed var(--ink-3); border-radius: var(--r);
  color: var(--ink-2); cursor: pointer;
  transition: background-color .15s, border-color .15s;
}
.dropzone svg { width: 28px; height: 28px; margin-bottom: 4px; color: var(--ink); }
.dropzone:hover, .dropzone.over { border-color: var(--gas); background: var(--gas-soft); }
.dropzone:focus-within { outline: 2px solid var(--focus); outline-offset: 2px; }
.dz-title { font-weight: 600; color: var(--ink); }
.dz-sub { font-size: 13px; }

.source-group { display: flex; flex-direction: column; gap: 10px; }
.group-head { display: flex; align-items: center; gap: 8px; }
.count {
  min-width: 22px; padding: 0 6px; border-radius: 999px; text-align: center;
  font-size: 12px; font-weight: 600; line-height: 20px; background: var(--surface-2); color: var(--ink-2);
}
.text-btn { margin-left: auto; padding: 2px 4px; border: 0; background: none; color: var(--ink-2); font-size: 13px; text-decoration: underline; text-underline-offset: 3px; }
.text-btn:hover { color: var(--ink); }
.hint { margin: 0; font-size: 13px; color: var(--ink-2); }

.thumb-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.thumb {
  display: flex; flex-direction: column; gap: 4px; padding: 4px;
  border: 1px solid var(--line); border-radius: var(--r); background: var(--surface-2); text-align: left;
}
.thumb img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 5px; display: block; background: var(--iron); }
.thumb span { font-size: 12px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 2px; }
.thumb:hover { border-color: var(--ink-3); }
.thumb.active { border-color: var(--gas); box-shadow: 0 0 0 2px var(--gas); }
.thumb.active span { color: var(--ink); font-weight: 600; }
.thumb-empty { grid-column: 1 / -1; margin: 0; padding: 14px; border-radius: var(--r); background: var(--surface-2); font-size: 13px; color: var(--ink-2); }

/* ---------------------------------------------------------- viewer */
.viewer { display: flex; flex-direction: column; gap: 14px; min-width: 0; }

.stage-frame {
  padding: 8px;
  border-radius: 18px;
  background: var(--iron-edge);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .05), 0 1px 0 rgba(0, 0, 0, .08);
}
.stage-bar { display: flex; align-items: center; gap: 12px; min-height: 40px; padding: 2px 6px 8px; color: var(--iron-ink); }
.stage-title { font: 600 19px/1.2 var(--font-cond); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.stage-meta { font-size: 13px; color: var(--iron-ink-2); white-space: nowrap; }
.stage-actions { margin-left: auto; display: flex; gap: 6px; flex: none; }
.stage-btn {
  padding: 6px 12px; border-radius: var(--r-sm);
  border: 1px solid rgba(255, 255, 255, .14); background: rgba(255, 255, 255, .06); color: var(--iron-ink);
  font-size: 13px; font-weight: 500;
}
.stage-btn:hover:not(:disabled) { background: rgba(255, 255, 255, .12); }
.stage-btn[aria-pressed="false"] { color: var(--iron-ink-2); text-decoration: line-through; }

/* The memorable piece: the image sits on a cast-iron valve box lid */
.stage {
  position: relative;
  display: grid; place-items: center;
  height: min(68vh, 760px); min-height: 320px;
  overflow: hidden;
  border-radius: 12px;
  background-color: var(--iron);
  background-image:
    linear-gradient(45deg, rgba(255,255,255,.03) 25%, transparent 25%, transparent 75%, rgba(255,255,255,.03) 75%),
    linear-gradient(45deg, rgba(255,255,255,.03) 25%, transparent 25%, transparent 75%, rgba(255,255,255,.03) 75%);
  background-size: 22px 22px;
  background-position: 0 0, 11px 11px;
  box-shadow: inset 0 0 40px rgba(0, 0, 0, .55);
}
.stage canvas { display: block; max-width: 100%; max-height: 100%; cursor: crosshair; }
.stage.drop-over { box-shadow: inset 0 0 0 3px var(--gas), inset 0 0 40px rgba(0,0,0,.55); }

.empty { max-width: 360px; padding: 24px; text-align: center; color: var(--iron-ink-2); }
.empty svg { width: 64px; height: 64px; color: var(--gas); opacity: .9; }
.empty p { margin: 6px 0 0; font-size: 14px; }
.empty .empty-title { margin-top: 14px; font: 600 22px/1.2 var(--font-cond); color: var(--iron-ink); }

.scan { position: absolute; inset: 0; pointer-events: none; background: rgba(15, 20, 22, .25); }
.scan::after {
  content: ""; position: absolute; left: 0; right: 0; height: 2px;
  background: var(--gas); box-shadow: 0 0 18px 4px rgba(242, 183, 5, .45);
  animation: sweep 1.2s ease-in-out infinite alternate;
}
@keyframes sweep { from { top: 4%; } to { top: 96%; } }

.live-badge {
  position: absolute; top: 12px; left: 12px;
  display: flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px;
  background: rgba(15, 20, 22, .75); color: var(--iron-ink); font-size: 13px; font-weight: 500;
}
.rec { width: 8px; height: 8px; border-radius: 50%; background: #E5484D; animation: blink 1.4s steps(2) infinite; }
@keyframes blink { 50% { opacity: .25; } }

@media (prefers-reduced-motion: reduce) {
  .scan::after { animation: none; top: 50%; }
  .rec { animation: none; }
  * { transition: none !important; }
}

/* ---------------------------------------------------------- controls */
.controls {
  display: grid; grid-template-columns: 1fr 1fr 170px; gap: 18px;
  padding: 14px 16px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg);
}
.control { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.control label { display: flex; justify-content: space-between; align-items: baseline; font-weight: 500; font-size: 14px; }
.control output { font: 600 16px/1 var(--font-cond); font-variant-numeric: tabular-nums; }
.control-hint { font-size: 12px; color: var(--ink-3); }
.control input[type="range"] { width: 100%; accent-color: var(--control); }
.control select {
  padding: 7px 10px; border-radius: var(--r-sm);
  border: 1px solid var(--line); background: var(--surface-2);
}

/* ---------------------------------------------------------- report */
.summary { display: flex; flex-direction: column; gap: 4px; }
.summary-count { margin: 0; display: flex; align-items: baseline; gap: 8px; }
#totalCount { font: 700 44px/1 var(--font-cond); font-variant-numeric: tabular-nums; }
#totalLabel { font: 600 20px/1 var(--font-cond); color: var(--ink-2); }
.summary-sub { margin: 2px 0 10px; font-size: 14px; color: var(--ink-2); }

.class-bars { display: flex; flex-direction: column; gap: 6px; }
.class-bar {
  display: grid; grid-template-columns: 12px 1fr auto; align-items: center; column-gap: 8px; row-gap: 5px;
  padding: 7px 10px; border: 1px solid var(--line); border-radius: var(--r-sm);
  background: var(--surface-2); text-align: left;
}
.class-bar .name { font-weight: 500; font-size: 14px; }
.class-bar .num { font: 600 16px/1 var(--font-cond); font-variant-numeric: tabular-nums; }
.class-bar .track { grid-column: 1 / -1; height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.class-bar .fill { display: block; height: 100%; background: var(--c); transition: width .25s ease; }
.class-bar[aria-pressed="false"] { opacity: .5; }
.class-bar[aria-pressed="false"] .name { text-decoration: line-through; }

.swatch { width: 12px; height: 12px; border-radius: 3px; background: var(--c); flex: none; }

.report-group { display: flex; flex-direction: column; gap: 10px; }

.det-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.det {
  display: grid; grid-template-columns: 12px 1fr auto; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: var(--r-sm); border: 1px solid transparent; cursor: default;
}
.det:hover, .det.hover, .det:focus-visible { background: var(--surface-2); border-color: var(--line); }
.det-main { display: flex; flex-direction: column; min-width: 0; }
.det-name { font-weight: 600; font-size: 14px; }
.det-box { font-size: 12px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.det-conf { display: flex; align-items: center; gap: 8px; font: 600 16px/1 var(--font-cond); font-variant-numeric: tabular-nums; }
.meter { width: 48px; height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--c); }
.det-empty { padding: 12px; border-radius: var(--r-sm); background: var(--surface-2); font-size: 13px; color: var(--ink-2); }

.guide { display: flex; flex-direction: column; gap: 8px; }
.guide-item { border: 1px solid var(--line); border-radius: var(--r); background: var(--surface-2); overflow: hidden; }
.guide-item[open] { border-left: 3px solid var(--c); }
.guide-item summary {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  cursor: pointer; list-style: none;
}
.guide-item summary::-webkit-details-marker { display: none; }
.guide-item summary::after { content: "+"; margin-left: auto; color: var(--ink-3); font-size: 18px; line-height: 1; }
.guide-item[open] summary::after { content: "\2212"; }
.g-name { font: 600 17px/1.2 var(--font-cond); }
.g-tag { padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--c); color: #fff; }
.g-body { padding: 0 14px 14px; font-size: 14px; }
.g-body p { margin: 0 0 10px; }
.g-body dl { margin: 0; }
.g-body dt { margin-top: 10px; font-weight: 600; font-size: 13px; color: var(--ink-2); }
.g-body dd { margin: 3px 0 0; }
.g-body ul { margin: 0; padding-left: 18px; }
.g-body li + li { margin-top: 2px; }

/* ---------------------------------------------------------- toast */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translate(-50%, 20px);
  max-width: min(520px, calc(100vw - 32px));
  padding: 10px 16px; border-radius: var(--r);
  background: var(--ink); color: var(--surface); font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

/* ---------------------------------------------------------- responsive */
@media (max-width: 1200px) {
  .workspace { grid-template-columns: 250px minmax(0, 1fr); }
  .report { grid-column: 1 / -1; position: static; max-height: none; display: grid; grid-template-columns: 1fr 1fr 1.3fr; align-items: start; }
}
@media (max-width: 860px) {
  .workspace { grid-template-columns: 1fr; padding: 12px; }
  .panel { position: static; max-height: none; }
  .report { grid-template-columns: 1fr; }
  .thumb-grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
  .controls { grid-template-columns: 1fr; }
  .stage { height: 56vh; }
  .brand-sub, .stage-meta { display: none; }
}
