/* abd-components.css - shared UI components (agronegocios-ui-restructure, design D1/D2/D3).
   Markup: templates/components/ui.html. Loaded after layout.css in base.html.
   Built only on layout.css semantic variables and --sh- and --abd- tokens (no hex), so dark mode
   follows [data-theme]. Helvetica comes from the body; never italic, never an all-properties transition. Touch
   targets 44px. Contrast: strong text sh-ink-900 / paper on light/dark (AA), accent underline
   green-dark / green-400 (>= 3:1 against the surface). */

[data-theme="light"] {
  --abd-ink-strong: var(--sh-ink-900);
  --abd-ink-soft:   var(--sh-ink-700);
  --abd-accent:     var(--abd-green-dark);
  --abd-chart-bg:   var(--sh-off-white);
}
[data-theme="dark"] {
  --abd-ink-strong: var(--text-primary);
  --abd-ink-soft:   var(--text-secondary);
  --abd-accent:     var(--abd-green-400);
  --abd-chart-bg:   var(--bg-surface);
}

/* ── Page header + tabs ───────────────────────────────────────────────────── */
.abd-phead { display: flex; flex-direction: column; gap: var(--sh-r-2); margin-bottom: var(--sh-r-4); }
.abd-phead__title { margin: 0; font-size: var(--font-xl); line-height: 1.2; color: var(--abd-ink-strong); }

.abd-tabs {
  position: relative;   /* contains the visually hidden .cos-sr of a tab badge: absolute, it escaped the scroller and widened the page to 667px at 390px */
  display: flex; gap: var(--sh-r-1); overflow-x: auto; scrollbar-width: none;
  border-bottom: 1px solid var(--border-base);
}
.abd-tabs::-webkit-scrollbar { display: none; }
.abd-tab {
  display: inline-flex; align-items: center; gap: var(--sh-r-2); flex: none;
  min-height: 44px; padding: 0 var(--sh-r-4);
  color: var(--abd-ink-soft); font-weight: 600; font-size: var(--font-lg); white-space: nowrap; text-decoration: none;
  border-bottom: 3px solid transparent; margin-bottom: -1px;
}
.abd-tab.hidden { display: none; }
.abd-tab:hover { color: var(--abd-ink-strong); }
.abd-tab.is-active { color: var(--abd-ink-strong); border-bottom-color: var(--abd-tab-line); }
.abd-tab:focus-visible { outline: 2px solid var(--abd-accent); outline-offset: -2px; border-radius: var(--sh-radius-sm); }
.abd-tab__count { min-width: 22px; padding: 0 var(--sh-r-2); text-align: center; font-size: var(--font-base); color: var(--abd-ink-strong); background: var(--bg-surface-2); border-radius: var(--sh-radius-sm); }
.abd-tab__count--mirar { color: var(--abd-mirar-on); background: var(--abd-mirar); font-weight: var(--weight-bold); }      /* pending to review: the number is the badge, amber says "look" (DESIGN.md rule z) */

.abd-tab > svg { width: 16px; height: 16px; flex: none; }      /* an optional feather icon before the label (Cosecha: Envases) */
.abd-tab--grupo { margin-left: var(--sh-r-4); }      /* a gap, not a rule, between the groups of a row (Cosecha: Leer | Operar | Revisar); the server marks the first tab of each group */

/* Second level (context bar, tray states): secondary to the primary tabs, so smaller, lighter and
   squarer (kit anti-pattern 6: no pills). The visible box is 34px; ::after pads the hit area to 44px. */
.abd-tabs--sub { border-bottom: 0; gap: var(--sh-r-2); }
.abd-tabs--sub .abd-tab {
  position: relative; min-height: 34px; padding: 0 var(--sh-r-3); margin-bottom: 0;
  font-size: var(--font-base); font-weight: 500;
  border: 1px solid color-mix(in srgb, var(--border-base) 55%, transparent); border-radius: var(--sh-radius-md);
}
.abd-tabs--sub .abd-tab::after { content: ""; position: absolute; inset: -5px 0; }
.abd-tabs--sub .abd-tab.is-active { font-weight: 600; border-color: var(--abd-accent); background: var(--bg-surface-2); }
.abd-tabs--sub .abd-tab:focus-visible { outline-offset: 2px; }

