/* Variax Core — base styles.
   Tokens on :root; the tenant's primary colour arrives at runtime as --brand. */

:root {
  --brand: #1F4E79;
  --brand-ink: #ffffff;

  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #17202a;
  --muted: #5b6573;
  --border: #d8dce2;
  --focus: #2563eb;

  --ok: #157347;
  --ok-bg: #e7f5ee;
  --warn: #8a5a00;
  --warn-bg: #fff4dc;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --info: #1d4f91;
  --info-bg: #e8f0fb;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --gutter: 16px;
}

[data-theme="dark"] {
  --bg: #0f1419;
  --surface: #171e26;
  --surface-2: #1f2832;
  --text: #e6eaef;
  --muted: #9aa5b1;
  --border: #2c3744;
  --focus: #60a5fa;

  --ok: #4ade80;
  --ok-bg: #12291c;
  --warn: #fbbf24;
  --warn-bg: #2d2410;
  --danger: #f87171;
  --danger-bg: #2f1517;
  --info: #93c5fd;
  --info-bg: #13233a;

  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  min-height: 100vh;
}
[hidden] { display: none !important; }
a { color: var(--brand); }
[data-theme="dark"] a { color: var(--focus); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75em; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.mono { font-family: var(--mono); font-size: .85em; }

/* Layout ------------------------------------------------------------------ */
.app { padding: 24px var(--gutter) 64px; max-width: 1100px; margin: 0 auto; }
.app.narrow { max-width: 440px; }
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.row.between { justify-content: space-between; }
.grow { flex: 1 1 auto; }
.page-head { display: flex; gap: 12px; align-items: flex-end; justify-content: space-between; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h1 { margin: 0; }

.boot { display: grid; place-items: center; gap: 12px; padding-top: 20vh; color: var(--muted); }
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--brand);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.noscript { padding: 24px; }

/* Top bar ----------------------------------------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 10px var(--gutter);
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); font-weight: 650; min-width: 0; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 7px; flex: none;
  display: grid; place-items: center;
  background: var(--brand); color: var(--brand-ink); font-weight: 800; font-size: 15px;
}
.brand-logo { height: 28px; max-width: 120px; object-fit: contain; }
.brand-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .spacer { flex: 1; }
.tenant-select { max-width: 220px; min-width: 0; }

/* Cards & tiles ----------------------------------------------------------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px;
}
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.tile, [data-theme="dark"] .tile {
  display: flex; flex-direction: column; gap: 6px; text-align: left;
  padding: 20px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  color: var(--text); text-decoration: none; font: inherit; cursor: pointer;
  transition: border-color .15s, transform .15s;
}
.tile:hover { border-color: var(--brand); transform: translateY(-1px); }
.tile:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.tile-icon {
  width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center;
  background: color-mix(in srgb, var(--brand) 14%, transparent); color: var(--brand);
}
[data-theme="dark"] .tile-icon { color: color-mix(in srgb, var(--brand) 55%, white); }
.tile-title { font-weight: 650; font-size: 1.05rem; }
.tile-text { color: var(--muted); font-size: .9rem; }

/* Forms ------------------------------------------------------------------- */
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 6px; }
.field { margin-bottom: 14px; }
.field .hint { font-weight: 400; color: var(--muted); font-size: .8rem; margin-top: 4px; }
input[type="text"], input[type="email"], input[type="password"], input[type="tel"], select, textarea {
  width: 100%; padding: 10px 12px;
  font: 16px/1.3 var(--font);   /* 16px: phones don't zoom on focus */
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--focus); outline-offset: 0; border-color: transparent; }
input[readonly] { background: var(--surface-2); color: var(--muted); }
.checks { display: flex; flex-wrap: wrap; gap: 8px; }
.check {
  display: inline-flex; align-items: center; gap: 8px; margin: 0;
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 999px;
  font-weight: 500; cursor: pointer; background: var(--surface);
}
.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--brand); }
.check:has(input:checked) { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 10%, var(--surface)); }
.form-error {
  padding: 10px 12px; border-radius: var(--radius-sm);
  background: var(--danger-bg); color: var(--danger); font-size: .9rem; margin-bottom: 14px;
}
.form-note {
  padding: 10px 12px; border-radius: var(--radius-sm);
  background: var(--info-bg); color: var(--info); font-size: .9rem; margin-bottom: 14px;
}

