/* =============================================================================
   Gray-Hub design system
   -----------------------------------------------------------------------------
   Single source of truth for the admin panel's visual language. Loaded from
   base.html (after tailwind.css so utility classes like .hidden cascade
   correctly — see the .sheet-backdrop note further down).

   Two layers:
     1. Tokens + base — colors, spacing, type, sidebar nav, focus rings.
     2. Components — .ui-*, .card, .alert, .sheet, .menu, .empty-state, etc.

   Token names are stable; refactor against them, not against raw hex values.
   ============================================================================= */

/* ─────────────────────────────────────────────────────────────────
   Design tokens (2025 refresh).
   Colors / shadows / radii live as CSS vars so themes are a single
   overlay. Every .ui-* class below references vars, so flipping
   :root <-> .dark recolors the whole panel without per-element
   overrides scattered across 78 templates.
───────────────────────────────────────────────────────────────── */
:root {
  /* Surfaces (light) */
  --bg-base:        #F5F7FA;
  --bg-surface:     #FFFFFF;
  --bg-surface-2:   #F0F2F5;
  --bg-surface-3:   #E5E9EF;
  --bg-elevated:    #FFFFFF;

  /* Text */
  --fg-primary:     #0F172A;
  --fg-secondary:   #475569;
  --fg-muted:       #64748B;
  --fg-on-accent:   #FFFFFF;

  /* Borders */
  --border-subtle:  #E2E8F0;
  --border-default: #CBD5E1;
  --border-strong:  #94A3B8;

  /* Accents */
  --accent:           #6366F1;
  --accent-hover:     #4F46E5;
  --accent-soft:      rgba(99, 102, 241, 0.10);
  --accent-ring:      rgba(99, 102, 241, 0.30);
  --accent-gradient:  linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
  --accent-gradient-hover: linear-gradient(135deg, #4F46E5 0%, #7C3AED 100%);
  --accent-gradient-2: linear-gradient(135deg, #06B6D4 0%, #3B82F6 100%);

  /* Status */
  --status-danger:    #DC2626;
  --status-danger-bg: #FEF2F2;
  --status-warn:      #D97706;
  --status-warn-bg:   #FFFBEB;
  --status-ok:        #16A34A;
  --status-ok-bg:     #F0FDF4;

  /* Shadows — visible enough to read on light surfaces. */
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06), 0 2px 6px rgba(15, 23, 42, 0.08);
  --shadow-md: 0 4px 12px -2px rgba(15, 23, 42, 0.10), 0 2px 6px -2px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 16px 32px -8px rgba(15, 23, 42, 0.14), 0 6px 12px -4px rgba(15, 23, 42, 0.10);
  --shadow-xl: 0 24px 48px -12px rgba(15, 23, 42, 0.20), 0 8px 16px -8px rgba(15, 23, 42, 0.10);
  --shadow-primary: 0 4px 12px rgba(99, 102, 241, 0.30);
  --shadow-primary-hover: 0 6px 20px rgba(99, 102, 241, 0.40);
  /* Neomorphic — soft UI. Used on .card-elevated and a few hero
     elements. Aged 2020 trend if overused, premium when applied
     once or twice per page. */
  --shadow-neo:
    8px 8px 20px rgba(15, 23, 42, 0.06),
    -6px -6px 16px rgba(255, 255, 255, 0.85);
  --shadow-neo-inset:
    inset 4px 4px 8px rgba(15, 23, 42, 0.05),
    inset -4px -4px 8px rgba(255, 255, 255, 0.7);

  /* Radii */
  --r-sm:  6px;
  --r-md:  10px;
  --r-lg:  12px;
  --r-xl:  16px;
  --r-2xl: 20px;
  --r-full: 9999px;

  /* Spacing scale — 4px base. Use these instead of arbitrary px so
     vertical rhythm stays clean. Tailwind's space-* utilities also
     line up with these (1=4, 2=8, 3=12, 4=16, 5=20, 6=24…). */
  --space-1:  4px;
  --space-2:  8px;
  --space-3:  12px;
  --space-4:  16px;
  --space-5:  20px;
  --space-6:  24px;
  --space-8:  32px;
  --space-10: 40px;
  --space-12: 48px;
  --space-16: 64px;

  /* Layout — single source of truth for content widths. */
  --content-max:    1200px;
  --content-padding: 24px;
  --card-padding:    24px;
  --card-padding-lg: 32px;
}
.dark {
  --bg-base:        #0B0F19;
  --bg-surface:     #111827;
  --bg-surface-2:   #1E293B;
  --bg-surface-3:   #273449;
  --bg-elevated:    #1E293B;

  --fg-primary:     #E5E7EB;
  --fg-secondary:   #CBD5E1;
  --fg-muted:       #9CA3AF;
  --fg-on-accent:   #FFFFFF;

  --border-subtle:  rgba(255, 255, 255, 0.06);
  --border-default: rgba(255, 255, 255, 0.10);
  --border-strong:  rgba(255, 255, 255, 0.18);

  --accent:           #818CF8;
  --accent-hover:     #6366F1;
  --accent-soft:      rgba(129, 140, 248, 0.14);
  --accent-ring:      rgba(129, 140, 248, 0.40);

  --status-danger-bg: rgba(220, 38, 38, 0.12);
  --status-warn-bg:   rgba(217, 119, 6, 0.12);
  --status-ok-bg:     rgba(22, 163, 74, 0.12);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.30), 0 1px 3px rgba(0, 0, 0, 0.40);
  --shadow-md: 0 4px 8px -2px rgba(0, 0, 0, 0.45), 0 2px 4px -2px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 12px 24px -8px rgba(0, 0, 0, 0.55), 0 4px 8px -4px rgba(0, 0, 0, 0.40);
  --shadow-xl: 0 24px 48px -12px rgba(0, 0, 0, 0.65), 0 8px 16px -8px rgba(0, 0, 0, 0.50);
  --shadow-primary: 0 4px 14px rgba(129, 140, 248, 0.38);
  --shadow-primary-hover: 0 6px 22px rgba(129, 140, 248, 0.48);
  --shadow-neo:
    6px 6px 16px rgba(0, 0, 0, 0.55),
    -3px -3px 8px rgba(255, 255, 255, 0.04);
  --shadow-neo-inset:
    inset 3px 3px 6px rgba(0, 0, 0, 0.45),
    inset -3px -3px 6px rgba(255, 255, 255, 0.04);
}

