/* days design system — tokens, themes, base, layout, components.
   Themes: light (default), dark, contrast; "auto" follows the device.
   Set explicitly with <html data-theme="light|dark|contrast">. */

/* ---------- Tokens: shared ---------- */
:root {
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;

  /* Fluid type scale: 360px → 1440px */
  --step--1: clamp(0.875rem, 0.85rem + 0.11vw, 0.9375rem);
  --step-0: clamp(1rem, 0.96rem + 0.19vw, 1.125rem);
  --step-1: clamp(1.2rem, 1.12rem + 0.37vw, 1.45rem);
  --step-2: clamp(1.44rem, 1.3rem + 0.63vw, 1.875rem);
  --step-3: clamp(1.73rem, 1.5rem + 1.02vw, 2.4rem);
  --step-4: clamp(2.07rem, 1.72rem + 1.56vw, 3.1rem);
  --leading: 1.6;
  --leading-tight: 1.2;

  /* Fluid spacing */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: clamp(1.25rem, 1.1rem + 0.6vw, 1.5rem);
  --space-6: clamp(1.5rem, 1.25rem + 1.1vw, 2.25rem);
  --space-7: clamp(2rem, 1.5rem + 2.2vw, 3.5rem);
  --space-8: clamp(3rem, 2rem + 4.4vw, 6rem);

  --radius-1: 6px;
  --radius-2: 10px;
  --radius-3: 16px;
  --radius-full: 999px;

  --container: 72rem;
  --container-narrow: 42rem;
  --tap: 44px;
  --border-width: 1px;
  --focus-width: 3px;
  --duration: 160ms;
  --ease: cubic-bezier(0.2, 0, 0, 1);
}

/* ---------- Theme: light (default) ---------- */
:root,
:root[data-theme="light"] {
  color-scheme: light;
  /* Chart series (validated categorical palette, fixed order; light steps) */
  --chart-1: #2a78d6; --chart-2: #eb6834; --chart-3: #1baf7a; --chart-4: #eda100;
  --chart-5: #e87ba4; --chart-6: #008300; --chart-7: #6250d6; --chart-8: #e34948; --chart-other: #7a8393;
  /* Measurement drawing (zamer): work lines on the plan, ≥ 4.5:1 on bg (7:1 contrast), ≥ 3:1 on surface */
  --plan-karniz: #7a3fd1; --plan-niche: #0b7a6b; --plan-zakl: #c43350; --plan-led: #237a30;
  --plan-track: #8f5b00; --plan-drill: #80653f; --plan-grid: #e2e5ea;
  --bg: #ffffff;
  --surface: #f5f6f8;
  --surface-2: #eaedf1;
  --text: #15171c;
  --muted: #535b69;
  /* Placeholder: readable (≥ 4.5:1 on the field) but dimmer than --text, so it never passes for a typed value */
  --placeholder: #6b7280;
  --border: #d6dae1;
  --border-strong: #7a8393;
  --accent: #2346d4;
  --accent-hover: #1a37aa;
  --accent-text: #ffffff;
  --accent-soft: #e8edfd;
  --focus: #2346d4;
  --success: #11703a;
  --success-bg: #e4f3ea;
  --warning: #855400;
  --warning-bg: #fbf0d9;
  --danger: #b8232a;
  --danger-bg: #fbe7e8;
  /* Toast stripes: the toast is inverted (dark on light, light on dark), so the opposite theme's steps */
  --toast-success: #5ccb8a; --toast-warning: #e9b553; --toast-danger: #ff8a8e;
  --shadow: 0 1px 2px rgb(16 24 40 / 0.06), 0 4px 16px rgb(16 24 40 / 0.08);
  --backdrop: rgb(16 18 24 / 0.5);
}

