:root {
  --night: #0f1226; --night-2: #151935; --indigo: #232a5c;
  --blue: #3d5afe; --blue-soft: #eef1ff; --teal: #00a88f; --teal-soft: #e3f8f4;
  --coral: #e5484d; --coral-soft: #fdecee; --amber: #b7791f; --amber-soft: #fdf4e3;
  --ink: #0f1226; --ink-2: #4a5078; --mute: #8a90b8; --line: #e6e8f3; --bg: #f6f7fc; --card: #fff;
  --grad: linear-gradient(135deg, #3d5afe, #00d1b2);
  --font: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: "Cascadia Mono", Consolas, ui-monospace, monospace;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.5 var(--font); color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; letter-spacing: -.01em; }
.muted { color: var(--mute); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* ---------- Botones y formularios ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 14px; border-radius: 9px;
  border: 1px solid var(--line); background: #fff; color: var(--ink); font: 600 13px var(--font); cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--bg); text-decoration: none; }
.btn.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--coral); }
.btn.sm { height: 30px; padding: 0 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: default; }
.btn i svg { width: 15px; height: 15px; }
.icon-btn { width: 34px; height: 34px; border: 0; border-radius: 8px; background: transparent; color: inherit; cursor: pointer; display: grid; place-items: center; }
.icon-btn:hover { background: rgba(255,255,255,.08); }
.icon-btn svg { width: 17px; height: 17px; }

input, select, textarea {
  width: 100%; height: 36px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
  font: 14px var(--font); color: var(--ink); outline: none;
}
textarea { height: auto; min-height: 90px; padding: 8px 10px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(61,90,254,.15); }
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--blue); }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; font-weight: 600; color: var(--ink-2); }
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; font-size: 13px; color: var(--ink); }
.error { color: var(--coral); font-size: 13px; min-height: 18px; }