body {
  font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: var(--fg-primary);
  font-feature-settings: "cv11", "ss01", "ss03";  /* Inter alternates: more readable digits + lower-case "a" */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  /* Subtle radial gradient adds visual depth without being noisy.
     Two soft indigo + violet tints in opposite corners reinforce
     the brand gradient on cards and the active nav-link. */
  background:
    radial-gradient(circle at 0% 0%, rgba(99, 102, 241, 0.04), transparent 50%),
    radial-gradient(circle at 100% 100%, rgba(139, 92, 246, 0.04), transparent 50%),
    var(--bg-base);
  background-attachment: fixed;
}
.dark body {
  background:
    radial-gradient(circle at 0% 0%, rgba(99, 102, 241, 0.06), transparent 50%),
    radial-gradient(circle at 100% 100%, rgba(139, 92, 246, 0.06), transparent 50%),
    var(--bg-base);
  background-attachment: fixed;
}

/* Type scale — gentle hierarchy. h1 is the page heading.
   Use Tailwind classes if you need ad-hoc sizing. */
h1 { font-size: 28px; line-height: 1.25; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 18px; line-height: 1.35; font-weight: 600; letter-spacing: -0.005em; }
h3 { font-size: 15px; line-height: 1.4;  font-weight: 600; }

.fade-in { animation: fadeIn .2s ease-out; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.spinner { border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; width: 14px; height: 14px; animation: spin .7s linear infinite; display: inline-block; vertical-align: middle; opacity: .5; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Inline-saving pulse — for selects/inputs that auto-submit via AJAX.
   Pulse-fade + cursor:wait makes "I clicked, did anything happen?"
   answerable in 100 ms without changing layout. */
.inline-saving { animation: inline-saving-pulse 1s ease-in-out infinite; cursor: wait; pointer-events: none; }
@keyframes inline-saving-pulse { 0%, 100% { opacity: 1; } 50% { opacity: .55; } }

/* ── Micro-UX baseline ────────────────────────────────────────────
   These rules apply to ALL buttons in the panel (not only .ui-btn)
   so the dozens of templates with ad-hoc Tailwind classes still
   benefit. Pure additive — no visual change unless previously buggy. */
button[disabled],
button:disabled,
a.is-disabled,
[aria-disabled="true"] {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}
button:not([disabled]):not(:disabled),
a:not(.is-disabled):not([aria-disabled="true"]) {
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease,
              opacity 120ms ease, transform 80ms ease, box-shadow 120ms ease;
}
/* Subtle press feedback so user knows the click registered even
   before the next paint. Tiny enough not to feel game-y. */
button:not([disabled]):not(:disabled):active,
a:not(.is-disabled):active {
  transform: translateY(0.5px);
}
/* Visible focus ring everywhere — keyboard users were guessing. */
button:focus-visible,
a:focus-visible,
select:focus-visible,
[role="button"]:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-ring);
  border-radius: var(--r-md);
}
/* Brief flash when an inline-AJAX action confirms success. Add the
   class via JS for ~600ms on the row/element. Subtle, not scream. */
.saved-flash { animation: saved-flash 600ms ease-out; }
@keyframes saved-flash {
  0% { background-color: rgb(34 197 94 / 0.18); }
  100% { background-color: transparent; }
}
/* Sidebar nav.
   Active state: soft accent tint + 3px gradient indicator on the
   left edge. Reads as "you are here" without filling the whole row
   with brand color, which felt heavy at scale (18 nav items). */
.nav-link {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: var(--r-md);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--fg-secondary);
  transition: background 150ms ease, color 150ms ease, transform 80ms ease;
}
.nav-link:hover {
  background: var(--bg-surface-2);
  color: var(--fg-primary);
}
.nav-link.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}
.nav-link.active::before {
  content: "";
  position: absolute;
  left: -12px;
  top: 6px;
  bottom: 6px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--accent-gradient);
}
.dark .nav-link.active { color: #C7D2FE; }
.nav-link .icon { width: 18px; height: 18px; flex-shrink: 0; }
.nav-section {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-muted);
  padding: 16px 12px 6px;
  font-weight: 600;
}
.badge {
  font-size: 10px;
  padding: 2px 7px;
  background: var(--status-danger);
  color: white;
  border-radius: var(--r-full);
  font-weight: 600;
  margin-left: auto;
  box-shadow: var(--shadow-sm);
}