/* ---------- Theme: dark ---------- */
:root[data-theme="dark"] {
  color-scheme: dark;
  /* Chart series: dark steps of the same palette (dark and contrast surfaces) */
  --chart-1: #3987e5; --chart-2: #d95926; --chart-3: #199e70; --chart-4: #c98500;
  --chart-5: #d55181; --chart-6: #008300; --chart-7: #9085e9; --chart-8: #e66767; --chart-other: #707a8c;
  /* Measurement drawing (zamer): work lines on the plan, ≥ 4.5:1 on bg (7:1 contrast), ≥ 3:1 on surface */
  --plan-karniz: #b794f6; --plan-niche: #3cc9b5; --plan-zakl: #ff7a8f; --plan-led: #5fd16e;
  --plan-track: #f2b13a; --plan-drill: #c9a97d; --plan-grid: #24282f;
  --bg: #0e1014;
  --surface: #171a21;
  --surface-2: #20242d;
  --text: #e9ebef;
  --muted: #a4abb8;
  --placeholder: #868e9c;
  --border: #2c313c;
  --border-strong: #707a8c;
  --accent: #8ea5ff;
  --accent-hover: #adbfff;
  --accent-text: #0b1024;
  --accent-soft: #1c2444;
  --focus: #8ea5ff;
  --success: #5ccb8a;
  --success-bg: #12301f;
  --warning: #e9b553;
  --warning-bg: #35290c;
  --danger: #ff8a8e;
  --danger-bg: #3a1618;
  --toast-success: #11703a; --toast-warning: #855400; --toast-danger: #b8232a;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 6px 20px rgb(0 0 0 / 0.35);
  --backdrop: rgb(0 0 0 / 0.65);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    /* Chart series: dark steps of the same palette (dark and contrast surfaces) */
    --chart-1: #3987e5; --chart-2: #d95926; --chart-3: #199e70; --chart-4: #c98500;
    --chart-5: #d55181; --chart-6: #008300; --chart-7: #9085e9; --chart-8: #e66767; --chart-other: #707a8c;
    /* Measurement drawing (zamer): work lines on the plan, ≥ 4.5:1 on bg (7:1 contrast), ≥ 3:1 on surface */
    --plan-karniz: #b794f6; --plan-niche: #3cc9b5; --plan-zakl: #ff7a8f; --plan-led: #5fd16e;
    --plan-track: #f2b13a; --plan-drill: #c9a97d; --plan-grid: #24282f;
    --bg: #0e1014;
    --surface: #171a21;
    --surface-2: #20242d;
    --text: #e9ebef;
    --muted: #a4abb8;
    --placeholder: #868e9c;
    --border: #2c313c;
    --border-strong: #707a8c;
    --accent: #8ea5ff;
    --accent-hover: #adbfff;
    --accent-text: #0b1024;
    --accent-soft: #1c2444;
    --focus: #8ea5ff;
    --success: #5ccb8a;
    --success-bg: #12301f;
    --warning: #e9b553;
    --warning-bg: #35290c;
    --danger: #ff8a8e;
    --danger-bg: #3a1618;
    --toast-success: #11703a; --toast-warning: #855400; --toast-danger: #b8232a;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 6px 20px rgb(0 0 0 / 0.35);
    --backdrop: rgb(0 0 0 / 0.65);
  }
}

