/* Shared operations workspace: quiet surfaces, readable comparisons, explicit states. */
:root {
    --bg-body: #F5F6F3;
    --bg-sidebar: #FFFFFF;
    --bg-card: #FFFFFF;
    --bg-inset: #EEF2F0;
    --bg-hover: #E5ECE9;
    --border: #CED8D4;
    --text-1: #192B29;
    --text-2: #485D59;
    --text-3: #566C67;
    --accent: #00695E;
    --accent-hover: #005B51;
    --on-accent: #FFFFFF;
    --green: #28674C;
    --red: #A43131;
    --orange: #805000;
    --purple: #614A91;
    --cyan: #246D75;
    --selected-bg: #DFECE8;
    --selected-fg: #164B44;
    --selected-border: #A7C7BD;
    --overlay: rgb(25 43 41 / .48);
    --shadow-pop: 0 12px 32px rgb(25 43 41 / .16);

    /* Type: 12px is the floor for any text. Display face is for headings and figures only. */
    --font-ui: 'Segoe UI', system-ui, -apple-system, sans-serif;
    --font-display: Bahnschrift, 'Segoe UI', system-ui, sans-serif;
    --fs-xs: 12px;
    --fs-sm: 13px;
    --fs-md: 14px;
    --fs-lg: 18px;
    --fs-xl: 20px;
    --fs-heading: 24px;
    --fs-num-sm: 22px;
    --fs-num-md: 30px;
    --fs-num-lg: 44px;
    --lh-tight: 1.25;
    --lh-base: 1.5;
    --lh-prose: 1.65;

    /* Space (4px base), shape, sizes */
    --sp-1: 4px;
    --sp-2: 8px;
    --sp-3: 12px;
    --sp-4: 16px;
    --sp-5: 20px;
    --sp-6: 24px;
    --sp-7: 32px;
    --radius-sm: 4px;
    --radius-md: 6px;
    --radius: 10px;
    --control-h: 36px;
    --sidebar-w: 220px;
    --gutter: var(--sp-6); /* horizontal page padding shared by the report bar, the status strip and the content */
    --bar-row-h: 52px;
    /* Sticky offsets use shell.js measurements, with first-paint estimates until they arrive. */
    --topbar-h: var(--measured-topbar-h, calc(var(--bar-row-h) + 1px));
    --status-h: var(--measured-status-h, 0px);

    /* Layering. Native <dialog> and popover render in the top layer and need no z-index. */
    --z-raised: 1; /* local stacking inside a component: menu panels in the bar, sticky table heads */
    --z-status: 99;
    --z-topbar: 100;
    --z-sidebar: 200;
    --z-toast: 400;
    --z-skip: 1000;

    --chart-grid: #CED8D4;
    --chart-tick: #485D59;
    --chart-legend: #485D59;
    --chart-bar-1: #286B91;
    --chart-bar-2: #337A5D;
    --chart-bar-3: #7159AC;
    --chart-bar-neg: #A43131;
    color-scheme: light;
}

