/* ==========================================================================
   RawFlow BuckPay — design tokens + components
   Plain CSS, no framework, no CDN. Everything here is CSP-safe.
   ========================================================================== */

:root {
  /* Brand — provisional; the Zylocon rebrand swaps these four and nothing else. */
  --brand: #2f855a;
  --brand-dark: #276749;
  --brand-tint: #e9f5ef;
  --brand-ring: #2f855a40;

  /* Neutrals */
  --ink: #16202b;
  --ink-soft: #475569;
  --muted: #64748b;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-alt: #f8fafc;

  /* Semantic */
  --ok: #15803d;      --ok-bg: #ecfdf3;   --ok-line: #a7f3c8;
  --warn: #b45309;    --warn-bg: #fffbeb; --warn-line: #fcd9a4;
  --err: #b42318;     --err-bg: #fef3f2;  --err-line: #fecdca;
  --info: #1d4ed8;    --info-bg: #eff6ff; --info-line: #bfdbfe;

  /* Spacing scale (4pt rhythm) */
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px; --s7: 48px;

  /* Type scale */
  --t-xs: 12px; --t-sm: 13px; --t-md: 14px; --t-base: 16px; --t-lg: 18px; --t-xl: 22px; --t-2xl: 28px;

  --r-sm: 6px; --r-md: 10px; --r-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(16, 32, 43, 0.06);
  --shadow-md: 0 4px 12px rgba(16, 32, 43, 0.08);
  --header-h: 56px;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  font-size: var(--t-base);
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--brand-dark); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Visible focus everywhere. Never remove this. */
:where(a, button, input, select, textarea, summary):focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/*
 * Every inline SVG needs an explicit size. An <svg> with no width/height falls
 * back to the CSS default 300×150 — which inside a flex row silently crushes
 * its siblings to zero width and makes them *invisible* rather than merely
 * ugly. This default has to come before any scoped override.
 */
.icon { width: 16px; height: 16px; flex: none; display: inline-block; vertical-align: -0.15em; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute; left: var(--s3); top: -60px; z-index: 100;
  background: var(--surface); color: var(--brand-dark); font-weight: 600;
  padding: var(--s2) var(--s3); border-radius: var(--r-sm); box-shadow: var(--shadow-md);
  transition: top 150ms ease-out;
}
.skip-link:focus { top: var(--s3); }

/* ── Header / navigation ─────────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--s4); height: var(--header-h); padding: 0 var(--s5);
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand a { display: inline-flex; align-items: center; gap: var(--s2); font-size: var(--t-lg); font-weight: 700; color: var(--ink); }
.brand a:hover { text-decoration: none; }
.brand-text span { color: var(--brand); }

.mainnav { display: flex; align-items: center; gap: var(--s1); }
.mainnav a {
  display: inline-flex; align-items: center; gap: var(--s2);
  padding: var(--s2) var(--s3); border-radius: var(--r-sm);
  font-size: var(--t-md); font-weight: 600; color: var(--ink-soft);
  transition: background 120ms ease-out, color 120ms ease-out;
}
.mainnav a:hover { background: var(--surface-alt); color: var(--ink); text-decoration: none; }
/* Current section — not colour alone: it also gets weight and a bar. */
.mainnav a[aria-current="page"] { background: var(--brand-tint); color: var(--brand-dark); box-shadow: inset 0 -2px 0 var(--brand); }
.mainnav .icon { width: 18px; height: 18px; flex: none; }

.usermenu { display: flex; align-items: center; gap: var(--s3); margin-left: var(--s3); padding-left: var(--s3); border-left: 1px solid var(--line); }
.who { color: var(--muted); font-size: var(--t-sm); }
.inline { display: inline; margin: 0; }
button.link { background: none; border: 0; color: var(--brand-dark); cursor: pointer; font-size: var(--t-sm); font-weight: 600; padding: var(--s1) var(--s2); border-radius: var(--r-sm); }
button.link:hover { background: var(--surface-alt); }

.container { max-width: 1240px; margin: 0 auto; padding: var(--s5) var(--s5) var(--s7); }