/* ---------- Theme: contrast (WCAG AAA, 7:1) ---------- */
:root[data-theme="contrast"] {
  color-scheme: dark;
  /* Chart series: dark steps of the same palette (dark and contrast surfaces) */
  --chart-1: #3987e5; --chart-2: #d95926; --chart-3: #199e70; --chart-4: #c98500;
  --chart-5: #d55181; --chart-6: #008300; --chart-7: #9085e9; --chart-8: #e66767; --chart-other: #707a8c;
  /* Measurement drawing (zamer): work lines on the plan, ≥ 4.5:1 on bg (7:1 contrast), ≥ 3:1 on surface */
  --plan-karniz: #d9b8ff; --plan-niche: #5ff5e0; --plan-zakl: #ff9eb0; --plan-led: #7dff8a;
  --plan-track: #ffe14d; --plan-drill: #e8c99a; --plan-grid: #333333;
  --bg: #000000;
  --surface: #000000;
  --surface-2: #141414;
  --text: #ffffff;
  --muted: #e6e6e6;
  --placeholder: #b3b3b3;
  --border: #ffffff;
  --border-strong: #ffffff;
  --accent: #ffe14d;
  --accent-hover: #ffffff;
  --accent-text: #000000;
  --accent-soft: #000000;
  --focus: #ffe14d;
  --success: #6dffa8;
  --success-bg: #000000;
  --warning: #ffe14d;
  --warning-bg: #000000;
  --danger: #ff9e9e;
  --danger-bg: #000000;
  --toast-success: var(--success); --toast-warning: var(--warning); --toast-danger: var(--danger);
  --shadow: none;
  --backdrop: rgb(0 0 0 / 0.85);
  --border-width: 2px;
  --focus-width: 4px;
}
@media (prefers-contrast: more) {
  :root:not([data-theme]) {
    color-scheme: dark;
    /* Chart series: dark steps of the same palette (dark and contrast surfaces) */
    --chart-1: #3987e5; --chart-2: #d95926; --chart-3: #199e70; --chart-4: #c98500;
    --chart-5: #d55181; --chart-6: #008300; --chart-7: #9085e9; --chart-8: #e66767; --chart-other: #707a8c;
    /* Measurement drawing (zamer): work lines on the plan, ≥ 4.5:1 on bg (7:1 contrast), ≥ 3:1 on surface */
    --plan-karniz: #d9b8ff; --plan-niche: #5ff5e0; --plan-zakl: #ff9eb0; --plan-led: #7dff8a;
    --plan-track: #ffe14d; --plan-drill: #e8c99a; --plan-grid: #333333;
    --bg: #000000;
    --surface: #000000;
    --surface-2: #141414;
    --text: #ffffff;
    --muted: #e6e6e6;
    --placeholder: #b3b3b3;
    --border: #ffffff;
    --border-strong: #ffffff;
    --accent: #ffe14d;
    --accent-hover: #ffffff;
    --accent-text: #000000;
    --accent-soft: #000000;
    --focus: #ffe14d;
    --success: #6dffa8;
    --success-bg: #000000;
    --warning: #ffe14d;
    --warning-bg: #000000;
    --danger: #ff9e9e;
    --danger-bg: #000000;
    --toast-success: var(--success); --toast-warning: var(--warning); --toast-danger: var(--danger);
    --shadow: none;
    --backdrop: rgb(0 0 0 / 0.85);
    --border-width: 2px;
    --focus-width: 4px;
  }
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
/* Components set display:flex etc.; keep the hidden attribute authoritative */
[hidden]:not([hidden="until-found"]) { display: none !important; }
* { margin: 0; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--step-0);
  line-height: var(--leading);
  -webkit-font-smoothing: antialiased;
  overflow-wrap: break-word;
}
main { flex: 1; }
img, picture, svg, video, canvas { display: block; max-width: 100%; height: auto; }
input, button, textarea, select { font: inherit; color: inherit; }
h1, h2, h3, h4 { line-height: var(--leading-tight); text-wrap: balance; font-weight: 700; }
h1 { font-size: var(--step-4); letter-spacing: -0.02em; }
h2 { font-size: var(--step-3); letter-spacing: -0.01em; }
h3 { font-size: var(--step-2); }
h4 { font-size: var(--step-1); }
p { text-wrap: pretty; }
/* Text selection in brand colours (accent-text on accent is a checked pair in every theme) */
::selection { background: var(--accent); color: var(--accent-text); }
/* On accent-coloured elements the accent selection would vanish: invert instead */
:is(.btn:not(.btn-secondary, .btn-ghost), .btn:not(.btn-secondary, .btn-ghost) *)::selection { background: var(--text); color: var(--bg); }
small, .text-small { font-size: var(--step--1); }
.muted { color: var(--muted); }
code, kbd, pre { font-family: var(--font-mono); font-size: 0.9em; }
code { background: var(--surface-2); padding: 0.1em 0.35em; border-radius: var(--radius-1); }
pre { background: var(--surface); border: var(--border-width) solid var(--border); border-radius: var(--radius-2); padding: var(--space-4); overflow-x: auto; }
pre code { background: none; padding: 0; }
hr { border: 0; border-top: var(--border-width) solid var(--border); }

a { color: var(--accent); text-underline-offset: 0.18em; text-decoration-thickness: 1px; }
a:hover { color: var(--accent-hover); text-decoration-thickness: 2px; }