:root[data-theme="dark"] {
    --bg-body: #141c27;
    --bg-sidebar: #17212e;
    --bg-card: #1b2735;
    --bg-inset: #202e3e;
    --bg-hover: #233346;
    --border: #314154;
    --text-1: #ecf1f7;
    --text-2: #a8b8ca;
    --text-3: #8da0b6;
    --accent: #82D7C9;
    --accent-hover: #A4E7DC;
    --on-accent: #102D29;
    --green: #9bcbb5;
    --red: #e49c98;
    --orange: #e1b277;
    --purple: #b4a8e7;
    --cyan: #8dc9d1;
    --selected-bg: #283b53;
    --selected-fg: #e3edff;
    --selected-border: #496183;
    --overlay: rgb(8 12 18 / .62);
    --shadow-pop: 0 14px 40px rgb(0 0 0 / .33);
    --chart-grid: #314154;
    --chart-tick: #a8b8ca;
    --chart-legend: #a8b8ca;
    --chart-bar-1: #91b6fa;
    --chart-bar-2: #9bcbb5;
    --chart-bar-3: #b4a8e7;
    --chart-bar-neg: #e49c98;
    color-scheme: dark;
}
html { scroll-padding-top: calc(var(--topbar-h) + var(--status-h) + var(--sp-3)); }
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
body { display: flex; min-height: 100vh; background: var(--bg-body); color: var(--text-1); font: var(--fs-md)/var(--lh-base) var(--font-ui); }
button, input, select, textarea { font-family: inherit; }
button { cursor: pointer; }
h1 { font-size: var(--fs-heading); }
h2 { font-size: var(--fs-lg); }
a { color: var(--accent); }
::selection { background: var(--selected-bg); color: var(--selected-fg); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
[hidden], .hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.skip-link { position: fixed; top: -80px; left: var(--sp-4); z-index: var(--z-skip); padding: var(--sp-3) var(--sp-4); border-radius: var(--radius-md); background: var(--accent); color: var(--on-accent); }
.skip-link:focus { top: var(--sp-3); }

/* The same navigation is a fixed sidebar on desktop and a disclosure on smaller screens. */
.sidebar { position: fixed; inset: 0 auto 0 0; z-index: var(--z-sidebar); display: flex; flex-direction: column; width: var(--sidebar-w); border-right: 1px solid var(--border); background: var(--bg-sidebar); }
.sidebar-brand { display: flex; flex: none; align-items: center; gap: var(--sp-3); min-height: calc(var(--bar-row-h) + 1px); padding: 0 var(--sp-4); border-bottom: 1px solid var(--border); color: var(--text-1); text-decoration: none; }
.sidebar-brand:focus-visible { outline-offset: -3px; }
.brand-mark { display: grid; flex: none; place-items: center; width: var(--brand-mark-size, 34px); height: var(--brand-mark-size, 34px); border: 1px solid var(--selected-border); border-radius: var(--radius-md); background: var(--selected-bg); color: var(--selected-fg); font: 700 var(--fs-sm)/1 var(--font-display); }
.nav-toggle.btn { display: none; }
.brand-name { font: 600 var(--fs-md)/var(--lh-tight) var(--font-display); }
.sidebar-nav { display: grid; align-content: start; flex: 1; gap: var(--sp-2); min-height: 0; padding: var(--sp-3) var(--sp-2); overflow-y: auto; }
.nav-group-title { padding: var(--sp-1) var(--sp-3); color: var(--text-3); font-size: var(--fs-xs); font-weight: 600; }
.nav-group .nav-link { padding-left: var(--sp-4); }
.nav-link { display: flex; align-items: center; min-height: 36px; padding: 0 var(--sp-3); border-radius: var(--radius-md); color: var(--text-2); font-size: var(--fs-sm); text-decoration: none; }
.nav-link:hover { background: var(--bg-hover); color: var(--text-1); }
.nav-link.active { background: var(--selected-bg); color: var(--selected-fg); font-weight: 600; }
.sidebar-footer { display: grid; gap: var(--sp-1); padding: var(--sp-3) var(--sp-2); border-top: 1px solid var(--border); }
.sidebar-db { display: flex; align-items: flex-start; gap: var(--sp-2); padding: var(--sp-1) var(--sp-3) var(--sp-2); color: var(--text-2); font-size: var(--fs-xs); line-height: var(--lh-base); overflow-wrap: anywhere; }
.sidebar-db-text { display: grid; min-width: 0; }
.sidebar-db-size { color: var(--text-3); }
.sidebar-db-size:empty { display: none; }
.db-dot { flex: none; width: 8px; height: 8px; margin-top: 6px; border-radius: 50%; background: var(--text-3); }
/* data-tone is set by static/js/dbstatus.js on .sidebar-db */
.sidebar-db[data-tone="ok"] .db-dot { background: var(--green); }
.sidebar-db[data-tone="warn"], .sidebar-db[data-tone="warn"] .sidebar-db-size { color: var(--orange); }
.sidebar-db[data-tone="warn"] .db-dot { background: var(--orange); }
.sidebar-db[data-tone="error"], .sidebar-db[data-tone="error"] .sidebar-db-size { color: var(--red); }
.sidebar-db[data-tone="error"] .db-dot { background: var(--red); }
.nav-logout { font-size: var(--fs-xs); }

/* One report bar. Editing a date range opens a panel; it never displaces the report. */
.main-wrap { display: flex; flex: 1; flex-direction: column; min-width: 0; min-height: 100vh; margin-left: var(--sidebar-w); }
.topbar { position: sticky; top: 0; z-index: var(--z-topbar); display: grid; grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "title appearance"; align-items: center; gap: var(--sp-2) var(--sp-4); min-height: var(--bar-row-h); padding: var(--sp-2) var(--gutter); border-bottom: 1px solid var(--border); background: var(--bg-sidebar); }
.page-heading { grid-area: title; min-width: 0; font: 600 var(--fs-heading)/var(--lh-tight) var(--font-display); overflow-wrap: anywhere; }
.topbar-theme { grid-area: appearance; }
[data-theme-toggle].btn { border: 1px solid var(--border); }
[data-theme-toggle].btn[aria-pressed="true"] { border-color: var(--selected-border); background: var(--selected-bg); color: var(--selected-fg); }
.topbar-workspace, .topbar-extra, .topbar-before-period { grid-column: 1 / -1; min-width: 0; }
.topbar-before-period:empty, .topbar-extra:empty { display: none; }
.topbar-workspace { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--sp-2) var(--sp-4); }
.topbar-context { display: grid; flex: 1 1 15rem; gap: var(--sp-1); min-width: 0; }
.topbar-status { min-width: 0; color: var(--text-2); font-size: var(--fs-md); overflow-wrap: anywhere; }
.topbar-status [data-state="empty"] { color: var(--text-3); }
.topbar-status [data-state="stale"] { color: var(--orange); }
.topbar-result-actions, .topbar-run, .topbar-extra { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); min-width: 0; }
.topbar-result-actions:not(:has(> :not([hidden]))), .topbar-share:empty { display: none; }
.topbar-result-actions > .btn { border: 1px solid var(--border); white-space: normal; }
.topbar-share { min-width: 0; overflow-wrap: anywhere; }
.topbar-period { display: none; }
.is-period-open .topbar-period { position: absolute; top: calc(100% + 1px); right: var(--gutter); display: grid; gap: var(--sp-3); max-width: calc(100% - 2 * var(--gutter)); max-height: calc(100dvh - var(--topbar-h)); overflow: auto; padding: var(--sp-4); border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-card); box-shadow: var(--shadow-pop); }
.period-footer { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); flex-wrap: wrap; }
.topbar-dates { display: flex; align-items: center; gap: var(--sp-2); min-width: 0; }
.topbar #loadBtn { position: relative; }
/* Quiet stale marker: the inputs no longer match the loaded report (class set by ui.js). */
.topbar #loadBtn.is-stale::after { content: ''; position: absolute; top: -4px; right: -4px; width: 10px; height: 10px; border: 2px solid var(--bg-sidebar); border-radius: 50%; background: var(--orange); }
#stopBtn:disabled { display: none; }

