/* ===========================================================
   Cotizador — CSS crudo, mobile-first
   =========================================================== */
:root {
    --bg: #0f172a;
    --bg-soft: #1e293b;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --border: #e2e8f0;
    --text: #0f172a;
    --text-soft: #64748b;
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --primary-soft: #eef2ff;
    --success: #059669;
    --success-soft: #ecfdf5;
    --danger: #dc2626;
    --danger-soft: #fef2f2;
    --warning: #d97706;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, .06);
    --shadow: 0 4px 16px rgba(15, 23, 42, .08);
    --shadow-lg: 0 12px 32px rgba(15, 23, 42, .16);
    --radius: 14px;
    --radius-sm: 9px;
    --nav-h: 64px;
}

* { box-sizing: border-box; }

/* Phosphor Icons: alineación y tamaño heredado */
i[class^="ph"], i[class*=" ph"] { line-height: 1; vertical-align: -.05em; }
.btn i { font-size: 1.1em; }
.tag i { vertical-align: -.1em; }

html, body {
    margin: 0;
    padding: 0;
}

body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--surface-2);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    line-height: 1.5;
    font-size: 15px;
}

a { color: inherit; text-decoration: none; }

h1, h2, h3 { margin: 0; line-height: 1.25; }

/* ---------- Layout general ---------- */
.app-shell {
    min-height: 100vh;
    padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 18px;
    background: rgba(255, 255, 255, .85);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
}

.topbar .brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    letter-spacing: -.02em;
    font-size: 18px;
}

.brand .logo {
    width: 34px; height: 34px;
    display: grid; place-items: center;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--primary), #7c3aed);
    color: #fff; font-size: 18px;
    box-shadow: var(--shadow-sm);
}

.topbar .user-menu {
    display: flex; align-items: center; gap: 10px;
    font-size: 13px; color: var(--text-soft);
}

.avatar {
    width: 34px; height: 34px; border-radius: 50%;
    background: var(--primary-soft); color: var(--primary);
    display: grid; place-items: center; font-weight: 700;
}

.container {
    max-width: 1040px;
    margin: 0 auto;
    padding: 18px;
}

.page-head {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 18px;
}

.page-head h1 { font-size: 22px; letter-spacing: -.02em; }
.page-head p { margin: 4px 0 0; color: var(--text-soft); font-size: 13px; }

/* ---------- Navegación inferior (móvil) ---------- */
.bottom-nav {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    display: flex;
    background: rgba(255, 255, 255, .92);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--border);
    z-index: 40;
}

.bottom-nav a {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    font-size: 11px;
    color: var(--text-soft);
    font-weight: 600;
    transition: color .15s;
}

.bottom-nav a .ico { font-size: 20px; line-height: 1; }
.bottom-nav a.active { color: var(--primary); }
.bottom-nav a.cta .ico {
    width: 44px; height: 44px; margin-top: -18px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), #7c3aed);
    color: #fff;
    display: grid; place-items: center;
    box-shadow: var(--shadow);
}

/* ---------- Tarjetas ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    padding: 18px;
}
.card + .card { margin-top: 14px; }

.stat-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    margin-bottom: 18px;
}
.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow-sm);
}
.stat .label { font-size: 12px; color: var(--text-soft); font-weight: 600; }
.stat .value { font-size: 22px; font-weight: 800; letter-spacing: -.03em; margin-top: 6px; }
.stat.accent .value { color: var(--primary); }
.stat.good .value { color: var(--success); }

/* ---------- Botones ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    padding: 10px 16px;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: transform .05s, background .15s, box-shadow .15s;
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; box-shadow: var(--shadow-sm); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-soft { background: var(--danger-soft); color: var(--danger); }
.btn-sm { padding: 6px 11px; font-size: 13px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* ---------- Formularios ---------- */
.field { margin-bottom: 14px; }
.field label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 6px;
    color: var(--text);
}
.input, textarea.input, select.input {
    width: 100%;
    padding: 11px 13px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-family: inherit;
    background: var(--surface);
    color: var(--text);
    transition: border-color .15s, box-shadow .15s;
}
.input:focus, textarea.input:focus, select.input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}
textarea.input { resize: vertical; min-height: 72px; }
.field .error { color: var(--danger); font-size: 12px; margin-top: 5px; display: block; }
.form-row { display: grid; gap: 12px; }

