/* =====================================================================
   SAVA · Capa responsive global (teléfono, tablet, escritorio)
   Se carga DESPUÉS de los estilos propios de cada vista.
   ===================================================================== */

/* ---------- Base segura para iOS / Android ---------- */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; scroll-behavior: smooth; }
html, body { max-width: 100%; overflow-x: hidden; overflow-x: clip; }
body {
    min-height: 100vh;
    min-height: 100dvh;                 /* barra de direcciones móvil */
    -webkit-tap-highlight-color: transparent;
    padding-top: max(12px, env(safe-area-inset-top));      /* notch / barra de estado en modo app (PWA) */
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-bottom: max(12px, env(safe-area-inset-bottom));
}
img, svg, video { max-width: 100%; height: auto; }
.bubbles-background { height: 100vh; height: 100dvh; }

/* Las burbujas decorativas NUNCA deben interceptar el toque (bloqueaban el scroll en iPhone) */
.bubble { pointer-events: none !important; }
@media (hover: hover) and (pointer: fine) and (min-width: 1025px) {
    .bubble { pointer-events: auto !important; }
}
@media (prefers-reduced-motion: reduce) {
    .bubble, .bubbles-background { display: none !important; }
    * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
@media (max-width: 640px) {
    .bubble:nth-child(n+11) { display: none; }   /* menos burbujas = menos batería */
}

/* ---------- Cabecera y navegación ---------- */
.header-bar { flex-wrap: wrap; gap: 10px; }
.header-title { min-width: 0; }
.header-title h2 { word-break: break-word; }
.nav-buttons { flex-wrap: wrap; }

.admin-tabs {
    display: flex; gap: 8px; overflow-x: auto; -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity; padding-bottom: 6px; scrollbar-width: thin;
}
.admin-tabs .tab-btn { flex: 0 0 auto; scroll-snap-align: start; white-space: nowrap; }

/* ---------- Controles táctiles (mín. 44 px, sin zoom en iOS) ---------- */
.btn-action, .tab-btn, .btn-table, button, select, input:not([type="checkbox"]):not([type="radio"]), textarea {
    min-height: 44px;
}
.btn-table { min-width: 44px; justify-content: center; }
input, select, textarea { font-size: 16px; max-width: 100%; }   /* 16px evita el zoom automático de iOS */
input[type="checkbox"], input[type="radio"] { width: 22px; height: 22px; min-height: 0; }
input[type="date"], input[type="time"] { -webkit-appearance: none; appearance: none; }
.form-control, .form-select { width: 100%; }
button, .btn-action, .btn-table, a { touch-action: manipulation; }

/* ---------- Tarjetas y rejillas ---------- */
.content-card { min-width: 0; }
.form-grid { grid-template-columns: 1fr; }
@media (min-width: 600px)  { .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 1024px) { .form-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.kpi-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 560px)  { .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 900px)  { .kpi-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }

/* ---------- Tablas ---------- */
.table-responsive { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-responsive table { min-width: 0; }

/* Teléfono: cada fila se convierte en una tarjeta con etiquetas (las pone sava.js) */
@media (max-width: 700px) {
    table.sv-stack, table.sv-stack tbody, table.sv-stack tr, table.sv-stack td { display: block; width: 100%; min-width: 0 !important; }
    table.sv-stack thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
    table.sv-stack tr {
        background: rgba(30, 41, 59, 0.55); border: 1px solid rgba(255,255,255,0.10);
        border-radius: 12px; margin: 0 0 10px; padding: 6px 12px;
    }
    table.sv-stack td {
        display: flex; justify-content: space-between; align-items: center; gap: 12px;
        padding: 8px 0 !important; border-bottom: 1px dashed rgba(255,255,255,0.08) !important;
        text-align: right; word-break: break-word;
    }
    table.sv-stack td:last-child { border-bottom: 0 !important; }
    table.sv-stack td::before {
        content: attr(data-label); flex: 0 0 38%; text-align: left;
        font-size: 0.72rem; font-weight: 700; color: #38bdf8; text-transform: uppercase; letter-spacing: .3px;
    }
    table.sv-stack td[colspan]::before, table.sv-stack td:not([data-label])::before { content: none; }
    table.sv-stack td[colspan] { justify-content: center; text-align: center; }
    table.sv-stack td > div { justify-content: flex-end; flex-wrap: wrap; }
    table.sv-stack td.sv-block { display: block; text-align: left; }
    table.sv-stack td.sv-block::before { display: block; margin-bottom: 6px; }
    table.sv-stack td.sv-block > div { justify-content: flex-start; }
    table.sv-stack th { position: static; }
}

/* ---------- Asistencia: opciones de estado cómodas con el dedo ---------- */
@media (max-width: 700px) {
    table.sv-stack td label { padding: 6px 8px; }
}

/* ---------- Búsqueda ---------- */
.search-box { width: 100%; }
@media (min-width: 500px) { .search-box { width: auto; } }
.search-box input { width: 100%; }

/* ---------- Tablet (iPad / Android 10") ---------- */
@media (min-width: 701px) and (max-width: 1024px) {
    body { padding-top: 16px; }
    .content-card { padding: 18px; }
    table { font-size: .85rem; }
}

/* ---------- Escritorio ---------- */
@media (min-width: 1025px) {
    body { padding-left: 24px; padding-right: 24px; }
}

/* ---------- Mensajes, estado de consulta de cédula ---------- */
.alert-msg { word-break: break-word; }
.sv-cedula-status { font-size: .78rem; margin-top: 4px; min-height: 1.1em; display: none; }
.sv-cedula-status.show { display: block; }
.sv-cedula-status.loading { color: #38bdf8; }
.sv-cedula-status.ok { color: #34d399; }
.sv-cedula-status.warn { color: #fcd34d; }
.sv-cedula-status.err { color: #fca5a5; }

/* ---------- Diálogos (editar / promover) ---------- */
dialog.sv-dialog {
    border: 1px solid rgba(255,255,255,.15); border-radius: 16px; padding: 0;
    background: #0f172a; color: #f8fafc; width: min(640px, calc(100vw - 24px));
    max-height: calc(100dvh - 24px); overflow: auto; box-shadow: 0 30px 80px rgba(0,0,0,.6);
}
dialog.sv-dialog::backdrop { background: rgba(2, 6, 23, .72); backdrop-filter: blur(3px); }
.sv-dialog .sv-dialog-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 14px 18px; border-bottom: 1px solid rgba(255,255,255,.1); position: sticky; top: 0; background: #0f172a; z-index: 1; }
.sv-dialog .sv-dialog-body { padding: 16px 18px 20px; }
.sv-dialog h4 { font-size: 1rem; color: #38bdf8; }
.sv-x { background: transparent; border: 0; color: #94a3b8; font-size: 1.4rem; cursor: pointer; width: 44px; }
.sv-grid2 { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 560px) { .sv-grid2 { grid-template-columns: 1fr 1fr; } }
.sv-field label { display: block; font-size: .74rem; font-weight: 600; color: #38bdf8; margin-bottom: 5px; }
.sv-field input, .sv-field select, .sv-field textarea {
    width: 100%; background: rgba(30, 41, 59, .6); border: 1px solid rgba(255,255,255,.12);
    border-radius: 10px; padding: 10px 12px; color: #fff; outline: none;
}
.sv-field input:focus, .sv-field select:focus, .sv-field textarea:focus { border-color: #38bdf8; box-shadow: 0 0 0 3px rgba(56,189,248,.2); }
.sv-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 16px; }
.sv-btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 10px 18px; border-radius: 10px; border: 0; color: #fff; font-weight: 600; cursor: pointer; text-decoration: none; background: linear-gradient(135deg, #0284c7, #2563eb); }
.sv-btn.green { background: linear-gradient(135deg, #059669, #0d9488); }
.sv-btn.violet { background: linear-gradient(135deg, #7c3aed, #4f46e5); }
.sv-btn.gray { background: rgba(255,255,255,.08); border: 1px solid rgba(255,255,255,.18); }
.sv-btn.red { background: rgba(239,68,68,.2); border: 1px solid rgba(239,68,68,.4); color: #fca5a5; }
@media (max-width: 480px) { .sv-actions .sv-btn { width: 100%; } }
.sv-note { font-size: .8rem; color: #94a3b8; margin: 4px 0 12px; line-height: 1.45; }
.sv-pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .7rem; font-weight: 700; background: rgba(56,189,248,.15); color: #7dd3fc; border: 1px solid rgba(56,189,248,.3); }
.sv-pill.off { background: rgba(239,68,68,.15); color: #fca5a5; border-color: rgba(239,68,68,.3); }
.sv-pill.on { background: rgba(52,211,153,.15); color: #6ee7b7; border-color: rgba(52,211,153,.3); }

/* ---------- Separación entre tarjetas y tablas (antes quedaban pegadas) ---------- */
.tab-content.active { gap: 18px; }                       /* las pestañas son columnas flex: el gap separa las tarjetas */
.tab-content > .content-card { flex-shrink: 0; }          /* que no se compriman unas contra otras */
.content-card > h3 { margin-bottom: 12px; }
.content-card .table-responsive { margin-top: 14px; }
.content-card .form-grid + .form-grid { margin-top: 14px; }
.sv-actions { margin-top: 16px; gap: 10px; align-items: center; }
.sv-actions .sv-pill { padding: 6px 12px; }
.sv-note { margin-bottom: 14px; }
.alert-msg { margin-bottom: 14px; }

/* ---------- Modo app instalada (PWA / "Añadir a pantalla de inicio") ---------- */
@media (display-mode: standalone) {
    body { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
    a[target="_blank"] { cursor: pointer; }
}
@supports (-webkit-touch-callout: none) {          /* solo iOS */
    input, select, textarea { -webkit-appearance: none; appearance: none; border-radius: 10px; }
    select { background-image: linear-gradient(45deg, transparent 50%, #94a3b8 50%), linear-gradient(135deg, #94a3b8 50%, transparent 50%); background-position: calc(100% - 18px) 55%, calc(100% - 12px) 55%; background-size: 6px 6px; background-repeat: no-repeat; padding-right: 32px; }
}

/* Submenú de opciones dentro de una pestaña */
.sv-submenu { display:flex; gap:8px; overflow-x:auto; padding:2px 2px 6px; scroll-snap-type:x proximity; -webkit-overflow-scrolling:touch; }
.sv-submenu button { flex:0 0 auto; scroll-snap-align:start; white-space:nowrap; background:rgba(255,255,255,.06); color:#cbd5e1; border:1px solid rgba(255,255,255,.12); border-radius:999px; padding:8px 14px; font-size:.82rem; cursor:pointer; font-family:inherit; }
.sv-submenu button.activo { background:rgba(56,189,248,.18); color:#7dd3fc; border-color:rgba(56,189,248,.5); font-weight:700; }

/* Asistente virtual */
.sv-asis-burbuja { position:fixed; right:18px; bottom:18px; z-index:9998; width:56px; height:56px; border-radius:50%; border:1px solid rgba(56,189,248,.5); background:linear-gradient(135deg,#0284c7,#2563eb); color:#fff; font-size:1.4rem; cursor:pointer; box-shadow:0 8px 24px rgba(2,132,199,.45); display:flex; align-items:center; justify-content:center; padding:0; }
.sv-asis-burbuja.sv-asis-alta { bottom:92px; }
.sv-asis-panel { position:fixed; right:18px; bottom:86px; z-index:9999; width:360px; max-width:calc(100vw - 24px); height:520px; max-height:calc(100vh - 110px); display:flex; flex-direction:column; background:rgba(15,23,42,.97); border:1px solid rgba(255,255,255,.12); border-radius:18px; box-shadow:0 20px 50px rgba(0,0,0,.55); overflow:hidden; backdrop-filter:blur(10px); }
.sv-asis-panel[hidden] { display:none; }
.sv-asis-cab { display:flex; justify-content:space-between; align-items:center; padding:12px 14px; background:linear-gradient(135deg,#0284c7,#2563eb); color:#fff; font-weight:700; }
.sv-asis-x { background:transparent; border:none; color:#fff; font-size:1.5rem; line-height:1; cursor:pointer; padding:0 4px; }
.sv-asis-msgs { flex:1; overflow-y:auto; padding:12px; display:flex; flex-direction:column; gap:8px; }
.sv-asis-m { max-width:92%; padding:9px 12px; border-radius:14px; font-size:.86rem; line-height:1.4; color:#e2e8f0; }
.sv-asis-m.bot { background:rgba(255,255,255,.07); align-self:flex-start; }
.sv-asis-m.yo { background:#0369a1; color:#fff; align-self:flex-end; }
.sv-asis-m ol { margin:6px 0 8px 18px; padding:0; }
.sv-asis-m li { margin-bottom:4px; }
.sv-asis-ir { background:#0ea5e9; color:#fff; border:none; border-radius:999px; padding:7px 14px; font-size:.8rem; font-weight:700; cursor:pointer; font-family:inherit; }
.sv-asis-chips { display:flex; gap:6px; overflow-x:auto; padding:6px 10px; border-top:1px solid rgba(255,255,255,.08); }
.sv-asis-chip { flex:0 0 auto; background:rgba(56,189,248,.12); color:#7dd3fc; border:1px solid rgba(56,189,248,.35); border-radius:999px; padding:6px 12px; font-size:.76rem; cursor:pointer; font-family:inherit; text-align:left; }
.sv-asis-msgs .sv-asis-chip { align-self:flex-start; white-space:normal; }
.sv-asis-form { display:flex; gap:8px; padding:10px; border-top:1px solid rgba(255,255,255,.08); }
.sv-asis-form input { flex:1; min-width:0; background:rgba(255,255,255,.06); border:1px solid rgba(255,255,255,.14); color:#f8fafc; border-radius:12px; padding:10px 12px; font-size:16px; font-family:inherit; }
.sv-asis-form button { background:#0ea5e9; color:#fff; border:none; border-radius:12px; padding:0 14px; cursor:pointer; }
.sv-resalte { outline:3px solid #fbbf24 !important; outline-offset:3px; border-radius:12px; animation:svPulso 1s ease-in-out 6; }
@keyframes svPulso { 0%,100% { box-shadow:0 0 0 0 rgba(251,191,36,.7); } 50% { box-shadow:0 0 0 10px rgba(251,191,36,0); } }
@media (max-width:600px) { .sv-asis-panel { right:8px; left:8px; width:auto; bottom:80px; height:calc(100vh - 100px); height:calc(100dvh - 100px); } }
@media print { .sv-asis-burbuja, .sv-asis-panel { display:none !important; } }

/* Visor de PDF interno */
.sv-pdf-ov { position:fixed; inset:0; z-index:10001; background:rgba(2,6,23,.78); display:flex; align-items:center; justify-content:center; padding:16px; backdrop-filter:blur(4px); }
.sv-pdf-caja { width:min(1000px,100%); height:min(92vh,900px); background:#0f172a; border:1px solid rgba(255,255,255,.14); border-radius:16px; display:flex; flex-direction:column; overflow:hidden; box-shadow:0 24px 60px rgba(0,0,0,.6); }
.sv-pdf-cab { display:flex; align-items:center; justify-content:space-between; gap:10px; padding:10px 14px; background:linear-gradient(135deg,#0284c7,#2563eb); color:#fff; }
.sv-pdf-t { font-weight:700; font-size:.95rem; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.sv-pdf-acc { display:flex; align-items:center; gap:8px; flex:0 0 auto; }
.sv-pdf-btn { background:rgba(255,255,255,.18); color:#fff; text-decoration:none; padding:7px 12px; border-radius:10px; font-size:.82rem; font-weight:600; }
.sv-pdf-x { background:rgba(255,255,255,.18); color:#fff; border:none; border-radius:10px; width:36px; height:34px; font-size:1.5rem; line-height:1; cursor:pointer; padding:0; }
.sv-pdf-cuerpo { position:relative; flex:1; background:#1e293b; }
.sv-pdf-cuerpo iframe { position:absolute; inset:0; width:100%; height:100%; border:0; background:#fff; }
.sv-pdf-carga { position:absolute; inset:0; display:flex; align-items:center; justify-content:center; color:#94a3b8; font-size:.9rem; }
.sv-pdf-pie { padding:6px 12px; font-size:.74rem; color:#94a3b8; text-align:center; background:#0f172a; }
@media (max-width:600px) { .sv-pdf-ov { padding:0; } .sv-pdf-caja { height:100vh; height:100dvh; border-radius:0; } }
@media print { .sv-pdf-ov { display:none !important; } }

/* ---------- Botones táctiles en iPhone / móviles (v6.4) ---------- */
.btn-asistencia, .btn-submit, .btn-save, .sv-btn, a.btn-table, a[data-pdf], .kpi-click, .level-bubble {
    min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
a[data-pdf] { padding: 0 10px; }
@media (max-width: 700px) {
    body { padding-bottom: calc(96px + env(safe-area-inset-bottom)) !important; }   /* deja libre la burbuja del asistente */
    .btn-asistencia, .btn-submit, .btn-save { width: 100%; }
    .tab-btn { min-height: 48px; padding-left: 16px; padding-right: 16px; }
    .kpi-card { min-height: 64px; }
    details > summary { min-height: 44px; display: flex; align-items: center; }
    .nav-buttons .btn-action { flex: 1 1 auto; justify-content: center; }
}


/* ===================== iPhone / iPad (Safari) =====================
   Causas de "cosas que no se mueven": alto fijo 100vh (en iOS es MÁS alto que la pantalla visible), pantallas con scroll
   interno anidado, zoom automático al tocar campos de texto y barras laterales. En pantallas táctiles la página
   se desplaza completa, de forma natural. */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
a, button, label, select, summary, .tab-btn, .btn-action, .btn-table { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
@media (max-width: 900px), (pointer: coarse) {
    html, body { height: auto !important; min-height: 100vh; min-height: 100dvh; overflow-y: auto !important; overflow-x: hidden; overflow-x: clip; position: static !important; -webkit-overflow-scrolling: touch; overscroll-behavior-y: auto; }
    body { padding-left: max(12px, env(safe-area-inset-left)) !important; padding-right: max(12px, env(safe-area-inset-right)) !important; padding-bottom: max(16px, env(safe-area-inset-bottom)) !important; }
    .bubbles-background { height: 100vh; height: 100dvh; pointer-events: none !important; }
    .bubble { pointer-events: none !important; }
    /* Sin contenedores con alto fijo ni scroll anidado: lo maneja la página */
    .tab-content, .tab-content.active { flex: none !important; min-height: 0 !important; height: auto !important; max-height: none !important; overflow: visible !important; }
    .content-card { max-height: none !important; overflow: visible !important; flex: none !important; }
    .table-responsive { flex: none !important; max-height: none !important; overflow-y: visible !important; overflow-x: auto !important; -webkit-overflow-scrolling: touch; }
    .support-tabs, .admin-tabs, .sv-tabs { overflow-x: auto !important; -webkit-overflow-scrolling: touch; scroll-snap-type: x proximity; flex-wrap: nowrap !important; }
    .tab-btn { flex: 0 0 auto; scroll-snap-align: start; white-space: nowrap; }
    /* Evita el zoom automático de iOS al enfocar un campo (exige 16 px) */
    input:not([type=checkbox]):not([type=radio]):not([type=range]):not([type=file]), select, textarea { font-size: 16px !important; max-width: 100%; }
    input[type=date], input[type=time], input[type=datetime-local], input[type=month] { min-width: 0; max-width: 100%; -webkit-appearance: none; appearance: none; min-height: 40px; }
    select { -webkit-appearance: menulist-button; }
    img, video, canvas, svg { max-width: 100%; height: auto; }
    pre, code { white-space: pre-wrap; word-break: break-word; }
    table { max-width: 100%; }
    .form-grid, .frm { grid-template-columns: 1fr !important; }
    .btn-action, .btn-submit, .btn-table, .tab-btn { min-height: 40px; }
    .sv-asis-burbuja { bottom: max(18px, env(safe-area-inset-bottom)); right: max(18px, env(safe-area-inset-right)); }
    #vvPill { bottom: max(14px, env(safe-area-inset-bottom)); }
    #vvToasts { bottom: calc(max(14px, env(safe-area-inset-bottom)) + 40px); }
}
@supports (-webkit-touch-callout: none) {
    /* Solo iOS: el alto "visible" real */
    .sv-asis-panel { max-height: calc(100dvh - 110px); }
    .sv-pdf-caja { height: min(92dvh, 900px); }
}

/* Botón del asistente e indicador "En vivo" flotan sobre la página: se deja espacio al final para que no tapen botones ni texto */
body { padding-bottom: max(88px, env(safe-area-inset-bottom)) !important; }
.tab-content::after { content: ''; display: block; flex: 0 0 72px; height: 72px; }
