/* PPV FacturaTracker — hoja de estilo
   Sale del index.html para que el navegador la guarde: antes viajaban
   43 KB de CSS en cada carga porque la pagina se genera cada vez. */

    :root {
        --bg: #f1f5f9;
        --surface: #ffffff;
        --surface-2: #f8fafc;
        --border: #e2e8f0;
        --border-light: #f1f5f9;
        --text: #0f172a;
        --text-2: #334155;
        --text-3: #64748b;
        --text-dim: #94a3b8;
        --primary: #2563eb;
        --primary-light: #3b82f6;
        --primary-bg: #eff6ff;
        --primary-border: #bfdbfe;
        --green: #16a34a;
        --green-bg: #f0fdf4;
        --green-border: #bbf7d0;
        --amber: #d97706;
        --amber-bg: #fffbeb;
        --amber-border: #fde68a;
        --orange: #ea580c;
        --orange-bg: #fff7ed;
        --orange-border: #fed7aa;
        --red: #dc2626;
        --red-bg: #fef2f2;
        --red-border: #fecaca;
        --sidebar-bg: #0f172a;
        --sidebar-text: #94a3b8;
        --sidebar-active: #1e293b;
        --radius: 10px;
        --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
        --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
        --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
        --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.12), 0 4px 6px -4px rgb(0 0 0 / 0.1);

        /* --- Nombres alternos que ya se usaban en el CSS y en los estilos en
           linea, pero que nunca se habian definido. El navegador no avisa: la
           declaracion se descarta y el elemento se queda sin color. Por eso el
           dialogo de confirmar salia con el fondo transparente y el buscador
           con el texto del color heredado. --- */
        --bg-card: var(--surface);
        --bg-2: var(--surface-2);
        --bg-hover: var(--surface-2);
        --text-primary: var(--text);
        --text-secondary: var(--text-3);

        /* --- Margenes seguros del iPhone ---
           En la app instalada en la pantalla de inicio, con viewport-fit=cover,
           la barra de estado y la Dynamic Island se comen la franja de arriba.
           Todo lo que va pegado al borde tiene que respetarla o queda debajo.
           Se pasa por variable para poder simular el notch en las pruebas. */
        --safe-top: env(safe-area-inset-top, 0px);
        --safe-bottom: env(safe-area-inset-bottom, 0px);
        --safe-left: env(safe-area-inset-left, 0px);
        --safe-right: env(safe-area-inset-right, 0px);
    }
    * { margin: 0; padding: 0; box-sizing: border-box; }
    body {
        font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
        background: var(--bg); color: var(--text);
        min-height: 100vh; min-height: 100dvh;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    /* ======================== */
    /* SPLASH SCREEN            */
    /* ======================== */
    .splash-screen {
        position: fixed; inset: 0; z-index: 9999;
        display: flex; flex-direction: column;
        align-items: center; justify-content: center;
        background: var(--sidebar-bg);
        transition: opacity .4s ease, visibility .4s ease;
    }
    .splash-screen.fade-out {
        opacity: 0; visibility: hidden;
    }
    .splash-logo {
        width: 120px; height: 120px; border-radius: 28px;
        box-shadow: 0 20px 60px rgba(0,0,0,0.3);
        animation: splashPulse 2s ease-in-out infinite;
    }
    .splash-title {
        margin-top: 24px; font-size: 32px; font-weight: 800;
        color: #fff; letter-spacing: -1px;
    }
    .splash-sub {
        margin-top: 6px; font-size: 14px; color: rgba(255,255,255,0.5);
        letter-spacing: 0.5px;
    }
    .splash-dots {
        display: flex; gap: 8px; margin-top: 40px;
    }
    .splash-dots span {
        width: 8px; height: 8px; border-radius: 50%;
        background: rgba(255,255,255,0.3);
        animation: splashDot 1.4s ease-in-out infinite;
    }
    .splash-dots span:nth-child(2) { animation-delay: .2s; }
    .splash-dots span:nth-child(3) { animation-delay: .4s; }
    @keyframes splashPulse {
        0%, 100% { transform: scale(1); }
        50% { transform: scale(1.04); }
    }
    @keyframes splashDot {
        0%, 80%, 100% { opacity: .3; transform: scale(.8); }
        40% { opacity: 1; transform: scale(1.1); }
    }

    /* ======================== */
    /* LOGIN                    */
    /* ======================== */
    .login-screen {
        display: flex; align-items: center; justify-content: center;
        min-height: 100vh; min-height: 100dvh; padding: 24px;
        background: var(--sidebar-bg);
        position: relative; overflow: hidden;
    }
    .login-screen::before {
        content: ''; position: absolute; width: 600px; height: 600px;
        background: radial-gradient(circle, rgba(37,99,235,0.15) 0%, transparent 70%);
        top: -200px; right: -100px; border-radius: 50%;
    }
    .login-screen::after {
        content: ''; position: absolute; width: 400px; height: 400px;
        background: radial-gradient(circle, rgba(16,163,74,0.1) 0%, transparent 70%);
        bottom: -100px; left: -50px; border-radius: 50%;
    }
    .login-card, .setup-card {
        background: var(--surface); border-radius: 16px;
        padding: 40px 36px; width: 100%; max-width: 400px;
        box-shadow: 0 25px 50px -12px rgba(0,0,0,0.4);
        position: relative; z-index: 1;
    }
    .login-brand {
        display: flex; align-items: center; gap: 12px;
        margin-bottom: 32px;
    }
    .login-brand-icon {
        width: 44px; height: 44px; border-radius: 12px;
        background: var(--primary); display: grid; place-items: center;
        color: #fff; flex-shrink: 0;
    }
    .login-brand h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.5px; }
    .login-brand p { font-size: 13px; color: var(--text-3); margin-top: 2px; }
    .setup-card h2 { font-size: 20px; font-weight: 700; margin-bottom: 4px; }
    .setup-card > p { color: var(--text-3); font-size: 13px; margin-bottom: 24px; }

    /* ======================== */
    /* APP LAYOUT               */
    /* ======================== */
    .app-layout {
        display: flex; min-height: 100vh; min-height: 100dvh;
    }

    /* Sidebar (desktop) */
    .sidebar {
        width: 240px; background: var(--sidebar-bg);
        display: none; flex-direction: column;
        position: fixed; top: 0; bottom: 0; left: 0; z-index: 50;
        border-right: 1px solid rgba(255,255,255,0.06);
    }
    .sidebar-brand {
        padding: 20px 20px 24px; display: flex; align-items: center; gap: 12px;
        border-bottom: 1px solid rgba(255,255,255,0.06);
    }
    .sidebar-brand-icon {
        width: 36px; height: 36px; border-radius: 10px;
        background: var(--primary); display: grid; place-items: center;
        color: #fff; flex-shrink: 0;
    }
    .sidebar-brand h2 { font-size: 15px; font-weight: 700; color: #fff; }
    .sidebar-brand p { font-size: 11px; color: var(--sidebar-text); margin-top: 1px; }
    .sidebar-nav { padding: 12px; flex: 1; }
    .sidebar-link {
        display: flex; align-items: center; gap: 10px;
        padding: 10px 12px; border-radius: 8px; border: none;
        background: transparent; color: var(--sidebar-text);
        font-family: inherit; font-size: 13px; font-weight: 500;
        cursor: pointer; transition: all 0.15s; width: 100%;
        text-align: left; margin-bottom: 2px;
    }
    .sidebar-link:hover { background: rgba(255,255,255,0.06); color: #e2e8f0; }
    .sidebar-link.active {
        background: var(--primary); color: #fff;
        box-shadow: 0 1px 3px rgba(37,99,235,0.4);
    }
    .sidebar-link i { width: 18px; height: 18px; stroke-width: 1.75; }
    .sidebar-footer {
        padding: 16px 20px; border-top: 1px solid rgba(255,255,255,0.06);
    }
    .sidebar-user {
        display: flex; align-items: center; justify-content: space-between;
    }
    .sidebar-user-info {
        display: flex; align-items: center; gap: 10px;
    }
    .sidebar-avatar {
        width: 32px; height: 32px; border-radius: 8px;
        background: var(--sidebar-active); display: grid; place-items: center;
        color: var(--sidebar-text); font-weight: 600; font-size: 12px;
    }
    .sidebar-user-name { font-size: 13px; color: #e2e8f0; font-weight: 500; }
    .sidebar-user-role { font-size: 11px; color: var(--sidebar-text); }
    .sidebar-logout {
        background: none; border: none; color: var(--sidebar-text);
        cursor: pointer; padding: 6px; border-radius: 6px; transition: all 0.15s;
    }
    .sidebar-logout:hover { background: rgba(220,38,38,0.15); color: #f87171; }

    .app-version-badge {
        position: fixed;
        right: 12px;
        bottom: 12px;
        z-index: 60;
        padding: 4px 8px;
        border-radius: 999px;
        background: rgba(15, 23, 42, 0.88);
        color: rgba(255, 255, 255, 0.78);
        font-size: 10px;
        font-weight: 700;
        letter-spacing: 0.06em;
        line-height: 1;
        box-shadow: 0 6px 18px rgba(15, 23, 42, 0.2);
        pointer-events: none;
        user-select: none;
    }
    @media (max-width: 640px) {
        .app-version-badge {
            right: 10px;
            bottom: 10px;
            font-size: 9px;
        }
    }

    /* Main area */
    .main-area {
        flex: 1; display: flex; flex-direction: column;
        /* Un hijo flex trae min-width:auto y se niega a encogerse por
           debajo del ancho de su contenido. Sin esto el area principal se
           queda en ~530px dentro de una pantalla de 390 y toda la app se
           desborda de lado en telefono. */
        min-width: 0;
    }

    /* Mobile Header */
    .mobile-header {
        display: flex; align-items: center; justify-content: space-between;
        padding: 12px 16px; background: var(--sidebar-bg);
        /* La franja de la Dynamic Island se suma arriba, no se resta al alto. */
        padding-top: calc(12px + var(--safe-top));
        padding-left: calc(16px + var(--safe-left));
        padding-right: calc(16px + var(--safe-right));
    }
    .mobile-header-brand {
        display: flex; align-items: center; gap: 10px;
    }
    .mobile-header-icon {
        width: 32px; height: 32px; border-radius: 8px;
        background: var(--primary); display: grid; place-items: center;
        color: #fff;
    }
    .mobile-header h1 { font-size: 16px; font-weight: 700; color: #fff; }
    .mobile-header-user {
        display: flex; align-items: center; gap: 8px;
    }
    .mobile-header-name {
        font-size: 12px; color: var(--sidebar-text); font-weight: 500;
        background: rgba(255,255,255,0.06); padding: 4px 10px;
        border-radius: 6px;
    }
    .mobile-header-logout {
        background: none; border: none; color: var(--sidebar-text);
        cursor: pointer; padding: 4px; border-radius: 6px;
    }

    /* Page header */
    .page-header {
        padding: 20px 4px 0;
    }
    .page-title {
        font-size: 22px; font-weight: 700; letter-spacing: -0.5px;
        color: var(--text);
    }
    .page-subtitle {
        font-size: 13px; color: var(--text-3); margin-top: 2px;
    }

    /* Content */
    .content {
        flex: 1; padding: 16px; padding-bottom: 80px;
        max-width: 960px; width: 100%;
    }

    /* Panels */
    .panel { display: none; }
    .panel.active { display: block; }

    /* ======================== */
    /* STAT CARDS               */
    /* ======================== */
    .stats {
        display: grid; grid-template-columns: repeat(2, 1fr);
        gap: 12px; margin-bottom: 20px;
    }
    .stat-card {
        background: var(--surface); border: 1px solid var(--border);
        border-radius: var(--radius); padding: 16px;
        transition: box-shadow 0.2s;
    }
    .stat-card:hover { box-shadow: var(--shadow-md); }
    .stat-top {
        display: flex; align-items: center; justify-content: space-between;
        margin-bottom: 10px;
    }
    .stat-icon-wrap {
        width: 36px; height: 36px; border-radius: 8px;
        display: grid; place-items: center;
    }
    .stat-icon-wrap i { width: 18px; height: 18px; stroke-width: 1.75; }
    .stat-icon-blue { background: var(--primary-bg); color: var(--primary); }
    .stat-icon-amber { background: var(--amber-bg); color: var(--amber); }
    .stat-icon-orange { background: var(--orange-bg); color: var(--orange); }
    .stat-icon-green { background: var(--green-bg); color: var(--green); }
    .stat-icon-red { background: var(--red-bg); color: var(--red); }
    .stat-value {
        font-size: 26px; font-weight: 700; line-height: 1;
        font-variant-numeric: tabular-nums;
        letter-spacing: -0.5px;
    }
    .stat-label {
        font-size: 12px; color: var(--text-3); margin-top: 4px;
        font-weight: 500;
    }

    /* ======================== */
    /* CARDS                    */
    /* ======================== */
    .card {
        background: var(--surface); border: 1px solid var(--border);
        border-radius: var(--radius); overflow: hidden; margin-bottom: 16px;
    }
    .card-header {
        padding: 14px 16px; display: flex; justify-content: space-between;
        align-items: center; border-bottom: 1px solid var(--border);
    }
    .card-title {
        font-size: 14px; font-weight: 600; color: var(--text);
    }

    /* ======================== */
    /* TABLE                    */
    /* ======================== */
    table { width: 100%; border-collapse: collapse; }
    thead th {
        padding: 10px 14px; text-align: left; font-size: 11px;
        text-transform: uppercase; letter-spacing: 0.5px;
        color: var(--text-dim); font-weight: 600;
        border-bottom: 1px solid var(--border);
        background: var(--surface-2);
    }
    tbody td {
        padding: 12px 14px; font-size: 14px;
        border-bottom: 1px solid var(--border-light);
        vertical-align: middle;
    }
    tbody tr:last-child td { border-bottom: none; }
    tbody tr:hover { background: var(--surface-2); }
    tbody tr.tr-selected { background: var(--primary-bg) !important; box-shadow: inset 3px 0 0 var(--primary); }
    tbody tr.tr-selected td { border-bottom-color: var(--primary-border); }
    .mono { font-variant-numeric: tabular-nums; font-weight: 600; }
    .table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    @media (max-width: 639px) {
        thead th { padding: 8px 6px; font-size: 11px; }
        tbody td { padding: 8px 6px; font-size: 12px; }
        /* iOS HIG: min 44px touch targets */
        .btn-sm { padding: 6px 8px; font-size: 11px; min-height: 44px; min-width: 44px; }
        .btn-sm .btn-label { display: none; }
        .hide-mobile-sm { display: none; }
        .td-actions { padding: 4px 2px !important; white-space: nowrap; }
        /* Prevent wide tables from overflowing on small screens */
        .table-scroll { margin: 0 -2px; }
        .table-scroll table { min-width: 500px; }
    }

    /* ======================== */
    /* BADGES                   */
    /* ======================== */
    .badge {
        display: inline-flex; align-items: center; gap: 5px;
        padding: 3px 10px; border-radius: 6px;
        font-size: 11px; font-weight: 600;
        letter-spacing: 0.2px;
    }
    .badge::before {
        content: ''; width: 6px; height: 6px;
        border-radius: 50%; flex-shrink: 0;
    }
    .badge-creada, .badge-pendiente {
        background: var(--amber-bg); color: var(--amber);
        border: 1px solid var(--amber-border);
    }
    .badge-creada::before, .badge-pendiente::before { background: var(--amber); }
    .badge-en_proceso {
        background: var(--orange-bg); color: var(--orange);
        border: 1px solid var(--orange-border);
    }
    .badge-en_proceso::before { background: var(--orange); }
    .badge-entregada, .badge-completada {
        background: var(--green-bg); color: var(--green);
        border: 1px solid var(--green-border);
    }
    .badge-entregada::before, .badge-completada::before { background: var(--green); }
    .badge-pagada {
        background: var(--primary-bg); color: var(--primary);
        border: 1px solid var(--primary-border);
    }
    .badge-pagada::before { background: var(--primary); }
    .badge-cancelada, .badge-anulada {
        background: var(--red-bg); color: var(--red);
        border: 1px solid var(--red-border);
    }
    .badge-cancelada::before, .badge-anulada::before { background: var(--red); }

    /* ======================== */
    /* BUTTONS                  */
    /* ======================== */
    .btn {
        padding: 9px 16px; border-radius: 8px; border: none;
        font-family: inherit; font-size: 13px; font-weight: 600;
        cursor: pointer; transition: all 0.15s;
        display: inline-flex; align-items: center; gap: 6px;
        -webkit-tap-highlight-color: transparent;
    }
    .btn:active { transform: scale(0.97); }
    .btn-primary {
        background: var(--primary); color: #fff;
        box-shadow: 0 1px 2px rgba(37,99,235,0.3);
    }
    .btn-primary:hover { background: var(--primary-light); }
    .btn-green {
        background: var(--green); color: #fff;
        box-shadow: 0 1px 2px rgba(16,163,74,0.3);
    }
    .btn-green:hover { background: #15803d; }
    .btn-ghost {
        background: var(--surface); color: var(--text-2);
        border: 1px solid var(--border);
    }
    .btn-ghost:hover { background: var(--surface-2); border-color: var(--text-dim); }
    .btn-danger {
        background: var(--red); color: #fff;
        box-shadow: 0 1px 2px rgba(220,38,38,0.3);
    }
    .btn-sm { padding: 6px 12px; font-size: 12px; border-radius: 6px; }
    .btn-block { width: 100%; justify-content: center; }

    /* ======================== */
    /* FORMS                    */
    /* ======================== */
    .form-card {
        background: var(--surface); border: 1px solid var(--border);
        border-radius: var(--radius); padding: 20px; margin-bottom: 16px;
        display: none;
    }
    .form-card.show { display: block; }
    .form-card h3 {
        font-size: 16px; font-weight: 700; margin-bottom: 16px;
        display: flex; align-items: center; gap: 8px;
    }
    .form-grid { display: grid; grid-template-columns: 1fr; gap: 14px; }
    /* min-width:0 porque .fg es hijo de .form-grid: sin esto se niega a
       encogerse bajo el ancho natural de su input y el segundo campo se
       sale del modal. */
    .fg { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
    .fg label {
        font-size: 13px; color: var(--text-2); font-weight: 500;
    }
    .fg.required label::after {
        content: ' *'; color: var(--red); font-weight: 700;
    }
    .fg input, .fg select, .fg textarea {
        padding: 10px 12px; border-radius: 8px;
        border: 1px solid var(--border);
        background: var(--surface); color: var(--text);
        font-family: inherit; font-size: 16px; outline: none;
        transition: border 0.15s, box-shadow 0.15s;
        -webkit-appearance: none;
        /* Dentro de .form-grid de dos columnas, un input trae min-width:auto
           y se niega a encogerse: el segundo campo se sale del modal. */
        min-width: 0;
    }
    .fg input:focus, .fg select:focus, .fg textarea:focus {
        border-color: var(--primary);
        box-shadow: 0 0 0 3px var(--primary-bg);
    }
    .fg textarea { resize: vertical; min-height: 60px; }
    .form-actions { display: flex; gap: 8px; margin-top: 16px; }

    /* ======================== */
    /* STOCK BAR                */
    /* ======================== */
    .stock-bar {
        background: var(--border-light); border-radius: 4px;
        height: 4px; margin-top: 6px; overflow: hidden;
    }
    .stock-bar-fill {
        height: 100%; border-radius: 4px; transition: width 0.4s;
    }

    /* ======================== */
    /* UPLOAD ZONE              */
    /* ======================== */
    .upload-zone {
        border: 2px dashed var(--border); border-radius: var(--radius);
        padding: 28px; text-align: center; cursor: pointer;
        transition: all 0.15s; position: relative;
        font-size: 13px; color: var(--text-3);
    }
    .upload-zone:hover { border-color: var(--primary); background: var(--primary-bg); }
    .upload-zone input {
        position: absolute; inset: 0; opacity: 0; cursor: pointer;
    }
    .upload-icon-wrap {
        width: 48px; height: 48px; border-radius: 12px;
        background: var(--primary-bg); color: var(--primary);
        display: grid; place-items: center; margin: 0 auto 10px;
    }

    /* ======================== */
    /* MODAL                    */
    /* ======================== */
    .modal-overlay {
        display: none; position: fixed; inset: 0;
        background: rgba(15,23,42,0.6); backdrop-filter: blur(4px);
        z-index: 200;
        padding: calc(16px + var(--safe-top)) 16px 0;
        align-items: flex-end; justify-content: center;
    }
    .modal-overlay.show { display: flex; }
    .modal {
        background: var(--surface); border-radius: 16px 16px 0 0;
        padding: 24px 24px calc(24px + var(--safe-bottom));
        width: 100%; max-width: 440px;
        max-height: calc(85vh - var(--safe-bottom));
        overflow-y: auto;
        box-shadow: 0 -8px 30px rgba(0,0,0,0.12);
    }
    .modal h3 {
        font-size: 17px; font-weight: 700; margin-bottom: 16px;
    }

    /* ======================== */
    /* SEARCH OVERLAY           */
    /* ======================== */
    .search-overlay {
        display: none; position: fixed; inset: 0;
        background: rgba(15,23,42,0.7); backdrop-filter: blur(6px);
        z-index: 250; justify-content: center; align-items: flex-start;
        /* El buscador se abre pegado arriba: sin este margen la barra de
           escribir queda debajo de la Dynamic Island. */
        padding: calc(16px + var(--safe-top)) calc(16px + var(--safe-right))
                 calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
    }
    .search-overlay.show { display: flex; }
    .search-box {
        background: var(--surface); border-radius: 16px;
        width: 100%; max-width: 480px; margin-top: 10vh;
        box-shadow: 0 12px 40px rgba(0,0,0,0.18);
        overflow: hidden; max-height: 75vh; display: flex; flex-direction: column;
    }
    .search-bar {
        display: flex; align-items: center; gap: 10px;
        padding: 14px 16px; border-bottom: 1px solid var(--border);
    }
    .search-bar i { color: var(--text-secondary); flex-shrink: 0; }
    .search-bar input {
        flex: 1; border: none; outline: none; font-size: 16px;
        background: transparent; color: var(--text-primary);
        font-family: inherit;
    }
    .search-bar input::placeholder { color: var(--text-secondary); }
    .search-results {
        overflow-y: auto; padding: 8px; flex: 1;
    }
    .search-empty {
        text-align: center; padding: 32px 16px;
        color: var(--text-secondary); font-size: 14px;
    }
    .search-group-title {
        font-size: 11px; font-weight: 700; text-transform: uppercase;
        letter-spacing: 0.5px; color: var(--text-secondary);
        padding: 10px 8px 4px; display: flex; align-items: center; gap: 6px;
    }
    .search-item {
        display: flex; align-items: center; gap: 10px;
        padding: 10px 12px; border-radius: 10px; cursor: pointer;
        transition: background .15s;
    }
    .search-item:hover, .search-item:active { background: var(--bg); }
    .search-item-icon {
        width: 36px; height: 36px; border-radius: 10px;
        display: flex; align-items: center; justify-content: center;
        flex-shrink: 0;
    }
    .search-item-icon.facturas { background: rgba(16,185,129,0.12); color: #10b981; }
    .search-item-icon.ordenes { background: rgba(59,130,246,0.12); color: #3b82f6; }
    .search-item-icon.productos { background: rgba(168,85,247,0.12); color: #a855f7; }
    .search-item-icon.clientes { background: rgba(245,158,11,0.12); color: #f59e0b; }
    .search-item-text { flex: 1; min-width: 0; }
    .search-item-title {
        font-size: 14px; font-weight: 600; color: var(--text-primary);
        white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
    .search-item-sub {
        font-size: 12px; color: var(--text-secondary);
        white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
    .search-item-badge {
        font-size: 11px; font-weight: 600; padding: 2px 8px;
        border-radius: 6px; flex-shrink: 0;
    }
    @keyframes search-flash {
        0% { background: rgba(250,204,21,0.45); }
        100% { background: transparent; }
    }
    .search-highlight {
        animation: search-flash 2.5s ease-out;
    }
    @media(max-width:640px) {
        .search-box { margin-top: 2vh; max-height: 82vh; border-radius: 14px; }
        .search-bar input { font-size: 16px; } /* prevents iOS zoom */
    }

    /* ======================== */
    /* PREVIEW MODAL            */
    /* ======================== */
    .preview-overlay {
        display: none; position: fixed; inset: 0;
        background: #000; z-index: 300;
        flex-direction: column;
    }
    .preview-overlay.show { display: flex; }
    .preview-toolbar {
        display: flex; align-items: center; justify-content: space-between;
        padding: 10px 16px; background: #111; color: #fff;
        flex-shrink: 0;
        padding-top: calc(10px + var(--safe-top));
        padding-left: calc(16px + var(--safe-left));
        padding-right: calc(16px + var(--safe-right));
    }
    .preview-toolbar h4 { font-size: 15px; font-weight: 600; margin: 0; }
    .preview-toolbar-btns { display: flex; gap: 8px; }
    .preview-toolbar-btns button {
        background: rgba(255,255,255,0.15); border: none; color: #fff;
        padding: 8px 14px; border-radius: 8px; font-size: 13px;
        cursor: pointer; display: flex; align-items: center; gap: 5px;
    }
    .preview-toolbar-btns button:hover { background: rgba(255,255,255,0.25); }
    .preview-iframe {
        flex: 1; width: 100%; border: none; background: #fff;
    }

    /* ======================== */
    /* CONFIRM DIALOG           */
    /* ======================== */
    .confirm-overlay {
        position: fixed; inset: 0; background: rgba(0,0,0,0.45);
        z-index: 400; display: none; align-items: center; justify-content: center;
    }
    .confirm-overlay.open { display: flex; }
    .confirm-box {
        background: var(--bg-card); border-radius: var(--radius);
        padding: 24px; max-width: 340px; width: 90%;
        box-shadow: var(--shadow-lg); text-align: center;
    }
    .confirm-box p { margin: 0 0 20px; font-size: 14px; color: var(--text-primary); line-height: 1.5; }
    .confirm-btns { display: flex; gap: 10px; justify-content: center; }
    .confirm-btns button { flex: 1; padding: 10px 0; border-radius: var(--radius); font-weight: 600; font-size: 13px; cursor: pointer; border: none; }
    .confirm-cancel { background: var(--bg-hover); color: var(--text-secondary); }
    .confirm-ok { background: var(--red); color: #fff; }

    /* ======================== */
    /* TOAST                    */
    /* ======================== */
    .toast {
        position: fixed;
        top: calc(16px + var(--safe-top));
        right: calc(16px + var(--safe-right));
        transform: translateX(120%);
        padding: 12px 20px; border-radius: var(--radius);
        font-size: 13px; font-weight: 600; z-index: 300;
        opacity: 0; transition: all 0.3s cubic-bezier(0.16,1,0.3,1);
        display: flex; align-items: center; gap: 8px;
        box-shadow: var(--shadow-md);
        max-width: 360px;
    }
    .toast.show { transform: translateX(0); opacity: 1; }
    .toast-success {
        background: var(--green-bg); color: var(--green);
        border: 1px solid var(--green-border);
    }
    .toast-error {
        background: var(--red-bg); color: var(--red);
        border: 1px solid var(--red-border);
    }

    /* ======================== */
    /* EMPTY STATE              */
    /* ======================== */
    .empty {
        text-align: center; padding: 48px 20px;
        color: var(--text-dim);
    }
    .empty-icon-wrap {
        width: 56px; height: 56px; border-radius: 14px;
        background: var(--bg); display: grid; place-items: center;
        margin: 0 auto 14px; color: var(--text-dim);
    }
    .empty h4 { color: var(--text-2); font-size: 15px; font-weight: 600; }
    .empty p { font-size: 13px; margin-top: 4px; }

    /* ======================== */
    /* BOTTOM NAV (Mobile)      */
    /* ======================== */
    .bottom-nav {
        display: flex; position: fixed; bottom: 0; left: 0; right: 0;
        background: var(--surface);
        border-top: 1px solid var(--border);
        z-index: 100; padding: 4px 4px;
        padding-bottom: max(4px, var(--safe-bottom));
    }
    .nav-btn {
        flex: 1; display: flex; flex-direction: column; align-items: center;
        gap: 2px; padding: 8px 4px; border-radius: 8px; border: none;
        background: transparent; color: var(--text-dim);
        font-family: inherit; font-size: 10px; font-weight: 600;
        cursor: pointer; transition: all 0.15s;
    }
    .nav-btn i { width: 20px; height: 20px; stroke-width: 1.75; }
    .nav-btn.active {
        color: var(--primary); background: var(--primary-bg);
    }

    /* ======================== */
    /* RESPONSIVE               */
    /* ======================== */
    .hide-mobile { display: none; }
    @media (min-width: 640px) {
        .stats { grid-template-columns: repeat(4, 1fr); }
        .form-grid { grid-template-columns: 1fr 1fr; }
        .modal { border-radius: 16px; align-self: center; }
        .modal-overlay { align-items: center; }
        .hide-mobile { display: table-cell; }
    }
    @media (min-width: 860px) {
        .sidebar { display: flex; }
        .main-area { margin-left: 240px; }
        .mobile-header { display: none; }
        .bottom-nav { display: none; }
        .content { padding: 0 24px 24px; padding-bottom: 24px; }
    }

    /* ======================== */
    /* PULL TO REFRESH (mobile) */
    /* ======================== */
    .ptr-indicator {
        display: none; text-align: center; padding: 12px 0 4px; color: var(--text-3); font-size: 12px;
        transition: opacity 0.3s;
    }
    .ptr-indicator.visible { display: block; }
    .ptr-indicator .ptr-spinner {
        display: inline-block; width: 20px; height: 20px;
        border: 2px solid var(--border); border-top-color: var(--primary);
        border-radius: 50%; animation: spin 0.6s linear infinite;
    }
    @keyframes spin { to { transform: rotate(360deg); } }

    /* ======================== */
    /* AI CHAT                  */
    /* ======================== */
    .chat-fab {
        position: fixed; bottom: 72px; right: 14px; z-index: 150;
        width: 46px; height: 46px; border-radius: 50%;
        background: transparent; border: none;
        box-shadow: 0 2px 10px rgba(37,99,235,0.35);
        cursor: pointer; display: grid; place-items: center;
        transition: transform 0.2s, box-shadow 0.2s, opacity 0.2s;
        padding: 0; opacity: 0.85;
    }
    .chat-fab img { width: 46px; height: 46px; border-radius: 50%; object-fit: cover; display: block; }
    .chat-fab:hover { transform: scale(1.08); box-shadow: 0 4px 18px rgba(37,99,235,0.6); opacity: 1; }
    .chat-fab:active { transform: scale(0.95); }
    .chat-fab i { width: 20px; height: 20px; stroke-width: 1.75; }
    @media (min-width: 860px) {
        .chat-fab { bottom: 24px; right: 24px; width: 56px; height: 56px; opacity: 1; }
        .chat-fab img { width: 56px; height: 56px; }
    }

    .chat-panel {
        display: none; position: fixed; z-index: 160;
        bottom: 0; right: 0; left: 0; top: 0;
        background: var(--surface);
        flex-direction: column;
    }
    .chat-panel.open { display: flex; }
    @media (min-width: 640px) {
        .chat-panel {
            bottom: 80px; right: 18px; left: auto; top: auto;
            width: 500px; height: 680px;
            border-radius: 16px; border: 1px solid var(--border);
            box-shadow: 0 12px 40px rgba(0,0,0,0.15);
        }
    }
    @media (min-width: 860px) {
        .chat-panel { bottom: 84px; right: 24px; width: 540px; height: 720px; }
    }

    .chat-header {
        padding: 14px 16px; display: flex; align-items: center; justify-content: space-between;
        border-bottom: 1px solid var(--border); background: var(--sidebar-bg);
        border-radius: 16px 16px 0 0; color: #fff;
        flex-shrink: 0;
    }
    /* En el telefono el chat ocupa toda la pantalla, asi que su cabecera queda
       pegada al borde de arriba: sin este margen, la Dynamic Island tapaba
       justo la X de cerrar y no habia como salir del chat. */
    @media (max-width: 639px) {
        .chat-header {
            border-radius: 0;
            padding-top: calc(14px + var(--safe-top));
            padding-left: calc(16px + var(--safe-left));
            padding-right: calc(16px + var(--safe-right));
        }
    }
    .chat-header-left { display: flex; align-items: center; gap: 10px; min-width: 0; overflow: hidden; }
    .chat-header-icon {
        width: 52px; height: 52px; border-radius: 50%;
        background: transparent; display: grid; place-items: center;
        overflow: hidden;
        box-shadow: 0 0 0 2px #2563eb, 0 0 12px rgba(37,99,235,0.5);
        flex-shrink: 0;
    }
    .chat-header-icon img { width: 52px; height: 52px; object-fit: cover; border-radius: 50%; }
    .bot-msg-avatar {
        width: 48px; height: 48px; border-radius: 50%; object-fit: cover;
        flex-shrink: 0; align-self: flex-end; margin-right: 10px;
        box-shadow: 0 2px 8px rgba(37,99,235,0.3);
    }
    .chat-msg-with-avatar {
        display: flex; align-items: flex-end; gap: 6px;
    }
    .chat-header h3 { font-size: 17px; font-weight: 600; }
    .chat-header p { font-size: 13px; color: var(--sidebar-text); }
    .chat-close {
        background: none; border: none; color: #fff;
        cursor: pointer; padding: 10px; border-radius: 8px;
        min-width: 44px; min-height: 44px;
        display: grid; place-items: center;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }
    .chat-close:hover { background: rgba(255,255,255,0.15); }
    .chat-close:active { background: rgba(255,255,255,0.25); }

    .chat-messages {
        flex: 1; overflow-y: auto; padding: 16px;
        display: flex; flex-direction: column; gap: 12px;
    }
    .chat-msg {
        max-width: 85%; padding: 14px 18px; border-radius: 16px;
        font-size: 16px; line-height: 1.65; word-wrap: break-word;
    }
    .chat-msg-assistant ul, .chat-msg-assistant ol { margin: 4px 0; padding-left: 18px; }
    .chat-msg-assistant li { margin: 2px 0; }
    .chat-msg-assistant p { margin: 4px 0; }
    .chat-msg-assistant strong { font-weight: 600; }
    .chat-msg-actions { display: flex; gap: 4px; margin-top: 6px; }
    .chat-msg-actions button {
        background: none; border: none; cursor: pointer; padding: 3px;
        color: var(--text-dim); border-radius: 4px; display: grid; place-items: center;
    }
    .chat-msg-actions button:hover { color: var(--primary); background: var(--primary-bg); }
    .chat-msg-actions button.speaking { color: var(--primary); }
    .chat-msg-user {
        align-self: flex-end; background: var(--primary); color: #fff;
        border-bottom-right-radius: 4px;
    }
    .chat-msg-assistant {
        align-self: flex-start; background: var(--surface-2);
        border: 1px solid var(--border); color: var(--text);
        border-bottom-left-radius: 4px;
    }
    .chat-msg-typing {
        align-self: flex-start; background: var(--surface-2);
        border: 1px solid var(--border); color: var(--text-3);
        font-style: italic; padding: 10px 14px; border-radius: 12px;
        border-bottom-left-radius: 4px;
        display: flex; align-items: center; gap: 6px;
    }
    .chat-msg-typing .typing-text { transition: opacity 0.18s ease; }
    .chat-msg-typing .typing-dots { display: inline-flex; gap: 3px; }
    .chat-msg-typing .typing-dots span {
        width: 7px; height: 7px; border-radius: 50%; background: var(--primary);
        animation: typingBounce 1.2s infinite ease-in-out;
    }
    .chat-msg-typing .typing-dots span:nth-child(2) { animation-delay: 0.15s; }
    .chat-msg-typing .typing-dots span:nth-child(3) { animation-delay: 0.3s; }
    @keyframes typingBounce {
        0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
        30% { transform: translateY(-5px); opacity: 1; }
    }

    .chat-input-area {
        padding: 12px; padding-bottom: max(12px, var(--safe-bottom));
        border-top: 1px solid var(--border);
        display: flex; gap: 8px; align-items: center;
    }
    .chat-input-area input {
        flex: 1; padding: 13px 16px; border-radius: 10px;
        border: 1px solid var(--border); background: var(--surface);
        color: var(--text); font-family: inherit; font-size: 16px;
        outline: none; transition: border 0.15s;
    }
    .chat-input-area input:focus { border-color: var(--primary); }
    .chat-send {
        width: 38px; height: 38px; border-radius: 10px;
        background: var(--primary); color: #fff; border: none;
        cursor: pointer; display: grid; place-items: center;
        transition: background 0.15s; flex-shrink: 0;
    }
    .chat-send:hover { background: var(--primary-light); }
    .chat-send:disabled { opacity: 0.5; cursor: not-allowed; }
    .chat-send.cancelling { background: #e74c3c; }
    .chat-send.cancelling:hover { background: #c0392b; }
    .chat-send i { width: 18px; height: 18px; stroke-width: 2; }
    .chat-mic {
        width: 38px; height: 38px; border-radius: 10px;
        background: var(--surface); color: var(--text-3); border: 1px solid var(--border);
        cursor: pointer; display: grid; place-items: center;
        transition: all 0.15s; flex-shrink: 0;
    }
    .chat-mic:hover { border-color: var(--primary); color: var(--primary); }
    .chat-mic.recording { background: var(--red); color: #fff; border-color: var(--red); animation: pulse-mic 1s infinite; }
    .chat-mic i { width: 18px; height: 18px; stroke-width: 2; }
    @keyframes pulse-mic { 0%,100% { opacity:1; } 50% { opacity:0.6; } }
    /* Leaflet layer control – clean compact style */
    .leaflet-control-layers { border-radius: 8px !important; box-shadow: 0 2px 8px rgba(0,0,0,.15) !important; font-size: 13px !important; padding: 6px 10px !important; }
    .leaflet-control-layers label { margin-bottom: 2px; }
    /* Keep maps below chat overlay */
    .leaflet-pane, .leaflet-top, .leaflet-bottom { z-index: 1 !important; }
    .leaflet-control { z-index: 2 !important; }
    /* ===== CADENA DE FRIO ===== */
    .temp-banner {
        display: flex; align-items: center; gap: 10px;
        padding: 12px 14px; border-radius: var(--radius);
        border: 1px solid var(--border); background: var(--surface);
        font-size: 13px; font-weight: 500; margin-bottom: 16px;
    }
    .temp-banner i { width: 16px; height: 16px; flex-shrink: 0; }
    .temp-banner-ok { background: var(--green-bg); border-color: var(--green-border); color: var(--green); }
    .temp-banner-warn { background: var(--amber-bg); border-color: var(--amber-border); color: var(--amber); }
    .temp-banner-bad { background: var(--red-bg); border-color: var(--red-border); color: var(--red); }
    .temp-grid {
        display: grid; gap: 12px; margin-bottom: 16px;
        grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    }
    /* En telefono cada nevera ocupa su fila: el numero grande es lo que
       se lee de lejos, no queremos dos tarjetas apretadas al lado. */
    @media (max-width: 560px) {
        .temp-grid { grid-template-columns: 1fr; }
    }
    .temp-card {
        background: var(--surface); border: 1px solid var(--border);
        border-radius: var(--radius); padding: 16px;
        box-shadow: var(--shadow-sm); border-left-width: 4px;
    }
    .temp-card-ok       { border-left-color: var(--green); }
    .temp-card-alto     { border-left-color: var(--red); }
    .temp-card-bajo     { border-left-color: var(--primary); }
    .temp-card-sin_datos{ border-left-color: var(--text-dim); background: var(--surface-2); }
    .temp-card-top {
        display: flex; align-items: flex-start; justify-content: space-between;
        gap: 8px; margin-bottom: 10px;
    }
    .temp-name { font-size: 13px; font-weight: 600; color: var(--text); line-height: 1.3; }
    .temp-pill {
        font-size: 10px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
        padding: 3px 8px; border-radius: 999px; white-space: nowrap;
    }
    .temp-pill-ok        { background: var(--green-bg); color: var(--green); }
    .temp-pill-alto      { background: var(--red-bg); color: var(--red); }
    .temp-pill-bajo      { background: var(--primary-bg); color: var(--primary); }
    .temp-pill-sin_datos { background: var(--border-light); color: var(--text-3); }
    .temp-value {
        font-size: 38px; font-weight: 700; line-height: 1;
        letter-spacing: -.02em; font-variant-numeric: tabular-nums;
    }
    .temp-value-ok        { color: var(--text); }
    .temp-value-alto      { color: var(--red); }
    .temp-value-bajo      { color: var(--primary); }
    .temp-value-sin_datos { color: var(--text-dim); }
    .temp-unit { font-size: 17px; font-weight: 600; margin-left: 3px; }
    .temp-sub  { font-size: 11px; color: var(--text-3); margin-top: 6px; }
    /* La edad del dato es lo mas importante de la tarjeta: un numero viejo
       ensenado como si fuera de ahora es peor que no ensenar nada. */
    .temp-age {
        font-size: 11px; font-weight: 600; margin-top: 10px;
        padding-top: 10px; border-top: 1px solid var(--border-light);
        display: flex; align-items: center; gap: 5px; color: var(--text-3);
    }
    .temp-age i { width: 12px; height: 12px; }
    .temp-age-stale { color: var(--red); }
    /* El SVG dibuja SOLO el area de la curva. Los grados, las horas y el globo
       del lector son HTML puesto encima, con margenes en pixeles de verdad:
       dentro de un viewBox estirado el texto sale aplastado a la mitad de su
       ancho en un telefono. */
    .temp-chart-wrap { position: relative; }
    .temp-plot {
        position: relative;
        margin: 6px 12px 24px 38px;
        /* El dedo arrastra de lado para leer la curva sin que la pagina se
           mueva; hacia arriba y abajo la pagina sigue haciendo scroll. */
        touch-action: pan-y;
        cursor: crosshair;
        -webkit-tap-highlight-color: transparent;
    }
    .temp-chart { width: 100%; height: 200px; display: block; overflow: visible; }
    .temp-ejeY {
        position: absolute; right: calc(100% + 6px); transform: translateY(-50%);
        font-size: 10px; color: var(--text-3); white-space: nowrap;
        font-variant-numeric: tabular-nums; pointer-events: none;
    }
    .temp-ejeY-max { color: var(--red); font-weight: 600; }
    .temp-ejeY-min { color: var(--primary); font-weight: 600; }
    .temp-ejeX {
        position: absolute; top: calc(100% + 5px); transform: translateX(-50%);
        font-size: 10px; color: var(--text-3); white-space: nowrap; pointer-events: none;
    }
    /* El lector: la linea, el punto y el globo que siguen al dedo. */
    .temp-scrub {
        position: absolute; top: 0; bottom: 0; width: 0;
        pointer-events: none; z-index: 2;
    }
    .temp-scrub-line {
        position: absolute; top: -4px; bottom: -4px; left: 0; width: 1px;
        background: var(--text-3); opacity: .6;
    }
    .temp-scrub-dot {
        position: absolute; left: 0; width: 9px; height: 9px; margin: -5px 0 0 -5px;
        border-radius: 50%; background: var(--primary);
        box-shadow: 0 0 0 2px var(--surface);
    }
    .temp-scrub-dot-fuera { background: var(--red); }
    .temp-scrub-tip {
        position: absolute; top: 4px; left: 12px;
        background: var(--surface); border: 1px solid var(--border);
        border-radius: 8px; box-shadow: var(--shadow-md);
        padding: 6px 9px; line-height: 1.3; white-space: nowrap;
    }
    /* Pegado al borde derecho el globo se voltea, o se sale de la tarjeta. */
    .temp-scrub-tip-izq { left: auto; right: 12px; }
    .temp-scrub-hora { display: block; font-size: 10px; color: var(--text-3); }
    .temp-scrub-val {
        font-size: 15px; font-weight: 700; color: var(--text);
        font-variant-numeric: tabular-nums;
    }
    .temp-scrub-val-fuera { color: var(--red); }
    .temp-scrub-nota { display: block; font-size: 10px; color: var(--text-3); }
    .temp-chart-hint {
        display: flex; align-items: center; gap: 5px;
        margin-top: 6px; font-size: 11px; color: var(--text-dim);
    }
    .temp-chart-hint i { width: 12px; height: 12px; flex: none; }
    /* Avisos: quien recibe la alarma. Una fila por pierna (push, WhatsApp). */
    .temp-aviso-fila {
        display: flex; gap: 12px; align-items: center;
        padding: 12px 0; border-bottom: 1px solid var(--border-light);
    }
    .temp-aviso-fila:first-child { padding-top: 0; }
    .temp-aviso-fila:last-child { border-bottom: none; padding-bottom: 0; }
    .temp-aviso-fila > i { width: 18px; height: 18px; flex: none; color: var(--primary); }
    .temp-detalle summary { cursor: pointer; font-size: 12px; font-weight: 600; color: var(--primary); }
    .temp-detalle ol { font-size: 12px; color: var(--text-2); padding-left: 18px; margin: 8px 0 0; line-height: 1.55; }
    .temp-detalle code { font-size: 11px; background: var(--surface-2); padding: 2px 5px; border-radius: 4px; }
    /* La lista de quien recibe WhatsApp. Era una tabla y en un iPhone el
       estado y los botones caian detras de un scroll horizontal que nadie
       descubre; asi cada persona se apila sola cuando no cabe. */
    .temp-canal {
        display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center;
        padding: 10px 0; border-top: 1px solid var(--border-light);
    }
    .temp-canal-quien { flex: 1 1 200px; min-width: 0; }
    .temp-canal-nombre { font-size: 13px; font-weight: 600; margin-right: 8px; }
    .temp-canal-tel { font-size: 12px; color: var(--text-2); }
    .temp-canal-estado { display: block; font-size: 11px; color: var(--text-3); margin-top: 2px; }
    .temp-canal-acciones { display: flex; gap: 6px; flex-wrap: wrap; }
    .temp-canal-acciones .btn { font-size: 11px; padding: 4px 9px; }
    .temp-canal-form { grid-template-columns: repeat(3, 1fr); }
    @media (max-width: 639px) { .temp-canal-form { grid-template-columns: 1fr; } }
    @media (max-width: 639px) {
        .temp-plot { margin: 6px 10px 22px 34px; }
        .temp-chart { height: 180px; }
    }
    /* El selector de sensor y los botones de rango tienen que poder bajar
       de linea: en telefono no caben al lado del titulo de la tarjeta. */
    #p-temperatura .card-header { flex-wrap: wrap; gap: 8px; }
    .temp-range-controls { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
    .temp-range-controls select { max-width: 100%; }
    .temp-empty { padding: 32px 16px; text-align: center; color: var(--text-3); font-size: 13px; }

    /* ======================================================== */
    /* TABLAS QUE SE VUELVEN TARJETAS EN EL TELEFONO            */
    /* ======================================================== */
    /* El equipo trabaja desde el celular, en la finca. Las tablas de
       Facturas, Ordenes y Entregas tienen de 4 a 8 columnas apretadas en
       390 px: los nombres de cliente se parten en dos lineas y los botones
       se amontonan. En pantalla ancha se quedan como tablas —ahi funcionan
       bien— y por debajo de 640 px cada fila pasa a ser una tarjeta.

       El reparto lo hace un grid sobre la propia <tr>: cada celda va a su
       sitio por su atributo data-celda, sin duplicar el HTML ni mantener
       dos versiones de cada pantalla. */
    @media (max-width: 639px) {
        .tabla-tarjetas { overflow: visible; }
        /* Sin esto la <table> se sigue midiendo como tabla y sale mas ancha
           que la pantalla, aunque las filas ya sean tarjetas. */
        .tabla-tarjetas table {
            display: block; width: 100%; max-width: 100%;
            /* .table-scroll table pone min-width: 500px para que la tabla se
               pueda desplazar de lado. Aqui no hay tabla que desplazar, y
               min-width le gana a max-width: sin este 0, la tarjeta seguia
               midiendo 500 px en una pantalla de 390 y el boton de anular
               quedaba fuera. */
            min-width: 0;
            border-collapse: separate; border-spacing: 0;
        }
        .tabla-tarjetas thead { display: none; }
        .tabla-tarjetas tbody { display: flex; flex-direction: column; gap: 10px; }

        .tabla-tarjetas tr {
            display: grid;
            grid-template-columns: minmax(0, 1fr) auto;
            align-items: center;
            gap: 3px 10px;
            padding: 12px 14px;
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 12px;
            box-shadow: var(--shadow-sm);
        }
        .tabla-tarjetas tr:hover { background: var(--surface); }
        .tabla-tarjetas td {
            display: block;
            border: none !important;
            padding: 0 !important;
            min-width: 0;
            overflow-wrap: anywhere;
        }

        /* Fila 1: el numero de factura o la fecha, y el estado a la derecha */
        .tabla-tarjetas td[data-celda="num"],
        .tabla-tarjetas td[data-celda="fecha"] { grid-column: 1; order: 1; font-size: 12px; }
        .tabla-tarjetas td[data-celda="estado"] { grid-column: 2; order: 2; justify-self: end; }

        /* Fila 2: el cliente, que es lo que se busca con la vista */
        .tabla-tarjetas td[data-celda="cliente"] {
            grid-column: 1 / -1; order: 3;
            font-size: 16px; line-height: 1.25; color: var(--text);
            margin-top: 1px;
        }
        .tabla-tarjetas td[data-celda="cliente"] strong { font-weight: 650; }

        /* Fila 3: el dinero grande, y a la derecha las cajas o la entrega */
        .tabla-tarjetas td[data-celda="total"] {
            grid-column: 1; order: 4;
            font-size: 19px; font-weight: 700; color: var(--text);
            letter-spacing: -0.02em; margin-top: 4px;
        }
        .tabla-tarjetas td[data-celda="entrega"],
        .tabla-tarjetas td[data-celda="cajas"] {
            grid-column: 2; order: 5; justify-self: end;
            font-size: 13px; color: var(--text-3); margin-top: 4px;
        }
        .tabla-tarjetas td[data-celda="cajas"]::after { content: ' cajas'; font-weight: 400; }
        .tabla-tarjetas td[data-celda="items"] { display: none; }

        /* Fila 4: las acciones, separadas por una linea para que no se lean
           como parte del dato de arriba */
        .tabla-tarjetas td[data-celda="acciones"] {
            grid-column: 1 / -1; order: 6;
            display: flex; flex-wrap: wrap; gap: 6px;
            margin-top: 10px; padding-top: 10px !important;
            border-top: 1px solid var(--border-light) !important;
            white-space: normal !important;
        }
        /* En la tarjeta si caben las etiquetas: un icono suelto no dice que
           hace el boton, y aqui hay sitio de sobra. */
        .tabla-tarjetas td[data-celda="acciones"] .btn-label { display: inline !important; }
        .tabla-tarjetas td[data-celda="acciones"] .btn-sm {
            padding: 7px 11px !important; font-size: 12.5px;
        }
        /* Anular queda al final y separado de Pagar: estaban pegados, y son
           el boton que cobra y el que borra. */
        .tabla-tarjetas td[data-celda="acciones"] .btn-danger { margin-left: auto; }

        .tabla-tarjetas tr:empty, .tabla-tarjetas td:empty { display: none; }
        /* La fila de "no hay nada" no es una tarjeta */
        .tabla-tarjetas td[colspan] {
            grid-column: 1 / -1; display: block !important;
            text-align: center; padding: 8px !important;
        }
    }

    /* ======================================================== */
    /* HOJA DE "MAS" (telefono)                                 */
    /* ======================================================== */
    .mas-hoja {
        display: none; position: fixed; inset: 0; z-index: 180;
        background: rgba(15,23,42,0.55); backdrop-filter: blur(3px);
        align-items: flex-end; justify-content: center;
        padding-bottom: var(--safe-bottom);
    }
    .mas-hoja.abierta { display: flex; }
    .mas-hoja-panel {
        width: 100%; background: var(--surface);
        border-radius: 18px 18px 0 0;
        padding: 8px 12px calc(16px + var(--safe-bottom));
        box-shadow: 0 -8px 32px rgba(0,0,0,0.18);
        animation: masSube .22s cubic-bezier(0.16,1,0.3,1);
    }
    @keyframes masSube { from { transform: translateY(16px); opacity: .6 } to { transform: none; opacity: 1 } }
    @media (prefers-reduced-motion: reduce) { .mas-hoja-panel { animation: none } }
    .mas-hoja-asa {
        width: 38px; height: 4px; border-radius: 99px;
        background: var(--border); margin: 6px auto 12px;
    }
    .mas-item {
        display: flex; align-items: center; gap: 14px; width: 100%;
        padding: 13px 12px; border: none; background: transparent;
        border-radius: 12px; cursor: pointer; text-align: left;
        font-family: inherit; color: var(--text);
    }
    .mas-item:active { background: var(--surface-2); }
    /* Al dibujarse, el <i data-lucide> se reemplaza por un <svg>: hay que
       nombrar los dos o la regla deja de aplicar en cuanto carga el icono. */
    .mas-item i, .mas-item svg { width: 21px; height: 21px; stroke-width: 1.9; color: var(--primary); flex-shrink: 0; }
    .mas-item span { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
    .mas-item strong { font-size: 15px; font-weight: 600; }
    .mas-item small { font-size: 12.5px; color: var(--text-3); font-weight: 400; }
    .mas-item-sec { border-top: 1px solid var(--border-light); border-radius: 0 0 12px 12px; margin-top: 4px; padding-top: 15px; }
    .mas-item-sec i, .mas-item-sec svg { color: var(--text-3); }
    @media (min-width: 640px) { .mas-hoja { display: none !important; } }

    /* ======================================================== */
    /* ANULAR: destructivo, pero no gritando                    */
    /* ======================================================== */
    /* En la tarjeta del telefono el boton de anular quedaba tan grande y tan
       rojo como el de cobrar. Los dos son importantes, pero solo uno se usa
       todos los dias. Contorno rojo en vez de bloque solido: sigue leyendose
       como peligroso y deja de competir con Pagar. */
    .tabla-tarjetas td[data-celda="acciones"] .btn-danger {
        background: transparent; color: var(--red);
        border: 1px solid var(--red-border);
    }
    .tabla-tarjetas td[data-celda="acciones"] .btn-danger:active { background: var(--red-bg); }

    /* ======================================================== */
    /* EL BOT ESCRIBIENDO                                       */
    /* ======================================================== */
    /* La respuesta llega palabra por palabra. El cursor parpadeante al final
       dice que todavia esta escribiendo, sin ocupar sitio ni mover el texto. */
    .chat-modelo { font-size: 10px; color: var(--text-3); margin-top: 4px; opacity: 0.7; }
    .chat-msg.escribiendo .chat-cuerpo > *:last-child::after,
    .chat-msg.escribiendo .chat-cuerpo:empty::after {
        content: '';
        display: inline-block; vertical-align: -2px;
        width: 2px; height: 1em; margin-left: 3px;
        background: var(--primary);
        animation: cursorParpadeo 1s steps(2, start) infinite;
    }
    @keyframes cursorParpadeo { 0%, 100% { opacity: 1 } 50% { opacity: 0 } }
    @media (prefers-reduced-motion: reduce) {
        .chat-msg.escribiendo .chat-cuerpo > *:last-child::after { animation: none }
    }

    /* ======================================================== */
    /* ESQUELETOS DE CARGA                                      */
    /* ======================================================== */
    .esqueleto {
        display: block; height: 12px; border-radius: 6px;
        background: linear-gradient(90deg, var(--surface-2) 25%, var(--border-light) 37%, var(--surface-2) 63%);
        background-size: 400% 100%;
        animation: esqueletoBrillo 1.4s ease-in-out infinite;
    }
    @keyframes esqueletoBrillo { 0% { background-position: 100% 50% } 100% { background-position: 0 50% } }
    @media (prefers-reduced-motion: reduce) {
        .esqueleto { animation: none; background: var(--surface-2); }
    }
    .fila-esqueleto td { padding: 14px !important; }
    /* Anchos desiguales: un bloque perfectamente regular no se lee como texto
       que esta por llegar, se lee como un error de dibujo. */
    .fila-esqueleto td:nth-child(1) .esqueleto { width: 70% }
    .fila-esqueleto td:nth-child(2) .esqueleto { width: 88% }
    .fila-esqueleto td:nth-child(3) .esqueleto { width: 52% }
    .fila-esqueleto td:nth-child(4) .esqueleto { width: 64% }
    .fila-esqueleto td:nth-child(5) .esqueleto { width: 40% }
    .fila-esqueleto td:nth-child(6) .esqueleto { width: 76% }
    .stat-card-esqueleto { display: flex; flex-direction: column; gap: 10px; }
    .esqueleto-icono { width: 34px; height: 34px; border-radius: 9px; }
    .esqueleto-cifra { width: 62%; height: 26px; }
    .esqueleto-linea { width: 44%; height: 10px; }
    /* En el telefono la fila del esqueleto no debe volverse tarjeta: no tiene
       data-celda y el grid la dejaria descolocada. */
    @media (max-width: 639px) {
        .tabla-tarjetas tr.fila-esqueleto { display: block; padding: 14px; }
        .tabla-tarjetas tr.fila-esqueleto td { display: block; margin-bottom: 8px; }
    }

    /* ======================================================== */
    /* FOCO DE TECLADO                                          */
    /* ======================================================== */
    /* Se navegaba con Tab a ciegas: el navegador quitaba su anillo por defecto
       y no se puso nada en su lugar. :focus-visible solo se activa cuando la
       navegacion es por teclado, asi que el raton no lo ve. */
    :where(button, a, input, select, textarea, [tabindex]):focus-visible {
        outline: 2px solid var(--primary);
        outline-offset: 2px;
        border-radius: 6px;
    }
    /* Sobre el fondo oscuro de la barra lateral, el azul no contrasta. */
    .sidebar :where(button, a):focus-visible,
    .mobile-header :where(button, a):focus-visible,
    .chat-header :where(button, a, select):focus-visible {
        outline-color: #fff;
    }

    /* ======================================================== */
    /* SALIDA DE EMERGENCIA DE LA PANTALLA DE ARRANQUE          */
    /* ======================================================== */
    /* Si la app no llega a arrancar, esto aparece a los 8 segundos. Antes la
       pantalla se quedaba en blanco y no habia forma de salir sin saber donde
       estan los ajustes del navegador. */
    .splash-rescate {
        display: none; margin-top: 34px; text-align: center;
        max-width: 300px; padding: 0 20px;
        animation: rescateEntra .3s ease-out;
    }
    .splash-rescate.visible { display: block; }
    @keyframes rescateEntra { from { opacity: 0; transform: translateY(6px) } to { opacity: 1; transform: none } }
    @media (prefers-reduced-motion: reduce) { .splash-rescate { animation: none } }
    .splash-rescate p { color: rgba(255,255,255,.85); font-size: 14.5px; margin-bottom: 14px; }
    .splash-rescate button {
        background: #2563eb; color: #fff; border: none;
        padding: 12px 22px; border-radius: 10px;
        font-family: inherit; font-size: 15px; font-weight: 600; cursor: pointer;
    }
    .splash-rescate button:active { transform: scale(.97); }
    .splash-rescate small {
        display: block; margin-top: 14px; line-height: 1.5;
        color: rgba(255,255,255,.45); font-size: 12px;
    }

    /* ======================== */
    /* PRODUCCION — LOTES       */
    /* ======================== */
    /* Solo se ve en el telefono: en la tarjeta el dato acompaña al codigo,
       en la tabla tiene su propia columna. */
    .solo-movil { display: none; }
    @media (max-width: 639px) { .solo-movil { display: inline; } }

    .lotes-accion {
        display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
        margin-bottom: 16px;
    }
    .lotes-boton-grande { padding: 14px 22px; font-size: 15px; border-radius: 12px; }
    .lotes-accion-texto { font-size: 12px; color: var(--text-3); flex: 1; min-width: 200px; }

    .ct-sensores { display: flex; flex-wrap: wrap; gap: 8px; }
    .ct-sensor {
        display: inline-flex; align-items: center; gap: 7px; margin: 0;
        padding: 8px 12px; border: 1px solid var(--border); border-radius: 9px;
        font-size: 13px; cursor: pointer; background: var(--bg);
    }
    /* El input global lleva appearance:none y la casilla se volvia invisible:
       se le devuelve su forma y se pinta la tarjeta entera cuando esta marcada. */
    .ct-sensor input {
        width: 16px; height: 16px; margin: 0; padding: 0;
        appearance: auto; -webkit-appearance: checkbox; accent-color: var(--primary);
    }
    .ct-sensor:has(input:checked) {
        border-color: var(--primary); background: var(--primary-bg); color: var(--primary); font-weight: 600;
    }

    .lote-codigo-grande {
        font-family: Consolas, Menlo, monospace; font-size: 34px; font-weight: 700;
        letter-spacing: 1px; margin: 10px 0 4px; color: var(--primary);
        font-variant-numeric: tabular-nums; word-break: break-all;
    }

    .modal-ancho { max-width: 680px; }
    .lote-datos {
        display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
        gap: 8px 14px; margin-top: 14px; padding: 12px; background: var(--bg);
        border-radius: 10px; font-size: 13px;
    }
    .lote-dato span { display: block; font-size: 11px; color: var(--text-3); }
    .lote-dato strong { font-weight: 600; }
    .lote-alerta {
        padding: 7px 0; border-bottom: 1px solid var(--border-light); line-height: 1.4;
    }
    .lote-alerta:last-child { border-bottom: none; }
    .lote-firma {
        display: flex; gap: 10px; align-items: flex-start; padding: 12px;
        border: 1px dashed var(--border); border-radius: 10px; font-size: 12px;
        color: var(--text-3);
    }
    .lote-firma i { width: 18px; height: 18px; flex: none; margin-top: 1px; }
    .lote-firma strong { color: var(--text); }
    .lote-firma-ok { border-style: solid; border-color: var(--green-border); background: var(--green-bg); }
    .lote-firma-ok i { color: var(--green); }
    .lote-acciones { flex-wrap: wrap; }

    /* ======================== */
    /* INSUMOS                  */
    /* ======================== */
    .insumos-grid {
        display: grid; gap: 12px; padding: 14px;
        grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    }
    .insumos-cat {
        grid-column: 1 / -1; font-size: 11px; font-weight: 700; letter-spacing: .06em;
        text-transform: uppercase; color: var(--text-3); margin-top: 4px;
    }
    .insumo-card {
        background: var(--surface); border: 1px solid var(--border);
        border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow-sm);
        border-left-width: 4px; border-left-color: var(--green);
    }
    .insumo-bajo { border-left-color: var(--red); background: var(--red-bg); }
    .insumo-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 6px; }
    .insumo-nombre { font-weight: 600; font-size: 14px; line-height: 1.3; }
    .insumo-cant {
        font-size: 30px; font-weight: 700; line-height: 1.1; margin: 6px 0 8px;
        font-variant-numeric: tabular-nums; letter-spacing: -.02em;
    }
    .insumo-cant span { font-size: 13px; font-weight: 500; color: var(--text-3); }
    .insumo-sub { font-size: 11px; color: var(--text-3); margin-top: 6px; }
    .insumo-btns { display: flex; gap: 6px; margin-top: 10px; }
    .insumo-btns .btn { flex: 1; justify-content: center; }
    .im-tipos { display: flex; gap: 6px; margin-top: 12px; }
    .im-tipos .btn { flex: 1; justify-content: center; }

    /* ======================== */
    /* AVISOS AL ENTRAR         */
    /* ======================== */
    .avisos-icono {
        width: 52px; height: 52px; border-radius: 50%; margin: 0 auto 12px;
        display: grid; place-items: center; background: var(--amber-bg); color: var(--amber);
    }
    .avisos-icono i { width: 26px; height: 26px; }
    .avisos-opciones { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }
    .avisos-opcion {
        display: flex; align-items: center; gap: 12px; text-align: left; width: 100%;
        padding: 12px 14px; border: 1px solid var(--border); border-radius: 12px;
        background: var(--bg); color: var(--text); cursor: pointer; font-family: inherit;
    }
    .avisos-opcion:hover { border-color: var(--primary); background: var(--primary-bg); }
    .avisos-opcion i { width: 22px; height: 22px; flex: none; color: var(--primary); }
    .avisos-opcion strong { display: block; font-size: 14px; }
    .avisos-opcion small { display: block; font-size: 12px; color: var(--text-3); margin-top: 2px; line-height: 1.4; }
