/* ==========================================================================
   SwiftsReader design system — v1
   One shared stylesheet for every marketing page, the school portal and the
   reader app. Link it BEFORE any page-level <style> so page rules still win
   while pages are migrated (everything here lives in cascade layers, and
   unlayered page CSS always beats layered CSS).

   Rules of thumb
   - Use tokens (--ds-*), never raw hex values, in new CSS.
   - Terracotta 600 (--ds-primary) is the action colour. Terracotta 500 is
     decorative only: white on it is 3.1:1, which fails WCAG AA for text.
   - Components are namespaced .ds-* so they can't collide with legacy CSS.
   ========================================================================== */

@layer ds-tokens, ds-base, ds-components, ds-utilities;

/* ---------- 1. Tokens ---------- */
@layer ds-tokens {
  :root {
    /* Terracotta scale (brand) */
    --ds-terra-50:  #fdf3ea;
    --ds-terra-100: #fbe3cf;
    --ds-terra-200: #f6c4a0;
    --ds-terra-300: #ffb489;
    --ds-terra-400: #f98a5a;
    --ds-terra-500: #f4633a;   /* decorative only */
    --ds-terra-600: #c2410c;   /* primary action — 5.18:1 with white */
    --ds-terra-700: #a8340a;   /* hover / text on cream — 6.2:1 */
    --ds-terra-800: #7c2708;
    --ds-terra-900: #3a1e0e;   /* dark section */
    --ds-ink:       #241609;   /* headings, 16.4:1 on page background */

    /* Cool accents: soft sky blues and slate greys, used to soften the warm terracotta */
    --ds-sky-50:   #eef4fa;
    --ds-sky-100:  #dbe7f3;
    --ds-sky-200:  #bcd1e6;
    --ds-sky-500:  #4f7fb0;
    --ds-sky-600:  #3b6a99;   /* 5.4:1 with white */
    --ds-sky-700:  #2f5680;
    --ds-slate-50:  #f3f5f8;
    --ds-slate-100: #e6eaf0;
    --ds-slate-300: #b9c2cf;
    --ds-slate-600: #4a5566;
    --ds-slate-800: #243042;

    /* Warm neutrals */
    --ds-bg:            #f8f7f4;
    --ds-surface:       #ffffff;
    --ds-surface-2:     #f3efe8;
    --ds-border:        #e8e1d6;
    --ds-border-strong: #d6ccbd;
    --ds-text:          #3b2a1c;
    --ds-text-muted:    #6b5646;  /* 6.4:1 on page background */

    /* Semantic */
    --ds-info:        #1d4ed8;
    --ds-info-soft:   #e8eefc;
    --ds-success:     #15803d;
    --ds-success-soft:#e6f4ea;
    --ds-warning:     #b45309;
    --ds-warning-soft:#fdf0dc;
    --ds-danger:      #b91c1c;
    --ds-danger-soft: #fbe9e7;

    /* Roles (what components actually use) */
    --ds-primary:        var(--ds-terra-600);
    --ds-primary-hover:  var(--ds-terra-700);
    --ds-primary-soft:   var(--ds-terra-50);
    --ds-on-primary:     #ffffff;
    --ds-focus:          #1d4ed8;

    /* Type */
    --ds-font: 'Instrument Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --ds-font-reading: 'Instrument Sans', Georgia, serif;
    --ds-text-xs:   0.8125rem;
    --ds-text-sm:   0.9375rem;
    --ds-text-base: 1.0625rem;                          /* 17px: comfortable reading size */
    --ds-text-lg:   1.25rem;
    --ds-text-xl:   clamp(1.35rem, 1.1rem + 0.8vw, 1.65rem);
    --ds-text-2xl:  clamp(1.65rem, 1.2rem + 1.6vw, 2.25rem);
    --ds-text-3xl:  clamp(2rem, 1.3rem + 2.6vw, 3rem);
    --ds-text-display: clamp(2.25rem, 1.4rem + 3.6vw, 3.75rem);
    --ds-leading-tight: 1.15;
    --ds-leading-snug:  1.35;
    --ds-leading-body:  1.65;                           /* >=1.5 for dyslexic readers */
    --ds-measure: 68ch;                                 /* max comfortable line length */

    /* Space (4px base) */
    --ds-space-1: 0.25rem;  --ds-space-2: 0.5rem;   --ds-space-3: 0.75rem;
    --ds-space-4: 1rem;     --ds-space-5: 1.5rem;   --ds-space-6: 2rem;
    --ds-space-7: 3rem;     --ds-space-8: 4rem;     --ds-space-9: 6rem;

    /* Shape & depth */
    --ds-radius-sm: 8px;  --ds-radius-md: 12px;  --ds-radius-lg: 20px;  --ds-radius-pill: 999px;
    --ds-shadow-sm: 0 1px 2px rgba(36, 22, 9, 0.06);
    --ds-shadow-md: 0 6px 20px rgba(36, 22, 9, 0.08);
    --ds-shadow-lg: 0 18px 48px rgba(36, 22, 9, 0.14);

    /* Layout & motion */
    --ds-container: 1200px;
    --ds-gutter: clamp(1rem, 4vw, 2rem);
    --ds-nav-h: 68px;
    --ds-ease: cubic-bezier(0.2, 0.7, 0.2, 1);
    --ds-dur: 160ms;
  }
}

