/* primitives/php/primitives.css
 *
 * The stylesheet the PHP primitives' markup assumes. Load AFTER tokens.css and
 * BEFORE the project's brand override:
 *
 *   tokens.css  ->  primitives.css  ->  brand.css
 *
 * There is not a single literal colour in this file, on purpose and enforced by
 * test/tokens.test.mjs. If a primitive could hardcode #f5f5f5, the grey the
 * operator objected to would be reachable through the library itself and
 * UI-FLD-001 would pass while the screen still looked wrong.
 */

/* ============================================================
 * Type stack. Without this every primitive inherits the browser's
 * default serif (Times), which reads as an unfinished draft no matter
 * how correct the layout is. Applied to the primitives' own roots
 * rather than to `body`, so vendoring this file into an existing page
 * cannot restyle markup the library does not own.
 * ============================================================ */
.ui-btn,
.ui-field,
.ui-search,
.ui-alert,
.ui-table-card,
.ui-page-size,
.ui-zoom,
.ui-list-head,
.ui-presets {
  font-family: var(--font-stack);
}

/* ============================================================
 * Button — UI-BTN-001: the underline dies in the base class, so a
 * caller can never forget it. A link styled as a button carries
 * data-ui="button" and lands here too.
 * ============================================================ */
.ui-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius-control);
  font-size: var(--font-size-body);
  letter-spacing: var(--tracking-body);
  line-height: var(--leading-body);
  text-decoration: none;
  cursor: pointer;
}

.ui-btn:disabled,
.ui-btn[disabled] {
  background: var(--disabled-bg);
  color: var(--disabled-ink);
  cursor: not-allowed;
}

.ui-btn:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.ui-btn--primary {
  background: var(--focus-ring);
  color: var(--ink-on-accent);
}

.ui-btn--secondary {
  background: var(--surface);
  border-color: var(--hairline);
  color: var(--ink);
}

.ui-btn--secondary:hover {
  background: var(--hover-bg);
}

.ui-btn--danger {
  background: var(--danger);
  color: var(--ink-on-accent);
}

/* ============================================================
 * Field — UI-FLD-001: the background is --field-bg and nothing else.
 * There is no grey to pick because no literal colour exists here.
 * ============================================================ */
.ui-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  margin-bottom: var(--space-4);
}

.ui-field__label {
  font-size: var(--font-size-label);
  letter-spacing: var(--tracking-label);
  line-height: var(--leading-label);
  color: var(--ink-muted);
}

.ui-field__input,
.ui-search {
  background: var(--field-bg);
  color: var(--ink);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-control);
  padding: var(--space-2) var(--space-3);
  font-size: var(--font-size-body);
  letter-spacing: var(--tracking-body);
  line-height: var(--leading-body);
}

.ui-field__input:focus-visible,
.ui-search:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}

.ui-field__input[aria-invalid="true"] {
  border-color: var(--danger);
}

/* An error message styled like a label reads as helper text and gets
 * skipped. It carries the danger colour and a little weight so the eye
 * lands on it — and it is bound to the input by aria-describedby, so a
 * screen reader announces it with the field rather than as loose text. */
.ui-field__error {
  margin: 0;
  font-size: var(--font-size-small);
  letter-spacing: var(--tracking-small);
  font-weight: 500;
  color: var(--danger);
}

/* ============================================================
 * Alert
 * ============================================================ */
.ui-alert {
  --ui-alert-tone: var(--info);
  position: relative;
  padding: var(--space-3) var(--space-4) var(--space-3) calc(var(--space-4) + 24px);
  border: 0;
  border-radius: var(--radius-control);
  background: color-mix(in srgb, var(--ui-alert-tone) 12%, var(--surface));
  color: var(--ink);
  margin-bottom: var(--space-4);
}

/* UI-ALR-004: the tone is a tinted fill plus a leading icon — never a side
 * stripe. The icon is a ::before mask painted in the tone colour, so it also
 * survives callers that set textContent on the box. */
