/* ============================================================
   Aripos · Gestión v1.1 — Ember Dark v2 (tokens firmados, skill Lúa)
   Sin azul. Danger terracota, success oliva. Sólidos + alpha, sin
   gradientes complicados ni drop-shadows fuertes (anti-patrones §4).
   Syne display · Figtree UI · Outfit datos.
   ============================================================ */
:root {
  --carbon: #0D0C0A;
  --ash:    #1A1815;
  --smoke:  #5A554C;
  --bone:   #EFE9E0;
  --brasa:  #F56820;
  --danger: #C2401A;
  --success:#5E7A3A;
  --ash-elevado: #232019;
  --borde-fuerte:#6F6A5F;
}

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

body {
  background: var(--carbon);
  color: var(--bone);
  font-family: 'Figtree', sans-serif;
  font-size: 15px;
  min-height: 100vh;
  overflow-x: hidden;  /* red de seguridad global (ENG-016.C-4.5) */
}

.oculto { display: none !important; }
.dato { font-family: 'Outfit', sans-serif; }

.pantalla { display: none; }
.pantalla.activa { display: block; }

/* ============================================================
   LOGIN — pantalla partida
   ============================================================ */
#pantalla-login.activa { display: grid; grid-template-columns: 1.1fr 1fr; min-height: 100vh; }

.login-marca {
  background: var(--ash);
  border-right: 2px solid var(--borde-fuerte);
  display: flex; align-items: center; justify-content: center;
  padding: 48px;
  position: relative;
  overflow: hidden;
}
/* textura sutil: sólido + alpha, permitido por §4 */
.login-marca::before {
  content: '';
  position: absolute; inset: 0;
  background:
    radial-gradient(ellipse 90% 60% at 20% 110%, rgba(245, 104, 32, 0.10), transparent 60%);
  pointer-events: none;
}
.marca-inner { position: relative; max-width: 440px; }
.ascua-grande { height: 56px; font-size: 52px; }
.ascua-fallback {
  font-family: 'Syne', sans-serif; font-weight: 800;
  letter-spacing: -0.02em; color: var(--bone);
}
.o-brasa { color: var(--brasa); }
.marca-titulo {
  font-family: 'Syne', sans-serif; font-weight: 700;
  font-size: 34px; line-height: 1.15;
  margin-top: 28px;
}
.marca-sub { color: var(--smoke); font-size: 16px; margin-top: 14px; line-height: 1.5; }
.marca-brasa-linea { width: 72px; height: 4px; background: var(--brasa); border-radius: 2px; margin-top: 28px; }

.login-panel { display: flex; align-items: center; justify-content: center; padding: 48px 32px; }
#form-login { width: min(380px, 100%); }
#form-login h2 { font-family: 'Syne', sans-serif; font-size: 26px; }
.login-hint { color: var(--smoke); font-size: 14px; margin: 6px 0 22px; }
#form-login label { margin-top: 16px; }

/* ============================================================
   Controles base
   ============================================================ */
label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; color: var(--bone); }

input, select {
  width: 100%;
  background: var(--carbon);
  border: 2px solid var(--smoke);
  border-radius: 10px;
  color: var(--bone);
  font-family: 'Figtree', sans-serif;
  font-size: 15px;
  padding: 12px 14px;
  margin-top: 4px;
}
input:focus, select:focus { outline: none; border-color: var(--brasa); }
input[type="search"] { width: 260px; background: var(--ash); }
input::placeholder { color: var(--smoke); }

.btn {
  font-family: 'Figtree', sans-serif;
  font-size: 15px; font-weight: 600;
  min-height: 44px;
  padding: 10px 20px;
  border-radius: 10px;
  border: 2px solid var(--borde-fuerte);
  background: var(--ash-elevado);
  color: var(--bone);
  cursor: pointer;
  transition: border-color .12s ease, filter .12s ease, transform .06s ease;
}
.btn:hover { border-color: var(--bone); }
.btn:active { transform: translateY(1px); }
.btn-brasa { background: var(--brasa); border-color: var(--brasa); color: var(--carbon); font-weight: 700; }
.btn-brasa:hover { filter: brightness(1.08); border-color: var(--brasa); }
.btn-ghost { background: transparent; }
.btn-lg { min-height: 52px; font-size: 16px; }
.btn-block { width: 100%; }
.btn-bloqueado { background: transparent; border-color: var(--smoke); color: var(--smoke); cursor: not-allowed; }
#btn-login { margin-top: 26px; width: 100%; }

