/* Agriz Créances -- dense, spreadsheet-familiar styling, dressed in the Agriz
   brand palette (orange CTA, black text, light-grey blocks).
   Deliberately low on chrome: sales reps are moving off Excel, so the table
   itself should feel like the primary surface. */

:root {
  color-scheme: light;
  --color-bg: #fafafa;
  --color-surface: #ffffff;
  --color-surface-2: #fafafa;
  --color-border: #e2e2e4;
  --color-text: #000000;
  --color-muted: #a8a9ad;
  --color-accent: #f95738;
  --color-accent-soft: #fde9e5;
  --color-danger: #c4362d;
  --color-danger-soft: #fbeae9;
  --color-warning: #a6650a;
  --color-warning-soft: #fdf1de;
  --color-success: #17794f;
  --color-success-soft: #e6f6ee;
  --shadow-sm: 0 1px 2px rgba(20, 20, 20, .05);
  --shadow-md: 0 10px 26px -14px rgba(20, 20, 20, .22);
  --font-heading: "Cormorant", Georgia, serif;
  --font-body: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  --row-height: 40px;
  font-size: 14px;

  /* Relances barème stage colors -- shared by relances.html and
     relance_history.html (moved here rather than duplicated in both). */
  --rl-s-auto:#1b3c59;        --rl-s-auto-bg:#e7edf2;
  --rl-s-tel1:#3f7cac;        --rl-s-tel1-bg:#eaf2f8;
  --rl-s-tel2:#2c5a80;        --rl-s-tel2-bg:#dfeaf3;
  --rl-s-med:#b8651f;         --rl-s-med-bg:#f8ece0;
  --rl-s-injonction:#c0392b;  --rl-s-injonction-bg:#fbe9e7;
  --rl-s-externe:#6b3fa0;     --rl-s-externe-bg:#f1eaf8;
  --rl-s-collective:#5b6672;  --rl-s-collective-bg:#eef0f2;
  --rl-s-plan:#0f766e;        --rl-s-plan-bg:#e6f3f1;

  /* Scoring client -- 5-step gradient from "excellent" to "à risque",
     shared by scoring/*.html. */
  --sc-excellent:#0f7a4a;    --sc-excellent-bg:#e3f6ec;
  --sc-tresbon:#3f9142;      --sc-tresbon-bg:#eaf6e7;
  --sc-bon:#a6650a;          --sc-bon-bg:#fdf1de;
  --sc-surveiller:#c17a15;   --sc-surveiller-bg:#fdeecb;
  --sc-risque:#c4362d;       --sc-risque-bg:#fbeae9;
}