.ui-alert::before {
  content: "";
  position: absolute;
  left: var(--space-4);
  top: calc(var(--space-3) + 2px);
  width: 16px;
  height: 16px;
  background-color: var(--ui-alert-tone);
  -webkit-mask: var(--ui-alert-icon) center / contain no-repeat;
  mask: var(--ui-alert-icon) center / contain no-repeat;
}

.ui-alert__title {
  margin: 0 0 var(--space-1);
  font-size: var(--font-size-body);
  letter-spacing: var(--tracking-body);
  font-weight: 600;
  color: var(--ui-alert-tone);
}

.ui-alert__body {
  margin: 0;
  font-size: var(--font-size-body);
  letter-spacing: var(--tracking-body);
  line-height: var(--leading-body);
}

/* A one-line alert has no title to carry the tone, so the message does. */
.ui-alert__body:first-child {
  color: var(--ui-alert-tone);
  font-weight: 600;
}

/* The tone must be legible at a glance: a "saved" alert and a "failed" one
 * that look alike communicate nothing. Fill, icon colour and title all follow
 * the tone, and the icon SHAPE differs per tone, so the signal survives for a
 * viewer who cannot tell the hues apart. */
.ui-alert--info { --ui-alert-tone: var(--info); --ui-alert-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='10'/%3E%3Cline x1='12' y1='16' x2='12' y2='12'/%3E%3Cline x1='12' y1='8' x2='12.01' y2='8'/%3E%3C/svg%3E"); }
.ui-alert--success { --ui-alert-tone: var(--success); --ui-alert-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='10'/%3E%3Cpolyline points='8 12.5 11 15.5 16 9.5'/%3E%3C/svg%3E"); }
.ui-alert--warning { --ui-alert-tone: var(--warning); --ui-alert-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M10.3 3.9 1.8 18a2 2 0 0 0 1.7 3h17a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0z'/%3E%3Cline x1='12' y1='9' x2='12' y2='13'/%3E%3Cline x1='12' y1='17' x2='12.01' y2='17'/%3E%3C/svg%3E"); }
.ui-alert--error { --ui-alert-tone: var(--danger); --ui-alert-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='10'/%3E%3Cline x1='12' y1='8' x2='12' y2='12'/%3E%3Cline x1='12' y1='16' x2='12.01' y2='16'/%3E%3C/svg%3E"); }

/* ============================================================
 * Table — UI-TBL-007 (search top-left, toolbar first) and
 * UI-TBL-006 (page-size top-right) are expressed as layout here;
 * the PHP guarantees the source order they depend on.
 * ============================================================ */
.ui-table-card {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-control);
  overflow: hidden;
}

.ui-table__toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--hairline);
}

.ui-table__toolbar-left  { display: flex; align-items: center; gap: var(--space-2); }
.ui-table__toolbar-right { display: flex; align-items: center; gap: var(--space-2); }

/* A wide table scrolls inside its own box; the page body never scrolls
 * horizontally. */
.ui-table__scroll { overflow-x: auto; }

.ui-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--font-size-body);
  letter-spacing: var(--tracking-body);
  line-height: var(--leading-body);
  color: var(--ink);
}

.ui-table th {
  text-transform: uppercase;
  font-size: var(--font-size-label);
  letter-spacing: var(--tracking-label);
  color: var(--ink-muted);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--hairline);
}

.ui-table td {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--hairline);
}

/* UI-ALN-001 — alignment follows the ROLE of the datum in body and totals
 * cells. UI-TBL-013 — every HEADER is centred regardless (operator,
 * 2026-09-26: "los números alineados a la derecha, los precios, pero los
 * headers siempre van centrados"), so the th rule comes last and wins. */
.ui-table [data-ui-col="amount"] { text-align: right; font-variant-numeric: tabular-nums; }
.ui-table [data-ui-col="name"]   { text-align: left; }
.ui-table [data-ui-col="other"]  { text-align: center; }
.ui-table th[data-ui-col]        { text-align: center; }

.ui-table__empty {
  text-align: center;
  color: var(--ink-muted);
}

.ui-table__empty td { padding: var(--space-6) var(--space-3); }

.ui-page-size {
  background: var(--field-bg);
  color: var(--ink);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-control);
  padding: var(--space-1) var(--space-2);
  font-size: var(--font-size-small);
}

