/* Colores de marca: se inyectan desde ajustes (--primary, --header, ...) */
:root {
  /* Ancho del recibo (pantalla y papel). A4 útil ≈ 780px. */
  --ancho-recibo: 752px;
  --primary: #0f766e;
  --primary-text: #fff;
  --header: #17343f;
  --header-text: #fff;
  --bg: #f2f4f7;
  --surface: #fff;
  --ink: #1b2430;
  --muted: #5f6c7b;
  --line: #dce2e9;
  --soft: color-mix(in srgb, var(--primary) 9%, #fff);
  --ok: #15803d;
  --warn: #a16207;
  --danger: #b42318;
  --font: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.5 var(--font); }
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 1.6rem; font-weight: 650; }
h2 { font-size: 1.1rem; font-weight: 650; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--primary) 55%, #fff); outline-offset: 2px; }

/* ---------- Barra superior ---------- */
.topbar { background: var(--header); color: var(--header-text); }
.topbar-in { max-width: 1120px; margin: 0 auto; padding: 0 20px; display: flex; align-items: center; gap: 24px; min-height: 60px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 12px; color: inherit; text-decoration: none; padding: 8px 0; }
.brand img { height: 38px; width: 38px; object-fit: contain; background: #fff; border-radius: 8px; padding: 3px; }
.brand span { font-weight: 650; font-size: 1.02rem; max-width: 26ch; line-height: 1.2; }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a { color: inherit; text-decoration: none; padding: 8px 14px; border-radius: 8px; font-weight: 500; opacity: .86; }
.nav a:hover { opacity: 1; background: color-mix(in srgb, currentColor 12%, transparent); }
.nav a.activo { opacity: 1; background: color-mix(in srgb, currentColor 18%, transparent); }
.usuario { display: flex; align-items: center; gap: 10px; font-size: .9rem; }
.usuario a, .usuario button { color: inherit; opacity: .85; background: none; border: 0; font: inherit; cursor: pointer; padding: 6px 8px; border-radius: 6px; text-decoration: none; }
.usuario a:hover, .usuario button:hover { opacity: 1; background: color-mix(in srgb, currentColor 12%, transparent); }
.usuario form { margin: 0; }

/* ---------- Página ---------- */
.page { max-width: 1120px; margin: 0 auto; padding: 28px 20px 60px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--muted); font-size: .9rem; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.back { display: inline-block; margin-bottom: 14px; text-decoration: none; font-size: .9rem; }
.back:hover { text-decoration: underline; }

.flash { padding: 11px 14px; border-radius: 8px; margin-bottom: 18px; border: 1px solid; font-size: .92rem; }
.flash.ok { background: #ecfdf3; border-color: #a6e9c1; color: #146c3a; }
.flash.error { background: #fef3f2; border-color: #f7b8b2; color: #93251b; }

/* ---------- Tarjetas y rejillas ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 22px; }
.card + .card { margin-top: 20px; }
.card h2 { margin-bottom: 14px; }
.card h2 small { font-weight: 400; color: var(--muted); font-size: .85rem; margin-left: 6px; }
.grid { display: grid; gap: 20px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g-side { grid-template-columns: minmax(0, 320px) minmax(0, 1fr); align-items: start; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 18px 20px; }
.stat b { display: block; font-size: 1.9rem; font-weight: 650; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: .88rem; }
.stat.principal { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.stat.principal span { color: inherit; opacity: .85; }

/* ---------- Botones ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 15px; border-radius: 8px; border: 1px solid var(--primary); background: var(--primary); color: var(--primary-text); font: inherit; font-weight: 550; font-size: .9rem; cursor: pointer; text-decoration: none; line-height: 1.3; }
.btn:hover { filter: brightness(.93); }
.btn.sec { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn.sec:hover { background: var(--bg); filter: none; }
.btn.warn { background: #fff8e6; color: var(--warn); border-color: #f0d48a; }
.btn.danger { background: #fff; color: var(--danger); border-color: #f0b4ae; }
.btn.danger:hover { background: #fef3f2; filter: none; }
.btn.sm { padding: 4px 10px; font-size: .82rem; }
form.inline { display: inline; margin: 0; }

/* ---------- Formularios ---------- */
.field { margin-bottom: 16px; }
.field label { display: block; font-weight: 550; font-size: .88rem; margin-bottom: 5px; }
.field .hint { color: var(--muted); font-size: .82rem; margin-top: 4px; }
input[type=text], input[type=email], input[type=tel], input[type=number], input[type=date], input[type=datetime-local], input[type=password], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid #c5cdd7; border-radius: 8px; font: inherit; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); outline: 3px solid color-mix(in srgb, var(--primary) 25%, transparent); outline-offset: 0; }
input[type=color] { width: 54px; height: 40px; padding: 2px; border: 1px solid #c5cdd7; border-radius: 8px; background: #fff; cursor: pointer; }
.cols { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 18px; }
.cols .full { grid-column: 1 / -1; }
.form-narrow { max-width: 620px; margin: 0 auto; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; padding-top: 16px; border-top: 1px solid var(--line); }
.req { color: var(--danger); }
.check { display: flex; gap: 8px; align-items: center; font-size: .9rem; margin-top: 8px; }
.check input { width: auto; }

/* ---------- Tablas ---------- */
.table-wrap { overflow-x: auto; }
table.tabla { width: 100%; border-collapse: collapse; font-size: .92rem; }
.tabla th { text-align: left; font-weight: 600; font-size: .82rem; color: var(--muted); padding: 10px 14px; border-bottom: 1px solid var(--line); background: #f8f9fb; white-space: nowrap; }
.tabla th[data-sort] { cursor: pointer; user-select: none; }
.tabla th[data-sort]::after { content: "  ↕"; opacity: .35; }
.tabla th[data-dir=asc]::after { content: "  ↑"; opacity: 1; }
.tabla th[data-dir=desc]::after { content: "  ↓"; opacity: 1; }
.tabla td { padding: 11px 14px; border-bottom: 1px solid #edf0f4; vertical-align: middle; }
.tabla tbody tr:last-child td { border-bottom: 0; }
.tabla tbody tr:hover { background: var(--soft); }
.tabla tr[data-href] { cursor: pointer; }
.tabla .num { text-align: right; font-variant-numeric: tabular-nums; }
.tabla .centro { text-align: center; }
.tabla tr.anulada td { color: var(--muted); }
.tabla tr.anulada td.importe { text-decoration: line-through; }
.vacio { padding: 26px; text-align: center; color: var(--muted); }
.buscador { margin-bottom: 14px; }

/* ---------- Etiquetas ---------- */
.badge { display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: .78rem; font-weight: 600; background: #eef1f5; color: #3b4756; white-space: nowrap; }
.badge.al-dia { background: #dcfce7; color: #14532d; }
.badge.pendiente { background: #fef3c7; color: #78350f; }
.badge.inactivo { background: #dbeafe; color: #1e3a8a; }
.badge.baja-auto { background: #e5e7eb; color: #374151; }
.badge.baja-manual { background: #475569; color: #fff; }
.badge.anulado { background: #fee4e2; color: #93251b; }
.badge.admin { background: var(--soft); color: var(--primary); }

/* ---------- Panel ---------- */
.reparto { display: flex; align-items: center; gap: 28px; flex-wrap: wrap; }
.donut { width: 168px; height: 168px; border-radius: 50%; flex-shrink: 0; position: relative; }
.donut::after { content: attr(data-total); position: absolute; inset: 30px; border-radius: 50%; background: var(--surface); display: grid; place-items: center; font-size: 1.7rem; font-weight: 650; }
.leyenda { list-style: none; margin: 0; padding: 0; display: grid; gap: 7px; font-size: .92rem; }
.leyenda li { display: flex; align-items: center; gap: 9px; }
.leyenda i { width: 11px; height: 11px; border-radius: 3px; flex-shrink: 0; }
.leyenda b { margin-left: auto; padding-left: 18px; font-variant-numeric: tabular-nums; }
.lista { list-style: none; margin: 0; padding: 0; }
.lista li { display: flex; justify-content: space-between; gap: 12px; align-items: center; padding: 9px 0; border-bottom: 1px solid #edf0f4; }
.lista li:last-child { border-bottom: 0; }
.lista small { color: var(--muted); display: block; }

/* ---------- Ficha ---------- */
.dato { margin-bottom: 14px; }
.dato small { display: block; color: var(--muted); font-size: .8rem; }
.dato p { margin: 0; }
.nota { background: #f8f9fb; border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; font-size: .9rem; white-space: pre-wrap; }
.historial { max-height: 300px; overflow-y: auto; }
.historial div { border-left: 3px solid var(--primary); padding: 2px 0 2px 12px; margin-bottom: 12px; }
.historial small { color: var(--muted); display: flex; justify-content: space-between; gap: 10px; }
.historial p { margin: 0; }
.historial p.det { color: var(--muted); font-size: .88rem; }

/* ---------- Acceso ---------- */
body.acceso { display: grid; place-items: center; min-height: 100vh; }
body.acceso .page { width: 100%; max-width: 400px; padding: 24px 20px; }
.acceso-marca { text-align: center; margin-bottom: 22px; }
.acceso-marca img { height: 68px; max-width: 200px; object-fit: contain; margin-bottom: 10px; }
.acceso-marca h1 { font-size: 1.35rem; }
.acceso-marca p { color: var(--muted); margin: 4px 0 0; font-size: .9rem; }

/* ---------- Vista previa de colores (ajustes) ---------- */
.preview { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin-top: 4px; }
.preview .pv-bar { padding: 10px 14px; display: flex; align-items: center; gap: 12px; font-weight: 600; }
.preview .pv-bar img { height: 26px; width: 26px; object-fit: contain; background: #fff; border-radius: 5px; padding: 2px; }
.preview .pv-body { padding: 12px 14px; background: #fff; display: flex; gap: 10px; align-items: center; }
.preview .pv-btn { padding: 5px 12px; border-radius: 7px; font-size: .85rem; font-weight: 550; }
.color-fila { display: flex; align-items: center; gap: 10px; }

/* ---------- Recibo ---------- */
.recibo { max-width: var(--ancho-recibo); margin: 0 auto; background: #fff; border: 1px solid var(--line); border-radius: 4px; padding: 44px; position: relative; overflow: hidden; }
.recibo-cab { display: flex; justify-content: space-between; gap: 24px; padding-bottom: 20px; border-bottom: 3px solid var(--primary); }
.recibo-emisor { display: flex; gap: 16px; align-items: flex-start; }
.recibo-emisor img { height: 68px; max-width: 120px; object-fit: contain; }
.recibo-emisor h1 { font-family: var(--serif); font-size: 1.45rem; font-weight: 700; letter-spacing: .01em; }
.recibo-emisor p { margin: 2px 0 0; font-size: .82rem; color: #4b5563; }
.recibo-num { text-align: right; flex-shrink: 0; }
.recibo-num small { display: block; color: var(--muted); font-size: .8rem; }
.recibo-num b { font-family: var(--serif); font-size: 1.4rem; color: var(--primary); }
.recibo-texto { font-family: var(--serif); font-size: 1.12rem; line-height: 1.7; margin: 28px 0; }
.recibo-datos { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; padding: 16px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.recibo-datos small { display: block; color: var(--muted); font-size: .78rem; }
.recibo-datos b { font-weight: 600; }
.recibo-importe { display: flex; justify-content: space-between; align-items: baseline; margin: 26px 0 8px; padding: 16px 20px; border: 2px solid var(--primary); border-radius: 6px; }
.recibo-importe span { font-family: var(--serif); font-size: 1.05rem; }
.recibo-importe b { font-family: var(--serif); font-size: 2rem; }
.recibo-firma { display: flex; justify-content: flex-end; margin-top: 64px; }
.recibo-firma div { width: 230px; text-align: center; border-top: 1px solid #6b7280; padding-top: 6px; font-size: .82rem; color: #4b5563; }
.recibo-pie { margin-top: 30px; font-size: .75rem; color: var(--muted); text-align: center; }
.sello { position: absolute; top: 42%; left: 50%; transform: translate(-50%, -50%) rotate(-18deg); font-family: var(--serif); font-size: 5rem; font-weight: 700; letter-spacing: .1em; color: rgba(180, 35, 24, .22); border: 8px solid rgba(180, 35, 24, .22); padding: 0 24px; border-radius: 10px; pointer-events: none; }
.anular-box { max-width: var(--ancho-recibo); margin: 18px auto 0; }
.anular-box summary { cursor: pointer; color: var(--danger); font-weight: 550; }

.oculto-print { }

@media screen and (max-width: 860px) {
  .g2, .g3, .g-side, .cols { grid-template-columns: 1fr; }
  .topbar-in { gap: 8px 16px; padding-top: 6px; padding-bottom: 6px; }
  .nav { order: 3; flex-basis: 100%; overflow-x: auto; }
  .usuario { margin-left: auto; }
  .recibo { padding: 26px 20px; }
  .recibo-cab { flex-direction: column; }
  .recibo-num { text-align: left; }
  .recibo-datos { grid-template-columns: 1fr; }
  .sello { font-size: 3rem; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

@media print {
  /* Margen 0 quita también la cabecera y el pie que añade el navegador
     (fecha, URL); el margen real se da con el relleno de .page. */
  @page { size: A4; margin: 0; }
  .topbar, .no-print, .flash { display: none !important; }
  body { background: #fff; }
  .page { max-width: none; padding: 10mm 0; }
  .recibo { max-width: var(--ancho-recibo); margin: 0 auto; padding: 44px !important; }
  .recibo-cab { flex-direction: row !important; }
  .recibo-num { text-align: right !important; }
  .recibo-datos { grid-template-columns: repeat(3, 1fr) !important; }
  .sello { font-size: 5rem !important; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

.preview .pv-bar { background: var(--pv-h); color: var(--pv-ht); }
.preview .pv-btn { background: var(--pv-p); color: var(--pv-pt); }
.grid + .card { margin-top: 20px; }

/* ---------- Importación ---------- */
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.opciones-fila { display: flex; gap: 24px; align-items: end; flex-wrap: wrap; }
.mapa th { vertical-align: bottom; background: #f8f9fb; }
.mapa select { min-width: 160px; padding: 6px 8px; font-size: .85rem; }
.mapa thead tr + tr th { font-weight: 500; color: var(--muted); border-bottom: 1px solid var(--line); }
.mapa td { max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.letra { display: inline-block; min-width: 22px; text-align: center; background: var(--soft); color: var(--primary); border-radius: 5px; font-weight: 700; margin-right: 4px; }
@media screen and (max-width: 860px) { .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

/* ---------- Agenda ---------- */
.agenda-nav { align-items: center; }
.nota-agenda { border-left: 3px solid var(--primary); padding: 4px 0 4px 14px; margin-bottom: 16px; }
.nota-agenda-cab { display: flex; justify-content: space-between; align-items: center; gap: 10px; color: var(--muted); font-size: .85rem; }
.nota-agenda-cab b { color: var(--ink); margin-right: 6px; }
.nota-agenda p { margin: 4px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.agenda-form { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--line); }
.agenda-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 280px); }
@media screen and (max-width: 860px) { .agenda-grid { grid-template-columns: 1fr; } }

/* ======================================================================
   Móvil y tablet pequeña
   Todo lo que sigue solo se activa en pantallas estrechas (salvo la
   primera regla, que no tiene efecto en escritorio): el diseño de
   escritorio no cambia.
   ====================================================================== */

.solo-movil { display: none; }

/* Una columna no debe ensancharse por su contenido (tablas, textos largos) */
@media screen and (max-width: 860px) {
  .g2, .g3, .g-side, .cols, .agenda-grid { grid-template-columns: minmax(0, 1fr); }
  .grid > *, .cols > * { min-width: 0; }
}

@media screen and (max-width: 640px) {
  .page { padding: 16px 14px calc(84px + env(safe-area-inset-bottom, 0px)); }
  h1 { font-size: 1.35rem; }
  .card { padding: 16px; border-radius: 10px; }
  .card + .card, .grid + .card { margin-top: 14px; }
  section[id] { scroll-margin-top: 64px; }

  /* ---- Cabecera compacta, fija arriba ---- */
  .topbar { position: sticky; top: 0; z-index: 20; }
  .topbar-in { flex-wrap: nowrap; gap: 8px; min-height: 54px; padding: 0 12px; }
  .brand { flex: 1; min-width: 0; gap: 10px; padding: 6px 0; }
  .brand img { height: 32px; width: 32px; }
  .brand span {
    font-size: .9rem; line-height: 1.2; max-width: none; overflow: hidden;
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  }
  .usuario { margin-left: 0; gap: 0; font-size: .82rem; flex-shrink: 0; }
  .usuario > span { display: none; }
  .usuario a, .usuario button { min-height: 44px; display: inline-flex; align-items: center; padding: 0 9px; }

  /* ---- Menú inferior, al alcance del pulgar ---- */
  .nav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    flex: none; flex-basis: auto; order: 0; gap: 0; overflow: visible;
    background: var(--header);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    border-top: 1px solid color-mix(in srgb, var(--header-text) 20%, transparent);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, .18);
  }
  .nav a {
    flex: 1 1 0; min-width: 0; min-height: 58px; padding: 6px 2px; border-radius: 0;
    display: flex; align-items: center; justify-content: center; text-align: center;
    font-size: .74rem; line-height: 1.15; font-weight: 600; opacity: .75;
  }
  .nav a.activo { opacity: 1; background: color-mix(in srgb, currentColor 16%, transparent); box-shadow: inset 0 3px 0 currentColor; }
  body.escribiendo .nav { display: none; }   /* el teclado ya ocupa media pantalla */

  /* ---- Cabecera de cada pantalla y botones ---- */
  .page-head { flex-direction: column; align-items: stretch; gap: 12px; }
  .page-head .actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .page-head .actions > .btn:last-child:nth-child(odd),
  .page-head .actions > form:last-child:nth-child(odd) { grid-column: 1 / -1; }
  .actions form.inline { display: block; }
  .actions form .btn { width: 100%; }
  .agenda-nav input[type=date] { width: 100% !important; }
  .btn { min-height: 44px; padding: 10px 16px; justify-content: center; }
  .btn.sm { min-height: 38px; padding: 6px 12px; }

  /* ---- Formularios: 16px evita el zoom automático de iOS al enfocar ---- */
  input[type=text], input[type=email], input[type=tel], input[type=number], input[type=date],
  input[type=datetime-local], input[type=time], input[type=password], input[type=search], select, textarea {
    font-size: 16px; padding: 11px 12px;
  }
  select { min-height: 46px; }
  .check input { width: 22px; height: 22px; }
  .solo-movil { display: block; }
  .form-actions { flex-direction: column-reverse; }
  .form-actions .btn { width: 100%; }
  .opciones-fila { gap: 14px; }

  /* ---- Panel de inicio ---- */
  .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .g3 .stat.principal { grid-column: 1 / -1; }
  .stat { padding: 14px 16px; }
  .stat b { font-size: 1.6rem; }
  .reparto { justify-content: center; gap: 20px; }

  /* ---- Tablas como tarjetas (las que llevan la clase tabla-tarjetas) ---- */
  .tabla-tarjetas, .tabla-tarjetas tbody { display: block; }
  .tabla-tarjetas thead { display: none; }
  .tabla-tarjetas tr { display: block; padding: 10px 16px; border-bottom: 1px solid var(--line); }
  .tabla-tarjetas tbody tr:last-child { border-bottom: 0; }
  .tabla-tarjetas td {
    display: flex; justify-content: space-between; align-items: baseline; gap: 14px;
    padding: 4px 0; border: 0; text-align: right; overflow-wrap: anywhere;
  }
  .tabla-tarjetas td[data-label]::before {
    content: attr(data-label); color: var(--muted); font-size: .8rem; font-weight: 600;
    text-align: left; flex-shrink: 0;
  }
  .tabla-tarjetas td:not([data-label]) { justify-content: flex-end; flex-wrap: wrap; gap: 8px; padding-top: 8px; }
  .tabla-tarjetas td.vacio { display: block; text-align: center; padding: 22px 0; }
  .tabla-tarjetas tr[data-href]:active { background: var(--soft); }
  /* en tarjetas con relleno, las filas llegan hasta el borde para no duplicar la sangría */
  .card:not([style*="padding:0"]) > .table-wrap { margin: 0 -16px; }

  /* ---- Recibo en pantalla ---- */
  .recibo-emisor h1 { font-size: 1.15rem; }
  .recibo-emisor img { height: 52px; }
  .recibo-importe b { font-size: 1.6rem; }
  .recibo-firma { margin-top: 40px; }
  .anular-box { margin-top: 14px; }
}