/* ============================================================
   APP — sidebar + contenido
   ============================================================ */
#pantalla-app.activa { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }

.sidebar {
  background: var(--ash);
  border-right: 2px solid var(--borde-fuerte);
  padding: 24px 16px;
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.ascua-side { height: 28px; font-size: 26px; margin: 0 8px 28px; }

.side-nav { display: flex; flex-direction: column; gap: 6px; flex: 1; }
.side-btn {
  display: flex; align-items: center; gap: 12px;
  font-family: 'Figtree', sans-serif;
  font-size: 15px; font-weight: 600;
  min-height: 52px; padding: 8px 12px;
  background: transparent; color: var(--smoke);
  border: 2px solid transparent; border-radius: 10px;
  cursor: pointer; text-align: left;
  transition: color .12s ease, background .12s ease;
}
.side-btn:hover { color: var(--bone); }
.side-btn.activa { color: var(--bone); background: var(--ash-elevado); border-color: var(--borde-fuerte); }

/* Iconos en chip sólido — regla legibilidad #1 + §3.1 dos tonos invertidos */
.side-chip {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: 8px;
  background: var(--bone);
  flex: 0 0 auto;
}
.side-chip svg { width: 20px; height: 20px; fill: none; stroke: var(--carbon); stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.side-chip svg .trazo-inv { stroke: var(--bone); }
.side-btn:not(.activa) .side-chip { background: var(--ash-elevado); border: 2px solid var(--borde-fuerte); }
.side-btn:not(.activa) .side-chip svg { stroke: var(--smoke); }
.side-btn:not(.activa) .side-chip svg .trazo-inv { stroke: var(--ash-elevado); }

.side-pie { border-top: 2px solid var(--ash-elevado); padding-top: 16px; display: flex; flex-direction: column; gap: 10px; }
.user-email { color: var(--smoke); font-size: 13px; word-break: break-all; padding: 0 8px; }

.contenido { padding: 32px 36px; max-width: 1280px; width: 100%; overflow-x: hidden; }

/* ============================================================
   Vistas
   ============================================================ */
.vista { display: none; }
.vista.activa { display: block; }

.vista-cabecera {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 16px; margin-bottom: 24px; flex-wrap: wrap;
}
.vista-cabecera h1 { font-family: 'Syne', sans-serif; font-size: 30px; font-weight: 700; }
.cabecera-sub { color: var(--smoke); font-size: 15px; }
.cabecera-derecha { display: flex; gap: 12px; align-items: center; }

/* ============================================================
   Panel — KPIs
   ============================================================ */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px; }
.kpi {
  background: var(--ash);
  border: 2px solid var(--borde-fuerte);
  border-radius: 14px;
  padding: 20px 22px;
  display: flex; flex-direction: column; gap: 6px;
}
.kpi-destacada { border-color: var(--brasa); background: linear-gradient(0deg, rgba(245,104,32,0.07), rgba(245,104,32,0.07)), var(--ash); }
.kpi-etiqueta { font-size: 14px; font-weight: 600; color: var(--smoke); text-transform: uppercase; letter-spacing: .05em; }
.kpi-cifra { font-size: 34px; font-weight: 700; line-height: 1.1; }
.kpi-destacada .kpi-cifra { color: var(--brasa); }
.kpi-nota { font-size: 13px; color: var(--smoke); }

.panel-doble { display: grid; grid-template-columns: 1.6fr 1fr; gap: 16px; }

.card {
  background: var(--ash);
  border: 2px solid var(--borde-fuerte);
  border-radius: 14px;
  padding: 20px 22px;
}
.card-cabecera { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.card h2 { font-family: 'Syne', sans-serif; font-size: 19px; }
.card-accion { display: flex; flex-direction: column; gap: 12px; }
.card-accion h2 { margin-bottom: 4px; }
/* Combo de cliente con búsqueda + filas recientes clicables (ENG-016.C-4.6) */
.cli-combo { position: relative; }
.cli-combo-input { width: 100%; }
.cli-combo-lista { position: absolute; z-index: 50; left: 0; right: 0; top: calc(100% + 4px); margin: 0; padding: 4px; list-style: none; max-height: 320px; overflow-y: auto; background: var(--ash-elevado); border: 1.5px solid var(--borde-fuerte); border-radius: 10px; box-shadow: 0 12px 32px rgba(0,0,0,.45); }
.cli-combo-opcion { padding: 11px 12px; min-height: 44px; border-radius: 8px; cursor: pointer; display: flex; flex-direction: column; justify-content: center; gap: 2px; }
.cli-combo-opcion:hover, .cli-combo-opcion.activa { background: rgba(245,104,32,.14); }
.cli-combo-opcion .cli-nif { font-size: 12px; color: var(--smoke); }
.cli-combo-crear { color: var(--brasa); font-weight: 600; }
.tabla-plana tbody tr[data-open-doc], .tabla-plana tbody tr[data-open-presu] { cursor: pointer; }
.tabla-plana tbody tr[data-open-doc]:hover, .tabla-plana tbody tr[data-open-presu]:hover { background: rgba(245,104,32,.07); }

.aviso-f4 {
  margin-top: auto;
  background: var(--carbon);
  border: 2px solid var(--ash-elevado);
  border-radius: 10px;
  padding: 14px;
}
.aviso-f4 p { font-size: 13px; color: var(--smoke); margin-top: 8px; line-height: 1.45; }

/* ============================================================
   Tablas
   ============================================================ */
.tabla-wrap { padding: 0; overflow: auto; }
.tabla { width: 100%; border-collapse: collapse; }
.tabla th {
  text-align: left; font-size: 13px; font-weight: 700;
  color: var(--smoke); text-transform: uppercase; letter-spacing: 0.05em;
  padding: 14px 18px;
  border-bottom: 2px solid var(--borde-fuerte);
}
.tabla td { padding: 14px 18px; border-bottom: 1px solid var(--ash-elevado); font-size: 15px; }
.tabla tbody tr:last-child td { border-bottom: none; }
.tabla tbody tr:hover { background: var(--ash-elevado); }
.tabla .num, .tabla td.num { text-align: right; font-family: 'Outfit', sans-serif; }
.tabla-plana td { padding: 12px 4px; }
.tabla-plana tbody tr:hover { background: transparent; }
.celda-vacia { text-align: center; color: var(--smoke); padding: 32px !important; }

.badge {
  display: inline-block;
  font-size: 13px; font-weight: 700;
  padding: 4px 12px; border-radius: 999px;
  letter-spacing: .02em;
}
.badge-borrador { background: var(--ash-elevado); border: 2px solid var(--borde-fuerte); color: var(--bone); }
.badge-emitida  { background: var(--success); border: 2px solid var(--success); color: var(--bone); }
.badge-anulada  { background: transparent; border: 2px solid var(--danger); color: var(--danger); }
.badge-migrada  { background: transparent; border: 2px solid var(--smoke); color: var(--smoke); }
.badge-aceptado  { background: transparent; border: 2px solid var(--success); color: var(--success); }
.badge-rechazado { background: transparent; border: 2px solid var(--danger); color: var(--danger); }
.badge-convertido{ background: transparent; border: 2px solid var(--smoke); color: var(--smoke); }
.badge-caducado  { background: transparent; border: 2px solid var(--smoke); color: var(--smoke); opacity: .6; }

/* ============================================================
   Modales
   ============================================================ */
.modal {
  background: var(--ash);
  border: 2px solid var(--borde-fuerte);
  border-radius: 14px;
  color: var(--bone);
  padding: 32px;
  width: min(560px, 94vw);
}
.modal-ancho { width: min(880px, 96vw); }
.modal:not([open]) { display: none; }  /* re-oculta <dialog> cerrado; gana por especificidad al @media 768 .modal{display:flex} (ENG-016.C-4.7) */
.modal::backdrop { background: rgba(13, 12, 10, 0.82); }
.modal h2 { font-family: 'Syne', sans-serif; font-size: 23px; margin-bottom: 20px; }
.modal h3 { font-size: 16px; margin: 24px 0 10px; }
.banda-provisional {
  background: var(--ash);
  border-bottom: 2px dashed var(--smoke);
  color: var(--smoke);
  font-family: 'Figtree', sans-serif; font-weight: 600; font-size: 14px;
  padding: 10px 14px; margin: 0 0 16px; border-radius: 8px;
}
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px 16px; }
.modal-ancho .form-grid { grid-template-columns: repeat(3, 1fr); }
.span2 { grid-column: span 2; }
.span3 { grid-column: span 3; }
.modal-botones { display: flex; justify-content: flex-end; gap: 12px; margin-top: 28px; flex-wrap: wrap; }

.tabla-wrap-lineas { background: var(--carbon); border: 2px solid var(--ash-elevado); border-radius: 10px; }
.tabla-lineas th { padding: 10px 12px; border-color: var(--ash-elevado); }
.tabla-lineas td { padding: 8px 10px; border-color: var(--ash-elevado); }
.tabla-lineas input { padding: 8px 10px; margin: 0; background: var(--ash); }
.tabla-lineas input.num { text-align: right; font-family: 'Outfit', sans-serif; }
.btn-linea-borrar {
  background: transparent; border: 2px solid var(--danger);
  color: var(--danger); border-radius: 8px;
  min-height: 36px; min-width: 36px; cursor: pointer;
  font-size: 16px; font-weight: 700;
}
#btn-add-linea { margin-top: 12px; }

.totales { margin-top: 20px; display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.totales div { display: flex; gap: 24px; font-size: 15px; align-items: baseline; }
.totales span { color: var(--smoke); }
.total-grande strong { font-size: 26px; color: var(--brasa); }

/* ============================================================
   Mensajes
   ============================================================ */
.msg-error { color: var(--danger); font-size: 14px; font-weight: 600; margin-top: 14px; }
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--ash-elevado);
  border: 2px solid var(--borde-fuerte);
  border-radius: 10px;
  padding: 14px 24px;
  font-size: 15px; font-weight: 600;
  z-index: 100;
}
.toast.ok { border-color: var(--success); }
.toast.error { border-color: var(--danger); color: var(--danger); }

