/* ============================================================
   MERIDIAN — Layer 3: components
   Single-class primitives. A primitive never knows its section.
   Ref: MERIDIAN-BUILD §8. Zero !important. Zero backdrop-filter.
   ============================================================ */
@layer tokens, base, components, sections, motion;

@layer components {

  /* ---------- Bespoke inline-SVG icon system (functional marks only) ---------- */
  .icon { width: 1.5em; height: 1.5em; display: inline-block; flex: none; vertical-align: middle; color: inherit; }
  .icon-sprite { position: absolute; width: 0; height: 0; overflow: hidden; }

  /* ---------- Loading spinner (no icon font) ---------- */
  .spinner {
    width: 16px; height: 16px; border-radius: 50%;
    border: 2px solid oklch(0.2 0.01 60 / 0.28);
    border-top-color: var(--ink);
    display: inline-block;
    animation: m-spin 0.7s linear infinite;
  }
  .spinner--on-dark { border-color: oklch(1 0 0 / 0.2); border-top-color: var(--accent-on-dark); }
  @keyframes m-spin { to { transform: rotate(360deg); } }
  @media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 1.6s; } }

  /* ---------- Buttons ---------- */
  .btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 10px;
    min-height: 52px; padding: 14px 28px;
    border-radius: var(--r-pill); border: 1.5px solid transparent;
    font-family: var(--font-display); font-weight: 800; font-size: var(--text-base);
    letter-spacing: -0.01em; line-height: 1; text-align: center;
    transition: transform var(--dur-fast) var(--ease-out),
                box-shadow var(--dur-fast) var(--ease-out),
                background var(--dur-fast) var(--ease-out),
                border-color var(--dur-fast) var(--ease-out),
                color var(--dur-fast) var(--ease-out);
  }
  .btn-primary { background: var(--accent-gradient); color: var(--ink); box-shadow: var(--shadow-cta); }
  .btn-primary:hover { color: var(--ink); transform: translateY(-2px); box-shadow: 0 16px 36px -8px oklch(0.72 0.15 75 / 0.55); }
  .btn-primary:active { transform: translateY(0); }
  .btn-secondary { background: transparent; color: var(--ink); border-color: var(--border-strong); }
  .btn-secondary:hover { color: var(--ink); border-color: var(--accent-text); background: var(--accent-wash); }
  .btn--sm { min-height: 44px; padding: 10px 20px; font-size: var(--text-sm); }

  .btn-text { display: inline-flex; align-items: center; gap: 8px; min-height: 24px; color: var(--accent-text); font-weight: 600; }
  .btn-text .icon { width: 1.05em; height: 1.05em; }
  .btn-text i, .btn-text .icon { transition: transform var(--dur-fast) var(--ease-out); }
  .btn-text:hover { color: var(--accent-deep); text-decoration: underline; text-underline-offset: 3px; }
  .btn-text:hover .icon { transform: translateX(3px); }

  .btn .icon { width: 1.05em; height: 1.05em; }
  .on-dark .btn-secondary { color: var(--ink-on-dark); border-color: var(--border-on-dark); }
  .on-dark .btn-secondary:hover { color: var(--ink-on-dark); border-color: var(--accent-on-dark); background: oklch(1 0 0 / 0.06); }

  /* ---------- Card ---------- */
  .card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); box-shadow: var(--shadow-sm); }
  .card--interactive { transition: border-color var(--dur-base) var(--ease-out), box-shadow var(--dur-base) var(--ease-out), transform var(--dur-base) var(--ease-out); }
  .card--interactive:hover { border-color: var(--accent-fill); box-shadow: var(--shadow-md); transform: translateY(-3px); }

  /* ---------- Kicker (exactly 3× per page; sentence case; amber rule) ---------- */
  .kicker {
    display: inline-flex; align-items: center; gap: 14px;
    font-family: var(--font-body); font-weight: 600; font-size: var(--text-sm);
    color: var(--accent-deep); margin-bottom: var(--sp-3);
  }
  .kicker::before { content: ""; width: 24px; height: 2px; background: var(--accent-fill); border-radius: 2px; flex: none; }
  .on-dark .kicker { color: var(--accent-on-dark); }

  /* ---------- Margin annotations (the worksheet's marginalia, ≥1280px) ---------- */
  .margin-note {
    position: absolute; top: calc(var(--section-pad) + 8px);
    font-family: var(--font-mono); font-size: 0.62rem; font-weight: 500;
    letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-muted);
    opacity: 0.6; writing-mode: vertical-rl; display: none;
    pointer-events: none; white-space: nowrap; z-index: 1;
  }
  .margin-note--left  { left:  clamp(12px, calc((100vw - var(--maxw)) / 2 - 48px), 96px); }
  .margin-note--right { right: clamp(12px, calc((100vw - var(--maxw)) / 2 - 48px), 96px); }
  .margin-note::before { content: "— "; }
  @media (min-width: 1280px) { .margin-note { display: block; } }

  /* Wider working canvas for the signature scenes */
  .canvas-wide { width: 100%; max-width: 1360px; margin-inline: auto; padding-inline: var(--gutter); }

  /* ---------- Mono data-label (telemetry voice) ---------- */
  .data-label {
    font-family: var(--font-mono); font-weight: 500; font-size: 0.72rem;
    letter-spacing: 0.04em; color: var(--ink-muted); text-transform: uppercase;
    font-variant-numeric: tabular-nums;
  }
  .data-label .lede { color: var(--accent-deep); }
  .on-dark .data-label { color: var(--ink-on-dark-2); }

  /* ---------- Status chip + status dot ---------- */
  .status-chip {
    display: inline-flex; align-items: center; gap: 10px;
    padding: 8px 16px 8px 14px; border-radius: var(--r-pill);
    background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-sm);
    font-family: var(--font-mono); font-size: 0.78rem; font-weight: 500;
    letter-spacing: 0.02em; color: var(--ink-body); text-transform: uppercase;
  }
  .status-chip b { color: var(--ink); font-weight: 600; }
  .on-dark .status-chip { background: var(--console-2); border-color: var(--border-on-dark); color: var(--ink-on-dark-2); }
  .on-dark .status-chip b { color: var(--ink-on-dark); }
  .status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent-fill); position: relative; flex: none; }
  .status-dot::after { content: ""; position: absolute; inset: 0; border-radius: 50%; background: var(--accent-fill); animation: status-pulse 2s var(--ease-out) infinite; }
  @keyframes status-pulse { 0% { transform: scale(1); opacity: 0.7; } 70%, 100% { transform: scale(2.6); opacity: 0; } }
  @media (prefers-reduced-motion: reduce) { .status-dot::after { animation: none; } }

  /* ---------- Chip ---------- */
  .chip {
    display: inline-flex; align-items: center; gap: 8px;
    padding: 7px 15px; border-radius: var(--r-pill);
    background: var(--accent-wash); color: var(--accent-deep);
    font-size: var(--text-sm); font-weight: 600; line-height: 1.2;
  }
  .chip strong { font-family: var(--font-display); font-weight: 800; font-size: var(--text-base); }

  /* ---------- Mono tag (TEL / EML / CAL / catalog codes) ---------- */
  .mono-tag {
    display: inline-flex; align-items: center; height: 30px; padding: 0 12px;
    border-radius: var(--r-sm); border: 1px solid var(--border); background: var(--surface-sunken);
    font-family: var(--font-mono); font-size: 0.72rem; font-weight: 600;
    letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent-deep);
  }
  .mono-tag--lg { height: 34px; font-size: 0.82rem; background: var(--surface); }
  .mono-tag--lg .num { font-family: var(--font-display); font-weight: 800; color: var(--accent-text); }

  /* ---------- Index badge (mono numeral in a plotted frame) ---------- */
  .idx-badge {
    width: 46px; height: 46px; flex: none; display: grid; place-items: center;
    border: 1px solid var(--border-strong); border-radius: var(--r-sm); background: var(--surface);
    font-family: var(--font-mono); font-weight: 600; font-size: 0.95rem;
    color: var(--accent-deep); font-variant-numeric: tabular-nums; position: relative;
  }
  .idx-badge::before, .idx-badge::after { content: ""; position: absolute; width: 5px; height: 5px; border-color: var(--accent-fill); border-style: solid; }
  .idx-badge::before { top: 4px; left: 4px; border-width: 1px 0 0 1px; }
  .idx-badge::after { bottom: 4px; right: 4px; border-width: 0 1px 1px 0; }

  /* ---------- Bespoke tick (drawn; replaces check icons) ---------- */
  .tick { width: 16px; height: 16px; flex: none; position: relative; margin-top: 3px; }
  .tick::after { content: ""; position: absolute; left: 4px; top: 0; width: 5px; height: 10px; border: solid var(--accent-text); border-width: 0 2px 2px 0; transform: rotate(42deg); }

  /* ---------- Metric ---------- */
  .metric { display: flex; flex-direction: column; gap: 4px; }
  .metric__num { font-family: var(--font-display); font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; color: var(--accent-text); line-height: 1; }
  .metric__label { font-size: var(--text-sm); color: var(--ink-muted); font-weight: 500; }

  /* ---------- Figure (framed micro-diagram) ---------- */
  .figure { margin: 0 0 var(--sp-3); border: 1px solid var(--border); border-radius: var(--r-md); background: var(--surface); overflow: hidden; }
  .figure__viz {
    display: block; width: 100%; height: auto;
    background:
      linear-gradient(var(--border) 1px, transparent 1px) 0 0 / 100% 24px,
      linear-gradient(90deg, var(--border) 1px, transparent 1px) 0 0 / 24px 100%;
    background-color: var(--surface-sunken);
  }
  .figure__cap { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 9px 12px; border-top: 1px solid var(--border); }
  .viz-ink { fill: var(--ink-muted); }
  .viz-amber-fill { fill: var(--accent-fill); }
  .viz-amber-line { fill: none; stroke: var(--accent-fill); stroke-width: 2.25; stroke-linecap: round; stroke-linejoin: round; }
  .viz-amber-stroke { fill: none; stroke: var(--accent-text); stroke-width: 1.5; }
  .viz-node { fill: var(--surface); stroke: var(--accent-fill); stroke-width: 2; }

  /* ---------- Field (inside the console form card) ---------- */
  .field-label { display: block; font-size: var(--text-sm); font-weight: 600; color: var(--ink-on-dark); margin-bottom: 7px; }
  .field {
    width: 100%; min-height: 52px; padding: 13px 16px;
    background: var(--surface); color: var(--ink);
    border: 1.5px solid transparent; border-radius: var(--r-md);
    font-family: var(--font-body); font-size: var(--text-base);
    transition: border-color var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out);
  }
  .field::placeholder { color: var(--ink-muted); }
  .field:focus { outline: none; border-color: var(--accent-fill); box-shadow: 0 0 0 3px oklch(0.78 0.15 75 / 0.35); }
  .field.hf-invalid, .field.is-invalid { border-color: var(--error-on-dark); }
  select.field { appearance: none; cursor: pointer; }

  /* ---------- Nav ---------- */
  .site-header {
    position: sticky; top: 0; z-index: var(--z-nav);
    background: oklch(0.995 0.002 75 / 0.95);
    border-bottom: 1px solid transparent;
    transition: border-color var(--dur-base) var(--ease-out), box-shadow var(--dur-base) var(--ease-out);
  }
  .site-header.scrolled { border-bottom-color: var(--border); box-shadow: var(--shadow-sm); }
  /* The header's own background already goes full-bleed, but its .container
     was inheriting both the body content's --maxw cap (1760px) AND the
     .has-thread rail-reservation padding (152px) meant to keep body content
     clear of the meridian thread. The thread's track starts at top:96px,
     below the header entirely (it never runs behind it), so that padding was
     pure dead space on the left with nothing balancing it on the right —
     logo and nav were centered in a narrower box instead of spanning the bar
     the way a header conventionally does. Reset both. */
  .site-header .container { max-width: none; padding-left: var(--gutter); }
  .nav-bar { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); min-height: 72px; }
  .brand { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; }
  .brand img { height: 34px; width: auto; }
  .brand__word { font-family: var(--font-display); font-weight: 800; font-size: 1.15rem; color: var(--ink); letter-spacing: -0.02em; }

  .nav-links { display: flex; align-items: center; gap: 2px; }
  .nav-links > a, .nav-dropdown > a {
    display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 0 13px;
    color: var(--ink-body); font-weight: 600; font-size: var(--text-sm);
    border-radius: var(--r-sm); position: relative;
  }
  .nav-links a:hover { color: var(--ink); }
  .nav-links a:not(.btn)::after {
    content: ""; position: absolute; left: 13px; right: 13px; bottom: 12px; height: 2px;
    background: var(--accent-fill); border-radius: 2px; transform: scaleX(0); transform-origin: left;
    transition: transform var(--dur-fast) var(--ease-out);
  }
  .nav-links a:not(.btn):hover::after { transform: scaleX(1); }
  /* .nav-links > a above (min-height:44px; padding:0 13px) outranks .btn--sm's
     own padding by specificity, so the nav's Book Audit button needs its own
     override here to actually render thinner than the regular nav links. */
  .nav-links .btn { margin-left: 6px; min-height: 38px; padding: 0 16px; }

  .nav-dropdown { position: relative; }
  .nav-dropdown__menu {
    position: absolute; top: calc(100% - 6px); left: 0; min-width: 240px;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md);
    box-shadow: var(--shadow-lg); padding: 6px;
    opacity: 0; visibility: hidden; transform: translateY(6px);
    transition: opacity var(--dur-fast) var(--ease-out), transform var(--dur-fast) var(--ease-out), visibility var(--dur-fast);
  }
  .nav-dropdown:hover .nav-dropdown__menu, .nav-dropdown:focus-within .nav-dropdown__menu { opacity: 1; visibility: visible; transform: translateY(0); }
  .nav-dropdown__menu a { display: block; padding: 11px 14px; border-radius: var(--r-sm); color: var(--ink-body); font-size: var(--text-sm); font-weight: 600; }
  .nav-dropdown__menu a::after { content: none; }
  .nav-dropdown__menu a:hover { background: var(--accent-wash); color: var(--accent-deep); }

  @media (max-width: 360px) {
    .nav-bar { gap: var(--sp-2); min-height: 64px; }
    .brand { gap: 8px; }
    .brand img { height: 28px; }
    .brand__word { font-size: 0.98rem; white-space: nowrap; }
  }

  .menu-btn { display: none; width: 44px; height: 44px; align-items: center; justify-content: center; background: transparent; border: 1px solid var(--border); border-radius: var(--r-sm); color: var(--ink); }

  /* Mobile drawer = console (mirror of the form card) */
  .drawer-overlay { position: fixed; inset: 0; background: oklch(0.2 0.01 60 / 0.5); opacity: 0; visibility: hidden; z-index: var(--z-overlay); transition: opacity var(--dur-base) var(--ease-out), visibility var(--dur-base); }
  .drawer-overlay.open { opacity: 1; visibility: visible; }
  .drawer {
    position: fixed; top: 0; right: 0; bottom: 0; width: min(84vw, 340px);
    background: var(--console); color: var(--ink-on-dark); z-index: var(--z-drawer);
    padding: var(--sp-3); transform: translateX(100%); transition: transform var(--dur-base) var(--ease-out);
    display: flex; flex-direction: column; gap: 6px;
  }
  .drawer.open { transform: translateX(0); }
  .drawer__head { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--sp-2); padding-bottom: var(--sp-2); border-bottom: 1px solid var(--border-on-dark); }
  .drawer__head h2 { color: var(--ink-on-dark); font-size: var(--text-lg); }
  .drawer__close { width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center; background: transparent; border: none; color: var(--ink-on-dark); }
  .drawer a { display: flex; align-items: center; min-height: 48px; padding: 0 12px; color: var(--ink-on-dark-2); font-weight: 600; border-radius: var(--r-sm); }
  .drawer a:hover { color: var(--accent-on-dark); background: oklch(1 0 0 / 0.05); }
}
