/* ============================================================================
   Shared reference-table scaffolding
   Used by /references/spectrum/, /references/cmd-cheat-sheet/, and any future
   big-table reference page. Provides the sticky controls strip, scrollable
   wrapper with sticky thead + sticky first column (via `.is-sticky-col`
   hook), generic legend, badge base, and the `.cell-collapse` family.

   Per-page CSS files layer on top of this with column widths, palette
   modifiers, and scoping under `.reference-page--<name>`.

   Uses site-wide CSS variables (--c-bg, --c-surface, --c-text, --c-accent…)
   from assets/css/style.css, so dark/light theme switching is automatic.
   ========================================================================== */

.reference-page {
  padding: var(--space-2xl) 0 var(--space-3xl);
}

.reference-container {
  max-width: 100rem;
  margin: 0 auto;
  padding: 0 var(--space-lg);
}

/* ---- Hero ---- */
.reference-hero { margin-bottom: var(--space-xl); }
.reference-hero__eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-size: 0.75rem;
  color: var(--c-accent);
  margin: 0 0 var(--space-sm);
  font-weight: 600;
}
.reference-hero__title {
  font-size: clamp(1.75rem, 1.4rem + 1.5vw, 2.75rem);
  margin: 0 0 var(--space-md);
  line-height: 1.15;
  color: var(--c-text);
}
.reference-hero__lede {
  font-size: 1.05rem;
  line-height: 1.65;
  color: var(--c-text);
  margin: 0 0 var(--space-md);
  max-width: 60rem;
}
.reference-hero__howto {
  font-size: 0.95rem;
  color: var(--c-text-muted);
  margin: 0 0 var(--space-lg);
  max-width: 60rem;
}
.reference-hero__howto strong { color: var(--c-text); }
.reference-hero__meta {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-md);
  font-size: 0.85rem;
  color: var(--c-text-muted);
}
.reference-hero__meta-item strong {
  color: var(--c-accent);
  font-variant-numeric: tabular-nums;
}

/* ---- Controls (tabs + search) ---- */
.reference-controls {
  display: flex;
  flex-wrap: nowrap;
  gap: var(--space-md);
  align-items: center;
  margin-bottom: var(--space-md);
  padding: var(--space-sm) 0 var(--space-md);
}
.reference-tabs {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.4rem;
  flex: 1 1 auto;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  scrollbar-color: var(--c-border) transparent;
  padding-bottom: 2px;
  -webkit-overflow-scrolling: touch;
}
.reference-tabs::-webkit-scrollbar { height: 4px; }
.reference-tabs::-webkit-scrollbar-thumb {
  background: var(--c-border);
  border-radius: 999px;
}
.reference-tabs::-webkit-scrollbar-track { background: transparent; }
.reference-tab {
  appearance: none;
  border: 1px solid var(--c-border);
  background: var(--c-surface);
  color: var(--c-text-muted);
  padding: 0.4rem 0.8rem;
  border-radius: 999px;
  font: inherit;
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  flex-shrink: 0;
  transition: background var(--transition-fast),
              color var(--transition-fast),
              border-color var(--transition-fast),
              transform var(--transition-fast);
  white-space: nowrap;
}
.reference-tab:hover {
  color: var(--c-text);
  border-color: var(--c-accent-border);
  background: var(--c-surface-alt);
}
.reference-tab.is-active {
  background: var(--c-accent-bg);
  color: var(--c-accent);
  border-color: var(--c-accent-border);
}
.reference-tab:focus-visible {
  outline: 2px solid var(--c-accent);
  outline-offset: 2px;
}