/* Period inputs: one bordered group per bound ("С" / "По"). Accessible names live on the inputs. */
.tb-datetime-group { display: inline-flex; align-items: center; height: var(--control-h); min-width: 0; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--bg-body); }
.tb-datetime-group:hover { border-color: var(--text-3); }
.tb-datetime-group:focus-within { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.tb-prefix { padding-left: var(--sp-3); color: var(--text-3); font-size: var(--fs-xs); user-select: none; }
.tb-date-part { width: 128px; min-width: 0; height: 100%; padding: 0 var(--sp-2); border: 0; outline: 0; background: transparent; color: var(--text-1); font-size: var(--fs-sm); cursor: pointer; }
.tb-date-part::-webkit-calendar-picker-indicator { opacity: .75; cursor: pointer; }
.tb-group-sep { flex: none; width: 1px; height: 18px; background: var(--border); }
.tb-time-picker { display: inline-flex; align-items: center; padding: 0 var(--sp-1); }
.tb-date-part:focus-visible, .tb-time-part:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.tb-time-part { width: 30px; height: 32px; padding: 0; border: 0; outline: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text-1); font-size: var(--fs-sm); font-variant-numeric: tabular-nums; text-align: center; appearance: textfield; }
.tb-time-part::-webkit-outer-spin-button, .tb-time-part::-webkit-inner-spin-button { margin: 0; -webkit-appearance: none; }
.tb-time-part:hover { background: var(--bg-hover); }
.tb-time-part:focus { background: color-mix(in srgb, var(--accent) 18%, transparent); }
.tb-time-sep, .tb-dash { color: var(--text-3); font-size: var(--fs-sm); user-select: none; }
.topbar .segmented { padding: 1px; gap: 1px; }

