/* ============================================================
   e2eforms — DESIGN TOKENS (tokens.css)
   Two-tier design-token system for a privacy-first, E2EE
   forms / e-sign SaaS.

   TIER 1 = PRIMITIVES (raw values; never used directly in
            components — only referenced by Tier 2).
   TIER 2 = SEMANTIC (roles: bg / surface / text / border /
            primary / encrypt / success / warning / error /
            focus-ring). Components use ONLY these.

   Brand:  privacy PURPLE  #6D28D9  = primary / brand
           encryption TEAL #06B6D4  = the dedicated "secure"
                                       signal (locks, shields).
   Neutrals are slightly purple-tinted to feel integrated.

   Load order:  tokens.css -> components.css -> app.css
   Pure CSS, no build step (SFTP-deployed). Targets PHP 8.1
   server-rendered markup + optional Alpine sprinkle.

   WCAG: all body/label/muted text pairs are AA+, input border
   uses #767676+ on light, visible 3px focus ring, reduced-motion
   guard, 16px input font, >=24px (mostly >=44px) touch targets.
   ============================================================ */

/* ============================================================
   TIER 1 — PRIMITIVE TOKENS
   ============================================================ */
:root {

  /* ---- PURPLE ramp (brand / privacy) — anchored on #6D28D9 ---- */
  --purple-50:  #f5f3ff;
  --purple-100: #ede9fe;
  --purple-200: #ddd6fe;
  --purple-300: #c4b5fd;
  --purple-400: #a78bfa;
  --purple-500: #8b5cf6;
  --purple-600: #7c3aed;
  --purple-700: #6d28d9;   /* ★ brand primary */
  --purple-800: #5b21b6;
  --purple-900: #4c1d95;
  --purple-950: #2e1065;

  /* ---- TEAL / CYAN ramp (encryption / "secure" signal) ---- */
  --teal-50:  #ecfeff;
  --teal-100: #cffafe;
  --teal-200: #a5f3fc;
  --teal-300: #67e8f9;
  --teal-400: #22d3ee;
  --teal-500: #06b6d4;   /* ★ encryption signal */
  --teal-600: #0891b2;
  --teal-700: #0e7490;
  --teal-800: #155e75;
  --teal-900: #164e63;

  /* ---- NEUTRAL ramp (slightly purple-tinted slate) ---- */
  --neutral-0:   #ffffff;
  --neutral-50:  #f7f7fb;   /* page bg (faint purple tint) */
  --neutral-100: #eeeef4;
  --neutral-200: #e2e2ec;   /* hairline borders */
  --neutral-300: #cbcbd8;
  --neutral-400: #9a9aac;
  --neutral-500: #71718a;   /* muted text on light (>=4.5:1) */
  --neutral-600: #565669;
  --neutral-700: #3f3f51;
  --neutral-800: #29293a;
  --neutral-900: #1a1a26;   /* primary text on light */
  --neutral-950: #0f0f17;

  /* Dedicated UI-control border that hits WCAG 3:1 on white.
     #767676 = 4.54:1 on #fff (per WEB_DESIGN_LAYOUT_TRENDS §20). */
  --control-border-light: #767676;

  /* ---- STATUS ramps ---- */
  /* success (green) */
  --green-50:  #f0fdf4; --green-100: #dcfce7; --green-200: #bbf7d0;
  --green-400: #4ade80; --green-500: #22c55e; --green-600: #16a34a;
  --green-700: #15803d; --green-800: #166534;
  /* warning (amber) */
  --amber-50:  #fffbeb; --amber-100: #fef3c7; --amber-200: #fde68a;
  --amber-400: #fbbf24; --amber-500: #f59e0b; --amber-600: #d97706;
  --amber-700: #b45309; --amber-800: #92400e;
  /* error (red/rose) */
  --red-50:  #fef2f2; --red-100: #fee2e2; --red-200: #fecaca;
  --red-400: #f87171; --red-500: #ef4444; --red-600: #dc2626;
  --red-700: #b91c1c; --red-800: #991b1b;
  /* info (blue) — reused for neutral informational states */
  --blue-50:  #eff6ff; --blue-100: #dbeafe; --blue-200: #bfdbfe;
  --blue-400: #60a5fa; --blue-500: #3b82f6; --blue-600: #2563eb;
  --blue-700: #1d4ed8;

  /* ---- TYPE SCALE — fluid clamp(), Perfect-Fourth (1.333), 16px base ----
     min  : mobile floor
     vw   : fluid growth term
     max  : desktop ceiling
     Steps are named by role so components stay semantic-ish. */
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
               'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'SF Mono', ui-monospace, Menlo, Consolas,
               'Liberation Mono', monospace;

  --text-xs:   clamp(0.75rem, 0.72rem + 0.15vw, 0.8125rem);   /* ~12 -> 13 */
  --text-sm:   clamp(0.875rem, 0.85rem + 0.15vw, 0.9375rem);  /* ~14 -> 15 */
  --text-base: 1rem;                                          /* 16 — inputs MUST be >=16 */
  --text-md:   clamp(1.0625rem, 1rem + 0.3vw, 1.125rem);      /* 17 -> 18 */
  --text-lg:   clamp(1.25rem, 1.15rem + 0.5vw, 1.5rem);       /* 20 -> 24 */
  --text-xl:   clamp(1.5rem, 1.3rem + 1vw, 2rem);             /* 24 -> 32 */
  --text-2xl:  clamp(1.9rem, 1.55rem + 1.75vw, 2.66rem);      /* ~30 -> 43 */
  --text-3xl:  clamp(2.4rem, 1.9rem + 2.5vw, 3.55rem);        /* ~38 -> 57 */
  --text-display: clamp(2.75rem, 1.9rem + 4.25vw, 4.7rem);    /* hero */

  --leading-tight:   1.15;
  --leading-snug:    1.3;
  --leading-normal:  1.6;
  --leading-relaxed: 1.75;
  --tracking-tight:  -0.02em;
  --tracking-wide:   0.04em;

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

  /* Optimal reading measure */
  --measure: 65ch;

  /* ---- SPACING SCALE (4px base) ---- */
  --space-0:   0;
  --space-1:   0.25rem;   /*  4 */
  --space-2:   0.5rem;    /*  8 */
  --space-3:   0.75rem;   /* 12 */
  --space-4:   1rem;      /* 16 */
  --space-5:   1.25rem;   /* 20 */
  --space-6:   1.5rem;    /* 24 */
  --space-8:   2rem;      /* 32 */
  --space-10:  2.5rem;    /* 40 */
  --space-12:  3rem;      /* 48 */
  --space-16:  4rem;      /* 64 */
  --space-20:  5rem;      /* 80 */
  --space-24:  6rem;      /* 96 */

  /* Fluid section padding (marketing) */
  --space-section: clamp(3rem, 8vw, 6rem);

  /* ---- RADIUS ---- */
  --radius-xs:   0.25rem;   /*  4 */
  --radius-sm:   0.375rem;  /*  6 */
  --radius-md:   0.5rem;    /*  8 — matches existing --radius */
  --radius-lg:   0.75rem;   /* 12 */
  --radius-xl:   1rem;      /* 16 */
  --radius-2xl:  1.5rem;    /* 24 — Proton-style rounded cards */
  --radius-pill: 999px;

  /* ---- SHADOWS (light-mode elevation) ---- */
  --shadow-xs: 0 1px 2px rgba(26, 26, 38, 0.06);
  --shadow-sm: 0 1px 3px rgba(26, 26, 38, 0.08), 0 1px 2px rgba(26, 26, 38, 0.06);
  --shadow-md: 0 4px 6px rgba(26, 26, 38, 0.07), 0 2px 4px rgba(26, 26, 38, 0.06);
  --shadow-lg: 0 12px 24px rgba(26, 26, 38, 0.10), 0 4px 8px rgba(26, 26, 38, 0.06);
  --shadow-xl: 0 24px 48px rgba(26, 26, 38, 0.14), 0 8px 16px rgba(26, 26, 38, 0.08);
  /* Brand glow for hero CTAs / focus moments */
  --shadow-brand: 0 8px 30px rgba(109, 40, 217, 0.25);
  --shadow-encrypt: 0 8px 30px rgba(6, 182, 212, 0.22);

  /* ---- MOTION / EASING ---- */
  --duration-instant: 75ms;
  --duration-fast:    150ms;
  --duration-base:    250ms;
  --duration-slow:    400ms;
  --ease-standard:    cubic-bezier(0.4, 0, 0.2, 1);
  --ease-out:         cubic-bezier(0, 0, 0.2, 1);
  --ease-in:          cubic-bezier(0.4, 0, 1, 1);
  --ease-spring:      cubic-bezier(0.34, 1.56, 0.64, 1);

  /* ---- Z-INDEX ladder ---- */
  --z-base:       0;
  --z-raised:     10;
  --z-sticky:     100;
  --z-dropdown:   200;
  --z-overlay:    900;
  --z-modal:      1000;
  --z-toast:      1100;

  /* ---- Layout primitives ---- */
  --container-max: 1100px;     /* matches app.css */
  --container-wide: 1280px;
  --sidebar-width: 16rem;
  --header-height: 3.5rem;     /* 56px — matches .nav-inner */
  --gutter: clamp(1rem, 3vw, 1.5rem);
}

