:root {
  /* Light by design - a single committed visual world, not an OS-dependent flip. */
  --paper: #FAFBFB;          /* page ground, faint cool cast */
  --panel: #FFFFFF;          /* cards and tables */
  --ink: #1B2426;            /* primary text */
  --muted: #6B7876;          /* secondary text */
  --accent: #0E6F56;         /* brand green, UI only */
  --accent-soft: #E9F3EF;
  --gold: #8A6D28;
  --gold-soft: #FBF4E2;
  --line: #E3E9E7;           /* hairlines */
  --tint: #F3F7F5;           /* table headers, wells */
  --ok: #0E6F56; --warn: #9A6B12; --bad: #A93B25;

  /* Chart categorical ramp - validated for colour-vision deficiency
     (worst adjacent pair ΔE 9.7 protan / 27.8 tritan on a white surface).
     Slot order is fixed: money in, money out, third measure. */
  --series-1: #0D8A66;
  --series-2: #C25E12;
  --series-3: #3352A0;
  --grid: #EDF1F0;

  --sans: "Avenir Next", "Avenir", Seravek, "Segoe UI", system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
/* A `display` rule beats the hidden attribute, so state it explicitly. */
[hidden] { display: none !important; }
body {
  margin: 0; background: var(--paper); color: var(--ink);
  font-family: var(--sans); font-size: 14.5px; line-height: 1.5;
}
.app { display: grid; grid-template-columns: 232px minmax(0,1fr); min-height: 100vh; }

/* ---------- sidebar ---------- */
.sidebar {
  border-right: 1px solid var(--line); padding: 1.4rem 1rem 1rem;
  display: flex; flex-direction: column; gap: 1rem;
  position: sticky; top: 0; height: 100vh;
  /* The nav is longer than a short window: let it scroll on its own, and keep
     that scroll from chaining into the page behind it. */
  overflow-y: auto; overscroll-behavior: contain;
  scrollbar-width: thin; scrollbar-color: var(--line) transparent;
}
.sidebar::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.brand { display: flex; gap: 0.65rem; align-items: center; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 8px; background: var(--accent);
  color: #fff; display: grid; place-items: center; font-size: 1.25rem; font-weight: 600;
}
.brand-name { font-weight: 700; font-size: 1.05rem; letter-spacing: -0.01em; }
.brand-sub { font-size: 0.68rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.12em; }
.switcher-label { font-size: 0.64rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); }
.switcher {
  width: 100%; padding: 0.5rem 0.6rem; border: 1px solid var(--line); border-radius: 6px;
  background: var(--panel); color: var(--ink); font-family: inherit; font-size: 0.85rem;
}
.nav { display: flex; flex-direction: column; gap: 2px; margin-top: 0.4rem; }
.nav a {
  padding: 0.5rem 0.7rem; border-radius: 6px; color: var(--muted);
  text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--tint); color: var(--ink); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav a:focus-visible { outline: 2px solid var(--accent); }
.sidebar-foot { margin-top: auto; font-size: 0.7rem; color: var(--muted); line-height: 1.5; }

/* ---------- main ---------- */
.main { padding: 1.8rem 2.2rem 3rem; min-width: 0; }
.page-title { font-size: 1.35rem; font-weight: 700; letter-spacing: -0.015em; margin: 0 0 0.2rem; }
.page-sub { color: var(--muted); margin: 0 0 1.4rem; font-size: 0.86rem; }

.grid { display: grid; gap: 1rem; }
.grid.cols3 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 1rem 1.15rem;
  min-width: 0; container-type: inline-size; }
.grid > * { min-width: 0; }
.card h3 {
  margin: 0 0 0.55rem; font-size: 0.68rem; text-transform: uppercase;
  letter-spacing: 0.12em; color: var(--muted); font-weight: 600;
}
/* Figures shrink to their card instead of spilling out of it. */
.big {
  font-size: clamp(1.05rem, 8cqi, 1.55rem); font-weight: 700; line-height: 1.15;
  font-variant-numeric: tabular-nums; letter-spacing: -0.01em;
  white-space: nowrap; overflow: hidden; text-overflow: clip; max-width: 100%;
}
@container (max-width: 200px) { .big { font-size: clamp(0.95rem, 9cqi, 1.2rem); } }
.card .sub { overflow-wrap: anywhere; }
.sub { color: var(--muted); font-size: 0.8rem; }