/* ============================================================
   Responsive
   ============================================================ */
@media (max-width: 980px) {
  #pantalla-login.activa { grid-template-columns: 1fr; }
  .login-marca { border-right: none; border-bottom: 2px solid var(--borde-fuerte); padding: 36px 24px; }
  #pantalla-app.activa { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; align-items: center; gap: 12px; padding: 12px 16px; }
  .ascua-side { margin: 0 12px 0 0; }
  .side-nav { flex-direction: row; }
  .side-btn { min-height: 44px; }
  .side-pie { border: none; padding: 0; margin-left: auto; flex-direction: row; align-items: center; }
  .user-email { display: none; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .panel-doble { grid-template-columns: 1fr; }
  .contenido { padding: 20px 16px; }
}

/* ============================================================
   Responsive movil — CONSOLIDADO (ENG-016.C-4.1).
   Orden de source: 980 -> 768 (este) -> 640, para cascada predecible.
   Este bloque va ANTES del 640: asi el icono-only de <=640 (mas especifico
   para pantallas estrechas) gana al icono+label del bottom bar.
   Solo tokens Ember Dark. Sin azul, sin brasa nueva. Desktop >768px SIN cambios.
   ============================================================ */
@media (max-width: 768px) {
  /* App sin columna lateral; hueco inferior para la barra fija */
  #pantalla-app.activa { grid-template-columns: 1fr; }
  .contenido { padding: 16px 14px 92px; }

  /* NAV -> barra inferior fija (gana al bloque 980) */
  .sidebar {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto;
    width: 100%; height: auto;
    flex-direction: row; align-items: center; gap: 6px;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    border-right: none; border-top: 2px solid var(--borde-fuerte);
    z-index: 90;
  }
  .ascua-side { display: none; }
  .side-nav { flex-direction: row; flex: 1; gap: 4px; justify-content: space-between; }
  .side-btn {
    flex: 1 1 0; min-width: 0; flex-direction: column; gap: 4px;
    min-height: 52px; padding: 6px 2px;
    font-size: 11px; font-weight: 600; text-align: center; justify-content: center;
  }
  .side-btn .side-chip { width: 28px; height: 28px; margin: 0; }
  .side-pie { flex-direction: row; align-items: center; border-top: none; padding: 0; margin: 0; flex: 0 0 auto; }
  .user-email { display: none; }
  .side-pie .btn { width: auto; min-height: 44px; padding: 6px 12px; font-size: 12px; }

  /* CABECERA de vista -> columna: h1 arriba, controles debajo a ancho completo */
  .vista-cabecera { flex-direction: column; align-items: stretch; gap: 12px; margin-bottom: 16px; }
  .vista-cabecera h1 { font-size: 24px; word-break: break-word; }
  .cabecera-derecha { width: 100%; flex-wrap: wrap; gap: 10px; }
  .cabecera-derecha input[type="search"] { width: 100%; min-height: 44px; }
  .cabecera-derecha .btn { flex: 1 1 auto; min-height: 44px; }

  /* LISTAS -> cards compactas con jerarquia.
     ORDEN DE COLUMNAS DEL THEAD (fijo; si cambia en gestion.js/HTML, actualizar nth-child):
       facturas/presupuestos: 1 Numero | 2 Fecha | 3 Cliente | 4 Base | 5 IVA | 6 Total | 7 Estado | 8 accion
       clientes:              1 Razon social | 2 NIF | 3 Email | 4 Localidad | 5 accion */
  .card.tabla-wrap { border: none; background: transparent; padding: 0; overflow: visible; }
  #tabla-facturas, #tabla-presupuestos, #tabla-clientes,
  #tabla-facturas tbody, #tabla-presupuestos tbody, #tabla-clientes tbody { display: block; }
  #tabla-facturas thead, #tabla-presupuestos thead, #tabla-clientes thead { display: none; }
  #tabla-facturas tr, #tabla-presupuestos tr, #tabla-clientes tr {
    display: flex; flex-direction: column;
    background: var(--ash); border: 2px solid var(--borde-fuerte);
    border-radius: 12px; padding: 14px 16px; margin-bottom: 12px;
  }
  #tabla-facturas tbody tr:hover, #tabla-presupuestos tbody tr:hover, #tabla-clientes tbody tr:hover { background: var(--ash); }
  #tabla-facturas td, #tabla-presupuestos td, #tabla-clientes td {
    display: flex; justify-content: space-between; align-items: baseline; gap: 16px;
    width: auto; padding: 3px 0; border: none; font-size: 15px; text-align: right;
  }
  #tabla-facturas td:empty, #tabla-presupuestos td:empty, #tabla-clientes td:empty { display: none; }

  /* etiqueta ::before (>=14px) */
  #tabla-facturas td::before, #tabla-presupuestos td::before, #tabla-clientes td::before {
    color: var(--smoke); font-size: 14px; font-weight: 700; text-transform: uppercase;
    letter-spacing: .04em; text-align: left; flex: 0 0 auto; font-family: 'Figtree', sans-serif;
  }
  #tabla-facturas td:nth-child(1):not(.celda-vacia)::before, #tabla-presupuestos td:nth-child(1):not(.celda-vacia)::before { content: 'Numero'; }
  #tabla-facturas td:nth-child(2)::before, #tabla-presupuestos td:nth-child(2)::before { content: 'Fecha'; }
  #tabla-facturas td:nth-child(3)::before, #tabla-presupuestos td:nth-child(3)::before { content: 'Cliente'; }
  #tabla-facturas td:nth-child(4)::before, #tabla-presupuestos td:nth-child(4)::before { content: 'Base'; }
  #tabla-facturas td:nth-child(5)::before, #tabla-presupuestos td:nth-child(5)::before { content: 'IVA'; }
  #tabla-facturas td:nth-child(6)::before, #tabla-presupuestos td:nth-child(6)::before { content: 'Total'; }
  #tabla-facturas td:nth-child(7)::before, #tabla-presupuestos td:nth-child(7)::before { content: 'Estado'; }
  #tabla-clientes td:nth-child(1):not(.celda-vacia)::before { content: 'Razon social'; }
  #tabla-clientes td:nth-child(2)::before { content: 'NIF'; }
  #tabla-clientes td:nth-child(3)::before { content: 'Email'; }
  #tabla-clientes td:nth-child(4)::before { content: 'Localidad'; }

  /* JERARQUIA (flex order dentro del card, tr es flex-column):
     arriba y grande: Numero, Cliente, Total, Estado ; abajo y atenuado: Fecha, Base, IVA ; boton al final */
  #tabla-facturas td:nth-child(1), #tabla-presupuestos td:nth-child(1) { order: 1; font-family: 'Outfit', sans-serif; font-size: 19px; font-weight: 700; color: var(--bone); padding: 4px 0; }
  #tabla-facturas td:nth-child(3), #tabla-presupuestos td:nth-child(3) { order: 2; font-size: 16px; font-weight: 600; color: var(--bone); }
  #tabla-facturas td:nth-child(6), #tabla-presupuestos td:nth-child(6) { order: 3; font-family: 'Outfit', sans-serif; font-size: 19px; font-weight: 700; color: var(--bone); padding: 4px 0; }
  #tabla-facturas td:nth-child(7), #tabla-presupuestos td:nth-child(7) { order: 4; padding: 6px 0; }
  #tabla-facturas td:nth-child(2), #tabla-presupuestos td:nth-child(2),
  #tabla-facturas td:nth-child(4), #tabla-presupuestos td:nth-child(4),
  #tabla-facturas td:nth-child(5), #tabla-presupuestos td:nth-child(5) { font-size: 13px; color: var(--smoke); padding: 1px 0; }
  #tabla-facturas td:nth-child(2), #tabla-presupuestos td:nth-child(2) { order: 5; }
  #tabla-facturas td:nth-child(4), #tabla-presupuestos td:nth-child(4) { order: 6; }
  #tabla-facturas td:nth-child(5), #tabla-presupuestos td:nth-child(5) { order: 7; }
  #tabla-facturas td:nth-child(2)::before, #tabla-presupuestos td:nth-child(2)::before,
  #tabla-facturas td:nth-child(4)::before, #tabla-presupuestos td:nth-child(4)::before,
  #tabla-facturas td:nth-child(5)::before, #tabla-presupuestos td:nth-child(5)::before { font-size: 13px; }
  #tabla-facturas td:nth-child(8), #tabla-presupuestos td:nth-child(8) { order: 8; }
  #tabla-clientes td:nth-child(1) { font-size: 17px; font-weight: 700; color: var(--bone); padding: 4px 0; }

  /* accion (ultima celda): boton a ancho completo, target >=44px */
  #tabla-facturas td:last-child, #tabla-presupuestos td:last-child, #tabla-clientes td:last-child { padding-top: 12px; flex-wrap: wrap; gap: 8px; }
  #tabla-facturas td:last-child .btn, #tabla-presupuestos td:last-child .btn, #tabla-clientes td:last-child .btn { flex: 1; min-height: 44px; }
  #tabla-facturas td.celda-vacia, #tabla-presupuestos td.celda-vacia, #tabla-clientes td.celda-vacia { display: block; text-align: center; color: var(--smoke); }

  /* MODAL -> pantalla completa, scroll interno, totales sticky al pie */
  .modal, .modal-ancho {
    width: 100%; max-width: 100%; height: 100vh; height: 100dvh; max-height: 100dvh;
    margin: 0; border: none; border-radius: 0; padding: 0; display: flex; flex-direction: column;
  }
  .modal form {
    display: flex; flex-direction: column; width: 100%; flex: 1 1 auto; overflow-y: auto;
    padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  }
  .modal-ancho .form-grid { grid-template-columns: 1fr; }
  .tabla-wrap-lineas { overflow-x: auto; }
  .tabla-lineas { min-width: 520px; }
  .tabla-lineas input { padding: 10px; min-height: 44px; }
  .btn-linea-borrar { min-height: 44px; min-width: 44px; }
  .totales {
    position: sticky; bottom: 0; z-index: 2;
    margin: 20px -16px 0; padding: 14px 16px;
    background: var(--ash); border-top: 2px solid var(--borde-fuerte);
  }
  .modal-botones { flex-wrap: wrap; margin-top: 16px; }
  .modal-botones .btn { flex: 1 1 auto; min-height: 44px; }

  /* Toast por encima de la barra inferior */
  .toast { bottom: calc(96px + env(safe-area-inset-bottom)); }
}