/* UI-TBL-022: the range sits immediately LEFT OF the pager arrows and shares
   their corner, so position, range and total are ONE glance. Operator, verbatim:
   "esto deberia ir a la derecha al lado de las flechas para verlo de una sola
   mirada". An earlier revision used justify-content: space-between, which pushed
   the range to the far left edge and split one reading across the whole card. */
.ui-table__pager-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
}

.ui-table__range {
  font-size: var(--font-size-small);
  letter-spacing: var(--tracking-small);
  color: var(--ink-muted);
  font-variant-numeric: tabular-nums;
}

.ui-table__pager {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
}

/* ============================================================
 * Zoom control — UI-A11Y-001: it must NOT scale with the scale it
 * controls, so it stays reachable at any size. Fixed px on purpose;
 * this is the one place --ui-scale is deliberately not read.
 *
 * UI-A11Y-006: top-RIGHT, clear of the viewport edges, with the
 * percentage BETWEEN the two symbols. Operator, verbatim: "eso debería
 * estar siempre arriba a la derecha, y con un número que indique el
 * porcentaje en el medio de los dos símbolos."
 * ============================================================ */
.ui-zoom {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  height: 28px;
}

/* Pinned to the viewport's top-right. The inset is fixed px, like the
 * rest of this control: an inset riding --ui-scale would drift the
 * control toward the centre as the user scales up — away from the
 * corner the norm puts it in. */
.ui-zoom--fixed {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 100;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-control);
  padding: 2px 4px;
}

/* The readout sits between the symbols and must not resize as the digits
 * change (100% -> 90% would otherwise shift both buttons under the
 * cursor mid-press). Tabular figures plus a min-width hold it still. */
.ui-zoom__value {
  min-width: 4ch;
  text-align: center;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--ink-muted);
  user-select: none;
}

/* The [-][+] buttons inside the zoom control are the one place .ui-btn's
 * scale-riding padding and font-size must be overridden: UI-A11Y-001 requires
 * the control that CHANGES the text size to keep its own box fixed, or it walks
 * off the screen at large scales. Fixed px here is deliberate, not an oversight.
 */
.ui-zoom .ui-btn {
  padding: 2px 6px;
  font-size: 14px;
}

/* ============================================================
 * Motion — honour the OS preference. Duration tokens do not exist
 * yet (deferred to a later phase); until they do, this zeroes the
 * transitions the primitives above actually declare.
 * ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .ui-btn,
  .ui-field__input,
  .ui-search {
    transition: none;
  }
}

/* ============================================================
 * Table — the "Ver" handle and the "#" column (UI-TBL-012/016/017)
 *
 * The Ver cell is sticky at the left edge with an OPAQUE background so it
 * never turns transparent over scrolled content (operator, verbatim in the
 * norm). The row number renders in body ink, never the faintest tone
 * (UI-TBL-016, operator: "los numeros no los pongas con esa fuente clara").
 * ============================================================ */
.ui-visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.ui-table .ui-table__view {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--surface);
  width: 1%;
  white-space: nowrap;
}

.ui-table .ui-table__num {
  width: 1%;
  white-space: nowrap;
}

.ui-table [data-ui="row-num"] {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

/* UI-BTN-005: "Ver" is a filled accent pill, never an underlined link. */
.ui-btn[data-ui-view] {
  border-radius: 999px;
  padding: var(--space-1) var(--space-3);
  font-size: var(--font-size-small);
  letter-spacing: var(--tracking-small);
  line-height: var(--leading-small);
}

/* ============================================================
 * Table — sortable headers (UI-TBL-008). The indicator is CSS so the
 * PHP emits only the state (aria-sort); a consuming project's shared
 * script turns the click into a sorted request.
 * ============================================================ */
.ui-table th[data-ui-sort] {
  cursor: pointer;
  user-select: none;
}

.ui-table th[data-ui-sort]::after {
  content: "\2195";
  margin-left: var(--space-1);
  opacity: .45;
  font-size: .85em;
}

.ui-table th[aria-sort="ascending"]::after  { content: "\2191"; opacity: 1; }
.ui-table th[aria-sort="descending"]::after { content: "\2193"; opacity: 1; }

.ui-table th:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: -2px;
}