/* Scroll cue: an edge fades when there is more to scroll to (class set by the header script). */
.abd-tabs.has-more-right, .tp-scroll.has-more-right { -webkit-mask-image: linear-gradient(to right, black calc(100% - 28px), transparent); mask-image: linear-gradient(to right, black calc(100% - 28px), transparent); }
.abd-tabs.has-more-left, .tp-scroll.has-more-left { -webkit-mask-image: linear-gradient(to left, black calc(100% - 28px), transparent); mask-image: linear-gradient(to left, black calc(100% - 28px), transparent); }
.abd-tabs.has-more-left.has-more-right, .tp-scroll.has-more-left.has-more-right { -webkit-mask-image: linear-gradient(to right, transparent, black 28px, black calc(100% - 28px), transparent); mask-image: linear-gradient(to right, transparent, black 28px, black calc(100% - 28px), transparent); }

/* ── Context bar, filters, season pills, status ───────────────────────────── */
.abd-ctx { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sh-r-2) var(--sh-r-4); margin-bottom: var(--sh-r-4); }
.abd-filter { display: inline-flex; flex-direction: column; gap: var(--sh-r-1); }
.abd-filter__label { font-size: var(--font-base); font-weight: 600; color: var(--abd-ink-soft); }
.abd-filter__control { display: inline-flex; align-items: center; gap: var(--sh-r-2); min-height: 44px; }
.abd-pills { display: inline-flex; flex-wrap: wrap; gap: var(--sh-r-2); }
.abd-pills button { min-height: 44px; padding: 0 var(--sh-r-4); border: 1px solid var(--border-base); border-radius: var(--sh-radius-md); background: transparent; color: var(--abd-ink-soft); font-weight: 600; cursor: pointer; }
.abd-pills button[aria-pressed="true"] { color: var(--abd-ink-strong); border-color: var(--abd-accent); background: var(--bg-surface-2); }
.abd-pills button:focus-visible { outline: 2px solid var(--abd-accent); outline-offset: 2px; }

.abd-status { display: inline-flex; align-items: center; gap: var(--sh-r-2); margin-left: auto; font-size: var(--font-base); color: var(--abd-ink-soft); white-space: nowrap; }
.abd-status__dot { width: 8px; height: 8px; border-radius: 50%; background: var(--abd-accent); }
.abd-status[data-state="busy"] .abd-status__dot { background: var(--sh-yellow); animation: abd-pulse 900ms ease-in-out infinite; }
.abd-status[data-state="error"] .abd-status__dot { background: var(--sh-negative); }
@keyframes abd-pulse { 50% { opacity: 0.3; } }

/* ── KPI strip, panel box, ranked list ────────────────────────────────────── */
.abd-kpis { display: grid; grid-template-columns: repeat(var(--abd-kpi-n, 6), minmax(0, 1fr)); border: 1px solid var(--border-base); border-radius: var(--sh-radius-md); background: var(--bg-surface); }
.abd-kpi { min-width: 0; padding: var(--sh-r-3) var(--sh-r-4); border-left: 1px solid var(--border-base); }
.abd-kpi:first-child { border-left: 0; }
.abd-kpi__label { font-size: var(--font-base); color: var(--abd-ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.abd-kpi__val { font-size: var(--font-kpi); font-weight: 700; color: var(--abd-ink-strong); white-space: nowrap; }
.abd-kpi__delta { font-size: var(--font-base); font-weight: 600; }

.abd-box { min-width: 0; padding: var(--sh-r-4); background: var(--abd-chart-bg); border: 1px solid var(--border-base); border-radius: var(--sh-radius-md); }
.abd-box__title { margin: 0; font-size: var(--font-lg); color: var(--abd-ink-strong); }
.abd-box__head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--sh-r-2) var(--sh-r-4); }