/* ── Page head ───────────────────────────────────────────────────────────── */
.page-head { display: flex; align-items: center; gap: var(--s3); flex-wrap: wrap; margin: 0 0 var(--s2); }
.page-head h1 { font-size: var(--t-2xl); line-height: 1.2; margin: 0; letter-spacing: -0.01em; }
.status { margin: 0 0 var(--s5); color: var(--muted); font-size: var(--t-md); max-width: 78ch; }

.badge {
  display: inline-flex; align-items: center; gap: var(--s1);
  background: var(--brand-tint); color: var(--brand-dark);
  font-size: var(--t-xs); font-weight: 700; padding: var(--s1) var(--s2);
  border-radius: 999px; white-space: nowrap;
}

/* ── Flash ───────────────────────────────────────────────────────────────── */
.flash { border-radius: var(--r-md); padding: var(--s3) var(--s4); margin: 0 0 var(--s4); font-size: var(--t-md); border: 1px solid; }
.flash ul { margin: var(--s2) 0 0; padding-left: var(--s4); }
.flash-msg { font-weight: 600; }
.flash-success { background: var(--ok-bg); color: var(--ok); border-color: var(--ok-line); }
.flash-error { background: var(--err-bg); color: var(--err); border-color: var(--err-line); }
.flash-info { background: var(--info-bg); color: var(--info); border-color: var(--info-line); }

/* ── Collapsible panel — native <details>, keyboard-accessible, JS-free ──── */
.panel {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-lg); margin-bottom: var(--s4); box-shadow: var(--shadow-sm);
  overflow: hidden;
}
.panel > summary {
  display: flex; align-items: center; gap: var(--s3);
  padding: var(--s3) var(--s4); cursor: pointer; list-style: none;
  font-weight: 650; font-size: var(--t-base); color: var(--ink);
  background: var(--surface); user-select: none; min-height: 48px;
}
.panel > summary::-webkit-details-marker { display: none; }
.panel > summary:hover { background: var(--surface-alt); }
/* The disclosure chevron is the summary's first icon; it rotates when open. */
.panel > summary > .icon:first-child { width: 18px; height: 18px; color: var(--muted); transition: transform 160ms ease-out; }
.panel[open] > summary > .icon:first-child { transform: rotate(90deg); }
.panel[open] > summary { border-bottom: 1px solid var(--line); }
.panel-title { flex: 1; min-width: 0; font-size: var(--t-base); margin: 0; }
.panel-meta { color: var(--muted); font-weight: 500; font-size: var(--t-sm); white-space: nowrap; }
.panel-body { padding: var(--s4); }
.panel-body > :first-child { margin-top: 0; }
.panel-body > :last-child { margin-bottom: 0; }
.panel-body.flush { padding: 0; }
.panel.accent > summary { box-shadow: inset 3px 0 0 var(--brand); }

/* ── Section jump bar ────────────────────────────────────────────────────── */
.jumpbar {
  position: sticky; top: var(--header-h); z-index: 30;
  display: flex; align-items: center; gap: var(--s2); flex-wrap: wrap;
  padding: var(--s2) var(--s3); margin: 0 0 var(--s4);
  background: rgba(244, 246, 248, 0.92); backdrop-filter: blur(6px);
  border: 1px solid var(--line); border-radius: var(--r-md);
}
.jumpbar .label { font-size: var(--t-xs); font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.jumpbar a {
  font-size: var(--t-sm); font-weight: 600; color: var(--ink-soft);
  padding: var(--s1) var(--s2); border-radius: var(--r-sm);
}
.jumpbar a:hover { background: var(--surface); color: var(--brand-dark); text-decoration: none; }
.jumpbar a.collapsed { color: var(--muted); }
/* A collapsed section is marked with a dot, not just a lighter grey. */
.jumpbar a.collapsed::after { content: '·'; margin-left: 4px; font-weight: 700; }
.jumpbar .spacer { flex: 1; }

/* ── Page toolbar (week picker + at-a-glance state) ──────────────────────── */
.toolbar {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: var(--s4); flex-wrap: wrap; margin: 0 0 var(--s4);
  padding: var(--s3) var(--s4); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--r-md);
}
.toolbar .weekbar { margin: 0; }

