:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #dfe3e8;
  --border-strong: #c3cad3;
  --text: #16202c;
  --text-dim: #5d6b7a;
  --text-faint: #8a97a5;
  --accent: #1f5f9e;
  --accent-text: #ffffff;
  --accent-soft: #e8f1fa;

  --diesel: #b4661a;
  --diesel-soft: #fdf0e2;
  --turners: #6b5bb5;
  --turners-soft: #eeebf8;
  --welders: #2b5fa8;
  --welders-soft: #e8f0fb;
  --electricians: #9a7b12;
  --electricians-soft: #fbf4dd;
  --workshop: #5a6470;
  --workshop-soft: #eceff2;
  --supply: #1f7a51;
  --supply-soft: #e5f4ed;
  --office: #8a5a8f;
  --office-soft: #f6ecf7;
  --paint: #b03a63;
  --paint-soft: #fbe9ef;
  --warn: #9a5b00;
  --warn-soft: #fdf3e0;
  --danger: #b3261e;
  --danger-soft: #fceceb;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 26, 38, 0.06), 0 4px 12px rgba(16, 26, 38, 0.06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181d;
    --surface: #1c2229;
    --surface-2: #222930;
    --border: #2f3841;
    --border-strong: #414c58;
    --text: #e8edf2;
    --text-dim: #a2b0bf;
    --text-faint: #7b8896;
    --accent: #5aa0e0;
    --accent-text: #0e1319;
    --accent-soft: #1b2b3a;

    --diesel: #e0954a;
    --diesel-soft: #322418;
    --turners: #a495e8;
    --turners-soft: #232036;
    --welders: #6fa5e8;
    --welders-soft: #1a2635;
    --electricians: #d9b845;
    --electricians-soft: #2e2916;
    --workshop: #9aa7b5;
    --workshop-soft: #23282e;
    --supply: #4fbe8c;
    --supply-soft: #162c23;
    --office: #c78fcd;
    --office-soft: #2d2130;
    --paint: #e87a9e;
    --paint-soft: #331c25;
    --warn: #e0a850;
    --warn-soft: #2f2617;
    --danger: #ef7b74;
    --danger-soft: #331d1c;

    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 14px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }

a { color: var(--accent); }

/* ------------------------------------------------------------- app shell */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
}

.brand {
  font-weight: 700;
  letter-spacing: -0.01em;
  display: flex;
  align-items: center;
  gap: 9px;
  white-space: nowrap;
}

.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  background: var(--accent);
  color: var(--accent-text);
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.02em;
}

.brand small {
  display: block;
  font-weight: 500;
  font-size: 11px;
  color: var(--text-faint);
  letter-spacing: 0.02em;
}

.topbar-spacer { flex: 1; }

.who {
  font-size: 13px;
  color: var(--text-dim);
  text-align: right;
  white-space: nowrap;
}
.who strong { color: var(--text); display: block; }

.layout {
  display: grid;
  grid-template-columns: 232px 1fr;
  gap: 20px;
  padding: 20px;
  max-width: 1500px;
  margin: 0 auto;
  align-items: start;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: sticky;
  top: 74px;
}

.nav-group-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-faint);
  padding: 14px 10px 5px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: none;
  cursor: pointer;
  text-align: left;
  width: 100%;
  color: var(--text-dim);
  font-weight: 500;
}
.nav-item:hover { background: var(--surface); color: var(--text); }
.nav-item.active {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
  font-weight: 600;
  box-shadow: var(--shadow);
}

.nav-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.nav-dot.diesel { background: var(--diesel); }
.nav-dot.turners { background: var(--turners); }
.nav-dot.welders { background: var(--welders); }
.nav-dot.electricians { background: var(--electricians); }
.nav-dot.workshop { background: var(--workshop); }
.nav-dot.supply { background: var(--supply); }
.nav-dot.office { background: var(--office); }
.nav-dot.paint { background: var(--paint); }
.nav-dot.queue { background: var(--warn); }
.nav-dot.all { background: var(--text-faint); }

.nav-count {
  margin-left: auto;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--text-faint);
  background: var(--bg);
  border-radius: 20px;
  padding: 1px 8px;
  min-width: 26px;
  text-align: center;
}
.nav-item.active .nav-count { background: var(--bg); color: var(--text-dim); }
.nav-count.alert { background: var(--warn-soft); color: var(--warn); font-weight: 700; }

.main { min-width: 0; display: flex; flex-direction: column; gap: 16px; }

