:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e2e6ec;
  --text: #17202c;
  --muted: #627080;
  --accent: #1f5fbf;
  --accent-soft: #e7effb;
  --ok: #157a4a;
  --ok-soft: #e3f4ea;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --danger: #b4232f;
  --danger-soft: #fbe6e8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 50, .06), 0 2px 8px rgba(20, 30, 50, .05);
  --sidebar: 248px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141b;
    --surface: #171e27;
    --surface-2: #1c2430;
    --border: #2a3441;
    --text: #e6ebf1;
    --muted: #94a1b2;
    --accent: #5b9bf0;
    --accent-soft: #1c2c45;
    --ok: #4cc38a;
    --ok-soft: #15301f;
    --warn: #f0b34e;
    --warn-soft: #33270f;
    --danger: #f2727b;
    --danger-soft: #3a1a1e;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.strong { font-weight: 600; }
.hidden { display: none !important; }
.nowrap { white-space: nowrap; }

/* Layout */
.layout { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 12px; font-weight: 700; }
.brand img { width: 30px; height: 30px; }
.company-switch { margin: 0 4px 12px; }
.company-switch select { width: 100%; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 8px; color: var(--text); font-weight: 500;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav svg { width: 18px; height: 18px; flex: none; }
.nav-sep { height: 1px; background: var(--border); margin: 8px 6px; }
.sidebar .user-box { margin-top: auto; padding: 10px 8px 0; border-top: 1px solid var(--border); font-size: .85rem; }
.main { padding: 24px 28px 80px; min-width: 0; }
.topbar { display: none; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { margin: 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card + .card { margin-top: 16px; }
.grid > .card + .card { margin-top: 0; }
.card-title { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 12px; }
.card-title h2 { margin: 0; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.col-2 { grid-column: span 2; }
.kpi { padding: 16px 18px; }
.kpi .label { color: var(--muted); font-size: .82rem; font-weight: 500; }
.kpi .value { font-size: 1.45rem; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--muted); font-size: .8rem; margin-top: 2px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer;
  min-height: 38px; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 4px 10px; min-height: 30px; font-size: .85rem; }
.btn svg { width: 16px; height: 16px; }
.icon-btn { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 6px; border-radius: 6px; font-size: 1.1rem; line-height: 1; }
.icon-btn:hover { background: var(--surface-2); color: var(--danger); }

/* Forms */
label { display: block; font-size: .83rem; font-weight: 500; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; min-height: 38px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
input[type=checkbox] { width: auto; min-height: 0; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: .9rem; font-weight: 400; margin: 0; cursor: pointer; }
.form-grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 12px 14px; }
.form-grid > * { grid-column: span 12; }
.span-2 { grid-column: span 2 !important; }
.span-3 { grid-column: span 3 !important; }
.span-4 { grid-column: span 4 !important; }
.span-5 { grid-column: span 5 !important; }
.span-6 { grid-column: span 6 !important; }
.span-8 { grid-column: span 8 !important; }
.hint { font-size: .78rem; color: var(--muted); margin-top: 3px; }
.hint.warn { color: var(--warn); }
.field-error { border-color: var(--danger) !important; }
fieldset { border: 0; margin: 0; padding: 0; }
.section-title { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; margin: 4px 0 -2px; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--surface-2); }
tfoot td { font-weight: 600; background: var(--surface-2); }
.empty { padding: 32px; text-align: center; color: var(--muted); }

/* Badges */
.badge { display: inline-block; font-size: .74rem; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge.info { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

/* Filters */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.filters > * { width: auto; flex: 0 1 auto; }
.filters input[type=search] { flex: 1 1 220px; min-width: 160px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs a { padding: 9px 14px; color: var(--muted); font-weight: 500; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a.active { color: var(--accent); border-color: var(--accent); }
.tabs a:hover { text-decoration: none; color: var(--text); }

/* Invoice editor */
.lines-table td { padding: 6px 4px; border: 0; }
.lines-table th { padding: 6px 4px; background: transparent; }
.lines-table input, .lines-table select, .lines-table textarea { min-height: 36px; padding: 6px 8px; }
.lines-table textarea { min-height: 36px; height: 36px; }
.lines-table .c-desc { width: 40%; }
.lines-table .c-qty { width: 9%; }
.lines-table .c-price { width: 13%; }
.lines-table .c-dto { width: 8%; }
.lines-table .c-vat { width: 10%; }
.lines-table .c-amount { width: 13%; }
.totals { margin-left: auto; max-width: 360px; width: 100%; }
.totals .row { display: flex; justify-content: space-between; padding: 5px 0; font-variant-numeric: tabular-nums; }
.totals .row.total { border-top: 2px solid var(--text); margin-top: 6px; padding-top: 10px; font-size: 1.15rem; font-weight: 700; }
.doc-meta { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
.doc-meta .k { font-size: .78rem; color: var(--muted); }
.doc-meta .v { font-weight: 600; }
.pdf-frame { width: 100%; height: 75vh; border: 1px solid var(--border); border-radius: 8px; background: #fff; }

/* Tax boxes */
.tax-table td.box { width: 70px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tax-table tr.strong td { font-weight: 700; background: var(--surface-2); }
.result { font-size: 1.6rem; font-weight: 700; }
.result.pay { color: var(--danger); }
.result.refund { color: var(--ok); }
.notice { padding: 12px 14px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); font-size: .88rem; }
.notice.info { background: var(--accent-soft); color: var(--accent); }
.notice.ok { background: var(--ok-soft); color: var(--ok); }

/* Chart */
.bars { display: flex; align-items: flex-end; gap: 6px; height: 180px; padding-top: 10px; }
.bars .m { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; min-width: 0; }
.bars .pair { display: flex; gap: 2px; align-items: flex-end; height: 100%; width: 100%; justify-content: center; }
.bars .b { width: 42%; max-width: 16px; border-radius: 4px 4px 0 0; min-height: 2px; }
.bars .b.in { background: var(--accent); }
.bars .b.out { background: #e0a43a; }
.bars .lbl { font-size: .7rem; color: var(--muted); }
.legend { display: flex; gap: 14px; font-size: .8rem; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.list-plain { list-style: none; margin: 0; padding: 0; }
.list-plain li { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.list-plain li:last-child { border-bottom: 0; }

/* Auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth .card { width: 100%; max-width: 440px; padding: 28px; }
.auth.wide .card { max-width: 720px; }
.auth h1 { text-align: center; }
.auth .brand { justify-content: center; font-size: 1.1rem; }

/* Modal & toasts */
.modal-bg { position: fixed; inset: 0; background: rgba(10, 15, 25, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: 12px; width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto; padding: 20px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.modal.lg { max-width: 820px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-head h2 { margin: 0; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
#toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.2); max-width: 360px; font-size: .9rem; }
.toast.error { background: var(--danger); color: #fff; }
.fab { display: none; }
.drawer-bg { display: none; }

/* Editor de diseño de documentos */
.design-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.design-layout > div > .card + .card { margin-top: 16px; }
.preview-card { position: sticky; top: 16px; }
.preview-card .pdf-frame { height: 78vh; }
.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.tpl { text-align: left; background: var(--surface); border: 2px solid var(--border); border-radius: 10px; padding: 10px; cursor: pointer; color: var(--text); font: inherit; }
.tpl:hover { border-color: var(--accent); }
.tpl.active { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.tpl .small { margin-top: 2px; line-height: 1.3; }
.tpl-thumb { background: #fff; border: 1px solid #e3e6eb; border-radius: 6px; height: 104px; padding: 10px 9px; margin-bottom: 8px; display: flex; flex-direction: column; gap: 5px; overflow: hidden; position: relative; }
.tpl-thumb > div { border-radius: 2px; }
.tp-band { position: absolute; left: 0; right: 0; top: 0; height: 6px; background: var(--accent); }
.tp-head { height: 12px; width: 55%; background: #cfd5de; margin-bottom: 4px; }
.tp-head.plain { background: #2b2f36; height: 9px; width: 45%; }
.tp-head.small { height: 8px; width: 45%; }
.tp-title.big { height: 14px; width: 60%; margin-left: auto; margin-top: 6px; background: var(--accent); }
.tp-row { height: 8px; background: #eef0f3; }
.tp-row.solid { background: var(--accent); }
.tp-row.tint { background: var(--accent-soft); }
.tp-row.zebra { background: #f6f7f9; }
.tp-row.line { background: transparent; border-bottom: 1.5px solid #2b2f36; }
.tp-row.thin { background: transparent; border-bottom: 1px solid #e5e7eb; }
.tp-row.small { height: 5px; }
.tp-total { height: 11px; width: 45%; margin-left: auto; margin-top: auto; }
.tp-total.solid { background: var(--accent); }
.tp-total.tint { background: var(--accent-soft); border: 1px solid var(--accent); }
.tp-total.line { border-top: 2px solid #2b2f36; }
.tp-total.small { height: 8px; }
#kind-tabs { margin-top: 4px; }
@media (max-width: 1100px) {
  .design-layout { grid-template-columns: 1fr; }
  .preview-card { position: static; }
}

/* Responsive */
@media (max-width: 1100px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (min-width: 861px) and (max-width: 1250px) {
  .form-grid > .span-2 { grid-column: span 4 !important; }
  .form-grid > .span-3 { grid-column: span 4 !important; }
  .form-grid > .span-5 { grid-column: span 8 !important; }
}

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; width: 280px; z-index: 40; height: 100%;
    transform: translateX(-100%); transition: transform .2s ease;
  }
  body.drawer-open .sidebar { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.25); }
  body.drawer-open .drawer-bg { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 39; }
  .topbar {
    display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 30;
    background: var(--surface); border-bottom: 1px solid var(--border); padding: 10px 14px;
    padding-top: max(10px, env(safe-area-inset-top));
  }
  .topbar .title { font-weight: 700; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .topbar button { border: 0; background: transparent; color: var(--text); padding: 6px; cursor: pointer; }
  .topbar svg { width: 24px; height: 24px; }
  .main { padding: 16px 16px 96px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .col-2 { grid-column: auto; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi { padding: 12px 14px; }
  .kpi .value { font-size: 1.15rem; }
  .form-grid > [class*=span-] { grid-column: span 12 !important; }
  .form-grid > .span-2, .form-grid > .span-3 { grid-column: span 6 !important; }
  .page-head h1 { font-size: 1.25rem; }
  .fab {
    display: grid; place-items: center; position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom));
    width: 56px; height: 56px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 28px;
    box-shadow: 0 6px 18px rgba(31, 95, 191, .4); z-index: 20; border: 0;
  }
  .fab:hover { text-decoration: none; }

  /* Tablas como tarjetas */
  table.responsive thead { display: none; }
  table.responsive, table.responsive tbody, table.responsive tr, table.responsive td { display: block; width: 100%; }
  table.responsive tr { border: 1px solid var(--border); border-radius: 10px; margin-bottom: 10px; padding: 8px 12px; background: var(--surface); }
  table.responsive td { border: 0; padding: 4px 0; display: flex; justify-content: space-between; gap: 12px; text-align: right; }
  table.responsive td::before { content: attr(data-label); color: var(--muted); font-size: .8rem; text-align: left; flex: none; }
  table.responsive td.primary { font-weight: 600; font-size: 1rem; text-align: left; }
  table.responsive td.primary::before { display: none; }
  table.responsive td:empty { display: none; }
  table.responsive tfoot { display: block; }
  table.responsive tfoot tr { display: flex; flex-wrap: wrap; }
  .card.flat-mobile { background: transparent; border: 0; box-shadow: none; padding: 0; }

  /* Líneas de factura apiladas */
  .lines-table thead { display: none; }
  .lines-table, .lines-table tbody { display: block; }
  .lines-table tr { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; padding: 10px; border: 1px solid var(--border); border-radius: 10px; margin-bottom: 10px; }
  .lines-table td { padding: 0; width: auto !important; }
  .lines-table td.c-desc { grid-column: 1 / -1; }
  .lines-table td.c-qty { grid-column: span 2; }
  .lines-table td.c-price { grid-column: span 2; }
  .lines-table td.c-dto { grid-column: span 2; }
  .lines-table td.c-vat { grid-column: span 3; }
  .lines-table td.c-amount { grid-column: span 2; align-self: center; }
  .lines-table td.c-del { grid-column: span 1; text-align: right; }
  .lines-table.exp tr { grid-template-columns: 1fr 1fr 1fr auto; }
  .lines-table.exp td { grid-column: auto !important; }
  .lines-table td[data-label]::before { content: attr(data-label); display: block; font-size: .72rem; color: var(--muted); }
  .totals { max-width: none; }
  .desktop-only { display: none !important; }
  #toasts { left: 16px; right: 16px; bottom: 86px; }
  .modal { padding: 16px; }
}

@media print {
  .sidebar, .topbar, .fab, .actions, .filters { display: none !important; }
  .layout { display: block; }
  .main { padding: 0; }
}
