/* UK Train Sim - design system (round 109).
   One set of colours, type and components for every page, in light and
   dark. Colours are variables, so a page only ever names a role (--text,
   --surface, --bad...) and the theme decides the shade; theme-init.js sets
   data-theme on <html> before anything is drawn. */

:root,
:root[data-theme="light"] {
  --bg: #F3F5F8;
  --surface: #FFFFFF;
  --surface-2: #F7F9FB;
  --surface-3: #EEF1F5;
  --hover: #F1F4F8;
  --border: #E2E7EE;
  --border-strong: #CED6E0;
  --text: #111A27;
  --text-2: #344255;
  --text-muted: #5B6878;
  --accent: #2563EB;
  --accent-hover: #1D4ED8;
  --accent-text: #FFFFFF;
  --accent-soft: #E8F0FE;
  --link: #1D4ED8;
  --focus: rgba(37, 99, 235, 0.35);
  --ok: #0E7A47;
  --ok-bg: #E4F5EC;
  --ok-solid: #15803D;
  --warn: #8A4B00;
  --warn-bg: #FEF3C7;
  --warn-solid: #B45309;
  --bad: #B42318;
  --bad-bg: #FDECEA;
  --bad-solid: #C62828;
  --info: #1D4ED8;
  --info-bg: #E8F0FE;
  --purple: #6B3FA0;
  --purple-bg: #F0E9FA;
  --purple-solid: #6B3FA0;
  --chart-1: #2563EB;
  --chart-2: #94B4F2;
  --chart-grid: #E2E7EE;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 8px 24px rgba(16, 24, 40, 0.12);
  --overlay: rgba(10, 16, 25, 0.45);
  /* The map page */
  --map-bg: #E9EDF1;
  --track: #5A6878;
  --track-dim: rgba(90, 104, 120, 0.25);
  --station-fill: #FFFFFF;
  --station-ring: #344255;
  --map-ok: #16A34A;
  --map-warn: #D97706;
  --map-bad: #DC2626;
  --map-purple: #7C3AED;
  --map-freight: #64748B;
  --map-you: #2563EB;
  --map-stroke: #FFFFFF;
  --map-panel: rgba(255, 255, 255, 0.96);
}

:root[data-theme="dark"] {
  --bg: #0E141D;
  --surface: #151D29;
  --surface-2: #192231;
  --surface-3: #1F2A3A;
  --hover: #1C2636;
  --border: #253246;
  --border-strong: #33445A;
  --text: #E8EEF5;
  --text-2: #C3CEDA;
  --text-muted: #96A5B7;
  --accent: #2563EB;
  --accent-hover: #3B74F0;
  --accent-text: #FFFFFF;
  --accent-soft: rgba(59, 130, 246, 0.18);
  --link: #8AB4FF;
  --focus: rgba(138, 180, 255, 0.45);
  --ok: #5FD69A;
  --ok-bg: rgba(52, 211, 153, 0.14);
  --ok-solid: #15803D;
  --warn: #F5C25B;
  --warn-bg: rgba(245, 184, 61, 0.14);
  --warn-solid: #B45309;
  --bad: #FF8F85;
  --bad-bg: rgba(255, 107, 107, 0.14);
  --bad-solid: #C62828;
  --info: #8AB4FF;
  --info-bg: rgba(59, 130, 246, 0.16);
  --purple: #C9AEF5;
  --purple-bg: rgba(167, 139, 250, 0.16);
  --purple-solid: #7C4DBA;
  --chart-1: #5B8DEF;
  --chart-2: #2E4A78;
  --chart-grid: #253246;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 12px 32px rgba(0, 0, 0, 0.45);
  --overlay: rgba(0, 0, 0, 0.55);
  --map-bg: #0E1520;
  --track: #7C8DA3;
  --track-dim: rgba(124, 141, 163, 0.18);
  --station-fill: #0E1520;
  --station-ring: #C3CEDA;
  --map-ok: #34D399;
  --map-warn: #F5B83D;
  --map-bad: #FF6B6B;
  --map-purple: #B794F6;
  --map-freight: #9AA8B8;
  --map-you: #6EA8FF;
  --map-stroke: #0E1520;
  --map-panel: rgba(17, 26, 38, 0.95);
}

