/* ============================================================
   Dashboard Fiscal — Styles
   ============================================================ */

/* ── JetBrains Mono self-hosted (cero conexiones externas) ──
   Las fuentes viven en ui/fonts/; nada se carga de Google Fonts. */
@font-face {
  font-family: 'JetBrains Mono'; font-style: normal; font-weight: 400; font-display: swap;
  src: url('fonts/jetbrains-mono-400.woff2') format('woff2');
}
@font-face {
  font-family: 'JetBrains Mono'; font-style: normal; font-weight: 500; font-display: swap;
  src: url('fonts/jetbrains-mono-500.woff2') format('woff2');
}
@font-face {
  font-family: 'JetBrains Mono'; font-style: normal; font-weight: 600; font-display: swap;
  src: url('fonts/jetbrains-mono-600.woff2') format('woff2');
}
@font-face {
  font-family: 'JetBrains Mono'; font-style: normal; font-weight: 700; font-display: swap;
  src: url('fonts/jetbrains-mono-700.woff2') format('woff2');
}

:root {
  --bg: #0a0a0a;
  --bg-secondary: #121212;
  --card: #1a1a1a;
  --card-border: #2a2a2a;
  --card-hover: #222222;
  --text: #e5e5e5;
  --text-muted: #a3a3a3;
  /* #737373 quedaba por debajo de AA (4.5:1) sobre --bg en texto pequeño.
     #8a8a8a sube el ratio a ~5:1 manteniendo la jerarquía visual. */
  --text-dim: #8a8a8a;
  --accent: #d4d4d4;
  --accent-hover: #ffffff;
  --accent-bg: rgba(212,212,212,0.06);
  --green: #22c55e;
  --green-bg: rgba(34,197,94,0.08);
  --red: #ef4444;
  --red-bg: rgba(239,68,68,0.08);
  --orange: #f59e0b;
  --orange-bg: rgba(245,158,11,0.08);
  --purple: #a78bfa;
  --purple-bg: rgba(167,139,250,0.08);
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(0,0,0,0.5);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.6);
  --transition: 0.15s ease;
  /* Anillo de foco accesible y consistente en toda la app */
  --focus-ring: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  height: 100vh;
  overflow: hidden;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ---- FOCUS VISIBLE (accesibilidad) ----
   Anillo claro y uniforme para teclado en todos los controles interactivos.
   Se usa :focus-visible para no molestar al ratón. */
:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--radius-sm);
}
/* Inputs/selects/textarea: el anillo respeta su propio radio */
.filter-input:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  box-shadow: var(--focus-ring);
}
/* Celdas editables: outline interno (no admiten box-shadow limpio) */
td[contenteditable="true"]:focus-visible {
  box-shadow: none;
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

/* ---- APP SHELL ---- */
#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ---- SIDEBAR ---- */
.sidebar {
  width: 210px;
  flex-shrink: 0;
  background: var(--bg-secondary);
  border-right: 1px solid var(--card-border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.sidebar-brand {
  padding: 1.5rem 1.25rem 1.25rem;
  border-bottom: 1px solid var(--card-border);
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.sidebar-brand-name {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

.sidebar-brand-sub {
  font-size: 0.68rem;
  color: var(--text-dim);
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.75rem 0.5rem;
  flex: 1;
}

.sidebar-nav-btn {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  background: none;
  border: none;
  border-left: 2px solid transparent;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 500;
  padding: 0.6rem 0.85rem;
  cursor: pointer;
  transition: all var(--transition);
  font-family: inherit;
  text-align: left;
}

.sidebar-nav-btn:hover {
  color: var(--text);
  background: rgba(255,255,255,0.04);
}

.sidebar-nav-btn.active {
  color: var(--accent);
  background: var(--accent-bg);
  border-left-color: var(--accent);
}

.tab-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sidebar-nav-label {
  flex: 1;
}

/* ---- MAIN CONTENT ---- */
.main-content {
  flex: 1;
  height: 100vh;
  overflow-y: auto;
  padding: 1.5rem 2rem 3rem;
  animation: fadeIn 0.2s ease;
}

/* ---- ONBOARDING ---- */
/* El onboarding se monta directo en #app (display:flex), así que este wrapper
   ocupa todo el ancho disponible y centra la tarjeta horizontal y verticalmente. */
.onboarding-wrap {
  flex: 1;
  min-width: 0;
  height: 100vh;
  overflow-y: auto;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 2.5rem 1.5rem 3rem;
}
.onboarding-card {
  width: 100%;
  max-width: 54rem;
  margin: auto;
}

/* ---- BIENVENIDA (paso 0 del onboarding) ----
   Mini-landing dentro de la app: centrada vertical y horizontalmente, sin
   scroll, contenido corto. Caja propia (no la card del wizard). */
.onboarding-bienvenida {
  width: 100%;
  max-width: 34rem;
  margin: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 1.25rem;
}
.bienvenida-brand {
  /* Anula el padding/borde heredados de .sidebar-brand. */
  padding: 0;
  border: none;
  align-items: center;
  gap: 0.5rem;
}
.bienvenida-marca {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.bienvenida-tagline {
  font-size: 0.95rem;
  color: var(--text-muted);
  font-weight: 500;
}
.bienvenida-desc {
  font-size: 0.9rem;
  color: var(--text-dim);
  margin: 0;
  max-width: 28rem;
  line-height: 1.6;
}
.bienvenida-edicion {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: 0;
}
/* Aviso de privacidad: caja sutil con borde verde suave (estilo del badge de
   la landing, adaptado a la paleta de la app). */
.bienvenida-privacidad {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  text-align: left;
  font-size: 0.82rem;
  line-height: 1.55;
  color: var(--text);
  background: rgba(34, 197, 94, 0.06);
  border: 1px solid rgba(34, 197, 94, 0.25);
  border-radius: var(--radius);
  padding: 0.85rem 1.1rem;
}
.bienvenida-privacidad svg { color: var(--green-soft, #4ade80); }
.bienvenida-cta {
  font-size: 0.95rem;
  padding: 0.75rem 2.5rem;
  margin-top: 0.25rem;
}
.bienvenida-importar { margin-top: 0.25rem; }
.bienvenida-importar .btn-link {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 0.8rem;
  text-decoration: underline;
  font-family: inherit;
}
.bienvenida-importar .btn-link:hover { color: var(--accent-hover); }

/* ---- BRAND LINK ---- */
/* La marca enlaza a la landing sin parecer un link azul subrayado. */
a.sidebar-brand,
.sidebar-brand a.sidebar-brand-name {
  color: inherit;
  text-decoration: none;
  cursor: pointer;
}
a.sidebar-brand:hover .sidebar-brand-name,
.sidebar-brand a.sidebar-brand-name:hover {
  color: var(--accent);
}

/* Legacy aliases kept for any leftover references */
.tab-content { display: block; }
.tab-content.active { display: block; }

/* ---- GLOBAL YEAR SELECT (kept for compat) ---- */
.global-year-select {
  background: var(--bg-secondary);
  color: var(--text-muted);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  margin-right: 0.25rem;
  -webkit-appearance: none;
  appearance: none;
}
.global-year-select:hover { color: var(--accent); border-color: var(--accent); }
.global-year-select:focus { outline: none; color: var(--accent); border-color: var(--accent); }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---- KPIs ---- */
.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(195px, 1fr));
  gap: 1rem;
  margin-bottom: 1.75rem;
}
.kpi {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  transition: border-color var(--transition);
}
.kpi:hover { border-color: var(--text-dim); }
.kpi-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
.kpi-value {
  font-size: 1.65rem;
  font-weight: 700;
  margin-top: 0.35rem;
  font-variant-numeric: tabular-nums;
}
.kpi-sub {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 0.2rem;
}
.kpi-green .kpi-value { color: var(--green); }
.kpi-blue .kpi-value { color: var(--accent); }
.kpi-red .kpi-value { color: var(--red); }
.kpi-orange .kpi-value { color: var(--orange); }
.kpi-purple .kpi-value { color: var(--purple); }

/* ---- CARDS ---- */
.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.5rem;
}
/* Primera card de la sección: padding-top reducido para equilibrar con card-header-row */
#tab-resumen .card:first-of-type {
  padding-top: 0.5rem;
}
.card h3 {
  font-size: 0.85rem;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 1rem;
  font-weight: 600;
}
.card h3.solo {
  margin-bottom: 1rem;
}
.card-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}
.card-header-row h3 {
  margin-bottom: 0;
}
.chart-toggle {
  padding: 0.25rem 0.55rem;
  font-size: 0.7rem;
  font-family: 'JetBrains Mono', monospace;
  border-radius: var(--radius-sm);
  transition: all var(--transition);
}
.chart-toggle.active {
  background: var(--green);
  color: #fff;
  border-color: var(--green);
}

/* ---- Toggle switch para Bruto/Neto ---- */
.neto-toggle-wrap {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 22px;
  cursor: pointer;
}
.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--card-border);
  border-radius: 22px;
  transition: background 0.25s ease;
}
.toggle-slider::before {
  content: "";
  position: absolute;
  width: 16px;
  height: 16px;
  left: 3px;
  bottom: 3px;
  background: var(--text);
  border-radius: 50%;
  transition: transform 0.25s ease;
}
.toggle-switch input:checked + .toggle-slider {
  background: var(--green);
}
.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(18px);
}
.toggle-label {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  transition: color 0.2s ease;
}
.toggle-label-active {
  color: var(--green);
}

