:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1a1d23;
  --muted: #6b7280;
  --border: #e3e6eb;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #c62828;
  --success-bg: #e8f5e9; --success-fg: #1b5e20;
  --error-bg: #fdecea;   --error-fg: #8e1c1c;
  --info-bg: #e8f0fe;    --info-fg: #1a3f8f;
  --warn-row: #fff8e1;
  --radius: 10px;
  /* charts: categorical slots (fixed order), chrome, status */
  --text-2: #52514e;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --tooltip-bg: #1a1d23; --tooltip-fg: #ffffff;
  --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; --series-4: #eda100;
  --series-5: #e87ba4; --series-6: #008300; --series-7: #6250d6; --series-8: #e34948;
  --series-other: #898781;
  --status-good: #0ca30c; --status-critical: #d03b3b;
  --text-good: #006300;   --text-critical: #b42318;
  --due-fill: #c3c2b7;
}
/* Dark palette: follows the OS unless the user picked a theme (data-theme on <html>). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111317;
    --surface: #1b1e24;
    --text: #e7e9ee;
    --muted: #9aa1ad;
    --border: #2c3038;
    --accent: #5b8def;
    --danger: #ef6b6b;
    --success-bg: #1c3320; --success-fg: #a5d6a7;
    --error-bg: #3a1d1d;   --error-fg: #f4a3a3;
    --info-bg: #1c2a44;    --info-fg: #a9c3f5;
    --warn-row: #332b12;
    --text-2: #c3c2b7;
    --grid: #2c2c2a;
    --axis: #383835;
    --tooltip-bg: #f0efec; --tooltip-fg: #111317;
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
    --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;
    --text-good: #0ca30c;   --text-critical: #ef6b6b;
    --due-fill: #4a4a46;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111317;
  --surface: #1b1e24;
  --text: #e7e9ee;
  --muted: #9aa1ad;
  --border: #2c3038;
  --accent: #5b8def;
  --danger: #ef6b6b;
  --success-bg: #1c3320; --success-fg: #a5d6a7;
  --error-bg: #3a1d1d;   --error-fg: #f4a3a3;
  --info-bg: #1c2a44;    --info-fg: #a9c3f5;
  --warn-row: #332b12;
  --text-2: #c3c2b7;
  --grid: #2c2c2a;
  --axis: #383835;
  --tooltip-bg: #f0efec; --tooltip-fg: #111317;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
  --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;
  --text-good: #0ca30c;   --text-critical: #ef6b6b;
  --due-fill: #4a4a46;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1 { font-size: 1.6rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
code { font-size: .85em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  padding: .6rem 1.25rem;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; }
.topbar nav { display: flex; gap: 1rem; flex: 1; }
.topbar nav a { color: var(--muted); text-decoration: none; padding: .25rem 0; }
.topbar nav a.active { color: var(--text); border-bottom: 2px solid var(--accent); }
.me { display: flex; align-items: center; gap: .6rem; }
.avatar { width: 28px; height: 28px; border-radius: 50%; }
@media (max-width: 640px) { .me-email { display: none; } }

.container { max-width: 1240px; margin: 0 auto; padding: 1.5rem 1rem 3rem; }
.center { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.25rem; margin-top: 1.25rem;
}
.login-card { width: 100%; max-width: 380px; text-align: center; margin: 0; }
.login-card p { margin: .5rem 0 1.25rem; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .5rem;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; padding: .5rem .9rem; font: inherit; cursor: pointer; text-decoration: none;
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-ghost { border-color: transparent; color: var(--muted); }
.btn-small { padding: .25rem .6rem; font-size: .85rem; }
.btn-danger { color: var(--danger); }
.btn-google { width: 100%; padding: .7rem; font-weight: 500; }

.flash { border-radius: 8px; padding: .6rem .9rem; margin-bottom: 1rem; text-align: left; }
.flash-success { background: var(--success-bg); color: var(--success-fg); }
.flash-error   { background: var(--error-bg);   color: var(--error-fg); }
.flash-info    { background: var(--info-bg);    color: var(--info-fg); }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; }
.tiles .tile { margin-top: 1.25rem; }
.tile-label { color: var(--muted); font-size: .85rem; }
.tile-value { font-size: 1.6rem; font-weight: 600; }

.add-user { display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-end; }
.add-user label { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--muted); }
input, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: .45rem .6rem;
}
.add-user input[type=email] { min-width: 260px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .55rem .5rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
tr.disabled td { opacity: .55; }
tr.warn td { background: var(--warn-row); }
td.actions { display: flex; gap: .4rem; justify-content: flex-end; }
td form { margin: 0; }
.count { color: var(--muted); font-weight: 400; }
.pill {
  display: inline-block; font-size: .75rem; padding: .05rem .5rem;
  border-radius: 999px; border: 1px solid var(--border); color: var(--muted);
}
.pill-admin { color: var(--accent); border-color: var(--accent); }

/* --- layout --------------------------------------------------------------- */
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: .75rem; flex-wrap: wrap; margin-bottom: .75rem; }
.card-head h2 { margin: 0; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 480px), 1fr)); gap: 0 1.25rem; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.kpis .tile-value { font-size: 1.45rem; }
.spaced { margin-top: 1.25rem; }
.spacer { flex: 1; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.text-critical { color: var(--text-critical); }
.text-good { color: var(--text-good); }
tfoot th { color: var(--text); text-transform: none; font-size: .9rem; letter-spacing: 0; border-top: 1px solid var(--muted); }
tfoot th.small { font-weight: 400; }

.month-picker { display: flex; gap: .4rem; align-items: center; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; padding: 2px; gap: 2px; }
.segmented button { font: inherit; font-size: .85rem; border: 0; background: none; color: var(--muted); padding: .25rem .7rem; border-radius: 6px; cursor: pointer; }
.segmented button[aria-selected="true"] { background: var(--bg); color: var(--text); font-weight: 600; }

/* --- charts ---------------------------------------------------------------- */
.pie-layout { display: grid; grid-template-columns: minmax(160px, 220px) 1fr; gap: 1.25rem; align-items: center; }
@media (max-width: 560px) { .pie-layout { grid-template-columns: 1fr; } }
.pie-box { position: relative; height: 220px; }
.chart-box { position: relative; height: 280px; }
.legend-table th, .legend-table td { padding: .35rem .4rem; font-size: .9rem; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; vertical-align: baseline; margin-right: .2rem; }
.dot-0 { background: var(--series-1); } .dot-1 { background: var(--series-2); }
.dot-2 { background: var(--series-3); } .dot-3 { background: var(--series-4); }
.dot-4 { background: var(--series-5); } .dot-5 { background: var(--series-6); }
.dot-6 { background: var(--series-7); } .dot-7 { background: var(--series-8); }
.dot-other { background: var(--series-other); }

.status { display: inline-flex; align-items: center; gap: .3rem; font-size: .8rem; font-weight: 600; white-space: nowrap;
          padding: .05rem .5rem; border-radius: 999px; border: 1px solid currentColor; }
.status-paid { color: var(--text-good); }
.status-due { color: var(--muted); }
.status-overdue { color: var(--text-critical); }

.progress, .meter { height: 6px; background: var(--bg); border-radius: 3px; overflow: hidden; margin: .4rem 0; }
.progress span { display: block; height: 100%; background: var(--status-good); border-radius: 3px; }
.meter span { display: block; height: 100%; background: var(--series-1); border-radius: 3px; }

/* month at a glance */
.calendar { display: grid; gap: 4px; }
.cal-row { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-head div { font-size: .75rem; color: var(--muted); text-align: center; }
.cal-cell { min-height: 78px; border: 1px solid var(--border); border-radius: 6px; padding: 3px 4px;
            display: flex; flex-direction: column; font-size: .75rem; }
.cal-cell.empty { border-color: transparent; }
.cal-cell.today { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.cal-cell.has { cursor: help; }
.cal-day { color: var(--muted); }
.cal-cell.today .cal-day { color: var(--accent); font-weight: 700; }
.cal-bar-track { flex: 1; display: flex; align-items: flex-end; justify-content: center; min-height: 44px; }
.cal-bar { width: 14px; background: var(--due-fill); border-radius: 4px 4px 0 0; display: flex; align-items: flex-end; overflow: hidden; }
.cal-bar.late { background: var(--status-critical); }
.cal-bar-paid { width: 100%; background: var(--status-good); }
.cal-amt { text-align: center; font-variant-numeric: tabular-nums; color: var(--text-2); }
@media (max-width: 560px) { .cal-amt { display: none; } .cal-cell { min-height: 56px; } }
.cal-legend { display: flex; gap: .8rem; color: var(--muted); }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; vertical-align: middle; }
.sw-paid { background: var(--status-good); } .sw-due { background: var(--due-fill); } .sw-overdue { background: var(--status-critical); }

.attention { list-style: none; margin: 0; padding: 0; }
.attention li { display: grid; grid-template-columns: auto 1fr auto auto; gap: .6rem; align-items: center; padding: .4rem 0; border-bottom: 1px solid var(--border); }
.top-list { margin: 0; padding-left: 1.2rem; }
.top-list li { margin-bottom: .5rem; }
.top-row { display: flex; justify-content: space-between; gap: 1rem; }

/* --- forms ----------------------------------------------------------------- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; max-width: 720px; }
.form-grid > label, .sub-grid > label { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; color: var(--muted); }
.form-grid .span-2 { grid-column: 1 / -1; }
.sub-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
@media (max-width: 560px) { .form-grid, .sub-grid { grid-template-columns: 1fr; } }
label.check { flex-direction: row !important; align-items: center; gap: .5rem !important; color: var(--text) !important; font-size: .95rem !important; }
label.check input { width: 18px; height: 18px; }
textarea { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: .45rem .6rem; resize: vertical; }
.rec-summary { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
               background: var(--info-bg); color: var(--info-fg); border-radius: 8px; padding: .6rem .9rem; }
.form-actions { display: flex; justify-content: flex-end; gap: .6rem; }
dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
         padding: 1.25rem; width: min(520px, calc(100vw - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog .form-actions { margin-top: 1rem; }
.inline-form { display: flex; gap: .5rem; margin-top: .75rem; flex-wrap: wrap; }
.type-list { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: .5rem; }
.type-list li { display: inline-flex; align-items: center; gap: .3rem; border: 1px solid var(--border); border-radius: 999px; padding: .15rem .3rem .15rem .65rem; }
.type-list form { margin: 0; }
.link-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 1rem; padding: 0 .3rem; }
.link-btn:hover { color: var(--danger); }
details.pay { position: relative; }
details.pay summary { list-style: none; }
details.pay summary::-webkit-details-marker { display: none; }
.pay-form { position: absolute; right: 0; top: calc(100% + 4px); z-index: 5; background: var(--surface); border: 1px solid var(--border);
            border-radius: 8px; padding: .75rem; display: grid; gap: .5rem; width: 220px; box-shadow: 0 6px 24px rgb(0 0 0 / .12); }
.pay-form label { display: flex; flex-direction: column; gap: .2rem; font-size: .8rem; color: var(--muted); }
.report-form { display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-end; }
.report-form label { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--muted); }
.report-title { margin-top: 1.5rem; font-size: 1.3rem; }
.matrix td, .matrix th { font-size: .85rem; padding: .4rem .35rem; }
.topbar nav { flex-wrap: wrap; }

/* The hidden attribute must win over component display rules (.sub-grid, labels, ...). */
[hidden] { display: none !important; }

@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; gap: .25rem 1rem; padding: .5rem 1rem; }
  .topbar nav { order: 3; flex: 0 0 100%; flex-wrap: nowrap; overflow-x: auto; gap: 1.1rem; }
  .topbar .me { margin-left: auto; }
  .container { padding: 1rem 1rem 3rem; }
  .calendar, .cal-row { gap: 2px; }
  .cal-cell { padding: 2px 3px; min-width: 0; }
  .month-picker select { max-width: 9.5rem; }
}
@media (max-width: 640px) {
  .attention li { grid-template-columns: auto minmax(0, 1fr) auto; }
  .attention li > .muted { display: none; }
}
.tile-value { white-space: nowrap; }
.kpis .tile-value { font-size: clamp(1.15rem, 1.6vw, 1.45rem); }
.legend-table td:first-child { white-space: nowrap; }