.topbar-period-toggle { display: inline-flex; flex: 1 1 auto; align-items: center; gap: var(--sp-2); min-width: 0; min-height: var(--control-h); padding: 0 var(--sp-3); border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--bg-body); color: var(--text-1); font-size: var(--fs-sm); text-align: left; }
.topbar-period-toggle:hover { border-color: var(--text-3); }
.topbar-period-name { color: var(--text-2); font-size: var(--fs-xs); }
.topbar-period-value { flex: 1; min-width: 0; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

/* Local secondary actions; the mobile navigation owns its separate sidebar disclosure. */
.app-actions-menu { position: relative; }
.app-actions-menu > summary, .nav-toggle.btn { gap: var(--sp-2); border: 1px solid var(--border); list-style: none; }
.app-actions-menu > summary::-webkit-details-marker { display: none; }
.app-actions-menu[open] > summary, .nav-toggle[aria-expanded="true"] { background: var(--bg-hover); color: var(--text-1); }
.menu-chevron { flex: none; width: 6px; height: 6px; margin: 0 2px 3px; border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor; transform: rotate(45deg); transition: transform .12s ease; }
details[open] > summary .menu-chevron, [aria-expanded="true"] > .menu-chevron { margin-bottom: -2px; transform: rotate(-135deg); }
.app-menu-panel { position: absolute; top: calc(100% + var(--sp-2)); right: 0; z-index: var(--z-raised); padding: var(--sp-1); border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-card); box-shadow: var(--shadow-pop); }
.app-actions-panel { display: grid; gap: 2px; width: min(248px, calc(100vw - 2 * var(--gutter))); }
.app-actions-panel > .btn { justify-content: flex-start; width: 100%; text-align: left; white-space: normal; }
.app-menu-panel .app-menu-danger { color: var(--red); }
.app-menu-panel .app-menu-danger:hover { background: color-mix(in srgb, var(--red) 14%, transparent); color: var(--red); }