/* ============================================================
   TIER 2 — SEMANTIC TOKENS  (LIGHT MODE — defaults)
   Components reference these only.
   ============================================================ */
:root {
  color-scheme: light;

  /* Surfaces & backgrounds (light = lower elements darker bg,
     raised elements lighter/white) */
  --color-bg:             var(--neutral-50);
  --color-surface:        var(--neutral-0);
  --color-surface-raised: var(--neutral-0);
  --color-surface-sunken: var(--neutral-100);
  --color-surface-muted:  var(--neutral-100);

  /* Text */
  --color-text:         var(--neutral-900);   /* 16:1 on bg */
  --color-text-muted:   var(--neutral-500);   /* #71718a -> >=4.5:1 on white */
  --color-text-subtle:  var(--neutral-400);   /* decorative only, not body copy */
  --color-text-inverse: var(--neutral-0);
  --color-text-on-brand: #ffffff;

  /* Borders */
  --color-border:         var(--neutral-200);
  --color-border-strong:  var(--neutral-300);
  --color-control-border: var(--control-border-light);  /* inputs/selects — AA */
  --color-border-focus:   var(--color-primary);

  /* Brand / primary (PURPLE) */
  --color-primary:          var(--purple-700);   /* #6D28D9 on white = AA */
  --color-primary-hover:    var(--purple-800);
  --color-primary-active:   var(--purple-900);
  --color-primary-subtle:   var(--purple-50);    /* tinted backgrounds */
  --color-primary-subtle-border: var(--purple-200);
  --color-primary-contrast: #ffffff;

  /* ENCRYPTION signal (TEAL) — locks, shields, "secure" badges.
     Use teal-700 for text/icons on light to satisfy AA. */
  --color-encrypt:          var(--teal-700);     /* #0e7490 = AA text on white */
  --color-encrypt-strong:   var(--teal-800);
  --color-encrypt-subtle:   var(--teal-50);
  --color-encrypt-subtle-border: var(--teal-200);
  --color-encrypt-contrast: #ffffff;
  --color-encrypt-bright:   var(--teal-500);     /* #06B6D4 — fills/gradients/glows */

  /* Status — text-safe (AA) variants for light mode */
  --color-success:         var(--green-700);
  --color-success-subtle:  var(--green-50);
  --color-success-border:  var(--green-200);
  --color-warning:         var(--amber-700);
  --color-warning-subtle:  var(--amber-50);
  --color-warning-border:  var(--amber-200);
  --color-error:           var(--red-600);
  --color-error-subtle:    var(--red-50);
  --color-error-border:    var(--red-200);
  --color-info:            var(--blue-600);
  --color-info-subtle:     var(--blue-50);
  --color-info-border:     var(--blue-200);

  /* Focus ring — purple, high-visibility, used everywhere */
  --color-focus-ring: var(--purple-600);

  /* Overlay scrim for modals/drawers */
  --color-overlay: rgba(15, 15, 23, 0.55);

  /* Brand gradient (hero CTAs, encryption flourishes) */
  --gradient-brand:   linear-gradient(135deg, var(--purple-700), var(--teal-500));
  --gradient-encrypt: linear-gradient(135deg, var(--teal-500), var(--purple-600));

  /* Elevation-aware shadow alias */
  --color-shadow: var(--shadow-sm);

  /* ---- BRIDGE: preset short-names -> semantic layer ----
     The public-form CSS and css_presets/* use these short token
     names. Aliasing them here makes the dark token set + presets
     first-class instead of !important hacks. A preset can override
     just these six and inherit correct light/dark behavior. */
  --primary: var(--color-primary);
  --error:   var(--color-error);
  --border:  var(--color-control-border);
  --text:    var(--color-text);
  --muted:   var(--color-text-muted);
  --bg:      var(--color-surface);
  --radius:  var(--radius-md);
}