h2.sect { font-size: var(--t-xl); margin: var(--s6) 0 var(--s2); letter-spacing: -0.01em; }

/* ── Sub tabs ────────────────────────────────────────────────────────────── */
.subtabs { display: flex; gap: var(--s1); margin: 0 0 var(--s4); border-bottom: 1px solid var(--line); overflow-x: auto; }
.subtab {
  padding: var(--s3) var(--s4); font-size: var(--t-md); font-weight: 600; color: var(--muted);
  border-bottom: 2px solid transparent; text-transform: capitalize; white-space: nowrap;
}
.subtab:hover { color: var(--ink); text-decoration: none; background: var(--surface-alt); }
.subtab.on { color: var(--brand-dark); border-bottom-color: var(--brand); }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  min-height: 38px; padding: var(--s2) var(--s4);
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--r-sm);
  font-size: var(--t-md); font-weight: 600; color: var(--ink); cursor: pointer;
  transition: background 120ms ease-out, border-color 120ms ease-out;
}
.btn:hover { background: var(--surface-alt); border-color: var(--muted); text-decoration: none; }
.btn:active { background: var(--line); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); border-color: var(--brand-dark); }
.btn.block { width: 100%; }
.btn.sm { min-height: 30px; padding: var(--s1) var(--s2); font-size: var(--t-xs); }
.btn.icon-only { min-width: 34px; padding: var(--s1); }
.btn.danger { color: var(--err); border-color: var(--err-line); }
.btn.danger:hover { background: var(--err-bg); border-color: var(--err); }
.btn[disabled], .btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn .icon { width: 16px; height: 16px; flex: none; }
.btn-row { display: flex; gap: var(--s2); align-items: center; flex-wrap: wrap; margin: var(--s3) 0 var(--s4); }

/* ── Chips ───────────────────────────────────────────────────────────────── */
.chip {
  display: inline-flex; align-items: center; gap: var(--s1);
  font-size: var(--t-xs); font-weight: 700; padding: var(--s1) var(--s2);
  border-radius: 999px; border: 1px solid transparent; white-space: nowrap;
}
.chip.ok { background: var(--ok-bg); color: var(--ok); border-color: var(--ok-line); }
.chip.wait { background: var(--warn-bg); color: var(--warn); border-color: var(--warn-line); }
.chip.err { background: var(--err-bg); color: var(--err); border-color: var(--err-line); }
.chip .icon { width: 13px; height: 13px; }
.readiness { display: flex; gap: var(--s2); flex-wrap: wrap; align-items: center; }