/* Buttons ----------------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 8px 16px; border-radius: var(--radius-sm);
  font: 600 15px/1 var(--font); text-decoration: none; white-space: nowrap;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.btn.danger.solid { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { min-height: 32px; padding: 6px 10px; font-size: 14px; }
.btn.block { width: 100%; }
.btn.icon { padding: 8px; min-width: 40px; }
.linkbtn { background: none; border: 0; padding: 0; color: var(--brand); font: inherit; cursor: pointer; text-decoration: underline; }
[data-theme="dark"] .linkbtn { color: var(--focus); }

/* Badges ------------------------------------------------------------------ */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: .78rem; font-weight: 600; background: var(--surface-2); color: var(--muted);
  white-space: nowrap;
}
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.danger { background: var(--danger-bg); color: var(--danger); }
.badge.brand { background: color-mix(in srgb, var(--brand) 14%, transparent); color: var(--brand); }
[data-theme="dark"] .badge.brand { color: color-mix(in srgb, var(--brand) 55%, white); }
.badges { display: flex; flex-wrap: wrap; gap: 4px; }

/* Tables (become cards on phones) ------------------------------------------ */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.table tr.inactive td { opacity: .6; }
.table td.actions { text-align: right; white-space: nowrap; }
.legend { margin-top: 16px; }
.table-wrap { overflow-x: auto; }

@media (max-width: 680px) {
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 12px; padding: 8px 4px; background: var(--surface); }
  .table td { border: 0; padding: 6px 12px; }
  .table td[data-label]::before { content: attr(data-label); display: block; font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
  .table td.actions { text-align: left; }
  .app { padding-top: 16px; }
  .topbar { gap: 4px; }
  .topbar:has(.tenant-select) .brand-name { display: none; }
  .tenant-select { flex: 1 1 auto; max-width: none; margin-left: 6px; }
  .topbar:has(.tenant-select) .spacer { display: none; }
}

/* Dialogs ----------------------------------------------------------------- */
dialog.modal {
  width: min(520px, calc(100vw - 2 * var(--gutter)));
  max-height: calc(100vh - 48px);
  padding: 0; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
}
dialog.modal::backdrop { background: rgba(10, 14, 20, .55); }
.modal-head { padding: 18px 20px 0; }
.modal-head h2 { margin: 0; }
.modal-body { padding: 14px 20px; overflow-y: auto; max-height: calc(100vh - 200px); }
.modal-foot { padding: 12px 20px 18px; display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
@media (max-width: 480px) {
  .modal-foot .btn { flex: 1 1 auto; }
}

/* Toasts ------------------------------------------------------------------ */
.toasts {
  position: fixed; z-index: 50; left: 50%; bottom: 20px; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; width: min(440px, calc(100vw - 32px));
  pointer-events: none;
}
.toast {
  pointer-events: auto; padding: 12px 16px; border-radius: var(--radius-sm);
  background: var(--text); color: var(--bg); box-shadow: var(--shadow); font-size: .92rem;
  animation: toast-in .2s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* Auth screens ------------------------------------------------------------ */
.auth-card { margin-top: 8vh; }
.auth-card form > p { margin: 14px 0 0; }
.auth-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 20px; font-weight: 700; font-size: 1.2rem; }

/* Home (launcher) --------------------------------------------------------- */
.home-hero { margin: 8px 0 24px; }
.home-hero h1 { font-size: 1.75rem; margin-bottom: 4px; }
.empty {
  padding: 32px; text-align: center; color: var(--muted);
  border: 1px dashed var(--border); border-radius: var(--radius);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ===== Phase 2: admin screens ============================================ */
.app.wide { max-width: 1400px; }
textarea { width: 100%; padding: 10px 12px; font: 16px/1.4 var(--font); color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); resize: vertical; }
input[type="search"], input[type="date"], input[type="number"] {
  width: 100%; padding: 10px 12px; font: 16px/1.3 var(--font);
  color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
input[type="color"] { width: 48px; height: 40px; padding: 2px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); cursor: pointer; }
input.colour-hex { width: 110px; }
input.short { width: 90px; }
input.invalid { outline: 2px solid var(--danger); }
input:disabled { background: var(--surface-2); color: var(--muted); }
[data-theme="dark"] input[type="date"] { color-scheme: dark; }

.grid-2, .grid-3, .grid-4 { display: grid; gap: 0 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 760px) {
  .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

.section { margin-bottom: 16px; }
.section-head { margin-bottom: 14px; }
.section-head h2 { margin-bottom: 2px; }
.back-link { display: inline-flex; align-items: center; gap: 4px; font-size: .88rem; text-decoration: none; margin-bottom: 4px; color: var(--muted); }
.back-link:hover { color: var(--text); }
.sticky-actions {
  position: sticky; bottom: 0; z-index: 5; padding: 12px 0;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.stack-sm > * + * { margin-top: 6px; }
.filters { margin: 12px 0; }
.filters select { width: auto; min-width: 180px; }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 4px; overflow-x: auto; }
.tab {
  background: none; border: 0; border-bottom: 3px solid transparent; padding: 10px 14px; margin-bottom: -1px;
  font: 600 15px var(--font); color: var(--muted); cursor: pointer; white-space: nowrap;
}
.tab.on { color: var(--text); border-bottom-color: var(--brand); }
.tab:focus-visible { outline: 3px solid var(--focus); outline-offset: -3px; }
.tab-count { margin-left: 6px; font-size: .8rem; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); }

/* Chips */
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); }
.chip { display: inline-flex; align-items: center; gap: 2px; padding: 4px 4px 4px 10px; border-radius: 999px; background: var(--surface-2); font-size: .9rem; }
.chip-x { display: grid; place-items: center; width: 24px; height: 24px; border: 0; border-radius: 50%; background: transparent; color: var(--muted); cursor: pointer; }
.chip-x:hover { background: var(--border); color: var(--text); }
.chips input { flex: 1 1 140px; border: 0; padding: 6px; min-width: 120px; }
.chips input:focus { outline: none; }

