/* ===================================
   CSS Variables
   =================================== */
:root {
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --primary-soft: #eef2ff;
    --bg-color: #f8fafc;
    --panel-bg: #ffffff;
    --border-color: #e2e8f0;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --success: #16a34a;
    --success-soft: #dcfce7;
    --danger: #dc2626;
    --danger-soft: #fee2e2;
    --warning: #d97706;
    --warning-soft: #fef3c7;
    --shadow: 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.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --radius: 12px;
}

/* ===================================
   Reset & Base
   =================================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-color);
    color: var(--text-primary);
    min-height: 100vh;
    font-size: 14px;
    line-height: 1.5;
}

h1 { font-size: 22px; font-weight: 600; }
h2 { font-size: 16px; font-weight: 600; }
h3 { font-size: 14px; font-weight: 600; }

/* ===================================
   Navegación
   =================================== */
.nav {
    background: var(--panel-bg);
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 20;
}

.nav-inner {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 24px;
    display: flex;
    align-items: center;
    gap: 28px;
    height: 60px;
}

.nav-brand {
    display: flex;
    align-items: center;
    gap: 9px;
    font-weight: 600;
    font-size: 15px;
    color: var(--text-primary);
    text-decoration: none;
    white-space: nowrap;
}

.nav-brand svg { color: var(--primary-color); }

.nav-links {
    display: flex;
    gap: 4px;
    overflow-x: auto;
}

.nav-links a {
    padding: 7px 13px;
    border-radius: 8px;
    color: var(--text-secondary);
    text-decoration: none;
    font-weight: 500;
    white-space: nowrap;
    transition: background .15s, color .15s;
}

.nav-links a:hover { background: var(--bg-color); color: var(--text-primary); }
.nav-links a.active { background: var(--primary-soft); color: var(--primary-hover); }

/* ===================================
   Layout
   =================================== */
.page {
    max-width: 1200px;
    margin: 0 auto;
    padding: 28px 24px 60px;
}

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 16px;
    flex-wrap: wrap;
    margin-bottom: 22px;
}

.page-header p {
    color: var(--text-secondary);
    margin-top: 3px;
}

.panel {
    background: var(--panel-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 20px;
    overflow: hidden;
}

.panel-header {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.panel-body { padding: 18px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }

@media (max-width: 860px) {
    .grid-2 { grid-template-columns: 1fr; }
}

/* ===================================
   Tarjetas de resumen
   =================================== */
.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 14px;
    margin-bottom: 22px;
}

.card {
    background: var(--panel-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 16px 18px;
    box-shadow: var(--shadow);
}

.card-label {
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: .04em;
}

.card-value {
    font-size: 28px;
    font-weight: 600;
    margin-top: 6px;
    font-variant-numeric: tabular-nums;
}

.card-value.warn { color: var(--warning); }
.card-value.bad { color: var(--danger); }
.card-value.good { color: var(--success); }

/* ===================================
   Formularios
   =================================== */
.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
    margin-bottom: 14px;
}

.field { display: flex; flex-direction: column; gap: 5px; }

.field label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
}

input, select {
    font-family: inherit;
    font-size: 14px;
    padding: 9px 11px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--panel-bg);
    color: var(--text-primary);
    width: 100%;
    transition: border-color .15s, box-shadow .15s;
}

input:focus, select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgb(99 102 241 / .15);
}

input[type="number"] { font-variant-numeric: tabular-nums; }

/* ===================================
   Botones
   =================================== */
.btn {
    font-family: inherit;
    font-size: 14px;
    font-weight: 500;
    padding: 9px 16px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: var(--primary-color);
    color: #fff;
    cursor: pointer;
    white-space: nowrap;
    transition: background .15s, opacity .15s;
}

.btn:hover { background: var(--primary-hover); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-secondary {
    background: var(--panel-bg);
    color: var(--text-primary);
    border-color: var(--border-color);
}

.btn-secondary:hover { background: var(--bg-color); }

.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #b91c1c; }

.btn-sm { padding: 5px 10px; font-size: 12.5px; }

/* ===================================
   Tablas
   =================================== */
.table-wrap { overflow-x: auto; }

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13.5px;
}

thead th {
    text-align: left;
    padding: 10px 14px;
    background: var(--bg-color);
    border-bottom: 1px solid var(--border-color);
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .03em;
    white-space: nowrap;
}

tbody td {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border-color);
    vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #fafbfc; }

.num {
    text-align: right;
    font-variant-numeric: tabular-nums;
    /* Un importe como "Q 3,187.50" no debe partirse en dos renglones. */
    white-space: nowrap;
}

.mono {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 12.5px;
}

.row-anulada td { color: var(--text-muted); text-decoration: line-through; }
.row-alerta { background: var(--warning-soft) !important; }
.negativo { color: var(--danger); font-weight: 600; }

/* ===================================
   Etiquetas
   =================================== */
.tag {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 11.5px;
    font-weight: 600;
    white-space: nowrap;
}

