/* =========================================================================
   Contabilidad Personal — Hoja de estilos
   Organizada en: tokens -> base -> layout -> componentes -> vistas -> movil
   ========================================================================= */

/* ---------- 1. Tokens de diseño ---------- */
:root {
  color-scheme: light;

  --bg: #eef2f7;
  --bg-grad: radial-gradient(1200px 600px at 10% -10%, #dbeafe 0%, transparent 60%),
             radial-gradient(900px 500px at 100% 0%, #ede9fe 0%, transparent 55%);
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-3: #f1f5f9;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #64748b;

  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #dbeafe;
  --primary-ink: #1e40af;

  --success: #059669;
  --success-soft: #d1fae5;
  --success-ink: #065f46;

  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-soft: #fee2e2;
  --danger-ink: #991b1b;

  --warning: #b45309;
  --warning-soft: #fef3c7;

  --info: #0e7490;
  --info-soft: #cffafe;

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --r-full: 999px;

  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;

  --shadow-1: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 3px rgba(15, 23, 42, .08);
  --shadow-2: 0 4px 12px rgba(15, 23, 42, .08);
  --shadow-3: 0 18px 45px rgba(15, 23, 42, .18);

  --header-h: 60px;
  --sidebar-w: 250px;
  --tap: 44px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          Arial, "Noto Sans", sans-serif;
  --font-num: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
}

/* Tema oscuro (automático) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #080d18;
    --bg-grad: radial-gradient(1200px 600px at 10% -10%, #14213d 0%, transparent 60%),
               radial-gradient(900px 500px at 100% 0%, #1b1a3a 0%, transparent 55%);
    --surface: #111a2b;
    --surface-2: #162033;
    --surface-3: #1c2740;
    --border: #26324a;
    --border-strong: #35435f;
    --text: #e6edf7;
    --text-muted: #93a3bd;

    --primary: #60a5fa;
    --primary-hover: #93c5fd;
    --primary-soft: #1e3a68;
    --primary-ink: #bfdbfe;

    --success: #34d399;
    --success-soft: #0d3b31;
    --success-ink: #a7f3d0;

    --danger: #f87171;
    --danger-hover: #fca5a5;
    --danger-soft: #451a1a;
    --danger-ink: #fecaca;

    --warning: #fbbf24;
    --warning-soft: #422c07;

    --info: #22d3ee;
    --info-soft: #0b3a44;

    --shadow-1: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-2: 0 4px 14px rgba(0, 0, 0, .45);
    --shadow-3: 0 18px 45px rgba(0, 0, 0, .6);
  }
}

/* Tema oscuro (forzado) */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #080d18;
  --bg-grad: radial-gradient(1200px 600px at 10% -10%, #14213d 0%, transparent 60%),
             radial-gradient(900px 500px at 100% 0%, #1b1a3a 0%, transparent 55%);
  --surface: #111a2b;
  --surface-2: #162033;
  --surface-3: #1c2740;
  --border: #26324a;
  --border-strong: #35435f;
  --text: #e6edf7;
  --text-muted: #93a3bd;

  --primary: #60a5fa;
  --primary-hover: #93c5fd;
  --primary-soft: #1e3a68;
  --primary-ink: #bfdbfe;

  --success: #34d399;
  --success-soft: #0d3b31;
  --success-ink: #a7f3d0;

  --danger: #f87171;
  --danger-hover: #fca5a5;
  --danger-soft: #451a1a;
  --danger-ink: #fecaca;

  --warning: #fbbf24;
  --warning-soft: #422c07;

  --info: #22d3ee;
  --info-soft: #0b3a44;

  --shadow-1: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-2: 0 4px 14px rgba(0, 0, 0, .45);
  --shadow-3: 0 18px 45px rgba(0, 0, 0, .6);
}

/* ---------- 2. Base ---------- */
*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background-color: var(--bg);
  background-image: var(--bg-grad);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
  min-height: 100dvh;
}

h1, h2, h3, h4 { margin: 0; font-weight: 650; letter-spacing: -.01em; line-height: 1.25; }
p { margin: 0; }
ul { margin: 0; padding-left: 1.25rem; }

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 4px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.num { font-variant-numeric: tabular-nums; }
.positive { color: var(--success); }
.negative { color: var(--danger); }
.nowrap { white-space: nowrap; }
.text-right { text-align: right; }
.muted { color: var(--text-muted); }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- 3. Layout: cabecera, sidebar, main ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-height: var(--header-h);
  padding: var(--sp-2) var(--sp-4);
  padding-top: max(var(--sp-2), env(safe-area-inset-top));
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-width: 0;
  flex: 1 1 auto;
}

.brand__logo {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 36px; height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--primary-hover));
  color: #fff;
  font-size: 18px;
  box-shadow: var(--shadow-1);
}