/* ---- Neto breakdown table (mobile-friendly) ---- */
.neto-breakdown {
  margin-top: 0.75rem;
  padding: 0.75rem 1rem;
  background: rgba(255,255,255,0.03);
  border-radius: var(--radius-sm);
  border: 1px solid var(--card-border);
  font-size: 0.75rem;
  font-family: 'JetBrains Mono', monospace;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.25rem 1rem;
  max-width: 360px;
}
.neto-breakdown .nb-row {
  display: contents;
}
.neto-breakdown .nb-label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--text-dim);
}
.neto-breakdown .nb-swatch {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  flex-shrink: 0;
}
.neto-breakdown .nb-value {
  text-align: right;
  color: var(--text);
}
.neto-breakdown .nb-sep {
  grid-column: 1 / -1;
  border-top: 1px solid var(--card-border);
  margin: 0.2rem 0;
}
.neto-breakdown .nb-total {
  font-weight: 700;
  color: var(--text);
}

/* ---- GRIDS ---- */
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}
.grid-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}
@media (max-width: 960px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

/* ---- CHART CONTAINERS ---- */
.chart-wrap { position: relative; height: 300px; }
.chart-wrap-sm { position: relative; height: 220px; }

/* ---- LEGEND ---- */
.legend-fixed {
  display: flex;
  justify-content: center;
  gap: 1.5rem;
  margin-top: 0.3rem;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.7rem;
  color: var(--text-muted);
  white-space: nowrap;
}
.legend-color {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}
.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ---- TABLES ---- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
th {
  text-align: left;
  color: var(--text-muted);
  font-weight: 600;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--card-border);
  font-size: 0.73rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
  /* Cabecera pegajosa al hacer scroll vertical del contenido. El fondo opaco
     evita que las filas se vean a través al deslizarse por debajo. */
  position: sticky;
  top: 0;
  background: var(--card);
  z-index: 1;
}
td {
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid rgba(45,58,78,0.5);
  vertical-align: middle;
  white-space: nowrap;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,0.02); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.total-row { font-weight: 700; }