@media (max-width: 640px) {
  .form-grid, .modal-ancho .form-grid { grid-template-columns: 1fr; }
  .span2, .span3 { grid-column: span 1; }
  input[type="search"] { width: 100%; }
  .kpis { grid-template-columns: repeat(2, 1fr); gap: 10px; }  /* 2x2 compacto en movil (ENG-016.C-4.4) */
  .kpi { padding: 12px 14px; border-radius: 10px; gap: 2px; }
  .kpi-cifra { font-size: 24px; }
  .kpi-etiqueta { font-size: 12px; }
  .kpi-nota { display: none; }
  /* recientes del panel: ocultar columna Compartir (redundante con "Ver todas") + proteger overflow (ENG-016.C-4.5) */
  #tbody-recientes td:nth-child(5) { display: none; }
  .panel-doble .card:not(.card-accion) { overflow-x: auto; }
  .card-recientes-presu { overflow-x: auto; }
  #tbody-recientes-presu td:nth-child(5) { display: none; }
  .side-btn { font-size: 0; gap: 3px; } /* texto suelto oculto; inicial via ::after (ENG-016.C-4.2) */
  .side-btn::after { font-size: 11px; font-weight: 700; color: var(--smoke); font-family: 'Figtree', sans-serif; }
  .side-btn.activa::after { color: var(--bone); }
  .side-btn[data-vista='panel']::after        { content: 'I'; }
  .side-btn[data-vista='facturas']::after     { content: 'F'; }
  .side-btn[data-vista='presupuestos']::after { content: 'P'; }
  .side-btn[data-vista='clientes']::after     { content: 'C'; }
  .side-btn .side-chip { margin: 0; }
}