/* Status strip: error, notices, loading and progress share one slim sticky row under the bar. Hidden while empty. */
.status-stack { position: sticky; top: var(--topbar-h); z-index: var(--z-status); display: flex; flex-wrap: wrap; align-items: center; column-gap: var(--sp-4); padding: 0 var(--gutter); border-bottom: 1px solid var(--border); background: var(--bg-card); font-size: var(--fs-sm); }
.status-stack:not(:has(> :not(.hidden))) { display: none; }
.status-stack > * { padding: var(--sp-2) 0; }
.status-error { display: flex; flex: 1 1 100%; align-items: flex-start; gap: var(--sp-2); margin: 0 calc(-1 * var(--gutter)); padding: var(--sp-2) var(--gutter); background: color-mix(in srgb, var(--red) 18%, var(--bg-card)); box-shadow: inset 3px 0 var(--red); color: var(--text-1); line-height: var(--lh-base); overflow-wrap: anywhere; }
.status-error::before { content: '!'; content: '!' / ''; display: grid; flex: none; place-items: center; width: 18px; height: 18px; margin-top: 1px; border-radius: 50%; background: var(--red); color: var(--bg-body); font: 700 var(--fs-xs)/1 var(--font-ui); }
.status-note { color: var(--text-2); }
.status-note-warn { color: var(--orange); }
.loading { display: flex; align-items: center; gap: var(--sp-2); color: var(--text-2); }
.spinner { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
/* While progress is visible the spinner stays and the words move to the progress row; the live region keeps its text. */
.status-stack:has(#progressContainer:not(.hidden)) #loading > span { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
.progress-card { display: flex; flex: 1 1 280px; align-items: center; gap: var(--sp-3); min-width: 0; }
.progress-title { flex: none; font-weight: 600; white-space: nowrap; }
.progress-value { min-width: 0; overflow: hidden; color: var(--text-2); font-variant-numeric: tabular-nums; text-overflow: ellipsis; white-space: nowrap; }
.progress-track { position: absolute; inset: auto 0 0; height: 3px; background: var(--bg-inset); }
.progress-track-fill { width: 0; height: 100%; background: var(--accent); transition: width .3s ease; }

/* Controls */
.btn { display: inline-flex; align-items: center; justify-content: center; min-height: var(--control-h); padding: 0 var(--sp-3); border: 0; border-radius: var(--radius-md); font-size: var(--fs-sm); font-weight: 600; line-height: var(--lh-tight); text-decoration: none; white-space: nowrap; cursor: pointer; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--bg-hover); color: var(--text-1); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.muted { color: var(--text-2); }

/* Page hierarchy and layout */
.main-content { flex: 1; width: 100%; min-width: 0; padding: var(--sp-6) var(--gutter) var(--sp-7); }
.content-stack { display: grid; gap: var(--sp-5); min-width: 0; }
.page-intro { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--sp-3) var(--sp-6); }
.page-intro h2 { margin-bottom: var(--sp-1); font: 600 var(--fs-lg)/var(--lh-tight) var(--font-display); }
.page-intro p { max-width: 72ch; color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-base); }
.workspace-panel { min-width: 0; padding: var(--sp-5); border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-card); }
.section-heading { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--sp-3); }
.surface-caption { color: var(--text-2); font-size: var(--fs-xs); line-height: var(--lh-prose); }
.empty-state { padding: var(--sp-7) var(--sp-5); color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-prose); text-align: center; }
/* Report pages: body[data-report-state] is set by ui.js. No attribute = the report layout, so a returning user never sees a flash. */
.report-empty { display: none; }
body[data-report-state="empty"] .report-empty { display: block; }
body[data-report-state="empty"] .report-loaded { display: none; }
.report-empty .section-title { margin-bottom: var(--sp-2); color: var(--text-1); }
.report-empty p { max-width: 60ch; margin-inline: auto; }
.report-empty .btn { margin-top: var(--sp-4); }
.card { min-width: 0; padding: var(--sp-4); border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-card); }
.metric-band { display: grid; grid-template-columns: repeat(var(--metric-columns, 4), minmax(0, 1fr)); min-width: 0; padding: var(--sp-5) 0; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-card); }
.metric-band > .card { padding: 0 var(--sp-5); border: 0; border-left: 1px solid var(--border); border-radius: 0; background: none; }
.metric-band > .card:first-child { border-left: 0; }
.stat-card { padding: var(--sp-4) var(--sp-5); }
.card-title { margin-bottom: var(--sp-2); color: var(--text-2); font-size: var(--fs-xs); font-weight: 500; }
.stat-value { font: 600 var(--fs-num-md)/1.2 var(--font-display); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stat-label { margin-top: var(--sp-2); color: var(--text-3); font-size: var(--fs-xs); line-height: var(--lh-prose); }
.section-title { margin-bottom: var(--sp-1); font: 600 var(--fs-lg)/var(--lh-tight) var(--font-display); }
.section-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--sp-3); margin-bottom: var(--sp-1); }
.no-margin { margin-bottom: 0; }