.total-row td { background: var(--accent-bg); border-top: 1px solid var(--card-border); }
.group-header-row td {
  background: var(--card);
  border-top: 2px solid var(--card-border);
  padding: 0.5rem 0.75rem;
  font-size: 0.8rem;
  color: var(--text-dim);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

/* ---- EDITABLE CELLS ---- */
td[contenteditable="true"] {
  cursor: text;
  border-radius: 4px;
  transition: all var(--transition);
}
td[contenteditable="true"]:focus {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  background: rgba(59,130,246,0.06);
}

/* ---- BADGES ---- */
.badge {
  display: inline-block;
  padding: 0.2rem 0.6rem;
  border-radius: 9999px;
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.badge-green { background: var(--green-bg); color: var(--green); }
.badge-orange { background: var(--orange-bg); color: var(--orange); }
.badge-gray { background: rgba(148,163,184,0.12); color: var(--text-muted); }
.badge-blue { background: var(--accent-bg); color: var(--accent); }

/* ---- BUTTONS ----
   Jerarquía: .btn-accent (primario, fondo claro + texto oscuro),
   .btn (secundario/ghost, contorno) y .btn-danger (destructivo). */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  border: 1px solid var(--card-border);
  border-radius: var(--radius-sm);
  background: var(--card);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 500;
  line-height: 1.2;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition), color var(--transition);
  font-family: inherit;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); color: var(--accent); background: var(--card-hover); }
.btn:active { transform: translateY(0.5px); }
.btn-accent {
  background: var(--accent);
  border-color: var(--accent);
  /* --accent es un gris muy claro: texto oscuro para contraste AA */
  color: var(--bg);
  font-weight: 600;
}
.btn-accent:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--bg); }
.btn-sm { padding: 0.3rem 0.6rem; font-size: 0.75rem; }

/* ---- PDF ICON ---- */
.pdf-link {
  cursor: pointer;
  color: var(--accent);
  font-size: 1rem;
  transition: color var(--transition);
  background: none;
  border: none;
  font-family: inherit;
  padding: 0.2rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.pdf-link:hover { color: var(--accent-hover); }
.pdf-link.disabled { color: var(--text-dim); cursor: default; }
td .pdf-link, td .pdf-doc-link { float: right; }
th:last-child { text-align: right; }

/* ---- PDF MODAL (SPLIT VIEW) ---- */
.pdf-modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0,0,0,0.85);
  backdrop-filter: blur(4px);
}
.pdf-modal.open { display: flex; flex-direction: column; }
.pdf-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1.25rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--card-border);
  flex-shrink: 0;
}
.pdf-modal-title {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
}
.pdf-modal-nav {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}
.pdf-modal-btn {
  background: var(--card);
  border: 1px solid var(--card-border);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  transition: all var(--transition);
}
.pdf-modal-btn:hover { border-color: var(--accent); color: var(--accent); }
.pdf-modal-btn.close-btn { font-size: 1.4rem; }

/* Split layout */
.pdf-split {
  flex: 1;
  display: flex;
  overflow: hidden;
}
.pdf-info-panel {
  width: 340px;
  min-width: 280px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--card-border);
  overflow-y: auto;
  padding: 1.25rem;
  flex-shrink: 0;
}
.pdf-view-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.pdf-view-panel iframe {
  flex: 1;
  border: none;
  width: 100%;
}
.pdf-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 1.1rem;
}

/* Info panel content */
.pdf-info-section {
  margin-bottom: 1.25rem;
}
.pdf-info-section:last-child { margin-bottom: 0; }
.pdf-info-section h4 {
  font-size: 0.7rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 0.6rem;
  font-weight: 600;
}
.pdf-info-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 0.3rem 0;
  font-size: 0.82rem;
}
.pdf-info-row .label { color: var(--text-muted); }
.pdf-info-row .value { font-weight: 600; font-variant-numeric: tabular-nums; }
.pdf-info-divider {
  border: none;
  border-top: 1px solid var(--card-border);
  margin: 0.75rem 0;
}
.pdf-info-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}
.pdf-doc-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.82rem;
  transition: color var(--transition);
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  display: inline-block;
}
.pdf-doc-link:hover { color: var(--accent-hover); background: rgba(255,255,255,0.05); }
.pdf-doc-link.active { color: #fff; font-weight: 700; background: rgba(255,255,255,0.12); border-left: 2px solid var(--accent); padding-left: 0.65rem; }
.pdf-doc-link::before { content: none; }

@media (max-width: 768px) {
  .pdf-split { flex-direction: column; }
  .pdf-info-panel {
    width: 100%;
    min-width: unset;
    flex: 1;
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--card-border);
  }
  .pdf-view-panel {
    flex: 0 0 auto;
    min-height: 60px;
    max-height: 80px;
    background: var(--bg-secondary);
    align-items: center;
    justify-content: center;
  }
  .pdf-view-panel iframe { display: none; }
  .pdf-placeholder { font-size: 0.85rem; min-height: unset; flex: none; padding: 0.75rem; }
  .pdf-info-actions {
    flex-direction: column;
  }
  .pdf-info-actions .btn {
    width: 100%;
    text-align: center;
    padding: 0.65rem 1rem;
    font-size: 0.85rem;
  }
}

/* ---- FILTERS ---- */
.filters {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  align-items: center;
}
.filter-input {
  background: var(--bg-secondary);
  border: 1px solid var(--card-border);
  color: var(--text);
  padding: 0.45rem 0.75rem;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  font-family: inherit;
  outline: none;
  transition: border-color var(--transition);
}
.filter-input:hover { border-color: var(--text-dim); }
.filter-input:focus { border-color: var(--accent); }
.filter-input::placeholder { color: var(--text-dim); }
textarea.filter-input { resize: vertical; min-height: 5rem; line-height: 1.5; }
select.filter-input {
  cursor: pointer;
  appearance: none;
  padding-right: 1.5rem;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2 4l4 4 4-4' stroke='%2394a3b8' stroke-width='1.5' fill='none'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.5rem center;
}
/* ---- CONTRACT CARDS ---- */
.contract-card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  transition: border-color var(--transition);
  overflow-x: auto;
  max-width: 100%;
}
.contract-card:hover { border-color: var(--accent); }
.contract-card h4 {
  font-size: 1rem;
  margin-bottom: 0.25rem;
}
.contract-card .meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
}
.progress-bar {
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
  margin: 0.75rem 0 0.5rem;
}
.progress-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.5s ease;
}
.progress-fill.blue { background: var(--accent); }
.progress-fill.purple { background: var(--purple); }
.progress-text {
  font-size: 0.75rem;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
}