.brand__text { min-width: 0; }
.brand__title {
  font-size: 1.02rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand__subtitle {
  font-size: .72rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex: 0 0 auto;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: var(--tap);
  height: var(--tap);
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  background: transparent;
  color: var(--text);
  font-size: 1.15rem;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
}
.icon-btn:hover { background: var(--surface-3); }
.icon-btn:active { transform: scale(.94); }
.icon-btn--menu { display: none; }

.icon-btn svg,
.dropdown__item svg {
  width: 20px; height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.brand__logo svg { width: 20px; height: 20px; }

/* Menú desplegable de la cabecera */
.dropdown { position: relative; }
.dropdown__panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 70;
  min-width: 216px;
  padding: var(--sp-2);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-3);
  display: none;
}
.dropdown__panel.open { display: block; animation: pop .14s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(-6px); } }

.dropdown__item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  width: 100%;
  padding: 10px var(--sp-3);
  border: 0;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: .9rem;
  text-align: left;
  cursor: pointer;
}
.dropdown__item:hover { background: var(--surface-3); }
.dropdown__sep { height: 1px; margin: var(--sp-2) 0; background: var(--border); }
.dropdown__label {
  padding: var(--sp-2) var(--sp-3) var(--sp-1);
  font-size: .68rem;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

/* Menú compartido de Ufumbuzi (dropdown.js). Se mantiene aparte del menú
   de opciones de la app para no mezclar “Más opciones” con enlaces del sitio. */
.site-menu { position: relative; display: inline-flex; }
.site-menu .dropdown-btn { background: transparent; border-color: transparent; }
#myDropdown {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 80;
  width: min(288px, calc(100vw - 24px));
  max-height: min(70vh, 520px);
  overflow-y: auto;
  padding: var(--sp-2);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-3);
  color: var(--text);
}
#myDropdown.show { display: block; animation: pop .14s ease-out; }
#myDropdown a {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 9px var(--sp-3);
  border-radius: var(--r-sm);
  color: var(--text);
  font-size: .875rem;
  text-decoration: none;
}
#myDropdown a:hover { background: var(--surface-3); }
#myDropdown a svg { flex: 0 0 auto; margin-right: 0; }
#myDropdown p { margin: 4px 0 0; color: var(--text-muted) !important; }

/* Estructura general */
.app-body { display: flex; align-items: stretch; min-height: calc(100dvh - var(--header-h)); }

.sidebar {
  flex: 0 0 var(--sidebar-w);
  width: var(--sidebar-w);
  padding: var(--sp-4) 0 var(--sp-6);
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.nav-group + .nav-group { margin-top: var(--sp-5); }
.nav-group__title {
  padding: 0 var(--sp-4) var(--sp-2);
  font-size: .68rem;
  font-weight: 700;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  width: 100%;
  padding: 10px var(--sp-4);
  border: 0;
  border-left: 3px solid transparent;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: .9rem;
  text-align: left;
  cursor: pointer;
  transition: background .13s, color .13s, border-color .13s;
}
.nav-item:hover { background: var(--surface-3); }
.nav-item__icon { width: 1.25rem; text-align: center; font-size: 1rem; flex: 0 0 auto; }
.nav-item__text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item[aria-current="page"] {
  background: color-mix(in srgb, var(--primary-soft) 55%, transparent);
  border-left-color: var(--primary);
  color: var(--primary-ink);
  font-weight: 600;
}

.scrim {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(15, 23, 42, .5);
  opacity: 0;
  visibility: hidden;
  transition: opacity .25s, visibility .25s;
}

main {
  flex: 1 1 auto;
  min-width: 0;
  padding: var(--sp-5);
  padding-bottom: max(var(--sp-6), calc(env(safe-area-inset-bottom) + 88px));
}

/* ---------- 4. Componentes ---------- */
.view { display: none; animation: fade .18s ease-out; }
.view.is-active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } }

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  flex-wrap: wrap;
  margin-bottom: var(--sp-4);
}
.view-header h2 { font-size: 1.35rem; }
.view-header p { font-size: .85rem; color: var(--text-muted); margin-top: 2px; }
.view-header__actions { display: flex; gap: var(--sp-2); flex-wrap: wrap; }