/* Tables and charts */
.table-container, .table-responsive { min-width: 0; max-width: 100%; overflow: auto; }
.table-container { margin-top: var(--sp-3); }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 10px var(--sp-3); text-align: left; }
:is(th, td):is(.is-num, .num, .num-col) { text-align: right; }
th { border-bottom: 1px solid var(--border); background: transparent; color: var(--text-2); font-size: var(--fs-xs); font-weight: 500; }
td { border-bottom: 1px solid color-mix(in srgb, var(--border) 65%, transparent); color: var(--text-1); }
tr:hover td { background: var(--bg-hover); }
.table-empty { padding: 28px var(--sp-4); color: var(--text-3); text-align: center; }
.sticky-table { max-height: 520px; overflow-y: auto; }
.sticky-table thead { position: sticky; top: 0; z-index: var(--z-raised); }
.sticky-table th { background: var(--bg-card); }
th.sortable { position: relative; padding-right: var(--sp-6); cursor: pointer; }
th.sortable::after { content: '↕'; position: absolute; top: 50%; right: 7px; color: var(--text-3); font-size: var(--fs-xs); transform: translateY(-50%); }
th.sortable.sorted-active { color: var(--accent); }
th.sortable.sort-asc::after { content: '↑'; color: var(--accent); }
th.sortable.sort-desc::after { content: '↓'; color: var(--accent); }
.badge, .badge-green, .badge-blue, .badge-orange, .badge-red { display: inline-flex; align-items: center; padding: 3px var(--sp-2); border-radius: var(--radius-sm); background: var(--bg-inset); color: var(--text-2); font-size: var(--fs-xs); font-weight: 500; }
.badge-green { color: var(--green); background: color-mix(in srgb, var(--green) 14%, var(--bg-inset)); }
.badge-blue { color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--bg-inset)); }
.badge-orange { color: var(--orange); background: color-mix(in srgb, var(--orange) 14%, var(--bg-inset)); }
.badge-red { color: var(--red); background: color-mix(in srgb, var(--red) 14%, var(--bg-inset)); }
.chart-container { position: relative; height: 280px; min-width: 0; margin-top: var(--sp-3); }
.chart-container.small { height: 180px; }
.chart-container.is-unavailable { height: auto; }
.toggle-btn { min-height: 32px; padding: 0 var(--sp-3); border: 1px solid var(--border); border-radius: var(--radius-md); background: transparent; color: var(--text-2); font-size: var(--fs-xs); cursor: pointer; }
.toggle-btn:hover { background: var(--bg-hover); color: var(--text-1); }
.toggle-btn.active { border-color: var(--selected-border); background: var(--selected-bg); color: var(--selected-fg); }

/* Shared components: pages compose these instead of redefining them. */
.num { font-family: var(--font-display); font-variant-numeric: tabular-nums; }

/* Exactly one active option: period presets, scopes, view switches. Children: button, a, or label wrapping a radio. */
.segmented { display: inline-flex; gap: 2px; padding: 3px; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--bg-body); min-width: 0; max-width: 100%; }
.segmented > button, .segmented > a, .segmented > label { position: relative; display: inline-flex; align-items: center; justify-content: center; min-height: 32px; padding: 0 var(--sp-3); border: 0; border-radius: var(--radius-sm); background: none; color: var(--text-2); font-size: var(--fs-xs); font-weight: 500; text-decoration: none; white-space: nowrap; cursor: pointer; }
.segmented > :hover { background: var(--bg-hover); color: var(--text-1); }
.segmented > [aria-pressed="true"], .segmented > [aria-selected="true"], .segmented > [aria-current="true"], .segmented > [aria-current="page"], .segmented > .active, .segmented > :has(input:checked) { background: var(--selected-bg); color: var(--selected-fg); }
.segmented > :has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 1px; }
.segmented input[type="radio"], .segmented input[type="checkbox"] { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }

/* Independent on/off filter option. */
.chip { position: relative; display: inline-flex; align-items: center; gap: var(--sp-2); min-height: 32px; padding: 0 var(--sp-3); border: 1px solid var(--border); border-radius: var(--radius-md); background: transparent; color: var(--text-2); font-size: var(--fs-xs); font-weight: 500; white-space: nowrap; cursor: pointer; }
.chip:hover { background: var(--bg-hover); color: var(--text-1); }
.chip[aria-pressed="true"], .chip.active, .chip:has(input:checked) { background: var(--selected-bg); border-color: var(--selected-border); color: var(--selected-fg); }
.chip > input[type="checkbox"], .chip > input[type="radio"] { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.chip:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 1px; }
.chip-count { color: var(--text-3); font-variant-numeric: tabular-nums; }
.chip[aria-pressed="true"] .chip-count, .chip.active .chip-count { color: inherit; opacity: .8; }