/* ============================================================
   TIER 2 — DARK MODE
   Strategy: dark needs its OWN elevation model — surfaces get
   LIGHTER as they rise. Lighter brand/status hues for contrast.
   Applied both by system preference AND manual [data-theme].
   ============================================================ */

/* Shared dark token block, reused by media query + manual attr. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --color-bg:             #0c0c12;
    --color-surface:        #15151f;   /* +1 elevation */
    --color-surface-raised: #1d1d2a;   /* +2 (cards/modals) */
    --color-surface-sunken: #0a0a10;   /* recessed panels */
    --color-surface-muted:  #14141d;

    --color-text:         #f1f1f6;
    --color-text-muted:   #a9a9bd;     /* >=4.5:1 on dark surfaces */
    --color-text-subtle:  #74748a;
    --color-text-inverse: #0c0c12;
    --color-text-on-brand:#0c0c12;

    --color-border:         rgba(255, 255, 255, 0.10);
    --color-border-strong:  rgba(255, 255, 255, 0.18);
    --color-control-border: #8b8b9e;   /* >=3:1 on dark surface */
    --color-border-focus:   var(--color-primary);

    /* Lighter purple so it pops on dark and stays AA */
    --color-primary:          var(--purple-400);   /* #a78bfa */
    --color-primary-hover:    var(--purple-300);
    --color-primary-active:   var(--purple-200);
    --color-primary-subtle:   rgba(139, 92, 246, 0.14);
    --color-primary-subtle-border: rgba(167, 139, 250, 0.35);
    --color-primary-contrast: var(--neutral-950);

    /* Encryption teal — brighter in dark */
    --color-encrypt:          var(--teal-400);      /* #22d3ee */
    --color-encrypt-strong:   var(--teal-300);
    --color-encrypt-subtle:   rgba(6, 182, 212, 0.14);
    --color-encrypt-subtle-border: rgba(34, 211, 238, 0.35);
    --color-encrypt-contrast: var(--neutral-950);
    --color-encrypt-bright:   var(--teal-400);

    --color-success:         var(--green-400);
    --color-success-subtle:  rgba(34, 197, 94, 0.14);
    --color-success-border:  rgba(74, 222, 128, 0.35);
    --color-warning:         var(--amber-400);
    --color-warning-subtle:  rgba(245, 158, 11, 0.15);
    --color-warning-border:  rgba(251, 191, 36, 0.35);
    --color-error:           var(--red-400);
    --color-error-subtle:    rgba(239, 68, 68, 0.15);
    --color-error-border:    rgba(248, 113, 113, 0.35);
    --color-info:            var(--blue-400);
    --color-info-subtle:     rgba(59, 130, 246, 0.15);
    --color-info-border:     rgba(96, 165, 250, 0.35);

    --color-focus-ring: var(--purple-300);
    --color-overlay: rgba(0, 0, 0, 0.66);

    /* Dark elevation shadows: shadows barely read on dark, so we
       lean on borders; keep soft depth + faint top highlight. */
    --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.5);
    --shadow-md: 0 4px 10px rgba(0, 0, 0, 0.55);
    --shadow-lg: 0 14px 30px rgba(0, 0, 0, 0.6);
    --shadow-xl: 0 26px 52px rgba(0, 0, 0, 0.65);

    /* keep bridge aliases pointed at semantic layer (re-resolved) */
    --border: var(--color-control-border);
    --text:   var(--color-text);
    --muted:  var(--color-text-muted);
    --bg:     var(--color-surface);
  }
}