/* Segmented control (ui.seg_toggle): the pressed button is told apart by weight, border and fill, not by colour alone. */
.abd-seg { display: inline-flex; border: 1px solid var(--border-base); border-radius: var(--sh-radius-md); overflow: hidden; }
.abd-seg__btn { min-height: 44px; min-width: 64px; padding: 0 var(--sh-r-4); border: 0; background: var(--bg-surface); color: var(--abd-ink-soft); font: inherit; font-size: var(--font-base); cursor: pointer; }
.abd-seg__btn + .abd-seg__btn { border-left: 1px solid var(--border-base); }
.abd-seg__btn:hover { background: var(--bg-surface-2); }
.abd-seg__btn[aria-pressed="true"] { background: var(--bg-surface-2); color: var(--abd-ink-strong); font-weight: 700; box-shadow: inset 0 -2px 0 var(--abd-accent); }
.abd-seg__btn:focus-visible { outline: 2px solid var(--abd-accent); outline-offset: -2px; }
.abd-box__sub { margin: 2px 0 var(--sh-r-3); font-size: var(--font-base); color: var(--abd-ink-soft); }
.abd-box__src { margin: var(--sh-r-3) 0 0; font-size: var(--font-sm); color: var(--abd-ink-soft); }

.abd-rank { display: grid; gap: var(--sh-r-1); }

/* ── Analytics: selector row + ranking table (Resumen de temporada, Productores) ─────────────── */
.tp-selector { display: flex; flex-wrap: wrap; gap: var(--sh-r-4); align-items: flex-end; }
.tp-field { display: flex; flex-direction: column; gap: var(--sh-r-2); min-width: 180px; }
.tp-field select {
  min-height: 44px; padding: 0 var(--sh-r-3); background: var(--bg-surface); color: var(--text-primary);
  border: 1px solid var(--border-base); border-radius: var(--sh-radius-md); font: inherit; font-size: var(--font-md);
}
.tp-field select:focus-visible, .tp-link:focus-visible { outline: 2px solid var(--abd-accent); outline-offset: 2px; }
.tp-scroll { overflow-x: auto; }
.tp-table { width: 100%; border-collapse: collapse; font-size: var(--font-base); }
.tp-table th, .tp-table td { text-align: left; padding: var(--sh-r-3); border-bottom: 1px solid var(--border-base); }
.tp-table th { font-size: var(--font-sm); text-transform: uppercase; letter-spacing: .04em; color: var(--abd-ink-soft); font-weight: 700; }
.tp-table td.num, .tp-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.tp-link {
  display: inline-flex; align-items: center; min-height: 44px; padding: 0; background: none; border: 0; cursor: pointer;
  font: inherit; color: var(--abd-ink-strong); text-align: left;
  text-decoration: underline; text-decoration-color: var(--abd-accent); text-underline-offset: 3px;
}
.tp-link:hover { text-decoration-thickness: 2px; }
/* Phone: five columns with long productor names do not fit 390px, so each row stacks into a card: the name on top,
   the four figures in a 2x2 grid labelled by their data-label (the header row stays for assistive tech, visually hidden). */
@media (max-width: 480px) {
  .tp-table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .tp-table, .tp-table tbody { display: block; }
  .tp-table tr { display: grid; grid-template-columns: 1fr 1fr; gap: var(--sh-r-2) var(--sh-r-4); padding: var(--sh-r-3) 0; border-bottom: 1px solid var(--border-base); }
  .tp-table td { display: block; padding: 0; border: 0; }
  .tp-table td:first-child { grid-column: 1 / -1; }
  .tp-table td.num { text-align: left; }
  .tp-table td[data-label]::before {
    content: attr(data-label); display: block; font-size: var(--font-sm); font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--abd-ink-soft);
  }
}

/* The context bar groups (species / stage / Proceso views) wrap and keep their focus rings. A divider precedes every group
   after the first; the filter slot sits between species and stage, so the selector is the general sibling, not the adjacent one.
   On a phone the groups wrap onto their own rows: no divider there (it would be an orphan line). */
.abd-ctx .abd-tabs--sub { flex-wrap: wrap; overflow: visible; }
@media (min-width: 769px) {
  .abd-ctx .abd-tabs--sub ~ .abd-tabs--sub { padding-left: var(--sh-r-4); border-left: 1px solid var(--border-base); }
}

@media (max-width: 768px) {
  .abd-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .abd-kpi:nth-child(odd) { border-left: 0; }
  .abd-kpi:last-child:nth-child(odd) { grid-column: span 2; }   /* the odd one out (Proceso's 7th) fills the row */
}
@media (prefers-reduced-motion: reduce) {
  .abd-status[data-state="busy"] .abd-status__dot { animation: none; }
}

