/*
 * host123 Webmail design tokens (ARCHITECTURE §29.1).
 *
 * This is the ONLY stylesheet that may contain literal colours. Every other stylesheet (core and
 * module CSS) uses var(--…) tokens, so light, dark and auto themes switch by changing the values
 * below. Theme selection: <html data-theme="light|dark|auto">; a missing attribute behaves like
 * "auto" (follows prefers-color-scheme).
 *
 * The WCAG contrast of every text/background pair is verified by tests/js/design/contrast.test.mjs.
 */

:root {
  color-scheme: light;

  /* Typography */
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --fs-xs: 12px; --fs-sm: 13px; --fs-md: 14px; --fs-lg: 16px; --fs-xl: 18px; --fs-2xl: 20px; --fs-3xl: 24px;
  --lh: 1.45; --lh-tight: 1.2;
  --fw-normal: 400; --fw-medium: 500; --fw-semibold: 600; --fw-bold: 700;

  /* Spacing and radii */
  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px; --space-5: 20px; --space-6: 24px; --space-8: 32px;
  --radius-sm: 6px; --radius-md: 10px; --radius-lg: 12px; --radius-xl: 16px; --radius-full: 999px;

  /* Palette: host123 purple brand, fuchsia accent, semantic hues */
  --brand-50: #faf5ff; --brand-100: #f3e8ff; --brand-200: #e9d5ff; --brand-300: #d8b4fe; --brand-400: #c084fc;
  --brand-500: #a855f7; --brand-600: #9333ea; --brand-700: #7e22ce; --brand-800: #6b21a8; --brand-900: #581c87;
  --brand-950: #3b0764;
  --accent-500: #d946ef; --accent-600: #c026d3; --accent-700: #a21caf;
  --sky-500: #0ea5e9;
  --success-600: #059669; --warning-600: #d97706; --danger-600: #e11d48; --info-600: #0284c7;
  --success-700: #047857; --warning-700: #b45309; --danger-700: #be123c; --info-700: #0369a1;

  /* App background and glass surfaces */
  --bg-app: linear-gradient(160deg, #eef2ff 0%, #faf5ff 35%, #fdf4ff 70%, #eff6ff 100%);
  --bg-canvas: #f5f3ff;
  --surface: rgba(255,255,255,.72); --surface-strong: rgba(255,255,255,.92); --surface-sunken: rgba(241,245,249,.8);
  --glass-bg: var(--surface);
  --border: rgba(148,163,184,.35); --border-strong: rgba(100,116,139,.5);

  /* Text */
  --text: #1e293b;
  /* Darker than slate-500 so muted text keeps 4.5:1 on sunken surfaces and on selected rows over the
     bare app background (the sidebar at >= 1280 px); see docs/packages/P04a.md. */
  --text-muted: #546377;
  --text-inverse: #ffffff;
  --link: var(--brand-700);
  --brand-text: var(--brand-700);

  /* Semantic colours: -text on -soft background with a -line border (tinted banners, badges, chips) */
  --success-text: var(--success-700); --success-soft: #ecfdf5; --success-line: #a7f3d0;
  --warning-text: var(--warning-700); --warning-soft: #fffbeb; --warning-line: #fde68a;
  --danger-text: var(--danger-700);   --danger-soft: #fff1f2;  --danger-line: #fecdd3;
  --info-text: var(--info-700);       --info-soft: #f0f9ff;    --info-line: #bae6fd;
  --brand-soft: var(--brand-50);      --brand-line: var(--brand-200);
  /* Solid fills. Only --danger-solid(-hover) may carry text (--text-inverse, 4.5:1); the others are
     non-text fills (meters, dots, toast accents): white text on them fails WCAG. */
  --danger-solid: var(--danger-600); --danger-solid-hover: var(--danger-700);
  --success-solid: var(--success-600); --warning-solid: var(--warning-600); --info-solid: var(--info-600);

  /* Focus and interaction */
  --focus-color: var(--brand-500);
  --focus-ring: 0 0 0 2px var(--brand-500);
  --focus-ring-danger: 0 0 0 2px var(--danger-600);
  --selection: var(--brand-100); --row-hover: rgba(147,51,234,.06); --row-selected: rgba(147,51,234,.12); --unread-dot: var(--brand-600);
  --flag-color: var(--danger-600);

  /* Controls */
  --btn-primary-bg: linear-gradient(90deg, var(--brand-600), var(--accent-600));
  --btn-primary-bg-hover: linear-gradient(90deg, var(--brand-700), var(--accent-700));
  --btn-split-sep: rgba(255,255,255,.35);
  --on-brand-hover: rgba(15,23,42,.2);
  --input-bg: rgba(255,255,255,.9);
  --input-border: #8492a6;
  --input-border-hover: #64748b;
  --switch-off: #8492a6;
  --switch-on: var(--brand-600);
  --switch-thumb: #ffffff;
  --check-accent: var(--brand-600);

  /* Overlays and misc components */
  --backdrop: rgba(15,23,42,.45);
  --tooltip-bg: #1e293b; --tooltip-text: #f8fafc;
  --toast-action: var(--brand-300); --on-inverse-hover: rgba(255,255,255,.12);
  --kbd-bg: #f8fafc; --kbd-border: rgba(100,116,139,.45);
  --skeleton-base: rgba(148,163,184,.22); --skeleton-shine: rgba(255,255,255,.55);
  --scrollbar-thumb: rgba(100,116,139,.45);
  --progress-track: rgba(148,163,184,.3);
  --badge-lock-bg: linear-gradient(90deg, var(--brand-600), var(--accent-600));
  /* The lock badge on a filled (gradient) button: a light translucent pill (same in both themes). */
  --badge-lock-on-fill: rgba(255, 255, 255, .24);
  --dropzone-bg: rgba(147,51,234,.05);

  /* Avatar colours (initials in --text-inverse); the index comes from a hash of the e-mail address */
  --avatar-1: #7e22ce; --avatar-2: #a21caf; --avatar-3: #be123c; --avatar-4: #b45309;
  --avatar-5: #047857; --avatar-6: #0f766e; --avatar-7: #0369a1; --avatar-8: #4338ca;

  /* Elevation, effects, stacking, motion */
  --shadow-sm: 0 1px 2px rgba(15,23,42,.06); --shadow-md: 0 8px 24px rgba(15,23,42,.08); --shadow-lg: 0 16px 48px rgba(15,23,42,.14);
  --blur: saturate(160%) blur(16px);
  --z-dropdown: 100; --z-sticky: 200; --z-dock: 250; --z-drawer: 300; --z-modal: 400; --z-toast: 500; --z-tooltip: 600;
  /* Menus and popovers are appended to <body> (a glass ancestor with backdrop-filter would capture
     position: fixed), so they stack above the dock, drawers and modals they are opened from. */
  --z-popover: 450;
  --dur-fast: 120ms; --dur: 200ms; --dur-slow: 320ms; --ease: cubic-bezier(.2,.8,.2,1);

  /* Sizes */
  --topbar-h: 56px; --nav-w: 64px; --sidebar-w: 248px; --list-w: 380px; --list-h: 42%;
  --bottomnav-h: 60px; --drawer-w: 300px;
  --compose-w: 560px; --compose-h: 640px; --compose-bar-h: 44px;
  --btn-h: 36px; --btn-h-sm: 30px; --btn-h-lg: 44px; --touch-min: 40px;
  --icon-xs: 14px; --icon-sm: 16px; --icon-md: 20px; --icon-lg: 24px;
  --msg-row-h: 80px; --msg-row-h-wide: 44px;

  /* Print (forced light, ink friendly) */
  --print-text: #000000; --print-muted: #404040; --print-bg: #ffffff; --print-border: #999999;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg-app: linear-gradient(160deg, #0f172a 0%, #1e1b4b 45%, #2e1065 100%);
  --bg-canvas: #0f172a;
  --surface: rgba(15,23,42,.55); --surface-strong: rgba(15,23,42,.92); --surface-sunken: rgba(2,6,23,.5);
  --border: rgba(255,255,255,.10); --border-strong: rgba(255,255,255,.2);
  --text: #f1f5f9; --text-muted: #94a3b8; --link: var(--brand-300); --brand-text: var(--brand-300);
  --success-text: #34d399; --success-soft: rgba(16,185,129,.14); --success-line: rgba(52,211,153,.35);
  --warning-text: #fbbf24; --warning-soft: rgba(245,158,11,.14); --warning-line: rgba(251,191,36,.35);
  --danger-text: #fb7185;  --danger-soft: rgba(244,63,94,.14);   --danger-line: rgba(251,113,133,.35);
  --info-text: #38bdf8;    --info-soft: rgba(14,165,233,.14);    --info-line: rgba(56,189,248,.35);
  --brand-soft: rgba(168,85,247,.16); --brand-line: rgba(192,132,252,.35);
  --focus-color: var(--brand-400);
  --focus-ring: 0 0 0 2px var(--brand-400);
  --focus-ring-danger: 0 0 0 2px #fb7185;
  --selection: rgba(168,85,247,.25); --row-hover: rgba(168,85,247,.10); --row-selected: rgba(168,85,247,.2); --unread-dot: var(--brand-400);
  --flag-color: #fb7185;
  --input-bg: rgba(2,6,23,.45);
  --input-border: #64748b;
  --input-border-hover: #94a3b8;
  --switch-off: #64748b;
  --switch-thumb: #f8fafc;
  --check-accent: var(--brand-400);
  --backdrop: rgba(2,6,23,.65);
  --tooltip-bg: #f1f5f9; --tooltip-text: #0f172a;
  --toast-action: var(--brand-700); --on-inverse-hover: rgba(15,23,42,.08);
  --kbd-bg: rgba(30,41,59,.9); --kbd-border: rgba(255,255,255,.2);
  --skeleton-base: rgba(148,163,184,.14); --skeleton-shine: rgba(255,255,255,.08);
  --scrollbar-thumb: rgba(148,163,184,.35);
  --progress-track: rgba(148,163,184,.2);
  --dropzone-bg: rgba(168,85,247,.08);
  --shadow-md: 0 8px 24px rgba(0,0,0,.35); --shadow-lg: 0 16px 48px rgba(0,0,0,.5);
}

/* "auto" (and a missing attribute) follows the operating system. Keep in sync with the block above:
   tests/js/design/contrast.test.mjs fails when the two blocks differ. */
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"],
  :root:not([data-theme]) {
    color-scheme: dark;
    --bg-app: linear-gradient(160deg, #0f172a 0%, #1e1b4b 45%, #2e1065 100%);
    --bg-canvas: #0f172a;
    --surface: rgba(15,23,42,.55); --surface-strong: rgba(15,23,42,.92); --surface-sunken: rgba(2,6,23,.5);
    --border: rgba(255,255,255,.10); --border-strong: rgba(255,255,255,.2);
    --text: #f1f5f9; --text-muted: #94a3b8; --link: var(--brand-300); --brand-text: var(--brand-300);
    --success-text: #34d399; --success-soft: rgba(16,185,129,.14); --success-line: rgba(52,211,153,.35);
    --warning-text: #fbbf24; --warning-soft: rgba(245,158,11,.14); --warning-line: rgba(251,191,36,.35);
    --danger-text: #fb7185;  --danger-soft: rgba(244,63,94,.14);   --danger-line: rgba(251,113,133,.35);
    --info-text: #38bdf8;    --info-soft: rgba(14,165,233,.14);    --info-line: rgba(56,189,248,.35);
    --brand-soft: rgba(168,85,247,.16); --brand-line: rgba(192,132,252,.35);
    --focus-color: var(--brand-400);
    --focus-ring: 0 0 0 2px var(--brand-400);
    --focus-ring-danger: 0 0 0 2px #fb7185;
    --selection: rgba(168,85,247,.25); --row-hover: rgba(168,85,247,.10); --row-selected: rgba(168,85,247,.2); --unread-dot: var(--brand-400);
    --flag-color: #fb7185;
    --input-bg: rgba(2,6,23,.45);
    --input-border: #64748b;
    --input-border-hover: #94a3b8;
    --switch-off: #64748b;
    --switch-thumb: #f8fafc;
    --check-accent: var(--brand-400);
    --backdrop: rgba(2,6,23,.65);
    --tooltip-bg: #f1f5f9; --tooltip-text: #0f172a;
    --toast-action: var(--brand-700); --on-inverse-hover: rgba(15,23,42,.08);
    --kbd-bg: rgba(30,41,59,.9); --kbd-border: rgba(255,255,255,.2);
    --skeleton-base: rgba(148,163,184,.14); --skeleton-shine: rgba(255,255,255,.08);
    --scrollbar-thumb: rgba(148,163,184,.35);
    --progress-track: rgba(148,163,184,.2);
    --dropzone-bg: rgba(168,85,247,.08);
    --shadow-md: 0 8px 24px rgba(0,0,0,.35); --shadow-lg: 0 16px 48px rgba(0,0,0,.5);
  }
}

/* Glass fallback (§29.1): without backdrop-filter translucent surfaces would be hard to read. */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  :root,
  :root[data-theme] {
    --glass-bg: var(--surface-strong);
  }
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --dur-fast: 0ms; --dur: 0ms; --dur-slow: 0ms;
  }
}

