/* ============================================================
   CRM dashboard design tokens

   AI/API contract:
   - Treat --space-*, --radius-*, --font-*, --line-height-* and
     --color-* as the public design-token API; do not hard-code
     component colors, spacing, radii, or type sizes.
   - Structural tokens live in :root. Theme colors inherit from the
     nearest [data-theme="light"] or [data-theme="dark"] ancestor.
   - Load this file before form.css and buttons.css so layer order is
     established before component styles and Tailwind utilities.
   ============================================================ */

/* Locks in layer precedence up front, before any other layered CSS
   loads (including Tailwind, if it declares its own layers with the
   same names) — without this, precedence is decided by whichever
   layer name is first encountered across the whole page, which is
   fragile and load-order-dependent. This must be the first layered
   CSS the page parses. */
@layer base, components, utilities;

/* The hidden attribute always hides, even on components that set display */
@layer base {
  [hidden] {
    display: none !important;
  }
}

:root {
  /* --- Spacing scale — base unit 0.25rem (4px). Everything (padding,
     margin, gap) references these tokens. --- */
  --space-1: 0.25rem;  /* 4px */
  --space-2: 0.5rem;   /* 8px */
  --space-3: 0.75rem;  /* 12px */
  --space-4: 1rem;     /* 16px */
  --space-5: 1.5rem;   /* 24px */
  --space-6: 2rem;     /* 32px */
  --space-7: 3rem;     /* 48px */
  --space-8: 4rem;     /* 64px */

  /* --- Radius scale — modern, harmonious rounding --- */
  --radius-xs: 0.25rem; /* 4px */
  --radius-sm: 0.375rem; /* 6px */
  --radius-md: 0.5rem;  /* 8px */
  --radius-lg: 0.75rem; /* 12px */
  --radius-xl: 1rem;    /* 16px */
  --radius-2xl: 1.25rem;/* 20px */
  --radius-full: 9999px;

  /* --- Typography scale --- */
  --font-family-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Inter", ui-sans-serif, system-ui, -apple-system, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  --font-family-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --font-size-xs: 0.75rem;    /* 12px */
  --font-size-sm: 0.84375rem; /* 13.5px */
  --font-size-base: 0.9375rem;/* 15px - optimal dashboard reading size */
  --font-size-md: 1rem;       /* 16px */
  --font-size-lg: 1.125rem;   /* 18px */
  --font-size-xl: 1.25rem;    /* 20px */
  --font-size-2xl: 1.5rem;    /* 24px */
  --font-size-3xl: 1.875rem;  /* 30px */

  --line-height-tight: 1.25;
  --line-height-snug: 1.375;
  --line-height-normal: 1.5;
  --line-height-relaxed: 1.625;

  --letter-spacing-tight: -0.02em;
  --letter-spacing-normal: -0.011em;
  --letter-spacing-wide: 0.025em;

  --font-weight-normal: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;

  /* --- Borders --- */
  --border-width: 1px;
  --border-width-thick: 2px;

  /* --- Multi-layered ambient & keylight shadows --- */
  --shadow-xs: 0 1px 2px 0 rgb(0 0 0 / 0.04);
  --shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.06), 0 1px 2px -1px rgb(0 0 0 / 0.06);
  --shadow-md: 0 4px 12px -2px rgb(0 0 0 / 0.07), 0 2px 6px -2px rgb(0 0 0 / 0.04);
  --shadow-lg: 0 12px 24px -4px rgb(0 0 0 / 0.09), 0 4px 10px -2px rgb(0 0 0 / 0.04);
  --shadow-xl: 0 20px 32px -6px rgb(0 0 0 / 0.12), 0 8px 16px -4px rgb(0 0 0 / 0.06);

  /* --- Transitions --- */
  --transition-fast: 140ms cubic-bezier(0.16, 1, 0.3, 1);
  --transition-base: 220ms cubic-bezier(0.16, 1, 0.3, 1);
  --transition-slow: 340ms cubic-bezier(0.16, 1, 0.3, 1);

  /* --- Z-index scale --- */
  --z-dropdown: 1000;
  --z-sticky: 1100;
  --z-overlay: 1200;
  --z-modal: 1300;
  --z-dialog: 1350;  /* notification dialogs: above popups (modal + 10), below toasts */
  --z-toast: 1400;
  --z-tooltip: 1500;

  /* --- Focus ring geometry --- */
  --focus-ring-width: 2px;
  --focus-ring-offset: 2px;

  /* --- Container widths --- */
  --container-sm: 42rem;
  --container-md: 58rem;
  --container-lg: 74rem;
  --container-xl: 90rem;

  /* --- Control density: inputs, selects, selector, buttons, table rows.
     Comfortable by default; data-density="compact" on <html> or any
     container shrinks everything inside it (filter bars, tables). --- */
  --control-font-size: var(--font-size-base);
  --control-padding-y: 0.5625rem;
  --control-padding-x: 0.8125rem;
  --control-min-height: 2.625rem;
  --button-padding-y: 0.5625em;
  --button-padding-x: 1.125em;
  --table-cell-padding-y: var(--space-3);
  --table-cell-padding-x: var(--space-4);
}

[data-density="compact"] {
  --control-font-size: var(--font-size-sm);
  --control-padding-y: 0.3125rem;
  --control-padding-x: 0.625rem;
  --control-min-height: 2rem;
  --button-padding-y: 0.375em;
  --button-padding-x: 0.875em;
  --table-cell-padding-y: var(--space-2);
  --table-cell-padding-x: var(--space-3);
}

/* ============================================================
   Light theme (modern zinc / neutral clean aesthetic)
   ============================================================ */