:focus-visible {
  outline: var(--focus-width) solid var(--focus);
  outline-offset: 2px;
  border-radius: var(--radius-1);
}

.skip-link {
  position: absolute; left: var(--space-4); top: -100px; z-index: 100;
  background: var(--accent); color: var(--accent-text); padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-1);
}
.skip-link:focus { top: var(--space-4); }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important; animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important; scroll-behavior: auto !important;
  }
}

/* ---------- Layout ---------- */
.container { width: min(100% - 2 * var(--space-4), var(--container)); margin-inline: auto; }
.container-narrow { width: min(100% - 2 * var(--space-4), var(--container-narrow)); margin-inline: auto; }
.section { padding-block: var(--space-7); }
.stack { display: flex; flex-direction: column; gap: var(--stack-gap, var(--space-4)); }
.stack-lg { --stack-gap: var(--space-6); }
.stack-sm { --stack-gap: var(--space-2); }
.cluster { display: flex; flex-wrap: wrap; align-items: center; gap: var(--cluster-gap, var(--space-3)); }
.cluster-between { justify-content: space-between; }
/* Auto-fit grid: columns collapse by available width, no breakpoints needed */
.grid { display: grid; gap: var(--grid-gap, var(--space-5)); grid-template-columns: repeat(auto-fit, minmax(min(100%, var(--grid-min, 16rem)), 1fr)); }
.grid-sm { --grid-min: 11rem; }
.grid-lg { --grid-min: 22rem; }
/* Sidebar layout: stacks on narrow screens */
.with-sidebar { display: flex; flex-wrap: wrap; gap: var(--space-6); }
.with-sidebar > :first-child { flex-basis: 16rem; flex-grow: 1; }
.with-sidebar > :last-child { flex-basis: 0; flex-grow: 999; min-width: min(100%, 55%); }

/* ---------- Header / nav ---------- */
.site-header {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: var(--border-width) solid var(--border);
}
.site-header .container { display: flex; align-items: center; gap: var(--space-4); min-height: 64px; }
.brand { font-weight: 800; font-size: var(--step-1); color: var(--text); text-decoration: none; letter-spacing: -0.02em; }
.brand:hover { color: var(--text); }
.nav { display: flex; align-items: center; gap: var(--space-1); margin-left: auto; }
.nav a {
  display: inline-flex; align-items: center; min-height: var(--tap); padding-inline: var(--space-3);
  color: var(--text); text-decoration: none; border-radius: var(--radius-1);
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a[aria-current="page"] { color: var(--accent); font-weight: 600; }

/* Mobile: nav collapses behind .nav-toggle (button aria-controls="site-nav", handled by days.js) */
.site-header .container { position: relative; }
.site-header :is(.nav-toggle, .theme-switch) { flex: none; }
.site-header .nav-toggle { display: none; }
@media (max-width: 767.98px) {
  .site-header .nav-toggle { display: inline-flex; margin-left: auto; }
  /* One row on phones: a long brand is cut with an ellipsis, the buttons keep their 44 px */
  .site-header .brand { display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: var(--tap); }
  .nav:not(.is-open) { display: none; }
  .nav.is-open {
    /* right: 0 — the dropdown ends at the header content edge, under the menu button */
    position: absolute; right: 0; top: calc(100% + var(--space-2)); min-width: 14rem;
    flex-direction: column; align-items: stretch; padding: var(--space-2);
    background: var(--bg); border: var(--border-width) solid var(--border); border-radius: var(--radius-2); box-shadow: var(--shadow);
  }
  /* A form in the menu (e.g. «Выйти»): its button lines up with the links — full width, text left */
  .nav.is-open form { display: flex; flex-direction: column; }
  .nav.is-open form .btn {
    width: 100%; justify-content: flex-start; padding-inline: calc(var(--space-3) - var(--border-width));
    border-radius: var(--radius-1); font-weight: inherit; line-height: inherit;
  }
}

.site-footer { border-top: var(--border-width) solid var(--border); padding-block: var(--space-6); color: var(--muted); font-size: var(--step--1); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--tap); padding: 0 var(--space-5);
  border: var(--border-width) solid transparent; border-radius: var(--radius-2);
  background: var(--accent); color: var(--accent-text);
  font-weight: 600; line-height: 1; text-decoration: none; cursor: pointer; white-space: nowrap;
  transition: background var(--duration) var(--ease), border-color var(--duration) var(--ease), transform var(--duration) var(--ease);
}
.btn:hover { background: var(--accent-hover); color: var(--accent-text); text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn-secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-ghost { background: transparent; color: var(--text); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--danger); color: var(--bg); }
.btn-danger:hover { background: var(--danger); color: var(--bg); filter: brightness(1.1); }
.btn-sm { min-height: 36px; padding-inline: var(--space-3); font-size: var(--step--1); }
/* Compact controls: the visual stays small, the hit area is at least --tap (44 px) square */
:is(.btn-sm, .theme-switch button, .toast-close) { position: relative; }
:is(.btn-sm, .theme-switch button, .toast-close)::after {
  content: ""; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: max(100%, var(--tap)); height: max(100%, var(--tap));
}
.btn-lg { min-height: 52px; padding-inline: var(--space-6); font-size: var(--step-1); }
.btn-block { width: 100%; }
.btn-icon { padding: 0; width: var(--tap); }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: var(--space-2); }
.field > label, .label { font-weight: 600; font-size: var(--step--1); }
.hint { color: var(--muted); font-size: var(--step--1); }
.error-text, .field-error { color: var(--danger); font-size: var(--step--1); font-weight: 600; }
/* One height in a row: .input, .select (date/time too) and .btn are --tap high — fixed leading and equal
   block padding keep the box at min-height whatever the font step */