/* Collapsed sidebar — icons-only mode on lg+ screens. Width shrinks
   from 16rem to 4rem; everything that isn't an .icon is hidden.
   Drives off the .sb-collapsed flag set on <html> by the head
   pre-paint script — guarantees the saved state applies on the
   very first frame, no full-width flash. Mobile (<lg) keeps the
   off-canvas behaviour and ignores the flag. */
#sidebar { transition: width .15s ease-out; }
@media (min-width: 1024px) {
  .sb-collapsed #sidebar { width: 4rem; overflow: hidden; }
  /* font-size:0 on the link kills every text node (labels are direct
     children of <a>, not wrapped in a span — element-only selectors
     like > *:not(.icon) miss them). Icons stay because they're SVGs;
     badges + nested children that need text reset their own size. */
  .sb-collapsed #sidebar .nav-link { justify-content: center; padding-left: 0; padding-right: 0; gap: 0; font-size: 0; white-space: nowrap; }
  .sb-collapsed #sidebar .nav-link > *:not(.icon) { display: none; }
  .sb-collapsed #sidebar .nav-section { display: none; }
  .sb-collapsed #sidebar-header-extras,
  .sb-collapsed #sidebar-logo,
  .sb-collapsed #user-menu-btn > *:not(.user-avatar),
  .sb-collapsed #user-menu { display: none; }
  .sb-collapsed #user-menu-btn { justify-content: center; padding-left: 0; padding-right: 0; }
  /* Avatar shrinks in collapsed mode (48px avatar would overflow the
     64px-wide rail once the parent padding is in). */
  .sb-collapsed .user-avatar { width: 2.5rem; height: 2.5rem; font-size: .875rem; }
  .sb-collapsed .sidebar-collapse-btn svg { transform: rotate(180deg); }
}
@media (max-width: 1023px) {
  #sidebar { transform: translateX(-100%); transition: transform .2s; }
  #sidebar.open { transform: translateX(0); }
}
.dark input:not([type=checkbox]):not([type=radio]):not([type=submit]):not([type=button]):not([type=file]),
.dark textarea,
.dark select {
  background-color: #13151a;
  color: #e8eaed;
  border-color: #363b45;
}
.dark input::placeholder, .dark textarea::placeholder { color: #6b7280; }
.dark input:focus, .dark textarea:focus, .dark select:focus { border-color: #6366f1; outline: none; }
.dark option { background: #181b21; color: #e8eaed; }
.dark ::-webkit-scrollbar { width: 10px; height: 10px; }
.dark ::-webkit-scrollbar-track { background: #0a0b0e; }
.dark ::-webkit-scrollbar-thumb { background: #262a33; border-radius: 5px; }
.dark ::-webkit-scrollbar-thumb:hover { background: #363b45; }

/* =============================================================================
   Components
   ============================================================================= */

.kbd-kb {
  display: inline-block;
  min-width: 1.5rem;
  padding: 1px 6px;
  margin-left: 4px;
  text-align: center;
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
  color: #475569;
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
  border-bottom-width: 2px;
  border-radius: 4px;
}
.dark .kbd-kb {
  color: #cbd5e1;
  background: #262a33;
  border-color: #363b45;
}

/* Unified iOS-style toggle — any checkbox with class `ui-toggle` renders
   as a 40×22 pill switch. Keeps the accessible checkbox semantics
   (tab, space, screen readers all just work). Used across settings,
   content forms, automation — any on/off control. */
.ui-toggle { position: absolute; opacity: 0; pointer-events: none; }
.ui-toggle-track {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 22px;
  flex-shrink: 0;
  border-radius: 9999px;
  background: #cbd5e1;
  transition: background 150ms ease;
  cursor: pointer;
  vertical-align: middle;
}
.dark .ui-toggle-track { background: #363b45; }
.ui-toggle:checked + .ui-toggle-track { background: #22c55e; }
.dark .ui-toggle:checked + .ui-toggle-track { background: #16a34a; }
.ui-toggle:focus-visible + .ui-toggle-track {
  outline: 2px solid rgb(79 70 229 / 0.5);
  outline-offset: 2px;
}
.ui-toggle-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 9999px;
  background: #ffffff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.2);
  transition: transform 150ms ease;
}
.ui-toggle:checked + .ui-toggle-track::after { transform: translateX(18px); }
.ui-toggle:disabled + .ui-toggle-track { opacity: 0.5; cursor: not-allowed; }

/* Button-form toggle variant (no checkbox, server state via aria-pressed).
   Used by /settings/automation where each toggle is its own POST form. */
.toggle-switch {
  position: relative;
  width: 40px; height: 22px;
  flex-shrink: 0;
  border: 0; padding: 0;
  border-radius: 9999px;
  background: #cbd5e1;
  transition: background 150ms ease;
  cursor: pointer;
}
.dark .toggle-switch { background: #363b45; }
.toggle-switch[aria-pressed="true"] { background: #22c55e; }
.dark .toggle-switch[aria-pressed="true"] { background: #16a34a; }
.toggle-switch:focus-visible {
  outline: 2px solid rgb(79 70 229 / 0.5);
  outline-offset: 2px;
}
.toggle-knob {
  position: absolute;
  top: 2px; left: 2px;
  width: 18px; height: 18px;
  border-radius: 9999px;
  background: #ffffff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.2);
  transition: transform 150ms ease;
}
.toggle-switch[aria-pressed="true"] .toggle-knob { transform: translateX(18px); }

/* ===================================================================
   Unified form controls — one size, one look, driven by design tokens.

   `.ui-input` on inputs/selects/textareas. `.ui-btn` on buttons,
   paired with `.ui-btn-primary` (gradient brand fill),
   `.ui-btn-secondary` (subtle outline), `.ui-btn-ghost` (no chrome
   until hover), `.ui-btn-danger` (red outline).

   Height: 36px. Radius: 10px. Focus: 3px accent ring.
=================================================================== */
.ui-input {
  display: inline-flex;
  align-items: center;
  height: 36px;
  padding: 0 14px;
  font-size: 13.5px;
  line-height: 1.3;
  border: 1px solid var(--border-default);
  border-radius: var(--r-md);
  background: var(--bg-surface);
  color: var(--fg-primary);
  transition: border-color 150ms ease, box-shadow 150ms ease, background 150ms ease;
  width: 100%;
  box-sizing: border-box;
}
.ui-input:hover:not(:disabled):not(:focus) { border-color: var(--border-strong); }
.ui-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}
.ui-input::placeholder { color: var(--fg-muted); opacity: 0.8; }
.ui-input:disabled {
  background: var(--bg-surface-2);
  color: var(--fg-muted);
  cursor: not-allowed;
}

/* ─── Inline validation ──────────────────────────────────────────
   Validation feedback only fires AFTER a failed submit attempt — the
   form gets `form.is-validated` from the global submit handler and
   every invalid field flips red. Plus a manual `.field-invalid`
   class for routes that surface server-side errors per field.

   We deliberately don't do "as you type" validation: it produced
   red borders on every required <textarea> without a placeholder
   (`:placeholder-shown` never matches when there's no placeholder
   attribute), spooking users before they ever touched Save.
   The .field-error sibling (auto-injected by JS or hand-placed) flips
   visible at the same time, so the user always sees WHY it's red. */
.ui-input.field-invalid,
form.is-validated .ui-input:invalid:not(:focus) {
  border-color: #ef4444;
  background-image: none;
}
.ui-input.field-invalid:focus,
form.is-validated .ui-input:invalid:focus {
  box-shadow: 0 0 0 3px rgb(239 68 68 / 0.15);
  border-color: #ef4444;
}
.field-error {
  display: none;
  margin-top: 4px;
  font-size: 11px;
  line-height: 1.3;
  color: #dc2626;
}
.dark .field-error { color: #f87171; }
.ui-input.field-invalid ~ .field-error,
form.is-validated .ui-input:invalid ~ .field-error {
  display: block;
}
/* Same treatment for the legacy .sf-input skin used in service /
   category form pages — keeps validation feedback consistent. */
form.is-validated .sf-input:invalid:not(:focus) {
  border-color: #ef4444 !important;
}
form.is-validated .sf-input:invalid ~ .field-error {
  display: block;
}

/* <textarea> grows vertically, so opt out of fixed height */
textarea.ui-input {
  height: auto;
  min-height: 72px;
  padding: 8px 12px;
  resize: vertical;
}

/* Native <select> arrow styling — show the caret but keep height flush */
select.ui-input {
  appearance: none;
  background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'/%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 16px;
  padding-right: 32px;
}

/* Buttons. Same height as .ui-input so toolbar rows align.
   Three variants: primary (gradient brand fill), secondary (outline,
   subtle), ghost (chromeless until hover). Hover lifts -1px and
   darkens slightly — Stripe/Linear style "alive" feedback. */
.ui-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 36px;
  padding: 0 16px;
  font-size: 13.5px;
  font-weight: 500;
  line-height: 1;
  border-radius: var(--r-md);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 200ms ease, border-color 200ms ease,
              color 200ms ease, transform 120ms ease,
              box-shadow 200ms ease, opacity 150ms ease, filter 200ms ease;
  white-space: nowrap;
  user-select: none;
}
.ui-btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-ring);
}
.ui-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.ui-btn-sm { height: 30px; padding: 0 12px; font-size: 12.5px; }
.ui-btn-lg { height: 42px; padding: 0 22px; font-size: 14.5px; }

/* Primary — brand gradient + glow shadow + lift on hover. Use both
   background-color (Tailwind preflight resets bg-color on button) AND
   background-image so the gradient always wins. */
.ui-btn-primary {
  background-color: var(--accent);
  background-image: var(--accent-gradient);
  color: var(--fg-on-accent);
  box-shadow: var(--shadow-primary);
  border-color: transparent;
}
.ui-btn-primary:hover:not(:disabled) {
  background-image: var(--accent-gradient-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-primary-hover);
}
.ui-btn-primary:active:not(:disabled) {
  transform: translateY(0);
  filter: brightness(0.97);
}

/* Secondary — flat outline, subtle. Sits next to a primary CTA. */
.ui-btn-secondary {
  background: var(--bg-surface);
  color: var(--fg-primary);
  border-color: var(--border-default);
  box-shadow: var(--shadow-sm);
}
.ui-btn-secondary:hover:not(:disabled) {
  background: var(--bg-surface-2);
  border-color: var(--border-strong);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}
.ui-btn-secondary:active:not(:disabled) { transform: translateY(0); }

/* Ghost — no chrome until hover. For tertiary actions inside dense
   panels (cancel, "see all", etc.). */
.ui-btn-ghost {
  background: transparent;
  color: var(--fg-secondary);
  border-color: transparent;
}
.ui-btn-ghost:hover:not(:disabled) {
  background: var(--bg-surface-2);
  color: var(--fg-primary);
}

/* Danger — outline red, fills on hover. Outline-first reads less
   "press me" than a solid red button which makes the action feel
   deliberate. */
.ui-btn-danger {
  background: var(--bg-surface);
  color: var(--status-danger);
  border-color: var(--border-default);
  box-shadow: var(--shadow-sm);
}
.ui-btn-danger:hover:not(:disabled) {
  background: var(--status-danger-bg);
  border-color: var(--status-danger);
  transform: translateY(-1px);
}
.dark .ui-btn-danger { color: #FCA5A5; }

/* `.card-elevated` — premium hero variant of `.card` with soft
   neomorphic dual-shadow + larger radius. Used sparingly (profile
   hero, marketing-style stat blocks); too noisy if applied broadly. */
.card-elevated {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-2xl);
  box-shadow: var(--shadow-neo);
  color: var(--fg-primary);
}
.card-elevated:hover { transform: translateY(-1px); }

/* Section divider for inside-card row separators. Replaces ad-hoc
   `border-t border-slate-200 dark:border-[#1e2128]` chains. */
.ui-divider {
  height: 1px;
  background: var(--border-subtle);
  margin: 16px 0;
  border: 0;
}

/* Subtle tinted surface — a "well" inside cards. Use for code
   samples, metadata strips, anything that needs to recess. */
.ui-surface-2 {
  background: var(--bg-surface-2);
  border-radius: var(--r-md);
}

/* ── Layout primitives — strict 4px grid + symmetric spacing ─────
   Drop these into any page to keep alignment pristine without
   re-typing the same Tailwind chain.

   .ui-container — max-width 1200, auto-centered, baseline padding.
   .ui-stack-N   — vertical flow with consistent gap (N = 2/3/4/6/8).
   .ui-row-N     — horizontal flex with the same gap scale.
   .ui-grid-N    — N equal columns, gap = --space-4 by default.
                    Add .ui-grid-N-md to switch to N cols on lg+ only. */

.ui-container {
  width: 100%;
  max-width: var(--content-max);
  margin-inline: auto;
  padding-inline: var(--content-padding);
}

.ui-stack-2 > * + * { margin-top: var(--space-2); }
.ui-stack-3 > * + * { margin-top: var(--space-3); }
.ui-stack-4 > * + * { margin-top: var(--space-4); }
.ui-stack-6 > * + * { margin-top: var(--space-6); }
.ui-stack-8 > * + * { margin-top: var(--space-8); }

.ui-row-2 { display: flex; gap: var(--space-2); align-items: center; }
.ui-row-3 { display: flex; gap: var(--space-3); align-items: center; }
.ui-row-4 { display: flex; gap: var(--space-4); align-items: center; }
.ui-row-6 { display: flex; gap: var(--space-6); align-items: center; }

.ui-grid-2 { display: grid; grid-template-columns: 1fr; gap: var(--space-4); }
.ui-grid-3 { display: grid; grid-template-columns: 1fr; gap: var(--space-4); }
.ui-grid-4 { display: grid; grid-template-columns: 1fr; gap: var(--space-4); }
@media (min-width: 768px) {
  .ui-grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .ui-grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (min-width: 1024px) {
  .ui-grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .ui-grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

/* All toolbar / action rows — buttons line up with inputs. The
   ui-input + ui-btn share the same 36px height; .ui-row-3 makes
   the gap consistent. */
.ui-toolbar {
  display: flex;
  gap: var(--space-3);
  align-items: center;
  flex-wrap: wrap;
}
.ui-toolbar .ui-spacer { flex: 1; }  /* push everything after to the right */

/* ╔════════════════════════════════════════════════════════════════╗
   ║  shadcn-style component layer                                   ║
   ║  These map closely to shadcn/ui primitives (Button, Input,      ║
   ║  Card, Alert, Badge, Avatar, Label, Separator, Tabs).           ║
   ║  Naming uses `.shad-*` prefix to coexist with legacy .ui-*      ║
   ║  while pages migrate. Where there's no conflict the unprefixed  ║
   ║  shorter form (e.g. `.label`, `.alert`) is also exposed.         ║
   ║  Visual style: flat, premium, low-noise. Stripe + Linear vibe.  ║
   ╚════════════════════════════════════════════════════════════════╝ */

/* Avatar — circular image w/ fallback. Sizes: sm/md/lg/xl. */
.avatar {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  overflow: hidden;
  border-radius: var(--r-full);
  background: var(--bg-surface-2);
  color: var(--fg-secondary);
  font-weight: 600;
  user-select: none;
}
.avatar-sm { width: 32px; height: 32px; font-size: 12px; }
.avatar-md { width: 40px; height: 40px; font-size: 14px; }
.avatar-lg { width: 56px; height: 56px; font-size: 18px; }
.avatar-xl { width: 80px; height: 80px; font-size: 24px; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-gradient {
  background: var(--accent-gradient);
  color: #fff;
}

/* Badge — rounded pill, four variants. */
.badge-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 10px;
  height: 22px;
  font-size: 11.5px;
  font-weight: 500;
  border-radius: var(--r-full);
  border: 1px solid transparent;
  line-height: 1;
  white-space: nowrap;
}
.badge-default { background: var(--accent-soft); color: var(--accent); }
.badge-secondary { background: var(--bg-surface-2); color: var(--fg-secondary); }
.badge-outline { background: transparent; border-color: var(--border-default); color: var(--fg-secondary); }
.badge-success { background: var(--status-ok-bg); color: var(--status-ok); }
.badge-warning { background: var(--status-warn-bg); color: var(--status-warn); }
.badge-destructive { background: var(--status-danger-bg); color: var(--status-danger); }
.dark .badge-success { color: #86EFAC; }
.dark .badge-warning { color: #FCD34D; }
.dark .badge-destructive { color: #FCA5A5; }

/* Alert — banner with icon + title + body. Variants: default/info/destructive/warning/success. */
.alert {
  position: relative;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-lg);
  background: var(--bg-surface);
  color: var(--fg-primary);
  font-size: 14px;
  line-height: 1.45;
}
.alert > svg { width: 18px; height: 18px; margin-top: 2px; color: var(--fg-muted); flex-shrink: 0; }
.alert-title { font-weight: 600; margin-bottom: 4px; line-height: 1.3; }
.alert-description { color: var(--fg-secondary); font-size: 13.5px; }
.alert-info { border-color: rgba(99, 102, 241, 0.30); background: var(--accent-soft); }
.alert-info > svg { color: var(--accent); }
.alert-info .alert-title { color: var(--accent); }
.alert-warning { border-color: rgba(217, 119, 6, 0.30); background: var(--status-warn-bg); }
.alert-warning > svg { color: var(--status-warn); }
.alert-warning .alert-title { color: var(--status-warn); }
.alert-destructive { border-color: rgba(220, 38, 38, 0.30); background: var(--status-danger-bg); }
.alert-destructive > svg { color: var(--status-danger); }
.alert-destructive .alert-title { color: var(--status-danger); }
.alert-success { border-color: rgba(22, 163, 74, 0.30); background: var(--status-ok-bg); }
.alert-success > svg { color: var(--status-ok); }
.alert-success .alert-title { color: var(--status-ok); }
.dark .alert-warning .alert-title,
.dark .alert-warning > svg { color: #FCD34D; }
.dark .alert-destructive .alert-title,
.dark .alert-destructive > svg { color: #FCA5A5; }
.dark .alert-success .alert-title,
.dark .alert-success > svg { color: #86EFAC; }

/* Label — small, semi-bold, sits over inputs. */
.label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.3;
  color: var(--fg-primary);
  margin-bottom: 6px;
}
.label-helper {
  display: block;
  font-size: 12px;
  color: var(--fg-muted);
  margin-top: 6px;
  line-height: 1.4;
}

/* Separator — horizontal divider. Use instead of <hr>. */
.separator { height: 1px; background: var(--border-subtle); border: 0; margin: 0; }
.separator-vertical { width: 1px; height: 100%; background: var(--border-subtle); }

/* Card primitives (shadcn-style composition). Use together: */
/*   <article class="card">                                  */
/*     <header class="card-header">                           */
/*       <h3 class="card-title">Title</h3>                    */
/*       <p class="card-description">Subtitle</p>             */
/*     </header>                                              */
/*     <div class="card-content">…body…</div>                 */
/*     <footer class="card-footer">…actions…</footer>         */
/*   </article>                                               */
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-sm);
  color: var(--fg-primary);
  overflow: hidden;
}
.card-header { padding: 24px 24px 16px; }
.card-title  { font-size: 16px; font-weight: 600; line-height: 1.3; letter-spacing: -0.005em; margin: 0; }
.card-description { margin-top: 6px; font-size: 13px; color: var(--fg-muted); line-height: 1.45; }
.card-content { padding: 16px 24px; }
.card-content:first-child { padding-top: 24px; }
.card-footer  { padding: 16px 24px 24px; display: flex; align-items: center; gap: 12px; }
.card-footer.justify-between { justify-content: space-between; }
.card-footer.justify-end { justify-content: flex-end; }
/* When header is followed directly by content, kill the duplicate gap */
.card-header + .card-content { padding-top: 0; }
/* Legacy/compat: many older pages use <section class="card"> without
   wrapping the body in .card-content (settings/statuses, custom-fields,
   automation, etc.). Without this, the content slams against the
   border because .card itself has no padding. New code should use
   .card-content explicitly; this just keeps existing pages legible. */
.card:not(:has(> .card-header)):not(:has(> .card-content)):not(:has(> .card-footer)) {
  padding: 20px;
}

/* Page header — h1 + optional subtitle on the left, action buttons on
   the right. Wraps to two rows under ~720px so 3+ buttons don't crash
   into the title. Use as:
      <header class="page-header">
        <div class="page-header-titles">
          <h1 class="page-header-title">…</h1>
          <p class="page-header-subtitle">…</p>   {# optional #}
        </div>
        <div class="page-header-actions">
          <a class="ui-btn ui-btn-primary">…</a>
          <a class="ui-btn ui-btn-secondary">…</a>
        </div>
      </header>
*/
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.page-header-titles {
  display: flex; flex-direction: column; gap: 2px;
  min-width: 0; flex: 1 1 280px;
}
.page-header-title {
  font-size: 22px; font-weight: 600;
  color: var(--fg-primary);
  margin: 0;
  display: inline-flex; align-items: center; gap: 8px;
}
.page-header-title svg { color: var(--fg-muted); flex-shrink: 0; }
.page-header-subtitle {
  font-size: 13px; color: var(--fg-muted);
  margin: 0;
}
.page-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  flex-shrink: 0;
}
/* On narrow viewports, actions go full-width below the title group. */
@media (max-width: 720px) {
  .page-header-actions {
    width: 100%;
    justify-content: flex-start;
  }
  .page-header-actions > * { flex: 1 1 auto; }
}

/* Compact pill-toggle group — for short multi-select sets like
   language picker on /users. Uses :has() so the "active" look paints
   instantly on click without JavaScript or page-reload. The actual
   filter applies on form submit. */
.lang-toggle {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  border-radius: var(--r-md);
  background: var(--bg-surface-2);
  border: 1px solid var(--border-subtle);
  height: 36px;  /* match .ui-input height for clean toolbar alignment */
  box-sizing: border-box;
}
.lang-toggle-pill {
  cursor: pointer;
  padding: 0 10px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--fg-secondary);
  transition: background 120ms ease, color 120ms ease;
  user-select: none;
}
.lang-toggle-pill input { position: absolute; opacity: 0; pointer-events: none; }
.lang-toggle-pill:hover { color: var(--fg-primary); }
.lang-toggle-pill:has(input:checked) {
  background: var(--accent);
  color: #fff;
}

/* Tabs — segmented control on top of card content. */
.tabs-list {
  display: inline-flex;
  height: 36px;
  align-items: center;
  padding: 4px;
  gap: 2px;
  border-radius: var(--r-md);
  background: var(--bg-surface-2);
}
.tabs-trigger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 12px;
  height: 28px;
  font-size: 13px;
  font-weight: 500;
  color: var(--fg-muted);
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  transition: background 150ms ease, color 150ms ease;
  border: 0;
}
.tabs-trigger:hover { color: var(--fg-primary); }
.tabs-trigger[aria-selected="true"], .tabs-trigger.active, .tabs-trigger.is-active {
  background: var(--bg-surface);
  color: var(--fg-primary);
  box-shadow: var(--shadow-sm);
}

/* Filter chip — used by /applications + /applications/pipeline + others.
   Active state uses the brand gradient; counts sit inside as soft pills. */
.filter-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px;
  font-size: 13px; font-weight: 500;
  border-radius: var(--r-md);
  border: 1px solid var(--border-default);
  background: var(--bg-surface);
  color: var(--fg-secondary);
  transition: all 150ms ease;
  white-space: nowrap;
}
.filter-chip:hover {
  background: var(--bg-surface-2);
  border-color: var(--border-strong);
  color: var(--fg-primary);
}
.filter-chip.is-active {
  background-color: var(--accent);
  background-image: var(--accent-gradient);
  color: #fff;
  border-color: transparent;
  box-shadow: var(--shadow-primary);
}
.filter-chip-count {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: var(--r-full);
  background: var(--bg-surface-2);
  color: var(--fg-muted);
  font-weight: 600;
  font-feature-settings: "tnum";
}
.filter-chip.is-active .filter-chip-count {
  background: rgba(255, 255, 255, 0.22);
  color: #fff;
}

/* Sheet/Modal backdrop — for any centered floating panel.
   IMPORTANT: hidden by default. design-system.css loads AFTER
   tailwind.css's `.hidden { display: none }`, so a bare
   `display: flex` here would override .hidden and the modal would
   pop on page load. Using :not(.hidden) keeps the cascade safe. */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(15, 23, 42, 0.50);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: none;
  align-items: center; justify-content: center;
  padding: 24px;
}
.sheet-backdrop:not(.hidden) { display: flex; }
.sheet {
  width: 100%; max-width: 480px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-xl);
  overflow: hidden;
}
.sheet-lg { max-width: 640px; }
.sheet-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border-subtle);
}
.sheet-title { font-size: 16px; font-weight: 600; }
.sheet-content { padding: 20px 24px; }
.sheet-footer {
  display: flex; align-items: center; justify-content: flex-end; gap: 8px;
  padding: 16px 24px;
  border-top: 1px solid var(--border-subtle);
  background: var(--bg-surface-2);
}
.sheet-close {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px;
  border-radius: var(--r-md);
  color: var(--fg-muted);
  border: 0; background: transparent; cursor: pointer;
}
.sheet-close:hover { background: var(--bg-surface-2); color: var(--fg-primary); }

/* Dropdown menu — for popovers anchored to a trigger. */
.menu {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  padding: 4px;
  min-width: 180px;
  z-index: 40;
}
.menu-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 13.5px;
  color: var(--fg-primary);
  background: transparent;
  border: 0;
  cursor: pointer;
  text-align: left;
}
.menu-item:hover { background: var(--bg-surface-2); }
.menu-item-destructive { color: var(--status-danger); }
.menu-item-destructive:hover { background: var(--status-danger-bg); }
.menu-separator { height: 1px; background: var(--border-subtle); margin: 4px 0; }
.menu-section-label {
  font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--fg-muted);
  padding: 8px 10px 4px;
}

/* Dropdown — wrapper that anchors a .menu under a button trigger.
   Markup:
     <div class="dropdown">
       <button class="ui-btn ui-btn-secondary" data-dropdown-trigger>Label ▾</button>
       <div class="dropdown-menu menu hidden">…items…</div>
     </div>
   The wire-up JS at the bottom of base.html toggles .hidden on click
   and closes on outside-click / Escape. */
.dropdown { position: relative; display: inline-block; }
.dropdown-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 220px;
}
.dropdown-menu.dropdown-menu-left { left: 0; right: auto; }

/* Empty state — centered icon + message + optional action.
   Default size for stand-alone empty pages; .empty-state-compact for
   in-card / in-table use where 64px feels excessive. */
.empty-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center;
  padding: 64px 24px;
  color: var(--fg-muted);
}
.empty-state svg { width: 32px; height: 32px; margin-bottom: 12px; opacity: 0.5; }
.empty-state-title { font-size: 15px; font-weight: 600; color: var(--fg-primary); margin-bottom: 4px; }
.empty-state-description { font-size: 13.5px; max-width: 320px; }
.empty-state-compact { padding: 32px 16px; }
.empty-state-compact svg { width: 24px; height: 24px; margin-bottom: 8px; }
.empty-state-compact .empty-state-title { font-size: 14px; margin-bottom: 2px; }
.empty-state-compact .empty-state-description { font-size: 12.5px; }