/* ---- NOTES ---- */
.note {
  background: var(--accent-bg);
  border-left: 3px solid var(--accent);
  padding: 1rem 1.25rem;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: 0.85rem;
  line-height: 1.6;
}
.note-orange { background: var(--orange-bg); border-left-color: var(--orange); }
.note-red { background: var(--red-bg); border-left-color: var(--red); }
.note-green { background: var(--green-bg); border-left-color: var(--green); }
.note strong { font-weight: 600; }

/* ---- UPCOMING ---- */
.upcoming {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}
.upcoming-item {
  background: var(--bg-secondary);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  flex: 1;
  min-width: 200px;
}
.upcoming-item .date {
  font-size: 0.75rem;
  color: var(--text-dim);
}
.upcoming-item .label {
  font-size: 0.85rem;
  margin-top: 0.15rem;
}
.upcoming-item .amount {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
  margin-top: 0.25rem;
}

/* ---- RENTA ESTIMATION ---- */
.renta-row { display: flex; justify-content: space-between; padding: 0.5rem 0; }
.renta-row.sep { border-top: 1px solid var(--card-border); margin-top: 0.25rem; padding-top: 0.75rem; }
.renta-label { color: var(--text-muted); font-size: 0.85rem; }
.renta-value { font-weight: 600; font-variant-numeric: tabular-nums; }
.renta-result { font-size: 1.2rem; }
.renta-positive { color: var(--red); }
.renta-negative { color: var(--green); }

/* ---- SEPARATOR ---- */
.sep-line {
  border: none;
  border-top: 1px solid var(--card-border);
  margin: 1.5rem 0;
}

/* ============================================================
   RESPONSIVE
   Breakpoints: 900px (sidebar a iconos), 640px (formularios a una
   columna), 360px (sin desbordes en pantallas mínimas).
   ============================================================ */

/* ── ≤900px: la barra lateral colapsa a una tira de solo iconos ── */
@media (max-width: 900px) {
  .sidebar {
    width: 56px;
  }
  .sidebar-brand {
    padding: 0.9rem 0.5rem;
    align-items: center;
  }
  .sidebar-brand-name,
  .sidebar-brand-sub,
  .sidebar-nav-label { display: none; }
  .sidebar-nav {
    padding: 0.6rem 0.4rem;
  }
  .sidebar-nav-btn {
    justify-content: center;
    padding: 0.65rem 0;
    border-left: none;
    border-radius: var(--radius-sm);
  }
  .sidebar-nav-btn.active {
    border-left: none;
    box-shadow: inset 0 0 0 1px var(--accent);
  }
  .tab-icon { width: 20px; height: 20px; }

  .main-content { padding: 1rem 1.25rem 2.5rem; }
  .onboarding-wrap { padding: 1.5rem 1rem 2.5rem; }

  .kpis { grid-template-columns: repeat(2, 1fr); gap: 0.75rem; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; gap: 1rem; }
  .detalle-grid { grid-template-columns: 1fr; }
  .upcoming { flex-direction: column; gap: 0.5rem; }
  .upcoming-item { min-width: unset; }
}

/* ── ≤640px: densidad compacta y formularios a una sola columna ── */
@media (max-width: 640px) {
  body { font-size: 13px; }

  .main-content { padding: 0.75rem 0.85rem 2rem; }

  .global-year-select { font-size: 0.78rem; padding: 0.3rem 0.5rem; }
  .privacy-btn { flex: none; width: 30px; height: 30px; }

  .kpi { padding: 0.85rem; }
  .kpi-label { font-size: 0.62rem; }
  .kpi-value { font-size: 1.15rem; }
  .kpi-sub { font-size: 0.68rem; }

  .card { padding: 1rem; margin-bottom: 1rem; }
  .card h3 { font-size: 0.78rem; margin-bottom: 0.75rem; }

  .filters { gap: 0.4rem; }
  .filter-input { font-size: 0.78rem; padding: 0.4rem 0.55rem; min-width: 0; flex: 1; }
  .btn-sm { font-size: 0.7rem; padding: 0.28rem 0.55rem; }
  #btn-csv, #btn-copy-data, #btn-copy-claude { display: none; }

  .table-wrap { overflow-x: auto; }
  table { font-size: 0.78rem; }
  th { font-size: 0.64rem; padding: 0.45rem 0.55rem; }
  td { padding: 0.45rem 0.55rem; }

  .contract-card { padding: 1rem; }
  .contract-card h4 { font-size: 0.9rem; }
  .alertas { gap: 0.35rem; }
  .alerta-item { padding: 0.5rem 0.75rem; }
  .alerta-text { font-size: 0.75rem; }
  .alerta-dias { display: block; margin-left: 0; margin-top: 0.15rem; }
  .renta-label, .renta-value { font-size: 0.78rem; }
  .detalle-header { flex-direction: column; align-items: flex-start; gap: 0.5rem; }

  /* Formularios genéricos a una columna */
  .form-row { grid-template-columns: 1fr; }
}

/* ── ≤360px: garantizar que nada desborde en pantallas mínimas ── */
@media (max-width: 360px) {
  .main-content { padding: 0.6rem 0.6rem 2rem; }
  .card { padding: 0.85rem; }
  .kpis { grid-template-columns: 1fr; }
  .nf-actions { flex-direction: column; }
  .nf-actions .btn { width: 100%; }
}