.input, .select, .textarea {
  width: 100%; min-height: var(--tap); padding: var(--space-2) var(--space-3); line-height: var(--leading-tight);
  font-size: max(16px, 1em); /* no iOS zoom */
  background: var(--bg); color: var(--text);
  border: var(--border-width) solid var(--border-strong); border-radius: var(--radius-2);
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}
.textarea { min-height: 7rem; resize: vertical; line-height: var(--leading); }
/* WebKit draws a native single-line select 3–5 px taller than min-height: pin it to --tap */
.select:not([multiple], [size]) { height: var(--tap); }
/* iOS Safari: native date/time fields ignore width:100% and stick out of narrow containers */
.input:is([type="date"], [type="datetime-local"], [type="time"], [type="month"]) {
  display: block; min-width: 0; max-width: 100%; -webkit-appearance: none; appearance: none;
}
.input::-webkit-date-and-time-value { text-align: left; }
/* WebKit/Blink date parts and the picker icon add their own block padding (48 px instead of 44) */
.input::-webkit-datetime-edit, .input::-webkit-datetime-edit-fields-wrapper { padding-block: 0; }
.input::-webkit-calendar-picker-indicator { margin-block: 0; padding-block: 0; }
.field { min-width: 0; }
.input::placeholder, .textarea::placeholder { color: var(--placeholder); opacity: 1; }
.input:focus-visible, .select:focus-visible, .textarea:focus-visible {
  /* Transparent outline: invisible here, but Windows High Contrast (forced colours) drops shadows and paints it */
  outline: var(--focus-width) solid transparent; outline-offset: 0;
  border-color: var(--focus); box-shadow: 0 0 0 var(--focus-width) color-mix(in srgb, var(--focus) 35%, transparent); }
.input[aria-invalid="true"], .textarea[aria-invalid="true"], .select[aria-invalid="true"] { border-color: var(--danger); }
.check {
  /* flex-start + padding: one line is centred in --tap, a wrapped label keeps the box on its first line */
  display: inline-flex; align-items: flex-start; gap: var(--space-3); min-height: var(--tap); cursor: pointer;
  padding-block: calc((var(--tap) - var(--leading) * 1em) / 2);
}
.check input {
  flex-shrink: 0; width: 1.25rem; height: 1.25rem; accent-color: var(--accent);
  margin: calc((var(--leading) * 1em - 1.25rem) / 2) 0 0; /* fallback without lh */
  margin: calc((1lh - 1.25rem) / 2) 0 0;
}
.input-group { display: flex; gap: var(--space-2); flex-wrap: wrap; }
.input-group > .input { flex: 1 1 12rem; }