.card {
  padding: var(--sp-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-1);
  margin-bottom: var(--sp-4);
}
.card > h3 { font-size: 1.05rem; margin-bottom: var(--sp-3); }
.card--flush { padding: 0; overflow: hidden; }
.card--subtle { background: var(--surface-2); box-shadow: none; }

.grid-2 { display: grid; gap: var(--sp-4); grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }

/* Botones */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: 40px;
  padding: 0 var(--sp-4);
  border: 1px solid transparent;
  border-radius: var(--r-md);
  font: inherit;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s, transform .08s, box-shadow .15s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; box-shadow: var(--shadow-1); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-success { background: var(--success); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-outline { background: var(--surface); border-color: var(--border-strong); color: var(--text); }
.btn-outline:hover:not(:disabled) { background: var(--surface-3); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-3); color: var(--text); }
.btn-sm { min-height: 32px; padding: 0 var(--sp-3); font-size: .8rem; border-radius: var(--r-sm); }
.btn-block { width: 100%; }

/* Campos de formulario */
.field { margin-bottom: var(--sp-4); }
.field > label,
.field-label {
  display: block;
  margin-bottom: 6px;
  font-size: .8rem;
  font-weight: 600;
  color: var(--text-muted);
}
.field-hint { margin-top: 5px; font-size: .76rem; color: var(--text-muted); }
.field-row { display: grid; gap: var(--sp-3); grid-template-columns: 1fr 1fr; }

input[type="text"], input[type="number"], input[type="date"],
input[type="search"], select, textarea {
  width: 100%;
  min-height: 42px;
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .95rem;
  transition: border-color .15s, box-shadow .15s;
}
textarea { min-height: 62px; resize: vertical; line-height: 1.45; }
select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath fill='%2364748b' d='M1.4 1.7 6 6.3l4.6-4.6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 11px;
  padding-right: 32px;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}
input::placeholder, textarea::placeholder { color: var(--text-muted); opacity: .75; }
input:disabled, select:disabled, textarea:disabled { background: var(--surface-3); cursor: not-allowed; }
input[type="date"] { min-height: 42px; }

/* Grupo de segments (selector segmented) */
.segmented {
  position: relative;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 4px;
  padding: 4px;
  background: var(--surface-3);
  border-radius: var(--r-md);
}
.segmented input { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0; }
.segmented label {
  display: grid;
  place-items: center;
  min-height: 38px;
  border-radius: var(--r-sm);
  font-size: .85rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  transition: background .15s, color .15s, box-shadow .15s;
}
.segmented input:checked + label { background: var(--surface); color: var(--primary-ink); box-shadow: var(--shadow-1); }
.segmented input:focus-visible + label { outline: 2px solid var(--primary); outline-offset: 1px; }
.segmented input[value="expense"]:checked + label { color: var(--danger-ink); }
.segmented input[value="transfer"]:checked + label { color: var(--primary-ink); }

/* Filtros */
.filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--sp-3);
  align-items: end;
  margin-bottom: var(--sp-4);
}
.filters__actions { display: flex; gap: var(--sp-2); align-items: end; }
.filters__actions .field { margin-bottom: 0; }

/* Insignias (badges) */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: var(--r-full);
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .01em;
  white-space: nowrap;
}
.badge--income { background: var(--success-soft); color: var(--success-ink); }
.badge--expense { background: var(--danger-soft); color: var(--danger-ink); }
.badge--transfer { background: var(--primary-soft); color: var(--primary-ink); }
.badge--neutral { background: var(--surface-3); color: var(--text-muted); }

/* Estado vacío */
.empty-state { padding: var(--sp-6) var(--sp-4); text-align: center; color: var(--text-muted); }
.empty-state__icon { font-size: 2rem; opacity: .55; }
.empty-state__title { margin-top: var(--sp-2); font-weight: 600; color: var(--text); }
.empty-state__text { margin-top: 4px; font-size: .85rem; }
.empty-state .btn { margin-top: var(--sp-4); }