/* Dark theme -- three-state per the app's viewer: an explicit choice stamps
   data-theme on <html> (see base.html's early inline script + the topbar
   toggle), "system" stamps nothing and only prefers-color-scheme applies.
   Every token above gets a dark counterpart here so no rule anywhere needs
   its own dark-mode special-casing -- components only ever read the tokens. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --color-bg: #16130f;
    --color-surface: #201b16;
    --color-surface-2: #241f19;
    --color-border: #352e25;
    --color-text: #f1ece4;
    --color-muted: #998f8b;
    --color-accent: #ff7a5c;
    --color-accent-soft: #3c2419;
    --color-danger: #ff6f5c;
    --color-danger-soft: #3a1e1a;
    --color-warning: #e0a542;
    --color-warning-soft: #3a2d14;
    --color-success: #4ad39b;
    --color-success-soft: #123227;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-md: 0 14px 32px -16px rgba(0, 0, 0, .65);

    --rl-s-auto:#8bbbe0;        --rl-s-auto-bg:#1a2530;
    --rl-s-tel1:#7fb8dd;        --rl-s-tel1-bg:#16232c;
    --rl-s-tel2:#6ba3cc;        --rl-s-tel2-bg:#131f28;
    --rl-s-med:#e0a35c;         --rl-s-med-bg:#2e2013;
    --rl-s-injonction:#ff8a76;  --rl-s-injonction-bg:#331e1a;
    --rl-s-externe:#b494db;     --rl-s-externe-bg:#241a2e;
    --rl-s-collective:#9aa5b1;  --rl-s-collective-bg:#21252a;
    --rl-s-plan:#57d6c9;        --rl-s-plan-bg:#132723;

    --sc-excellent:#4ad39b;    --sc-excellent-bg:#123227;
    --sc-tresbon:#7dd88f;      --sc-tresbon-bg:#17301c;
    --sc-bon:#e0a542;          --sc-bon-bg:#3a2d14;
    --sc-surveiller:#f0b429;   --sc-surveiller-bg:#3a2c10;
    --sc-risque:#ff6f5c;       --sc-risque-bg:#3a1e1a;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --color-bg: #16130f;
  --color-surface: #201b16;
  --color-surface-2: #241f19;
  --color-border: #352e25;
  --color-text: #f1ece4;
  --color-muted: #998f8b;
  --color-accent: #ff7a5c;
  --color-accent-soft: #3c2419;
  --color-danger: #ff6f5c;
  --color-danger-soft: #3a1e1a;
  --color-warning: #e0a542;
  --color-warning-soft: #3a2d14;
  --color-success: #4ad39b;
  --color-success-soft: #123227;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-md: 0 14px 32px -16px rgba(0, 0, 0, .65);

  --rl-s-auto:#8bbbe0;        --rl-s-auto-bg:#1a2530;
  --rl-s-tel1:#7fb8dd;        --rl-s-tel1-bg:#16232c;
  --rl-s-tel2:#6ba3cc;        --rl-s-tel2-bg:#131f28;
  --rl-s-med:#e0a35c;         --rl-s-med-bg:#2e2013;
  --rl-s-injonction:#ff8a76;  --rl-s-injonction-bg:#331e1a;
  --rl-s-externe:#b494db;     --rl-s-externe-bg:#241a2e;
  --rl-s-collective:#9aa5b1;  --rl-s-collective-bg:#21252a;
  --rl-s-plan:#57d6c9;        --rl-s-plan-bg:#132723;

  --sc-excellent:#4ad39b;    --sc-excellent-bg:#123227;
  --sc-tresbon:#7dd88f;      --sc-tresbon-bg:#17301c;
  --sc-bon:#e0a542;          --sc-bon-bg:#3a2d14;
  --sc-surveiller:#f0b429;   --sc-surveiller-bg:#3a2c10;
  --sc-risque:#ff6f5c;       --sc-risque-bg:#3a1e1a;
}

.stage { display:inline-flex; align-items:center; gap:6px; font-size:12px; font-weight:700;
  padding:4px 9px; border-radius:999px; white-space:nowrap; }
.stage .dot { width:6px; height:6px; border-radius:50%; }
.stage.auto{background:var(--rl-s-auto-bg);color:var(--rl-s-auto);}
.stage.tel1{background:var(--rl-s-tel1-bg);color:var(--rl-s-tel1);}
.stage.tel2{background:var(--rl-s-tel2-bg);color:var(--rl-s-tel2);}
.stage.med{background:var(--rl-s-med-bg);color:var(--rl-s-med);}
.stage.injonction{background:var(--rl-s-injonction-bg);color:var(--rl-s-injonction);}
.stage.externe{background:var(--rl-s-externe-bg);color:var(--rl-s-externe);}
.stage.collective{background:var(--rl-s-collective-bg);color:var(--rl-s-collective);}
.stage.plan{background:var(--rl-s-plan-bg);color:var(--rl-s-plan);}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-body);
  background: var(--color-bg);
  color: var(--color-text);
}

h1, h2, h3 { font-family: var(--font-heading); font-weight: 700; }

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

.link-button {
  background: none; border: none; padding: 0; margin: 0;
  font: inherit; color: var(--color-accent); cursor: pointer;
}
.link-button:hover { text-decoration: underline; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: 20;
}

.theme-toggle {
  display: flex; align-items: center; gap: 2px;
  background: var(--color-bg); border: 1px solid var(--color-border); border-radius: 999px; padding: 3px;
}
.theme-toggle button {
  display: flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; border-radius: 999px; border: none; background: transparent;
  color: var(--color-muted); cursor: pointer;
}
.theme-toggle button.active { background: var(--color-surface); color: var(--color-accent); box-shadow: var(--shadow-sm); }
.theme-toggle svg { width: 14px; height: 14px; display: block; }

.topbar__brand {
  display: flex; align-items: center; gap: 12px;
  font-family: var(--font-heading); font-weight: 700; font-size: 20px;
}
.topbar__logo { height: 28px; width: auto; display: block; }
.topbar__nav { display: flex; align-items: center; gap: 18px; }
.topbar__nav a { color: var(--color-text); font-weight: 500; }

.topbar__dropdown { position: relative; }
.topbar__dropdown-toggle {
  display: flex; align-items: center; gap: 5px;
  background: none; border: none; padding: 0; margin: 0;
  font: inherit; font-weight: 500; color: var(--color-text); cursor: pointer;
}
.topbar__dropdown-caret { font-size: 10px; color: var(--color-muted); }
.topbar__dropdown-menu {
  display: none;
  position: absolute; top: calc(100% + 12px); left: 0; min-width: 170px;
  background: var(--color-surface); border: 1px solid var(--color-border); border-radius: 10px;
  box-shadow: var(--shadow-md); padding: 6px; z-index: 30;
}
.topbar__dropdown-menu.open { display: block; }
.topbar__dropdown-menu a {
  display: block; padding: 7px 10px; border-radius: 6px; font-size: 13px;
}
.topbar__dropdown-menu a:hover { background: var(--color-accent-soft); text-decoration: none; }
.topbar__nav .role-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--color-accent-soft);
  color: var(--color-accent);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.admin-view-select {
  font-size: 12px; padding: 3px 6px; border: 1px solid var(--color-border); border-radius: 4px;
  background: var(--color-surface); color: var(--color-text);
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 14px;
  flex-wrap: wrap;
  gap: 8px;
}

.page-header h1 { font-size: 30px; margin: 0; }
.page-header .subtitle { color: var(--color-muted); font-size: 13px; }

.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.sort-links {
  display: flex; gap: 2px; font-size: 12px;
  background: var(--color-surface); border: 1px solid var(--color-border); border-radius: 10px;
  padding: 3px; box-shadow: var(--shadow-sm);
}
.sort-links a {
  padding: 6px 11px;
  border: none;
  border-radius: 7px;
  color: var(--color-muted);
  font-weight: 600;
  background: transparent;
}
.sort-links a.active { background: var(--color-accent); color: #fff; }

.search-input {
  padding: 9px 13px;
  border: 1px solid var(--color-border);
  border-radius: 10px;
  background: var(--color-surface);
  color: var(--color-text);
  font-size: 13px;
  font-family: inherit;
  box-shadow: var(--shadow-sm);
}

table.receivables-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  overflow: hidden;
  table-layout: fixed;
  box-shadow: var(--shadow-md);
}

/* Drag-to-resize column handle -- see makeColumnsResizable() in base.html. */
table.receivables-table th, table.invoice-table th {
  position: relative;
}
.col-resize-handle {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 6px;
  cursor: col-resize;
  user-select: none;
}
.col-resize-handle:hover, .col-resize-handle.resizing {
  background: var(--color-accent-soft);
}

