:root {
    --bg: #f4f6fb;
    --card: #fff;
    --text: #1d2433;
    --muted: #6b7489;
    --line: #e3e7ef;
    --primary: #1f4fb8;
    --primary-d: #163c8f;
    --primary-l: #e8eefb;
    --green: #12a150;
    --green-l: #e5f7ed;
    --red: #d93a3a;
    --red-l: #fdecec;
    --amber: #b7791f;
    --amber-l: #fdf5e3;
    --side: #0d1b36;
    --side-t: #b8c3da;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}
* { box-sizing: border-box; }
html { font-size: 14px; }
body {
    margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    color: var(--text); background: var(--bg); line-height: 1.45;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 .6em; line-height: 1.25; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; margin-top: 1.4em; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: .9em; margin: .3em 0 1em; }

/* ---------- Layout ---------- */
.sidebar {
    position: fixed; inset: 0 auto 0 0; width: 250px; background: var(--side); color: var(--side-t);
    overflow-y: auto; z-index: 20; padding-bottom: 2rem;
}
.brand { display: flex; align-items: center; gap: .6rem; padding: 1.1rem 1.2rem; color: #fff; font-weight: 700; font-size: 1.1rem; }
.brand:hover { text-decoration: none; }
.logo {
    display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 8px;
    background: linear-gradient(135deg, #3d8bff, #1463ff); color: #fff; font-weight: 800;
}
.company { margin: 0 1rem .6rem; padding: .7rem .8rem; border-radius: 8px; background: rgba(255,255,255,.06); color: #fff; font-weight: 600; font-size: .92rem; }
.company small { display: block; color: var(--side-t); font-weight: 400; }
.nav-section { padding: 1rem 1.2rem .35rem; font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: #7d8aa8; }
.sidebar nav a {
    display: flex; align-items: center; gap: .65rem; padding: .48rem 1.2rem; color: var(--side-t);
    border-left: 3px solid transparent;
}
.sidebar nav a:hover { background: rgba(255,255,255,.05); color: #fff; text-decoration: none; }
.sidebar nav a.active { background: rgba(20,99,255,.18); color: #fff; border-left-color: #3d8bff; }
.ic { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.main { margin-left: 250px; min-height: 100vh; }
.topbar {
    position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 1rem;
    background: #fff; border-bottom: 1px solid var(--line); padding: .8rem 1.6rem;
}
.topbar h1 { font-size: 1.2rem; margin: 0; flex: 1; }
.topbar .user { display: flex; gap: 1rem; align-items: center; color: var(--muted); }
.menu-btn { display: none; border: 0; background: none; font-size: 1.4rem; cursor: pointer; }
.content { padding: 1.5rem 1.6rem 3rem; max-width: 1400px; }

/* ---------- Componentes ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.2rem 1.3rem; margin-bottom: 1.2rem; }
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }
.card.p0 { padding: 0; overflow: hidden; }
.card.p0 > h3 { padding: 1rem 1.2rem 0; }

.toolbar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .7rem; margin-bottom: 1rem; }
.toolbar .spacer { flex: 1; }
.toolbar label { margin: 0; }
.toolbar form { display: flex; gap: .6rem; align-items: flex-end; flex-wrap: wrap; }

.btn {
    display: inline-flex; align-items: center; gap: .4rem; border: 1px solid var(--line); background: #fff; color: var(--text);
    padding: .5rem .95rem; border-radius: 8px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: #f6f8fc; text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-d); }
.btn.danger { color: var(--red); border-color: #f3c4c4; }
.btn.danger:hover { background: var(--red-l); }
.btn.success { background: var(--green); border-color: var(--green); color: #fff; }
.btn.sm { padding: .28rem .6rem; font-size: .88rem; }
.btn.block { width: 100%; justify-content: center; padding: .7rem; margin-top: 1rem; }
.link-btn { background: none; border: 0; color: var(--primary); cursor: pointer; font: inherit; padding: 0; }
.link-btn.danger { color: var(--red); }

label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: .8rem; color: #39425a; }
input, select, textarea {
    display: block; width: 100%; margin-top: .3rem; padding: .5rem .65rem; border: 1px solid #cfd6e4; border-radius: 7px;
    font: inherit; font-weight: 400; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-l); border-color: var(--primary); }
input[readonly] { background: #f5f7fb; }
input.invalid { border-color: var(--red); background: var(--red-l); }
input[type=checkbox] { display: inline; width: auto; margin: 0 .4rem 0 0; }
label.check { display: flex; align-items: center; font-weight: 500; }
.money, .num, td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.grid2, .grid3, .grid4 { display: grid; gap: 0 1rem; }
.grid2 { grid-template-columns: repeat(2, 1fr); }
.grid3 { grid-template-columns: repeat(3, 1fr); }
.grid4 { grid-template-columns: repeat(4, 1fr); }
.span2 { grid-column: span 2; }
.form-actions { display: flex; gap: .6rem; margin-top: .6rem; }

table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: .55rem .75rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table th { background: #f7f9fc; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 700; }
.table tbody tr:hover { background: #fafbff; }
.table tfoot td, .table tr.total td { font-weight: 700; background: #f7f9fc; }
.table tr.group td { font-weight: 700; background: #fbfcfe; }
.table td.actions { white-space: nowrap; text-align: right; }
.table .indent1 { padding-left: 1.6rem; } .table .indent2 { padding-left: 2.6rem; } .table .indent3 { padding-left: 3.6rem; }
.table input, .table select { margin: 0; padding: .35rem .5rem; }
.table-wrap { overflow-x: auto; }
.empty { padding: 2rem; text-align: center; color: var(--muted); }

.badge { display: inline-block; padding: .12rem .55rem; border-radius: 99px; font-size: .78rem; font-weight: 700; background: #eef1f6; color: #4a5470; }
.badge.ok { background: var(--green-l); color: var(--green); }
.badge.warn { background: var(--amber-l); color: var(--amber); }
.badge.err { background: var(--red-l); color: var(--red); }
.badge.info { background: var(--primary-l); color: var(--primary); }

.alert { padding: .75rem 1rem; border-radius: 8px; margin-bottom: 1rem; border: 1px solid; }
.alert.ok { background: var(--green-l); border-color: #bfe8cf; color: #0b6b35; }
.alert.error { background: var(--red-l); border-color: #f3c4c4; color: #a52222; }
.alert.warn, .alert.info { background: var(--amber-l); border-color: #f1dfb2; color: #7a5212; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 1rem; margin-bottom: 1.2rem; }
.kpi { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.2rem; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: .85rem; font-weight: 600; }
.kpi .value { font-size: 1.55rem; font-weight: 750; margin-top: .25rem; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--muted); font-size: .82rem; margin-top: .2rem; }
.kpi.blue { border-top: 3px solid var(--primary); }
.kpi.green { border-top: 3px solid var(--green); }
.kpi.red { border-top: 3px solid var(--red); }
.kpi.amber { border-top: 3px solid var(--amber); }
.pos { color: var(--green); } .neg { color: var(--red); }

.tabs { display: flex; gap: .3rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; flex-wrap: wrap; }
.tabs a { padding: .55rem 1rem; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); }
.tabs a:hover { text-decoration: none; color: var(--text); }

/* Gráfico de barras simple */
.bars { display: flex; align-items: flex-end; gap: .5rem; height: 200px; padding-top: 1rem; }
.bars .col { flex: 1; display: flex; flex-direction: column; align-items: center; height: 100%; justify-content: flex-end; }
.bars .pair { display: flex; gap: 3px; align-items: flex-end; height: 100%; width: 100%; justify-content: center; }
.bars .bar { width: 42%; max-width: 22px; border-radius: 4px 4px 0 0; min-height: 2px; }
.bars .bar.v { background: var(--primary); } .bars .bar.c { background: #ffb547; }
.bars .lbl { font-size: .72rem; color: var(--muted); margin-top: .35rem; }
.legend { display: flex; gap: 1rem; font-size: .85rem; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: .3rem; }

.split { display: grid; grid-template-columns: 2fr 1fr; gap: 1.2rem; }
.diff-ok { color: var(--green); font-weight: 700; } .diff-bad { color: var(--red); font-weight: 700; }
dl.kv { display: grid; grid-template-columns: auto 1fr; gap: .35rem 1.2rem; margin: 0; }
dl.kv dt { color: var(--muted); } dl.kv dd { margin: 0; font-weight: 600; }

/* Documento imprimible (liquidación, comprobante) */
.doc-print { background: #fff; max-width: 820px; margin: 0 auto; padding: 2rem 2.2rem; border: 1px solid var(--line); border-radius: var(--radius); }
.doc-print .head { display: flex; justify-content: space-between; border-bottom: 2px solid var(--text); padding-bottom: .8rem; margin-bottom: 1rem; }
.doc-print h2 { text-align: center; margin: 1rem 0; letter-spacing: .04em; }
.doc-print .cols { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }
.doc-print .table th, .doc-print .table td { padding: .35rem .5rem; }
.doc-print .firmas { display: grid; grid-template-columns: 1fr 1fr; gap: 4rem; margin-top: 4rem; text-align: center; }
.doc-print .firmas div { border-top: 1px solid var(--text); padding-top: .4rem; }

/* ---------- Ratios financieros ---------- */
.ratios { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.ratio { background: #fff; border: 1px solid var(--line); border-left: 4px solid #cfd6e4; border-radius: var(--radius); padding: .9rem 1rem; box-shadow: var(--shadow); }
.ratio.ok { border-left-color: var(--green); } .ratio.warn { border-left-color: #f2994a; } .ratio.err { border-left-color: var(--red); }
.ratio-top { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.ratio .label { color: var(--muted); font-weight: 600; font-size: .88rem; }
.ratio .value { font-size: 1.6rem; font-weight: 750; margin: .25rem 0; font-variant-numeric: tabular-nums; }
.ratio .formula { font-size: .78rem; color: var(--primary); }
.ratio .exp { font-size: .82rem; color: var(--muted); margin-top: .3rem; }
.minibar { height: 6px; background: #eef1f6; border-radius: 3px; margin-top: .35rem; overflow: hidden; }
.minibar span { display: block; height: 100%; background: #ffb547; }

/* ---------- Barra de antigüedad de saldos ---------- */
.bars-h { display: flex; height: 18px; border-radius: 6px; overflow: hidden; background: #eef1f6; margin: 0 1.2rem; padding: 0 !important; }
.bars-h .seg { height: 100%; }
.seg-por_vencer { background: #12a150; } .seg-d30 { background: #f2c94c; } .seg-d60 { background: #f2994a; }
.seg-d90 { background: #eb5757; } .seg-d90m { background: #9b1c1c; }
.legend i.seg-por_vencer, .legend i.seg-d30, .legend i.seg-d60, .legend i.seg-d90, .legend i.seg-d90m { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: .3rem; }

/* ---------- Modo consulta (perfil sin permiso de edición) ---------- */
body.readonly .content form[method="post"], body.readonly .content a[href*="a=form"],
body.readonly .content a.btn.primary, body.readonly .content .btn.success { display: none !important; }

/* ---------- Selector de empresa ---------- */
.company-switch select { margin-top: .5rem; background: rgba(255,255,255,.1); color: #fff; border-color: rgba(255,255,255,.2); font-size: .85rem; padding: .35rem .5rem; }
.company-switch option { color: var(--text); }

/* ---------- Logo de la empresa ---------- */
.company-logo { background: #fff; border-radius: 6px; padding: .45rem; margin-bottom: .55rem; text-align: center; }
.company-logo img { max-width: 100%; max-height: 56px; object-fit: contain; display: inline-block; vertical-align: middle; }
.login-logo { text-align: center; margin-bottom: .4rem; }
.login-logo img { max-width: 230px; max-height: 170px; object-fit: contain; }
.login-empresa { text-align: center; font-size: 1.05rem !important; color: var(--muted); font-weight: 600; }
.brand-icon { width: 34px; height: 30px; object-fit: contain; }
.head-empresa { display: flex; align-items: center; gap: .9rem; }
.doc-logo { max-height: 60px; max-width: 160px; object-fit: contain; }
.print-logo { display: block; max-height: 50px; max-width: 160px; object-fit: contain; margin-bottom: .4rem; }
.logo-editor { display: flex; gap: 1.5rem; align-items: flex-start; flex-wrap: wrap; }
.logo-preview { width: 220px; height: 110px; border: 1px dashed #cfd6e4; border-radius: 8px; display: grid; place-items: center; background: #fafbfd; padding: .6rem; }
.logo-preview-img { max-width: 100%; max-height: 100%; object-fit: contain; }

/* ---------- Login / instalación ---------- */
body.auth { display: grid; place-items: center; min-height: 100vh; padding: 2rem 1rem; background: linear-gradient(160deg, #0f1b33 0%, #1d3a7a 100%); }
.auth-card { background: #fff; width: 100%; max-width: 400px; padding: 2rem; border-radius: 14px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.auth-card.wide { max-width: 720px; }
.auth-card h1 { font-size: 1.4rem; margin-top: 1rem; }
.brand-lg { display: flex; align-items: center; gap: .6rem; font-weight: 750; font-size: 1.2rem; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
    .sidebar { transform: translateX(-100%); transition: transform .2s; }
    .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.3); }
    .main { margin-left: 0; }
    .menu-btn { display: block; }
    .grid3, .grid4 { grid-template-columns: repeat(2, 1fr); }
    .split { grid-template-columns: 1fr; }
    .topbar .user span { display: none; }
    .content { padding: 1rem; }
}
@media (max-width: 560px) { .grid2, .grid3, .grid4 { grid-template-columns: 1fr; } .span2 { grid-column: auto; } }

/* ---------- Impresión ---------- */
@media print {
    .sidebar, .topbar, .no-print, .toolbar, .alert, .btn { display: none !important; }
    .main { margin: 0; }
    .content { padding: 0; max-width: none; }
    body { background: #fff; font-size: 11px; }
    .card, .doc-print { border: 0; box-shadow: none; padding: 0; }
    .print-only { display: block !important; }
    a { color: inherit; }
}
.print-only { display: none; }