/* Manual override — user toggled dark explicitly (localStorage).
   Mirrors the media block; wins over system because attr is set. */
[data-theme="dark"] {
  color-scheme: dark;

  --color-bg:             #0c0c12;
  --color-surface:        #15151f;
  --color-surface-raised: #1d1d2a;
  --color-surface-sunken: #0a0a10;
  --color-surface-muted:  #14141d;

  --color-text:         #f1f1f6;
  --color-text-muted:   #a9a9bd;
  --color-text-subtle:  #74748a;
  --color-text-inverse: #0c0c12;
  --color-text-on-brand:#0c0c12;

  --color-border:         rgba(255, 255, 255, 0.10);
  --color-border-strong:  rgba(255, 255, 255, 0.18);
  --color-control-border: #8b8b9e;
  --color-border-focus:   var(--color-primary);

  --color-primary:          var(--purple-400);
  --color-primary-hover:    var(--purple-300);
  --color-primary-active:   var(--purple-200);
  --color-primary-subtle:   rgba(139, 92, 246, 0.14);
  --color-primary-subtle-border: rgba(167, 139, 250, 0.35);
  --color-primary-contrast: var(--neutral-950);

  --color-encrypt:          var(--teal-400);
  --color-encrypt-strong:   var(--teal-300);
  --color-encrypt-subtle:   rgba(6, 182, 212, 0.14);
  --color-encrypt-subtle-border: rgba(34, 211, 238, 0.35);
  --color-encrypt-contrast: var(--neutral-950);
  --color-encrypt-bright:   var(--teal-400);

  --color-success:         var(--green-400);
  --color-success-subtle:  rgba(34, 197, 94, 0.14);
  --color-success-border:  rgba(74, 222, 128, 0.35);
  --color-warning:         var(--amber-400);
  --color-warning-subtle:  rgba(245, 158, 11, 0.15);
  --color-warning-border:  rgba(251, 191, 36, 0.35);
  --color-error:           var(--red-400);
  --color-error-subtle:    rgba(239, 68, 68, 0.15);
  --color-error-border:    rgba(248, 113, 113, 0.35);
  --color-info:            var(--blue-400);
  --color-info-subtle:     rgba(59, 130, 246, 0.15);
  --color-info-border:     rgba(96, 165, 250, 0.35);

  --color-focus-ring: var(--purple-300);
  --color-overlay: rgba(0, 0, 0, 0.66);

  --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.5);
  --shadow-md: 0 4px 10px rgba(0, 0, 0, 0.55);
  --shadow-lg: 0 14px 30px rgba(0, 0, 0, 0.6);
  --shadow-xl: 0 26px 52px rgba(0, 0, 0, 0.65);

  --border: var(--color-control-border);
  --text:   var(--color-text);
  --muted:  var(--color-text-muted);
  --bg:     var(--color-surface);
}

