/* ── Form controls: styled by element so plain markup looks right ───────────── */
input:not([type]), input[type="text"], input[type="search"], input[type="number"], input[type="date"], input[type="email"], input[type="password"], input[type="tel"], input[type="url"], input[type="time"], input[type="datetime-local"], input[type="month"], select, textarea {
  min-height: var(--space-5);
  padding: 0 var(--space-2);
  color: var(--text-primary);
  background: var(--bg-card);
  border: var(--hairline) solid var(--border-control);
  border-radius: var(--radius-sm);
  font-family: var(--font-body);
  font-size: var(--font-size-sm);
  transition: border-color var(--transition-base);
}
/* Search: magnifier drawn as a background (stroke = --text-muted of each theme), wider than a plain field. */
input[type="search"] { padding-left: 40px; background: var(--bg-card) var(--icon-search) no-repeat 12px center / 16px 16px; }
input[type="search"]::-webkit-search-cancel-button { cursor: pointer; }
textarea { padding: var(--space-1) var(--space-2); }
input:hover, select:hover, textarea:hover { border-color: var(--color-accent-text); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--color-accent); box-shadow: 0 0 0 1px var(--color-accent); }
input::placeholder { color: var(--text-muted); }
input:disabled, select:disabled, textarea:disabled { opacity: .6; cursor: not-allowed; }
/* Autofill: keep the card background/text instead of the browser's yellow/blue (password managers fill login fields). */
input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus {
  -webkit-text-fill-color: var(--text-primary);
  box-shadow: 0 0 0 100px var(--bg-card) inset;
  caret-color: var(--text-primary);
}
select option { background: var(--bg-card); color: var(--text-primary); }

.field { display: grid; gap: 4px; color: var(--text-muted); font-size: var(--font-size-xs); }

/* ── Filter bar ──────────────────────────────────────────────────────────────── */
.filter-bar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: var(--space-2); margin-bottom: var(--space-2); }
.filter-bar label { display: grid; gap: 4px; color: var(--text-muted); font-size: var(--font-size-xs); font-weight: var(--font-weight-medium); letter-spacing: var(--letter-spacing-label); text-transform: uppercase; }
.filter-bar label.check { display: inline-flex; align-items: center; gap: var(--space-1); min-height: var(--space-5); cursor: pointer; }
.filter-bar input[type="checkbox"] { min-width: 0; width: var(--icon-size-sm); height: var(--icon-size-sm); margin: 0; accent-color: var(--color-accent); }
.filter-bar input, .filter-bar select { min-width: 160px; text-transform: none; letter-spacing: normal; }
.filter-bar input[type="search"] { min-width: min(100%, 320px); width: 24rem; max-width: 100%; }

/* ── Cards ───────────────────────────────────────────────────────────────────── */
.card, .stat-card, .item-card {
  padding: var(--space-2);
  background: var(--bg-card);
  border: var(--hairline) solid var(--border-color);
  border-radius: var(--radius-lg);
}
.card__title { margin: 0 0 var(--space-2); }
.stat-card { display: grid; align-content: start; gap: 4px; container-type: inline-size; }
.stat-card > p { margin: 0; }
.stat-card__label { color: var(--text-muted); font-size: var(--font-size-xs); letter-spacing: var(--letter-spacing-label); text-transform: uppercase; }
.stat-card__value { font-family: var(--font-heading); font-size: clamp(var(--font-size-md), 11cqi, var(--font-size-xl)); font-weight: var(--font-weight-bold); color: var(--color-accent-text); font-variant-numeric: tabular-nums; white-space: nowrap; }
.stat-card__hint { color: var(--text-secondary); font-size: var(--font-size-sm); }

/* ── Tabs ────────────────────────────────────────────────────────────────────── */
.tabs { display: flex; gap: var(--space-1); margin-bottom: var(--space-2); border-bottom: var(--hairline) solid var(--border-color); }
.tab {
  padding: var(--space-1) var(--space-2);
  color: var(--text-secondary);
  border-bottom: 2px solid transparent;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  text-decoration: none;
}
.tab:hover { color: var(--text-primary); }
.tab.tab--active, .tab[aria-current="page"] { color: var(--color-accent-text); border-bottom-color: var(--color-accent-text); }