:root {
  color-scheme: light;
  --font-sans: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --radius: 12px;
  --radius-sm: 8px;
  --sidebar-w: 240px;
  --topbar-h: 64px;
  --content-max: 1240px;
  --gutter: 28px;
  /* The app's chrome stays dark in both themes. */
  --chrome-bg: #0B1119;
  --chrome-2: #131C28;
  --chrome-3: #1B2738;
  --chrome-border: #1C2735;
  --chrome-text: #E6EDF5;
  --chrome-text-2: #B4C1CF;
  --chrome-muted: #8B99AA;
  /* Names the older pages used, kept pointing at the new roles. */
  --navy: var(--text);
  --navy-light: var(--link);
  --green: var(--ok);
  --red: var(--bad);
  --amber: var(--warn);
  --card-bg: var(--surface);
  --shadow: var(--shadow-sm);
}
:root[data-theme="dark"] { color-scheme: dark; --chrome-bg: #090E15; --chrome-2: #121A26; --chrome-3: #1B2738; }

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { color: var(--text); letter-spacing: -0.01em; line-height: 1.3; }
h1 { font-size: 22px; font-weight: 600; margin: 0 0 6px; }
h2 { font-size: 16px; font-weight: 600; margin: 28px 0 12px; }
h3 { font-size: 14px; font-weight: 600; margin: 18px 0 8px; }
h4 { font-size: 13px; font-weight: 600; margin: 12px 0 6px; }
p { margin: 0 0 10px; }
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
b, strong { font-weight: 600; }
hr { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }
code, kbd, .mono { font-family: var(--font-mono); }
.num, .tabular { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-muted); }
.small { font-size: 12.5px; }
.ok-text { color: var(--ok); }
.warn-text { color: var(--warn); }
.bad-text { color: var(--bad); }
[hidden] { display: none !important; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
::selection { background: var(--accent-soft); }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 6px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Form controls ---------- */
button, input, select, textarea { font: inherit; color: inherit; }
button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 36px; padding: 0 14px;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: var(--accent); color: var(--accent-text);
  font-size: 13px; font-weight: 600; line-height: 1.2;
  cursor: pointer; white-space: nowrap;
  transition: background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}