table.receivables-table th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-muted);
  background: var(--color-surface-2);
  padding: 10px 12px;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

table.receivables-table td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}

table.receivables-table tfoot td {
  padding: 10px 12px;
  border-top: 2px solid var(--color-border);
  font-weight: 700;
  background: var(--color-surface-2);
}

table.receivables-table tbody tr.client-row {
  cursor: pointer;
}
table.receivables-table tbody tr.client-row:hover { background: var(--color-accent-soft); }

.client-row__toggle {
  display: inline-block;
  width: 16px;
  transition: transform 0.15s ease;
  color: var(--color-muted);
}
.client-row.expanded .client-row__toggle { transform: rotate(90deg); }

.amount { font-variant-numeric: tabular-nums; font-weight: 600; }
.amount.zero { color: var(--color-muted); font-weight: 400; }
.amount.overdue { color: var(--color-danger); }

.risk-status-view {
  display: flex; align-items: center; gap: 8px; cursor: pointer;
  padding: 4px 6px; border-radius: 6px;
}
.risk-status-view:hover { background: var(--color-accent-soft); }

.pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  text-transform: capitalize;
  white-space: nowrap;
}
.pill--unpaid, .pill--late { background: var(--color-danger-soft); color: var(--color-danger); }
.pill--partially_paid { background: var(--color-warning-soft); color: var(--color-warning); }
.pill--paid { background: var(--color-success-soft); color: var(--color-success); }
.pill--draft, .pill--cancelled, .pill--not-due { background: var(--color-surface-2); color: var(--color-muted); }
.pill--risk {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--color-surface-2); color: var(--color-text); border: 1px solid var(--color-border);
  margin-left: 6px; text-transform: none; font-weight: 700;
}
.pill--risk::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--color-muted); }
.pill--blocked {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--color-danger-soft); color: var(--color-danger);
  margin-left: 6px; text-transform: none; font-weight: 700;
}
.pill--blocked::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--color-danger); }