/* List row — for "Settings rows" pattern. Icon + label + value + chevron. */
.list-row {
  display: flex; align-items: center; gap: 16px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border-subtle);
}
.list-row:last-child { border-bottom: 0; }
.list-row-label { font-size: 13.5px; font-weight: 500; flex: 1; min-width: 0; }
.list-row-value { font-size: 13px; color: var(--fg-muted); }

/* ╔════════════════════════════════════════════════════════════════╗
   ║  Creative microinteractions                                     ║
   ║  Subtle animations and visual flourishes that turn a "correct" ║
   ║  UI into one with personality. Use sparingly per page.          ║
   ╚════════════════════════════════════════════════════════════════╝ */

/* Pulse — for urgent/unread indicators on cards. Subtle ring expansion. */
@keyframes pulse-ring {
  0%   { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.45); }
  70%  { box-shadow: 0 0 0 12px rgba(99, 102, 241, 0); }
  100% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0); }
}
@keyframes pulse-ring-danger {
  0%   { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.45); }
  70%  { box-shadow: 0 0 0 12px rgba(220, 38, 38, 0); }
  100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0); }
}
.pulse-dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse-ring 2s ease-out infinite;
}
.pulse-dot-danger {
  background: var(--status-danger);
  animation: pulse-ring-danger 2s ease-out infinite;
}