/* ---------- Gráfica de facturación (panel · ENG-016.GRAFICA) ---------- */
.grafica { margin-bottom: 24px; position: relative; }
.grafica-cab { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; gap: 12px; flex-wrap: wrap; }
.grafica-cab h2 { font-family: 'Syne', sans-serif; font-size: 19px; }
.grafica-toggle { display: inline-flex; gap: 3px; background: var(--carbon); border: 1px solid var(--borde-fuerte); border-radius: 10px; padding: 3px; }
.grafica-toggle button { background: transparent; border: none; color: var(--smoke); padding: 6px 14px; border-radius: 7px; font-size: 13px; font-weight: 600; cursor: pointer; font-family: 'Figtree', sans-serif; }
.grafica-toggle button:hover { color: var(--bone); }
.grafica-toggle button.activa { background: var(--brasa); color: var(--carbon); font-weight: 700; }
.grafica-barras { display: flex; align-items: flex-end; gap: 8px; height: 190px; padding-top: 8px; }
.gb-col { flex: 1 1 0; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; cursor: pointer; outline: none; }
.gb-bar { width: 66%; max-width: 46px; background: linear-gradient(180deg, var(--brasa), var(--danger)); border-radius: 5px 5px 0 0; transition: height .35s ease, filter .15s; }
.gb-col:hover .gb-bar, .gb-col:focus-visible .gb-bar { filter: brightness(1.18); }
.gb-col:focus-visible { outline: 2px solid var(--brasa); outline-offset: 2px; border-radius: 4px; }
.gb-label { margin-top: 8px; font-size: 12px; font-weight: 600; color: var(--smoke); }
.grafica-tooltip { position: absolute; transform: translate(-50%, -100%); background: var(--carbon); border: 1px solid var(--brasa); border-radius: 9px; padding: 8px 11px; font-size: 13px; line-height: 1.35; color: var(--bone); pointer-events: none; white-space: nowrap; z-index: 6; box-shadow: 0 6px 18px rgba(0,0,0,.45); }
.grafica-tooltip .tt-sub { color: var(--smoke); font-size: 12px; }
.grafica-tooltip.oculto { display: none; }
.grafica-nota { margin-top: 12px; font-size: 12px; color: var(--smoke); line-height: 1.4; }
.grafica-nota.oculto { display: none; }