button:hover { background: var(--accent-hover); }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
button.go, .btn-ok { background: var(--ok-solid); color: #FFFFFF; }
button.go:hover, .btn-ok:hover { background: #166534; }
button.danger, .btn-danger { background: var(--bad-solid); color: #FFFFFF; }
button.danger:hover, .btn-danger:hover { background: #A61B1B; }
button.secondary, .btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
button.secondary:hover, .btn-secondary:hover { background: var(--hover); }
button.ghost, .btn-ghost { background: transparent; color: var(--text-2); border-color: transparent; }
button.ghost:hover, .btn-ghost:hover { background: var(--hover); color: var(--text); }
button.small, .btn-sm { min-height: 30px; padding: 0 10px; font-size: 12.5px; }
a.btn, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 36px; padding: 0 14px; border-radius: var(--radius-sm);
  border: 1px solid transparent; background: var(--accent); color: var(--accent-text);
  font-size: 13px; font-weight: 600; text-decoration: none; cursor: pointer; white-space: nowrap;
}
a.btn:hover { background: var(--accent-hover); text-decoration: none; }
a.btn.btn-secondary, a.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
a.btn.btn-secondary:hover, a.btn.secondary:hover { background: var(--hover); }
a.btn.small { min-height: 30px; padding: 0 10px; font-size: 12.5px; }
button.ghost.danger-text, .danger-text { color: var(--bad); }
button.ghost.danger-text:hover { background: var(--bad-bg); color: var(--bad); }

input[type="text"], input[type="number"], input[type="search"], input[type="password"], input[type="email"],
input[type="time"], input[type="date"], input[type="datetime-local"], input:not([type]), select, textarea {
  min-height: 36px; padding: 6px 10px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
  font-size: 13.5px; max-width: 100%;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
textarea { min-height: 80px; line-height: 1.45; }
select { padding-right: 28px; }
input::placeholder, textarea::placeholder { color: var(--text-muted); opacity: 0.85; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus); }
input[type="checkbox"], input[type="radio"], input[type="range"] { accent-color: var(--accent); }
input[type="checkbox"], input[type="radio"] { width: 16px; height: 16px; vertical-align: middle; }
label { color: var(--text-2); }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > label, .field-label { font-size: 12.5px; color: var(--text-muted); font-weight: 500; }

/* ---------- Tables ---------- */
table {
  width: 100%; border-collapse: separate; border-spacing: 0;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
  font-size: 13.5px;
}
th {
  text-align: left; padding: 10px 14px;
  background: var(--surface-2); color: var(--text-muted);
  font-size: 12px; font-weight: 600; white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--hover); }
td.num, th.num, .num-col { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { position: relative; overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.table-wrap > table { border: 0; border-radius: 0; }
.card > table, .card .table-wrap > table { border-radius: 0; }

/* ---------- Surfaces ---------- */
.card, .panel {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px;
}
.card.flush, .panel.flush { padding: 0; overflow: hidden; }
.card-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  margin-bottom: 12px;
}
.card-head h2, .card-head h3 { margin: 0; }
.card.flush > .card-head { padding: 14px 20px; margin: 0; border-bottom: 1px solid var(--border); }
.section-title { font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-muted); margin: 24px 0 10px; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 18px; }
.page-intro { color: var(--text-muted); font-size: 13.5px; margin: 0 0 18px; max-width: 760px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { margin: 0; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 15px 18px; display: flex; flex-direction: column; gap: 4px; min-width: 0;
}
.kpi-label { font-size: 12.5px; color: var(--text-muted); }
.kpi-value { font-size: 22px; font-weight: 600; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; line-height: 1.2; overflow-wrap: anywhere; }
.kpi-value.mono { font-family: var(--font-mono); font-size: 20px; }
.kpi-sub { font-size: 12.5px; color: var(--text-muted); }
.kpi-sub.ok { color: var(--ok); } .kpi-sub.bad { color: var(--bad); } .kpi-sub.warn { color: var(--warn); }

/* Badges */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 600; line-height: 1.5; white-space: nowrap;
  background: var(--surface-3); color: var(--text-2);
}
.badge-ok, .badge-good, .badge-available, .badge.live { background: var(--ok-bg); color: var(--ok); }
.badge-warn, .badge-renovation, .badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge-bad, .badge-maintenance, .badge-nodriver, .badge-conflict { background: var(--bad-bg); color: var(--bad); }
.badge-info, .badge-upgrade, .badge-assigned, .badge-replacement { background: var(--info-bg); color: var(--info); }
.badge-purple, .badge-upgraded, .badge-ai { background: var(--purple-bg); color: var(--purple); }
.badge-neutral, .badge.paused { background: var(--surface-3); color: var(--text-muted); }

/* Notices - a tinted box for something to read or act on */
.notice {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px 14px; border-radius: var(--radius-sm);
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  font-size: 13.5px; margin: 10px 0;
}
.notice-bad { background: var(--bad-bg); border-color: transparent; }
.notice-warn { background: var(--warn-bg); border-color: transparent; }
.notice-info { background: var(--info-bg); border-color: transparent; }
.notice-ok { background: var(--ok-bg); border-color: transparent; }
.notice b { color: var(--text); }
.empty { padding: 22px; text-align: center; color: var(--text-muted); font-size: 13.5px; }
.card .empty { padding: 14px 0; text-align: left; }

/* Tabs */
.tabs {
  display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none;
  border-bottom: 1px solid var(--border); margin: 0 0 20px;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs .tab {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 44px; padding: 0 12px; margin-bottom: -1px;
  background: none; border: 0; border-bottom: 2px solid transparent; border-radius: 0;
  color: var(--text-muted); font-size: 13.5px; font-weight: 500; text-decoration: none; white-space: nowrap; cursor: pointer;
}
.tabs .tab:hover { color: var(--text); background: none; text-decoration: none; }
.tabs .tab.active, .tabs .tab[aria-selected="true"], .tabs .tab[aria-current="page"] { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }
.tab-count:empty { display: none; }
.tab-count { padding: 0 7px; border-radius: 9px; background: var(--surface-3); color: var(--text-muted); font-size: 12px; font-weight: 600; }
.tab-count.warn { background: var(--warn-bg); color: var(--warn); }
.tab-count.bad { background: var(--bad-bg); color: var(--bad); }
[data-tab-panel][hidden] { display: none !important; }

/* Segmented control */
.seg { display: inline-flex; gap: 2px; padding: 3px; border-radius: 10px; background: var(--surface-3); }
.seg button {
  min-height: 32px; padding: 0 12px; border-radius: 7px; border: 0;
  background: transparent; color: var(--text-2); font-weight: 500;
}
.seg button:hover { background: var(--hover); color: var(--text); }
.seg button.active, .seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow-sm); }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin: 0 0 14px; }
.search {
  display: flex; align-items: center; gap: 8px; min-height: 40px; padding: 0 12px;
  border: 1px solid var(--border-strong); border-radius: 10px; background: var(--surface); color: var(--text-muted);
  flex: 0 1 300px; min-width: 180px;
}
.search input { border: 0; box-shadow: none; padding: 0; min-height: 0; background: transparent; flex: 1; min-width: 0; }
.search input:focus { box-shadow: none; }
.search:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus); }