/* Printing always uses the light, ink-friendly palette whatever the screen theme is: every token the
   dark theme overrides is reset here (tests/js/design/contrast.test.mjs checks the list). */
@media print {
  :root,
  :root[data-theme],
  :root:not([data-theme]) {
    color-scheme: light;
    --bg-app: #ffffff;
    --bg-canvas: #ffffff;
    --surface: #ffffff; --surface-strong: #ffffff; --surface-sunken: #ffffff; --glass-bg: #ffffff;
    --border: #999999; --border-strong: #666666;
    --text: #000000; --text-muted: #404040; --link: #000000; --brand-text: #000000;
    --success-text: var(--success-700); --success-soft: #ffffff; --success-line: #999999;
    --warning-text: var(--warning-700); --warning-soft: #ffffff; --warning-line: #999999;
    --danger-text: var(--danger-700);   --danger-soft: #ffffff;  --danger-line: #999999;
    --info-text: var(--info-700);       --info-soft: #ffffff;    --info-line: #999999;
    --brand-soft: #ffffff; --brand-line: #999999;
    --focus-color: #000000; --focus-ring: none; --focus-ring-danger: none;
    --selection: transparent; --row-hover: transparent; --row-selected: transparent; --unread-dot: #000000;
    --flag-color: #000000;
    --input-bg: #ffffff; --input-border: #666666; --input-border-hover: #666666;
    --switch-off: #666666; --switch-thumb: #ffffff; --check-accent: #000000;
    --backdrop: transparent;
    --tooltip-bg: #ffffff; --tooltip-text: #000000;
    --toast-action: #000000; --on-inverse-hover: transparent;
    --kbd-bg: #ffffff; --kbd-border: #999999;
    --skeleton-base: transparent; --skeleton-shine: transparent;
    --scrollbar-thumb: #999999;
    --progress-track: #dddddd;
    --dropzone-bg: transparent;
    --shadow-sm: none; --shadow-md: none; --shadow-lg: none;
  }
}