/* ---- PRINT ---- */
@media print {
  body { background: white; color: black; height: auto; overflow: visible; }
  .sidebar { display: none; }
  .main-content { height: auto; overflow: visible; padding: 0; }
  .card { border-color: #ddd; box-shadow: none; }
  .tab-content { display: block !important; page-break-inside: avoid; }
  .pdf-modal { display: none !important; }
}

/* ---- DETALLE TRIMESTRAL (Modelo 303) ---- */
.tr-expandable {
  cursor: pointer;
  transition: background var(--transition);
}
.tr-expandable:hover {
  background: var(--accent-bg);
}
.tr-expandable .expand-arrow {
  display: inline-block;
  font-size: 0.7rem;
  transition: transform 0.2s ease;
  color: var(--text-dim);
  margin-right: 0.25rem;
}
.tr-expandable.expanded .expand-arrow {
  transform: rotate(90deg);
  color: var(--accent);
}

.detalle-row td {
  padding: 0 !important;
  background: transparent;
}

.detalle-303 {
  padding: 1.5rem 0.5rem 1rem;
}

.detalle-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}
.detalle-title {
  font-size: 1.05rem;
  font-weight: 700;
}
.detalle-subtitle {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 0.15rem;
}
.detalle-plazo-box {
  text-align: right;
  background: var(--orange-bg);
  border: 1px solid rgba(245,158,11,0.2);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.85rem;
}
.detalle-plazo-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}
.detalle-plazo-value {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--orange);
}

.detalle-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1rem;
}
@media (max-width: 900px) {
  .detalle-grid { grid-template-columns: 1fr; }
}

.detalle-card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 1.25rem;
}
.detalle-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}
.detalle-card-header h4 {
  font-size: 0.85rem;
  font-weight: 700;
  margin: 0;
}
.detalle-card-hint {
  font-size: 0.72rem;
  color: var(--text-dim);
}
.detalle-card-total {
  text-align: right;
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--card-border);
  font-size: 0.9rem;
  color: var(--text-muted);
}

.detalle-sub-section {
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px dashed var(--card-border);
}
.detalle-sub-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  display: block;
  margin-bottom: 0.5rem;
}

.detalle-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}
.detalle-table th {
  text-align: left;
  font-weight: 600;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid var(--card-border);
}
.detalle-table th.num,
.detalle-table td.num { text-align: right; }
.detalle-table td {
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid rgba(42,42,42,0.4);
}
.detalle-table tbody tr:last-child td {
  border-bottom: none;
}

.detalle-empty {
  font-size: 0.82rem;
  color: var(--text-dim);
  font-style: italic;
  padding: 1rem 0;
}

.detalle-card-resultado {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.detalle-card-resultado h4 {
  font-size: 0.85rem;
  font-weight: 700;
  margin-bottom: 1rem;
}
.detalle-calc-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  padding: 0.35rem 0;
  color: var(--text-muted);
}
.detalle-calc-final {
  border-top: 2px solid var(--card-border);
  margin-top: 0.5rem;
  padding-top: 0.65rem;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--orange);
}

.detalle-casillas {
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.8rem;
}
.casilla-row {
  display: flex;
  align-items: center;
  padding: 0.4rem 0;
  border-bottom: 1px solid rgba(42,42,42,0.3);
}
.casilla-num {
  width: 36px;
  color: var(--orange);
  font-weight: 700;
  flex-shrink: 0;
}
.casilla-desc {
  flex: 1;
  color: var(--text-muted);
}
.casilla-val {
  font-weight: 600;
  text-align: right;
  min-width: 90px;
}
.casilla-final {
  border-bottom: none;
  border-top: 2px solid var(--card-border);
  margin-top: 0.25rem;
  padding-top: 0.5rem;
  color: var(--orange);
  font-weight: 700;
  font-size: 0.9rem;
}
.casilla-section-label {
  font-family: var(--font-body, inherit);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 0.6rem 0 0.25rem 0;
  font-weight: 600;
  border-bottom: 1px dashed rgba(42,42,42,0.3);
  margin-bottom: 0.25rem;
}
.casilla-section-label:first-child { padding-top: 0; }
.casilla-subtotal {
  background: rgba(255,255,255,0.02);
  font-weight: 600;
  color: var(--text);
}
.casilla-subtotal .casilla-val { color: var(--text); }

.detalle-card-info {
  grid-column: 1 / -1;
  border-left: 3px solid var(--purple, var(--orange));
}

.badge-isp {
  font-size: 0.62rem;
  background: var(--purple-bg);
  color: var(--purple);
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  font-weight: 600;
  vertical-align: middle;
}
.badge-bien {
  font-size: 0.62rem;
  background: var(--green-bg);
  color: var(--green);
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  font-weight: 600;
  vertical-align: middle;
}

.detalle-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

/* ============================================================
   Mejoras v2: Alertas, ISP, Clear Filters, Teal KPI
   ============================================================ */

/* --- Teal KPI --- */
:root {
  --teal: #14b8a6;
  --teal-bg: rgba(20,184,166,0.08);
}
.kpi-teal .kpi-value { color: var(--teal); }
.kpi-white .kpi-value { color: #e5e5e5; }

/* --- Alertas --- */
.alertas {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}
.alerta-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--card);
  border: 1px solid var(--card-border);
}
.alerta-icon {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
  flex-shrink: 0;
}
.alerta-rojo .alerta-icon { background: var(--red-bg); color: var(--red); }
.alerta-naranja .alerta-icon { background: var(--orange-bg); color: var(--orange); }
.alerta-gris .alerta-icon { background: rgba(148,163,184,0.12); color: var(--text-muted); }
.alerta-rojo { border-color: rgba(239,68,68,0.2); }
.alerta-naranja { border-color: rgba(245,158,11,0.2); }
.alerta-text {
  flex: 1;
  font-size: 0.82rem;
  line-height: 1.4;
}
.alerta-text strong {
  color: var(--text);
}
.alerta-dias {
  display: inline-block;
  margin-left: 0.5rem;
  color: var(--text-dim);
  font-size: 0.75rem;
}
.alerta-importe {
  font-weight: 600;
  font-size: 0.85rem;
  white-space: nowrap;
}
.alerta-rojo .alerta-importe { color: var(--orange); }
.alerta-naranja .alerta-importe { color: var(--orange); }

/* --- Badge purple (ISP) --- */
.badge-purple { background: var(--purple-bg); color: var(--purple); }

