/* ==========================================================================
   DESIGN TOKENS + BASE
   --------------------------------------------------------------------------
   Every colour in this project is a semantic token defined here. No component
   file contains a raw hex value, so rebranding means editing this block only.

   Theme resolution, in priority order:
     1. html[data-theme="dark"]  / html[data-theme="light"]  — explicit choice
     2. prefers-color-scheme: dark                            — system default
     3. bare :root                                            — light fallback
   ========================================================================== */

:root {
  /* --- Surfaces --------------------------------------------------------- */
  --bg: #ffffff;
  --surface: #f8fafc;
  --surface-2: #f1f5f9;
  --surface-raised: #ffffff;

  /* --- Text ------------------------------------------------------------- */
  --text: #0b1220;          /* 17.4:1 on --bg */
  --text-muted: #52627a;    /*  7.1:1 on --bg */
  --text-subtle: #64748b;   /*  5.3:1 on --bg */
  --text-on-primary: #ffffff;

  /* --- Lines ------------------------------------------------------------ */
  --border: #e2e8f0;
  --border-strong: #cbd5e1;

  /* --- Brand ------------------------------------------------------------ */
  --primary: #4338ca;       /*  8.6:1 on white — safe for text and icons */
  --primary-hover: #3730a3;
  --primary-soft: #eef2ff;
  --primary-soft-text: #3730a3;

  --accent: #0e7490;
  --accent-soft: #ecfeff;

  /* --- Status ----------------------------------------------------------- */
  --online: #047857;
  --online-soft: #ecfdf5;
  --busy: #b45309;
  --busy-soft: #fffbeb;
  --offline: #64748b;
  --offline-soft: #f1f5f9;
  --danger: #b91c1c;
  --danger-soft: #fef2f2;

  /* --- Chat bubbles ----------------------------------------------------- */
  --bubble-in: #f1f5f9;
  --bubble-in-text: #0b1220;
  --bubble-out: #4338ca;
  --bubble-out-text: #ffffff;
  --bubble-bot: #eef2ff;
  --bubble-bot-text: #1e1b4b;

  /* --- Elevation -------------------------------------------------------- */
  --shadow-sm: 0 1px 2px rgb(15 23 42 / 0.06), 0 1px 3px rgb(15 23 42 / 0.08);
  --shadow-md: 0 4px 6px -2px rgb(15 23 42 / 0.05), 0 10px 15px -3px rgb(15 23 42 / 0.1);
  --shadow-lg: 0 10px 20px -5px rgb(15 23 42 / 0.08), 0 20px 40px -12px rgb(15 23 42 / 0.18);
  --shadow-widget: 0 12px 28px -8px rgb(15 23 42 / 0.18), 0 32px 64px -16px rgb(15 23 42 / 0.24);
  --scrim: rgb(2 6 23 / 0.55);

  /* --- Radii ------------------------------------------------------------ */
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --r-xl: 24px;
  --r-full: 999px;

  /* --- Type ------------------------------------------------------------- */
  --font-heading: 'Plus Jakarta Sans', ui-sans-serif, system-ui, -apple-system,
    'Segoe UI', Roboto, sans-serif;
  --font-body: 'Inter', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto,
    sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;

  /* Fluid scale: readable at 375px, generous at 1440px. */
  --t-xs: 0.75rem;
  --t-sm: 0.875rem;
  --t-base: 1rem;
  --t-lg: 1.125rem;
  --t-xl: clamp(1.25rem, 1.1rem + 0.6vw, 1.5rem);
  --t-2xl: clamp(1.5rem, 1.3rem + 1vw, 2rem);
  --t-3xl: clamp(1.875rem, 1.5rem + 1.8vw, 2.75rem);
  --t-4xl: clamp(2.25rem, 1.6rem + 3vw, 3.75rem);

  /* --- Spacing (4px rhythm) --------------------------------------------- */
  --s-1: 0.25rem;
  --s-2: 0.5rem;
  --s-3: 0.75rem;
  --s-4: 1rem;
  --s-5: 1.25rem;
  --s-6: 1.5rem;
  --s-8: 2rem;
  --s-10: 2.5rem;
  --s-12: 3rem;
  --s-16: 4rem;
  --s-20: 5rem;
  --s-24: 6rem;

  /* --- Motion ----------------------------------------------------------- */
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-in: cubic-bezier(0.4, 0, 1, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --d-fast: 150ms;
  --d-base: 220ms;
  --d-slow: 320ms;

  /* --- Layout ----------------------------------------------------------- */
  --container: 1120px;
  --container-wide: 1280px;
  --header-h: 68px;

  /* --- Z-index scale ---------------------------------------------------- */
  --z-header: 40;
  --z-widget: 90;
  --z-widget-panel: 95;
  --z-modal: 100;
  --z-toast: 120;
}

/* --- Dark, from the system setting ---------------------------------------- */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #0a0f1c;
    --surface: #111827;
    --surface-2: #1a2438;
    --surface-raised: #151d2f;

    --text: #e8edf7;          /* 14.8:1 on --bg */
    --text-muted: #9aa8be;    /*  7.6:1 on --bg */
    --text-subtle: #7f8da5;   /*  5.4:1 on --bg */
    --text-on-primary: #0a0f1c;

    --border: #243047;
    --border-strong: #33415c;

    --primary: #a5b4fc;       /* lightened, not inverted — keeps 9:1 on --bg */
    --primary-hover: #c7d2fe;
    --primary-soft: #1e1b4b;
    --primary-soft-text: #c7d2fe;

    --accent: #67e8f9;
    --accent-soft: #083344;

    --online: #34d399;
    --online-soft: #052e21;
    --busy: #fbbf24;
    --busy-soft: #3a2a06;
    --offline: #94a3b8;
    --offline-soft: #1a2438;
    --danger: #fca5a5;
    --danger-soft: #3f1414;

    --bubble-in: #1a2438;
    --bubble-in-text: #e8edf7;
    --bubble-out: #a5b4fc;
    --bubble-out-text: #10142a;
    --bubble-bot: #1e2749;
    --bubble-bot-text: #dbe2fb;

    --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
    --shadow-md: 0 4px 12px rgb(0 0 0 / 0.45);
    --shadow-lg: 0 16px 40px rgb(0 0 0 / 0.55);
    --shadow-widget: 0 16px 48px rgb(0 0 0 / 0.6);
    --scrim: rgb(0 0 0 / 0.7);
  }
}

