/* =====================================================================
   manav.id — FOUNDATION
   Cross-theme floor: layout containment, safe-area discipline, touch
   ergonomics, motion and accessibility. Theme-agnostic by design — it
   reads no palette tokens, so it loads before (and under) css/theme.css,
   blog/theme.css or brand.css without fighting any of them.

   Authored against uiuxaudit.md:
     Checklist B — accessibility legal & neurodiversity gate
     Checklist D — responsiveness, mobile constraints & viewport alignment
     Part 12     — mobile and browser protocol
     Part 13     — motion rules

   Load order on every page:
     <link rel="stylesheet" href="css/foundation.css">
     <link rel="stylesheet" href="<the page's theme>">
   ===================================================================== */

/* ---------------------------------------------------------------------
   1. ROOT CONTAINMENT
   The banned pattern is `body { overflow-x: hidden }` — it hides a broken
   layout instead of fixing it, and on iOS it silently kills
   `position: sticky` in descendants. Contain structurally instead.
   ------------------------------------------------------------------ */
html, body { max-width: 100%; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body { overscroll-behavior-y: contain; }

/* ---------------------------------------------------------------------
   2. UNCONSTRAINED ELEMENT CONTAINMENT  (Checklist D)
   A grid `1fr` track is `minmax(auto, 1fr)`: its floor is the child's
   min-content width, so one long code line or wide table pushes the
   track — and the page — past the viewport. Flex items have the same
   trap via the default `min-width: auto`. Zero both, then let the wide
   child scroll itself.
   ------------------------------------------------------------------ */
pre, .term, pre.term, .table-scroll, .scroll-x {
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}
/* `overflow-x: auto` does nothing on `display: table`, so a wide table keeps
   widening the page. Below the tablet breakpoint the table becomes a block
   box, which can actually scroll; above it, it stays a real table. */
table {
  max-width: 100%;
  overscroll-behavior-x: contain;
}
@media (max-width: 860px) {
  table {
    display: block;
    width: max-content;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}
img, svg, canvas, video, iframe, embed, object { max-width: 100%; }
img, video { height: auto; }

/* Long unbroken tokens — URLs, hashes, API keys, wallet addresses — are
   the other common cause of a blown-out line box. */
p, li, dd, dt, td, th, figcaption, blockquote, .lede, h1, h2, h3, h4 {
  overflow-wrap: anywhere;
}
code { overflow-wrap: anywhere; }
pre code { overflow-wrap: normal; } /* code blocks scroll, they do not wrap */

/* Headings read better when the last line is not a single orphan word. */
h1, h2, h3, h4 { text-wrap: balance; }
p, li { text-wrap: pretty; }

/* ---------------------------------------------------------------------
   3. TOUCH ERGONOMICS & GESTURE ISOLATION  (Checklist D, WCAG 2.5.8)
   ------------------------------------------------------------------ */
a, button, .btn, [role="button"], label, summary,
input, select, textarea {
  touch-action: manipulation; /* removes the 300ms double-tap delay */
}

/* Hover affordances must not stick to a finger. Anything that only makes
   sense with a real pointer belongs behind this query in the theme files;
   this neutralises the common leftovers. */
@media (hover: none) {
  a:hover, .btn:hover, button:hover { text-decoration: none; }
}

/* ---------------------------------------------------------------------
   4. MOBILE INPUT ERGONOMICS  (Checklist D)
   iOS Safari zooms the whole viewport when a focused field renders below
   16px, and never zooms back out. This is the single most common mobile
   form defect.
   ------------------------------------------------------------------ */
input, select, textarea, button { font-family: inherit; }
@media (max-width: 860px) {
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
  select,
  textarea,
  /* A class-based field rule out-specifies a bare element selector, so match
     on the class too rather than losing the zoom guard to it. */
  input[class]:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
  select[class],
  textarea[class] { font-size: 16px; }
}

/* ---------------------------------------------------------------------
   5. SAFE AREA  (Checklist D — notch, Dynamic Island, home indicator)
   Requires <meta name="viewport" content="... viewport-fit=cover">.
   Every value carries a fallback so non-notched browsers get 0, not an
   invalid declaration.
   ------------------------------------------------------------------ */
.safe-x { padding-inline: env(safe-area-inset-left, 0px) env(safe-area-inset-right, 0px); }
.safe-top { padding-top: env(safe-area-inset-top, 0px); }
.safe-bottom { padding-bottom: env(safe-area-inset-bottom, 0px); }

/* Bottom-pinned bars must clear the home indicator, and JS may set
   --kb-offset from visualViewport so the keyboard never covers them. */
.sticky-bottom, .action-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
  transform: translateY(calc(var(--kb-offset, 0px) * -1));
  transition: transform .18s ease;
}

/* ---------------------------------------------------------------------
   6. DYNAMIC VIEWPORT UNITS  (Checklist D)
   `100vh` is measured with the mobile URL bar collapsed, so a 100vh hero
   overflows by the height of the bar while the bar is showing.
   ------------------------------------------------------------------ */
@supports (height: 100dvh) {
  .full-h, .hero-full, .screen, .vh100 { min-height: 100dvh; }
}

/* ---------------------------------------------------------------------
   7. SKIP LINK  (WCAG 2.4.1 Bypass Blocks)
   Visually hidden until focused, then the first thing a keyboard user
   reaches. Colours are deliberately theme-neutral.
   ------------------------------------------------------------------ */
.skip-link {
  position: absolute;
  left: max(12px, env(safe-area-inset-left, 0px));
  top: 0;
  z-index: 999;
  transform: translateY(-140%);
  background: #0a0a0a;
  color: #ffffff;
  font-family: ui-monospace, 'JetBrains Mono', 'SF Mono', Menlo, monospace;
  font-size: 13px;
  line-height: 1;
  padding: 14px 18px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  border-radius: 0 0 8px 8px;
  text-decoration: none;
  transition: transform .18s ease;
}
.skip-link:focus,
.skip-link:focus-visible {
  transform: translateY(env(safe-area-inset-top, 0px));
  color: #ffffff;
  text-decoration: none;
}

/* A skip target landing under a sticky header is a skip link that does
   not work. Keep the heading clear of it. */
[id] { scroll-margin-top: calc(72px + env(safe-area-inset-top, 0px)); }

/* ---------------------------------------------------------------------
   8. FOCUS VISIBILITY  (WCAG 2.4.7 / 2.4.11)
   ------------------------------------------------------------------ */
:is(a, button, .btn, input, select, textarea, summary, [tabindex], [role="button"]):focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 3px;
  border-radius: 6px;
}
/* Never remove the ring without replacing it. */
:focus:not(:focus-visible) { outline: none; }

@media (forced-colors: active) {
  :is(a, button, .btn, input, select, textarea, [role="button"]):focus-visible {
    outline: 2px solid Highlight;
    outline-offset: 2px;
  }
}

/* ---------------------------------------------------------------------
   9. MOTION  (Part 13 — honour the OS-level request)
   ------------------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  html { scroll-behavior: auto !important; }
}

/* ---------------------------------------------------------------------
   10. UTILITIES
   ------------------------------------------------------------------ */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}
.sr-only-focusable:focus,
.sr-only-focusable:focus-visible {
  position: static;
  width: auto; height: auto;
  margin: 0; overflow: visible;
  clip: auto; clip-path: none;
  white-space: normal;
}