/* ── Forms ───────────────────────────────────────────────────────────────── */
input, select, textarea { font: inherit; color: inherit; }
input[type=text], input[type=date], input[type=file], input:not([type]), select, textarea {
  padding: var(--s2) var(--s3); border: 1px solid var(--line-strong);
  border-radius: var(--r-sm); background: var(--surface); font-size: var(--t-md); min-height: 38px;
}
input:hover, select:hover { border-color: var(--muted); }
.fld { display: flex; flex-direction: column; gap: var(--s1); font-size: var(--t-xs); font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.fld input, .fld select { min-width: 130px; text-transform: none; letter-spacing: normal; font-weight: 400; color: var(--ink); }
.chk { display: inline-flex; align-items: center; gap: var(--s2); font-size: var(--t-md); color: var(--ink-soft); }
.chk input { min-height: auto; width: 18px; height: 18px; }

.add-row {
  display: flex; gap: var(--s3); align-items: flex-end; flex-wrap: wrap;
  padding: var(--s4); background: var(--surface-alt);
  border: 1px dashed var(--line-strong); border-radius: var(--r-md);
}
.add-row input { min-width: 160px; }
/* …but not the checkbox, which would otherwise be a 160px-wide tick box. */
.add-row .chk input { min-width: 0; }
.add-row .chk { align-self: center; padding-bottom: var(--s2); }
.hint { color: var(--muted); font-size: var(--t-sm); margin: var(--s2) 0 0; max-width: 78ch; }

/* ── Tables ──────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
.grid { width: 100%; border-collapse: separate; border-spacing: 0; background: var(--surface); font-size: var(--t-md); }
.grid th, .grid td { text-align: left; padding: var(--s2) var(--s3); border-bottom: 1px solid var(--line); }
.grid thead th {
  background: var(--surface-alt); color: var(--muted);
  font-size: var(--t-xs); font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  border-bottom: 1px solid var(--line-strong); white-space: nowrap;
}
/*
 * NO sticky column headers — deliberately.
 *
 * Every table here sits in a `.table-wrap` with `overflow-x: auto`, which makes
 * that wrapper a scroll container, which makes it the containing block for
 * `position: sticky`. The header then offsets DOWN from the wrapper's own top
 * by whatever `top` says and parks there permanently, on top of the first rows
 * — it never tracks the page scroll at all, because the wrapper itself never
 * scrolls vertically. On the Tickets grid, where every cell IS its own editor,
 * that made the top rows unclickable; Playwright timed out on them for 31s a
 * row. The audit table showed the same header sitting over row 1 at rest.
 *
 * Sticky headers would need the wrapper to be the vertical scroller too, i.e. a
 * nested scroll region inside the page — worse than the problem. Collapsible
 * panels and per-table filters cover the same need instead.
 */
.grid tbody tr, .grid td, .panel { scroll-margin-top: calc(var(--header-h) + 64px); }
.grid tbody tr:last-child td { border-bottom: 0; }
.grid tbody tr:hover { background: var(--surface-alt); }
/* Numeric columns shrink to their content so the names get the leftover width. */
.grid td.num, .grid th.num { text-align: right; font-variant-numeric: tabular-nums; width: 1%; white-space: nowrap; }
.grid td.actions, .grid th.actions { white-space: nowrap; width: 1%; text-align: right; }
.grid td.actions .btn + .btn { margin-left: var(--s1); }
.grid tr.inactive td { opacity: 0.5; }
.grid tr.inactive td:first-child::after { content: ' (inactive)'; font-size: var(--t-xs); color: var(--muted); }
.grid input:not([type=checkbox]) { width: 100%; min-height: 32px; padding: var(--s1) var(--s2); font-size: var(--t-sm); }
.grid select { min-height: 32px; padding: var(--s1) var(--s2); font-size: var(--t-sm); }
/* A money field is 6 characters wide. Stretching it across the column reads as
   a text field and puts the caret nowhere near the digits. */
.grid td.num input { max-width: 110px; margin-left: auto; text-align: right; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }
/* `.grid th, .grid td` sets text-align, so the empty state has to out-specify it. */
.grid td.empty { padding: var(--s6) var(--s4); text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--ink); font-size: var(--t-base); margin-bottom: var(--s1); }

/* Live filter box above a table */
.filterbar { display: flex; align-items: center; gap: var(--s3); flex-wrap: wrap; margin-bottom: var(--s3); }
.filterbar input { min-width: 300px; }
.filter-count { font-size: var(--t-sm); color: var(--muted); }
tr[hidden] { display: none; }