/* A thin bar for a 0-100 value */
.meter { display: inline-block; width: 64px; height: 6px; border-radius: 3px; background: var(--surface-3); overflow: hidden; vertical-align: middle; }
.meter > span { display: block; height: 100%; background: var(--ok-solid); border-radius: 3px; }
.meter.warn > span { background: var(--warn-solid); }
.meter.bad > span { background: var(--bad-solid); }

details.help { border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2); padding: 0 14px; margin: 10px 0; }
details.help > summary { cursor: pointer; padding: 10px 0; font-weight: 600; font-size: 13px; color: var(--text-2); list-style: none; }
details.help > summary::-webkit-details-marker { display: none; }
details.help > summary::before { content: '+'; display: inline-block; width: 16px; color: var(--text-muted); }
details.help[open] > summary::before { content: '\2212'; }
details.help > *:not(summary) { font-size: 13px; color: var(--text-2); }
details.help[open] { padding-bottom: 12px; }

/* ---------- App shell (shell.js builds it) ---------- */
html.has-shell body { padding-left: var(--sidebar-w); padding-top: var(--topbar-h); }
html.has-shell body:not(.page-full) {
  padding-top: calc(var(--topbar-h) + 24px);
  padding-bottom: 48px;
  padding-left: calc(var(--sidebar-w) + max(var(--gutter), (100vw - var(--sidebar-w) - var(--content-max)) / 2));
  padding-right: max(var(--gutter), (100vw - var(--sidebar-w) - var(--content-max)) / 2);
}
body[data-width="narrow"] { --content-max: 860px; }
body[data-width="wide"] { --content-max: 1480px; }

