/* ============================================================
   Precept — Design System (Phase 1 Foundation, 2026-05-24)
   ============================================================
   Single shared stylesheet consumed by every page. Tokens, type,
   geometry, motion, paper texture, header, bubbles, base reset.
   Cascade layers ensure page-local styles can override safely.
   ============================================================ */

@layer reset, tokens, base, components, utilities, motion;

/* ---------- LAYER: reset ----------------------------------- */
@layer reset {
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  html { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;
         text-rendering: optimizeLegibility; scroll-behavior: smooth;
         width: 100%; max-width: 100%; }
  html, body { min-height: 100dvh; }
  body { width: 100%; max-width: 100%; overflow-x: hidden; touch-action: pan-y pinch-zoom; }
  img, svg, video { display: block; max-width: 100%; }
  input, select, textarea { font: inherit; max-width: 100%; }
  button { font: inherit; color: inherit; background: transparent; border: 0; cursor: pointer; }
  a { color: inherit; text-decoration: inherit; }

  @media (max-width: 767px), (pointer: coarse) {
    input, select, textarea { font-size: 16px !important; }
  }

}

/* ---------- LAYER: tokens ---------------------------------- */
@layer tokens {
  :root {
    /* Brand — slate blue family (anchored to new 2026 logo: #2A4F5C + #3D6B7A) */
    --p-primary:        #3D6B7A;                /* slate blue (logo cls-2) */
    --p-primary-strong: #2A4F5C;                /* deeper teal (logo cls-1) */
    --p-primary-90:     #305866;                /* hover/pressed */
    --p-primary-tint:   rgba(61, 107, 122, 0.08);
    --p-primary-soft:   rgba(61, 107, 122, 0.06);

    /* Accent — preserved warm gold */
    --p-accent:         #C79F14;

    /* Surfaces */
    --p-bg:             #FAFAF9;                /* warm bone */
    --p-surface:        #FFFFFF;
    --p-surface-2:      #F5F4F1;                /* warm off-white */

    /* Ink scale */
    --p-ink:            #3F3F3D;
    --p-ink-70:         rgba(63, 63, 61, 0.70);
    --p-ink-50:         rgba(63, 63, 61, 0.50);
    --p-ink-30:         rgba(63, 63, 61, 0.28);

    /* Rules */
    --p-rule:           rgba(63, 63, 61, 0.18);
    --p-rule-soft:      rgba(63, 63, 61, 0.10);

    /* Danger / Safety */
    --p-danger:         #B3261E;
    --p-danger-bg:      #FAEAE9;

    /* Typography */
    --f-display: 'Crimson Pro', Georgia, 'Times New Roman', serif;
    --f-mono:    'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

    /* Fluid type scale (mobile → desktop) */
    --ts-9:  clamp(10px,   0.18vw + 9.5px,  11px);
    --ts-10: clamp(10.5px, 0.22vw + 10px,   12px);
    --ts-12: clamp(12px,   0.30vw + 11.5px, 13.5px);
    --ts-14: clamp(13px,   0.36vw + 12.5px, 15px);
    --ts-16: clamp(15px,   0.42vw + 14px,   17px);   /* body */
    --ts-18: clamp(16px,   0.50vw + 15px,   19px);
    --ts-22: clamp(20px,   0.80vw + 18px,   24px);   /* h3 */
    --ts-28: clamp(24px,   1.20vw + 20px,   32px);   /* h2 */
    --ts-40: clamp(32px,   2.60vw + 24px,   52px);   /* h1 */
    --ts-56: clamp(40px,   4.80vw + 28px,   68px);   /* hero */

    /* Geometry — one scale, used everywhere */
    --r-2: 2px; --r-4: 4px; --r-6: 6px; --r-8: 8px; --r-12: 12px; --r-18: 18px; --r-full: 999px;

    /* Spacing */
    --s-1: 4px; --s-2: 8px; --s-3: 12px; --s-4: 16px; --s-5: 20px; --s-6: 24px;
    --s-8: 32px; --s-10: 40px; --s-12: 48px; --s-16: 64px; --s-24: 96px;

    /* Elevation — soft, warm-toned */
    --shadow-1: 0 1px 2px rgba(63, 63, 61, 0.06);
    --shadow-2: 0 2px 6px rgba(63, 63, 61, 0.08), 0 1px 2px rgba(63, 63, 61, 0.04);
    --shadow-3: 0 10px 30px -10px rgba(61, 107, 122, 0.30), 0 4px 10px rgba(63, 63, 61, 0.06);
    --shadow-press: 0 0 0 0 rgba(61, 107, 122, 0);

    /* Motion */
    --ease-settle: cubic-bezier(0.22, 0.61, 0.36, 1);
    --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
    --ease-exit:   cubic-bezier(0.45, 0, 0.55, 1);
    --dur-fast:    140ms;
    --dur-base:    220ms;
    --dur-slow:    420ms;
  }

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

/* ---------- LAYER: base ------------------------------------ */
@layer base {
  body {
    font-family: var(--f-display);
    font-variation-settings: "opsz" 14, "wght" 400, "SOFT" 50;
    font-size: var(--ts-16);
    line-height: 1.6;
    color: var(--p-ink);
    background: var(--p-bg);
    min-height: 100dvh;
    position: relative;
    overscroll-behavior-x: none;
    overscroll-behavior-y: auto;
  }

  /* Paper-grain overlay — opt-in via [data-texture="grain"] on body */
  body[data-texture="grain"]::before {
    content: "";
    position: fixed; inset: 0; pointer-events: none; z-index: 1;
    opacity: 0.45; mix-blend-mode: multiply;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='280' height='280'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.92' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.04 0 0 0 0 0.12 0 0 0 0 0.11 0 0 0 0.14 0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
  }

  ::selection { background: var(--p-accent); color: var(--p-ink); }

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

  /* View Transitions — page-to-page fade by default */
  @media (prefers-reduced-motion: no-preference) {
    ::view-transition-old(root) { animation: pt-fade-out var(--dur-base) var(--ease-exit) both; }
    ::view-transition-new(root) { animation: pt-fade-in  var(--dur-slow) var(--ease-settle) both; }
  }
}

/* ---------- LAYER: components ------------------------------ */
@layer components {

  /* Masthead — single canonical sticky header */
  .p-masthead {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s-4);
    min-height: 68px;
    /* Reserve the iOS safe-area (notch / Dynamic Island) above the bar so
       the header content doesn't slide under the status bar in the native
       app. env() resolves to 0 on the web, so this is a no-op in browsers.
       Pages must carry <meta name="viewport" ... viewport-fit=cover> for
       the inset to be reported. */
    padding: calc(2px + env(safe-area-inset-top, 0px)) var(--s-6) 2px;
    background: rgba(250, 250, 249, 0.85);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
    position: sticky; top: 0; z-index: 10;
    backdrop-filter: saturate(140%) blur(8px);
    -webkit-backdrop-filter: saturate(140%) blur(8px);
  }
  .p-masthead__logo {
    display: inline-flex; align-items: center; gap: var(--s-2);
    line-height: 1; flex-shrink: 0;
    text-decoration: none;
  }
  /* Wordmark width target: ~190-220px desktop. With SVG viewBox
     320 300 650 180 (aspect 3.61), height 56 → 202px wide. */
  .p-masthead__logo img { height: 56px; width: auto; max-width: 240px; }
  .p-masthead__right { display: inline-flex; align-items: center; gap: var(--s-3); }

  @media (max-width: 480px) {
    .p-masthead { min-height: 56px; padding: 2px var(--s-4); }
    .p-masthead__logo img { height: 44px; max-width: 180px; }
  }

  /* Role pill */
  .p-pill {
    display: inline-flex; align-items: center; gap: var(--s-2);
    padding: 5px 10px;
    background: var(--p-primary-soft);
    border: 1px solid var(--p-rule-soft);
    border-radius: var(--r-full);
    font-family: var(--f-mono);
    font-size: var(--ts-9);
    font-weight: 500;
    letter-spacing: 0.14em;
    color: var(--p-ink-70);
    text-transform: uppercase;
    white-space: nowrap;
  }
  .p-pill em { font-style: normal; color: var(--p-primary); font-weight: 500; text-transform: capitalize; }

  /* Buttons — canonical CTA */
  .p-cta {
    display: inline-flex; align-items: center; justify-content: center; gap: var(--s-3);
    padding: 16px 26px;
    background: var(--p-primary);
    color: var(--p-bg);
    font-family: var(--f-mono);
    font-variation-settings: "opsz" 24, "wght" 500, "SOFT" 40;
    font-size: var(--ts-16);
    letter-spacing: -0.005em;
    border-radius: var(--r-4);
    cursor: pointer;
    text-decoration: none;
    box-shadow: var(--shadow-2), 0 2px 0 0 var(--p-primary-90);
    transition: background var(--dur-fast) ease,
                transform var(--dur-fast) var(--ease-settle),
                box-shadow var(--dur-base) ease;
    -webkit-tap-highlight-color: transparent;
  }
  .p-cta:hover { background: var(--p-primary-90); transform: translateY(-1px); box-shadow: var(--shadow-3), 0 3px 0 0 var(--p-primary-90); }
  .p-cta:active { transform: translateY(1px); box-shadow: var(--shadow-1), 0 0 0 0 var(--p-primary-90); }
  .p-cta--ghost { background: transparent; color: var(--p-ink-70); border: 1.5px solid var(--p-rule-soft); box-shadow: none; }
  .p-cta--ghost:hover { border-color: var(--p-rule); background: var(--p-primary-soft); box-shadow: none; transform: none; }

  /* Chat bubbles — self/other (Phase 1 contract; replaces role-mentor/role-mentee) */
  .p-msg {
    max-width: 78%;
    padding: 10px 14px;
    border-radius: var(--r-18);
    font-size: var(--ts-16);
    line-height: 1.5;
    word-wrap: break-word;
    white-space: pre-wrap;
    box-shadow: var(--shadow-1);
  }
  .p-msg--self {
    align-self: flex-end;
    background: var(--p-primary);
    color: var(--p-bg);
    border-bottom-right-radius: var(--r-6);
  }
  .p-msg--other {
    align-self: flex-start;
    background: var(--p-surface);
    color: var(--p-ink);
    border: 1px solid rgba(63, 63, 61, 0.06);
    border-bottom-left-radius: var(--r-6);
  }

  .p-msg__sender {
    display: flex; align-items: center; gap: var(--s-2);
    margin-bottom: 6px;
    font-family: var(--f-display);
    font-size: var(--ts-12);
    line-height: 1.2;
  }
  .p-msg__sender-name { font-variation-settings: "opsz" 14, "wght" 700, "SOFT" 40; text-transform: capitalize; }
  .p-msg--self  .p-msg__sender { color: rgba(255, 255, 255, 0.95); }
  .p-msg--other .p-msg__sender { color: var(--p-ink); }
  .p-msg__time { display: block; margin-top: 4px; font-family: var(--f-mono); font-size: var(--ts-9); letter-spacing: 0.1em; opacity: 0.65; }
  .p-msg--self .p-msg__time { text-align: right; }

  /* Role badge inside bubble */
  .p-msg__role {
    display: inline-block; padding: 2px 7px; margin-bottom: 6px;
    background: var(--p-bg); color: var(--p-ink-70);
    border: 1px solid var(--p-rule-soft); border-radius: var(--r-full);
    font-family: var(--f-mono); font-size: var(--ts-9);
    letter-spacing: 0.16em; text-transform: uppercase;
    font-weight: 500; line-height: 1;
  }
  .p-msg--self .p-msg__role {
    background: rgba(255, 255, 255, 0.18);
    border-color: rgba(255, 255, 255, 0.30);
    color: rgba(255, 255, 255, 0.92);
  }
  .p-msg--other .p-msg__role.mentor { background: var(--p-primary); color: var(--p-bg); border-color: var(--p-primary); }
  .p-msg--other .p-msg__role.mentee { background: var(--p-accent); color: var(--p-ink); border-color: var(--p-accent); }
}