/* --- Badge red --- */
.badge-red { background: var(--red-bg); color: var(--red); }

/* --- Clear filters button --- */
.btn-clear-filters {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--card-border);
  background: transparent;
  color: var(--text-muted);
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
  flex-shrink: 0;
}
.btn-clear-filters:hover {
  background: var(--card-hover);
  color: var(--text);
  border-color: var(--text-dim);
}

/* --- Concepto con fade (facturas + gastos) --- */
#facturas-table td.concepto-cell,
#gastos-table td.concepto-cell {
  max-width: 220px;
  overflow: hidden;
  -webkit-mask-image: linear-gradient(to right, black calc(100% - 30px), transparent 100%);
  mask-image: linear-gradient(to right, black calc(100% - 30px), transparent 100%);
}

/* --- ISP note in gastos --- */
.nota-isp {
  font-size: 0.72rem;
  color: var(--text-dim);
  margin-top: 0.5rem;
  padding-left: 0.5rem;
  border-left: 2px solid var(--purple);
}

/* ============================================================
   Modo Privado
   ============================================================ */
.privacy-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  flex-shrink: 0;
  padding: 0.4rem;
}
.privacy-btn:hover { color: var(--text); }
.privacy-btn.active { color: var(--red); }
.privacy-btn svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Importes y datos financieros */
body.private .kpi-value,
body.private .kpi-sub,
body.private td.num,
body.private .total-row td:first-child,
body.private .renta-value,
body.private .casilla-val,
body.private .detalle-calc-row span:last-child,
body.private .detalle-calc-final span:last-child,
body.private .detalle-card-total strong,
body.private .upcoming-item .amount,
body.private .alerta-importe,
body.private .contract-card strong,
body.private .contract-card .progress-bar,
body.private .contract-card .progress-text,
body.private canvas,
body.private #pdf-frame,
body.private #pdf-info-panel {
  filter: blur(8px);
  -webkit-filter: blur(8px);
  user-select: none;
  pointer-events: none;
  transition: filter 0.3s ease;
}
/* Nombres de clientes */
body.private #header-sub,
body.private #facturas-table td:nth-child(3),
body.private #tabla-irpf tr:not(.total-row) td:first-child,
body.private .contract-card .meta,
body.private .detalle-table td:nth-child(3) {
  filter: blur(8px);
  -webkit-filter: blur(8px);
  user-select: none;
  pointer-events: none;
  transition: filter 0.3s ease;
}

/* --- autofacturas --- */

/* Form layout for nueva-factura */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 1rem;
}
.form-group label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
}
.form-group .filter-input {
  width: 100%;
}
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
@media (max-width: 600px) {
  .form-row { grid-template-columns: 1fr; }
}

/* Line items */
.linea-item {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  align-items: center;
}
.linea-item .filter-input.concepto { flex: 1; }
.linea-item .filter-input.base { width: 130px; }

/* Totals preview inside nueva-factura card */
.totales-preview {
  width: 100%;
  max-width: 320px;
  margin-top: 0;
}
.totales-preview td {
  padding: 0.4rem 0.75rem;
  border-bottom: 1px solid rgba(45,58,78,0.4);
  font-size: 0.85rem;
}
.totales-preview tr:last-child td {
  border-bottom: none;
  border-top: 1px solid var(--card-border);
  font-weight: 700;
}
.totales-preview td:last-child {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* btn-danger (eliminar cliente) */
.btn-danger {
  border-color: rgba(239,68,68,0.3);
  color: var(--red);
}
.btn-danger:hover {
  border-color: var(--red);
  background: var(--red-bg);
  color: var(--red);
}

/* --- nueva factura split --- */

/*
  Layout a pantalla completa: el formulario es flexible (1fr) y arranca pegado
  al borde izquierdo del área de contenido; la vista previa queda anclada a la
  derecha. El ancho de la preview lo deriva su propio aspect-ratio (A4) a partir
  de la altura disponible, así que aquí solo le reservamos su hueco con flex:none.
*/
.nf-split {
  display: flex;
  align-items: stretch;
  gap: 1.5rem;
  padding: 0.25rem 0 0;
  min-height: 0;
}

/* ── Left column: form (flexible, ocupa todo el ancho sobrante) ── */
.nf-form {
  /* El formulario se ensancha hasta quedar a ras de la preview (gap de 1.5rem).
     Sin tope de lectura: aprovecha todo el espacio sobrante a la izquierda de
     la hoja A4. */
  flex: 1 1 auto;
  min-width: 0;
}
.nf-form .card {
  margin-bottom: 0;
  padding: 1.6rem 2rem;
}

/* Form header: number + meta */
.nf-form-header {
  margin-bottom: 1.25rem;
  padding-bottom: 0.9rem;
  border-bottom: 1px solid var(--card-border);
}
.nf-form-title {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.01em;
  margin-bottom: 0.15rem;
}
.nf-form-meta {
  font-size: 0.78rem;
  color: var(--text-dim);
  letter-spacing: 0.03em;
}

/* Form sections */
.nf-section {
  margin-bottom: 1.15rem;
}
.nf-section:last-of-type {
  margin-bottom: 0;
}
.nf-section-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  font-weight: 600;
  margin-bottom: 0.5rem;
}

/* Inputs del formulario de nueva factura: un punto más grandes y cómodos */
.nf-form .filter-input,
.nf-form select.filter-input {
  font-size: 0.92rem;
  padding: 0.6rem 0.8rem;
}
.nf-form .form-group label {
  font-size: 0.74rem;
}

/* Two-column row for cliente/IVA */
.nf-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.85rem;
}

/* Three-column row for serie/fecha/plazo (mismo gap que nf-row-2).
   En modo «Personalizado…» se añade una 4ª columna (#plazo-custom-grp)
   para los días, así serie/fecha/plazo siguen en una sola línea. */
.nf-row-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 0.85rem;
}
.nf-row-3--custom {
  grid-template-columns: 1fr 1fr 1fr 0.7fr;
}