.app-sidebar {
  position: fixed; z-index: 2000; top: 0; left: 0; bottom: 0; width: var(--sidebar-w);
  background: var(--chrome-bg); color: var(--chrome-text-2);
  display: flex; flex-direction: column; gap: 14px;
  padding: 16px 12px 10px; overflow-y: auto; overscroll-behavior: contain;
  border-right: 1px solid var(--chrome-border);
  scrollbar-width: thin; scrollbar-color: #2A3748 transparent;
}
.app-brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px; color: #FFFFFF; font-weight: 700; font-size: 15px; letter-spacing: -0.01em; text-decoration: none; }
.app-brand:hover { text-decoration: none; }
.app-brand .signal { width: 10px; height: 10px; border-radius: 50%; background: #34D399; box-shadow: 0 0 0 4px rgba(52, 211, 153, 0.18); flex: none; }
.app-company { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 10px; background: var(--chrome-2); color: inherit; text-decoration: none; min-width: 0; }
.app-company:hover { text-decoration: none; background: var(--chrome-3); }
.app-company .avatar { width: 36px; height: 36px; flex: none; border-radius: 8px; background: #2563EB; color: #FFFFFF; font-weight: 700; font-size: 13px; display: flex; align-items: center; justify-content: center; }
.app-company .who { display: flex; flex-direction: column; min-width: 0; }
.app-company .name { color: #FFFFFF; font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-company .home { color: var(--chrome-muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-nav { display: flex; flex-direction: column; gap: 8px; }
.app-nav .group { display: flex; flex-direction: column; gap: 1px; }
.app-nav .group-title { font-size: 11px; font-weight: 600; letter-spacing: 0.07em; text-transform: uppercase; color: var(--chrome-muted); padding: 4px 10px 3px; }
.app-nav a {
  display: flex; align-items: center; gap: 10px; min-height: 36px; padding: 0 10px; border-radius: 8px;
  color: var(--chrome-text-2); font-size: 13.5px; font-weight: 500; text-decoration: none;
}
.app-nav a:hover { background: var(--chrome-2); color: #FFFFFF; text-decoration: none; }
.app-nav a.active { background: var(--chrome-3); color: #FFFFFF; font-weight: 600; }
.app-nav a svg { flex: none; opacity: 0.9; }
.app-nav a .label { flex: 1; }
.app-nav .count { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: #F5B83D; color: #1A1205; font-size: 11.5px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; }
@media (min-width: 901px) and (max-height: 960px) {
  .app-sidebar { gap: 10px; padding-top: 12px; }
  .app-nav { gap: 4px; }
  .app-nav a { min-height: 32px; }
  .app-nav .group-title { padding-top: 3px; padding-bottom: 2px; }
}
.app-sidebar-foot { margin-top: auto; padding: 8px 6px 0; border-top: 1px solid var(--chrome-border); display: flex; flex-direction: column; gap: 10px; }
.theme-menu { width: 300px; }
.theme-choices { display: flex; flex-direction: column; padding: 6px; gap: 2px; }
.theme-choices button {
  justify-content: flex-start; text-align: left; gap: 12px; min-height: 48px; padding: 8px 12px;
  background: transparent; color: var(--text); border: 0; border-radius: var(--radius-sm); white-space: normal; font-weight: 400;
}
.theme-choices button > span { display: flex; flex-direction: column; gap: 1px; }
.theme-choices button:hover { background: var(--hover); }
.theme-choices button[aria-pressed="true"] { background: var(--accent-soft); }
.theme-choices button svg { flex: none; color: var(--text-2); }
.app-sidebar-foot .links { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.app-sidebar-foot .links a, .app-sidebar-foot .links button { display: inline-flex; align-items: center; color: var(--chrome-muted); font-size: 12.5px; background: none; border: 0; padding: 0 4px; min-height: 32px; font-weight: 500; line-height: 1; }
.app-sidebar-foot .links a:hover, .app-sidebar-foot .links button:hover { color: #FFFFFF; background: none; text-decoration: none; }
.app-sidebar-foot .links .admin { color: #FF9B91; }

.app-topbar {
  position: fixed; z-index: 1900; top: 0; left: var(--sidebar-w); right: 0; height: var(--topbar-h);
  display: flex; align-items: center; gap: 20px; padding: 0 24px;
  background: var(--surface); border-bottom: 1px solid var(--border); color: var(--text);
}
body.page-full .app-topbar, body.topbar-dark .app-topbar { background: var(--chrome-bg); border-bottom-color: var(--chrome-border); color: var(--chrome-text); }
.app-topbar .menu-btn { display: none; width: 40px; height: 40px; min-height: 40px; padding: 0; background: transparent; color: inherit; border-radius: 10px; }
.app-topbar .menu-btn:hover { background: var(--hover); }
body.page-full .app-topbar .menu-btn:hover { background: var(--chrome-2); }
.app-topbar .title { margin: 0; min-width: 0; flex: 0 0 auto; max-width: 45%; font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: inherit; }
.app-topbar .clock { display: flex; align-items: baseline; gap: 10px; white-space: nowrap; min-width: 0; }
.app-topbar .clock .time { font-family: var(--font-mono); font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; }
.app-topbar .clock .date { font-size: 12.5px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; min-width: 0; white-space: nowrap; }
body.page-full .app-topbar .clock .date, body.topbar-dark .app-topbar .clock .date { color: var(--chrome-muted); }
.app-topbar .spacer { flex: 1; }
.app-topbar .money { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.25; }
.app-topbar .money .k { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.06em; }
body.page-full .app-topbar .money .k { color: var(--chrome-muted); }
.app-topbar .money .v { font-family: var(--font-mono); font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.app-topbar .money .v.up { color: var(--ok); }
.app-topbar .money .v.down { color: var(--bad); }
body.page-full .app-topbar .money .v.up { color: #6EE7B7; }
body.page-full .app-topbar .money .v.down { color: #FF9B91; }
.app-topbar .icon-btn {
  position: relative; width: 44px; height: 44px; min-height: 44px; padding: 0;
  border-radius: 10px; border: 1px solid var(--border-strong); background: var(--surface); color: var(--text-2);
}
.app-topbar .icon-btn:hover { background: var(--hover); }
body.page-full .app-topbar .icon-btn { border-color: #263447; background: #111A26; color: #C9D3DE; }
body.page-full .app-topbar .icon-btn:hover { background: var(--chrome-3); }
.app-topbar .icon-btn .dot { position: absolute; top: 5px; right: 5px; min-width: 17px; height: 17px; padding: 0 4px; border-radius: 9px; background: #F5B83D; color: #1A1205; font-size: 10.5px; font-weight: 700; display: none; align-items: center; justify-content: center; }
.app-topbar .icon-btn .dot.show { display: flex; }
.app-topbar .new-btn { min-height: 44px; border-radius: 10px; padding: 0 16px; }

.app-popover {
  position: fixed; z-index: 2100; top: calc(var(--topbar-h) - 4px); right: 16px; width: 360px; max-width: calc(100vw - 24px);
  max-height: min(520px, calc(100vh - var(--topbar-h) - 24px)); overflow-y: auto;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-md);
  display: none;
}
.app-popover.open { display: block; }
.app-popover .pop-head { position: sticky; top: 0; background: var(--surface); padding: 12px 16px; font-weight: 600; border-bottom: 1px solid var(--border); }
.app-popover .pop-head.pop-sub { position: static; border-top: 1px solid var(--border); }
.pref-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 10px 16px 14px; cursor: pointer; }
.pref-row > span { display: flex; flex-direction: column; gap: 2px; }
/* A checkbox drawn as an on/off switch */
input.switch { appearance: none; -webkit-appearance: none; flex: none; width: 40px; height: 24px; border-radius: 12px; background: var(--border-strong); position: relative; cursor: pointer; transition: background-color 0.15s ease; margin: 0; }
input.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #FFFFFF; box-shadow: 0 1px 2px rgba(0,0,0,0.25); transition: transform 0.15s ease; }
input.switch:checked { background: var(--accent); }
input.switch:checked::after { transform: translateX(16px); }
input.switch:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
/* Round 111: with freight hidden (theme-init.js), anything marked freight-only goes */
.no-freight .freight-only { display: none !important; }
.app-popover .item { padding: 11px 16px; border-bottom: 1px solid var(--border); font-size: 13px; }
.app-popover .item:last-child { border-bottom: none; }
.app-popover .item .time { font-size: 11.5px; color: var(--text-muted); margin-top: 2px; }
.app-popover .amount-pos { color: var(--ok); font-weight: 600; }
.app-popover .amount-neg { color: var(--bad); font-weight: 600; }
.app-popover .pop-foot { position: sticky; bottom: 0; display: flex; align-items: center; gap: 8px; padding: 10px 16px; background: var(--surface); border-top: 1px solid var(--border); font-size: 13px; font-weight: 500; }
.app-popover .pop-foot:hover { background: var(--hover); text-decoration: none; }
.app-backdrop { position: fixed; inset: 0; z-index: 1950; background: var(--overlay); display: none; }

/* Pages that sit under a page-level tab bar shared between pages */
.page-tabs { margin-top: -6px; }

/* ---------- Leaflet maps, in either theme ---------- */
html .leaflet-container { background: var(--map-bg); font-family: var(--font-sans); }
:root[data-theme="light"] .leaflet-tile-pane { filter: saturate(0.55) brightness(1.02); }
:root[data-theme="dark"] .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.82) contrast(0.92) saturate(0.45); }
html .leaflet-popup-content-wrapper, html .leaflet-popup-tip { background: var(--surface); color: var(--text); box-shadow: var(--shadow-md); }
html .leaflet-popup-content-wrapper { border-radius: var(--radius); border: 1px solid var(--border); }
html .leaflet-container a.leaflet-popup-close-button { color: var(--text-muted); }
html .leaflet-tooltip { background: var(--surface); color: var(--text); border: 1px solid var(--border); box-shadow: var(--shadow-md); border-radius: var(--radius-sm); }
html .leaflet-tooltip-top:before { border-top-color: var(--surface); }
html .leaflet-tooltip-bottom:before { border-bottom-color: var(--surface); }
html .leaflet-tooltip-left:before { border-left-color: var(--surface); }
html .leaflet-tooltip-right:before { border-right-color: var(--surface); }
html .leaflet-control-attribution { background: color-mix(in srgb, var(--surface) 82%, transparent) !important; color: var(--text-muted); }
html .leaflet-control-attribution a { color: var(--link); }
html .leaflet-bar a, html .leaflet-bar a:hover { background: var(--surface); color: var(--text); border-bottom-color: var(--border); }
html .leaflet-bar { border: 1px solid var(--border-strong) !important; box-shadow: var(--shadow-sm) !important; }

/* ---------- Small screens ---------- */
@media (max-width: 900px) {
  :root { --gutter: 16px; }
  html.has-shell body, html.has-shell body:not(.page-full) { padding-left: var(--gutter); padding-right: var(--gutter); }
  html.has-shell body.page-full { padding-left: 0; padding-right: 0; }
  .app-sidebar { transform: translateX(-100%); transition: transform 0.2s ease; box-shadow: var(--shadow-md); width: min(300px, 86vw); }
  html.nav-open .app-sidebar { transform: none; }
  html.nav-open .app-backdrop { display: block; }
  .app-topbar { left: 0; gap: 10px; padding: 0 12px; }
  .app-topbar .menu-btn { display: inline-flex; }
}
@media (max-width: 640px) {
  :root { --topbar-h: 58px; }
  body { font-size: 14.5px; }
  .app-topbar .clock, .app-topbar .money.today, .app-topbar .new-btn, .app-topbar .money .k { display: none; }
  .app-topbar .icon-btn { width: 40px; height: 40px; min-height: 40px; }
  .app-topbar .title { font-size: 16px; flex: 1 1 auto; max-width: none; }
  /* Round 118: a long page name gives way, not the alerts button (it was pushed off the edge). */
  .app-topbar .spacer { display: none; }
  .app-topbar .money, .app-topbar .icon-btn, .app-topbar .menu-btn { flex: none; }
  .kpi-value { font-size: 19px; }
  .card, .panel { padding: 14px 16px; }
  th, td { padding: 9px 10px; }
  .search { flex-basis: 100%; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