/* <details class="disclosure"><summary>Title</summary>content</details> */
.disclosure > summary { display: flex; align-items: center; gap: var(--sp-2); min-height: 32px; list-style: none; color: var(--text-2); font-size: var(--fs-sm); font-weight: 600; cursor: pointer; }
.disclosure > summary::-webkit-details-marker { display: none; }
.disclosure > summary::before { content: ''; flex: 0 0 auto; width: 6px; height: 6px; margin: 0 2px 2px 3px; border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor; transform: rotate(-45deg); transition: transform .12s ease; }
.disclosure[open] > summary::before { transform: rotate(45deg); }
.disclosure > summary:hover { color: var(--text-1); }

/* Label/value pairs: <dl class="metric-list" style="--cols: 3"><div><dt>Label</dt><dd>Value</dd></div></dl> */
.metric-list { display: grid; grid-template-columns: repeat(var(--cols, 4), minmax(0, 1fr)); gap: var(--sp-4) var(--sp-5); margin: 0; }
.metric-list > div { min-width: 0; }
.metric-list dt { color: var(--text-2); font-size: var(--fs-xs); line-height: 1.4; }
.metric-list dd { margin: var(--sp-1) 0 0; font: 600 var(--fs-num-sm)/var(--lh-tight) var(--font-display); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.metric-list .is-action dd { color: var(--orange); }
.metric-list dd.metric-hint { margin-top: 2px; color: var(--text-3); font: 400 var(--fs-xs)/var(--lh-base) var(--font-ui); }

/* Form controls */
.field { display: grid; gap: var(--sp-1); min-width: 0; }
.field > label, .field-label { color: var(--text-2); font-size: var(--fs-xs); }
.input, .select { width: 100%; min-height: var(--control-h); padding: 6px 10px; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--bg-body); color: var(--text-1); font-size: var(--fs-sm); }
.input:hover, .select:hover { border-color: var(--text-3); }
.input:focus-visible, .select:focus-visible { border-color: var(--accent); outline: 2px solid var(--accent); outline-offset: 0; }
.input::placeholder { color: var(--text-3); }

/* Native <dialog>: centered modal, or add .dialog-drawer for a right-hand panel. Keep the dialog's own padding at 0 (see static/js/dialog.js). */
dialog.dialog { width: min(960px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); margin: auto; padding: 0; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-card); color: var(--text-1); box-shadow: var(--shadow-pop); overflow: hidden; }
dialog.dialog[open] { display: flex; flex-direction: column; }
dialog.dialog::backdrop { background: var(--overlay); }
dialog.dialog-drawer { width: min(560px, 100vw); height: 100dvh; max-width: none; max-height: none; margin: 0 0 0 auto; border-width: 0 0 0 1px; border-radius: 0; }
.dialog-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--sp-3); padding: var(--sp-4) var(--sp-5); border-bottom: 1px solid var(--border); }
.dialog-title { font: 600 var(--fs-lg)/var(--lh-tight) var(--font-display); }
.dialog-body { flex: 1; min-height: 0; padding: var(--sp-5); overflow: auto; }
.dialog-foot { display: flex; justify-content: flex-end; gap: var(--sp-2); padding: var(--sp-3) var(--sp-5); border-top: 1px solid var(--border); }
dialog.dialog-confirm { width: min(440px, calc(100vw - 32px)); }
dialog.dialog-confirm > form { display: flex; flex-direction: column; min-height: 0; }
.dialog-confirm .dialog-body p { color: var(--text-2); line-height: var(--lh-prose); white-space: pre-line; }
.btn-danger { background: color-mix(in srgb, var(--red) 20%, transparent); color: var(--red); }
.btn-danger:hover { background: color-mix(in srgb, var(--red) 30%, transparent); }
html:has(dialog:modal) { overflow: hidden; }

