:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --ink: #1d1d1b;
  --muted: #6b6a66;
  --line: #e3e1db;
  --accent: #b5281c;      /* rojo sobrio */
  --accent-ink: #ffffff;
  --ok: #1f7a4d;
  --ok-bg: #e5f3ea;
  --warn-bg: #fdf3dc;
  --err-bg: #fbe6e3;
  --radius: 8px;
  font-family: ui-sans-serif, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); line-height: 1.45; }
a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: 0 0 1rem; letter-spacing: -0.01em; }
h3 { margin: 1.4rem 0 .6rem; font-size: 1rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
code { background: #efede8; padding: 1px 5px; border-radius: 4px; font-size: .9em; word-break: break-all; }
small, .muted, .hint { color: var(--muted); }
.hint { font-size: .88rem; }

.topbar { display: flex; align-items: center; gap: 1.5rem; padding: .8rem 1.5rem; background: var(--ink); color: #fff; flex-wrap: wrap; }
.topbar .brand { color: #fff; font-weight: 700; text-decoration: none; letter-spacing: .02em; }
.topbar nav { display: flex; gap: 1.1rem; flex: 1; flex-wrap: wrap; }
.topbar nav a { color: #d8d6d0; text-decoration: none; }
.topbar nav a:hover { color: #fff; }
.logout { display: flex; gap: .6rem; align-items: center; margin: 0; }
.who { color: #a9a7a1; font-size: .85rem; }
button.link { background: none; border: 0; color: #fff; cursor: pointer; text-decoration: underline; font: inherit; }

main { max-width: 1040px; margin: 0 auto; padding: 2rem 1.25rem 4rem; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head h1 { margin: 0; display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }

.btn { display: inline-block; background: var(--accent); color: var(--accent-ink); border: 0; border-radius: var(--radius);
  padding: .6rem 1.1rem; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; }
.btn:hover { filter: brightness(.94); }
.btn.secondary { background: var(--surface); color: var(--ink); border: 1px solid var(--line); }
.btn.small { padding: .35rem .7rem; font-size: .88rem; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.5rem; margin-bottom: 1.25rem; }
.card.center { text-align: center; max-width: 520px; margin: 3rem auto; }
.auth { max-width: 400px; margin: 3rem auto; }
.auth label { display: block; margin-bottom: .9rem; }

label { display: flex; flex-direction: column; gap: .3rem; font-size: .88rem; font-weight: 600; }
label small { font-weight: 400; }
input, select, textarea { font: inherit; padding: .55rem .65rem; border: 1px solid var(--line); border-radius: 6px; background: #fff; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.grid .wide { grid-column: 1 / -1; }
form.inline { display: inline-flex; gap: .5rem; align-items: center; margin: 0 0 1rem; }
form.inline select { width: auto; }

.flash { padding: .75rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; border: 1px solid transparent; }
.flash.ok { background: var(--ok-bg); border-color: #bfe0cc; }
.flash.err { background: var(--err-bg); border-color: #f0c4bd; }
.flash.warn, .flash.info { background: var(--warn-bg); border-color: #efdcae; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.stats div { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.2rem; }
.stats span { display: block; color: var(--muted); font-size: .85rem; }
.stats strong { font-size: 1.4rem; font-variant-numeric: tabular-nums; }

.filters { display: flex; gap: .5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.filters input { max-width: 280px; }
.filters select { width: auto; }

table { border-collapse: collapse; width: 100%; }
table.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
table.list th, table.list td { padding: .65rem .9rem; border-bottom: 1px solid var(--line); text-align: left; }
table.list th { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); background: #faf9f6; }
table.list tr:last-child td { border-bottom: 0; }
.r { text-align: right !important; font-variant-numeric: tabular-nums; }
.empty { text-align: center; color: var(--muted); padding: 2rem !important; }

.badge { font-size: .75rem; font-weight: 700; padding: .2rem .55rem; border-radius: 999px; text-transform: uppercase; letter-spacing: .04em; vertical-align: middle; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.pending { background: var(--warn-bg); color: #8a5a00; }

.edit-lines th { text-align: left; font-size: .8rem; color: var(--muted); padding: 0 .3rem .3rem; font-weight: 600; }
.edit-lines td { padding: .25rem .3rem; }
.edit-lines td:first-child { width: 42%; }
.edit-lines td:last-child select { min-width: 86px; }
#add-line { margin: .5rem 0 1.5rem; }
.edit-lines input { min-width: 70px; }
.edit-lines .num { text-align: right; }
.preview { display: flex; gap: 1.5rem; justify-content: flex-end; flex-wrap: wrap; padding: 1rem; background: #faf9f6; border-radius: 6px; margin: 1rem 0; font-variant-numeric: tabular-nums; }
.preview .grand { font-size: 1.15rem; }

/* ---- Documento de factura ---- */
.invoice { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 2.5rem; margin-bottom: 1.5rem; }
.inv-head { display: flex; justify-content: space-between; gap: 2rem; border-bottom: 3px solid var(--ink); padding-bottom: 1.2rem; margin-bottom: 1.5rem; flex-wrap: wrap; }
.inv-title { font-size: 2rem; font-weight: 800; letter-spacing: .12em; }
.inv-number { font-size: 1.05rem; color: var(--accent); font-weight: 700; }
.inv-dates { display: grid; gap: .35rem; text-align: right; }
.inv-dates span { display: block; font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.parties { display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin-bottom: 2rem; }
.parties h4 { margin: 0 0 .4rem; font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.lines th { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); text-align: left; border-bottom: 1px solid var(--ink); padding: .5rem .4rem; }
.lines td { padding: .6rem .4rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.totals-wrap { display: flex; justify-content: space-between; gap: 2rem; margin-top: 1.5rem; flex-wrap: wrap; align-items: flex-start; }
.vat { width: auto; font-size: .88rem; }
.vat th { font-size: .7rem; text-transform: uppercase; color: var(--muted); padding: .3rem .8rem .3rem 0; text-align: left; }
.vat td { padding: .25rem .8rem .25rem 0; font-variant-numeric: tabular-nums; }
.totals { width: 300px; }
.totals td { padding: .35rem 0; font-variant-numeric: tabular-nums; }
.totals td:last-child { text-align: right; }
.totals .grand td { border-top: 2px solid var(--ink); font-size: 1.2rem; font-weight: 800; padding-top: .6rem; }
.legal { margin-top: 1.5rem; font-weight: 600; }
.notes { color: var(--muted); font-size: .9rem; }
.registry { color: var(--muted); font-size: .75rem; margin-top: 2rem; }

.table-scroll { overflow-x: auto; }
.lines td, .lines th { white-space: nowrap; }
.lines td:first-child { white-space: normal; min-width: 140px; }
@media (max-width: 640px) {
  .who { display: none; }
  .topbar { gap: .6rem 1rem; padding: .7rem 1rem; }
  .topbar nav a { white-space: nowrap; }
  .edit-lines { display: block; overflow-x: auto; }
  .edit-lines td:first-child input { min-width: 180px; }
  .parties { grid-template-columns: 1fr; }
  .inv-dates { text-align: left; }
  .invoice { padding: 1.25rem; }
  .totals { width: 100%; }
  table.list { display: block; overflow-x: auto; }
}

@media print {
  body { background: #fff; }
  .no-print, .topbar, .flash { display: none !important; }
  main { padding: 0; max-width: none; }
  .invoice { border: 0; padding: 0; }
  .table-scroll { overflow: visible; }
  a { color: inherit; text-decoration: none; }
}