/* ---------- 2. Base (low specificity via :where) ---------- */
@layer ds-base {
  :where(.ds-page) {
    margin: 0;
    font-family: var(--ds-font);
    font-size: var(--ds-text-base);
    line-height: var(--ds-leading-body);
    color: var(--ds-text);
    background: var(--ds-bg);
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
  }
  :where(.ds-page) *, :where(.ds-page) *::before, :where(.ds-page) *::after { box-sizing: border-box; }
  :where(.ds-page) :is(h1, h2, h3, h4) { color: var(--ds-ink); line-height: var(--ds-leading-tight); margin: 0; letter-spacing: -0.01em; }
  :where(.ds-page) img { max-width: 100%; height: auto; }
  :where(.ds-page) a { color: var(--ds-terra-700); text-underline-offset: 3px; }
  :where(.ds-page) a:hover { color: var(--ds-terra-800); }

  /* Visible, high-contrast focus for everyone (keyboard and switch users) */
  :where(.ds-page) :focus-visible {
    outline: 3px solid var(--ds-focus);
    outline-offset: 3px;
    border-radius: 4px;
  }
  .ds-skip-link {
    position: absolute; left: var(--ds-space-4); top: -100px; z-index: 1000;
    background: var(--ds-ink); color: #fff; padding: var(--ds-space-3) var(--ds-space-4);
    border-radius: var(--ds-radius-sm); text-decoration: none;
  }
  .ds-skip-link:focus { top: var(--ds-space-4); }

  @media (prefers-reduced-motion: reduce) {
    :where(.ds-page) *, :where(.ds-page) *::before, :where(.ds-page) *::after {
      animation-duration: 0.01ms !important; animation-iteration-count: 1 !important;
      transition-duration: 0.01ms !important; scroll-behavior: auto !important;
    }
  }
}