.reference-search {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--border-radius);
  padding: 0.3rem 0.65rem;
  flex: 0 1 14rem;
  min-width: 0;
  max-width: 18rem;
  transition: border-color var(--transition-fast),
              box-shadow var(--transition-fast);
}
.reference-search:focus-within {
  border-color: var(--c-accent-border);
  box-shadow: 0 0 0 3px var(--c-accent-bg);
}
.reference-search__icon {
  display: inline-flex;
  color: var(--c-text-faint);
  flex-shrink: 0;
}
.reference-search__icon svg { width: 1rem; height: 1rem; }
.reference-search input {
  flex: 1;
  background: transparent;
  border: 0;
  outline: 0;
  color: var(--c-text);
  font: inherit;
  font-size: 0.9rem;
  padding: 0.15rem 0;
  min-width: 0;
}
.reference-search input::placeholder { color: var(--c-text-faint); }
.reference-search__count {
  font-size: 0.75rem;
  color: var(--c-text-faint);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Optional secondary filter (e.g. OS / appliance) — a compact labelled select
   living in the controls strip beside the search box. */
.reference-osfilter {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  flex: 0 0 auto;
  font-size: 0.8rem;
  color: var(--c-text-muted);
}
.reference-osfilter__label {
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
  font-size: 0.7rem;
  color: var(--c-text-faint);
}
.reference-osfilter select {
  appearance: none;
  -webkit-appearance: none;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--border-radius);
  color: var(--c-text);
  font: inherit;
  font-size: 0.85rem;
  padding: 0.35rem 1.8rem 0.35rem 0.65rem;
  cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--c-text-faint) 50%),
                    linear-gradient(135deg, var(--c-text-faint) 50%, transparent 50%);
  background-position: calc(100% - 1.05rem) 53%, calc(100% - 0.7rem) 53%;
  background-size: 0.35rem 0.35rem, 0.35rem 0.35rem;
  background-repeat: no-repeat;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.reference-osfilter select:focus-visible {
  outline: none;
  border-color: var(--c-accent-border);
  box-shadow: 0 0 0 3px var(--c-accent-bg);
}

/* ---- Active-batch blurb ---- */
.reference-batch-blurb {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-left: 3px solid var(--c-accent);
  border-radius: var(--border-radius);
  padding: var(--space-md) var(--space-lg);
  margin-bottom: var(--space-md);
  font-size: 0.9rem;
  color: var(--c-text-muted);
}
.reference-batch-blurb strong {
  display: block;
  color: var(--c-text);
  margin-bottom: 0.25rem;
  font-size: 0.95rem;
}

/* ---- Table ---- */
/* The wrapper is the scroll container for both axes (`overflow: auto` plus
   a `max-height` cap), AND is itself `position: sticky` so it pins to the
   page viewport below the fixed header. The thead inside is
   `position: sticky; top: 0` relative to the wrapper, so once the wrapper
   pins, the thead visually pins to the viewport just below the header.

   A bounded scroll box is what makes the sticky thead + sticky first column +
   horizontal scroll all coexist (a pure page-flow table can't scroll wide
   content horizontally without breaking the viewport-pinned header). The
   offsets key off --header-offset, so unpinning the header (which sets
   --header-offset: 0) reclaims that height: the box pins to the top and grows
   taller.

   `.reference-controls` (tabs + search) is plain in-flow content directly
   above this wrapper — it is NOT sticky, so it scrolls away normally. Because
   nothing sits between them in the DOM, the wrapper's own sticky trigger
   point (top: --header-offset) cannot be reached until the entire controls
   block has already scrolled past that same line — true regardless of the
   controls' height (1 row desktop, 2-3 row mobile/tablet stack), so no
   JS-measured coupling is needed here. */
.reference-table-wrapper {
  position: sticky;
  top: var(--header-offset, var(--header-height, 3.75rem));
  max-height: calc(100vh - var(--header-offset, var(--header-height, 3.75rem)) - 1rem);
  overflow: auto;
  border: 1px solid var(--c-border);
  border-radius: var(--border-radius);
  background: var(--c-surface);
  box-shadow: 0 4px 16px var(--c-card-shadow);
}
.reference-table-wrapper:focus-visible {
  outline: 2px solid var(--c-accent);
  outline-offset: 2px;
}

.reference-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.875rem;
  color: var(--c-text);
}

.reference-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--c-surface-alt);
  color: var(--c-text);
  text-align: left;
  font-weight: 600;
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 1.15rem 1rem 0.95rem;
  border-bottom: 2px solid var(--c-border);
  white-space: normal;
  box-shadow: 0 2px 6px var(--c-card-shadow);
}

/* Sortable header: text + a small sort button sit on one flex row. The button
   shows a neutral up/down glyph that resolves to a single arrow once the column
   is the active sort. */