/* ---------- Cards, badges, alerts ---------- */
.card {
  background: var(--surface); border: var(--border-width) solid var(--border);
  border-radius: var(--radius-3); padding: var(--space-5);
  display: flex; flex-direction: column; gap: var(--space-3);
}
.card-elevated { background: var(--bg); box-shadow: var(--shadow); }
a.card { color: inherit; text-decoration: none; transition: border-color var(--duration) var(--ease), transform var(--duration) var(--ease); }
a.card:hover { border-color: var(--border-strong); transform: translateY(-2px); }
/* Last row pinned to the bottom (buttons, links): cards of one grid row end on one line */
.card:has(> .card-foot) { display: flex; flex-direction: column; }
.card-foot { margin-top: auto; }

/* ---------- Disclosure (<details class="disclosure">) ----------
   Visible chevron ▸ closed / ▾ open, drawn with ::before — works with a flex summary too */
.disclosure > summary {
  display: flex; align-items: center; min-height: var(--tap);
  font-weight: 600; cursor: pointer; list-style: none; border-radius: var(--radius-1);
}
.disclosure > summary::-webkit-details-marker { display: none; }
.disclosure > summary::marker { content: ""; }
.disclosure > summary::before {
  content: ""; flex: none; display: inline-block; width: 0.5em; height: 0.5em; margin-inline: 0.15em var(--space-3);
  border: solid currentColor; border-width: 0 2px 2px 0; transform: rotate(-45deg);
  transition: transform var(--duration) var(--ease);
}
.disclosure[open] > summary::before { transform: translateY(-0.15em) rotate(45deg); }
.disclosure > summary:hover { color: var(--accent); }
.disclosure > summary:focus-visible { outline-offset: 2px; }
.disclosure[open] > summary { margin-bottom: var(--space-2); }
@media (prefers-reduced-motion: reduce) { .disclosure > summary::before { transition: none; } }

.badge {
  display: inline-flex; width: fit-content; align-self: flex-start; align-items: center; gap: var(--space-1);
  padding: 0.15em 0.6em; border-radius: var(--radius-full);
  font-size: var(--step--1); font-weight: 600; line-height: 1.4;
  background: var(--surface-2); color: var(--text); border: var(--border-width) solid var(--border);
}
.badge-accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge-success { background: var(--success-bg); color: var(--success); border-color: transparent; }
.badge-warning { background: var(--warning-bg); color: var(--warning); border-color: transparent; }
.badge-danger { background: var(--danger-bg); color: var(--danger); border-color: transparent; }

.alert {
  display: flex; gap: var(--space-3); padding: var(--space-4);
  border-radius: var(--radius-2); border: var(--border-width) solid var(--border);
  border-left-width: 4px; background: var(--surface);
}
.alert-info { border-left-color: var(--accent); }
.alert-success { border-left-color: var(--success); background: var(--success-bg); }
.alert-warning { border-left-color: var(--warning); background: var(--warning-bg); }
.alert-danger { border-left-color: var(--danger); background: var(--danger-bg); }

/* ---------- Table (scrolls inside its wrapper on narrow screens) ---------- */
/* Scroll shadows without JS: the --table-bg covers scroll with the content (local), the shades stay at the
   edges (scroll) — a shade shows only where there is more to scroll. Set --table-bg to the wrapper's surface. */
.table-wrap {
  --table-shade: color-mix(in srgb, var(--text) 24%, transparent);
  overflow-x: auto; border: var(--border-width) solid var(--border); border-radius: var(--radius-2);
  background:
    linear-gradient(to right, var(--table-bg, var(--bg)) 30%, transparent) left center / 2.5rem 100% no-repeat local,
    linear-gradient(to left, var(--table-bg, var(--bg)) 30%, transparent) right center / 2.5rem 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, var(--table-shade), transparent) left center / 0.875rem 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, var(--table-shade), transparent) right center / 0.875rem 100% no-repeat scroll,
    var(--table-bg, var(--bg));
}
.table { width: 100%; border-collapse: collapse; font-size: var(--step--1); }
.table th, .table td { padding: var(--space-3) var(--space-4); text-align: left; border-bottom: var(--border-width) solid var(--border); white-space: nowrap; }
.table th { background: var(--surface); font-weight: 600; }
.table tr:last-child td { border-bottom: 0; }