/* ── Tickets ─────────────────────────────────────────────────────────────── */
.weekbar { display: flex; gap: var(--s3); align-items: flex-end; flex-wrap: wrap; }
table.tickets td { font-size: var(--t-sm); }
table.tickets .cell { cursor: pointer; min-width: 64px; position: relative; }
table.tickets .cell:hover { background: var(--brand-tint); box-shadow: inset 0 0 0 1px var(--brand-ring); }
table.tickets .cell.red { background: var(--err-bg); box-shadow: inset 0 0 0 1px var(--err-line); }
table.tickets .cell.red:empty::after { content: 'required'; color: var(--err); font-weight: 700; font-size: var(--t-xs); }
table.tickets .cell.saving { opacity: 0.45; }
table.tickets .cell.save-error { background: #fee4e2; box-shadow: inset 0 0 0 2px var(--err); }
table.tickets .cell select, table.tickets .cell input { border-color: var(--brand); }
table.tickets tr.no-rate td { background: var(--warn-bg); }
table.tickets tr.no-rate .rate { color: var(--warn); font-weight: 700; }
table.tickets tr.extra td { background: var(--brand-tint); }
table.tickets td.extra-label strong { color: var(--brand-dark); }
table.tickets td.amount { font-variant-numeric: tabular-nums; font-weight: 650; }
table.tickets td.rate { font-variant-numeric: tabular-nums; }
table.tickets tr.scan-row td { background: var(--surface-alt); text-align: center; padding: var(--s4); }
.scan-img { max-width: 720px; max-height: 540px; border: 1px solid var(--line); border-radius: var(--r-md); background: #fff; }

/* ── Rates ───────────────────────────────────────────────────────────────── */
.grid tr.group-head td { background: var(--surface-alt); font-weight: 700; font-size: var(--t-xs); text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }

/* ── Reports ─────────────────────────────────────────────────────────────── */
.steps { display: grid; gap: var(--s3); grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); margin: 0 0 var(--s4); padding: 0; list-style: none; }
.step { display: flex; gap: var(--s3); align-items: flex-start; padding: var(--s4); background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-md); }
.step .n { display: grid; place-items: center; width: 26px; height: 26px; flex: none; border-radius: 999px; background: var(--line); color: var(--ink-soft); font-weight: 700; font-size: var(--t-sm); }
.step.done .n { background: var(--ok); color: #fff; }
.step.active .n { background: var(--brand); color: #fff; }
.step h3 { margin: 0 0 var(--s1); font-size: var(--t-md); }
.step p { margin: 0; font-size: var(--t-sm); color: var(--muted); }
table.comparison td.won { font-weight: 700; color: var(--ok); }
table.comparison td.total, table.comparison th.total { font-weight: 700; }
table.comparison tfoot th {
  background: var(--surface-alt); border-top: 2px solid var(--line-strong);
  text-align: left; font-size: var(--t-sm); padding: var(--s2) var(--s3);
}
table.comparison tfoot th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.preview { font-size: var(--t-xs); }
table.preview tr.totals-row th {
  background: var(--surface-alt); border-top: 2px solid var(--line-strong);
  text-align: left; font-size: var(--t-xs); text-transform: uppercase; letter-spacing: 0.03em;
}
table.preview tr.totals-row th.num { text-align: right; }

/* ── Audit ───────────────────────────────────────────────────────────────── */
table.audit td { vertical-align: top; font-size: var(--t-xs); }
table.audit code { background: var(--surface-alt); border: 1px solid var(--line); padding: 1px 6px; border-radius: var(--r-sm); font-size: 11px; }
table.audit .changes div { margin-bottom: 2px; }
table.audit .old { color: var(--err); text-decoration: line-through; }
table.audit .new { color: var(--ok); font-weight: 600; }

/* ── Login ───────────────────────────────────────────────────────────────── */
.login-card {
  max-width: 380px; margin: 8vh auto; background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--r-lg); padding: var(--s6);
  text-align: center; box-shadow: var(--shadow-md);
}
.login-logo { font-size: var(--t-xl); margin: 0 0 var(--s1); }
.login-logo span { color: var(--brand); }
.login-form { display: flex; flex-direction: column; gap: var(--s1); margin-top: var(--s5); text-align: left; }
.login-form label { font-size: var(--t-sm); font-weight: 600; color: var(--muted); }
.login-form label + input { margin-bottom: var(--s3); }
.login-form .btn { margin-top: var(--s3); }

/* Hidden container for row-scoped forms referenced via the `form` attribute. */
.row-forms { display: none; }

.foot { text-align: center; color: var(--muted); font-size: var(--t-xs); padding: var(--s5); }

@media (max-width: 900px) {
  .container { padding: var(--s4) var(--s3) var(--s6); }
  .topbar { padding: 0 var(--s3); }
  .mainnav a .label { display: none; }
  .steps { grid-template-columns: 1fr; }
}
