:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e3e2de;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #75746f;
  /* Claygate logo colours: navy for the header, links and buttons; pink for highlights */
  --brand: #002c5d;
  --brand-ink: #ffffff;
  --accent: #002c5d;
  --on-accent: #ffffff;
  --link: #0b4a8f;
  --pink: #e5027e;
  --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: #8d8c84;
  --grid: #e9e8e4;
  --good: #1d7a3a;
  --bad: #c4302b;
  --warn-bg: #fdf1d8;
  --warn-ink: #7a4b00;
  --radius: 10px;
  --page-max: 1840px;
  --shadow: 0 1px 2px rgba(20, 20, 18, 0.05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #2f2f2c;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #9a998f;
    --brand: #002c5d;
    --accent: #7aa7e6;
    --on-accent: #0a1a33;
    --link: #8db4ea;
    --pink: #f0479f;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
    --series-other: #6f6e68;
    --grid: #2a2a27;
    --good: #4cbb6c;
    --bad: #ef6a65;
    --warn-bg: #3a2a08;
    --warn-ink: #f2c46b;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface: #1a1a19;
  --surface-2: #232321;
  --border: #2f2f2c;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --text-3: #9a998f;
  --brand: #002c5d;
  --accent: #7aa7e6;
  --on-accent: #0a1a33;
  --link: #8db4ea;
  --pink: #f0479f;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
  --series-other: #6f6e68;
  --grid: #2a2a27;
  --good: #4cbb6c;
  --bad: #ef6a65;
  --warn-bg: #3a2a08;
  --warn-ink: #f2c46b;
  --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  font-variant-numeric: tabular-nums;
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; letter-spacing: -0.01em; }
.muted { color: var(--text-3); }
.small { font-size: 0.85rem; }
.nowrap { white-space: nowrap; }