/* Toasts: static/js/toast.js */
.toast-region { position: fixed; inset: auto var(--sp-4) var(--sp-4) auto; z-index: var(--z-toast); display: grid; gap: var(--sp-2); width: min(360px, calc(100vw - 32px)); margin: 0; padding: 0; border: 0; background: none; color: inherit; overflow: visible; pointer-events: none; }
.toast { pointer-events: none; padding: 10px 14px; border: 1px solid var(--border); border-inline-start-color: var(--accent); border-radius: var(--radius-md); background: var(--bg-card); color: var(--text-1); font-size: var(--fs-sm); line-height: 1.4; overflow-wrap: anywhere; box-shadow: var(--shadow-pop); animation: toast-in .16s ease-out; }
.toast-dismiss { display: block; min-height: 44px; border: 0; background: none; color: var(--accent); text-decoration: underline; font: inherit; cursor: pointer; pointer-events: auto; }
.toast-success { border-left-color: var(--green); }
.toast-warning { border-left-color: var(--orange); }
.toast-error { border-left-color: var(--red); }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }

/* Leave room for the 220px sidebar at narrower desktop sizes. */
@media (max-width: 1180px) {
    :root { --gutter: var(--sp-5); }
    .tb-date-part { width: 112px; }
    .tb-time-part { width: 26px; }
    .topbar .segmented > button { padding: 0 10px; }
}

@media (max-width: 980px) {
    .main-wrap { margin-left: 0; }
    .topbar { grid-template-columns: auto minmax(0, 1fr) auto; grid-template-areas: "nav title appearance"; }
    .nav-toggle.btn { display: inline-flex; grid-area: nav; }
    .nav-toggle[aria-expanded="false"] + .sidebar { display: none; }
    .sidebar { position: absolute; inset: calc(var(--bar-row-h) + var(--sp-1)) auto auto var(--gutter); width: min(296px, calc(100% - 2 * var(--gutter))); max-height: calc(100dvh - var(--bar-row-h) - var(--sp-2)); overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-pop); }
    .sidebar-nav { flex: none; overflow-y: visible; }
}

@media (max-width: 560px) {
    .topbar { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "nav appearance" "title title"; }
    .nav-toggle.btn { justify-self: start; }
}

/* On phones, keep the inputs and the result actions legible instead of compressing the toolbar. */
@media (max-width: 760px) {
    :root { --gutter: var(--sp-4); --control-h: 44px; --bar-row-h: 60px; }
    .topbar-workspace { display: grid; grid-template-columns: minmax(0, 1fr); }
    .topbar-period-toggle { flex-basis: 100%; }
    .nav-link, .toggle-btn, .chip, .disclosure > summary,
    .segmented > button, .segmented > a, .segmented > label { min-height: var(--control-h); }
    .content-stack { gap: var(--sp-4); }
    .workspace-panel { padding: var(--sp-4); }
    .metric-band { grid-template-columns: repeat(2, minmax(0, 1fr)); padding: 0; }
    .metric-band > .card { padding: var(--sp-4); border-left: 0; border-bottom: 1px solid var(--border); }
    .metric-band > .card:nth-child(even) { border-left: 1px solid var(--border); }
    .metric-band > .card:nth-last-child(-n+2) { border-bottom: 0; }
    th, td { padding: 10px; }
    .chart-container { height: 250px; }

    .topbar .app-actions-menu { position: static; }
    .topbar .app-menu-panel { right: var(--gutter); }
    .is-period-open .topbar-period { inset: 100% var(--gutter) auto; padding: var(--sp-3); }
    .topbar-dates { display: grid; }
    .tb-dash { display: none; }
    .tb-datetime-group { width: 100%; }
    .tb-date-part { flex: 1; width: auto; }
    .tb-time-part { width: var(--control-h); height: calc(var(--control-h) - 2px); }
    .topbar .segmented { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); }
    .topbar .segmented > button { padding-inline: var(--sp-1); }
}

/* In short viewports the full toolbar can occupy most of the screen. Let it scroll with the
   document and give the editor its own viewport-bounded scrolling area. */
@media (max-height: 480px) {
    :root { --topbar-h: 0px; --status-h: 0px; }
    .topbar, .status-stack { position: relative; top: auto; }
    .is-period-open .topbar-period {
        position: fixed;
        inset: var(--sp-2) 0 auto;
        width: min(42rem, calc(100% - 2 * var(--gutter)));
        max-height: calc(100dvh - 2 * var(--sp-2));
        margin-inline: auto;
    }
}
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
    .spinner { animation: none; }
}