@media (max-width: 640px) {
  .grafica-barras { height: 150px; gap: 4px; }
  .gb-bar { width: 80%; }
  .gb-label { font-size: 10px; }
  .grafica-cab { flex-direction: column; align-items: stretch; }
  .grafica-toggle { justify-content: center; }
  .grafica-tooltip { font-size: 12px; }
}

/* ===== ENG-016.C-4.8 · Líneas del modal como TARJETAS apiladas en móvil (<=640) ===== */
/* Solo presentación: filaLineaHTML y las clases/ids (lin-concepto/lin-cantidad/lin-precio/lin-dto/lin-iva) NO cambian; el submit sigue leyéndolas. Desktop >640 intacto. */
@media (max-width: 640px) {
  .tabla-lineas { min-width: 0; }
  .tabla-lineas thead { display: none; }
  .tabla-lineas, .tabla-lineas tbody { display: block; }
  .tabla-lineas tbody tr {
    position: relative; display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end;
    background: var(--ash); border: 1.5px solid var(--borde-fuerte); border-radius: 12px;
    padding: 14px; margin-bottom: 12px;
  }
  .tabla-lineas tbody td { display: block; padding: 0; border: none; }
  .tabla-lineas tbody td::before {
    display: block; margin-bottom: 3px; color: var(--smoke); font-size: 11px; font-weight: 700;
    text-transform: uppercase; letter-spacing: .03em; font-family: 'Figtree', sans-serif;
  }
  .tabla-lineas tbody td input { width: 100%; }
  .tabla-lineas tbody td:nth-child(1) { flex: 1 1 100%; padding-right: 42px; }
  .tabla-lineas tbody td:nth-child(1)::before { content: 'Concepto'; }
  .tabla-lineas tbody td:nth-child(1) input { font-size: 15px; }
  .tabla-lineas tbody td:nth-child(2), .tabla-lineas tbody td:nth-child(3),
  .tabla-lineas tbody td:nth-child(4), .tabla-lineas tbody td:nth-child(5) { flex: 1 1 62px; min-width: 62px; }
  .tabla-lineas tbody td:nth-child(2)::before { content: 'Cant'; }
  .tabla-lineas tbody td:nth-child(3)::before { content: 'Precio'; }
  .tabla-lineas tbody td:nth-child(4)::before { content: 'Dto %'; }
  .tabla-lineas tbody td:nth-child(5)::before { content: 'IVA %'; }
  .tabla-lineas tbody td:nth-child(6) { flex: 1 1 100%; text-align: right; font-weight: 700; font-family: 'Outfit', sans-serif; }
  .tabla-lineas tbody td:nth-child(6)::before { content: 'Importe'; display: inline; margin-right: 8px; }
  .tabla-lineas tbody td:nth-child(7) { position: absolute; top: 8px; right: 6px; flex: 0 0 auto; }
  .tabla-lineas tbody td:nth-child(7)::before { display: none; }
  .tabla-lineas .btn-linea-borrar { min-width: 44px; min-height: 44px; font-size: 20px; }
  .tabla-wrap-lineas { max-height: 60vh; overflow-y: auto; overflow-x: hidden; }
}