.reference-table thead th.is-sortable { cursor: default; }
.ref-th-inner {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.4rem;
}
.ref-sort {
  appearance: none;
  border: 0;
  background: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
  color: var(--c-text-faint);
  flex-shrink: 0;
  line-height: 1;
  font-size: 0.95rem;
  border-radius: var(--border-radius-sm);
  transition: color var(--transition-fast);
}
.ref-sort::after { content: "\2195"; }            /* ↕ neutral */
.ref-sort:hover { color: var(--c-text); }
.ref-sort:focus-visible {
  outline: 2px solid var(--c-accent);
  outline-offset: 2px;
}
.reference-table thead th.is-sorted-asc  .ref-sort { color: var(--c-accent); }
.reference-table thead th.is-sorted-desc .ref-sort { color: var(--c-accent); }
.reference-table thead th.is-sorted-asc  .ref-sort::after { content: "\2191"; }  /* ↑ */
.reference-table thead th.is-sorted-desc .ref-sort::after { content: "\2193"; }  /* ↓ */

.reference-table tbody td {
  padding: 0.85rem 1rem;
  vertical-align: top;
  border-bottom: 1px solid var(--c-border-light);
  line-height: 1.5;
}

.reference-table tbody tr:hover td {
  background: var(--c-surface-alt);
}

.reference-table tbody tr:last-child td {
  border-bottom: 0;
}

/* Sticky first column — any column marked `.is-sticky-col` stays pinned
   during horizontal scroll. */
.reference-table td.is-sticky-col,
.reference-table th.is-sticky-col {
  position: sticky;
  left: 0;
  z-index: 1;
  border-right: 1px solid var(--c-border);
  background: var(--c-surface);
}
.reference-table tbody tr:hover td.is-sticky-col {
  background: var(--c-surface-alt);
}
.reference-table thead th.is-sticky-col {
  z-index: 3;
  background: var(--c-surface-alt);
}

/* ---- Monospace cell helper ---- */
.mono {
  font-family: var(--f-mono);
  font-size: 0.825rem;
  color: var(--c-text);
  font-variant-numeric: tabular-nums;
}

/* ---- Collapsible cells (`<details class="cell-collapse">`) ---- */
.cell-collapse {
  font-size: inherit;
}
.cell-collapse > summary {
  list-style: none;
  cursor: pointer;
  display: inline-flex;
  align-items: baseline;
  gap: 0.5rem;
  flex-wrap: wrap;
  color: var(--c-text);
  user-select: none;
}
.cell-collapse > summary::-webkit-details-marker { display: none; }
.cell-collapse > summary::marker { display: none; content: ""; }
.cell-collapse__count {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--c-accent);
}
.cell-collapse__label {
  color: var(--c-text-muted);
}
.cell-collapse__preview {
  color: var(--c-text);
  font-size: 0.85rem;
  line-height: 1.45;
}
.cell-collapse__more {
  font-size: 0.75rem;
  color: var(--c-accent);
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-thickness: 1px;
  flex-shrink: 0;
}
.cell-collapse[open] > summary .cell-collapse__more::after { content: " (collapse)"; }
.cell-collapse[open] > summary .cell-collapse__preview { display: none; }
.cell-collapse[open] > summary .cell-collapse__count,
.cell-collapse[open] > summary .cell-collapse__label { opacity: 0.6; }

.cell-list {
  margin: 0.6rem 0 0;
  padding-left: 1.1rem;
}
.cell-list li {
  margin: 0.15rem 0;
  line-height: 1.5;
}
.cell-prose {
  margin-top: 0.6rem;
  line-height: 1.6;
  color: var(--c-text);
}
.cell-empty {
  color: var(--c-text-faint);
  font-style: italic;
}

/* Empty-state when filters match nothing */
.reference-empty {
  text-align: center;
  padding: var(--space-xl);
  color: var(--c-text-muted);
  font-size: 0.95rem;
}

/* ---- Legend (color-coded swatches) ---- */
.reference-legend__list {
  list-style: none;
  margin: var(--space-md) 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  font-size: 0.95em;
}
.reference-legend__list > li {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--c-text);
}
.reference-legend__swatch {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.15rem 0.55rem;
  border-radius: 4px;
  line-height: 1;
  color: #ffffff;
  background: var(--swatch-bg, var(--c-surface-alt));
}