/* ---------- 5. Tablas ---------- */
.table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
}
.data-table th, .data-table td {
  padding: 11px var(--sp-4);
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.data-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--text-muted);
  white-space: nowrap;
}
.data-table tbody tr:last-child td { border-bottom: 0; }
.data-table tbody tr:hover td { background: var(--surface-2); }
.data-table tfoot td {
  background: var(--surface-3);
  font-weight: 700;
  border-top: 2px solid var(--border);
  border-bottom: 0;
}
.data-table .cell-actions { text-align: right; white-space: nowrap; }
.data-table .col-num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.data-table .col-date { white-space: nowrap; }
.data-table .col-obs { max-width: 220px; color: var(--text-muted); font-size: .85rem; }
.row-actions { display: inline-flex; gap: 6px; justify-content: flex-end; }
.row-actions .btn { min-height: 30px; padding: 0 10px; font-size: .76rem; }

/* ---------- 6. Dashboard ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: var(--sp-4);
  margin-bottom: var(--sp-4);
}
.stat {
  position: relative;
  overflow: hidden;
  padding: var(--sp-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-1);
}
.stat::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  background: var(--primary);
}
.stat--income::before { background: var(--success); }
.stat--expense::before { background: var(--danger); }
.stat--transfer::before { background: var(--warning); }
.stat__label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.stat__value {
  margin-top: 6px;
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.02em;
  overflow-wrap: anywhere;
}
.stat__meta { margin-top: 4px; font-size: .78rem; color: var(--text-muted); }

.progress {
  height: 6px;
  margin-top: 10px;
  border-radius: var(--r-full);
  background: var(--surface-3);
  overflow: hidden;
}
.progress__bar { height: 100%; border-radius: var(--r-full); background: var(--primary); transition: width .3s; }
.progress__bar--ok { background: var(--success); }
.progress__bar--over { background: var(--danger); }

/* ---------- 7. Reportes ---------- */
.report-block { margin-bottom: var(--sp-5); }
.report-block__title {
  padding-bottom: 6px;
  margin-bottom: var(--sp-3);
  font-size: 1rem;
  border-bottom: 2px solid var(--primary);
}
.summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--sp-3);
  padding: var(--sp-4);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  margin-bottom: var(--sp-4);
}
.summary > .summary__item { text-align: center; justify-self: stretch; }
.summary__label { font-size: .7rem; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--text-muted); }
.summary__value { margin-top: 4px; font-size: 1.2rem; font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

.report-group + .report-group { margin-top: var(--sp-4); }
.report-group__title { font-size: .95rem; margin-bottom: var(--sp-2); }

.chk-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: var(--sp-1);
  max-height: 260px;
  padding: var(--sp-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface-2);
  overflow-y: auto;
  overscroll-behavior: contain;
}
.chk {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: 38px;
  padding: 6px 10px;
  border-radius: var(--r-sm);
  font-size: .88rem;
  cursor: pointer;
}
.chk:hover { background: var(--surface-3); }
.chk input { width: 17px; height: 17px; flex: 0 0 auto; accent-color: var(--primary); cursor: pointer; }
.chk__text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chart-box { position: relative; height: 320px; }
.chart-box--tall { height: 380px; }
.chart-box--short { height: 260px; }

