:root {
  --bg: #f4f5f8;
  --panel: #ffffff;
  --ink: #161b26;
  --ink-2: #444c5e;
  --muted: #6c7488;
  --line: #e3e6ed;
  --line-2: #eef0f4;
  --accent: #1f4fd1;
  --accent-ink: #ffffff;
  --accent-soft: #e8eefc;
  --ok: #157f4a; --ok-soft: #e3f4ea;
  --warn: #a15c00; --warn-soft: #fff1dc;
  --bad: #b42318; --bad-soft: #fdecea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131a; --panel: #171c25; --ink: #e8ebf1; --ink-2: #c1c7d3; --muted: #8b93a6; --line: #2a3140; --line-2: #222835;
    --accent-soft: #1d2a4d; --ok-soft: #12321f; --warn-soft: #3a2a10; --bad-soft: #3d1714; --ok: #4cc38a; --warn: #f0a447; --bad: #f97066;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.5 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; background: var(--bg); color: var(--ink); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; font-weight: 650; }
h2 { font-size: 16px; margin: 0 0 12px; font-weight: 620; }
h3 { font-size: 13px; margin: 0 0 8px; font-weight: 620; color: var(--ink-2); text-transform: uppercase; letter-spacing: .05em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); font-size: 12px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--bad); }
.pos { color: var(--ok); }

.layout { display: grid; grid-template-columns: 236px minmax(0, 1fr); min-height: 100vh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); padding: 18px 14px; display: flex; flex-direction: column; gap: 14px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; padding: 0 6px; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 800; }
.company-switch { display: grid; gap: 4px; padding: 0 6px; }
nav { display: flex; flex-direction: column; gap: 1px; overflow: auto; }
nav a { color: var(--ink-2); padding: 7px 10px; border-radius: 7px; font-weight: 500; }
nav a:hover { background: var(--line-2); }
nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); padding: 12px 10px 4px; font-weight: 600; }
.sidebar-foot { margin-top: auto; padding: 0 6px; }

.view { padding: 26px 32px 60px; max-width: 1280px; width: 100%; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card + .card { margin-top: 16px; }
.grid > .card + .card, .grid > div > .card:first-child { margin-top: 0; }
.page-head .actions select { width: auto; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 1000px) { .g3, .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  .view { padding: 18px 16px 48px; }
  .g2, .g3, .g4 { grid-template-columns: 1fr; }
}

.kpi { display: flex; flex-direction: column; gap: 4px; }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; }
.kpi .value { font-size: 24px; font-weight: 650; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; color: var(--muted); }