/* ── Badges ──────────────────────────────────────────────────────────────────── */
.badge { display: inline-block; padding: 2px var(--space-1); border-radius: var(--radius-lg); background: rgba(100, 100, 100, 0.2); color: var(--text-secondary); font-size: var(--font-size-xs); font-weight: var(--font-weight-semibold); }
.badge.badge--accent { background: var(--color-accent-muted); color: var(--color-accent-text); }
.badge.badge--success { background: rgba(56, 175, 100, 0.15); color: var(--color-success-text); }
.badge.badge--warning { background: rgba(255, 160, 0, 0.15); color: var(--color-warning-fg); }
.badge.badge--danger { background: rgba(224, 85, 85, 0.15); color: var(--color-danger-text); }
.badge.badge--neutral { background: rgba(100, 100, 100, 0.2); color: var(--text-secondary); }

/* ── Data grid ───────────────────────────────────────────────────────────────── */
.grid-bar, .grid-pager { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-1); margin-bottom: var(--space-2); }
.grid-bar { justify-content: space-between; }
.grid-pager { justify-content: flex-end; margin: var(--space-2) 0 0; }
.grid-pager__gap { color: var(--text-muted); }
.grid thead th a.sort { color: inherit; text-decoration: none; display: inline-flex; align-items: center; gap: 2px; }
.grid thead th a.sort:hover, .grid thead th[aria-sort] a.sort { color: var(--color-accent); }
.grid thead th a.sort .material-icons { font-size: 1.2em; }
.grid td.wrap { white-space: normal; }
.grid-summary { color: var(--text-secondary); font-size: var(--font-size-sm); }

/* One scroller per page: .shell__content > .page is a flex column and the grid takes the space left under the
   header/filters/totals (flex: 1), so the page itself does not scroll and both grid scrollbars stay on screen.
   Edge cue (shadow where more columns exist): local/scroll background-attachment trick on the scroller. */
.table-wrapper {
  overflow-x: auto;
  border: var(--hairline) solid var(--border-color);
  /* Same corner as .btn: a grid is a working surface, not a floating card, and --radius-lg read as too pronounced
     against the controls above it. */
  border-radius: var(--radius-sm);
  background:
    linear-gradient(to right, var(--bg-card) 30%, transparent) left center / 40px 100% no-repeat local,
    linear-gradient(to left, var(--bg-card) 30%, transparent) right center / 40px 100% no-repeat local,
    linear-gradient(to right, var(--edge-shadow), transparent) left center / 14px 100% no-repeat scroll,
    linear-gradient(to left, var(--edge-shadow), transparent) right center / 14px 100% no-repeat scroll,
    var(--bg-card);
}
.grid-scroll { overflow: auto; }
.shell__content > .page { display: flex; flex-direction: column; flex: 1 0 auto; min-height: 100%; }
.shell__content > .page > .grid-scroll { flex: 1 1 0; min-height: 200px; }
.grid-scroll:focus-visible { outline-offset: -2px; }

