/* CTC Take Off — desktop-first pro tool, with a stacked phone layout per host. Light theme, neutral palette. */
* { box-sizing: border-box; }
[hidden] { display: none !important; }  /* class display rules must not override the attribute */
:root {
  --bg: #f4f5f7; --panel: #ffffff; --ink: #111827; --muted: #6b7280;
  --line: #e5e7eb; --line2: #d1d5db; --brand: #2563eb; --brand-ink: #1d4ed8;
  --warn-bg: #fef3c7; --warn-ink: #92400e; --ok: #059669; --danger: #dc2626;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.04);
}
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.4 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink); background: var(--bg);
  display: flex; flex-direction: column; overflow: hidden;
  -webkit-user-select: none; user-select: none;
}
button { font: inherit; cursor: pointer; border: 1px solid var(--line2);
  background: #fff; color: var(--ink); padding: 6px 10px; border-radius: 7px; }
button:hover { background: #f9fafb; }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
button.primary:hover { background: var(--brand-ink); }
button.ghost { background: transparent; }
button.mini { padding: 1px 7px; font-size: 12px; border-radius: 6px; }
button.danger { color: var(--danger); border-color: transparent; background: transparent; }
button.danger:hover { background: #fef2f2; }
input, select { font: inherit; border: 1px solid var(--line2); border-radius: 7px;
  padding: 5px 8px; color: var(--ink); background: #fff; }
h2 { font-size: 16px; margin: 0; }
h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); margin: 0 0 8px; display: flex; justify-content: space-between; align-items: center; }
.muted { color: var(--muted); } .small { font-size: 12px; } .strong { font-weight: 700; }

/* top bar */
.topbar { display: flex; align-items: center; gap: 10px; padding: 8px 14px;
  background: var(--panel); border-bottom: 1px solid var(--line); box-shadow: var(--shadow); z-index: 5; }
.brand { font-weight: 800; letter-spacing: -.01em; }
.brand .ver { color: var(--muted); font-weight: 600; font-size: 12px; }
.takeoff-name { width: 260px; }
.spacer { flex: 1; }
.storage-note { color: var(--muted); font-size: 12px; }

/* layout */
.layout { flex: 1; display: flex; min-height: 0; }
.stage { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.sidebar { width: 340px; background: var(--panel); border-left: 1px solid var(--line);
  overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 14px; }

/* toolbar */
.toolbar { display: flex; align-items: center; gap: 14px; padding: 8px 12px;
  background: var(--panel); border-bottom: 1px solid var(--line); }
.group { display: flex; align-items: center; gap: 4px; }
.group.modes button.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.sheet-label { min-width: 96px; text-align: center; font-variant-numeric: tabular-nums; }

/* drawing stage */
.viewport { position: relative; flex: 1; overflow: auto; background: #e9ebee;
  background-image: radial-gradient(#dcdfe4 1px, transparent 1px); background-size: 22px 22px; }
.surface { position: relative; margin: 24px auto; width: 0; height: 0;
  box-shadow: 0 4px 20px rgba(0,0,0,.14); background: #fff; }
.surface canvas { display: block; position: absolute; inset: 0; }
.overlay { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible;
  touch-action: none; }

/* HUD — always visible, states the next action (C4) */
.hud { position: absolute; left: 14px; top: 14px; z-index: 3; display: flex; gap: 10px;
  align-items: center; background: rgba(17,24,39,.92); color: #fff; padding: 7px 12px;
  border-radius: 9px; box-shadow: var(--shadow); pointer-events: none; max-width: calc(100% - 28px); }
.hud-mode { font-weight: 700; background: rgba(255,255,255,.16); padding: 2px 8px; border-radius: 6px; }
.hud-next { font-size: 13px; }
.hud-actions { pointer-events: auto; display: inline-flex; gap: 6px; margin-left: 4px; }
.hud-actions button { padding: 3px 9px; font-size: 12px; font-weight: 600; border-radius: 6px;
  border: 1px solid rgba(255,255,255,.25); background: rgba(255,255,255,.12); color: #fff; }
.hud-actions button:hover { background: rgba(255,255,255,.24); }
.hud-actions button[data-act="finish"] { background: var(--brand); border-color: var(--brand); }
.hud-actions button[data-act="close"] { background: var(--ok); border-color: var(--ok); }

.busy { position: absolute; right: 14px; top: 14px; z-index: 3; display: flex; gap: 8px; align-items: center;
  background: rgba(17,24,39,.92); color: #fff; padding: 7px 12px; border-radius: 9px; font-size: 13px;
  box-shadow: var(--shadow); pointer-events: none; }
.spinner { width: 13px; height: 13px; border: 2px solid rgba(255,255,255,.35); border-top-color: #fff;
  border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.empty { position: absolute; inset: 0; display: grid; place-items: center; }
.empty-card { background: #fff; border: 1px solid var(--line); border-radius: 14px;
  padding: 28px 32px; text-align: center; max-width: 380px; box-shadow: var(--shadow); }
.empty-card p { color: var(--muted); }

/* panels */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.panel .row { margin-top: 8px; }
.panel .row label { display: block; font-size: 12px; color: var(--muted); }
.panel .row select, .panel .row button { width: 100%; margin-top: 3px; }
.wall-h input { width: 60px; }
.scale-status { border-radius: 8px; padding: 8px 10px; display: flex; flex-direction: column; gap: 2px; font-size: 13px; }
.scale-status.ok { background: #ecfdf5; color: #065f46; }
.scale-status.approx { background: var(--warn-bg); color: var(--warn-ink); }
.warn { background: var(--warn-bg); color: var(--warn-ink); padding: 8px 10px; border-radius: 8px; font-size: 13px; margin-top: 8px; }
.calib-input { margin-top: 10px; padding: 10px; border: 1px dashed var(--brand); border-radius: 9px; background: #eff6ff; }
.calib-input input { width: 100%; margin: 4px 0 8px; }
.calib-input button { margin-right: 6px; }

/* scopes */
.scopes { display: flex; flex-direction: column; gap: 8px; }
.scope { border: 1px solid var(--line); border-left: 4px solid var(--c); border-radius: 9px; padding: 8px; }
.scope.active { box-shadow: 0 0 0 2px color-mix(in srgb, var(--c) 40%, transparent); }
.scope-head { display: flex; align-items: center; gap: 6px; }
.scope-head .dot { width: 16px; height: 16px; border-radius: 50%; background: var(--c); border: none; padding: 0; flex: none; }
.scope.active .scope-head .dot { box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 30%, transparent); }
.scope-name { flex: 1; border: none; background: transparent; font-weight: 700; padding: 2px 4px; }
.scope-name:focus { background: #f3f4f6; }
.scope-metrics { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: 6px 0; font-size: 13px; }
.scope-metrics .h input { width: 42px; }
.badge { font-size: 11px; padding: 1px 6px; border-radius: 999px; }
.badge.approx { background: var(--warn-bg); color: var(--warn-ink); }
.badge.warn { background: #fee2e2; color: var(--danger); }
.runs { display: flex; flex-direction: column; gap: 2px; margin-top: 4px; }
.run { display: flex; align-items: center; gap: 6px; padding: 3px 6px; border-radius: 6px; font-size: 13px; }
.run:hover { background: #f3f4f6; }
.run.sel { background: #eff6ff; }
.run-icon { color: var(--muted); }
.run-len { flex: 1; font-variant-numeric: tabular-nums; }

/* totals */
.grand { display: flex; flex-direction: column; gap: 8px; }
.grand > div { display: flex; align-items: baseline; justify-content: space-between; }
.grand .big { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.grand .cap { color: var(--muted); font-size: 12px; }
.export { display: flex; gap: 8px; }
.export button { flex: 1; }
.send-panel { display: flex; flex-direction: column; gap: 6px; }
.send-btn { width: 100%; }
.send-panel p { margin: 0; }

/* picker */
.picker { position: fixed; inset: 0; background: rgba(17,24,39,.4); display: grid; place-items: center; z-index: 20; }
.picker-box { background: #fff; border-radius: 14px; width: min(560px, 92vw); max-height: 80vh; overflow: hidden; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.picker-head { display: flex; justify-content: space-between; align-items: center; padding: 16px; border-bottom: 1px solid var(--line); }
.picker-list { overflow-y: auto; padding: 8px; }
.pick { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px; border-radius: 9px; cursor: pointer; }
.pick:hover { background: #f3f4f6; }

@media (max-width: 820px) {
  .sidebar { width: 300px; }
  .takeoff-name { width: 140px; }
}

/* Phone-layout hosts at every width: the hint box is moved into .stage by app.js, pinned to the foot of the visible plan. */
html[data-phone-layout] .stage { position: relative; }
html[data-phone-layout] .hud { top: auto; bottom: 14px; }

/* ── Phone layout ───────────────────────────────────────────────────────────────
   Only for hosts listed in index.html (html[data-phone-layout]) and only under 640px wide, so desktop, iPad and every other
   host are untouched. The side-by-side panes stack: plan on top, the side panel below it, and the page scrolls between them.
   Every control is kept. The plan area stops short of the bottom of the screen so a strip of the panel always shows: drawing
   and panning inside the plan capture the finger, so the page can only be scrolled from outside it. */
/* 640px is also in public/js/app.js (PHONE_LAYOUT); change both together. */
@media (max-width: 640px) {
  html[data-phone-layout], html[data-phone-layout] body { height: auto; }
  html[data-phone-layout] body { display: block; min-height: 100%; overflow-x: hidden; overflow-y: auto; }

  html[data-phone-layout] .topbar { flex-wrap: wrap; gap: 6px 8px; padding: 8px 10px; }
  html[data-phone-layout] .takeoff-name { flex: 1 1 90px; width: auto; min-width: 0; }
  html[data-phone-layout] .spacer { display: none; }
  html[data-phone-layout] .storage-note:empty { display: none; }
  /* After New / Open PDF rather than a row of its own: it is nearly always showing, and a third row costs plan height.
     Row 1 is the name + Open…; row 2 is New, Open PDF and this note. */
  html[data-phone-layout] .storage-note { order: 10; flex: 1 1 100px; min-width: 0; font-size: 11px; line-height: 1.25; }
  html[data-phone-layout] .topbar button { min-height: 36px; }

  html[data-phone-layout] .layout { display: block; }
  /* vh first: a browser without dvh (iOS Safari before 15.4) drops the dvh line and keeps this one. */
  html[data-phone-layout] .stage { height: calc(100vh - var(--topbar-h, 96px) - 88px); height: calc(100dvh - var(--topbar-h, 96px) - 88px);
    min-height: 320px; }

  /* Tools on their own row, scrolling sideways; zoom and sheet stay put on the row below. */
  html[data-phone-layout] .toolbar { flex-wrap: wrap; gap: 6px 10px; padding: 6px 10px; }
  html[data-phone-layout] .group.modes { flex: 1 1 100%; overflow-x: auto; -webkit-overflow-scrolling: touch;
    scrollbar-width: none; padding-bottom: 2px; }
  html[data-phone-layout] .group.modes::-webkit-scrollbar { display: none; }
  html[data-phone-layout] .toolbar button { flex: none; white-space: nowrap; min-height: 36px; }
  html[data-phone-layout] .group.sheets { margin-left: auto; }
  html[data-phone-layout] .sheet-label { min-width: 64px; }

  /* At the foot of the plan (see above): down here Finish / Close loop / Undo sit under the thumb. */
  html[data-phone-layout] .hud { flex-wrap: wrap; left: 8px; bottom: 8px; max-width: calc(100% - 16px); }
  html[data-phone-layout] .hud-actions button { min-height: 32px; }
  html[data-phone-layout] .hud-next { font-size: 12px; }
  html[data-phone-layout] .empty-card { padding: 20px; margin: 0 16px; }

  html[data-phone-layout] .sidebar { width: auto; border-left: 0; border-top: 1px solid var(--line); overflow: visible; padding: 12px 10px 24px; }
  html[data-phone-layout] .panel button:not(.dot):not(.mini) { min-height: 36px; }
  html[data-phone-layout] .panel button.mini { min-height: 30px; padding: 3px 9px; }   /* remove / add scope: thumb-sized */
}