/* ── Auto-applied filters (PR7): status retry, active-filter chips, phone bottom sheet ────────────────── */
.abd-status[hidden] { display: none; }
.abd-status__retry { min-height: 44px; margin-left: var(--sh-r-2); padding: 0 var(--sh-r-2); background: none; border: 0; cursor: pointer; font: inherit; font-weight: 600; color: var(--abd-ink-strong); text-decoration: underline; text-decoration-color: var(--abd-accent); text-underline-offset: 3px; }
.abd-status__retry:focus-visible, .abd-chip__x:focus-visible, .abd-chips__clear:focus-visible, .abd-filters-btn:focus-visible, .abd-sheet__done:focus-visible, .ms-trigger:focus-visible { outline: 2px solid var(--abd-accent); outline-offset: 2px; }

/* The chips take their own row under the sub-tabs so a changing count never shifts the tabs. */
.abd-chips { flex: 1 1 100%; display: flex; flex-wrap: wrap; align-items: center; gap: var(--sh-r-2); min-height: 44px; }
.abd-chips:empty { display: none; }
.abd-chip { display: inline-flex; align-items: center; min-height: 32px; padding: 0 var(--sh-r-3); gap: var(--sh-r-1); border: 1px solid var(--border-base); border-radius: var(--sh-radius-md); background: var(--bg-surface); color: var(--abd-ink-strong); font-size: var(--font-base); font-weight: 500; }
.abd-chip__x { position: relative; display: inline-flex; align-items: center; justify-content: center; width: 24px; height: 24px; padding: 0; background: none; border: 0; border-radius: var(--sh-radius-md); color: var(--abd-ink-soft); cursor: pointer; }
.abd-chip__x::after { content: ""; position: absolute; inset: -10px; }
.abd-chip__x:hover { color: var(--abd-ink-strong); background: var(--bg-surface-2); }
.abd-chip__x i, .abd-chip__x svg { width: 14px; height: 14px; }
.abd-chips__clear { min-height: 44px; padding: 0 var(--sh-r-2); background: none; border: 0; cursor: pointer; font: inherit; font-weight: 600; color: var(--abd-ink-soft); text-decoration: underline; text-underline-offset: 3px; }
.abd-chips__clear:hover { color: var(--abd-ink-strong); }

/* While a refresh is in flight the numbers dim slightly; nothing moves (no skeletons, no spinner). */
body:has(.abd-status[aria-busy="true"]) :is(.kpi-card .kpi-value, .abd-kpi__val) { opacity: .55; }
:is(.kpi-card .kpi-value, .abd-kpi__val) { transition: opacity 150ms ease-out; }

.abd-filters-btn, .abd-sheet__bar { display: none; }

/* End-of-row slot of the context bar: status, "Limpiar filtros" and (phone) the "Filtros" button live together.
   In the DOM it follows the species switcher, so a phone shows it on that row; on desktop `order` sends it to the end. */
.abd-ctx__end { display: inline-flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: var(--sh-r-1) var(--sh-r-3); margin-left: auto; }
.abd-ctx__end .abd-status { margin-left: 0; }
@media (max-width: 768px) { .abd-ctx__end[data-align="start"] { margin-left: 0; justify-content: flex-start; } }   /* producers: no species switcher, so the row starts at the left */
.abd-ctx__end .abd-chips__clear { display: none; }
@media (min-width: 769px) {
  .abd-ctx__end { order: 99; }
  .abd-ctx__end .abd-chips__clear:not([hidden]) { display: inline-flex; align-items: center; }
  .abd-chips { display: none; }                      /* the visible panel already shows the selection */
}
@media (max-width: 768px) {
  /* Same box as the stage pills beside it: 34px visible, 44px hit area. */
  .abd-filters-btn:not([hidden]) { position: relative; display: inline-flex; align-items: center; min-height: 34px; padding: 0 var(--sh-r-3); border: 1px solid color-mix(in srgb, var(--border-base) 55%, transparent); border-radius: var(--sh-radius-md); background: transparent; color: var(--abd-ink-strong); font: inherit; font-size: var(--font-base); font-weight: 500; cursor: pointer; }
  .abd-filters-btn:not([hidden])::after { content: ""; position: absolute; inset: -5px 0; }
  .abd-sheet { overscroll-behavior: contain; position: fixed; left: 0; right: 0; bottom: 0; z-index: 121; max-height: 85vh; overflow-y: auto; margin: 0; border-radius: var(--sh-radius-md) var(--sh-radius-md) 0 0; transform: translateY(100%); visibility: hidden; transition: transform 200ms ease-out, visibility 0s linear 200ms; }
  .abd-sheet.is-open { transform: none; visibility: visible; transition: transform 200ms ease-out, visibility 0s; }
  .abd-sheet__bar { position: sticky; top: calc(-1 * var(--sh-r-4)); z-index: 1; display: flex; align-items: center; justify-content: space-between; margin: calc(-1 * var(--sh-r-4)) calc(-1 * var(--sh-r-4)) var(--sh-r-3); padding: var(--sh-r-3) var(--sh-r-4); background: var(--bg-surface); border-bottom: 1px solid var(--border-base); }
  .abd-sheet__title { font-weight: 700; color: var(--abd-ink-strong); }
  .abd-sheet__done { min-height: 44px; padding: 0 var(--sh-r-5); border: 0; border-radius: var(--sh-radius-md); background: var(--abd-green-800); color: var(--sh-white); font: inherit; font-weight: 600; cursor: pointer; }
  .abd-sheet-scrim { position: fixed; inset: 0; z-index: 120; background: var(--overlay-bg); }
}
@media (prefers-reduced-motion: reduce) {
  .abd-sheet, .abd-sheet.is-open, .kpi-card .kpi-value, .abd-kpi__val { transition: none; }
}