/* ---------- Login ---------- */
.login { min-height: 100%; display: grid; place-items: center; background: radial-gradient(70% 60% at 20% 10%, rgba(61,90,254,.35), transparent), var(--night); padding: 16px; }
.login-card { width: 360px; max-width: 100%; background: #fff; border-radius: 18px; padding: 32px; display: flex; flex-direction: column; gap: 14px; box-shadow: 0 30px 80px rgba(0,0,0,.35); }
.login-card h1 { font-size: 22px; }
.login-card p { margin: -8px 0 6px; }

/* ---------- Estructura ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100%; }
.sidebar { background: var(--night); color: #c9cdf2; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; padding: 16px 12px; }
.brand { display: flex; align-items: center; gap: 10px; color: #fff; font: 800 19px var(--font); padding: 6px 10px 18px; letter-spacing: -.3px; }
.brand:hover { text-decoration: none; }
.brand img { width: 28px; height: 28px; }
nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow: auto; }
nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 9px; color: #c9cdf2; font-weight: 500; }
nav a:hover { background: rgba(255,255,255,.06); text-decoration: none; }
nav a.active { background: rgba(61,90,254,.25); color: #fff; }
nav a svg, .me svg { width: 17px; height: 17px; }
.me { display: flex; align-items: center; gap: 8px; padding: 12px 10px 4px; border-top: 1px solid rgba(255,255,255,.08); color: #fff; }
.me > div { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.main { padding: 28px 32px 60px; min-width: 0; }

.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { font-size: 24px; flex: 1; min-width: 200px; }
.page-head .sub { font-size: 13px; color: var(--mute); font-weight: 400; margin-top: 2px; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar input, .toolbar select { width: auto; min-width: 160px; }
.toolbar .grow { flex: 1; min-width: 220px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 18px; }
.card h2 { font-size: 15px; margin-bottom: 12px; display: flex; align-items: center; gap: 8px; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 16px; }
.kpi .v { font: 700 24px var(--font); letter-spacing: -.5px; margin-top: 4px; }
.kpi .l { font-size: 12px; color: var(--mute); font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }

/* ---------- Tablas ---------- */
.table-wrap { background: #fff; border: 1px solid var(--line); border-radius: 14px; overflow: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--mute); font-weight: 600; padding: 10px 12px; border-bottom: 1px solid var(--line); background: #fafbfe; white-space: nowrap; }
td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tr.click { cursor: pointer; }
tr.click:hover td { background: #f8f9ff; }
.empty { padding: 28px; text-align: center; color: var(--mute); }
.card .table-wrap { border: 0; border-radius: 0; margin: 0 -18px -18px; }

.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--bg); color: var(--ink-2); }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.ok { background: var(--teal-soft); color: var(--teal); }
.badge.warn { background: var(--amber-soft); color: var(--amber); }
.badge.bad { background: var(--coral-soft); color: var(--coral); }
.badge.info { background: var(--blue-soft); color: var(--blue); }

/* ---------- Pestañas ---------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 10px 14px; font: 600 13px var(--font); color: var(--mute); cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--ink); border-bottom-color: var(--blue); }

.dl { display: grid; grid-template-columns: 150px 1fr; gap: 8px 16px; margin: 0; }
.dl dt { color: var(--mute); font-size: 13px; }
.dl dd { margin: 0; word-break: break-word; }

.timeline { display: flex; flex-direction: column; gap: 10px; }
.timeline .item { display: flex; gap: 12px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.timeline .item .kind { font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--blue); min-width: 70px; }
.timeline .item .body { flex: 1; white-space: pre-wrap; }
.timeline .item.done { opacity: .6; }

/* ---------- Gráfico ---------- */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: 220px; overflow: visible; }
.chart .tip { position: absolute; pointer-events: none; background: var(--night); color: #fff; font-size: 12px; padding: 6px 9px; border-radius: 8px; transform: translate(-50%, -110%); white-space: nowrap; opacity: 0; transition: opacity .1s; }

/* ---------- Editor de factura ---------- */
.lines input { height: 32px; }
.lines td { padding: 6px 6px; }
.totals { margin-left: auto; width: 320px; max-width: 100%; display: grid; grid-template-columns: 1fr auto; gap: 6px 16px; margin-top: 14px; }
.totals .big { font-size: 18px; font-weight: 700; border-top: 1px solid var(--ink); padding-top: 6px; }
.hash { font: 11px var(--mono); color: var(--mute); word-break: break-all; }

/* ---------- Modal ---------- */
.modal-wrap { position: fixed; inset: 0; background: rgba(15,18,38,.5); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 50; overflow: auto; }
.modal { width: 640px; max-width: 100%; background: #fff; border-radius: 16px; box-shadow: 0 30px 80px rgba(0,0,0,.3); }
.modal header { display: flex; align-items: center; padding: 18px 20px; border-bottom: 1px solid var(--line); }
.modal header h3 { flex: 1; font-size: 17px; }
.modal header .icon-btn:hover { background: var(--bg); }
.modal .body { padding: 20px; display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; }
.modal .body > * { grid-column: span 6; }
.modal .body .c2 { grid-column: span 2; }
.modal .body .c3 { grid-column: span 3; }
.modal .body .c4 { grid-column: span 4; }
.modal .body h4 { margin: 6px 0 -4px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--mute); }
.modal footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--line); align-items: center; }
.modal footer .error { flex: 1; }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px); background: var(--night); color: #fff; padding: 10px 16px; border-radius: 10px; opacity: 0; transition: .2s; z-index: 60; pointer-events: none; max-width: 90vw; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--coral); }

.list-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list-row:last-child { border-bottom: 0; }
.list-row .grow { flex: 1; min-width: 0; }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  nav { flex-direction: row; flex-wrap: wrap; }
  .g2, .g3 { grid-template-columns: 1fr; }
  .main { padding: 20px 16px; }
  .modal .body .c2, .modal .body .c3, .modal .body .c4 { grid-column: span 6; }
  .dl { grid-template-columns: 1fr; }
}