tr.client-row--blocked, tr.admin-row--blocked { background: var(--color-danger-soft); }

.aging { font-size: 12px; color: var(--color-muted); }
.aging.overdue { color: var(--color-danger); font-weight: 600; }

.detail-row td { background: var(--color-surface-2); padding: 0; }
.detail-panel { padding: 14px 20px 20px 44px; }

table.invoice-table { width: 100%; border-collapse: collapse; margin-bottom: 16px; font-size: 13px; table-layout: fixed; }
table.invoice-table th {
  text-align: left; font-size: 10.5px; text-transform: uppercase; color: var(--color-muted);
  padding: 6px 10px; border-bottom: 1px solid var(--color-border);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
table.invoice-table td { padding: 6px 10px; border-bottom: 1px solid var(--color-border); }
table.invoice-table tr:last-child td { border-bottom: none; }

.status-form { display: flex; gap: 6px; align-items: center; }
.status-form select, .status-form input {
  font-size: 12px; padding: 3px 6px; border: 1px solid var(--color-border); border-radius: 4px;
}
.status-form button {
  font-size: 12px; padding: 4px 8px; border-radius: 4px; border: 1px solid var(--color-accent);
  background: var(--color-accent); color: #fff; cursor: pointer;
}

.comments-columns { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.comment-column h4 {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--color-muted);
  margin: 0 0 8px 0;
}
.comment-list { list-style: none; margin: 0 0 10px 0; padding: 0; max-height: 220px; overflow-y: auto; }
.comment-list li {
  background: var(--color-surface); border: 1px solid var(--color-border); border-radius: 6px;
  padding: 8px 10px; margin-bottom: 6px; font-size: 13px;
}
.comment-list .comment-meta { display: block; font-size: 11px; color: var(--color-muted); margin-top: 4px; }
.comment-empty { color: var(--color-muted); font-size: 12px; font-style: italic; }

.comment-form textarea {
  width: 100%; border: 1px solid var(--color-border); border-radius: 6px; padding: 8px; font-family: inherit;
  font-size: 13px; resize: vertical;
}
.comment-form button {
  margin-top: 6px; font-size: 12px; padding: 6px 12px; border-radius: 6px; border: 1px solid var(--color-accent);
  background: var(--color-accent); color: #fff; cursor: pointer;
}
.comment-form .errorlist { color: var(--color-danger); font-size: 12px; padding-left: 16px; }
.comment-form-disabled { color: var(--color-muted); font-size: 12px; font-style: italic; }

.notification-bell {
  position: relative; background: none; border: none; cursor: pointer; font-size: 16px; color: var(--color-text);
}
.notification-bell .badge {
  position: absolute; top: -4px; right: -8px; background: var(--color-danger); color: #fff;
  font-size: 10px; padding: 1px 5px; border-radius: 8px; font-weight: 700;
}

.notification-panel {
  position: absolute; right: 20px; top: 52px; width: 340px; max-height: 420px; overflow-y: auto;
  background: var(--color-surface); border: 1px solid var(--color-border); border-radius: 8px;
  box-shadow: var(--shadow-md); z-index: 50;
}
.notification-panel__header {
  display: flex; justify-content: space-between; align-items: center; padding: 10px 14px;
  border-bottom: 1px solid var(--color-border); font-weight: 600; font-size: 13px;
}
.notification-panel__header button { font-size: 11px; background: none; border: none; color: var(--color-accent); cursor: pointer; }
.notification-item { padding: 10px 14px; border-bottom: 1px solid var(--color-border); font-size: 12.5px; }
.notification-item.unread { background: var(--color-accent-soft); }
.notification-item__meta { color: var(--color-muted); font-size: 11px; margin-top: 3px; }
.notification-empty { padding: 18px 14px; color: var(--color-muted); font-size: 12.5px; text-align: center; }

.toast-stack { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast {
  /* Deliberately a fixed dark chip in both themes (not var(--color-text),
     which flips light in dark mode and would make this unreadable). */
  background: #1c1a17; color: #fff; padding: 10px 16px; border-radius: 6px; font-size: 13px;
  box-shadow: var(--shadow-md); animation: toast-in 0.2s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.btn {
  display: inline-block; padding: 7px 14px; border-radius: 6px; border: 1px solid var(--color-accent);
  background: var(--color-accent); color: #fff; font-size: 13px; cursor: pointer; font-weight: 600;
}
.btn.btn--secondary { background: var(--color-surface); color: var(--color-text); border-color: var(--color-border); }
.btn.btn--danger { background: var(--color-danger); border-color: var(--color-danger); }

.card { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: 12px; padding: 18px; box-shadow: var(--shadow-md); }

.messages { list-style: none; padding: 0; margin: 0 0 14px 0; }
.messages li { padding: 8px 12px; border-radius: 6px; font-size: 13px; margin-bottom: 6px; }
.messages li.success { background: var(--color-success-soft); color: var(--color-success); }
.messages li.error { background: var(--color-danger-soft); color: var(--color-danger); }
.messages li.warning { background: var(--color-warning-soft); color: var(--color-warning); }

form.stacked-form p { margin: 0 0 12px 0; display: flex; flex-direction: column; gap: 4px; }
form.stacked-form label { font-size: 12px; font-weight: 600; color: var(--color-muted); text-transform: uppercase; }
form.stacked-form input[type=text], form.stacked-form input[type=email],
form.stacked-form input[type=password], form.stacked-form textarea, form.stacked-form select {
  padding: 8px 10px; border: 1px solid var(--color-border); border-radius: 6px; font-family: inherit; font-size: 13px;
}
form.stacked-form .errorlist { color: var(--color-danger); font-size: 12px; padding-left: 14px; margin: 2px 0; }

.admin-table td, .admin-table th { padding: 8px 12px; }
/* Fixed-width columns (see makeColumnsResizable) don't shrink data to fit --
   without this, a long value like an email just overflows into the next
   column instead of stopping. Ellipsis + a title attribute (see template)
   keeps it readable; drag the column wider for the full value. */
.admin-table td {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.empty-state { padding: 40px; text-align: center; color: var(--color-muted); }
.help-note { font-size: 12px; color: var(--color-muted); margin-top: 4px; }

/* Scoring client ---------------------------------------------------- */

.score-badge {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700;
  padding: 4px 10px; border-radius: 999px; white-space: nowrap;
}
.score-badge .dot { width: 6px; height: 6px; border-radius: 50%; }
.score-badge.excellent { background: var(--sc-excellent-bg); color: var(--sc-excellent); }
.score-badge.excellent .dot { background: var(--sc-excellent); }
.score-badge.tres_bon { background: var(--sc-tresbon-bg); color: var(--sc-tresbon); }
.score-badge.tres_bon .dot { background: var(--sc-tresbon); }
.score-badge.bon { background: var(--sc-bon-bg); color: var(--sc-bon); }
.score-badge.bon .dot { background: var(--sc-bon); }
.score-badge.surveiller { background: var(--sc-surveiller-bg); color: var(--sc-surveiller); }
.score-badge.surveiller .dot { background: var(--sc-surveiller); }
.score-badge.risque { background: var(--sc-risque-bg); color: var(--sc-risque); }
.score-badge.risque .dot { background: var(--sc-risque); }

.score-number { font-variant-numeric: tabular-nums; font-weight: 700; }
.score-number.excellent, .score-number.tres_bon { color: var(--sc-excellent); }
.score-number.bon { color: var(--sc-bon); }
.score-number.surveiller { color: var(--sc-surveiller); }
.score-number.risque { color: var(--sc-risque); }

.score-bar {
  position: relative; width: 72px; height: 6px; border-radius: 999px;
  background: var(--color-surface-2); overflow: hidden; display: inline-block; vertical-align: middle;
}
.score-bar__fill { position: absolute; inset: 0 auto 0 0; height: 100%; border-radius: 999px; background: var(--color-accent); }
.score-bar-row { display: flex; align-items: center; gap: 8px; }
.score-bar-row .value { font-variant-numeric: tabular-nums; font-size: 12.5px; color: var(--color-muted); min-width: 32px; }

.score-hero {
  display: flex; align-items: center; gap: 28px; flex-wrap: wrap;
}
.score-hero__number { font-family: var(--font-heading); font-size: 56px; font-weight: 700; line-height: 1; }
.score-hero__max { font-size: 18px; color: var(--color-muted); font-weight: 400; }

.stat-cards {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 20px;
}
.stat-card {
  background: var(--color-surface); border: 1px solid var(--color-border); border-radius: 12px;
  padding: 14px 16px; box-shadow: var(--shadow-sm);
}
.stat-card .stat-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--color-muted); margin-bottom: 6px;
}
.stat-card .stat-value { font-family: var(--font-heading); font-size: 26px; font-weight: 700; }
.stat-card .stat-sub { font-size: 12px; color: var(--color-muted); margin-top: 2px; }

.metric-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin: 18px 0;
}
.metric-tile {
  background: var(--color-surface-2); border: 1px solid var(--color-border); border-radius: 10px; padding: 12px 14px;
}
.metric-tile .metric-label { font-size: 11px; text-transform: uppercase; color: var(--color-muted); letter-spacing: 0.03em; }
.metric-tile .metric-value { font-size: 20px; font-weight: 700; margin-top: 4px; }

.ranking-list { list-style: none; margin: 0; padding: 0; }
.ranking-list li {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 0; border-bottom: 1px solid var(--color-border); font-size: 13px;
}
.ranking-list li:last-child { border-bottom: none; }
.ranking-list .rank-name { font-weight: 600; }
.ranking-list .rank-meta { color: var(--color-muted); font-size: 12px; }

.ranking-columns { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; margin-bottom: 20px; }
.ranking-columns h3 { font-size: 15px; margin: 0 0 10px; display: flex; align-items: center; gap: 6px; }

.alert-list { list-style: none; margin: 0; padding: 0; }
.alert-item {
  display: flex; align-items: flex-start; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--color-border); font-size: 13px;
}
.alert-item:last-child { border-bottom: none; }
.alert-item .alert-icon { font-size: 14px; line-height: 1.4; }
.alert-item .alert-label { font-weight: 600; }
.alert-item .alert-detail { color: var(--color-muted); font-size: 12px; }
.alert-item a { color: inherit; }
.alert-item a:hover .alert-label { text-decoration: underline; }