/* Trend chip — small ↑ or ↓ pill for KPI deltas. */
.trend-up, .trend-down, .trend-flat {
  display: inline-flex; align-items: center; gap: 2px;
  font-size: 11px; font-weight: 600;
  padding: 1px 6px; border-radius: var(--r-full);
}
.trend-up   { color: var(--status-ok); background: var(--status-ok-bg); }
.trend-down { color: var(--status-danger); background: var(--status-danger-bg); }
.trend-flat { color: var(--fg-muted); background: var(--bg-surface-2); }

/* Donut — composable inline SVG sized via CSS. The donut path comes
   from the template; this just provides consistent stroke + bg. */
.donut-track { stroke: var(--bg-surface-2); }
.donut-fill  { stroke: url(#brand-grad); transition: stroke-dasharray .6s ease-out; }

/* Sparkline svg — inline tiny chart for KPI cards. */
.sparkline { stroke: var(--accent); stroke-width: 1.5; fill: none; }
.sparkline-area { fill: var(--accent-soft); stroke: none; }

/* Card flair — diagonal gradient accent on the card top. Use on
   hero blocks for visual identity. */
.card-flair-top::before {
  content: ""; position: absolute;
  top: 0; left: 0; right: 0; height: 4px;
  background: var(--accent-gradient);
}
.card-flair-top { position: relative; padding-top: calc(24px + 4px); }

/* Stagger-fade — for list reveals. Apply via JS by adding `.stagger-in`
   to the parent; children fade in one-by-one. */
@keyframes stagger-fade {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.stagger-in > * {
  opacity: 0;
  animation: stagger-fade .35s ease-out forwards;
}
.stagger-in > *:nth-child(1) { animation-delay: 0ms; }
.stagger-in > *:nth-child(2) { animation-delay: 60ms; }
.stagger-in > *:nth-child(3) { animation-delay: 120ms; }
.stagger-in > *:nth-child(4) { animation-delay: 180ms; }
.stagger-in > *:nth-child(5) { animation-delay: 240ms; }
.stagger-in > *:nth-child(6) { animation-delay: 300ms; }

/* Number-tick — large stat that reads as "the number". */
.stat-number {
  font-size: 32px;
  font-weight: 700;
  line-height: 1.05;
  letter-spacing: -0.02em;
  font-feature-settings: "tnum";  /* tabular numerals — aligns digits */
}
.stat-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-muted);
  font-weight: 600;
}
.stat-helper {
  font-size: 12px;
  color: var(--fg-muted);
  margin-top: 4px;
}

/* Checkbox — matches toggle look (22×22 rounded square) */
.ui-checkbox {
  appearance: none;
  width: 18px; height: 18px;
  border: 1.5px solid #cbd5e1;
  border-radius: 4px;
  background: #ffffff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 120ms, border-color 120ms;
}
.dark .ui-checkbox { background: #181b21; border-color: #363b45; }
.ui-checkbox:checked {
  background: #4f46e5;
  border-color: #4f46e5;
}
.ui-checkbox:checked::after {
  content: "";
  width: 10px;
  height: 6px;
  border-left: 2px solid #fff;
  border-bottom: 2px solid #fff;
  transform: translateY(-1px) rotate(-45deg);
}
.ui-checkbox:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgb(99 102 241 / 0.35);
}

/* Field wrapper — label + control, standard vertical rhythm */
.ui-field { display: flex; flex-direction: column; gap: 4px; }
.ui-label {
  font-size: 11px;
  font-weight: 500;
  color: #64748b;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.dark .ui-label { color: #94a3b8; }
