/* 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;
  --bg: #ffffff;
  --surface: #f5f6f8;
  --surface-2: #eaedf1;
  --text: #15171c;
  --muted: #535b69;
  --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;
  --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;
  --bg: #0e1014;
  --surface: #171a21;
  --surface-2: #20242d;
  --text: #e9ebef;
  --muted: #a4abb8;
  --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;
  --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;
    --bg: #0e1014;
    --surface: #171a21;
    --surface-2: #20242d;
    --text: #e9ebef;
    --muted: #a4abb8;
    --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;
    --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;
  --bg: #000000;
  --surface: #000000;
  --surface-2: #141414;
  --text: #ffffff;
  --muted: #e6e6e6;
  --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;
  --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;
    --bg: #000000;
    --surface: #000000;
    --surface-2: #141414;
    --text: #ffffff;
    --muted: #e6e6e6;
    --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;
    --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; }
[data-theme="contrast"] a { 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; 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 .nav-toggle { display: none; }
@media (max-width: 767.98px) {
  .site-header .nav-toggle { display: inline-flex; margin-left: auto; }
  .nav:not(.is-open) { display: none; }
  .nav.is-open {
    position: absolute; right: var(--space-4); 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);
  }
}

.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); }
.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); }
[data-theme="contrast"] .btn-secondary, [data-theme="contrast"] .btn-ghost { border-color: var(--border); }
@media (prefers-contrast: more) { :root:not([data-theme]) .btn-secondary, :root:not([data-theme]) .btn-ghost { border-color: var(--border); } }

/* ---------- 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 { color: var(--danger); font-size: var(--step--1); font-weight: 600; }
.input, .select, .textarea {
  width: 100%; min-height: var(--tap); padding: var(--space-2) var(--space-3);
  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; }
/* 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; }
.field { min-width: 0; }
.input::placeholder, .textarea::placeholder { color: var(--muted); opacity: 1; }
.input:focus-visible, .select:focus-visible, .textarea:focus-visible { outline: none; border-color: var(--focus); box-shadow: 0 0 0 var(--focus-width) color-mix(in srgb, var(--focus) 35%, transparent); }
[data-theme="contrast"] :is(.input, .select, .textarea):focus-visible { outline: var(--focus-width) solid var(--focus); outline-offset: 2px; box-shadow: none; }
.input[aria-invalid="true"], .textarea[aria-invalid="true"], .select[aria-invalid="true"] { border-color: var(--danger); }
.check { display: inline-flex; align-items: center; gap: var(--space-3); min-height: var(--tap); cursor: pointer; }
.check input { width: 1.25rem; height: 1.25rem; accent-color: var(--accent); margin: 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); }

.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); }
[data-theme="contrast"] .badge { border-color: currentColor; }

/* ---------- Table (scrolls inside its wrapper on narrow screens) ---------- */
.table-wrap { overflow-x: auto; border: var(--border-width) solid var(--border); border-radius: var(--radius-2); }
.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);
  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);
}
.toast-success { border-left: 4px solid var(--success); }
.toast-warning { border-left: 4px solid var(--warning); }
.toast-danger { border-left: 4px solid var(--danger); }
[data-theme="contrast"] .toast { background: var(--bg); color: var(--text); border: 2px solid var(--text); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- Theme switcher ---------- */
.theme-switch { display: inline-flex; padding: 3px; gap: 2px; background: var(--surface-2); border: var(--border-width) solid var(--border); border-radius: var(--radius-full); }
.theme-switch button {
  min-width: 36px; min-height: 36px; padding: 0 var(--space-2);
  border: 0; 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); }
.theme-switch button[aria-pressed="true"] { background: var(--bg); color: var(--text); box-shadow: var(--shadow); }
[data-theme="contrast"] .theme-switch button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }
.theme-switch svg { width: 18px; height: 18px; }

/* ---------- 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; } }
/* Fade swapped content in: add class="fade-in" to returned partials */
.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; } }