.period-picker { display: flex; gap: 2px; font-size: 12px; background: var(--color-surface); border: 1px solid var(--color-border);
  border-radius: 10px; padding: 3px; box-shadow: var(--shadow-sm); flex-wrap: wrap; }
.period-picker a { padding: 6px 11px; border-radius: 7px; color: var(--color-muted); font-weight: 600; background: transparent; }
.period-picker a.active { background: var(--color-accent); color: #fff; }
.period-picker form { display: flex; align-items: center; gap: 6px; padding: 0 4px; }
.period-picker input[type=date] { font-size: 12px; padding: 4px 6px; border: 1px solid var(--color-border); border-radius: 6px;
  background: var(--color-surface); color: var(--color-text); }
.period-picker button { font-size: 12px; padding: 5px 10px; border-radius: 6px; border: 1px solid var(--color-accent);
  background: var(--color-accent); color: #fff; cursor: pointer; }

.chart-card {
  background: var(--color-surface); border: 1px solid var(--color-border); border-radius: 12px; padding: 16px 18px;
  box-shadow: var(--shadow-sm);
}
.chart-card h3 { font-size: 14px; margin: 0 0 10px; }
.chart-dates { display: flex; justify-content: space-between; font-size: 11px; color: var(--color-muted); margin-top: 4px; }
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; margin-bottom: 20px; }

/* Hover tooltips on chart lines -- see static/js/charts.js. .chart-svg wraps
   the chart so the tooltip (positioned absolutely) has something to anchor
   to; .chart-dot is an invisible hit-target per data point, enlarged to a
   visible ring only for the one currently hovered. */
.chart-svg-wrap { position: relative; }
.chart-dot { fill: transparent; stroke: none; cursor: crosshair; }
.chart-dot.active { fill: var(--color-accent); }
.chart-tooltip {
  /* Inverted contrast (text-colour background, surface-colour text) reads
     correctly in both themes automatically, since --color-text/--color-surface
     already swap for dark mode above -- no separate dark-mode rule needed. */
  position: absolute; top: 4px; transform: translateX(-50%);
  background: var(--color-text); color: var(--color-surface);
  font-size: 11.5px; font-weight: 600; white-space: nowrap;
  padding: 4px 8px; border-radius: 6px; pointer-events: none; z-index: 5;
}

.fidelity-breakdown-table { width: 100%; border-collapse: collapse; margin: 14px 0; }
.fidelity-breakdown-table th, .fidelity-breakdown-table td {
  padding: 8px 10px; border-bottom: 1px solid var(--color-border); text-align: left; font-size: 13px;
}
.fidelity-breakdown-table th { color: var(--color-muted); font-weight: 600; font-size: 11px; text-transform: uppercase; }

/* Wide tables (dashboards, admin lists) scroll horizontally on narrow
   screens rather than being squeezed unreadable -- same pattern the
   Kanban board already uses. A wrapper div (not overflow on the <table>
   itself) so table-layout:fixed's column algorithm isn't disturbed. */
.table-scroll { width: 100%; }

.topbar__menu-toggle {
  display: none;
  background: none; border: none; padding: 4px; cursor: pointer; color: var(--color-text);
}
.topbar__menu-toggle svg { width: 22px; height: 22px; display: block; }

/* Mobile layout ------------------------------------------------------ */

@media (max-width: 860px) {
  .topbar { position: relative; flex-wrap: nowrap; }
  .topbar__menu-toggle { display: flex; align-items: center; justify-content: center; }
  .topbar__brand span { display: none; } /* logo alone is enough once the title barely fits */

  .topbar__nav {
    display: none;
    position: absolute; top: 100%; left: 0; right: 0;
    flex-direction: column; align-items: stretch; gap: 0;
    background: var(--color-surface); border-bottom: 1px solid var(--color-border);
    box-shadow: var(--shadow-md); padding: 6px 16px; z-index: 40;
    max-height: calc(100vh - 52px); overflow-y: auto;
  }
  .topbar__nav.topbar__nav--open { display: flex; }
  .topbar__nav > * { padding: 10px 2px; border-bottom: 1px solid var(--color-border); width: 100%; }
  .topbar__nav > *:last-child { border-bottom: none; }
  .topbar__nav .role-badge { width: auto; }

  .topbar__dropdown-toggle { width: 100%; justify-content: space-between; }
  .topbar__dropdown-menu {
    position: static; box-shadow: none; border: none; padding: 0 0 0 10px;
    min-width: 0; width: 100%;
  }
  .topbar__dropdown-menu.open { margin-top: 6px; }

  .notification-panel { right: 10px; left: 10px; width: auto; top: 52px; }

  .container { padding: 14px; }
  .page-header h1 { font-size: 24px; }
  .detail-panel { padding: 12px 14px 16px 20px; }
  .comments-columns { grid-template-columns: 1fr; }

  .table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table-scroll table.receivables-table,
  .table-scroll table.admin-table,
  .table-scroll table.fidelity-breakdown-table {
    width: auto; min-width: 720px;
  }
  .table-scroll table.invoice-table { width: auto; min-width: 640px; }
  /* An expanded detail row spans every column (colspan) purely to break out
     of the table -- on mobile it should show at the viewport's width, not
     get dragged along with the rest of the (intentionally wider, scrolling)
     table. Sticky-pinned to the scroll container's visible left edge with
     an explicit width achieves that; table-layout:fixed would otherwise
     force it to the table's full (720px+) width regardless of its own CSS. */
  tr.detail-row .detail-panel {
    /* left:0 pins to the scroll container's own edge, which already sits
       .container's 14px in from the real viewport edge -- so the box's
       width has to subtract that same 14px (both sides) or it overflows
       past the right edge by exactly that much. */
    position: sticky; left: 0; width: calc(100vw - 28px); max-width: calc(100vw - 28px);
    box-sizing: border-box;
  }

  .rl-modal { padding: 16px 10px; }
  .score-hero { gap: 14px; }
  .score-hero__number { font-size: 42px; }
}