/* ----------------------------------------------------------------- cards */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card-head {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.card-head h2 { margin: 0; font-size: 16px; font-weight: 650; }
.card-head p { margin: 2px 0 0; font-size: 13px; color: var(--text-dim); }
.card-body { padding: 18px; }

/* ---------------------------------------------------------------- upload */

.dropzone {
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 26px 20px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover, .dropzone.dragging {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.dropzone h3 { margin: 0 0 4px; font-size: 15px; }
.dropzone p { margin: 0; font-size: 13px; color: var(--text-dim); }
.dropzone-actions { margin-top: 12px; display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* --------------------------------------------------------------- buttons */

.btn {
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: 8px;
  padding: 7px 13px;
  cursor: pointer;
  font-weight: 550;
  font-size: 14px;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--text-faint); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn-sm { padding: 4px 10px; font-size: 13px; }

/* Shown rather than hidden on a desktop: the button is struck through so it is obvious the
   feature exists for phones, instead of silently disappearing and leaving people wondering
   how to photograph an invoice at all. */
.btn.unavailable,
.btn.unavailable:hover {
  text-decoration: line-through;
  color: var(--text-faint);
  background: var(--surface-2);
  border-color: var(--border);
  cursor: not-allowed;
  opacity: 1;
}
.btn-danger { color: var(--danger); border-color: var(--border-strong); }
.btn-danger:hover:not(:disabled) { background: var(--danger-soft); border-color: var(--danger); }

.btn-dept { border-width: 1.5px; }
.btn-dept.diesel { color: var(--diesel); border-color: var(--diesel); }
.btn-dept.diesel:hover:not(:disabled) { background: var(--diesel-soft); }
.btn-dept.diesel.current { background: var(--diesel); border-color: var(--diesel); }
.btn-dept.turners { color: var(--turners); border-color: var(--turners); }
.btn-dept.turners:hover:not(:disabled) { background: var(--turners-soft); }
.btn-dept.turners.current { background: var(--turners); border-color: var(--turners); }
.btn-dept.welders { color: var(--welders); border-color: var(--welders); }
.btn-dept.welders:hover:not(:disabled) { background: var(--welders-soft); }
.btn-dept.welders.current { background: var(--welders); border-color: var(--welders); }
.btn-dept.electricians { color: var(--electricians); border-color: var(--electricians); }
.btn-dept.electricians:hover:not(:disabled) { background: var(--electricians-soft); }
.btn-dept.electricians.current { background: var(--electricians); border-color: var(--electricians); }
.btn-dept.workshop { color: var(--workshop); border-color: var(--workshop); }
.btn-dept.workshop:hover:not(:disabled) { background: var(--workshop-soft); }
.btn-dept.workshop.current { background: var(--workshop); border-color: var(--workshop); }
.btn-dept.supply { color: var(--supply); border-color: var(--supply); }
.btn-dept.supply:hover:not(:disabled) { background: var(--supply-soft); }
.btn-dept.supply.current { background: var(--supply); border-color: var(--supply); }
.btn-dept.office { color: var(--office); border-color: var(--office); }
.btn-dept.office:hover:not(:disabled) { background: var(--office-soft); }
.btn-dept.office.current { background: var(--office); border-color: var(--office); }
.btn-dept.paint { color: var(--paint); border-color: var(--paint); }
.btn-dept.paint:hover:not(:disabled) { background: var(--paint-soft); }
.btn-dept.paint.current { background: var(--paint); border-color: var(--paint); }

/* ------------------------------------------------------------------ pills */

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 650;
  padding: 2px 9px;
  border-radius: 20px;
  white-space: nowrap;
}
.pill.diesel { background: var(--diesel-soft); color: var(--diesel); }
.pill.turners { background: var(--turners-soft); color: var(--turners); }
.pill.welders { background: var(--welders-soft); color: var(--welders); }
.pill.electricians { background: var(--electricians-soft); color: var(--electricians); }
.pill.workshop { background: var(--workshop-soft); color: var(--workshop); }
.pill.supply { background: var(--supply-soft); color: var(--supply); }
.pill.office { background: var(--office-soft); color: var(--office); }
.pill.paint { background: var(--paint-soft); color: var(--paint); }
.pill.queue { background: var(--warn-soft); color: var(--warn); }
.pill.processing { background: var(--accent-soft); color: var(--accent); }
.pill.failed { background: var(--danger-soft); color: var(--danger); }
.pill.muted { background: var(--bg); color: var(--text-faint); }
/* The comments mark is meant to be seen: a note on an invoice usually means "wait" or "look". */
.pill.comments { background: var(--accent); color: var(--accent-text); font-weight: 700; }

/* The comments column: mark first, then one line of the newest comment, cut off rather than
   wrapped so a long note cannot stretch the row. */
.comment-cell { max-width: 280px; cursor: pointer; }
.comment-cell .snippet {
  display: block;
  margin-top: 4px;
  font-size: 12.5px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.comment-cell .snippet strong { color: var(--text); font-weight: 600; }
.comment-none { color: var(--text-faint); }

/* ------------------------------------------------------------------ table */

.table-wrap { overflow-x: auto; }

table.grid { width: 100%; border-collapse: collapse; font-size: 14px; }
table.grid th {
  text-align: left;
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
  font-weight: 700;
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  background: var(--surface-2);
}
table.grid td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.grid tbody tr { cursor: pointer; }
table.grid tbody tr:hover { background: var(--surface-2); }
table.grid tbody tr:last-child td { border-bottom: none; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.vendor-cell { font-weight: 600; }
.sub { font-size: 12.5px; color: var(--text-dim); margin-top: 1px; }
.nowrap { white-space: nowrap; }

.empty { padding: 44px 20px; text-align: center; color: var(--text-dim); }
.empty h3 { margin: 0 0 4px; color: var(--text); font-size: 15px; }
.empty p { margin: 0; font-size: 13.5px; }

/* ---------------------------------------------------------------- filters */

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.input, .select {
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: 8px;
  padding: 7px 11px;
  min-width: 0;
}
.input:focus, .select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.input.search { flex: 1; min-width: 180px; }

/* ----------------------------------------------------------------- drawer */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 16, 23, 0.45);
  z-index: 40;
  display: flex;
  justify-content: flex-end;
}
.drawer {
  background: var(--bg);
  width: min(1140px, 100%);
  height: 100%;
  overflow-y: auto;
  box-shadow: -8px 0 32px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
}
.drawer-head {
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 2;
}
.drawer-head h2 { margin: 0; font-size: 16px; }
.drawer-title { flex: 1; min-width: 0; }
.drawer-body {
  padding: 20px;
  display: grid;
  grid-template-columns: 1.35fr 1fr;
  gap: 18px;
  align-items: start;
}

.doc-frame {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  position: sticky;
  top: 74px;
}
.doc-frame img { width: 100%; display: block; }
.doc-frame iframe { width: 100%; height: 80vh; border: none; display: block; background: #fff; }
.doc-fallback { padding: 40px 20px; text-align: center; color: var(--text-dim); }

.detail-stack { display: flex; flex-direction: column; gap: 14px; }

.field-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
}
.field .input { width: 100%; }
.field.wide { grid-column: 1 / -1; }

.suggestion {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 15px;
  background: var(--surface-2);
}
.suggestion.unsure { background: var(--warn-soft); border-color: var(--warn); }
.suggestion h4 { margin: 0 0 3px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-dim); }
.suggestion p { margin: 6px 0 0; font-size: 13.5px; color: var(--text-dim); }

.confidence-bar { height: 5px; border-radius: 3px; background: var(--border); overflow: hidden; margin-top: 8px; }
.confidence-bar span { display: block; height: 100%; background: var(--accent); }

.dept-picker { display: flex; gap: 8px; flex-wrap: wrap; }

.history { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.history li { padding: 7px 0; border-bottom: 1px dashed var(--border); display: flex; gap: 8px; }
.history li:last-child { border-bottom: none; }
.history time { color: var(--text-faint); white-space: nowrap; font-variant-numeric: tabular-nums; }

.line-items { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.line-items td { padding: 6px 0; border-bottom: 1px dashed var(--border); }
.line-items tr:last-child td { border-bottom: none; }

/* ---------------------------------------------------------------- reports */

.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; }
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.stat.diesel { border-left-color: var(--diesel); }
.stat.turners { border-left-color: var(--turners); }
.stat.welders { border-left-color: var(--welders); }
.stat.electricians { border-left-color: var(--electricians); }
.stat.workshop { border-left-color: var(--workshop); }
.stat.supply { border-left-color: var(--supply); }
.stat.office { border-left-color: var(--office); }
.stat.paint { border-left-color: var(--paint); }
.stat.queue { border-left-color: var(--warn); }
.stat h4 { margin: 0; font-size: 12px; font-weight: 650; color: var(--text-dim); }
.stat .value { font-size: 23px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; margin-top: 3px; }
.stat .meta { font-size: 12px; color: var(--text-faint); }

.bar-row { display: flex; align-items: center; gap: 10px; padding: 7px 0; }
.bar-label { width: 74px; font-size: 13px; font-variant-numeric: tabular-nums; color: var(--text-dim); flex-shrink: 0; }
.bar-track { flex: 1; display: flex; height: 22px; border-radius: 4px; overflow: hidden; background: var(--bg); }
.bar-seg { height: 100%; }
.bar-seg.diesel { background: var(--diesel); }
.bar-seg.turners { background: var(--turners); }
.bar-seg.welders { background: var(--welders); }
.bar-seg.electricians { background: var(--electricians); }
.bar-seg.workshop { background: var(--workshop); }
.bar-seg.supply { background: var(--supply); }
.bar-seg.office { background: var(--office); }
.bar-seg.paint { background: var(--paint); }
.bar-total { width: 100px; text-align: right; font-size: 13px; font-variant-numeric: tabular-nums; font-weight: 600; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12.5px; color: var(--text-dim); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

/* ---------------------------------------------------------------- notices */

.notice {
  padding: 11px 15px;
  border-radius: var(--radius);
  font-size: 13.5px;
  border: 1px solid;
}
.notice.warn { background: var(--warn-soft); border-color: var(--warn); color: var(--warn); }
.notice.error { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
.notice.info { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.toast-stack { position: fixed; bottom: 18px; right: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: 10px 15px;
  box-shadow: var(--shadow);
  font-size: 13.5px;
  max-width: 340px;
}
.toast.error { border-left-color: var(--danger); }

.spinner {
  width: 14px; height: 14px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  display: inline-block;
  animation: spin 0.7s linear infinite;
  vertical-align: -2px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------------ auth */

.auth-page { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card { width: 100%; max-width: 380px; }
.auth-card .card-body { display: flex; flex-direction: column; gap: 13px; }
.auth-head { text-align: center; margin-bottom: 4px; }
.auth-head .brand { justify-content: center; font-size: 19px; }
.auth-head p { color: var(--text-dim); font-size: 13.5px; margin: 8px 0 0; }

/* ---------------------------------------------------------------- mobile */

/* The sidebar's groups are wrapped in .nav-row so a phone can lay them out as separate strips.
   On a desktop the wrapper must not exist as a box, or the column's spacing would break. */
.nav-row { display: contents; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 14px; gap: 14px; }
  /* One labelled sideways-scrolling strip per group - departments, Money, Management - each
     starting at the left edge with its heading, so nothing hides off-screen. A single strip
     used to hide Money and Management behind eight departments with no hint of more. */
  .sidebar { position: static; flex-direction: column; gap: 6px; }
  .nav-row {
    display: flex;
    align-items: center;
    gap: 6px;
    overflow-x: auto;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
  }
  .nav-group-label { display: inline-flex; align-items: center; flex: 0 0 auto; margin: 0; padding: 0 6px 0 2px; }
  .nav-item { width: auto; flex: 0 0 auto; white-space: nowrap; background: var(--surface); border-color: var(--border); }
  .drawer-body { grid-template-columns: 1fr; }
  /* On a phone the title and details take the whole width and the buttons drop to their own
     row. Side by side, the title was squeezed into a third of the screen, one word per line. */
  .drawer-head { flex-wrap: wrap; gap: 8px 10px; }
  .drawer-title { flex: 1 1 100%; }
  .drawer-head .btn { flex: 1 1 auto; text-align: center; }
  .doc-frame { position: static; }
  .doc-frame iframe { height: 58vh; }
  .field-grid { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
  .comment-cell { max-width: none; }
  .comment-cell .snippet { display: none; }
  .topbar { padding: 10px 14px; }
}

/* --- who wrote a comment ----------------------------------------------------------------
   One colour per role, always beside the word for it. Office is navy; management is amber -
   deliberately not red, which already means Overdue and Duplicate in the same list. */
:root {
  --role-worker: var(--text-dim);
  --role-worker-soft: var(--bg);
  --role-office: #1f3a93;
  --role-office-soft: #e6ebf8;
  --role-admin: #9a4a00;
  --role-admin-soft: #fbeedd;
}
@media (prefers-color-scheme: dark) {
  :root {
    --role-office: #8fa8ff;
    --role-office-soft: #1c2542;
    --role-admin: #f0b06a;
    --role-admin-soft: #3a2a14;
  }
}
.pill.role { font-weight: 700; letter-spacing: 0.01em; }
.pill.role-worker { background: var(--role-worker-soft); color: var(--role-worker); }
.pill.role-secretary { background: var(--role-office-soft); color: var(--role-office); }
.pill.role-admin, .pill.role-head_admin { background: var(--role-admin-soft); color: var(--role-admin); }
.author.role-secretary { color: var(--role-office); }
.author.role-admin, .author.role-head_admin { color: var(--role-admin); }
.comment-cell .snippet .pill.role { font-size: 10.5px; padding: 1px 6px; vertical-align: middle; }