/* ---------- 8. Modales ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  padding: var(--sp-4);
  background: rgba(15, 23, 42, .55);
  backdrop-filter: blur(2px);
}
.modal-overlay.is-open { display: flex; animation: fade .15s ease-out; }

.modal {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 540px;
  max-height: min(90dvh, 780px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-3);
  overflow: hidden;
  animation: slide-up .2s cubic-bezier(.2, .8, .3, 1);
}
@keyframes slide-up { from { opacity: 0; transform: translateY(18px) scale(.98); } }

.modal__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--border);
}
.modal__title { font-size: 1.05rem; }
.modal__body { padding: var(--sp-5); overflow-y: auto; overscroll-behavior: contain; }
.modal__foot {
  display: flex;
  justify-content: flex-end;
  gap: var(--sp-2);
  padding: var(--sp-4) var(--sp-5);
  padding-bottom: max(var(--sp-4), env(safe-area-inset-bottom));
  background: var(--surface-2);
  border-top: 1px solid var(--border);
}

/* ---------- 9. Notificaciones (toast) y confirmación ---------- */
.toast-host {
  position: fixed;
  z-index: 200;
  right: var(--sp-4);
  bottom: var(--sp-4);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-width: 240px;
  max-width: min(420px, calc(100vw - 32px));
  padding: 12px var(--sp-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-3);
  font-size: .88rem;
  pointer-events: auto;
  animation: toast-in .22s cubic-bezier(.2, .8, .3, 1);
}
.toast.is-leaving { animation: toast-out .2s ease-in forwards; }
.toast--success { border-left-color: var(--success); }
.toast--error { border-left-color: var(--danger); }
.toast--warning { border-left-color: var(--warning); }
.toast__icon { font-size: 1.05rem; }
.toast__msg { flex: 1; min-width: 0; }
@keyframes toast-in { from { opacity: 0; transform: translateX(24px); } }
@keyframes toast-out { to { opacity: 0; transform: translateX(24px); } }

/* ---------- 10. Botón flotante (móvil) ---------- */
.fab {
  position: fixed;
  right: var(--sp-4);
  bottom: calc(env(safe-area-inset-bottom) + var(--sp-4));
  z-index: 50;
  display: none;
  align-items: center;
  gap: var(--sp-2);
  height: 54px;
  padding: 0 var(--sp-5);
  border: 0;
  border-radius: var(--r-full);
  background: var(--primary);
  color: #fff;
  font: inherit;
  font-size: .92rem;
  font-weight: 700;
  box-shadow: 0 8px 24px color-mix(in srgb, var(--primary) 45%, transparent);
  cursor: pointer;
  transition: transform .12s, background .15s;
}
.fab:active { transform: scale(.95); }

/* ---------- 11. Utilidades ---------- */
.mt-0 { margin-top: 0; }
.mt-3 { margin-top: var(--sp-3); }
.mb-3 { margin-bottom: var(--sp-3); }
.flex { display: flex; }
.flex-between { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); }
.gap-2 { gap: var(--sp-2); }
.help-content h3 { margin: var(--sp-5) 0 var(--sp-2); font-size: 1rem; color: var(--primary-ink); }
.help-content h3:first-child { margin-top: 0; }
.help-content p, .help-content li { line-height: 1.65; font-size: .9rem; }
.help-content p { margin-bottom: var(--sp-3); color: var(--text-muted); }
.help-content li { margin-bottom: 6px; }
.help-content ul { margin-bottom: var(--sp-3); color: var(--text-muted); }
.help-content strong { color: var(--text); }
.kbd {
  display: inline-block;
  padding: 1px 6px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-2);
  font-family: var(--font-num);
  font-size: .8em;
}

/* ---------- 12. Responsive ---------- */
@media (max-width: 960px) {
  :root { --sidebar-w: 264px; }

  .icon-btn--menu { display: inline-grid; }
  .header-actions .hide-sm { display: none; }

  .app-body { position: relative; }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 58;
    width: var(--sidebar-w);
    max-width: 84vw;
    padding-top: calc(env(safe-area-inset-top) + var(--sp-4));
    transform: translateX(-102%);
    transition: transform .26s cubic-bezier(.2, .8, .3, 1);
    box-shadow: var(--shadow-3);
  }
  .sidebar.is-open { transform: translateX(0); }
  .scrim.is-open { opacity: 1; visibility: visible; }

  .nav-item { min-height: var(--tap); }
  .fab { display: inline-flex; }
  main { padding: var(--sp-4); }
}

/* En el movil el FAB se reduce a un circulo: la etiqueta ocupaba media pantalla */
@media (max-width: 620px) {
  .fab {
    width: 56px; height: 56px;
    padding: 0;
    justify-content: center;
  }
  .fab > span:not([aria-hidden]) { display: none; }
  .fab > span[aria-hidden] { font-size: 1.6rem; }
  .hide-mobile { display: none !important; }
}