.healthbar {
  display: flex; flex-wrap: wrap; gap: 1rem; align-items: center;
  border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
  padding: 0.9rem 1.15rem; margin-bottom: 1.2rem;
}
.days-badge {
  min-width: 64px; text-align: center; border-radius: 8px; padding: 0.45rem 0.6rem;
  font-weight: 700; font-size: 1.15rem; font-variant-numeric: tabular-nums;
}
.days-ok { background: var(--accent-soft); color: var(--ok); }
.days-warn { background: var(--gold-soft); color: var(--warn); }
.days-bad { background: color-mix(in srgb, var(--bad) 14%, transparent); color: var(--bad); }

.pill {
  display: inline-block; font-size: 0.64rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.08em; border-radius: 999px; padding: 0.14rem 0.6rem;
}
.pill.green { background: var(--accent-soft); color: var(--ok); }
.pill.gold { background: var(--gold-soft); color: var(--warn); }
.pill.red { background: color-mix(in srgb, var(--bad) 14%, transparent); color: var(--bad); }
.pill.grey { background: var(--tint); color: var(--muted); }

.progress { height: 8px; border-radius: 99px; background: var(--tint); overflow: hidden; margin: 0.5rem 0 0.3rem; }
.progress > div { height: 100%; background: var(--accent); }