/* ---------- 3. Components ---------- */
@layer ds-components {
  /* Layout */
  .ds-container { width: 100%; max-width: var(--ds-container); margin-inline: auto; padding-inline: var(--ds-gutter); }
  .ds-section { padding-block: clamp(3rem, 2rem + 5vw, 6rem); }
  .ds-section--compact { padding-block: clamp(2rem, 1.5rem + 3vw, 3.5rem); }   /* inner-page hero bands */
  .ds-section--alt  { background: var(--ds-surface-2); }
  .ds-section--card { background: var(--ds-surface); border-block: 1px solid var(--ds-border); }
  .ds-section--dark { background: var(--ds-terra-900); color: var(--ds-terra-200); }
  .ds-section--dark :is(h1, h2, h3, h4) { color: var(--ds-terra-50); }
  .ds-section--dark a:not(.ds-btn) { color: var(--ds-terra-300); }
  .ds-section--dark .ds-btn--on-dark { color: var(--ds-ink); }
  .ds-section__head { max-width: 46rem; margin: 0 auto var(--ds-space-7); text-align: center; }
  .ds-section__head p { color: var(--ds-text-muted); margin-top: var(--ds-space-3); }
  .ds-section--dark .ds-section__head p { color: var(--ds-terra-200); }
  .ds-grid { display: grid; gap: var(--ds-space-5); grid-template-columns: repeat(auto-fit, minmax(min(100%, 17rem), 1fr)); }
  .ds-stack > * + * { margin-top: var(--ds-space-4); }
  .ds-cluster { display: flex; flex-wrap: wrap; gap: var(--ds-space-3); align-items: center; }

  /* Type */
  .ds-display { font-size: var(--ds-text-display); font-weight: 700; letter-spacing: -0.02em; }
  .ds-h1 { font-size: var(--ds-text-3xl); font-weight: 700; letter-spacing: -0.015em; }
  .ds-h2 { font-size: var(--ds-text-2xl); font-weight: 700; }
  .ds-h3 { font-size: var(--ds-text-xl); font-weight: 650; line-height: var(--ds-leading-snug); }
  .ds-lead { font-size: var(--ds-text-lg); color: var(--ds-text-muted); max-width: 40rem; }
  .ds-eyebrow {
    display: inline-block; font-size: var(--ds-text-xs); font-weight: 700; letter-spacing: 0.08em;
    text-transform: uppercase; color: var(--ds-terra-700); margin-bottom: var(--ds-space-3);
  }
  .ds-section--dark .ds-eyebrow { color: var(--ds-terra-300); }
  .ds-prose { max-width: var(--ds-measure); }
  .ds-prose > * + * { margin-top: 1em; }
  .ds-prose :is(h2, h3) { margin-top: 1.8em; }
  .ds-prose h2 { font-size: var(--ds-text-xl); font-weight: 700; line-height: 1.25; }
  .ds-prose h3 { font-size: var(--ds-text-lg); font-weight: 700; line-height: 1.3; }
  .ds-prose :is(ul, ol) { padding-left: 1.4em; }
  .ds-prose li + li { margin-top: 0.4em; }
  .ds-prose--wide { max-width: 48rem; }

  /* Data table (scrolls inside its wrapper on narrow screens) */
  .ds-table-wrap { overflow-x: auto; margin-block: var(--ds-space-5); }
  .ds-table { width: 100%; border-collapse: collapse; font-size: var(--ds-text-sm); }
  .ds-table :is(th, td) { text-align: left; padding: var(--ds-space-3) var(--ds-space-4); border-bottom: 1px solid var(--ds-border); vertical-align: top; }
  .ds-table th { font-size: var(--ds-text-xs); text-transform: uppercase; letter-spacing: 0.06em; color: var(--ds-text-muted); font-weight: 700; }
  .ds-table td:first-child { font-weight: 600; }

  /* Fact grid of small cards */
  .ds-facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: var(--ds-space-4); margin-block: var(--ds-space-6); list-style: none; padding: 0; }
  .ds-facts > li { margin: 0; }

  /* Buttons */
  .ds-btn {
    --_bg: var(--ds-primary); --_fg: var(--ds-on-primary); --_bd: transparent;
    display: inline-flex; align-items: center; justify-content: center; gap: var(--ds-space-2);
    min-height: 48px; padding: 0 var(--ds-space-5);          /* 48px: comfortable touch target */
    font: 600 var(--ds-text-base)/1 var(--ds-font); text-decoration: none; cursor: pointer;
    background: var(--_bg); color: var(--_fg); border: 2px solid var(--_bd); border-radius: var(--ds-radius-md);
    transition: background var(--ds-dur) var(--ds-ease), transform var(--ds-dur) var(--ds-ease), box-shadow var(--ds-dur) var(--ds-ease);
  }
  .ds-btn:hover { --_bg: var(--ds-primary-hover); color: var(--_fg); transform: translateY(-1px); box-shadow: var(--ds-shadow-md); }
  .ds-btn:active { transform: none; box-shadow: none; }
  .ds-btn[disabled], .ds-btn[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; }
  .ds-btn--secondary { --_bg: transparent; --_fg: var(--ds-terra-700); --_bd: var(--ds-terra-600); }
  .ds-btn--secondary:hover { --_bg: var(--ds-terra-50); color: var(--ds-terra-800); }
  .ds-btn--ghost { --_bg: transparent; --_fg: var(--ds-ink); }
  .ds-btn--ghost:hover { --_bg: var(--ds-surface-2); color: var(--ds-ink); box-shadow: none; }
  .ds-btn--on-dark { --_bg: var(--ds-terra-50); --_fg: var(--ds-ink); }
  .ds-btn--on-dark:hover { --_bg: #fff; color: var(--ds-ink); }
  .ds-btn--lg { min-height: 56px; padding: 0 var(--ds-space-6); font-size: var(--ds-text-lg); }
  .ds-btn--sm { min-height: 40px; padding: 0 var(--ds-space-4); font-size: var(--ds-text-sm); }

  /* Card */
  .ds-card {
    background: var(--ds-surface); border: 1px solid var(--ds-border); border-radius: var(--ds-radius-lg);
    padding: var(--ds-space-5); box-shadow: var(--ds-shadow-sm);
  }
  .ds-card--interactive { transition: transform var(--ds-dur) var(--ds-ease), box-shadow var(--ds-dur) var(--ds-ease); }
  .ds-card--interactive:hover { transform: translateY(-3px); box-shadow: var(--ds-shadow-md); }
  .ds-card__icon {
    display: grid; place-items: center; width: 48px; height: 48px; margin-bottom: var(--ds-space-4);
    border-radius: var(--ds-radius-md); background: var(--ds-terra-50); color: var(--ds-terra-700);
  }
  .ds-card--featured { border: 2px solid var(--ds-terra-600); box-shadow: var(--ds-shadow-md); position: relative; }

  /* Badge / pill */
  .ds-badge {
    display: inline-flex; align-items: center; gap: var(--ds-space-1); padding: 4px 12px;
    font-size: var(--ds-text-xs); font-weight: 600; border-radius: var(--ds-radius-pill);
    background: var(--ds-terra-50); color: var(--ds-terra-800); border: 1px solid var(--ds-terra-200);
  }
  .ds-badge--success { background: var(--ds-success-soft); color: var(--ds-success); border-color: transparent; }
  .ds-badge--warning { background: var(--ds-warning-soft); color: var(--ds-warning); border-color: transparent; }
  .ds-badge--danger  { background: var(--ds-danger-soft);  color: var(--ds-danger);  border-color: transparent; }
  .ds-badge--info    { background: var(--ds-info-soft);    color: var(--ds-info);    border-color: transparent; }

  /* Form fields */
  .ds-field { display: grid; gap: var(--ds-space-2); }
  .ds-label { font-weight: 600; color: var(--ds-ink); font-size: var(--ds-text-sm); }
  .ds-hint  { font-size: var(--ds-text-xs); color: var(--ds-text-muted); }
  .ds-input, .ds-select, .ds-textarea {
    width: 100%; min-height: 48px; padding: 0 var(--ds-space-4); font: inherit; color: var(--ds-ink);
    background: var(--ds-surface); border: 2px solid var(--ds-border-strong); border-radius: var(--ds-radius-md);
    transition: border-color var(--ds-dur) var(--ds-ease);
  }
  .ds-textarea { padding-block: var(--ds-space-3); min-height: 120px; line-height: var(--ds-leading-body); }
  .ds-input:hover, .ds-select:hover, .ds-textarea:hover { border-color: var(--ds-terra-400); }
  .ds-input[aria-invalid="true"] { border-color: var(--ds-danger); }
  .ds-error { color: var(--ds-danger); font-size: var(--ds-text-xs); font-weight: 600; }

  /* Callout */
  .ds-callout {
    display: flex; gap: var(--ds-space-3); padding: var(--ds-space-4) var(--ds-space-5);
    border-radius: var(--ds-radius-md); border: 1px solid var(--ds-border); border-left: 4px solid var(--ds-terra-600);
    background: var(--ds-surface);
  }
  .ds-callout--info    { border-left-color: var(--ds-info);    background: var(--ds-info-soft); }
  .ds-callout--success { border-left-color: var(--ds-success); background: var(--ds-success-soft); }
  .ds-callout--warning { border-left-color: var(--ds-warning); background: var(--ds-warning-soft); }

  /* Navigation: transparent over hero, solid after scroll (toggle .is-solid) */
  .ds-nav {
    position: fixed; inset: 0 0 auto 0; z-index: 100; height: var(--ds-nav-h);
    display: flex; align-items: center; color: var(--ds-terra-50);
    transition: background var(--ds-dur) var(--ds-ease), box-shadow var(--ds-dur) var(--ds-ease), color var(--ds-dur) var(--ds-ease);
  }
  .ds-nav.is-solid { background: rgba(248, 247, 244, 0.94); backdrop-filter: blur(10px); color: var(--ds-ink); box-shadow: 0 1px 0 var(--ds-border); }
  .ds-nav--static { position: sticky; top: 0; background: rgba(248, 247, 244, 0.94); backdrop-filter: blur(10px); color: var(--ds-ink); box-shadow: 0 1px 0 var(--ds-border); }
  .ds-nav__inner { display: flex; flex-wrap: nowrap; align-items: center; justify-content: space-between; gap: var(--ds-space-4); width: 100%; }
  .ds-nav__inner > .ds-cluster { flex-wrap: nowrap; gap: var(--ds-space-2); flex-shrink: 0; }
  .ds-nav__mobile-only { display: none; }
  .ds-nav__brand { display: inline-flex; align-items: center; gap: var(--ds-space-2); font-weight: 700; font-size: var(--ds-text-lg); color: inherit; text-decoration: none; }
  .ds-nav__links { display: flex; align-items: center; gap: var(--ds-space-5); list-style: none; margin: 0; padding: 0; }
  .ds-nav__links a { color: inherit; text-decoration: none; font-weight: 500; font-size: var(--ds-text-sm); padding-block: var(--ds-space-2); }
  .ds-nav__links a:hover, .ds-nav__links a[aria-current="page"] { color: var(--ds-terra-600); }
  .ds-nav__toggle { display: none; }
  @media (max-width: 860px) {
    .ds-nav__links { display: none; }
    .ds-nav__links.is-open {
      display: flex; flex-direction: column; align-items: stretch; gap: 0;
      position: absolute; top: var(--ds-nav-h); inset-inline: 0; background: var(--ds-bg); color: var(--ds-ink);
      padding: var(--ds-space-3) var(--ds-gutter) var(--ds-space-5); box-shadow: var(--ds-shadow-md);
    }
    .ds-nav__links.is-open a { display: block; padding: var(--ds-space-4) 0; border-bottom: 1px solid var(--ds-border); }
    .ds-nav__toggle { display: inline-flex; }
    .ds-nav__mobile-only { display: list-item; }
    .ds-nav__inner > .ds-cluster .ds-nav__desktop-only { display: none; }
  }
  @media (max-width: 420px) {
    .ds-nav__brand { font-size: var(--ds-text-base); gap: var(--ds-space-2); }
    .ds-nav__inner { gap: var(--ds-space-2); }
  }

  /* Footer */
  .ds-footer { background: var(--ds-ink); color: var(--ds-terra-200); padding-block: var(--ds-space-8) var(--ds-space-6); font-size: var(--ds-text-sm); }
  .ds-footer a { color: var(--ds-terra-100); text-decoration: none; }
  .ds-footer a:hover { color: #fff; text-decoration: underline; }
  .ds-footer__cols { display: grid; gap: var(--ds-space-6); grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr)); }
  .ds-footer h4 { color: var(--ds-terra-50); font-size: var(--ds-text-sm); margin-bottom: var(--ds-space-3); }
  .ds-footer ul { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--ds-space-2); }
  .ds-footer__legal { margin-top: var(--ds-space-7); padding-top: var(--ds-space-5); border-top: 1px solid rgba(255, 255, 255, 0.12); color: var(--ds-terra-300); font-size: var(--ds-text-xs); }

  /* Accordion (native <details>, works without JS) */
  .ds-accordion { border-top: 1px solid var(--ds-border); }
  .ds-accordion details { border-bottom: 1px solid var(--ds-border); }
  .ds-accordion summary {
    cursor: pointer; list-style: none; padding: var(--ds-space-4) 0; font-weight: 600; color: var(--ds-ink);
    display: flex; justify-content: space-between; gap: var(--ds-space-4); align-items: center; min-height: 48px;
  }
  .ds-accordion summary::-webkit-details-marker { display: none; }
  .ds-accordion summary::after { content: '+'; font-size: 1.5rem; line-height: 1; color: var(--ds-terra-600); transition: transform var(--ds-dur) var(--ds-ease); }
  .ds-accordion details[open] summary::after { transform: rotate(45deg); }
  .ds-accordion details > :not(summary) { padding-bottom: var(--ds-space-4); color: var(--ds-text); max-width: var(--ds-measure); }

  /* Pricing / tier helpers */
  .ds-price { font-size: var(--ds-text-3xl); font-weight: 700; color: var(--ds-ink); line-height: 1; }
  .ds-price small { font-size: var(--ds-text-sm); font-weight: 500; color: var(--ds-text-muted); }
  .ds-checklist { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--ds-space-3); }
  .ds-checklist li { display: grid; grid-template-columns: 1.25rem 1fr; gap: var(--ds-space-3); align-items: start; }
  .ds-checklist li::before { content: '✓'; color: var(--ds-success); font-weight: 700; }
}

/* ---------- 4. Utilities ---------- */
@layer ds-utilities {
  .ds-sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
  .ds-text-center { text-align: center; }
  .ds-text-muted { color: var(--ds-text-muted); }
  .ds-mt-0 { margin-top: 0 !important; }
}