/* --- language & theme toggles ----------------------------------------------- */
.prefs { display: inline-flex; align-items: center; gap: .4rem; }
.lang-switch { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; padding: 2px; margin: 0; }
.lang-switch button { font: inherit; font-size: .75rem; font-weight: 600; border: 0; background: none; color: var(--muted);
                      padding: .15rem .45rem; border-radius: 6px; cursor: pointer; }
.lang-switch button[aria-pressed="true"] { background: var(--bg); color: var(--text); }
.theme-toggle { display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 32px;
                border: 1px solid var(--border); border-radius: 8px; background: none; color: var(--muted); cursor: pointer; }
.theme-toggle:hover { color: var(--text); }
.theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .icon-sun { display: block; }
  :root:not([data-theme="light"]) .theme-toggle .icon-moon { display: none; }
}
.floating-prefs { position: fixed; top: 1rem; right: 1rem; z-index: 10; }

/* --- modals ------------------------------------------------------------------- */
dialog.modal { padding: 0; }
dialog.modal > form, dialog.modal > .modal-body { padding: 1.25rem; display: grid; gap: .75rem; }
dialog.modal h2 { margin: 0; }
dialog.modal p { margin: 0; }
dialog.modal .form-actions { margin-top: .25rem; }
label.stack { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; color: var(--muted); }
.pill-variable { border-style: dashed; }
.role-help { margin: 0 0 1rem; padding-left: 1.1rem; }
@media (max-width: 640px) { .me .prefs { order: -1; } }