/* Logo */
.logo-row { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.logo-preview {
  width: 220px; height: 90px; display: grid; place-items: center; padding: 8px;
  border: 1px dashed var(--border); border-radius: var(--radius-sm);
  background: repeating-conic-gradient(var(--surface-2) 0 25%, var(--surface) 0 50%) 0 0 / 16px 16px;
}
.logo-preview img { max-width: 100%; max-height: 100%; object-fit: contain; }

/* Settings */
.table.compact th, .table.compact td { padding: 8px 10px; }
.table.compact input { max-width: 120px; }
.preview { margin-top: 12px; padding: 12px; border-radius: var(--radius-sm); background: var(--surface-2); }
.preview .row label { margin: 0; }
.preview-lines { margin-top: 8px; font-size: .9rem; }
.check-list { max-height: 260px; overflow-y: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); margin-top: 8px; }
.check-row { display: flex; gap: 10px; align-items: flex-start; padding: 8px 12px; margin: 0; font-weight: 400; border-bottom: 1px solid var(--border); cursor: pointer; }
.check-row:last-child { border-bottom: 0; }
.module-list { border: 1px solid var(--border); border-radius: var(--radius-sm); }
.module-row { display: flex; gap: 10px; align-items: flex-start; padding: 8px 12px; margin: 0; font-weight: 400; border-bottom: 1px solid var(--border); cursor: pointer; }
.module-row:last-child { border-bottom: 0; }
.module-row input { width: 18px; height: 18px; margin-top: 2px; accent-color: var(--brand); flex: none; }
.module-row input:disabled + span { opacity: .75; }
.check-row input { width: 18px; height: 18px; margin-top: 2px; accent-color: var(--brand); flex: none; }
.date-row { padding: 4px 0; flex-wrap: nowrap; }
.date-row input[type="date"] { width: 170px; flex: none; }
.date-row input[type="text"] { flex: 1; }
.holiday-list { list-style: none; padding: 0; margin: 0; columns: 2 320px; font-size: .9rem; }
.holiday-list li { padding: 3px 0; break-inside: avoid; }
.holiday-list .badge { margin-left: 6px; }