/* Top bar */
.topbar { background: var(--brand); color: var(--brand-ink); }
.topbar-inner {
  margin: 0 auto; padding: 0 20px; /* full width, so the logo block sits at the left edge */
  display: flex; align-items: center; gap: 24px; min-height: 56px; flex-wrap: wrap;
}
.brand { color: inherit; font-weight: 650; font-size: 1.05rem; display: inline-flex; align-items: center; gap: 16px; align-self: stretch; }
/* The logo sits on a white block filling the header's height, flush to its left edge */
.brand-logo { display: flex; align-items: center; align-self: stretch; background: #fff; padding: 0 20px; margin-left: -20px; line-height: 0; }
.brand-logo img { display: block; height: 20px; width: auto; }
.brand-name { white-space: nowrap; }
.brand:hover { text-decoration: none; }
.nav { display: flex; gap: 4px; flex-wrap: wrap; }
.nav a { color: rgba(255, 255, 255, 0.78); padding: 7px 12px; border-radius: 8px; font-weight: 500; }
.nav a:hover { color: #fff; background: rgba(255, 255, 255, 0.08); text-decoration: none; }
.nav a[aria-current="page"] { color: #fff; background: rgba(255, 255, 255, 0.14); }
.who { margin-left: auto; display: flex; align-items: center; gap: 14px; font-size: 0.875rem; }
.who form { margin: 0; }
.who-name { color: rgba(255, 255, 255, 0.85); }
.freshness { color: rgba(255, 255, 255, 0.7); display: inline-flex; align-items: center; gap: 6px; }
.freshness::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #4cbb6c; }
.freshness.is-error::before { background: #ef6a65; }
.freshness.is-warn::before { background: #f2b33d; }
.link-btn { background: none; border: 0; color: rgba(255, 255, 255, 0.85); font: inherit; cursor: pointer; padding: 0; text-decoration: underline; }

/* Layout */
.page { max-width: var(--page-max); margin: 0 auto; padding: 24px 20px 64px; }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head form { margin: 0; }
.grid { display: grid; gap: 16px; margin-bottom: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (max-width: 960px) { .grid-2 { grid-template-columns: 1fr; } }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow); margin: 0 0 16px; min-width: 0;
}
.grid > .card { margin: 0; }
.card.narrow { max-width: 460px; }
.card-title { font-weight: 600; margin-bottom: 12px; color: var(--text); }
.card-link { display: inline-block; margin-top: 10px; font-size: 0.9rem; }
.chart-wrap { position: relative; width: 100%; }
.empty { color: var(--text-3); padding: 12px 0; margin: 0; }

/* Filters */
.filters {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow);
}
.fld { display: flex; flex-direction: column; gap: 4px; font-size: 0.8rem; color: var(--text-2); min-width: 130px; }
.fld[hidden] { display: none; }
.fld-wide { min-width: 220px; }
.fld-check { flex-direction: row; align-items: center; gap: 8px; min-width: 0; padding-bottom: 8px; font-size: 0.875rem; color: var(--text); }
.fld span { font-weight: 500; }
input, select {
  font: inherit; font-size: 0.9rem; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-height: 36px; width: 100%;
}
input[type="checkbox"] { width: 16px; height: 16px; min-height: 0; accent-color: var(--accent); }
input:focus-visible, select:focus-visible, .btn:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
select:disabled { opacity: 0.7; }

/* Filter bar: dates and search on top, every other filter as a chip below */
.fbar { display: block; padding: 14px 16px; }
.fbar-main { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px 12px; }
.fbar-main .fld { min-width: 150px; flex: 0 1 auto; }
.fbar-main .fld-search { flex: 1 1 260px; max-width: 440px; }
.fbar-main .fld-date { min-width: 140px; }
.fbar-chips { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.fbar-chips:not(:has(*)) { display: none; }

.chip {
  position: relative; display: inline-flex; align-items: center; gap: 6px; min-height: 34px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface);
  font-size: 0.85rem; color: var(--text); white-space: nowrap; transition: border-color .12s, background-color .12s;
}
.chip:hover { border-color: color-mix(in srgb, var(--pink) 40%, var(--border)); }
.chip-k { color: var(--text-2); font-weight: 500; }
.chip-v { font-weight: 600; color: var(--text); max-width: 16rem; overflow: hidden; text-overflow: ellipsis; }
.chip.is-set { background: color-mix(in srgb, var(--pink) 9%, var(--surface)); border-color: color-mix(in srgb, var(--pink) 50%, var(--border)); }
.chip.is-set .chip-v, .chip.is-set select { color: var(--accent); }
.chip-fixed { opacity: .8; }
.chip-select { padding-right: 4px; cursor: pointer; }
.chip-select select {
  width: auto; min-height: 0; border: 0; background: transparent; padding: 4px 4px 4px 0; margin-left: -2px; font-weight: 600; font-size: 0.85rem; cursor: pointer;
}
.chip-select select:focus-visible { outline-offset: 0; }
.chip-check { cursor: pointer; }
.chip-check:has(input:checked) { background: color-mix(in srgb, var(--pink) 9%, var(--surface)); border-color: color-mix(in srgb, var(--pink) 50%, var(--border)); }

/* Multi-select chip and its panel */
.ms.chip { padding: 0; }
.ms-btn { list-style: none; cursor: pointer; display: flex; align-items: center; gap: 6px; min-height: 32px; padding: 0 28px 0 12px; border-radius: 999px; }
.ms-btn::-webkit-details-marker { display: none; }
.ms-btn::after {
  content: ""; position: absolute; right: 12px; top: 50%; width: 6px; height: 6px; margin-top: -5px;
  border-right: 1.5px solid var(--text-2); border-bottom: 1.5px solid var(--text-2); transform: rotate(45deg); transition: transform .12s;
}
.ms[open] > .ms-btn::after { transform: rotate(225deg); margin-top: -1px; }
.ms[open] { border-color: var(--pink); }
.ms-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.ms-pop {
  position: absolute; z-index: 40; top: calc(100% + 6px); left: 0; width: 340px; max-width: calc(100vw - 24px);
  display: flex; flex-direction: column; gap: 10px; padding: 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18); color: var(--text); white-space: normal; cursor: default;
}
.ms-big .ms-pop { width: 780px; }
.ms-head { display: flex; align-items: baseline; gap: 10px; }
.ms-head strong { font-size: 0.95rem; }
.ms-close { margin-left: auto; font: inherit; font-size: 1.3rem; line-height: 1; color: var(--text-2); background: none; border: 0; cursor: pointer; padding: 0 4px; }
.ms-close:hover { color: var(--text); }
.ms-tools .ms-search { min-height: 36px; }
.ms-list { max-height: 320px; overflow-y: auto; margin: 0 -4px; padding: 0 4px; }
.ms-big .ms-list {
  max-height: min(58vh, 540px); display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 2px 10px; align-content: start;
}
.ms-opt { display: flex; align-items: flex-start; gap: 9px; padding: 7px 8px; border-radius: 7px; font-size: 0.875rem; color: var(--text); cursor: pointer; line-height: 1.3; }
.ms-opt:hover { background: var(--surface-2); }
.ms-opt[hidden] { display: none; }
.ms-opt input { flex: none; margin-top: 1px; }
.ms-opt:has(input:checked) { font-weight: 600; background: color-mix(in srgb, var(--pink) 8%, transparent); }
.ms-empty, .ms-more { margin: 6px; font-size: 0.82rem; color: var(--text-3); grid-column: 1 / -1; }
.ms-foot { display: flex; align-items: center; gap: 8px; padding-top: 10px; border-top: 1px solid var(--border); }
.ms-all { margin-right: auto; }
.ms-all[hidden], .ms-clear[hidden] { display: none; }
@media (max-width: 640px) {
  .fbar-main .fld, .fbar-main .fld-search { flex: 1 1 calc(50% - 6px); max-width: none; }
  .fbar-main .fld-search { flex-basis: 100%; }
  .ms-big .ms-list { grid-template-columns: 1fr; }
}
.filter-actions { display: flex; gap: 8px; margin-left: auto; }
.range-note { font-size: 0.82rem; color: var(--text-3); margin: 8px 2px 16px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-size: 0.9rem; font-weight: 600; min-height: 36px; padding: 0 16px;
  border-radius: 8px; border: 1px solid var(--accent); background: var(--accent); color: var(--on-accent); cursor: pointer;
}
.btn:hover { filter: brightness(1.06); text-decoration: none; }
.btn-quiet { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-small { min-height: 30px; padding: 0 12px; font-size: 0.85rem; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.pager { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 14px; }
.sort-link a { color: inherit; }
.sort-link a:hover { color: var(--accent); text-decoration: none; }
th[aria-sort="ascending"].sort-link a::after { content: " ▲"; font-size: 0.7em; }
th[aria-sort="descending"].sort-link a::after { content: " ▼"; font-size: 0.7em; }
.table-kv th { width: 42%; background: none; position: static; font-size: 0.85rem; color: var(--text-2); font-weight: 500; }
.table-kv td, .table-kv th { padding: 7px 10px; }
.page-head > div h1 + p { margin: 4px 0 0; }
.row-actions form { margin: 0; }
.period-form { box-shadow: none; padding: 0; border: 0; background: none; }
.quick-dates { display: flex; gap: 6px; flex-wrap: wrap; padding-bottom: 3px; }
.btn-block { width: 100%; }

/* Invoice: original sales order */
.order-block + .order-block { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--border); }
.order-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.order-head h3 { margin: 0; font-size: 1.05rem; }
.kv-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px 18px; margin-bottom: 14px; font-size: 0.9rem; }
.kv-grid span:first-child { display: block; font-size: 0.78rem; color: var(--text-3); font-weight: 500; }
.table tr.is-linked td { background: color-mix(in srgb, var(--good) 7%, transparent); }

/* Report tabs */
.tabs { display: flex; gap: 4px; flex-wrap: wrap; border-bottom: 1px solid var(--border); margin: 0 0 16px; }
.tabs a { padding: 9px 14px; color: var(--text-2); font-weight: 500; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a[aria-current="page"] { color: var(--text); border-bottom-color: var(--pink); font-weight: 600; }
.seg { display: inline-flex; flex-wrap: wrap; gap: 6px; margin: 0 0 14px; }
.seg a { padding: 6px 12px; border: 1px solid var(--border); border-radius: 999px; color: var(--text-2); background: var(--surface); font-size: 0.875rem; }
.seg a:hover { text-decoration: none; border-color: var(--text-3); color: var(--text); }
.seg a[aria-current="true"] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.seg .count { opacity: 0.8; margin-left: 4px; }
.pivot td.num, .pivot th.num { min-width: 92px; }
.pivot tr.total td, .pivot td.total { font-weight: 650; background: var(--surface-2); }
.heat-pos { background: color-mix(in srgb, var(--good) 10%, transparent); }
.heat-neg { background: color-mix(in srgb, var(--bad) 10%, transparent); }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.kpi-label { font-size: 0.82rem; color: var(--text-2); font-weight: 500; }
.kpi-value { font-size: 1.65rem; font-weight: 650; letter-spacing: -0.02em; margin: 4px 0 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kpi-change { font-size: 0.82rem; color: var(--text-3); min-height: 1.2em; }
.kpi-change.up { color: var(--good); }
.kpi-change.down { color: var(--bad); }
.kpi-change .arrow { font-size: 0.7em; }
.kpi-note { color: var(--text-3); display: block; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 -18px; padding: 0 18px; }
.table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
.table th, .table td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table th { font-size: 0.78rem; font-weight: 600; color: var(--text-2); background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
.table tbody tr:hover { background: var(--surface-2); }
.table .num { text-align: right; white-space: nowrap; }
.table .pos { color: var(--good); }
.table .neg { color: var(--bad); }
.table td .muted { font-size: 0.8rem; }
[data-sortable] th { cursor: pointer; user-select: none; }
[data-sortable] th[aria-sort="ascending"]::after { content: " ▲"; font-size: 0.7em; }
[data-sortable] th[aria-sort="descending"]::after { content: " ▼"; font-size: 0.7em; }
.users input:not([type="checkbox"]), .users select { min-width: 120px; }

.tag { display: inline-block; font-size: 0.72rem; font-weight: 600; padding: 2px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); }
.tag-warn { background: var(--warn-bg); color: var(--warn-ink); border-color: transparent; }
.tag-ok { color: var(--good); }
.tag-err { color: var(--bad); }
a.tag-link { text-decoration: none; cursor: pointer; }
a.tag-link:hover { border-color: var(--accent); color: var(--accent); }

.alert, .notice, .warn { padding: 10px 14px; border-radius: 8px; margin: 0 0 16px; }
.warn { background: var(--warn-bg); color: var(--warn-ink); }
.alert { background: color-mix(in srgb, var(--bad) 12%, var(--surface)); color: var(--bad); }
.notice { background: color-mix(in srgb, var(--good) 12%, var(--surface)); color: var(--good); }

/* Footer */
.site-footer { max-width: var(--page-max); margin: 0 auto; padding: 20px 20px 32px; border-top: 1px solid var(--border); color: var(--text-3); font-size: 0.82rem;
  display: flex; align-items: center; justify-content: center; gap: 8px 16px; flex-wrap: wrap; }
.theme-toggle { font: inherit; color: var(--text-2); background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 4px 12px; cursor: pointer; }
.theme-toggle:hover { color: var(--text); border-color: var(--text-3); }
.theme-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.page { padding-bottom: 32px; }

/* Login */
.login { max-width: 380px; margin: 8vh auto 0; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.login h1 { margin-bottom: 4px; }
.login .muted { margin: 0 0 20px; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.stack .fld { font-size: 0.85rem; }

@media (max-width: 640px) {
  .topbar-inner { padding: 8px 16px; gap: 8px; }
  .brand-logo { margin: -8px 0 -8px -16px; padding: 0 16px; }
  .who { margin-left: 0; width: 100%; justify-content: space-between; }
  .page { padding: 16px 16px 24px; }
  .site-footer { padding: 16px 16px 24px; }
  .fld, .fld-wide, .fld-multi { min-width: calc(50% - 6px); flex: 1 1 calc(50% - 6px); }
  .filter-actions { margin-left: 0; width: 100%; }
  .filter-actions .btn { flex: 1; }
  .kpi-value { font-size: 1.35rem; }
}
@media print {
  .topbar, .site-footer, .filters, .filter-actions, .page-head .btn { display: none; }
  body { background: #fff; }
}
@media (min-width: 1280px) {
  .topbar-inner { padding: 0 32px; }
  .brand-logo { margin-left: -32px; padding-left: 32px; }
  .page { padding-left: 32px; padding-right: 32px; }
  .site-footer { padding-left: 32px; padding-right: 32px; }
}

/* Sync page: choose which parts to sync */
.sync-parts { display: grid; gap: .75rem; }
.check-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: .4rem 1.25rem; }
.check { display: flex; align-items: baseline; gap: .5rem; cursor: pointer; }
.check input { width: 1rem; height: 1rem; margin: 0; accent-color: var(--accent, currentColor); }

/* Compact lists: one line per row, long text cut short (full text on hover) */
.table-compact td { white-space: nowrap; padding-top: 6px; padding-bottom: 6px; }
.table-compact .clip { display: block; max-width: 10rem; overflow: hidden; text-overflow: ellipsis; }
.table-compact .clip-wide { width: 20rem; max-width: none; }
@media (max-width: 1600px) { .table-compact .clip { max-width: 8rem; } .table-compact .clip-wide { width: 11rem; max-width: none; } }

/* Resizable columns (drag a heading's edge; double-click to reset) */
.table th { position: sticky; }
.col-grip { position: absolute; top: 0; right: -4px; bottom: 0; width: 9px; cursor: col-resize; z-index: 2; touch-action: none; }
.col-grip::after { content: ""; position: absolute; top: 25%; bottom: 25%; left: 4px; width: 1px; background: var(--border); }
.table th:hover .col-grip::after, .table.is-resizing .col-grip:active::after { background: var(--pink); width: 2px; left: 3px; }
.table th:last-child .col-grip { right: 0; }
.table.is-sized { table-layout: fixed; max-width: none; }
.table.is-sized th, .table.is-sized td { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.table.is-sized .clip, .table.is-sized .clip-wide { width: auto; max-width: none; }
.table.is-resizing { user-select: none; cursor: col-resize; }

/* Keep the top bar on one line on laptop screens */
@media (max-width: 1600px) {
  .topbar-inner { gap: 12px; }
  .nav { gap: 2px; }
  .nav a { padding: 7px 8px; font-size: 0.92rem; }
  .brand { gap: 12px; }
  .brand-logo { padding-right: 16px; }
  .brand-name { font-size: 0.98rem; }
  .who { gap: 10px; }
}
.table tfoot th { border-top: 1px solid var(--border); font-weight: 600; text-align: left; padding-top: 10px; }
.table tfoot th.num { text-align: right; }
@media (max-width: 1500px) and (min-width: 641px) {
  .brand-name { display: none; } /* room for the menu; the logo already says Claygate */
}
@media (max-width: 1320px) and (min-width: 641px) {
  .nav a { padding: 7px 6px; font-size: 0.88rem; }
  .who { font-size: 0.82rem; }
}

/* Admin menu at the end of the top bar */
.nav-menu { position: relative; }
.nav-menu > summary { list-style: none; cursor: pointer; color: rgba(255, 255, 255, 0.78); padding: 7px 12px; border-radius: 8px; font-weight: 500; }
.nav-menu > summary::-webkit-details-marker { display: none; }
.nav-menu > summary::after { content: " ▾"; font-size: 0.8em; }
.nav-menu > summary:hover, .nav-menu[open] > summary { color: #fff; background: rgba(255, 255, 255, 0.08); }
.nav-menu > summary[aria-current="page"] { color: #fff; background: rgba(255, 255, 255, 0.14); }
.nav-menu-pop {
  position: absolute; z-index: 50; top: calc(100% + 6px); right: 0; min-width: 180px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
  display: flex; flex-direction: column;
}
.nav-menu-pop a { color: var(--text) !important; padding: 8px 10px; border-radius: 6px; font-weight: 500; }
.nav-menu-pop a:hover { background: var(--surface-2) !important; text-decoration: none; }
.nav-menu-pop a[aria-current="page"] { background: var(--surface-2) !important; font-weight: 650; }
@media (max-width: 1600px) { .nav-menu > summary { padding: 7px 8px; font-size: 0.92rem; } }
@media (max-width: 1320px) and (min-width: 641px) { .nav-menu > summary { padding: 7px 6px; font-size: 0.88rem; } }

/* Permission editor */
.perm-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
.perm-group { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px 14px; margin: 0; min-width: 0; }
.perm-group legend { font-weight: 650; padding: 0 6px; }
.perm-wide { grid-column: 1 / -1; }
.perm-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.perm-row:last-of-type { border-bottom: 0; }
.perm-row select { width: auto; min-width: 190px; }
.perm-check { justify-content: flex-start; cursor: pointer; }
.perm-scope { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.perm-scope:last-child { border-bottom: 0; }
.perm-scope-label { min-width: 140px; font-weight: 550; }
.perm-scope-mode { width: auto; }
.perm-scope-pick[hidden] { display: none; }
.perm-top { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 16px; margin-bottom: 12px; }
.perm-top .fld { min-width: 240px; }
.inline-add { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--border); }
.danger-zone { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }

/* Department dashboards: tiles on a 6-column grid */
.dash-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 16px; align-items: start; }
.dash-tile { min-width: 0; }
.dash-tile > .card, .dash-tile > .chart-card, .dash-tile > .kpi { margin: 0; height: 100%; }
.dash-kpi { grid-column: span 1; }
.dash-third { grid-column: span 2; }
.dash-half { grid-column: span 3; }
.dash-full { grid-column: 1 / -1; }
@media (max-width: 1200px) { .dash-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } .dash-third, .dash-half { grid-column: span 2; } }
@media (max-width: 760px) { .dash-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } .dash-third, .dash-half, .dash-full { grid-column: 1 / -1; } }

/* Dashboard builder */
.tile-list { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.tile-edit { margin: 0; }
.tile-edit.is-dragging { opacity: 0.5; }
.tile-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.tile-grip { cursor: grab; color: var(--text-3); font-weight: 700; letter-spacing: -2px; padding: 2px 4px; user-select: none; }
.tile-move { margin-left: auto; display: flex; gap: 6px; }
.tile-fields { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px 14px; }
.tile-fields .fld { min-width: 160px; flex: 0 1 auto; }
.tile-check { padding-bottom: 8px; }
.tile-note { margin: 0 0 8px; }
.tile-filters { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border); }
.tile-add { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; }
.dash-depts { border: 0; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; }
.dash-depts legend { font-size: 0.82rem; color: var(--text-2); font-weight: 500; padding: 0; margin-bottom: 6px; }
.inline-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.inline-actions form { margin: 0; }
@media (min-width: 1300px) {
  .tile-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; }
}

/* Phones and tablets: logo and a Menu button on one row; nav, sync time and sign-out open below it.
   The filter chips fold behind a "Filters" button so results are on screen straight away. */
.nav-toggle, .fbar-more { display: none; }
@media (max-width: 1279px) {
  .topbar-inner { flex-wrap: wrap; padding: 0 16px; gap: 0; min-height: 52px; }
  .brand { min-height: 52px; gap: 12px; }
  .brand-logo { margin: 0 0 0 -16px; padding: 0 16px; }
  .brand-name { display: inline; font-size: 0.98rem; }
  .nav-toggle {
    display: inline-flex; align-items: center; gap: 8px; margin-left: auto; align-self: center;
    font: inherit; font-size: 0.9rem; font-weight: 600; color: #fff; background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 8px; min-height: 36px; padding: 0 12px; cursor: pointer;
  }
  .nav-toggle-bars, .nav-toggle-bars::before, .nav-toggle-bars::after {
    display: block; width: 16px; height: 2px; border-radius: 2px; background: currentColor; position: relative; transition: transform .15s, opacity .15s;
  }
  .nav-toggle-bars::before, .nav-toggle-bars::after { content: ''; position: absolute; left: 0; }
  .nav-toggle-bars::before { top: -5px; }
  .nav-toggle-bars::after { top: 5px; }
  .topbar.is-open .nav-toggle-bars { background: transparent; }
  .topbar.is-open .nav-toggle-bars::before { transform: translateY(5px) rotate(45deg); }
  .topbar.is-open .nav-toggle-bars::after { transform: translateY(-5px) rotate(-45deg); }
  .topbar .nav, .topbar .who { display: none; }
  .topbar.is-open .nav { display: flex; flex-direction: column; gap: 2px; width: 100%; padding: 8px 0; border-top: 1px solid rgba(255, 255, 255, 0.12); }
  .topbar.is-open .nav a, .topbar.is-open .nav-menu > summary { padding: 11px 12px; font-size: 1rem; }
  .topbar.is-open .nav-menu { width: 100%; }
  .topbar.is-open .nav-menu-pop { position: static; box-shadow: none; background: transparent; border: 0; padding: 0 0 0 12px; min-width: 0; }
  .topbar.is-open .nav-menu-pop a { color: rgba(255, 255, 255, 0.78) !important; padding: 11px 12px; font-size: 1rem; }
  .topbar.is-open .nav-menu-pop a:hover, .topbar.is-open .nav-menu-pop a[aria-current="page"] { color: #fff !important; background: rgba(255, 255, 255, 0.14) !important; }
  .topbar.is-open .who { display: flex; width: 100%; justify-content: space-between; margin: 0; padding: 10px 0 14px; border-top: 1px solid rgba(255, 255, 255, 0.12); }

}
@media (max-width: 760px) {
  .page-head { flex-wrap: wrap; gap: 10px; }
  .page-head h1 { font-size: 1.45rem; }
  .fbar { padding: 12px; }
  .fbar-more { display: inline-flex; }
  .fbar-more[hidden] { display: none; }
  .fbar-chips { display: none; }
  .fbar.fbar-open .fbar-chips { display: flex; }
  .filter-actions { flex-wrap: wrap; }
  .filter-actions .btn { flex: 1 1 0; min-width: 0; padding: 0 10px; }
  .chip { max-width: 100%; }
  .chip-v { max-width: 9rem; }
  .fbar.fbar-open .fbar-more { background: var(--surface-2); border-color: var(--accent); }
  /* Filter pick lists open as a sheet along the bottom of the screen. */
  .ms-pop, .ms-big .ms-pop {
    position: fixed; top: auto; left: 8px; right: 8px; bottom: 8px; width: auto; max-width: none;
    max-height: 78vh; border-radius: 14px; box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.3);
  }
  .ms-list, .ms-big .ms-list { max-height: 48vh; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi { padding: 12px; }
  .kpi-value { font-size: 1.25rem; }
}