.tag-compra { background: var(--primary-soft); color: var(--primary-hover); }
.tag-venta { background: var(--success-soft); color: var(--success); }
.tag-anulada { background: var(--danger-soft); color: var(--danger); }
.tag-inactivo { background: #f1f5f9; color: var(--text-secondary); }
.tag-alerta { background: var(--warning-soft); color: var(--warning); }

/* ===================================
   Avisos y estados
   =================================== */
.alert {
    padding: 11px 14px;
    border-radius: 8px;
    margin-bottom: 14px;
    font-weight: 500;
}

.alert-error { background: var(--danger-soft); color: #991b1b; }
.alert-ok { background: var(--success-soft); color: #166534; }
.alert-warn { background: var(--warning-soft); color: #92400e; }

.empty {
    padding: 36px 18px;
    text-align: center;
    color: var(--text-muted);
}

.hidden { display: none !important; }

/* ===================================
   Reportes
   =================================== */
.reportes-layout {
    display: grid;
    grid-template-columns: 240px 1fr;
    gap: 20px;
    align-items: start;
}

@media (max-width: 900px) {
    .reportes-layout { grid-template-columns: 1fr; }
}

.reportes-menu { display: flex; flex-direction: column; gap: 3px; padding: 8px; }

.reportes-menu button {
    font-family: inherit;
    font-size: 13.5px;
    text-align: left;
    padding: 9px 12px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: background .15s, color .15s;
}

.reportes-menu button:hover { background: var(--bg-color); color: var(--text-primary); }

.reportes-menu button.active {
    background: var(--primary-soft);
    color: var(--primary-hover);
    font-weight: 600;
}

.filtros {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    flex-wrap: wrap;
}

.filtros .field { min-width: 140px; }

/* ===================================
   Cierre de factura (IVA + totales)
   =================================== */
.cierre-factura {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 20px;
    flex-wrap: wrap;
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid var(--border-color);
}

.cierre-factura .field { max-width: 130px; }

.totales {
    min-width: 260px;
    margin-left: auto;
}

.totales-fila {
    display: flex;
    justify-content: space-between;
    gap: 24px;
    padding: 4px 0;
    font-variant-numeric: tabular-nums;
}

.totales-fila span:first-child { color: var(--text-secondary); }

.totales-fila.descuento span:last-child { color: var(--danger); }

.totales-fila.total {
    margin-top: 6px;
    padding-top: 8px;
    border-top: 1px solid var(--border-color);
    font-size: 16px;
    font-weight: 600;
}

/* ===================================
   Modal de detalle de factura
   =================================== */
.modal-fondo {
    position: fixed;
    inset: 0;
    background: rgb(15 23 42 / .5);
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 40px 20px;
    overflow-y: auto;
    z-index: 50;
}

.modal {
    background: var(--panel-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 780px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}

.modal-acciones {
    display: flex;
    align-items: center;
    gap: 10px;
}

.modal-cerrar {
    background: none;
    border: none;
    font-size: 26px;
    line-height: 1;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0 6px;
    border-radius: 6px;
}

.modal-cerrar:hover { background: var(--bg-color); color: var(--text-primary); }

.modal-body { padding: 18px 20px 20px; }

/* Datos de cabecera de la factura */
.factura-datos {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 14px;
    margin-bottom: 18px;
}

.factura-datos .dato-etiqueta {
    font-size: 11.5px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: .04em;
}

.factura-datos .dato-valor { margin-top: 3px; font-weight: 500; }

/* Enlace que abre la factura desde el buscador */
.link-factura {
    background: none;
    border: none;
    padding: 0;
    font: inherit;
    color: var(--primary-color);
    font-weight: 600;
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.link-factura:hover { color: var(--primary-hover); }

/* Mientras el modal está abierto, el fondo no debe poder desplazarse. */
body.modal-abierto { overflow: hidden; }

/* ===================================
   Impresión de la factura

   Solo se imprime cuando hay una factura abierta: en ese caso el modal deja de ser una capa
   flotante y pasa a ser la hoja. Si no hay modal abierto, estas reglas no aplican y la página
   se imprime normal.
   =================================== */
@media print {
    @page { margin: 1.5cm; }

    body.modal-abierto > *:not(.modal-fondo) { display: none !important; }

    body.modal-abierto .modal-fondo {
        position: static;
        display: block;
        background: none;
        padding: 0;
        overflow: visible;
    }

    body.modal-abierto .modal {
        max-width: none;
        box-shadow: none;
        border-radius: 0;
    }

    body.modal-abierto .modal-header {
        padding: 0 0 12px;
        border-bottom: 2px solid #000;
    }

    body.modal-abierto .modal-body { padding: 16px 0 0; }

    /* Los botones no tienen sentido en papel. */
    .modal-acciones { display: none !important; }

    /* Sin el ancho del modal, el bloque de totales se estiraría de margen a margen y dejaría
       las etiquetas separadas de sus cifras. Se acota para que quede como en una factura. */
    body.modal-abierto .totales {
        max-width: 300px;
        margin-left: auto;
    }

    body.modal-abierto .modal-header h2 { font-size: 19px; }

    /* Sin esto el navegador descarta los fondos y el descuento en rojo o la etiqueta de
       anulada se imprimirían en gris plano, perdiendo justo la información que distingue. */
    body.modal-abierto .modal,
    body.modal-abierto .modal * {
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    /* Una factura larga no debe partir una fila a la mitad. */
    body.modal-abierto tr { break-inside: avoid; }
    body.modal-abierto thead { display: table-header-group; }
    body.modal-abierto .totales { break-inside: avoid; }
}

.subtabla {
    margin: 0;
    background: var(--bg-color);
}

.subtabla td { padding: 5px 14px; border: none; font-size: 12.5px; }