:root,
[data-theme="light"] {
  /* Native parts (scrollbars, checkboxes, option lists, pickers) follow the theme */
  color-scheme: light;

  --color-bg: #f8fafc;
  --color-bg-subtle: #f1f5f9;
  --color-bg-muted: #e2e8f0;
  --color-surface: #ffffff;
  --color-surface-raised: #ffffff;

  --color-text: #0f172a;
  --color-text-muted: #475569;
  --color-text-subtle: #64748b;
  --color-text-inverse: #ffffff;

  --color-border: #e2e8f0;
  --color-border-strong: #cbd5e1;
  --color-border-subtle: #f1f5f9;

  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-primary-active: #1e40af;
  --color-primary-subtle: #eff6ff;
  --color-primary-border: #bfdbfe;
  --color-primary-text: #1d4ed8;    /* primary-coloured text on a subtle primary background (4.5:1 or more) */

  --color-secondary: #334155;
  --color-secondary-hover: #1e293b;
  --color-secondary-subtle: #f1f5f9;

  --color-success: #059669;
  --color-success-hover: #047857;
  --color-success-subtle: #ecfdf5;
  --color-success-border: #a7f3d0;

  --color-warning: #d97706;
  --color-warning-hover: #b45309;
  --color-warning-subtle: #fffbeb;
  --color-warning-border: #fde68a;

  --color-error: #e11d48;
  --color-error-hover: #be123c;
  --color-error-subtle: #fff1f2;
  --color-error-border: #fecdd3;

  --color-info: #0284c7;
  --color-info-hover: #0369a1;
  --color-info-subtle: #f0f9ff;
  --color-info-border: #bae6fd;

  /* Status colours for text on light or tinted backgrounds (4.5:1 or more);
     the base status colours above stay for fills, borders and icons. */
  --color-success-text: #065f46;
  --color-warning-text: #92400e;
  --color-error-text: #be123c;
  --color-info-text: #0369a1;

  --color-overlay: rgb(15 23 42 / 0.45);
  --focus-ring-color: #3b82f6;
}

/* ============================================================
   Dark theme (deep modern obsidian / slate aesthetic)
   ============================================================ */
[data-theme="dark"] {
  color-scheme: dark;

  --color-bg: #090d16;
  --color-bg-subtle: #0f1422;
  --color-bg-muted: #1e2433;
  --color-surface: #131927;
  --color-surface-raised: #182032;

  --color-text: #f1f5f9;
  --color-text-muted: #94a3b8;
  --color-text-subtle: #8291a7;
  --color-text-inverse: #090d16;

  --color-border: #232c3f;
  --color-border-strong: #334155;
  --color-border-subtle: #1a2233;

  --color-primary: #3b82f6;
  --color-primary-hover: #60a5fa;
  --color-primary-active: #2563eb;
  --color-primary-subtle: rgba(59, 130, 246, 0.12);
  --color-primary-border: rgba(59, 130, 246, 0.25);
  --color-primary-text: #60a5fa;

  --color-secondary: #94a3b8;
  --color-secondary-hover: #cbd5e1;
  --color-secondary-subtle: #1e2638;

  --color-success: #10b981;
  --color-success-hover: #34d399;
  --color-success-subtle: rgba(16, 185, 129, 0.12);
  --color-success-border: rgba(16, 185, 129, 0.25);

  --color-warning: #f59e0b;
  --color-warning-hover: #fbbf24;
  --color-warning-subtle: rgba(245, 158, 11, 0.12);
  --color-warning-border: rgba(245, 158, 11, 0.25);

  --color-error: #f43f5e;
  --color-error-hover: #fb7185;
  --color-error-subtle: rgba(244, 63, 94, 0.12);
  --color-error-border: rgba(244, 63, 94, 0.25);

  --color-info: #38bdf8;
  --color-info-hover: #7dd3fc;
  --color-info-subtle: rgba(56, 189, 248, 0.12);
  --color-info-border: rgba(56, 189, 248, 0.25);

  --color-success-text: #10b981;
  --color-warning-text: #f59e0b;
  --color-error-text: #fb7185;
  --color-info-text: #38bdf8;

  --color-overlay: rgb(0 0 0 / 0.7);
  --focus-ring-color: #60a5fa;
}

/* ============================================================
   prefers-reduced-motion — defined ONCE, here. Every component
   that animates via var(--transition-*) automatically respects
   this with zero extra media queries in component files. Any
   component using a transition NOT built from these tokens
   needs its own reduced-motion override — that's a bug, not a
   style choice.
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  :root {
    --transition-fast: 0ms;
    --transition-base: 0ms;
    --transition-slow: 0ms;
  }
}

/* ============================================================
   forced-colors (Windows High Contrast Mode) — safety net.
   Forced-colors mode overrides most author background/text colors
   with a small system palette regardless of what's declared above;
   the CSS spec lets deliberate system-color keywords (Highlight,
   ButtonText, Canvas, CanvasText) pass through, so remapping the
   handful of tokens that carry MEANING (checked state, focus,
   borders used as the only indicator of state) keeps those states
   visually distinguishable. This is a base-level safety net only —
   components with checked/selected states driven purely by
   background-color (see form.css .choice) still need their own
   forced-colors block to add a non-color indicator (e.g. a border)
   since backgrounds can still be flattened in some browsers.
   ============================================================ */
@media (forced-colors: active) {
  :root {
    --color-primary: Highlight;
    --color-primary-text: Highlight;
    --color-border-strong: ButtonText;
    --focus-ring-color: Highlight;
  }
}