/* Line items */
.nf-lineas {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}
/*
  Da el máximo de ancho al concepto: 1fr para el concepto, base con un mínimo
  legible (~10rem) que no crece, y la papelera fija. La cabecera comparte el
  mismo grid para que las columnas queden alineadas con las filas.
*/
.nf-linea {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(8rem, 10rem) 30px;
  gap: 0.5rem;
  align-items: center;
}
.nf-linea .filter-input.concepto { width: 100%; }
.nf-linea .filter-input.base { width: 100%; font-variant-numeric: tabular-nums; text-align: right; }

/* ── Desplegable propio de conceptos guardados ──
   Wrapper relativo: contiene el input de concepto + (opcional) la flecha y el
   menú flotante. Reemplaza al <datalist> nativo de Chromium. */
.nf-concepto-wrap {
  position: relative;
  min-width: 0;
}
/* Solo cuando hay conceptos guardados (.has-menu) reservamos sitio para la flecha. */
.nf-concepto-wrap.has-menu .filter-input.concepto {
  padding-right: 2rem;
}
/* Botón-flecha: mismo aspecto/posición que la flecha de los select.filter-input. */
.nf-concepto-arrow {
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  width: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
  color: #94a3b8;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  transition: color var(--transition);
}
.nf-concepto-arrow:hover { color: var(--accent); }

/* Menú flotante: card con borde del tema, bajo el input, con scroll si es largo. */
.nf-concepto-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 50;
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  max-height: 12rem;
  overflow-y: auto;
  padding: 0.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}
.nf-concepto-opt {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  padding: 0.45rem 0.6rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background var(--transition), color var(--transition);
}
.nf-concepto-opt:hover,
.nf-concepto-opt:focus-visible {
  background: var(--accent-bg);
  color: var(--accent);
  outline: none;
}

/* Cabecera de columnas de conceptos: única etiqueta de la sección.
   Usa el estilo de .nf-section-label (CONCEPTO / BASE (€)). */
.nf-linea-head {
  margin-bottom: 0.5rem;
}
.nf-col-head {
  /* hereda tamaño/peso/color de .nf-section-label; solo ajusta alineación */
  margin-bottom: 0;
  padding-left: 0.15rem;
}
.nf-col-head-num {
  text-align: right;
  padding-left: 0;
  padding-right: 0.2rem;
}
.nf-linea-del {
  background: none;
  border: 1px solid transparent;
  color: var(--text-dim);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  border-radius: var(--radius-sm);
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  transition: all var(--transition);
}
.nf-linea-del:hover { color: var(--red); border-color: rgba(239,68,68,0.35); background: var(--red-bg); }
.nf-linea-del[disabled] { opacity: 0; pointer-events: none; }

/* Add line button */
.nf-addlinea {
  margin-top: 0.5rem;
}

/* ── Total KPI block ── */
.nf-totales {
  background: var(--bg-secondary);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-top: 0.25rem;
}
.nf-totales-rows {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.2rem 1rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
  font-variant-numeric: tabular-nums;
}
.nf-totales-rows .nf-tr-val {
  text-align: right;
}
.nf-totales-rows .nf-tr-label-neg { color: var(--red); }
.nf-totales-rows .nf-tr-val-neg { color: var(--red); text-align: right; }
.nf-totales-sep {
  border: none;
  border-top: 1px solid var(--card-border);
  margin: 0.5rem 0;
}
.nf-total-kpi {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
}
.nf-total-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 700;
  color: var(--text-muted);
}
.nf-total-value {
  font-size: 1.9rem;
  font-weight: 700;
  color: var(--green);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  line-height: 1;
}

/* ── Action buttons ── */
.nf-actions {
  display: flex;
  flex-wrap: wrap; /* la botonera nunca debe provocar scroll horizontal */
  gap: 0.75rem;
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--card-border);
}
.nf-actions .btn-accent {
  /* Tamaño según contenido (con el formulario ancho, flex:1 lo estiraba a lo bestia) */
  justify-content: center;
  padding: 0.65rem 1.75rem;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.nf-actions .btn {
  padding: 0.65rem 1rem;
  font-size: 0.85rem;
}

/* ── Right column: preview (anclada arriba-derecha, ocupa el alto disponible) ── */
.nf-preview {
  /* El ancho se deriva del aspect-ratio A4 del scaler a partir de su alto */
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  position: sticky;
  top: 0;
  align-self: flex-start;
  max-height: calc(100vh - 5rem);
  /* Empuja la preview al borde derecho del área de contenido */
  margin-left: auto;
}

.nf-preview-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  font-weight: 600;
  padding-left: 0.15rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.nf-preview-label::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--card-border);
}

/*
  KEY FIX: the scaler has a fixed height (viewport-based) and aspect-ratio 210/297 (A4).
  This means width = height × (210/297) is computed automatically by the browser.
  The iframe (intrinsic 794×1123px) is then scaled to fill that exact width.
  Result: NO grey bars — the A4 page fills its box perfectly.
*/
.nf-preview-scaler {
  /* Height is fixed; width is derived from aspect-ratio → no grey margins.
     Reservamos el padding superior del main (1.5rem) + la etiqueta y gap
     (~1.5rem) + un respiro inferior, para que la hoja A4 quepa entera SIN
     forzar scroll vertical en el área de contenido a 1080p. */
  height: calc(100vh - 6.5rem);
  aspect-ratio: 210 / 297;
  overflow: hidden;
  border-radius: var(--radius-sm);
  background: transparent;
  position: relative;
  flex: none;
  /* Subtle shadow/border to define the page edge against the dark bg */
  box-shadow: 0 2px 32px rgba(0,0,0,0.7), 0 0 0 1px rgba(255,255,255,0.06);
}

/* iframe always rendered at true A4 (794×1123px), scaled via --nf-scale */
#nf-preview-iframe {
  border: none;
  width: 794px;
  height: 1123px;
  display: block;
  transform-origin: top left;
  transform: scale(var(--nf-scale, 0.5));
  pointer-events: none;
}