/* Schedules */
.stats { display: flex; gap: 20px; flex-wrap: wrap; }
.stat { display: flex; flex-direction: column; }
.stat strong { font-size: 1.4rem; line-height: 1.2; }
.current-schedule { display: block; color: var(--text); text-decoration: none; margin-bottom: 16px; }
.current-schedule:hover { border-color: var(--brand); }
.current-schedule h2 { margin: 6px 0 2px; }
.issues { margin-top: 12px; }
.flags { list-style: none; padding: 0; margin: 8px 0 0; max-height: 320px; overflow-y: auto; font-size: .9rem; }
.flag { padding: 6px 10px; border-left: 3px solid; margin-bottom: 4px; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }
.flag.error { border-color: var(--danger); background: var(--danger-bg); }
.flag.warn { border-color: var(--warn); background: var(--warn-bg); }
.flag.info { border-color: var(--info); background: var(--info-bg); }
.flag-where { font-weight: 600; margin-right: 6px; }
.schedule-meta { margin-bottom: 16px; }
.schedule-meta .grid-3 { margin-top: 12px; }
.check-panel { margin-bottom: 16px; padding: 12px 16px; }
.check-panel summary { cursor: pointer; font-weight: 600; }
.try-card { margin-bottom: 16px; }
.try-out { margin-top: 8px; min-height: 1.5em; }
.table-card { padding: 0; overflow: hidden; }
.table-card .table-wrap { max-height: 70vh; overflow: auto; }
.table-card thead th { position: sticky; top: 0; background: var(--surface); z-index: 1; }
.table-foot { padding: 10px 12px; border-top: 1px solid var(--border); }
.table.editable td { padding: 4px 6px; }
.table.editable input[type="text"] { padding: 7px 8px; font-size: 15px; min-width: 70px; }
.table.editable td:first-child input { min-width: 220px; }
.table.readonly td { padding: 8px 12px; }
.table .num { text-align: right; }
.table.editable td.num input { text-align: right; width: 100px; }
.table .center { text-align: center; }
.table .center input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--brand); }
@media (max-width: 680px) {
  .table.editable td:first-child input { min-width: 0; }
  .table.editable td.num input { width: 100%; text-align: left; }
  .table .num, .table .center { text-align: left; }
  .table-card .table-wrap { max-height: none; }
  .table-card thead th { position: static; }
  .filters select { width: 100%; }
}

.table th { white-space: nowrap; }
.filters { flex-wrap: nowrap; }
.filters input[type="search"] { flex: 1 1 auto; width: auto; min-width: 0; }
@media (max-width: 680px) { .filters { flex-wrap: wrap; } .filters input[type="search"] { flex-basis: 100%; } }

/* Schedule table on phones: compact two/three-line rows, no repeated labels */
@media (max-width: 680px) {
  .sched-table tr { position: relative; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px 8px; padding: 10px 12px; margin-bottom: 8px; }
  .sched-table td { padding: 0 !important; }
  .sched-table td[data-label]::before { display: none; }
  .sched-table td:first-child { grid-column: 1 / -1; padding-right: 40px !important; font-weight: 600; }
  .sched-table.items.readonly td:nth-child(n+2), .sched-table.labour.readonly td:nth-child(n+2) { font-size: .88rem; color: var(--muted); }
  .sched-table.items td:nth-child(5), .sched-table.items td:nth-child(6) { grid-column: span 3; }
  .sched-table.items.readonly td:nth-child(6):not(:has(*:not(.muted))) { display: none; }
  .sched-table.labour td:nth-child(2) { grid-column: 1 / -1; }
  .sched-table td.actions { position: absolute; top: 6px; right: 6px; }
  .sched-table td.center { display: flex; align-items: center; gap: 6px; font-size: .88rem; }
  .sched-table.editable td.center::after { content: 'Cost +'; color: var(--muted); }
}