@media (max-width: 720px) {
  body { font-size: 14.5px; }
  .brand__subtitle { display: none; }
  .brand__logo { width: 32px; height: 32px; font-size: 16px; }
  .brand__title { font-size: .95rem; }

  main { padding: var(--sp-3); }
  .card { padding: var(--sp-4); border-radius: var(--r-md); }
  .card--flush { padding: 0; }
  .view-header h2 { font-size: 1.15rem; }
  .view-header { margin-bottom: var(--sp-3); }

  /* Dos columnas en vez de cinco filas apiladas; el buscador ocupa el ancho completo. */
  .filters { grid-template-columns: 1fr 1fr; gap: var(--sp-2) var(--sp-3); }
  .filter-wide { grid-column: 1 / -1; }
  .filters__actions { grid-column: 1 / -1; }
  .filters__actions .btn { flex: 1; min-height: 38px; }
  .filters .field { margin-bottom: 0; }

  .stat-grid { grid-template-columns: 1fr 1fr; gap: var(--sp-3); }
  .stat { padding: var(--sp-3); }
  .stat__value { font-size: 1.1rem; }
  .stat__label { font-size: .62rem; letter-spacing: .03em; }
  .stat__meta { font-size: .72rem; }

  .field-row { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }

  .chart-box { height: 260px; }
  .chart-box--tall { height: 300px; }

  .toast-host { left: var(--sp-3); right: var(--sp-3); bottom: calc(env(safe-area-inset-bottom) + 78px); }
  .toast { min-width: 0; max-width: none; }

  .modal-overlay { padding: 0; align-items: flex-end; }
  .modal {
    max-width: none;
    max-height: 92dvh;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    border-bottom: 0;
    animation: sheet-up .24s cubic-bezier(.2, .8, .3, 1);
  }
  @keyframes sheet-up { from { transform: translateY(100%); } }
  .modal__foot .btn { flex: 1; min-height: var(--tap); }

  /* Tablas -> tarjetas apiladas */
  .table-wrap { overflow-x: visible; }
  table.data-table,
  table.data-table tbody,
  table.data-table tfoot,
  table.data-table tr,
  table.data-table td { display: block; width: 100%; }
  table.data-table thead { display: none; }
  table.data-table tr {
    padding: var(--sp-3) var(--sp-4);
    border-bottom: 1px solid var(--border);
  }
  table.data-table tr:last-child { border-bottom: 0; }
  table.data-table td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-3);
    padding: 3px 0;
    border: 0;
    text-align: right;
  }
  /* La primera celda acts como titulo de la tarjeta. */
  table.data-table tbody td:first-child {
    padding-bottom: 6px;
    margin-bottom: 2px;
    border-bottom: 1px dashed var(--border);
  }
  table.data-table tbody td:first-child::before { display: none; }
  table.data-table td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    font-size: .7rem;
    font-weight: 700;
    letter-spacing: .04em;
    text-transform: uppercase;
    color: var(--text-muted);
    text-align: left;
  }
  /* Sin data-label (p. ej. celdas de tfoot) no se genera etiqueta. */
  table.data-table td:not([data-label])::before,
  table.data-table td[data-label=""]::before { content: none; }
  table.data-table td.cell-actions,
  table.data-table tfoot td { justify-content: flex-end; }
  /* La fila de total se mantiene en una sola linea (etiqueta ... monto). */
  table.data-table tfoot tr {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--sp-3);
    background: var(--surface-2);
    border-top: 2px solid var(--border);
    border-bottom: 0;
  }
  table.data-table tfoot td {
    width: auto;
    border-top: 0;
    padding-top: var(--sp-2);
    font-weight: 700;
    text-align: right;
  }
  table.data-table tfoot td:first-child { text-align: left; }
  table.data-table .col-obs { max-width: none; white-space: normal; }
  table.data-table .empty-row td { display: block; text-align: center; }
  table.data-table .empty-row td::before { content: none; }
  .row-actions .btn { min-height: 36px; padding: 0 var(--sp-4); }
}

@media (max-width: 360px) {
  .stat-grid { grid-template-columns: 1fr; }
  .summary { grid-template-columns: 1fr 1fr; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

/* ---------- 13. Impresión ---------- */
@media print {
  body { background: #fff; }
  .app-header, .sidebar, .scrim, .fab, .toast-host,
  .view-header__actions, .filters, .no-print { display: none !important; }
  main { padding: 0; }
  .card { box-shadow: none; border: 1px solid #ddd; break-inside: avoid; }
  .view { display: none !important; }
  .view.is-active { display: block !important; }
  .data-table thead th { position: static; }
}