/* --- Dark, chosen explicitly ---------------------------------------------- */
:root[data-theme='dark'] {
  --bg: #0a0f1c;
  --surface: #111827;
  --surface-2: #1a2438;
  --surface-raised: #151d2f;

  --text: #e8edf7;
  --text-muted: #9aa8be;
  --text-subtle: #7f8da5;
  --text-on-primary: #0a0f1c;

  --border: #243047;
  --border-strong: #33415c;

  --primary: #a5b4fc;
  --primary-hover: #c7d2fe;
  --primary-soft: #1e1b4b;
  --primary-soft-text: #c7d2fe;

  --accent: #67e8f9;
  --accent-soft: #083344;

  --online: #34d399;
  --online-soft: #052e21;
  --busy: #fbbf24;
  --busy-soft: #3a2a06;
  --offline: #94a3b8;
  --offline-soft: #1a2438;
  --danger: #fca5a5;
  --danger-soft: #3f1414;

  --bubble-in: #1a2438;
  --bubble-in-text: #e8edf7;
  --bubble-out: #a5b4fc;
  --bubble-out-text: #10142a;
  --bubble-bot: #1e2749;
  --bubble-bot-text: #dbe2fb;

  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
  --shadow-md: 0 4px 12px rgb(0 0 0 / 0.45);
  --shadow-lg: 0 16px 40px rgb(0 0 0 / 0.55);
  --shadow-widget: 0 16px 48px rgb(0 0 0 / 0.6);
  --scrim: rgb(0 0 0 / 0.7);
}

/* ==========================================================================
   RESET
   ========================================================================== */

*,
*::before,
*::after {
  box-sizing: border-box;
}

/*
 * The `hidden` attribute must always win.
 *
 * Browsers implement it as `[hidden] { display: none }` in the UA stylesheet,
 * which any author rule of equal specificity overrides — so a component styled
 * `.thing { display: grid }` stays fully visible even after `el.hidden = true`.
 * That silently breaks every show/hide in the app, and it does not show up when
 * tests assert on `el.hidden`, because the attribute really is set.
 *
 * `!important` here is deliberate and is the standard defence.
 */
[hidden] {
  display: none !important;
}

* {
  margin: 0;
}

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  /* Anchor links must not slide under the sticky header. */
  scroll-padding-top: calc(var(--header-h) + var(--s-4));
}