/* ===== Phase 3: site form ================================================== */
.offline-badge { margin-right: 4px; }
input[type="time"] {
  width: 100%; padding: 10px 12px; font: 16px/1.3 var(--font);
  color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
[data-theme="dark"] input[type="time"] { color-scheme: dark; }
.record-status { margin: -8px 0 12px; gap: 8px; }
.save-state { font-size: .85rem; }
.save-state.warn { color: var(--warn); }
.save-state.danger { color: var(--danger); font-weight: 600; }
.save-state.muted { color: var(--muted); }
.form-section { margin-bottom: 14px; }
.form-section .section-head h2 { display: flex; align-items: center; gap: 8px; }
.rows > * + * { margin-top: 8px; }
.form-row { display: grid; gap: 8px; align-items: center; padding: 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2); }
.form-row.labour   { grid-template-columns: minmax(0, 1.4fr) minmax(0, 1.4fr) 110px 40px; }
.form-row.operator { grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr) 110px 40px; }
.form-row.item     { grid-template-columns: minmax(0, 1fr) 150px 40px; }
.form-row.other    { grid-template-columns: minmax(0, 1fr) 90px 100px 40px; }
.form-row input, .form-row select { background: var(--surface); }
.with-unit { display: flex; align-items: center; gap: 6px; }
.with-unit input { min-width: 0; }
.with-unit .unit { font-size: .85rem; color: var(--muted); min-width: 1.5em; white-space: nowrap; }
.narrow-input { max-width: 200px; }
.add-row { margin-top: 10px; width: 100%; border-style: dashed; }
.row-x { justify-self: end; }
.pick-btn { justify-content: flex-start; width: 100%; min-height: 44px; font-weight: 500; text-align: left; white-space: normal; background: var(--surface); }
.pick-btn.empty { color: var(--muted); border-style: dashed; }
@media (max-width: 680px) {
  .form-row.labour   { grid-template-columns: minmax(0, 1fr) 40px; }
  .form-row.labour > :nth-child(1) { grid-column: 1; }
  .form-row.labour > :nth-child(2) { grid-column: 1 / -1; grid-row: 2; }
  .form-row.labour > :nth-child(3) { grid-column: 1 / -1; grid-row: 3; }
  .form-row.labour > .row-x { grid-column: 2; grid-row: 1; }
  .form-row.operator { grid-template-columns: minmax(0, 1fr) 40px; }
  .form-row.operator > :nth-child(2), .form-row.operator > :nth-child(3) { grid-column: 1 / -1; }
  .form-row.operator > .row-x { grid-column: 2; grid-row: 1; }
  .form-row.item { grid-template-columns: minmax(0, 1fr) 40px; }
  .form-row.item > :nth-child(2) { grid-column: 1; grid-row: 2; }
  .form-row.item > .row-x { grid-column: 2; grid-row: 1; }
  .form-row.other { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 40px; }
  .form-row.other > :nth-child(1) { grid-column: 1 / 3; }
  .form-row.other > :nth-child(2) { grid-column: 1; grid-row: 2; }
  .form-row.other > :nth-child(3) { grid-column: 2; grid-row: 2; }
  .form-row.other > .row-x { grid-column: 3; grid-row: 1; }
}
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; }
.photo { position: relative; margin: 0; aspect-ratio: 1; border-radius: var(--radius-sm); overflow: hidden; background: var(--surface-2); border: 1px solid var(--border); }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-off { display: grid; place-items: center; height: 100%; padding: 8px; font-size: .75rem; color: var(--muted); text-align: center; }
.photo-badge { position: absolute; left: 4px; bottom: 4px; padding: 1px 6px; border-radius: 999px; font-size: .7rem; background: var(--warn-bg); color: var(--warn); }
.photo-x { position: absolute; top: 4px; right: 4px; width: 30px; height: 30px; display: grid; place-items: center; border: 0; border-radius: 50%; background: rgba(0, 0, 0, .6); color: #fff; cursor: pointer; }
.photo-add { aspect-ratio: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; border: 2px dashed var(--brand); border-radius: var(--radius-sm); background: color-mix(in srgb, var(--brand) 6%, var(--surface)); color: var(--brand); font: 600 .85rem var(--font); cursor: pointer; }
.photo-add.secondary { border-color: var(--border); color: var(--muted); background: var(--surface); }
[data-theme="dark"] .photo-add { color: color-mix(in srgb, var(--brand) 55%, white); }
.file-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; align-items: center; }
.file-chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 6px 4px 10px; border: 1px solid var(--border); border-radius: 999px; font-size: .88rem; max-width: 100%; }
.file-chip span:first-of-type { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px; }
.form-actions {
  position: sticky; bottom: 0; z-index: 6; display: flex; justify-content: space-between; align-items: center; gap: 8px;
  margin: 16px calc(-1 * var(--gutter)) 0; padding: 12px var(--gutter) calc(12px + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--border); box-shadow: 0 -4px 16px rgba(0, 0, 0, .06);
}
.form-actions .btn { min-height: 46px; }
dialog.modal.full { width: min(640px, 100vw); height: min(760px, 100dvh); max-height: 100dvh; }
dialog.modal.full .modal-body { max-height: none; flex: 1; }
dialog.modal.full[open] { display: flex; flex-direction: column; }
@media (max-width: 680px) {
  dialog.modal.full { width: 100vw; height: 100dvh; max-width: 100vw; margin: 0; border-radius: 0; border: 0; }
}
.picker { display: flex; flex-direction: column; gap: 8px; height: 100%; }
.pick-list { overflow-y: auto; flex: 1; max-height: 60dvh; border: 1px solid var(--border); border-radius: var(--radius-sm); }
dialog.modal.full .pick-list { max-height: none; }
.pick-cat { position: sticky; top: 0; padding: 6px 12px; font-size: .75rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); background: var(--surface-2); }
.pick-item { display: flex; justify-content: space-between; gap: 12px; width: 100%; padding: 12px; border: 0; border-bottom: 1px solid var(--border); background: var(--surface); color: var(--text); font: 15px var(--font); text-align: left; cursor: pointer; }
.pick-item:hover, .pick-item:focus-visible { background: color-mix(in srgb, var(--brand) 8%, var(--surface)); outline: none; }
.pick-item.on { font-weight: 700; }
.pad { padding: 12px; }
.record-list > * + * { margin-top: 10px; }
.record-card { display: block; color: var(--text); text-decoration: none; }
.record-card:hover { border-color: var(--brand); }
.record-card .record-desc { margin: 6px 0 0; font-size: .92rem; }
.sync-line:empty { display: none; }