.input-money { position: relative; }
.input-money::before {
    content: "$";
    position: absolute;
    left: 12px; top: 50%;
    transform: translateY(-50%);
    color: var(--text-soft);
    font-weight: 600;
    pointer-events: none;
}
.input-money .input { padding-left: 24px; }

/* ---------- Grid.js ---------- */
.gridjs-wrapper { box-shadow: none !important; border-radius: var(--radius-sm) !important; }
.gridjs-table { font-size: 14px; }
table.gridjs-table th, table.gridjs-table td { padding: 11px 12px; }
.gridjs-th { background: var(--surface-2) !important; font-weight: 700 !important; }
.gridjs-td { border-color: var(--border) !important; }
input.gridjs-input { border-radius: var(--radius-sm); }
.gridjs-pagination { font-size: 13px; }

/* ---------- Cotización ---------- */
.buscador-productos { position: relative; }
.sugerencias {
    position: absolute;
    top: calc(100% + 4px);
    left: 0; right: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    z-index: 20;
    max-height: 280px;
    overflow-y: auto;
    display: none;
}
.sugerencias.open { display: block; }
.sugerencia {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 13px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
}
.sugerencia:last-child { border-bottom: none; }
.sugerencia:hover, .sugerencia.hl { background: var(--primary-soft); }
.sugerencia .nom { font-weight: 600; }
.sugerencia .precio { color: var(--success); font-weight: 700; font-size: 13px; }
.sugerencia .empty { color: var(--text-soft); }

.linea {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 4px 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
    align-items: center;
}
.linea:last-child { border-bottom: none; }

/* Línea editable de la pantalla de cotización */
#lineas .linea {
    display: block;
    padding: 14px 0;
}
#lineas .linea-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#lineas .linea-head .nom { font-weight: 700; }
.input-sm { padding: 8px 11px !important; font-size: 14px !important; margin-top: 8px; }
#lineas .linea-campos {
    display: grid;
    grid-template-columns: 1fr 1fr auto;
    gap: 8px;
    margin-top: 8px;
    align-items: end;
}
#lineas .mini {
    display: block;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-soft);
}
.input-money-sm { margin-top: 4px; }
.input-money-sm .input { padding: 8px 11px 8px 22px !important; font-size: 14px !important; }
.input-money-sm::before { font-size: 13px; }
#lineas .mini .qty { margin-top: 4px; height: 36px; }
#lineas .mini .qty button { height: 36px; }
#lineas .mini .qty input { height: 36px; width: 40px; }
#lineas .linea-foot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 10px;
}
#lineas .linea-foot .monto { font-weight: 800; font-size: 16px; }
.linea .info .nom { font-weight: 600; }
.linea .info .sub { font-size: 12px; color: var(--text-soft); margin-top: 2px; }
.linea .controls { display: flex; align-items: center; gap: 8px; }
.qty {
    display: flex; align-items: center;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}
.qty button {
    width: 32px; height: 34px;
    border: none; background: var(--surface-2);
    font-size: 18px; cursor: pointer; color: var(--text);
    display: grid; place-items: center;
}
.qty button:active { background: var(--border); }
.qty input {
    width: 46px; height: 34px; border: none; text-align: center;
    font-size: 15px; font-family: inherit; -moz-appearance: textfield;
}
.qty input::-webkit-outer-spin-button,
.qty input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.linea .monto { font-weight: 700; min-width: 82px; text-align: right; }
.btn-remove {
    border: none; background: var(--danger-soft); color: var(--danger);
    width: 32px; height: 32px; border-radius: 8px; cursor: pointer; font-size: 16px;
}

.empty-lineas {
    text-align: center;
    color: var(--text-soft);
    padding: 28px 10px;
    font-size: 14px;
}