/* EERR: related-companies select (same box as .ms-trigger) + its provisional-list note */
.fp-select {
  width: 100%; min-height: 38px; padding: 6px 10px; font: inherit; font-size: var(--font-sm);
  color: var(--text-secondary); background: var(--bg-surface-2);
  border: 1.5px solid var(--border-base); border-radius: 8px;
}
.fp-select:hover { border-color: var(--border-focus); }
.fp-select:focus-visible { outline: 2px solid var(--abd-accent); outline-offset: 2px; }
.fp-filter-note { margin: 10px 0 0; font-size: var(--font-xs); color: var(--text-secondary); }

/* ── Zebra: every data table, portal-wide (Joaquín, 2026-11-09) ─────────────────────────────
   One rule for the whole portal. Alternate body rows take --abd-zebra (opaque, so sticky cells keep
   covering what scrolls under them); hover takes --abd-row-hover.
   Specificity is ZERO on purpose (:where): any state row (.cos-fila--mirar, .is-off, .ar-actual, group
   rows...) or table-specific rule wins over the stripe, so colour states are never repainted.
   Cells that paint their own opaque background (a sticky column, .pp-table td) are covered by the
   explicit selectors below. Opt out with .no-zebra (layout tables, nested sub-tables, mobile cards). */
:where(.content-area table:not(.no-zebra, [role="presentation"], .pp-subtable, .cos-traz)) > tbody > tr:nth-child(even) { background: var(--abd-zebra); }
:where(.content-area table:not(.no-zebra, [role="presentation"], .pp-subtable)) > tbody > tr:hover { background: var(--abd-row-hover); }
/* Tables whose cells paint their own background */
.pp-table tbody tr:nth-child(even):not(.pp-sub) > td { background: var(--abd-zebra); }
.pp-table tbody tr.pp-row:hover > td { background: var(--abd-row-hover); }
.cos-tabla--fija:not(.cos-tabla--num) tbody tr:nth-child(even) > th, .cos-plan tbody tr:nth-child(even):not(.is-off) > th { background: var(--abd-zebra); }   /* sticky first column: opaque, follows the stripe */
.cos-tabla--fija:not(.cos-tabla--num) tbody tr:hover > th, .cos-plan tbody tr:not(.is-off):hover > th { background: var(--abd-row-hover); }
.cos-tabla--num:not(.cos-traz) tbody tr:nth-child(even):not(.cos-fila--mirar) > * { background: var(--abd-zebra); }
.cos-tabla--num:not(.cos-traz) tbody tr:not(.cos-fila--mirar):hover > * { background: var(--abd-row-hover); }
@media (max-width: 640px) {   /* stacked cards: the card (tr) is the unit, its cells stay transparent */
  .cos-tabla--tarjeta tbody tr:nth-child(even):not(.cos-fila--mirar) { background: var(--abd-zebra); }
  .cos-tabla--tarjeta tbody tr:nth-child(even):not(.cos-fila--mirar) > * { background: transparent; }
}