/* ===== Phase 4: signatures ================================================= */
.sigpad {
  --ink: #111;
  position: relative; height: 200px; border: 2px dashed var(--border); border-radius: var(--radius);
  background: #fff; touch-action: none; user-select: none; -webkit-user-select: none;
}
.sigpad canvas { position: absolute; inset: 0; cursor: crosshair; touch-action: none; }
.sigpad-line { position: absolute; left: 24px; right: 24px; bottom: 48px; border-bottom: 1px solid #c8ccd2; pointer-events: none; }
.sigpad-hint { position: absolute; left: 24px; bottom: 24px; font-size: .8rem; color: #8a94a1; pointer-events: none; }
.sigpad.signing .sigpad-hint { display: none; }
.app.narrow-wide { max-width: 760px; }
.sign-head { display: flex; flex-direction: column; gap: 4px; margin-bottom: 16px; }
.sign-logo { max-height: 56px; max-width: 220px; object-fit: contain; align-self: flex-start; }
.sign-company { font-size: 1.2rem; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0 0 16px; }
.facts dt { color: var(--muted); font-size: .9rem; }
.facts dd { margin: 0; }
.prewrap { white-space: pre-wrap; }
.sign-form { margin-top: 16px; }
.agree { border: 1px solid var(--border); border-radius: var(--radius-sm); margin: 4px 0 14px; }
.sign-table { margin-bottom: 12px; }
.sig-block { display: grid; gap: 12px; }
.sig-row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; padding: 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); }
.sig-row img { height: 56px; max-width: 200px; object-fit: contain; background: #fff; border-radius: 4px; padding: 2px; }
.sig-row .grow { min-width: 180px; }
.link-box { display: flex; gap: 8px; }
.link-box input { font-family: var(--mono); font-size: 13px; }
@media (max-width: 680px) { .facts { grid-template-columns: 1fr; gap: 2px; } .facts dd { margin-bottom: 8px; } }
.sign-summary { padding: 12px; margin-bottom: 14px; border-radius: var(--radius-sm); background: var(--surface-2); max-height: 38vh; overflow-y: auto; }
.summary-list { margin: 2px 0 8px; padding-left: 18px; font-size: .9rem; }
.submit-row { margin-top: 14px; gap: 10px; }
#sec-signatures .sig-row .btn { flex: none; }
@media (max-width: 680px) { #sec-signatures .sig-row .btn { flex: 1 1 auto; } }

/* ===== Phase 5: review and register ======================================= */
.register-filters { display: grid; grid-template-columns: 2fr 1.4fr 1.2fr 1.2fr 1.6fr; gap: 8px; margin: 12px 0; }
.register-filters select, .register-filters input { width: 100%; }
.date-range { flex-wrap: nowrap; gap: 6px; }
.date-range input { min-width: 0; }
@media (max-width: 980px) { .register-filters { grid-template-columns: 1fr 1fr; } .register-filters > :first-child { grid-column: 1 / -1; } }
@media (max-width: 520px) { .register-filters { grid-template-columns: 1fr; } }
.register-summary { display: flex; justify-content: space-between; gap: 12px; margin: 0 0 10px; font-size: .92rem; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: color-mix(in srgb, var(--brand) 5%, var(--surface)); }
.clip { max-width: 420px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.price-table td { vertical-align: top; }
.price-table select { min-width: 180px; }
.price-table input[type="text"] { padding: 7px 8px; font-size: 15px; }
.price-table .with-unit input { width: 90px; }
.rate-btn { display: inline-flex; align-items: center; gap: 6px; padding: 6px 8px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); color: var(--text); font: 15px var(--font); cursor: pointer; white-space: nowrap; }
.rate-btn:hover:not(:disabled) { border-color: var(--brand); }
.rate-btn:disabled { cursor: default; border-color: transparent; background: none; }
.rate-tag { display: inline-block; margin-left: 4px; padding: 0 5px; border-radius: 4px; font-size: .68rem; font-weight: 700; letter-spacing: .04em; background: var(--info-bg); color: var(--info); vertical-align: middle; }
.cost-plus { display: flex; flex-direction: column; gap: 4px; align-items: flex-end; }
.cost-plus .with-unit input { width: 110px; }
tr.excluded td { opacity: .55; }
tr.excluded td:first-child { text-decoration: line-through; }
.small-check { padding: 4px 8px; font-size: .85rem; }
.markup-field { max-width: 320px; margin-top: 12px; }
.totals-card { max-width: 460px; margin-left: auto; }
.totals { display: grid; grid-template-columns: 1fr auto; gap: 6px 16px; margin: 0 0 8px; }
.totals dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.totals .strong { font-weight: 700; font-size: 1.1rem; }
.ok-text { color: var(--ok); }
.warn-text { color: var(--warn); }
.form-error.inline { display: inline; padding: 2px 6px; }
.flag-dismiss { margin-left: 8px; }
.history { list-style: none; padding: 0; margin: 0; }
.history li { padding: 8px 0; border-bottom: 1px solid var(--border); }
.history li:last-child { border-bottom: 0; }
@media (max-width: 680px) {
  .price-table select { min-width: 0; width: 100%; }
  .cost-plus { align-items: flex-start; }
  .totals-card { max-width: none; }
  .clip { max-width: 100%; white-space: normal; }
}

.price-table .linkbtn { margin-left: 6px; }

/* --- Phase 6: PDFs and claims ------------------------------------------------- */
dialog.modal.full.pdf { width: min(1040px, 100vw); height: min(94dvh, 1100px); }
.pdf-preview { display: flex; flex-direction: column; gap: 8px; height: 100%; }
.pdf-frame { flex: 1; width: 100%; min-height: 60dvh; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
.busy-note { display: flex; align-items: center; gap: 10px; }
.busy-note .spinner { width: 18px; height: 18px; border-width: 2px; }
.claim-pick td.pick, .claim-pick th.pick { width: 40px; }
.claim-pick td a strong { white-space: nowrap; }
.claim-pick input[type=checkbox] { width: 20px; height: 20px; }
.claim-total { font-size: 1.15rem; font-weight: 700; }
.file-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.file-row .file-chip { flex: 1 1 220px; min-width: 0; }
.file-row .pages-note { font-size: .85rem; color: var(--muted); }
.file-list.stacked { flex-direction: column; align-items: stretch; flex-wrap: nowrap; }
.file-block { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.ai-panel { display: flex; flex-direction: column; gap: 8px; margin-left: 12px; padding: 10px 12px; border-left: 3px solid var(--brand); background: var(--surface-2); border-radius: 0 var(--radius-sm) var(--radius-sm) 0; min-width: 0; }
.ai-panel p { margin: 0; }
.ai-panel .flags { margin: 0; }
.ai-status { display: flex; align-items: center; gap: 8px; }
.ai-status .spinner, .spinner.small { width: 16px; height: 16px; border-width: 2px; }
.invoice-details .field { margin-bottom: 8px; }
.ai-usage { margin-top: 10px; }
@media (max-width: 760px) { .ai-panel { margin-left: 0; } }
.signature-box { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.signature-box img { max-width: 260px; max-height: 90px; background: #fff; border: 1px solid var(--border); border-radius: 8px; padding: 6px; }
.signature-pad-wrap { max-width: 640px; }
.bulk-bar { gap: 16px; margin: 8px 0 12px; flex-wrap: wrap; }
table.register td.pick, table.register th.pick { width: 40px; }
table.register td.pick input { width: 20px; height: 20px; }
/* Review: side-by-side PDF preview on wide screens */
.review-layout.with-preview { display: grid; grid-template-columns: minmax(0, 1fr) 12px var(--preview-w, minmax(420px, 44%)); gap: 4px; align-items: start; }
.pdf-divider { display: none; }
.review-layout.with-preview .pdf-divider { display: block; position: sticky; top: 72px; height: calc(100dvh - 90px); cursor: col-resize; border-radius: 6px; touch-action: none; }
.review-layout.with-preview .pdf-divider::after { content: ''; display: block; width: 4px; height: 48px; margin: 40vh auto 0; border-radius: 4px; background: var(--border); }
.review-layout.with-preview .pdf-divider:hover::after, .pdf-divider:focus-visible::after { background: var(--brand); }
.review-layout.dragging { user-select: none; }
.review-layout.dragging iframe { pointer-events: none; }
.pdf-side { position: sticky; top: 72px; height: calc(100dvh - 90px); display: flex; flex-direction: column; gap: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; }
.pdf-side[hidden] { display: none; }
.pdf-side .pdf-frame { flex: 1; min-height: 0; }
.pdf-side-bar .row { gap: 6px; }
@media (max-width: 1299px) {
  .wide-only { display: none !important; }
  .review-layout.with-preview { display: block; }
  .review-layout.with-preview .pdf-divider { display: none; }
  .review-layout.with-preview .pdf-side { display: none; }
}
.app.wide:has(.review-layout.with-preview) { max-width: 1840px; }
/* Setup checklist */
.ob-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.ob-step { display: flex; gap: 12px; align-items: flex-start; padding: 10px 4px; border-bottom: 1px solid var(--border); }
.ob-step:last-child { border-bottom: 0; }
.ob-mark { flex: 0 0 26px; height: 26px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  font-size: .85rem; font-weight: 700; background: var(--surface-2); color: var(--muted); }
.ob-step.done .ob-mark { background: var(--ok-bg); color: var(--ok); }
.ob-step .check-row input { width: 20px; height: 20px; flex: 0 0 auto; }
.ob-progress { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin: 4px 0 16px; }
.ob-progress span { display: block; height: 100%; background: var(--ok); }
/* Never wider than the screen, whatever the font metrics */
.clip { max-width: min(420px, 100%); }
.register-filters > * { min-width: 0; }
.kind-badge { margin-left: 6px; font-size: .72rem; vertical-align: middle; }

/* Schedule of rates side panel (non-modal, over the page) */
dialog.rates-panel {
  position: fixed; inset: 0 0 0 auto; margin: 0; width: min(420px, 100vw); height: 100dvh; max-height: 100dvh;
  padding: 0; border: 0; border-left: 1px solid var(--border); background: var(--surface); color: var(--text);
  box-shadow: -8px 0 24px rgba(0, 0, 0, .18); z-index: 60; overflow: hidden;
}
dialog.rates-panel[open] { display: flex; flex-direction: column; }
.rates-head { display: flex; align-items: center; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.rates-tools { padding: 12px 16px 4px; display: flex; flex-direction: column; gap: 8px; }
.rates-tools p { margin: 0; }
.rates-tabs { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; }
.rates-tab { flex: none; padding: 4px 10px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); color: var(--text); font: 500 .82rem var(--font); cursor: pointer; }
.rates-tab.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.rates-list { flex: 1; overflow-y: auto; padding: 4px 8px 16px; }
.rates-row { display: flex; align-items: center; gap: 8px; padding: 8px; border-bottom: 1px solid var(--border); min-width: 0; }
.rates-row .grow { min-width: 0; overflow-wrap: anywhere; }
.rates-tiers { display: flex; gap: 4px; flex: none; }
.rate-copy { flex: none; padding: 3px 8px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2); color: var(--text); font: 600 .82rem var(--font); font-variant-numeric: tabular-nums; cursor: copy; }
.rate-copy:hover { border-color: var(--brand); }
@media (max-width: 480px) { .rates-tiers { flex-direction: column; } }