/* Explicit light reset: user toggled light while system is dark.
   Tokens already default to light at :root, so this just locks
   color-scheme so native UI (scrollbars/inputs) stays light. */
[data-theme="light"] { color-scheme: light; }

/* ============================================================
   GLOBAL A11Y BASELINE
   ============================================================ */

/* Strong, visible focus on every focusable element. */
*:focus-visible {
  outline: 3px solid var(--color-focus-ring);
  outline-offset: 2px;
  border-radius: var(--radius-xs);
}
/* Don't double up when a component manages its own focus ring. */
*:focus:not(:focus-visible) { outline: none; }

/* Respect reduced-motion globally. */
@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;
  }
}

/* Screen-reader-only utility. */
.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
/* Becomes visible when focused (e.g. skip links). */
.sr-only-focusable:focus,
.sr-only-focusable:focus-within {
  position: static !important;
  width: auto; height: auto;
  margin: 0; overflow: visible;
  clip: auto; white-space: normal;
}

/* Skip-link: hidden until focused, then pinned top-left. */
.skip-link {
  position: absolute;
  top: 0; left: 0;
  z-index: var(--z-toast);
  transform: translateY(-150%);
  padding: var(--space-3) var(--space-4);
  background: var(--color-primary);
  color: var(--color-primary-contrast);
  font: var(--weight-semibold) var(--text-base) / 1 var(--font-sans);
  text-decoration: none;
  border-radius: 0 0 var(--radius-md) 0;
  transition: transform var(--duration-fast) var(--ease-out);
}
.skip-link:focus {
  transform: translateY(0);
  outline: 3px solid var(--color-focus-ring);
  outline-offset: 2px;
}