/* Resumen de 3 totales */
.resumen-3 {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    margin: 4px 0 12px;
}
.res-box {
    border-radius: var(--radius-sm);
    padding: 14px 16px;
    border: 1px solid var(--border);
}
.res-box .t { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; }
.res-box .n { font-size: 24px; font-weight: 800; letter-spacing: -.03em; margin-top: 4px; }
.res-cliente { background: var(--primary-soft); border-color: #c7d2fe; }
.res-cliente .t { color: var(--primary-dark); }
.res-costo { background: var(--surface-2); }
.res-costo .t { color: var(--text-soft); }
.res-ganancia { background: var(--success-soft); border-color: #a7f3d0; }
.res-ganancia .t { color: var(--success); }
.res-ganancia .n { color: var(--success); }
.res-ganancia.negativa { background: var(--danger-soft); border-color: #fecaca; }
.res-ganancia.negativa .t, .res-ganancia.negativa .n { color: var(--danger); }

.desglose { border-top: 1px dashed var(--border); padding-top: 12px; margin-top: 4px; }
.desglose .row { display: flex; justify-content: space-between; padding: 4px 0; font-size: 14px; }
.desglose .row .lbl { color: var(--text-soft); }
.desglose .row.total { font-weight: 800; border-top: 1px solid var(--border); margin-top: 6px; padding-top: 10px; }

/* ---------- Detalle / impresión ---------- */
.detalle-head {
    display: flex; justify-content: space-between; align-items: flex-start;
    flex-wrap: wrap; gap: 12px; margin-bottom: 16px;
}
.folio-badge {
    background: var(--primary-soft); color: var(--primary-dark);
    font-weight: 800; padding: 6px 12px; border-radius: 99px; font-size: 14px;
}
.tabla-items { width: 100%; border-collapse: collapse; font-size: 14px; }
.tabla-items th, .tabla-items td { padding: 10px 8px; text-align: left; border-bottom: 1px solid var(--border); }
.tabla-items th { color: var(--text-soft); font-size: 12px; text-transform: uppercase; }
.tabla-items td.num, .tabla-items th.num { text-align: right; }

/* ---------- Modal ---------- */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(15, 23, 42, .5);
    backdrop-filter: blur(2px);
    display: none;
    align-items: flex-end;
    justify-content: center;
    z-index: 60;
    padding: 0;
}
.modal-overlay.open { display: flex; }
.modal {
    background: var(--surface);
    width: 100%;
    max-width: 460px;
    border-radius: 20px 20px 0 0;
    padding: 20px;
    box-shadow: var(--shadow-lg);
    animation: sheet-up .22s ease;
    max-height: 92vh;
    overflow-y: auto;
}
@keyframes sheet-up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
.modal h2 { font-size: 18px; margin-bottom: 4px; }
.modal .modal-sub { color: var(--text-soft); font-size: 13px; margin-bottom: 16px; }
.modal-actions { display: flex; gap: 10px; margin-top: 8px; }
.modal-actions .btn { flex: 1; }

/* ---------- Toast ---------- */
.toast-stack {
    position: fixed;
    top: 16px; left: 50%; transform: translateX(-50%);
    z-index: 90;
    display: flex; flex-direction: column; gap: 8px;
    width: calc(100% - 32px); max-width: 420px;
}
.toast {
    background: var(--text); color: #fff;
    padding: 12px 16px; border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg); font-size: 14px; font-weight: 500;
    animation: toast-in .2s ease;
}
.toast.ok { background: var(--success); }
.toast.err { background: var(--danger); }
@keyframes toast-in { from { transform: translateY(-12px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- Login ---------- */
.auth-wrap {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
    background: radial-gradient(1200px 600px at 50% -10%, #312e81, var(--bg));
}
.auth-card {
    width: 100%; max-width: 380px;
    background: var(--surface);
    border-radius: 20px;
    padding: 30px 26px;
    box-shadow: var(--shadow-lg);
}
.auth-card .brand { justify-content: center; margin-bottom: 6px; }
.auth-card h1 { text-align: center; font-size: 20px; margin-bottom: 4px; }
.auth-card .sub { text-align: center; color: var(--text-soft); font-size: 13px; margin-bottom: 22px; }
.auth-alert {
    background: var(--danger-soft); color: var(--danger);
    border-radius: var(--radius-sm); padding: 10px 12px; font-size: 13px; margin-bottom: 14px;
}
.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 13px; margin-bottom: 16px; }

.status-ok {
    background: var(--success-soft); color: var(--success);
    border-radius: var(--radius-sm); padding: 10px 12px; font-size: 13px; margin-bottom: 14px;
}

.muted { color: var(--text-soft); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.mt-2 { margin-top: 12px; } .mt-3 { margin-top: 18px; }
.flex-between { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.tag { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 99px; background: var(--surface-2); color: var(--text-soft); }

/* Selector Factura / Cotización */
.chooser { display: grid; gap: 12px; }
.chooser-card {
    display: flex; align-items: center; gap: 14px;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow-sm);
    transition: border-color .15s, transform .05s;
}
.chooser-card:hover { border-color: var(--primary); }
.chooser-card:active { transform: translateY(1px); }
.chooser-ico {
    width: 52px; height: 52px; border-radius: 14px; flex-shrink: 0;
    display: grid; place-items: center; font-size: 26px; color: #fff;
}
.chooser-ico.factura { background: linear-gradient(135deg, var(--success), #10b981); }
.chooser-ico.cotizacion { background: linear-gradient(135deg, var(--primary), #7c3aed); }
.chooser-txt { flex: 1; display: flex; flex-direction: column; }
.chooser-tit { font-weight: 700; font-size: 17px; }
.chooser-sub { font-size: 13px; color: var(--text-soft); }
.chooser-arrow { color: var(--text-soft); font-size: 18px; }
@media (min-width: 760px) { .chooser { grid-template-columns: 1fr 1fr; } }

/* Pestañas */
.tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.tab {
    display: inline-flex; align-items: center; gap: 6px;
    border: 1px solid var(--border); background: var(--surface);
    color: var(--text-soft); font-weight: 600; font-size: 14px; font-family: inherit;
    padding: 9px 16px; border-radius: 99px; cursor: pointer; transition: all .15s;
}
.tab:hover { color: var(--text); }
.tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* Badge de tipo */
.badge-tipo { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 99px; margin-right: 4px; }
.badge-tipo.is-factura { background: var(--success-soft); color: var(--success); }
.badge-tipo.is-cotizacion { background: var(--primary-soft); color: var(--primary); }

/* ---------- Desktop ---------- */
@media (min-width: 760px) {
    body { font-size: 15px; }
    .app-shell { padding-bottom: 30px; }
    .bottom-nav { display: none; }
    .container { padding: 26px 24px; }
    .stat-grid { grid-template-columns: repeat(4, 1fr); }
    .form-row.cols-2 { grid-template-columns: 1fr 1fr; }
    .form-row.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
    .resumen-3 { grid-template-columns: repeat(3, 1fr); }
    .cotizacion-layout { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; align-items: start; }
    .cotizacion-layout .col-resumen { position: sticky; top: 84px; }
    .modal-overlay { align-items: center; padding: 20px; }
    .modal { border-radius: 20px; }

    .topbar .desk-nav { display: flex; gap: 4px; }
    .topbar .desk-nav a {
        padding: 8px 14px; border-radius: 99px; font-size: 14px; font-weight: 600; color: var(--text-soft);
    }
    .topbar .desk-nav a.active { background: var(--primary-soft); color: var(--primary); }
    .topbar .desk-nav a:hover { color: var(--text); }
}
@media (max-width: 759px) {
    .topbar .desk-nav { display: none; }
}

/* ---------- Documento de impresión (hoja carta) ---------- */
.print-doc { display: none; }

@media print {
    /* Hoja carta 8.5 x 11 in */
    @page { size: letter; margin: 16mm; }

    html, body {
        background: #fff !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    .topbar, .bottom-nav, .no-print { display: none !important; }
    .app-shell { padding: 0 !important; min-height: 0 !important; }
    .container { max-width: none; padding: 0; margin: 0; }

    /* Solo se imprime el documento carta */
    .print-doc {
        display: block;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}