button, .btn { font: inherit; border: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: 7px 13px; border-radius: 8px; cursor: pointer; font-weight: 550; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
button:hover, .btn:hover { border-color: var(--muted); }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--bad); }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover { background: var(--line-2); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.sm { padding: 4px 9px; font-size: 12px; }

input, select, textarea { font: inherit; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; width: 100%; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
input[type=checkbox] { width: auto; }
textarea { min-height: 64px; resize: vertical; }
label.field { display: grid; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 500; }
label.field > span { display: flex; justify-content: space-between; gap: 8px; }
label.check { display: flex; gap: 8px; align-items: center; color: var(--ink-2); font-size: 13px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.form-grid .span2 { grid-column: span 2; }
.form-grid .span3 { grid-column: span 3; }
.form-grid .span4 { grid-column: 1 / -1; }
@media (max-width: 900px) { .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } .form-grid .span3 { grid-column: span 2; } }

.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.filters select, .filters input { width: auto; }
.filters input[type=search] { min-width: 220px; }

table.data { width: 100%; border-collapse: collapse; }
table.data th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data td { padding: 9px 10px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
table.data tbody tr.click { cursor: pointer; }
table.data tbody tr.click:hover { background: var(--line-2); }
table.data tfoot td { font-weight: 650; border-top: 1px solid var(--line); }
.table-wrap { overflow-x: auto; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 11.5px; font-weight: 600; background: var(--line-2); color: var(--ink-2); white-space: nowrap; }
.badge.pagada, .badge.ok, .badge.aceptado { background: var(--ok-soft); color: var(--ok); }
.badge.pendiente, .badge.parcial { background: var(--warn-soft); color: var(--warn); }
.badge.vencida, .badge.anulada, .badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.borrador { background: var(--line-2); color: var(--muted); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }

.lines-table input { padding: 5px 7px; }
.lines-table td { padding: 5px 4px; }
.lines-table .desc { min-width: 260px; }
.lines-table textarea { min-height: 34px; padding: 5px 7px; }

.totals-box { margin-left: auto; min-width: 300px; }
.totals-box table { width: 100%; }
.totals-box td { padding: 4px 0; }
.totals-box tr.grand td { font-size: 18px; font-weight: 700; border-top: 1px solid var(--line); padding-top: 8px; }

.notice { border-radius: 8px; padding: 10px 14px; background: var(--accent-soft); color: var(--ink); font-size: 13px; }
.notice.warn { background: var(--warn-soft); }
.notice.bad { background: var(--bad-soft); }
.notice.ok { background: var(--ok-soft); }
.notice + .notice { margin-top: 8px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; flex-wrap: wrap; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 8px 12px; color: var(--muted); }
.tabs button.active { border-bottom-color: var(--accent); color: var(--accent); }

.casillas td:first-child { font-family: var(--mono); color: var(--muted); width: 56px; }
.casillas tr.total td { font-weight: 700; background: var(--line-2); }
.casillas tr.section td { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding-top: 16px; font-weight: 600; border-bottom: 1px solid var(--line); }

.chart { width: 100%; height: 220px; }
.chart text { fill: var(--muted); font-size: 11px; }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }

.modal { position: fixed; inset: 0; background: rgba(10, 14, 22, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-box { background: var(--panel); border-radius: 12px; width: min(640px, 100%); max-height: 90vh; overflow: auto; padding: 22px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.modal-box .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--panel); padding: 10px 18px; border-radius: 9px; z-index: 60; max-width: min(640px, 90vw); box-shadow: 0 8px 24px rgba(0,0,0,.2); }
.toast.error { background: var(--bad); color: #fff; }

.steps { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.steps span { padding: 4px 12px; border-radius: 99px; background: var(--line-2); color: var(--muted); font-size: 12px; font-weight: 600; }
.steps span.on { background: var(--accent); color: var(--accent-ink); }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }

/* Lectura automática de facturas (OCR) */
.dropzone { border: 2px dashed var(--line); border-radius: var(--radius); background: var(--panel); padding: 18px 20px; margin-bottom: 16px; transition: border-color .15s, background .15s; }
.dropzone.slim { padding: 12px 16px; font-size: 13px; cursor: pointer; }
.dropzone.slim:hover { border-color: var(--accent); }
.dz-main { display: flex; gap: 14px; align-items: center; }
.dz-icon { width: 40px; height: 40px; flex: none; border-radius: 10px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-size: 22px; font-weight: 700; }
.dropzone.busy .dz-icon { animation: dzpulse 1.2s ease-in-out infinite; }
@keyframes dzpulse { 50% { opacity: .4; } }
body.dragging .dropzone { border-color: var(--accent); background: var(--accent-soft); }
body.dragging .view::before { content: "Suelta aquí para leer la factura"; position: fixed; inset: 12px 12px 12px 248px; border: 3px dashed var(--accent); border-radius: 14px;
  background: color-mix(in srgb, var(--accent-soft) 88%, transparent); color: var(--accent); display: grid; place-items: center; font-size: 22px; font-weight: 650; z-index: 40; pointer-events: none; }
@media (max-width: 760px) { body.dragging .view::before { inset: 12px; } }
#dzStatus:not(:empty) { margin-top: 14px; }
.dz-progress { display: grid; gap: 6px; font-size: 13px; color: var(--ink-2); }
.dz-progress .bar { height: 6px; border-radius: 99px; background: var(--line-2); overflow: hidden; }
.dz-progress .bar i { display: block; height: 100%; background: var(--accent); transition: width .2s; }
.dz-result { display: grid; gap: 10px; }
.dz-warn { margin: 0; padding: 8px 12px 8px 28px; background: var(--warn-soft); border-radius: 8px; font-size: 13px; }
.dz-doc iframe, .dz-doc img { width: 100%; height: 560px; border: 1px solid var(--line); border-radius: 8px; background: #fff; object-fit: contain; }
.dz-raw { max-height: 260px; overflow: auto; background: var(--line-2); padding: 10px; border-radius: 8px; font-size: 11.5px; white-space: pre-wrap; margin: 0; }
.autofilled { background: var(--accent-soft) !important; border-color: color-mix(in srgb, var(--accent) 45%, var(--line)) !important; }

/* Usuarios y acceso */
body.ro .w { display: none !important; }
.user-box { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); display: grid; gap: 2px; color: var(--ink-2); }
.user-box span { font-size: 11.5px; color: var(--muted); }
.user-box .row { margin-top: 4px; }
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth-box { width: min(420px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.auth-box .brand { padding: 0; }
.auth-form { display: grid; gap: 12px; }
.auth-form button.primary { justify-content: center; padding: 10px; }