.grid { width: 100%; border-collapse: collapse; font-size: var(--font-size-sm); }
.grid th, .grid td { padding: 0 var(--space-2); height: 36px; line-height: var(--line-height-base); text-align: left; white-space: nowrap; border-bottom: var(--hairline) solid var(--border-color); }
.grid thead th { position: sticky; height: 40px; top: 0; z-index: 1; background: var(--bg-grid-head); box-shadow: 0 1px 0 var(--border-control), 0 3px 6px var(--edge-shadow); color: var(--text-muted); font-size: var(--font-size-xs); font-weight: var(--font-weight-medium); letter-spacing: var(--letter-spacing-label); text-transform: uppercase; }
/* .grid--dense is a kept no-op alias: every grid is dense (36 px rows, 40 px header). */
/* First column pinned while scrolling sideways. */
.grid--sticky-first th:first-child, .grid--sticky-first td:first-child { position: sticky; left: 0; background: var(--bg-card); box-shadow: 1px 0 0 var(--border-color); }
.grid--sticky-first thead th:first-child { z-index: 2; background: var(--bg-grid-head); box-shadow: 1px 0 0 var(--border-control), 0 3px 6px var(--edge-shadow); }
.grid--sticky-first tbody tr:hover td:first-child { background: var(--row-hover); }
.grid--sticky-first tr.grid__total td:first-child, .grid--sticky-first tfoot td:first-child { background: var(--bg-surface); }
.grid th.num, .grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
/* Amounts: zero is a muted dash (the figure stays in the DOM for screen readers); negatives (credit notes) are marked. */
.grid .zero { color: var(--text-muted); }
.grid td.num--neg { color: var(--color-danger-text); }
/* Plan de cuentas: heading (type X) rows are bold; type codes are badges. */
.grid tr.grid__group td { font-weight: var(--font-weight-bold); }
.grid .badge { min-width: 24px; text-align: center; }
.grid tbody tr { transition: background var(--transition-base); }
.grid tbody tr:hover { background: var(--row-hover); }
.grid tbody tr:last-child td { border-bottom: 0; }
.grid tfoot td, .grid tr.grid__total td { background: var(--bg-surface); font-weight: var(--font-weight-semibold); border-top: var(--hairline) solid var(--border-color); }
/* Journal: subdiary heading = strong band; voucher "Total" = quiet; repeated voucher numbers dimmed (text kept for AT). */
.grid--sticky-first tr.jg-row[data-level="1"] td, .grid--sticky-first tr.jg-row[data-level="1"] td:first-child { background: var(--bg-grid-head); color: var(--color-accent-text); font-family: var(--font-heading); letter-spacing: var(--letter-spacing-heading); font-weight: var(--font-weight-bold); border-top: 2px solid var(--border-control); }
.journal-grid tr.jg-flat-total td { font-weight: var(--font-weight-medium); color: var(--text-secondary); }
/* Journal: one-line totals; memo is Concar's fixed 28-char padded field, keep every space */
.journal-strip { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 var(--space-1); margin: 0 0 var(--space-1); padding: var(--space-1) 0; font-size: var(--font-size-sm); font-variant-numeric: tabular-nums; border-bottom: var(--hairline) solid var(--border-color); }
.journal-strip strong { color: var(--color-accent-text); font-weight: var(--font-weight-semibold); }
.journal-strip__totals { margin-left: auto; }
.grid td.memo { white-space: pre; min-width: calc(28ch + 2 * var(--space-2)); font: inherit; }

.page-empty__action { display: inline-block; margin-top: var(--space-1); font-weight: var(--font-weight-semibold); }
/* KPI cards sit at the top of their cell instead of stretching to the tallest sibling. */
.card-list { align-items: start; }
.card-list__caption { margin: 0 0 var(--space-1); } /* one scope caption above the cards (Purchases) instead of a hint on a single card */
.card-list:has(> .stat-card) { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }

@media (max-width: 768px) {
  .card, .stat-card, .item-card { border-radius: var(--radius-md); }
  /* .table-wrapper keeps --radius-sm at every width: the grid is the same working surface on a phone. */
  /* Phones: KPI cards two per row; a grid is one screen tall (page scrolls to it, the grid scrolls inside). */
  .card-list:has(> .stat-card) { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-1); }
  .stat-card { padding: 12px; }
  .shell__content > .page > .grid-scroll { flex: none; height: calc(100dvh - var(--topbar-height) - var(--space-3)); min-height: 280px; }
  .filter-bar label:not(.check), .filter-bar input:not([type="checkbox"]), .filter-bar select { width: 100%; min-width: 0; }
  .filter-bar .btn { width: 100%; }
  .journal-strip__totals { margin-left: 0; width: 100%; }
}

/* Journal tree (outline grouping). Off by default: header rows of vouchers, counts, sums and toggles only exist when .is-grouped. */
.journal-tree-bar { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); margin: 0 0 var(--space-1); }
.journal-tree-bar[hidden], .journal-tree-bar [hidden] { display: none; }
.journal-tree-bar__actions, .journal-tree-bar__levels { display: inline-flex; flex-wrap: wrap; align-items: center; gap: var(--space-1); }
.journal-tree-bar .btn[aria-pressed="true"] { background: var(--color-accent-muted); border-color: var(--color-accent-text); color: var(--color-accent-text); }
.journal-grid .tree-toggle, .journal-grid .jg-count, .journal-grid .jg-sum, .journal-grid .jg-voucher { display: none; }
.journal-grid.is-grouped .tree-toggle { display: inline-flex; }
.journal-grid.is-grouped .jg-count, .journal-grid.is-grouped .jg-sum { display: inline; }
.journal-grid.is-grouped .jg-voucher:not([hidden]) { display: table-row; }
.journal-grid.is-grouped .jg-flat-total { display: none; }
.journal-grid.is-grouped .jg-row { cursor: pointer; }
/* Grouped: a line belongs to the voucher header above it, so it does not repeat the voucher number (kept for screen readers);
   it starts at the date, indented past the header's number, with a guide line on the left. */