/* ── Responsive: por debajo de 1100px no hay espacio para las dos columnas
   lado a lado, así que se apilan (formulario arriba, preview debajo). ── */
@media (max-width: 1100px) {
  .nf-split {
    flex-direction: column;
    align-items: stretch;
    gap: 1.5rem;
  }
  .nf-form {
    flex: none;
    max-width: 100%;
    width: 100%;
  }
  .nf-preview {
    flex: none;
    width: 100%;
    position: static;
    max-height: none;
    align-items: center;
  }
  .nf-preview-scaler {
    /* Apilada: limitar el alto por el ancho de la ventana para que la hoja
       no sea gigantesca; se centra dentro de la columna. */
    height: min(70vh, 90vw);
    aspect-ratio: 210 / 297;
  }
}

/* En móvil estrecho, las filas serie/fecha/plazo y cliente/IVA se apilan */
@media (max-width: 640px) {
  .nf-form .card { padding: 1.25rem; }
  .nf-row-2 { grid-template-columns: 1fr; gap: 0.75rem; }
  .nf-row-3,
  .nf-row-3--custom { grid-template-columns: 1fr; gap: 0.75rem; }
  .nf-preview-scaler { height: min(75vh, 110vw); }
}

/* En pantallas mínimas, garantizar que la fila de concepto no desborde:
   la base puede encoger por debajo de su mínimo cómodo. */
@media (max-width: 400px) {
  .nf-linea { grid-template-columns: minmax(0, 1fr) minmax(5.5rem, 7rem) 30px; gap: 0.35rem; }
}

/* ── Toasts (tematizados con variables del :root) ────── */
.toasts { position: fixed; bottom: 1.25rem; right: 1.25rem; display: flex; flex-direction: column; gap: .5rem; z-index: 1000; max-width: calc(100vw - 2.5rem); }
.toast {
  padding: .7rem 1rem;
  border-radius: var(--radius-sm);
  font-size: .85rem;
  line-height: 1.45;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .2s ease, transform .2s ease;
  max-width: 22rem;
  box-shadow: var(--shadow-lg);
}
.toast.visible { opacity: 1; transform: none; }
.toast-ok { background: var(--card); border: 1px solid var(--card-border); color: var(--text); }
/* Error: capa sólida (--card) + tinte rojo translúcido para legibilidad sobre fondo oscuro */
.toast-error {
  background: linear-gradient(var(--red-bg), var(--red-bg)), var(--card);
  border: 1px solid var(--red);
  color: #fca5a5;
}

/* ── Modal ──────────────────────────────────────────── */
.modal-fondo {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.6);
  backdrop-filter: blur(2px);
  display: grid; place-items: center;
  z-index: 900;
  padding: 1rem;
  animation: fadeIn 0.18s ease;
}
.modal {
  /* Ancho generoso: que el mensaje y la botonera respiren sin scroll. */
  width: min(48rem, calc(100vw - 2rem));
  max-height: calc(100vh - 2rem);
  overflow: auto;
  box-shadow: var(--shadow-lg);
}

/* ============================================================
   AJUSTES · Tarjeta «Plantilla de factura»
   Dos columnas (opciones | preview); apilado ≤900px.
   ============================================================ */
.pl-split { display: grid; grid-template-columns: 1fr minmax(220px, 300px); gap: 1.5rem; align-items: start; }
.pl-opciones { min-width: 0; }

.pl-radios { display: flex; flex-direction: column; gap: .5rem; }
.pl-radio {
  display: flex; align-items: center; gap: .6rem;
  padding: .55rem .75rem; border: 1px solid var(--card-border);
  border-radius: var(--radius-sm); cursor: pointer; font-size: .85rem;
  transition: border-color var(--transition), background var(--transition);
}
.pl-radio:hover { border-color: var(--accent); }
.pl-radio--on { border-color: var(--accent); background: var(--accent-bg); }
.pl-radio input { accent-color: var(--accent); }

.pl-check {
  display: flex; align-items: center; gap: .55rem; margin-top: 1rem;
  font-size: .85rem; cursor: pointer;
}
.pl-check input { accent-color: var(--accent); width: 1rem; height: 1rem; }

.pl-logo-thumb {
  width: 96px; height: 56px; border: 1px solid var(--card-border);
  border-radius: var(--radius-sm); background: #fff;
  display: grid; place-items: center; overflow: hidden; padding: 4px;
}
.pl-logo-thumb img { max-width: 100%; max-height: 100%; object-fit: contain; }

.pl-preview { display: flex; flex-direction: column; gap: .5rem; position: sticky; top: 0; }
.pl-preview-label {
  font-size: .65rem; text-transform: uppercase; letter-spacing: .08em;
  color: var(--text-dim); font-weight: 600;
}
.pl-preview-scaler {
  width: 100%; aspect-ratio: 210 / 297; overflow: hidden;
  border-radius: var(--radius-sm); box-shadow: var(--shadow);
  background: #e8e8e8;
}
#plPreviewIframe {
  border: none; width: 794px; height: 1123px; display: block;
  transform-origin: top left; transform: scale(var(--pl-scale, 0.34));
  pointer-events: none;
}
@media (max-width: 900px) {
  .pl-split { grid-template-columns: 1fr; }
  .pl-preview { position: static; max-width: 360px; }
}

/* ============================================================
   SCROLLBAR OSCURA
   La barra de scroll por defecto (clara) desentona con el fondo negro.
   Firefox usa scrollbar-color; WebKit/Chromium usa los pseudo-elementos.
   Se aplica a toda la app (cualquier contenedor con overflow).
   ============================================================ */
* {
  scrollbar-color: #333 #0a0a0a;
  scrollbar-width: thin;
}
*::-webkit-scrollbar { width: 12px; height: 12px; }
*::-webkit-scrollbar-track { background: #0a0a0a; }
*::-webkit-scrollbar-thumb {
  background: #2a2a2a;
  border-radius: 8px;
  border: 3px solid #0a0a0a;
}
*::-webkit-scrollbar-thumb:hover { background: #3a3a3a; }