/* ---------- Dialog (native <dialog>) ---------- */
.dialog {
  width: min(100% - 2 * var(--space-4), 32rem); max-height: min(90dvh, 48rem);
  margin: auto; /* the global reset removes the browser centring of a modal <dialog> */
  padding: var(--space-6); border: var(--border-width) solid var(--border); border-radius: var(--radius-3);
  background: var(--bg); color: var(--text); box-shadow: var(--shadow);
}
.dialog::backdrop { background: var(--backdrop); }
.dialog[open] { animation: dialog-in var(--duration) var(--ease); }
@keyframes dialog-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
@media (max-width: 479.98px) {
  .dialog { width: 100%; max-width: 100%; margin: auto 0 0; border-radius: var(--radius-3) var(--radius-3) 0 0; }
}

/* ---------- Toasts ---------- */
.toasts {
  position: fixed; z-index: 50; inset: auto var(--space-4) var(--space-4) auto;
  display: flex; flex-direction: column; gap: var(--space-2); width: min(100% - 2 * var(--space-4), 24rem);
  pointer-events: none;
}
@media (max-width: 479.98px) { .toasts { inset: auto var(--space-4) var(--space-4) var(--space-4); width: auto; } }
.toast {
  pointer-events: auto; display: flex; align-items: flex-start; gap: var(--space-3);
  padding: var(--space-3) var(--space-4); border-radius: var(--radius-2);
  background: var(--text); color: var(--bg); box-shadow: var(--shadow);
  animation: toast-in var(--duration) var(--ease);
}
/* Type: glyph (✓ ! ✕, with a hidden text label from days.js) plus a stripe ≥ 3:1 on the toast */
.toast:is(.toast-success, .toast-warning, .toast-danger) { border-left: 4px solid var(--toast-stripe); }
.toast-success { --toast-stripe: var(--toast-success); }
.toast-warning { --toast-stripe: var(--toast-warning); }
.toast-danger { --toast-stripe: var(--toast-danger); }
.toast-icon { flex: none; width: 1.25em; font-weight: 800; text-align: center; }
.toast-text { flex: 1; min-width: 0; }
.toast-close {
  flex: none; width: 1.75rem; height: 1.75rem; margin: -0.125rem calc(-1 * var(--space-2)) 0 0;
  display: inline-flex; align-items: center; justify-content: center;
  border: 0; border-radius: var(--radius-1); background: transparent; color: inherit; cursor: pointer;
  font-size: var(--step-1); line-height: 1;
}
/* The toast is inverted: the theme focus colour can be lost on it, the text colour is not */
.toast :focus-visible { outline-color: currentColor; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- Theme switcher ---------- */
.theme-switch { display: inline-flex; padding: 2px; background: var(--surface-2); border: var(--border-width) solid var(--border); border-radius: var(--radius-full); }
.theme-switch button {
  min-width: var(--tap); min-height: 36px; padding: 0 var(--space-2);
  border: var(--border-width) solid transparent; border-radius: var(--radius-full); background: transparent; color: var(--muted); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.theme-switch button:hover { color: var(--text); }
/* Selected: a --border-strong ring (≥ 3:1 on the track), not only the lighter background */
.theme-switch button[aria-pressed="true"] { background: var(--bg); color: var(--text); border-color: var(--border-strong); box-shadow: var(--shadow); }
.theme-switch svg { width: 18px; height: 18px; }
/* Phones: only the active theme's button is shown; tapping it cycles auto → light → dark → contrast (days.js) */
@media (max-width: 767.98px) {
  .theme-switch button:not([aria-pressed="true"]),
  .theme-switch:not(:has([aria-pressed="true"])) button:not(:first-child) { display: none; }
}

/* ---------- Contrast theme: component rules ----------
   For [data-theme="contrast"] and, in the @media block, for the auto theme (prefers-contrast: more, no data-theme).
   Keep the two lists identical: check-contrast.py fails if a rule has no auto counterpart. */
[data-theme="contrast"] a { text-decoration-thickness: 2px; }
[data-theme="contrast"] :is(.btn-secondary, .btn-ghost) { border-color: var(--border); }
[data-theme="contrast"] :is(.input, .select, .textarea):focus-visible { outline: var(--focus-width) solid var(--focus); outline-offset: 2px; box-shadow: none; }
[data-theme="contrast"] .badge { border-color: currentColor; }
[data-theme="contrast"] :is(.input, .textarea)::placeholder { font-style: italic; }
[data-theme="contrast"] .toast { background: var(--bg); color: var(--text); border: 2px solid var(--text); }
[data-theme="contrast"] .toast:is(.toast-success, .toast-warning, .toast-danger) { border-left: 4px solid var(--toast-stripe); }
[data-theme="contrast"] .theme-switch button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
@media (prefers-contrast: more) {
  :root:not([data-theme]) a { text-decoration-thickness: 2px; }
  :root:not([data-theme]) :is(.btn-secondary, .btn-ghost) { border-color: var(--border); }
  :root:not([data-theme]) :is(.input, .select, .textarea):focus-visible { outline: var(--focus-width) solid var(--focus); outline-offset: 2px; box-shadow: none; }
  :root:not([data-theme]) .badge { border-color: currentColor; }
  :root:not([data-theme]) :is(.input, .textarea)::placeholder { font-style: italic; }
  :root:not([data-theme]) .toast { background: var(--bg); color: var(--text); border: 2px solid var(--text); }
  :root:not([data-theme]) .toast:is(.toast-success, .toast-warning, .toast-danger) { border-left: 4px solid var(--toast-stripe); }
  :root:not([data-theme]) .theme-switch button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
}

/* ---------- Forced colours (Windows High Contrast) ---------- */
@media (forced-colors: active) {
  .theme-switch button { border-color: Canvas; }
  .theme-switch button[aria-pressed="true"] { border-color: Highlight; }
}

/* ---------- HTMX helpers ---------- */
.htmx-indicator { opacity: 0; transition: opacity var(--duration) var(--ease); }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }
/* Inside buttons the indicator takes no space until a request starts */
.btn .htmx-indicator { display: none; }
.htmx-request .btn .htmx-indicator, .btn.htmx-request .htmx-indicator { display: inline-block; }
.spinner { width: 1em; height: 1em; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 0.7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--surface), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: var(--radius-1); min-height: 1em; }
@keyframes shimmer { to { background-position: -200% 0; } }
/* Reduced motion: a still dotted ring instead of a spinning one, a flat skeleton */
@media (prefers-reduced-motion: reduce) {
  .spinner { animation: none; border-style: dotted; border-right-color: currentColor; }
  .skeleton { animation: none; background: var(--surface-2); }
}
/* Fade swapped content in: add class="fade-in" to returned partials */
/* ---------- Motion: appear on scroll ----------
   data-reveal — the element slides in when it enters the screen; data-reveal-stagger — its children do, one after another.
   days.js sets html.motion only when JS works and the device does not ask for reduced motion: otherwise all is just visible. */
.motion [data-reveal] {
  opacity: 0; transform: translateY(18px);
  transition: opacity 640ms var(--ease), transform 640ms var(--ease); transition-delay: calc(var(--reveal-i, 0) * 80ms);
}
.motion [data-reveal].is-revealed { opacity: 1; transform: none; }
@media print { .motion [data-reveal] { opacity: 1; transform: none; } }
/* Lift on hover: cards that are links */
.lift { transition: transform var(--duration) var(--ease), box-shadow var(--duration) var(--ease); }
@media (hover: hover) { .lift:hover { transform: translateY(-3px); box-shadow: var(--shadow); } }
@media (prefers-reduced-motion: reduce) { .lift:hover { transform: none; } }

.fade-in { animation: fade-in var(--duration) var(--ease); }
@keyframes fade-in { from { opacity: 0; } }

/* ---------- Utilities ---------- */
.text-center { text-align: center; }
.full-bleed { width: 100%; }
@media (max-width: 767.98px) { .hide-mobile { display: none !important; } }
@media (min-width: 768px) { .show-mobile { display: none !important; } }