/* ---------- LAYER: utilities ------------------------------- */
@layer utilities {
  .p-shell        { max-width: 600px; margin: 0 auto; padding: var(--s-8) var(--s-6) var(--s-12); position: relative; z-index: 2; }
  .p-shell--narrow { max-width: 520px; }
  .p-shell--wide   { max-width: 720px; }

  .p-eyebrow {
    font-family: var(--f-mono); font-size: var(--ts-10); font-weight: 500;
    letter-spacing: 0.22em; color: var(--p-ink-50); text-transform: uppercase;
  }

  .p-divider { border: 0; border-top: 1px solid var(--p-rule-soft); margin: var(--s-6) 0; }
}

/* ---------- LAYER: motion ---------------------------------- */
@layer motion {
  @keyframes pt-fade-in  { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
  @keyframes pt-fade-out { from { opacity: 1; } to { opacity: 0; } }
  @keyframes p-settle    { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
  @keyframes p-slide-up  { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
  @keyframes p-pulse     { 0%, 60%, 100% { opacity: 0.3; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-2px); } }

  .p-settle     { opacity: 0; transform: translateY(6px); animation: p-settle var(--dur-slow) var(--ease-settle) forwards; }
  .p-settle--d1 { animation-delay: 80ms; }
  .p-settle--d2 { animation-delay: 200ms; }
  .p-settle--d3 { animation-delay: 320ms; }
  .p-settle--d4 { animation-delay: 460ms; }

  /* Scroll-driven progress — gracefully degrades where unsupported */
  @supports (animation-timeline: scroll()) {
    .p-scroll-progress { animation: p-scroll-progress linear; animation-timeline: scroll(); }
    @keyframes p-scroll-progress { from { transform: scaleX(0); } to { transform: scaleX(1); } }
  }
}

/* Android Chrome-only motion lock. Kept outside cascade layers so it can
   override page-local inline styles that are declared after precept.css. */
@media (pointer: coarse) {
  @supports not (-webkit-touch-callout: none) {
    body::after,
    .hero__field {
      animation: none !important;
      transform: none !important;
    }

    .hero__ornament,
    .signin-hero__ornament,
    .util-hero__ornament,
    .greet__ornament {
      animation: none !important;
      transform: none !important;
      display: none !important;
    }
  }
}

/* ---------- Native safe-area: pin per-page `.masthead` headers ----------
   Several pages define their own inline `.masthead` (legal pages, profile,
   onboarding, mentor availability). Unlike `.nav` / `.dash-bar` /
   `.p-masthead`, those were NOT sticky — so on iOS, once the header
   scrolled off the top, page content slid up under the status bar /
   Dynamic Island (e.g. the mentor-availability screen). Pin them so the
   header stays put and content scrolls *behind* it.

   Inline page styles set background/padding/etc. but never declare
   position/top/z-index/backdrop-filter, so these apply cleanly without
   !important. Kept unlayered (outside @layer) so it wins over the layered
   cascade. The header's own env(safe-area-inset-top) top padding keeps it
   clear of the cutout; env() is 0 on the web, so this is a no-op there. */
.masthead {
  position: sticky;
  top: 0;
  z-index: 20;
  -webkit-backdrop-filter: saturate(140%) blur(8px);
  backdrop-filter: saturate(140%) blur(8px);
}
