/*
 * FACTURYA · Hoja de estilos
 * © Limonada Web. Todos los derechos reservados. Prohibida su copia o uso para entrenar IA.
 */

:root {
  --ink: #152D4A;
  --ink-2: #0C1C31;
  --text: #1B2533;
  --muted: #56667A;
  --line: #E1E6ED;
  --line-strong: #CBD3DD;
  --bg: #F4F6F9;
  --surface: #FFFFFF;
  --surface-2: #F8FAFC;
  --accent: #0E8F77;
  --accent-strong: #0B7562;
  --accent-soft: #E2F5F0;
  --ok: #0B6E55;      --ok-bg: #E2F5EE;
  --warn: #7A4E00;    --warn-bg: #FFF3D1;
  --bad: #A8231A;     --bad-bg: #FDEBE9;
  --info: #1D4F91;    --info-bg: #E7EFFA;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(16, 32, 56, .06), 0 1px 1px rgba(16, 32, 56, .04);
  --focus: 0 0 0 3px rgba(14, 143, 119, .35);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 15px/1.5 var(--font); color: var(--text); background: var(--bg); }
a { color: var(--accent-strong); }
a:hover { color: var(--ink); }
h1, h2, h3 { color: var(--ink); line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.5rem; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; font-weight: 650; }
h3 { font-size: 1rem; font-weight: 650; }
p { margin: 0 0 .75rem; }
img { max-width: 100%; }
:focus-visible { outline: none; box-shadow: var(--focus); border-radius: var(--radius-sm); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.skip-link { position: absolute; left: -999px; top: 8px; background: var(--ink); color: #fff; padding: 8px 12px; border-radius: var(--radius-sm); z-index: 100; }
.skip-link:focus { left: 8px; color: #fff; }

/* ---------- Estructura ---------- */
.shell { display: grid; grid-template-columns: 248px minmax(0, 1fr); min-height: 100vh; background: linear-gradient(to right, var(--surface) 248px, var(--bg) 248px); }
.sidebar { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.sidebar__brand { padding: 18px 20px 14px; border-bottom: 1px solid var(--line); }
.sidebar__brand img { height: 34px; width: auto; display: block; }
.nav { padding: 12px 10px; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; flex: 1; }
.nav__section { font-size: .78rem; font-weight: 600; color: var(--muted); padding: 14px 12px 6px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: var(--radius-sm); color: var(--text); text-decoration: none; font-weight: 500; }
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a[aria-current="page"] { background: var(--accent-soft); color: var(--ink); font-weight: 650; box-shadow: inset 3px 0 0 var(--accent); }
.nav svg { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav__badge { margin-left: auto; background: var(--warn-bg); color: var(--warn); font-size: .75rem; font-weight: 700; border-radius: 999px; padding: 1px 8px; }
.sidebar__foot { padding: 12px 20px 16px; border-top: 1px solid var(--line); font-size: .78rem; color: var(--muted); }
.sidebar__foot a { color: var(--muted); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 12px 28px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.topbar__company { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar__company strong { color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar__spacer { flex: 1; }
.btn.menu-toggle { display: none; }
[hidden] { display: none !important; }
.content { padding: 26px 28px 40px; max-width: 1240px; width: 100%; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { color: var(--muted); margin: 2px 0 0; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.app-footer { margin-top: auto; padding: 18px 28px; border-top: 1px solid var(--line); color: var(--muted); font-size: .8rem; display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; background: var(--surface); }
.app-footer a { color: var(--muted); }

/* ---------- Componentes ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.card__head h2 { margin: 0; }
.card__body { padding: 18px 20px; }
.card + .card, .card + .grid, .grid + .card, .grid + .grid { margin-top: 20px; }
.grid { display: grid; gap: 20px; }
.grid > * { min-width: 0; }
.grid > .card { margin-top: 0; }
.grid > div > .card + .card, .grid > div > .card + details, .grid > div > details + p { margin-top: 20px; }
.grid--2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid--main { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 40px; padding: 8px 16px; border-radius: var(--radius-sm); border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink); font: inherit; font-weight: 600; text-decoration: none; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); color: var(--ink); border-color: var(--muted); }
.btn--primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn--primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; }
.btn--dark { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn--dark:hover { background: var(--ink-2); color: #fff; }
.btn--danger { color: var(--bad); border-color: #E7B4AF; }
.btn--danger:hover { background: var(--bad-bg); color: var(--bad); border-color: var(--bad); }
.btn--ghost { border-color: transparent; background: transparent; }
.btn--sm { min-height: 32px; padding: 4px 10px; font-size: .875rem; }
.btn[disabled] { opacity: .55; cursor: not-allowed; }
.link-btn { background: none; border: 0; padding: 0; color: var(--accent-strong); font: inherit; cursor: pointer; text-decoration: underline; }

.badge { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px; font-size: .78rem; font-weight: 650; white-space: nowrap; border: 1px solid transparent; }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge--ok { background: var(--ok-bg); color: var(--ok); }
.badge--warn { background: var(--warn-bg); color: var(--warn); }
.badge--bad { background: var(--bad-bg); color: var(--bad); }
.badge--info { background: var(--info-bg); color: var(--info); }
.badge--muted { background: #EEF1F5; color: var(--muted); }

.alert { border-radius: var(--radius); padding: 12px 16px; margin-bottom: 16px; border: 1px solid; display: flex; gap: 10px; align-items: flex-start; }
.alert p:last-child { margin-bottom: 0; }
.alert--ok { background: var(--ok-bg); border-color: #A9DCCB; color: #08493A; }
.alert--bad { background: var(--bad-bg); border-color: #F0B9B3; color: #7A1A13; }
.alert--warn { background: var(--warn-bg); border-color: #EBCB7A; color: #5C3B00; }
.alert--info { background: var(--info-bg); border-color: #B8CDEB; color: #163C6F; }
.alert ul { margin: 0; padding-left: 18px; }

/* ---------- Formularios ---------- */
.form-grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 14px 16px; }
.col-12 { grid-column: span 12; } .col-8 { grid-column: span 8; } .col-6 { grid-column: span 6; }
.col-4 { grid-column: span 4; } .col-3 { grid-column: span 3; } .col-2 { grid-column: span 2; }
.field label, .label { display: block; font-weight: 600; font-size: .875rem; color: var(--ink); margin-bottom: 5px; }
.field .hint { display: block; color: var(--muted); font-size: .8rem; margin-top: 4px; }
.field .error { display: block; color: var(--bad); font-size: .82rem; margin-top: 4px; font-weight: 600; }
.input, select.input, textarea.input { width: 100%; min-height: 40px; padding: 8px 11px; font: inherit; color: var(--text); background: #fff; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); }
textarea.input { min-height: 84px; resize: vertical; }
.input:focus { border-color: var(--accent); box-shadow: var(--focus); outline: none; }
.input[aria-invalid="true"] { border-color: var(--bad); }
.input--num { text-align: right; font-variant-numeric: tabular-nums; }
.input--sm { min-height: 32px; padding: 4px 8px; width: auto; font-size: .875rem; }
.meter { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; margin-top: 8px; }
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.meter--warn > span { background: var(--warn, #b45309); }
.check { display: flex; align-items: flex-start; gap: 10px; font-weight: 500; }
.check input { width: 18px; height: 18px; margin-top: 2px; accent-color: var(--accent); }
.radio-row { display: flex; gap: 18px; flex-wrap: wrap; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; padding-top: 6px; }
fieldset { border: 0; padding: 0; margin: 0; }
legend { font-weight: 650; color: var(--ink); margin-bottom: 8px; padding: 0; }

/* ---------- Tablas ---------- */
.table-wrap { overflow-x: auto; position: relative; }
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th { text-align: left; font-size: .8rem; font-weight: 650; color: var(--muted); padding: 10px 14px; border-bottom: 1px solid var(--line); background: var(--surface-2); white-space: nowrap; }
.table td { padding: 11px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: #FAFBFD; }
.table .num { text-align: right; white-space: nowrap; }
.table .muted { color: var(--muted); font-size: .85rem; }
.table a.row-link { color: var(--ink); font-weight: 650; text-decoration: none; }
.table a.row-link:hover { text-decoration: underline; }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty h3 { color: var(--ink); }

/* ---------- Filtros ---------- */
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; padding: 14px 20px; border-bottom: 1px solid var(--line); }
.filters .field { min-width: 150px; }
.filters .field--grow { flex: 1; min-width: 220px; }

/* ---------- Métricas ---------- */
.stat { padding: 16px 20px; }
a.card { text-decoration: none; color: inherit; display: block; }
a.card:hover { border-color: var(--line-strong); }
.stat__label { color: var(--muted); font-size: .85rem; font-weight: 600; }
.stat__value { font-size: 1.6rem; font-weight: 750; color: var(--ink); font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.stat__note { color: var(--muted); font-size: .85rem; }

/* ---------- Factura ---------- */
.lines-table td { padding: 8px 6px; vertical-align: top; }
.lines-table th { padding: 8px 6px; }
.lines-table .col-desc { min-width: 240px; }
.lines-table .col-qty { width: 92px; }
.lines-table .col-price { width: 120px; }
.lines-table .col-disc { width: 82px; }
.lines-table .col-tax { width: 170px; }
.lines-table .col-total { width: 110px; text-align: right; padding-top: 16px; font-weight: 650; }
.lines-table .col-rm { width: 44px; }
.totals { margin-left: auto; width: min(100%, 360px); }
.totals dl { display: grid; grid-template-columns: 1fr auto; gap: 6px 16px; margin: 0; font-variant-numeric: tabular-nums; }
.totals dt { color: var(--muted); }
.totals dd { margin: 0; text-align: right; font-weight: 600; }
.totals .grand { font-size: 1.15rem; color: var(--ink); font-weight: 800; border-top: 1px solid var(--line); padding-top: 8px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; margin: 0; }
.kv dt { color: var(--muted); font-weight: 600; font-size: .875rem; }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.qr-box { display: flex; gap: 16px; align-items: center; }
.qr-box img { width: 120px; height: 120px; border: 1px solid var(--line); border-radius: var(--radius-sm); }
.hash { font-family: var(--mono); font-size: .78rem; overflow-wrap: anywhere; color: var(--muted); }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 10px 0 10px 18px; border-left: 2px solid var(--line); position: relative; }
.timeline li::before { content: ""; position: absolute; left: -6px; top: 15px; width: 10px; height: 10px; border-radius: 50%; background: var(--line-strong); }
.timeline li.ok::before { background: var(--ok); }
.timeline li.bad::before { background: var(--bad); }
.timeline li.warn::before { background: #D99A00; }

.env-banner { background: var(--warn-bg); color: var(--warn); border-bottom: 1px solid #EBCB7A; padding: 8px 28px; font-size: .875rem; font-weight: 600; }

/* ---------- Acceso ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: linear-gradient(180deg, #EEF3F7 0%, var(--bg) 60%); }
.auth__card { width: min(100%, 420px); }
.auth__logo { display: block; height: 44px; margin: 0 auto 22px; }
.auth__foot { text-align: center; color: var(--muted); font-size: .8rem; margin-top: 18px; }
.auth__foot a { color: var(--muted); }

/* ---------- Paginación ---------- */
.pagination { display: flex; gap: 6px; justify-content: center; padding: 14px; flex-wrap: wrap; }
.pagination a, .pagination span { min-width: 36px; min-height: 36px; display: inline-flex; align-items: center; justify-content: center; padding: 0 10px; border: 1px solid var(--line); border-radius: var(--radius-sm); text-decoration: none; color: var(--ink); background: #fff; }
.pagination [aria-current="page"] { background: var(--ink); color: #fff; border-color: var(--ink); }
.pagination .disabled { color: var(--line-strong); }

details.danger-zone { border: 1px solid #F0B9B3; border-radius: var(--radius); padding: 12px 16px; background: #FFFBFA; }
details.danger-zone summary { cursor: pointer; font-weight: 650; color: var(--bad); }
.stack > * + * { margin-top: 12px; }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.mt-0 { margin-top: 0; }
.inline-form { display: inline; }

/* ---------- Adaptación ---------- */
@media (max-width: 1080px) {
  .grid--3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid--main { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); background: var(--bg); }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 272px; transform: translateX(-100%); transition: transform .2s ease; z-index: 50; box-shadow: 0 0 40px rgba(0,0,0,.15); }
  .sidebar.is-open { transform: none; }
  .btn.menu-toggle { display: inline-flex; }
  .topbar { padding: 10px 16px; }
  .content { padding: 18px 16px 32px; }
  .env-banner { padding: 8px 16px; }
  .grid--2, .grid--3 { grid-template-columns: 1fr; }
  .col-8, .col-6, .col-4, .col-3, .col-2 { grid-column: span 12; }
  .app-footer { padding: 16px; }
  .hide-mobile { display: none; }
  .card__head, .card__body, .filters { padding-left: 16px; padding-right: 16px; }
  /* Tabla de líneas en tarjetas apiladas */
  .lines-table thead { display: none; }
  .lines-table, .lines-table tbody, .lines-table tr, .lines-table td { display: block; width: 100%; }
  .lines-table tr { border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; margin-bottom: 10px; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
  .lines-table td { border: 0; padding: 4px; width: auto; }
  .lines-table td.col-desc, .lines-table td.col-tax { grid-column: span 2; }
  .lines-table td[data-label]::before { content: attr(data-label); display: block; font-size: .78rem; color: var(--muted); font-weight: 600; margin-bottom: 2px; }
  .lines-table .col-total { text-align: left; padding-top: 4px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
@media print {
  .sidebar, .topbar, .app-footer, .page-actions, .env-banner { display: none !important; }
  .shell { display: block; }
}

/* Declaración responsable */
.auth__card:has(.legal) { max-width: 760px; }
.legal h1 { font-size: 1.35rem; margin: 0 0 4px; }
.legal h2 { font-size: 1.05rem; margin: 24px 0 8px; }
.legal p { line-height: 1.6; }
.legal__dl { display: grid; grid-template-columns: minmax(0, 12rem) minmax(0, 1fr); gap: 6px 16px; margin: 0; }
.legal__dl dt { color: var(--muted, #5b6470); }
.legal__dl dd { margin: 0; }
@media (max-width: 560px) { .legal__dl { grid-template-columns: minmax(0, 1fr); } .legal__dl dd { margin-bottom: 8px; } }

/* Ventanilla única */
.oss-box { border: 1px solid var(--line-strong); border-radius: var(--radius-sm); padding: 12px 16px 4px; margin: 16px 0; }
.oss-box legend { font-weight: 700; padding: 0 6px; }

/* Informes */
.report-range { display: flex; align-items: end; padding-bottom: 10px; }
.report-files { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 320px), 1fr)); gap: 24px; }
.report-files h3 { margin: 0 0 6px; font-size: 1rem; }
.report-files p { margin: 0 0 12px; }

/* Prueba de la consulta VIES */
.vies-proof { display: flex; flex-wrap: wrap; gap: 6px 8px; align-items: center; margin-top: 6px; font-size: .875rem; }
.vies-proof code { word-break: break-all; }

/* Conexiones: claves, secretos y pasos */
.secret { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.secret code { font-family: var(--mono); font-size: .82rem; background: var(--bg, #F4F6F8); border: 1px solid var(--line, #E2E6EA); border-radius: 6px; padding: 6px 10px; overflow-wrap: anywhere; min-width: 0; flex: 1 1 260px; }
.kv--wide { grid-template-columns: max-content minmax(0, 1fr); align-items: center; }
.steps { margin: 8px 0 0; padding-left: 20px; display: grid; gap: 8px; }
.steps li::marker { font-weight: 700; color: var(--accent); }
.small { font-size: .82rem; }
.connect-form { border-top: 1px solid var(--line, #E2E6EA); }
.doc h2 { margin-top: 0; }
.doc h3 { margin: 22px 0 8px; font-size: 1rem; }
.doc pre { font-family: var(--mono); font-size: .8rem; background: #0F1720; color: #E6EDF3; border-radius: 8px; padding: 14px 16px; overflow-x: auto; margin: 8px 0 14px; }
.doc code { font-family: var(--mono); font-size: .85em; }
.doc .method { display: inline-block; font-family: var(--mono); font-size: .75rem; font-weight: 700; padding: 2px 6px; border-radius: 4px; background: var(--info-bg); color: #163C6F; margin-right: 6px; }
.doc .method--post { background: var(--ok-bg); color: #08493A; }
@media (max-width: 720px) { .kv--wide { grid-template-columns: minmax(0, 1fr); } }