/* Hidden columns (per-user preference, UI-TBL-011) stay in the DOM so the
 * column picker can list them; the attribute alone is not enough once the
 * mobile rules below give cells display: flex. */
.ui-table th[hidden],
.ui-table td[hidden] {
  display: none;
}

/* ============================================================
 * Status pill — UI-TBL-023. The dot carries the tone, the label names it.
 * Tones are the structural state colours; neutral is the muted ink.
 * ============================================================ */
.ui-status {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-3);
  border-radius: 999px;
  background: var(--hover-bg);
  color: var(--ink);
  font-size: var(--font-size-small);
  letter-spacing: var(--tracking-small);
  line-height: var(--leading-small);
  font-weight: 500;
  white-space: nowrap;
}

.ui-status__dot {
  width: .5em;
  height: .5em;
  border-radius: 50%;
  background: var(--ink-muted);
  flex: 0 0 auto;
}

.ui-status--success { color: var(--success); }
.ui-status--warning { color: var(--warning); }
.ui-status--danger  { color: var(--danger); }
.ui-status--info    { color: var(--info); }
.ui-status--neutral { color: var(--ink-muted); }

.ui-status--success .ui-status__dot { background: var(--success); }
.ui-status--warning .ui-status__dot { background: var(--warning); }
.ui-status--danger  .ui-status__dot { background: var(--danger); }
.ui-status--info    .ui-status__dot { background: var(--info); }

/* ============================================================
 * Presets — UI-TBL-024. Exactly one pill is filled; the rest are outlined.
 * ============================================================ */
.ui-presets {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.ui-preset {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--hairline);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  text-decoration: none;
  font-size: var(--font-size-small);
  letter-spacing: var(--tracking-small);
  line-height: var(--leading-small);
}

.ui-preset:hover { background: var(--hover-bg); }

.ui-preset[aria-current="true"] {
  background: var(--focus-ring);
  border-color: var(--focus-ring);
  color: var(--ink-on-accent);
}

.ui-preset__count {
  opacity: .8;
  font-variant-numeric: tabular-nums;
}

/* ============================================================
 * List head — UI-TBL-021. Title + "N registros", one glance.
 * ============================================================ */
.ui-list-head {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

.ui-list-head__title {
  margin: 0;
  font-size: var(--font-size-heading);
  letter-spacing: var(--tracking-heading);
  line-height: var(--leading-heading);
  font-weight: 600;
  color: var(--ink);
}

.ui-list-head__count {
  color: var(--ink-muted);
  font-size: var(--font-size-small);
  letter-spacing: var(--tracking-small);
  font-variant-numeric: tabular-nums;
}

/* ============================================================
 * Mobile — UI-TBL-018. A wide table collapses into stacked cards, one per
 * row, each cell labelled by its column (the data-label the PHP emits).
 * Operator, verbatim: "SIEMPRE TODO DEBE SER RESPONSIVE". The header row
 * is kept for assistive tech but taken out of the visual flow.
 * ============================================================ */
@media (max-width: 640px) {
  .ui-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
  }

  .ui-table,
  .ui-table tbody,
  .ui-table tfoot,
  .ui-table tr {
    display: block;
  }

  .ui-table tbody tr {
    margin: var(--space-3);
    padding: var(--space-2) 0;
    border: 1px solid var(--hairline);
    border-radius: var(--radius-control);
  }

  .ui-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-3);
    border-bottom: none;
  }

  .ui-table td[data-label]::before {
    content: attr(data-label);
    flex: 0 0 auto;
    text-transform: uppercase;
    font-size: var(--font-size-label);
    letter-spacing: var(--tracking-label);
    color: var(--ink-muted);
  }

  .ui-table td[hidden] { display: none; }

  /* The handle row of the card: Ver on the left, # beside it. */
  .ui-table .ui-table__view { position: static; width: auto; display: inline-flex; }
  .ui-table .ui-table__num  { width: auto; display: inline-flex; }
  .ui-table td.ui-table__num::before { content: "#"; }

  .ui-table__empty td { justify-content: center; }
}