.tablewrap {
  overflow-x: auto; overscroll-behavior-x: contain;
  border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
}
.tablewrap thead th { position: sticky; top: 0; z-index: 1; background: var(--tint); }
/* A nested table (expanded ageing rows, journal detail) must not sticky-stack. */
.tablewrap table table thead th { position: static; }
table { border-collapse: collapse; width: 100%; font-size: 0.82rem; }
th {
  text-align: left; font-size: 0.63rem; text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--muted); background: var(--tint); padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--line); white-space: nowrap;
}
td { padding: 0.5rem 0.75rem; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

button, .btn {
  font-family: inherit; font-size: 0.84rem; font-weight: 600; border-radius: 6px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  padding: 0.5rem 0.95rem; cursor: pointer;
}
button:hover { background: var(--tint); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.primary:disabled { opacity: 0.45; cursor: not-allowed; }
button.small { padding: 0.28rem 0.6rem; font-size: 0.74rem; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

input, select, textarea {
  font-family: inherit; font-size: 0.86rem; color: var(--ink); background: var(--panel);
  border: 1px solid var(--line); border-radius: 6px; padding: 0.45rem 0.6rem; width: 100%;
}
input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 0; }
label.fld { display: block; font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); margin-bottom: 0.25rem; font-weight: 600; }
.fld-group { margin-bottom: 0.85rem; }

/* ---------- invoice editor ---------- */
.editor-grid { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 1.2rem; align-items: start; }
@media (max-width: 980px) { .editor-grid { grid-template-columns: minmax(0, 1fr); } }
.type-banner {
  border-radius: 8px; padding: 0.75rem 1.05rem; margin-bottom: 1rem;
  font-weight: 600; border: 1px solid var(--line); background: var(--panel);
  display: flex; gap: 0.8rem; align-items: center; flex-wrap: wrap;
}
.type-banner .headline { font-size: 1.02rem; letter-spacing: 0.02em; }
.type-banner .why { color: var(--muted); font-weight: 400; font-size: 0.8rem; }
.type-standard { border-left: 4px solid var(--gold); }
.type-simplified { border-left: 4px solid var(--accent); }
.lines-table input, .lines-table select { min-width: 70px; }
.lines-table td { padding: 0.35rem 0.4rem; }
.violation { display: flex; gap: 0.55rem; padding: 0.5rem 0; border-bottom: 1px solid var(--line); font-size: 0.8rem; }
.violation:last-child { border-bottom: 0; }
.v-dot { flex: none; width: 8px; height: 8px; border-radius: 99px; margin-top: 5px; }
.v-error .v-dot { background: var(--bad); }
.v-warning .v-dot { background: var(--warn); }
.allgreen { color: var(--ok); font-weight: 600; font-size: 0.85rem; }
.totals-row { display: flex; justify-content: space-between; padding: 0.3rem 0; font-size: 0.86rem; }
.totals-row.grand { font-weight: 700; font-size: 1rem; border-top: 1px solid var(--line); margin-top: 0.4rem; padding-top: 0.6rem; }
.totals-row .num { font-variant-numeric: tabular-nums; }
.aed-vat { background: var(--gold-soft); border-radius: 6px; padding: 0.45rem 0.6rem; margin-top: 0.5rem; font-size: 0.8rem; }

/* ---------- calendar ---------- */
.month-group { margin-bottom: 1.3rem; }
.month-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); margin: 0 0 0.5rem; font-weight: 700; }
.event {
  display: flex; gap: 1rem; align-items: flex-start; background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.5rem;
}
.event-date { flex: none; text-align: center; min-width: 52px; }
.event-date .d { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.event-date .m { font-size: 0.62rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.event-body { min-width: 0; flex: 1; }
.event-title { font-weight: 600; }
.event-meta { font-size: 0.76rem; color: var(--muted); margin-top: 0.15rem; }
.shifted-note { color: var(--gold); font-size: 0.76rem; margin-top: 0.15rem; }

/* ---------- misc ---------- */
.sevchip { font-family: var(--mono); font-size: 0.68rem; padding: 0.1rem 0.5rem; border-radius: 4px; font-weight: 700; text-transform: uppercase; }
.sev-blocker { background: color-mix(in srgb, var(--bad) 15%, transparent); color: var(--bad); }
.sev-warning { background: var(--gold-soft); color: var(--warn); }
.sev-info { background: var(--tint); color: var(--muted); }
.mono { font-family: var(--mono); font-size: 0.8em; }
.checklist li { list-style: none; padding: 0.3rem 0; }
.checklist { padding-left: 0; margin: 0.4rem 0; }
.check-ok::before { content: "✓ "; color: var(--ok); font-weight: 700; }
.check-bad::before { content: "✕ "; color: var(--bad); font-weight: 700; }
/* ---------- login ---------- */
.login-screen { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.login-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; padding: 1.8rem;
}
.login-error {
  background: color-mix(in srgb, var(--bad) 12%, transparent); color: var(--bad);
  border-radius: 6px; padding: 0.55rem 0.75rem; font-size: 0.82rem;
  font-weight: 600; margin-bottom: 0.8rem;
}
.login-foot { color: var(--muted); font-size: 0.75rem; margin: 1rem 0 0; line-height: 1.5; }
.enquiry-card {
  margin-top: 1.1rem; border-left: 3px solid var(--accent);
  background: linear-gradient(to right, var(--accent-soft) 0%, var(--panel) 38%);
}
.enquiry-card textarea {
  width: 100%; padding: 0.5rem 0.6rem; border: 1px solid var(--line); border-radius: 6px;
  background: var(--panel); color: var(--ink); font: inherit; resize: vertical;
}
.login-intro { color: var(--muted); font-size: 0.8rem; line-height: 1.5; margin: 0 0 1.1rem; }
/* A button that reads as a link: the switch between signing in and signing up. */
.linkish {
  background: none; border: 0; padding: 0; font: inherit; color: var(--accent);
  font-weight: 600; cursor: pointer; text-decoration: underline; text-underline-offset: 2px;
}
.linkish:hover { color: var(--ink); }
/* While a new account is finishing its company profile there is nowhere else to go. */
body.onboarding .nav,
body.onboarding #companyPicker { display: none; }
.whoami {
  border: 1px solid var(--line); border-radius: 8px; padding: 0.6rem 0.7rem;
  margin-bottom: 0.8rem; display: flex; flex-direction: column; gap: 0.35rem;
  align-items: flex-start;
}
.whoami > div:first-child { font-weight: 700; color: var(--ink); font-size: 0.82rem; }
.credential-box {
  background: var(--gold-soft); border: 1px solid var(--gold); border-radius: 8px;
  padding: 0.9rem 1.1rem; margin: 1rem 0;
}
.credential-box .cred { font-family: var(--mono); font-size: 1.05rem; font-weight: 700; }

.toolbar { display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; }
.tabbar { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-bottom: 1rem; }
.tabbar button.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.preset-row { display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: end; }
a.dl { display: inline-block; text-decoration: none; font-weight: 600; font-size: 0.84rem;
  border: 1px solid var(--accent); color: var(--accent); border-radius: 6px; padding: 0.5rem 0.95rem; }
a.dl:hover { background: var(--accent-soft); }
.spacer { flex: 1; }
.toast {
  position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%) translateY(120%);
  background: var(--ink); color: var(--paper); padding: 0.6rem 1.1rem; border-radius: 8px;
  font-size: 0.85rem; font-weight: 600; transition: transform 0.25s ease; z-index: 50;
}
@media (prefers-reduced-motion: reduce) { .toast { transition: none; } }
.toast.show { transform: translateX(-50%) translateY(0); }
.empty { color: var(--muted); padding: 1.4rem; text-align: center; font-size: 0.85rem; }
.section-gap { margin-top: 1.4rem; }
h2.h-sec { font-size: 0.95rem; font-weight: 700; margin: 1.6rem 0 0.6rem; }

.callout-warn {
  background: color-mix(in srgb, var(--bad) 12%, transparent);
  border: 1px solid var(--bad); border-radius: 8px;
  padding: 0.8rem 1rem; margin: 1rem 0; font-size: 0.9rem;
}

.logo-preview {
  border: 1px dashed var(--line); border-radius: 8px; padding: 1rem;
  min-height: 92px; display: grid; place-items: center; background: var(--tint);
}
.logo-preview img { max-height: 72px; max-width: 100%; object-fit: contain; }
@media print {
  .sidebar, .tabbar, .toolbar, .preset-row, .toast { display: none !important; }
  .main { padding: 0; }
  body { background: #fff; color: #000; }
}

.acting-banner {
  background: var(--gold-soft); border: 1px solid var(--gold); border-radius: 8px;
  padding: 0.6rem 0.9rem; margin-bottom: 1rem; font-size: 0.85rem;
  display: flex; gap: 0.7rem; align-items: center; flex-wrap: wrap;
}
.note-card {
  border: 1px solid var(--line); background: var(--tint); border-radius: 8px;
  padding: 0.85rem 1.05rem; margin-bottom: 1rem; font-size: 0.86rem; line-height: 1.6;
}

/* ---------- analytics ---------- */
.chart-card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 1.1rem 1.2rem; display: flex; flex-direction: column; gap: 0.2rem;
  min-width: 0; container-type: inline-size; }
.chart-card > * { min-width: 0; }
.chart-card .grid > div { min-width: 0; container-type: inline-size; }
.chart-card h3 { margin: 0; font-size: 0.72rem; text-transform: uppercase;
  letter-spacing: 0.11em; color: var(--muted); font-weight: 600; }
.chart-card .why { color: var(--muted); font-size: 0.78rem; margin: 0 0 0.5rem; }
.chart-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.legend { display: flex; gap: 1rem; flex-wrap: wrap; font-size: 0.76rem; color: var(--muted);
  margin-top: 0.5rem; }
.legend span { display: inline-flex; align-items: center; gap: 0.35rem; }
.swatch { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
svg.chart { width: 100%; height: auto; display: block; overflow: visible; }
svg.chart text { font-family: var(--sans); fill: var(--muted); }
svg.chart .axis { stroke: var(--line); stroke-width: 1; }
svg.chart .grid { stroke: var(--grid); stroke-width: 1; }
svg.chart .mark:hover { opacity: 0.78; }
.health-grid { display: grid; gap: 0.75rem; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); }
.health { border: 1px solid var(--line); border-radius: 10px; background: var(--panel);
  padding: 0.85rem 1rem; display: flex; flex-direction: column; gap: 0.25rem; }
.health .lab { font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.health { min-width: 0; container-type: inline-size; }
.health .val { font-size: clamp(0.95rem, 7cqi, 1.2rem); font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; }
.health .det { font-size: 0.76rem; color: var(--muted); line-height: 1.45; }
.health .badge { font-size: 0.64rem; text-transform: uppercase; letter-spacing: 0.08em;
  font-weight: 700; display: inline-flex; align-items: center; gap: 0.3rem; }
.h-good .badge { color: var(--ok); } .h-watch .badge { color: var(--warn); }
.h-attention .badge { color: var(--bad); } .h-neutral .badge { color: var(--muted); }
.h-good { border-left: 3px solid var(--ok); }
.h-watch { border-left: 3px solid var(--warn); }
.h-attention { border-left: 3px solid var(--bad); }
.projection-note { font-size: 0.76rem; color: var(--muted); background: var(--tint);
  border-radius: 6px; padding: 0.5rem 0.7rem; margin-top: 0.5rem; overflow-wrap: anywhere; }
details.datatable { margin-top: 0.6rem; }
details.datatable summary { font-size: 0.76rem; color: var(--accent); cursor: pointer; }

.ranked { display: flex; flex-direction: column; gap: 0.4rem; margin-top: 0.3rem; }
.ranked-row { display: grid; grid-template-columns: minmax(90px, 34%) 1fr auto;
  gap: 0.6rem; align-items: center; font-size: 0.78rem; }
.ranked-label { color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ranked-track { background: var(--tint); border-radius: 3px; height: 12px; overflow: hidden; }
.ranked-fill { height: 100%; border-radius: 3px; }
.ranked-value { font-variant-numeric: tabular-nums; color: var(--ink); white-space: nowrap; }
.ranked-pct { color: var(--muted); }
.threshold { margin-top: 0.5rem; }
.threshold-track { position: relative; background: var(--tint); border-radius: 4px;
  height: 16px; overflow: visible; }
.threshold-fill { height: 100%; border-radius: 4px; }
.threshold-mark { position: absolute; top: -3px; bottom: -3px; width: 2px;
  background: var(--ink); opacity: 0.55; }
.threshold-labels { display: flex; justify-content: space-between; font-size: 0.74rem;
  color: var(--muted); margin-top: 0.3rem; font-variant-numeric: tabular-nums; }

/* ---------- scrolling and motion ---------- */
/* Smoothing is applied per action in scrollToTop(), not globally: a global
   rule also animates the jump-to-top when the screen changes, which reads as
   the page sliding away under you. */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
/* Panels that scroll inside the page keep their scroll to themselves. */
#chatBox, .scroll-panel { overscroll-behavior: contain; }
details.needs summary { cursor: pointer; font-size: 0.82rem; color: var(--accent); font-weight: 600; }
details.needs[open] summary { margin-bottom: 0.3rem; }
details.needs p { margin: 0; }
.card .err:not(:empty) { margin: 0.5rem 0; }
textarea {
  width: 100%; padding: 0.55rem 0.7rem; border: 1px solid var(--line); border-radius: 6px;
  background: var(--panel); color: var(--ink); font-family: inherit; font-size: 0.9rem; resize: vertical;
}
table.sheet input, table.sheet select { font-size: 0.82rem; padding: 0.3rem 0.4rem; }

/* ---------- narrow screens ---------- */
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: static; height: auto; overflow: visible;
    border-right: 0; border-bottom: 1px solid var(--line);
  }
  .nav { flex-direction: row; flex-wrap: wrap; }
  .nav a { padding: 0.4rem 0.6rem; font-size: 0.85rem; }
  .sidebar-foot { margin-top: 0.6rem; }
  .main { padding: 1.2rem 1rem 3rem; }
  .tablewrap thead th { position: static; }
}

/* A voided document stays visible but is plainly no longer live. */
tr.voided td { opacity: 0.55; }
tr.voided td.mono { text-decoration: line-through; text-decoration-thickness: 1px; }

/* ---------- filter bar, shared by the ledger and the document lists ---------- */
.filterbar {
  border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
  padding: 0.8rem 0.9rem; margin-bottom: 0.9rem; display: flex; flex-direction: column; gap: 0.7rem;
}
.fb-presets { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.fb-fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.6rem 0.7rem; }
.fb-f { display: flex; flex-direction: column; gap: 0.25rem; min-width: 0; }
.fb-f.fb-wide { grid-column: span 2; }
.fb-label { font-size: 0.62rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); font-weight: 600; }
.fb-f input, .fb-f select { padding: 0.38rem 0.5rem; font-size: 0.82rem; }
.fb-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.6rem; }
.fb-balance { font-size: 0.84rem; margin: -0.2rem 0 0.8rem; }
.pager { display: flex; align-items: center; gap: 0.6rem; margin-top: 0.7rem; flex-wrap: wrap; }
tfoot td { border-top: 2px solid var(--line); background: var(--tint); }
@media (max-width: 560px) { .fb-f.fb-wide { grid-column: span 1; } }
