/* Base sobria para la herramienta interna; sin framework por ahora.
   Tema claro por defecto; oscuro si el sistema lo pide o si el usuario lo elige (data-theme). */
:root {
  --ink: #1f2933;
  --muted: #616e7c;
  --line: #d9e2ec;
  --surface: #ffffff;
  --ground: #f5f7fa;
  --accent: #0b5fa5;
  --hover: #eef3f8;
  --ok-bg: #e3f6e8; --ok-ink: #1e4620;
  --bad-bg: #fdecea; --bad-ink: #611a15;
  --warn-bg: #fff4d6; --warn-ink: #6b4b00;
  --draft-bg: #e8edf3; --draft-ink: #3b4a5a;
  --shadow: rgba(0,0,0,.08);
  --chevron: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path d='M2 4l4 4 4-4' fill='none' stroke='%233b4a5a' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
  --ink: #e6edf3;
  --muted: #9aa7b4;
  --line: #2b3a4a;
  --surface: #16212c;
  --ground: #0f171f;
  --accent: #5aa9ec;
  --hover: #1f2d3b;
  --ok-bg: #173524; --ok-ink: #9fe0b0;
  --bad-bg: #42201d; --bad-ink: #f3b4ad;
  --warn-bg: #3d3012; --warn-ink: #f3d58a;
  --draft-bg: #263241; --draft-ink: #c3cdd8;
  --shadow: rgba(0,0,0,.45);
  --chevron: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path d='M2 4l4 4 4-4' fill='none' stroke='%23b7c2cf' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --ink: #e6edf3;
  --muted: #9aa7b4;
  --line: #2b3a4a;
  --surface: #16212c;
  --ground: #0f171f;
  --accent: #5aa9ec;
  --hover: #1f2d3b;
  --ok-bg: #173524; --ok-ink: #9fe0b0;
  --bad-bg: #42201d; --bad-ink: #f3b4ad;
  --warn-bg: #3d3012; --warn-ink: #f3d58a;
  --draft-bg: #263241; --draft-ink: #c3cdd8;
  --shadow: rgba(0,0,0,.45);
  --chevron: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path d='M2 4l4 4 4-4' fill='none' stroke='%23b7c2cf' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  color-scheme: dark;
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; color: var(--ink); background: var(--ground); }
a { color: var(--accent); }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar .brand { font-weight: 700; text-decoration: none; color: var(--ink); display: inline-flex; align-items: center; gap: 8px; }
.topbar .brand svg { width: 26px; height: 26px; border-radius: 6px; flex: none; }
.theme-toggle { background: none; border: 1px solid var(--line); border-radius: 999px; width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; color: var(--muted); padding: 0; }
.theme-toggle:hover { color: var(--ink); background: var(--hover); }
.theme-toggle svg { width: 17px; height: 17px; }
.theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .icon-sun { display: block; }
  :root:not([data-theme="light"]) .theme-toggle .icon-moon { display: none; }
}
.topbar nav { display: flex; align-items: center; gap: 14px; }
.topbar .user { color: var(--muted); font-size: 13px; }
.container { max-width: 1080px; margin: 0 auto; padding: 24px 20px; }
.panel, .auth-card { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 20px 24px; }
.auth-card { max-width: 420px; margin: 60px auto; }
.muted { color: var(--muted); }
.field { margin-bottom: 14px; }
.field label { display: block; font-weight: 600; margin-bottom: 4px; }
.field input, .field select, .field textarea, .inline-form input, .lines-table input, input[type=search], input[type=month], input[type=date], input[type=text], input[type=email], input[type=password], input[type=number], textarea { background: var(--surface); color: var(--ink); }
.field input { width: 100%; padding: 9px 10px; border: 1px solid var(--line); border-radius: 6px; font: inherit; }
.button, button.link-button { font: inherit; cursor: pointer; }
.button { background: var(--accent); color: #fff; border: 0; border-radius: 6px; padding: 10px 16px; font-weight: 600; white-space: nowrap; display: inline-block; text-align: center; }
button.link-button { background: none; border: 0; color: var(--accent); padding: 0; text-decoration: underline; }
.flash { padding: 10px 14px; border-radius: 6px; margin-bottom: 16px; }
.flash-notice { background: var(--ok-bg); color: var(--ok-ink); }
.flash-alert { background: var(--bad-bg); color: var(--bad-ink); }

/* navegación */
.topbar { flex-wrap: wrap; }
.menu { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.menu a { text-decoration: none; color: var(--muted); font-weight: 600; padding: 4px 0; border-bottom: 2px solid transparent; }
.menu a.active { color: var(--ink); border-bottom-color: var(--accent); }
.menu-group { position: relative; }
.menu-group summary { list-style: none; cursor: pointer; color: var(--muted); font-weight: 600; padding: 4px 0; border-bottom: 2px solid transparent; user-select: none; }
.menu-group summary::-webkit-details-marker { display: none; }
.menu-group summary::after { content: " ▾"; font-size: 11px; }
.menu-group.active summary, .menu-group[open] summary { color: var(--ink); }
.menu-group.active summary { border-bottom-color: var(--accent); }
.menu-dropdown { position: absolute; top: calc(100% + 6px); left: -10px; z-index: 20; min-width: 150px; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px var(--shadow); padding: 6px; display: flex; flex-direction: column; }
.menu-dropdown a { padding: 7px 10px; border-radius: 6px; border-bottom: 0; }
.menu-dropdown a:hover, .menu-dropdown a.active { background: var(--hover); color: var(--ink); }
.nav-count { display: inline-block; min-width: 18px; padding: 1px 6px; border-radius: 999px; background: #c62828; color: #fff; font-size: 11px; font-weight: 700; text-align: center; line-height: 16px; vertical-align: middle; }

/* selectores */
select { appearance: none; -webkit-appearance: none; padding: 9px 34px 9px 10px; border: 1px solid var(--line); border-radius: 6px; font: inherit; color: var(--ink); background: var(--surface) var(--chevron) no-repeat right 10px center; min-height: 40px; cursor: pointer; max-width: 100%; }
select:focus, input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
select:disabled { opacity: .6; cursor: default; }
td select, .inline-form select, .lines-table select { padding: 6px 30px 6px 8px; min-height: 34px; }
.year-selector select { font-weight: 600; }

/* datos de la empresa en la portada */
.company-card { font-size: 15px; line-height: 1.45; margin: 4px 0 6px; color: var(--ink); }

/* tablas y tarjetas */
h1 { font-size: 24px; margin: 0 0 6px; }
h2 { font-size: 18px; margin: 0 0 10px; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.actions select, .actions input[type=month], .actions input[type=date], .actions input[type=text], .actions input[type=search] { padding: 9px 10px; border: 1px solid var(--line); border-radius: 6px; font: inherit; background: var(--surface); color: var(--ink); min-height: 40px; }
.panel + .panel { margin-top: 18px; }
/* visor del documento adjunto (ficha de factura recibida) */
.document-viewer { margin-top: 18px; }
/* alto proporcional al ancho (A4 ajustado al ancho + barra del visor), no a la ventana: así una factura de una
   página llena el marco sin dejar el fondo gris del visor; los PDF de varias páginas se desplazan dentro */
.document-viewer iframe { display: block; width: 100%; height: auto; aspect-ratio: 1 / 1.48; min-height: 420px; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
.document-viewer img { display: block; max-width: 100%; border: 1px solid var(--line); border-radius: 6px; }
.grid { display: grid; gap: 18px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.grid .wide { grid-column: 1 / -1; }
.grid .panel, .grid > * { min-width: 0; }
.kv td { white-space: normal; overflow-wrap: break-word; }
.kv td:first-child { color: var(--muted); width: 48%; padding-right: 14px; }
.kv td:last-child { font-variant-numeric: tabular-nums; }
.grid .subcol { background: var(--ground); border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px; }
table { width: 100%; border-collapse: collapse; background: var(--surface); }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; white-space: normal; overflow-wrap: break-word; }
td.wrap, th.wrap { white-space: normal; }
td.nowrap, th.nowrap, td:has(> .badge:only-child), td:has(> form.button_to:only-child), td:has(> a.button:only-child) { white-space: nowrap; }
.badge, .button { white-space: nowrap; }
table { table-layout: auto; max-width: 100%; }
td.ellipsis { max-width: 170px; overflow: hidden; text-overflow: ellipsis; }
.table-dense th, .table-dense td { padding: 7px 8px; font-size: 13.5px; }
.table-dense .badge { font-size: 11px; padding: 2px 7px; }
.inline-form select { max-width: 100%; }
.table-wrap table { table-layout: auto; }
.panel { overflow-x: auto; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
/* Barra de orden del libro diario */
.journal-sort { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 0 0 14px; }
.journal-sort label { font-weight: 600; }
.journal-sort .button_to, .journal-sort form { margin: 0; }
/* Cabeceras que ordenan la tabla (p. ej. el diario por número o por fecha) */
th .sort-link { color: inherit; text-decoration: none; white-space: nowrap; }
th .sort-link:hover, th .sort-link.active { color: var(--ink); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.total td { font-weight: 700; border-top: 2px solid var(--ink); }
.table-wrap { overflow-x: auto; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge-ok { background: var(--ok-bg); color: var(--ok-ink); }
.badge-link { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.badge-link:hover { text-decoration-thickness: 2px; }
.badge-bad { background: var(--bad-bg); color: var(--bad-ink); }
.badge-warn { background: var(--warn-bg); color: var(--warn-ink); }
.badge-draft { background: var(--draft-bg); color: var(--draft-ink); }
.button-secondary { background: var(--surface); color: var(--accent); border: 1px solid var(--accent); }
.button-danger { background: var(--bad-ink); }
.button-small { padding: 5px 10px; font-size: 13px; }
.kpis { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 18px; }
.kpi { flex: 1 1 140px; min-width: 0; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 12px 16px; }
.kpi .label { color: var(--muted); font-size: 12px; text-transform: uppercase; }
.kpi .value { font-size: 22px; font-weight: 700; }
.inline-form { display: inline; }
.tx-actions { display: flex; flex-direction: column; gap: 6px; min-width: 320px; }
/* banco: pagar varias facturas con un movimiento */
.tx-multi { border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; }
.tx-multi summary { cursor: pointer; font-size: 13px; color: var(--muted); }
.tx-multi form { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; }
.tx-multi-item { display: block; font-size: 13px; }
.tx-multi-item input { margin-right: 6px; }
.tx-row { display: flex; align-items: center; gap: 6px; flex-wrap: nowrap; margin: 0; }
.tx-row select { flex: 1 1 auto; min-width: 0; max-width: 280px; }
.tx-row .button { white-space: nowrap; }
.tx-suggestion { color: var(--muted); font-size: 12px; line-height: 1.4; }
.tx-suggestion .badge { margin-left: 4px; }
.inline-form input[type=date] { padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; font: inherit; }

/* formularios */
.form-grid { display: grid; gap: 12px 18px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.form-grid .wide { grid-column: 1 / -1; }
.field select, .field textarea { width: 100%; padding: 9px 10px; border: 1px solid var(--line); border-radius: 6px; font: inherit; }
.field select { padding-right: 34px; }
.field textarea { min-height: 80px; }
.field .hint { color: var(--muted); font-size: 12px; margin-top: 3px; }
.field.check { display: flex; align-items: center; gap: 10px; min-height: 40px; }
.field.check label { display: inline; font-weight: 600; margin: 0; cursor: pointer; }
input[type=checkbox], input[type=radio] { accent-color: var(--accent); width: 18px; height: 18px; margin: 0; padding: 0; cursor: pointer; flex: none; vertical-align: middle; }
.field.check input[type=checkbox] { width: 20px; height: 20px; }
td input[type=checkbox] { width: 16px; height: 16px; }
.lines-table input { width: 100%; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; font: inherit; }
.lines-table select { width: 100%; border: 1px solid var(--line); border-radius: 6px; font: inherit; }
.lines-table .drag { cursor: grab; color: var(--muted); text-align: center; width: 28px; user-select: none; font-size: 18px; }
.lines-table tr.dragging { opacity: .5; }
.lines-table tr.drop-target td { border-top: 2px solid var(--accent); }
.lines-table tfoot td { font-weight: 600; border-bottom: 0; }
.lines-table tfoot td.muted { font-weight: 400; }
.lines-table .remove { background: none; border: 0; color: var(--bad-ink); cursor: pointer; font-size: 16px; }
.form-actions { margin-top: 18px; display: flex; gap: 10px; align-items: center; }

/* documento de factura (pantalla e impresión) */
.invoice-doc { background: #fff; color: #1f2933; max-width: 800px; margin: 0 auto; padding: 36px 44px; border: 1px solid var(--line); }
/* La factura es un papel: siempre con los colores del tema claro, también en
   modo oscuro (si no, sus tablas cogían fondos oscuros sobre el papel blanco). */
.invoice-doc {
  --ink: #1f2933; --muted: #616e7c; --line: #d9e2ec; --surface: #ffffff; --ground: #f5f7fa;
  --accent: #0b5fa5; --hover: #eef3f8; --shadow: rgba(0,0,0,.08);
  --ok-bg: #e3f6e8; --ok-ink: #1e4620; --bad-bg: #fdecea; --bad-ink: #611a15;
  --warn-bg: #fff4d6; --warn-ink: #6b4b00; --draft-bg: #e8edf3; --draft-ink: #3b4a5a;
  color-scheme: light;
}
.invoice-doc .doc-head { display: flex; justify-content: space-between; gap: 24px; }
.invoice-doc .doc-title { font-size: 26px; font-weight: 700; margin: 0; }
.invoice-doc .block { margin-top: 22px; }
.invoice-doc .block h3 { font-size: 12px; text-transform: uppercase; color: var(--muted); margin: 0 0 4px; }
.invoice-doc table { margin-top: 18px; }
.invoice-doc .totals td { border: 0; padding: 4px 10px; }
.invoice-doc .footer { margin-top: 36px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 10px; }
.invoice-doc .fingerprint { font-family: ui-monospace, monospace; font-size: 10px; word-break: break-all; }
body.print { background: #fff; }
body.print .invoice-doc { border: 0; }
@media print { .no-print { display: none !important; } }

/* cuerpo de un correo de la bandeja: completo, con saltos de línea y sin barras */
.email-body { white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; line-height: 1.45; font-size: 14px; }

/* Selectores de archivo con la estética de la web: el botón como .button-secondary */
input[type=file] { font: inherit; font-size: 14px; color: var(--muted); background: var(--surface); border: 1px dashed var(--line); border-radius: 6px; padding: 5px 10px 5px 5px; min-height: 40px; max-width: 100%; cursor: pointer; }
.field input[type=file] { padding: 5px 10px 5px 5px; }
input[type=file]:hover { border-color: var(--accent); }
input[type=file]::file-selector-button { font: inherit; font-weight: 600; color: var(--accent); background: var(--surface); border: 1px solid var(--accent); border-radius: 6px; padding: 6px 14px; margin-right: 10px; cursor: pointer; transition: background .15s, color .15s; }
input[type=file]:hover::file-selector-button { background: var(--accent); color: #fff; }