body {
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: var(--t-base);
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

img,
picture,
svg,
video {
  display: block;
  max-width: 100%;
}

svg {
  flex-shrink: 0;
}

input,
button,
textarea,
select {
  font: inherit;
  color: inherit;
}

button {
  background: none;
  border: none;
  cursor: pointer;
}

h1,
h2,
h3,
h4 {
  font-family: var(--font-heading);
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.02em;
  text-wrap: balance;
}

p {
  text-wrap: pretty;
}

a {
  color: var(--primary);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

ul,
ol {
  padding: 0;
  list-style: none;
}

/* ==========================================================================
   FOCUS — visible, consistent, never removed
   ========================================================================== */

:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* Only suppress the ring for pointer users who already see a hover state. */
:focus:not(:focus-visible) {
  outline: none;
}

.skip-link {
  position: absolute;
  left: var(--s-4);
  top: -100px;
  z-index: var(--z-toast);
  padding: var(--s-3) var(--s-5);
  background: var(--primary);
  color: var(--text-on-primary);
  border-radius: var(--r-md);
  font-weight: 600;
  transition: top var(--d-fast) var(--ease-out);
}

.skip-link:focus {
  top: var(--s-4);
}

/* ==========================================================================
   PRIMITIVES
   ========================================================================== */

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

@media (min-width: 768px) {
  .container {
    padding-inline: var(--s-8);
  }
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* --- Buttons -------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  /* 44px minimum touch target, per WCAG + Apple HIG. */
  min-height: 44px;
  padding: var(--s-3) var(--s-5);
  border-radius: var(--r-md);
  font-weight: 600;
  font-size: var(--t-sm);
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
  border: 1px solid transparent;
  /* Only transform and opacity animate, so nothing triggers layout. */
  transition:
    background-color var(--d-fast) var(--ease-out),
    border-color var(--d-fast) var(--ease-out),
    transform var(--d-fast) var(--ease-out),
    box-shadow var(--d-fast) var(--ease-out);
  touch-action: manipulation;
}

.btn:active:not(:disabled) {
  transform: scale(0.97);
}

.btn:disabled,
.btn[aria-disabled='true'] {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--primary);
  color: var(--text-on-primary);
  box-shadow: var(--shadow-sm);
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
  box-shadow: var(--shadow-md);
}

.btn-secondary {
  background: var(--surface-raised);
  color: var(--text);
  border-color: var(--border-strong);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: var(--primary);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text);
}

.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: transparent;
}

.btn-danger:hover:not(:disabled) {
  background: var(--danger);
  color: var(--bg);
}

.btn-lg {
  min-height: 52px;
  padding: var(--s-4) var(--s-8);
  font-size: var(--t-base);
  border-radius: var(--r-lg);
}

.btn-icon {
  min-height: 44px;
  min-width: 44px;
  padding: var(--s-2);
  border-radius: var(--r-md);
}

/* --- Status pill ---------------------------------------------------------- */

.status {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  padding: var(--s-1) var(--s-3);
  border-radius: var(--r-full);
  font-size: var(--t-xs);
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--r-full);
  background: currentColor;
  flex-shrink: 0;
}

/* State is carried by an icon-free dot AND the text label, never colour alone. */
.status[data-state='online'] {
  background: var(--online-soft);
  color: var(--online);
}

.status[data-state='online'] .status-dot {
  animation: pulse 2s var(--ease-out) infinite;
}

.status[data-state='busy'] {
  background: var(--busy-soft);
  color: var(--busy);
}

.status[data-state='offline'] {
  background: var(--offline-soft);
  color: var(--offline);
}

@keyframes pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 currentColor;
    opacity: 1;
  }
  70% {
    box-shadow: 0 0 0 6px transparent;
    opacity: 0.85;
  }
}

/* --- Cards ---------------------------------------------------------------- */

.card {
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--s-6);
  box-shadow: var(--shadow-sm);
}

/* --- Form fields ---------------------------------------------------------- */

.field {
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
}

.field label {
  font-size: var(--t-sm);
  font-weight: 600;
  color: var(--text);
}

.field .hint {
  font-size: var(--t-xs);
  color: var(--text-muted);
}

.field .error {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  font-size: var(--t-xs);
  font-weight: 500;
  color: var(--danger);
}

.input {
  width: 100%;
  /* 44px keeps mobile taps comfortable; 16px font stops iOS auto-zoom. */
  min-height: 44px;
  padding: var(--s-3) var(--s-4);
  font-size: var(--t-base);
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  transition:
    border-color var(--d-fast) var(--ease-out),
    box-shadow var(--d-fast) var(--ease-out);
}

.input::placeholder {
  color: var(--text-subtle);
}

.input:hover {
  border-color: var(--text-subtle);
}

.input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 22%, transparent);
}

.input[aria-invalid='true'] {
  border-color: var(--danger);
}

.input[aria-invalid='true']:focus {
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 22%, transparent);
}

/* ==========================================================================
   REDUCED MOTION
   Honour the OS setting: keep the interface functional, drop the movement.
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ==========================================================================
   PRINT — a visitor printing a transcript should get something readable
   ========================================================================== */

@media print {
  .site-header,
  .chat-launcher,
  .chat-panel,
  .site-footer nav {
    display: none !important;
  }

  body {
    background: #fff;
    color: #000;
  }
}