/* ---- Generic badge base (per-page palettes layer modifiers on top) ---- */
.reference-badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 4px;
  background: var(--c-surface-alt);
  border: 1px solid var(--c-border);
  font-size: 0.95em;
  white-space: nowrap;
}

/* ---- Custom resize handle (assets/js/resize-handle.js) ----
   The JS sets `resize: none` on opted-in textareas and wraps them in a
   `.resize-host`, then injects a `.resize-handle` grabber with a much larger
   hit area than the native ~12px corner. Works mouse + touch. */
.resize-host {
  position: relative;
  display: block;
}
.resize-host > textarea {
  resize: none;
  display: block;
  width: 100%;
}
.resize-handle {
  position: absolute;
  right: 2px;
  bottom: 2px;
  width: 1.5rem;
  height: 1.5rem;
  cursor: ns-resize;
  touch-action: none;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  padding: 2px;
  color: var(--c-text-faint);
  border-radius: 0 0 4px 0;
  z-index: 2;
}
.resize-handle:hover,
.resize-handle.is-dragging { color: var(--c-accent); }
.resize-handle::after {
  content: "";
  width: 0.85rem;
  height: 0.85rem;
  background:
    linear-gradient(135deg, transparent 0 45%, currentColor 45% 55%, transparent 55% 100%),
    linear-gradient(135deg, transparent 0 70%, currentColor 70% 80%, transparent 80% 100%);
  opacity: 0.8;
}
.resize-handle:focus-visible {
  outline: 2px solid var(--c-accent);
  outline-offset: 2px;
}

/* Tablet — between the phone stack and desktop. The desktop controls strip
   keeps tabs + search on one row, which squeezes the search box on narrow
   tablets. Let the strip wrap and give the search its own full-width line
   below the tabs. (Does NOT reset the sticky table wrapper — that is handled
   by the pointer-coarse rule below so the footer stays reachable.) */
@media (min-width: 40.0625rem) and (max-width: 64rem) {
  .reference-controls { flex-wrap: wrap; }
  .reference-tabs { flex: 1 1 100%; order: 1; }
  .reference-search {
    flex: 1 1 auto;
    max-width: none;
    order: 2;
  }
  .reference-osfilter { order: 3; }
}

/* Small screens — generic responsive stack + opt-in mobile snap behaviour. */
@media (max-width: 40rem) {
  .reference-container { padding: 0 var(--space-md); }
  .reference-controls {
    flex-direction: column;
    align-items: stretch;
    gap: 0.4rem;
    padding: 0.4rem 0;
    margin-bottom: 0.4rem;
  }
  .reference-search { max-width: 100%; flex: 1 1 auto; }
  .reference-osfilter { justify-content: space-between; }
  .reference-osfilter select { flex: 1 1 auto; }

  /* Keep the bounded sticky scroll box on phones too (sticky thead inside,
     sticky command column), so the header stays pinned while scrolling rows —
     same behaviour as desktop. The `max-height` is capped well below the
     viewport so there's always page area beneath the box: scrolling past the
     table reaches the footer, and because the box is a solid-background,
     bounded element with the footer in normal flow below it, the footer can't
     bleed into the table. */
  .reference-table-wrapper {
    max-height: calc(100vh - var(--header-offset, var(--header-height, 3.75rem)) - 3.75rem);
  }
  .reference-table thead th { padding: 0.95rem 0.6rem 0.7rem; }
  .reference-table tbody td { padding: 0.65rem 0.6rem; overflow-wrap: anywhere; }

  /* Opt-in mobile snap: pages add `.has-snap-cols` on the wrapper to get
     column-by-column scroll snapping on top of the wrapper's existing
     horizontal overflow. Useful for very wide tables. */
  .reference-table-wrapper.has-snap-cols {
    overflow-x: auto;
    scroll-snap-type: x mandatory;
  }
  .reference-table-wrapper.has-snap-cols .reference-table thead th:not(.is-sticky-col),
  .reference-table-wrapper.has-snap-cols .reference-table tbody td:not(.is-sticky-col) {
    scroll-snap-align: start;
    scroll-snap-stop: always;
  }
  .reference-table td.is-sticky-col,
  .reference-table th.is-sticky-col {
    box-shadow: 6px 0 6px -4px rgba(0, 0, 0, 0.45);
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .reference-tab,
  .reference-search { transition: none; }
}