.journal-grid.is-grouped tr[data-level="3"] .jg-vno { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.journal-grid.is-grouped tr[data-level="3"] td.jg-line-no { border-left: 2px solid var(--color-accent-muted); }
.journal-grid .jg-date-first { display: none; }
/* Grouped: the date moves into the first cell, left-aligned with the voucher number of its header (padding + toggle + gap). */
.journal-grid.is-grouped tr[data-level="3"] td.jg-line-no { padding-left: calc(var(--space-4) + var(--space-4) + var(--space-1)); }
.journal-grid.is-grouped tr[data-level="3"] .jg-date-first { display: inline; }
.journal-grid.is-grouped tr[data-level="3"] td.jg-date { visibility: hidden; }
.journal-grid tr[data-level="3"] td { font: inherit; }
.journal-grid .jg-count { color: var(--text-muted); font-weight: var(--font-weight-regular, 400); }
.journal-grid.is-grouped .jg-voucher td:first-child { padding-left: var(--space-4); }
.journal-grid.is-grouped .jg-voucher td { background: var(--bg-card); font-weight: var(--font-weight-medium); }
.tree-toggle { align-items: center; justify-content: center; width: var(--space-4); height: var(--space-4); margin-right: var(--space-1); padding: 0; vertical-align: middle; background: none; border: 0; border-radius: var(--radius-sm, 4px); color: var(--color-accent-text); cursor: pointer; }
.tree-toggle .material-icons { font-size: 20px; }
.tree-toggle:hover { background: var(--color-accent-muted); }
@media print {
  .journal-tree-bar { display: none; }
  .journal-grid tr[hidden] { display: table-row !important; } /* print everything, whatever is collapsed */
}

/* Grouped headers show their own total next to the name (the Debe/Haber cells can be scrolled out of view). */
.journal-grid .jg-tot { display: none; color: var(--text-muted); font-weight: var(--font-weight-regular, 400); white-space: nowrap; }
.journal-grid .jg-tot b { color: var(--text-primary); font-weight: var(--font-weight-medium); font-variant-numeric: tabular-nums; }
.journal-grid.is-grouped .jg-tot { display: inline; }
@media print { .journal-grid .jg-tot { display: none !important; } }

/* Full screen (fullscreen.js): any element with .fs-target can be the Fullscreen API target of a [data-fullscreen-target] button. Tokens apply because the element itself is promoted. */
.journal-actions .btn[hidden], .filter-bar .btn[hidden] { display: none; }
.btn[data-fullscreen-target]:focus-visible { outline: 2px solid var(--color-accent-text, currentColor); outline-offset: 2px; }
.fs-target:fullscreen { width: 100vw; height: 100vh; box-sizing: border-box; padding: var(--space-2); overflow: auto; background: var(--bg-page); color: var(--text-primary); font-family: inherit; }
.fs-target:-webkit-full-screen { width: 100vw; height: 100vh; box-sizing: border-box; padding: var(--space-2); overflow: auto; background: var(--bg-page); color: var(--text-primary); }
.fs-target.table-wrapper:fullscreen { max-height: none; border: 0; border-radius: 0; }
.fs-target:fullscreen:has(> .table-wrapper) { display: flex; flex-direction: column; overflow: hidden; }
.fs-target:fullscreen > .table-wrapper { flex: 1 1 0; min-height: 0; max-height: none; }
.fs-target:fullscreen .grid { background: var(--bg-card); }
.fs-target:fullscreen:focus-visible, .fs-target:fullscreen .table-wrapper:focus-visible { outline-offset: -2px; }
/* In-place navigation in progress (fullscreen.js): the table skeleton (skeleton.css) is the loading feedback; only the cursor changes here. */
.fs-target.is-loading { cursor: progress; }
/* Normal view: a wrapper around the table must not change the page flex layout (the .grid-scroll rules above expect a direct child of .page). */
.fs-target:has(> .table-wrapper):not(:fullscreen) { display: contents; }
.shell__content > .page > .fs-target:not(:fullscreen) > .grid-scroll { flex: 1 1 0; min-height: 200px; }
@media (max-width: 768px) { .shell__content > .page > .fs-target:not(:fullscreen) > .grid-scroll { flex: none; height: calc(100dvh - var(--topbar-height) - var(--space-3)); min-height: 280px; } }
@media print { [data-fullscreen-target] { display: none; } }
