/* FacturApp — hoja de estilos. Tema claro por defecto, oscuro por ajuste o por el sistema (auto). */
:root {
  --bg: #f3f5f9; --panel: #ffffff; --panel2: #f1f3f7; --border: #e2e6ee; --border2: #cfd5e0;
  --text: #1c2130; --muted: #6b7382;
  --accent: #3b66e0; --accent-hover: #3257bf; --accent-soft: #e7edfc;
  --accent2: #17925c; --danger: #cf3f36; --warn: #b7791f;
  --sidebar-bg: #171b26; --sidebar-text: #c6ccd8; --sidebar-muted: #7b8496; --sidebar-active: rgba(255,255,255,.08);
  --radio: 10px; --sombra: 0 1px 2px rgba(20,25,40,.05), 0 1px 8px rgba(20,25,40,.04);
  --ancho-menu: 248px; --alto-topbar: 56px;
}
html[data-tema="oscuro"] { color-scheme: dark; }
html[data-tema="oscuro"], html[data-tema="oscuro"] body { --bg: #0f1218; --panel: #171b23; --panel2: #1f2430; --border: #2a303d; --border2: #3a4150; --text: #e6e9ef; --muted: #9aa3b3; --accent-soft: #223050; --sidebar-bg: #0b0e14; }
@media (prefers-color-scheme: dark) {
  html[data-tema="auto"] { color-scheme: dark; }
  html[data-tema="auto"], html[data-tema="auto"] body { --bg: #0f1218; --panel: #171b23; --panel2: #1f2430; --border: #2a303d; --border2: #3a4150; --text: #e6e9ef; --muted: #9aa3b3; --accent-soft: #223050; --sidebar-bg: #0b0e14; }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px; line-height: 1.45; background: var(--bg); color: var(--text);
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
svg { display: inline-block; vertical-align: middle; }

/* ---------- Estructura: menú lateral + contenido ---------- */
.nav-toggle { display: none; }
.nav-backdrop { display: none; }
body.con-menu { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--ancho-menu); flex: 0 0 var(--ancho-menu); background: var(--sidebar-bg); color: var(--sidebar-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 14px 16px 12px; color: #fff; text-decoration: none; border-bottom: 1px solid rgba(255,255,255,.07); }
.brand:hover { text-decoration: none; }
.brand-logo { width: 38px; height: 38px; object-fit: contain; border-radius: 8px; background: #fff; padding: 3px; }
.brand-mark { width: 38px; height: 38px; border-radius: 10px; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 18px; }
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-name { font-weight: 700; font-size: 15px; letter-spacing: .01em; }
.brand-sub { font-size: 11.5px; color: var(--sidebar-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.menu { padding: 6px 8px; flex: 1; }
.menu-grupo { display: flex; align-items: center; justify-content: space-between; width: 100%; background: none; border: 0; cursor: pointer; font-family: inherit; font-size: 10px; text-transform: uppercase; letter-spacing: .08em; color: var(--sidebar-muted); padding: 10px 10px 2px; }
.menu-grupo:hover { color: var(--sidebar-text); }
.menu-grupo svg { width: 12px; height: 12px; opacity: .7; transition: transform .15s; }
.menu-bloque.cerrado .menu-grupo svg { transform: rotate(-90deg); }
.menu-bloque.cerrado .menu-item { display: none; }
.menu-bloque.cerrado .menu-grupo { padding-bottom: 6px; }
.menu-item {
  display: flex; align-items: center; gap: 10px; padding: 5px 10px; border-radius: 7px; color: var(--sidebar-text);
  font-size: 13px; text-decoration: none; min-height: 32px; white-space: nowrap;
}
.menu-item span { overflow: hidden; text-overflow: ellipsis; }
.menu-item svg { width: 18px; height: 18px; flex: 0 0 18px; opacity: .85; }
.menu-plegar { width: 100%; background: none; border: 0; font: inherit; cursor: pointer; color: var(--sidebar-muted); }
.menu-plegar:hover { color: #fff; }
.menu-plegar .ico-ampliar { display: none; }
.menu-item:hover { background: var(--sidebar-active); color: #fff; text-decoration: none; }
.menu-item.activo { background: var(--accent); color: #fff; }
.menu-item.activo svg { opacity: 1; }
.sidebar-pie { padding: 6px 8px; border-top: 1px solid rgba(255,255,255,.07); }

/* ---------- Menú plegado a iconos (botón "Reducir menú", se recuerda en el navegador) ---------- */
@media (min-width: 768px) {
  html.menu-plegado { --ancho-menu: 60px; }
  html.menu-plegado .brand { padding: 12px 11px; justify-content: center; }
  html.menu-plegado .brand-text, html.menu-plegado .menu-item span { display: none; }
  html.menu-plegado .menu-grupo { font-size: 0; justify-content: center; padding: 6px 0 2px; }
  html.menu-plegado .menu-grupo svg { width: 11px; height: 11px; }
  html.menu-plegado .menu-bloque.cerrado .menu-grupo { padding-bottom: 2px; }
  html.menu-plegado .menu-bloque + .menu-bloque { margin-top: 4px; border-top: 1px solid rgba(255,255,255,.07); }
  html.menu-plegado .menu-item { justify-content: center; padding: 6px; min-height: 30px; }
  html.menu-plegado .menu-item svg { width: 20px; height: 20px; flex-basis: 20px; }
  html.menu-plegado .menu, html.menu-plegado .sidebar-pie { padding: 6px; }
  html.menu-plegado .menu-plegar .ico-reducir { display: none; }
  html.menu-plegado .menu-plegar .ico-ampliar { display: block; }
}
.sidebar-pie .user-email { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.contenido { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: var(--alto-topbar); display: flex; align-items: center; gap: 12px; padding: 0 22px;
  background: var(--panel); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.topbar-titulo { font-weight: 600; font-size: 15px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-acciones { display: flex; gap: 8px; }
.nav-toggle-btn { display: none; width: 40px; height: 40px; align-items: center; justify-content: center; border-radius: 8px; cursor: pointer; color: var(--text); margin-left: -8px; }
.nav-toggle-btn svg { width: 24px; height: 24px; }
.nav-toggle-btn:hover { background: var(--panel2); }

main { padding: 22px 24px 60px; max-width: 1180px; width: 100%; margin: 0 auto; }
body.sin-menu main { max-width: 100%; }

h1 { font-size: 21px; font-weight: 700; margin: 0 0 16px; letter-spacing: -.01em; }
h2 { font-size: 12.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin: 26px 0 10px; font-weight: 600; }
h3 { font-size: 14px; margin: 18px 0 8px; }
code { background: var(--panel2); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }

.flash { padding: 10px 14px; border-radius: var(--radio); margin-bottom: 16px; font-size: 13.5px; border: 1px solid var(--border); background: var(--panel); }
.flash-ok { background: #e9f7ef; border-color: #b9e3c9; color: #0f5c3c; }
.flash-error { background: #fdeceb; border-color: #f3c1bd; color: #8f2b23; }
html[data-tema="oscuro"] .flash-ok, html[data-tema="auto"] .flash-ok { background: #12301f; border-color: #1f5a37; color: #9fe0bb; }
html[data-tema="oscuro"] .flash-error, html[data-tema="auto"] .flash-error { background: #3a1815; border-color: #6a2a25; color: #f4b5ae; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 20px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radio); padding: 16px 18px; box-shadow: var(--sombra); }
.card .num { font-size: 24px; font-weight: 700; color: var(--accent2); letter-spacing: -.01em; font-variant-numeric: tabular-nums; }
.card .lbl { font-size: 12px; color: var(--muted); margin-top: 4px; }
form.card, div.card { display: block; }

.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar form { display: flex; gap: 8px; flex-wrap: wrap; }
.toolbar > span { display: flex; gap: 8px; flex-wrap: wrap; }

input[type=text], input[type=search], input[type=email], input[type=password], input[type=number],
input[type=date], input[type=month], input[type=url], input[type=tel], input[type=color], select, textarea {
  background: var(--panel); border: 1px solid var(--border2); color: var(--text);
  border-radius: 8px; padding: 9px 11px; font-size: 14px; font-family: inherit; min-height: 40px;
}
input[type=color] { padding: 3px; width: 56px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
input[type=checkbox], input[type=radio] { width: 17px; height: 17px; accent-color: var(--accent); vertical-align: -3px; }
input[type=file] { font-size: 13px; }
label { cursor: default; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; background: var(--accent); color: #fff; border: 1px solid transparent;
  border-radius: 8px; padding: 9px 16px; font-size: 14px; font-weight: 500; cursor: pointer; text-decoration: none; min-height: 40px; line-height: 1.2;
  font-family: inherit; transition: background .12s, box-shadow .12s;
}
.btn:hover { background: var(--accent-hover); text-decoration: none; }
.btn-secondary { background: var(--panel); border-color: var(--border2); color: var(--text); }
.btn-secondary:hover { background: var(--panel2); }
.btn-danger { background: var(--danger); border-color: transparent; color: #fff; }
.btn-danger:hover { background: #b23028; }
.btn-secondary.btn-danger { background: var(--panel); color: var(--danger); border-color: var(--border2); }
.btn-secondary.btn-danger:hover { background: #fdeceb; }
.btn-small { padding: 5px 11px; font-size: 12.5px; min-height: 30px; }
.btn[disabled], button[disabled] { opacity: .55; cursor: not-allowed; }

table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13.5px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radio); overflow: hidden; box-shadow: var(--sombra); }
thead th { background: var(--panel2); text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; font-weight: 600; font-size: 12.5px; color: var(--muted); }
tbody td { padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; overflow-wrap: anywhere; }
tbody td .btn { overflow-wrap: normal; }
tbody tr:last-child td { border-bottom: none; }
/* Filas alternas (cebra) para seguir cada línea con la vista; el hover sigue resaltando */
tbody tr:nth-child(even) > td { background: var(--panel2); }
tbody tr:hover > td { background: var(--accent-soft); }
html[data-tema="oscuro"] tbody tr:hover > td, html[data-tema="auto"] tbody tr:hover > td { background: #26304a; }
.num-cell { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

.pagination { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-top: 14px; color: var(--muted); font-size: 13px; }
.pagination a { display: inline-flex; align-items: center; justify-content: center; min-width: 32px; min-height: 32px; padding: 0 8px; border: 1px solid var(--border2); border-radius: 8px; background: var(--panel); color: var(--text); text-decoration: none; }
.pagination a:hover { background: var(--panel2); border-color: var(--accent); }
.pagination .pag-nums { display: inline-flex; gap: 4px; align-items: center; }
.pagination .pag-actual { display: inline-flex; align-items: center; justify-content: center; min-width: 32px; min-height: 32px; padding: 0 8px; border-radius: 8px; background: var(--accent); color: #fff; font-weight: 600; }
.pagination .pag-salto { padding: 0 2px; }
.pagination .pag-info { margin-left: 8px; }
@media (max-width: 767px) { .pagination .pag-info { width: 100%; margin: 4px 0 0; } }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; max-width: 760px; }
.form-grid.full { grid-template-columns: 1fr; }
.form-group { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.form-group label { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.form-group.span2 { grid-column: span 2; }
textarea { resize: vertical; }

.login-box {
  max-width: 360px; margin: 10vh auto; background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; padding: 30px; box-shadow: 0 6px 30px rgba(20,25,40,.08);
}
.login-box h1 { text-align: center; font-size: 18px; margin-bottom: 20px; }
.login-box .login-logo { display: block; margin: 0 auto 12px; max-height: 64px; max-width: 200px; object-fit: contain; }
.login-box .form-group { margin-bottom: 14px; }
.login-box .btn { width: 100%; }

.note { color: var(--muted); font-size: 12.5px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radio); padding: 10px 14px; margin-bottom: 14px; }
table .note, .form-group .note, .toolbar .note, .zona-drop .note, .zona-drop + div .note, p .note, li .note, .card .note, h1 .note { display: inline; background: none; border: none; border-radius: 0; padding: 0; margin: 0; }
.form-group .note { display: block; margin-top: 2px; }

.lineas-table input, .lineas-table select { width: 100%; }
.lineas-table td { vertical-align: middle; }
.lineas-table .col-desc { min-width: 200px; }
.lineas-table .col-num { width: 90px; }

.totales { max-width: 360px; margin-left: auto; margin-top: 14px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radio); padding: 10px 14px; }
.totales div { display: flex; justify-content: space-between; padding: 4px 0; font-size: 13.5px; }
.totales .total { font-weight: 700; font-size: 16px; border-top: 1px solid var(--border); margin-top: 6px; padding-top: 10px; }

.top-list { list-style: none; padding: 0; margin: 0; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radio); }
.top-list li { display: flex; justify-content: space-between; padding: 9px 14px; border-bottom: 1px solid var(--border); font-size: 13.5px; }
.top-list li:last-child { border-bottom: none; }
.top-list .n { color: var(--accent2); font-variant-numeric: tabular-nums; }

.btn-filtrar { display: none; }
.tag { display: inline-block; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; padding: 1px 7px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); vertical-align: 1px; }

.avisos { display: flex; flex-direction: column; gap: 6px; margin-bottom: 18px; }
.aviso { display: block; padding: 10px 14px; border-radius: var(--radio); border: 1px solid var(--border); background: var(--panel); color: var(--text); font-size: 13.5px; box-shadow: var(--sombra); }
.aviso:hover { border-color: var(--accent); text-decoration: none; }
.aviso-error { border-left: 4px solid var(--danger); }
.aviso-info { border-left: 4px solid var(--accent); }

/* Pestañas (Ajustes) */
.tabs { display: flex; gap: 4px; flex-wrap: wrap; border-bottom: 1px solid var(--border); margin-bottom: 18px; overflow-x: auto; }
.tabs a { padding: 9px 13px; border-radius: 8px 8px 0 0; color: var(--muted); font-weight: 500; white-space: nowrap; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a:hover { color: var(--text); text-decoration: none; background: var(--panel2); }
.tabs a.activa { color: var(--accent); border-bottom-color: var(--accent); }
.tab { display: none; }
.tab.activa { display: block; }
.tab > h2:first-child { margin-top: 4px; }

/* ---------- Tablet (menú lateral estrecho, solo iconos) ---------- */
@media (max-width: 1100px) and (min-width: 768px) {
  :root { --ancho-menu: 68px; }
  .brand { padding: 14px 15px; justify-content: center; }
  .brand-text, .menu-grupo, .menu-item span { display: none; }
  .menu-bloque.cerrado .menu-item { display: flex; }
  .menu-item { justify-content: center; padding: 10px; }
  .menu-item svg { width: 22px; height: 22px; }
  .menu { padding: 8px 8px; }
  main { padding: 18px 18px 50px; }
}

/* ---------- Móvil (menú en cajón deslizante) ---------- */
@media (max-width: 767px) {
  body.con-menu { display: block; }
  .sidebar {
    position: fixed; top: 0; left: 0; height: 100vh; height: 100dvh; width: min(300px, 86vw); z-index: 40;
    transform: translateX(-102%); transition: transform .2s ease; box-shadow: 0 0 30px rgba(0,0,0,.35);
  }
  .nav-toggle:checked ~ .sidebar { transform: none; }
  .nav-backdrop { display: block; position: fixed; inset: 0; background: rgba(10,12,18,.45); z-index: 30; opacity: 0; pointer-events: none; transition: opacity .2s; }
  .nav-toggle:checked ~ .nav-backdrop { opacity: 1; pointer-events: auto; }
  .nav-toggle-btn { display: inline-flex; }
  .topbar { padding: 0 12px; }
  .topbar-acciones .btn { padding: 6px 10px; min-height: 34px; font-size: 13px !important; }
  main { padding: 14px 14px 48px; }
  h1 { font-size: 19px; }

  .cards { grid-template-columns: 1fr 1fr; gap: 10px; }
  .card { padding: 12px 14px; }
  .card .num { font-size: 20px; }

  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar form { flex-direction: column; }
  .toolbar form > *, .toolbar > span > .btn { width: 100%; }
  .toolbar > span { flex-direction: column; }

  .form-grid { grid-template-columns: 1fr !important; max-width: 100%; }
  .form-group.span2 { grid-column: span 1; }
  input, select, textarea, button, .btn { font-size: 16px !important; }
  .btn { padding: 11px 16px; min-height: 44px; }
  .btn-small { min-height: 36px; padding: 6px 12px; }

  .totales { max-width: 100%; }

  table:not(.lineas-table) thead { display: none; }
  table:not(.lineas-table), table:not(.lineas-table) tbody, table:not(.lineas-table) tr, table:not(.lineas-table) td { display: block; width: 100%; }
  table:not(.lineas-table) { border: none; background: none; box-shadow: none; }
  table:not(.lineas-table) tr { border: 1px solid var(--border); border-radius: var(--radio); margin-bottom: 10px; padding: 8px 12px; background: var(--panel); box-shadow: var(--sombra); }
  table:not(.lineas-table) tbody tr:nth-child(even) > td, table:not(.lineas-table) tbody tr:hover > td { background: transparent; }
  table:not(.lineas-table) tbody tr:hover { background: var(--panel); }
  table:not(.lineas-table) td { border: none; padding: 6px 0; display: flex; justify-content: space-between; align-items: center; gap: 10px; }
  table:not(.lineas-table) td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; flex: 0 0 auto; }
  table:not(.lineas-table) td[data-label] { flex-wrap: wrap; }
  table:not(.lineas-table) td[data-label] > .celda { flex: 1 1 auto; text-align: right; min-width: 0; overflow-wrap: anywhere; }
  table:not(.lineas-table) td[data-label] > .celda .note { display: block; }
  table:not(.lineas-table) td[data-label=""] { justify-content: flex-start; flex-wrap: wrap; gap: 6px; }
  table:not(.lineas-table) td[data-label=""]::before { display: none; }

  .lineas-table thead { display: none; }
  .lineas-table, .lineas-table tbody, .lineas-table tr, .lineas-table td { display: block; width: 100%; }
  .lineas-table { border: none; background: none; box-shadow: none; }
  .lineas-table tr { border: 1px solid var(--border); border-radius: var(--radio); margin-bottom: 12px; padding: 10px; background: var(--panel); }
  .lineas-table td { border: none; padding: 6px 0; }
  .lineas-table td[data-label]::before { content: attr(data-label); display: block; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 3px; }
  .lineas-table input, .lineas-table select { width: 100%; }

  .tabs { gap: 0; }
  .tabs a { padding: 9px 10px; font-size: 13px; }
}
@media (max-width: 360px) {
  .cards { grid-template-columns: 1fr; }
}
/* Filtros plegables en móvil (layout_bottom.php añade el botón) */
@media (max-width: 767px) {
  .toolbar form.filtros-plegados { display: none; }
  .btn-filtros { width: 100%; }
}
@media (min-width: 768px) { .btn-filtros { display: none; } }

/* Barra flotante de selección múltiple (listado de facturas) */
.barra-seleccion { position: fixed; left: 50%; bottom: 18px; transform: translate(-50%, 120%); transition: transform .18s; z-index: 25; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: center; max-width: calc(100vw - 32px); padding: 10px 14px; background: var(--panel); border: 1px solid var(--border2); border-radius: 12px; box-shadow: 0 8px 30px rgba(20,25,40,.18); font-size: 13.5px; }
.barra-seleccion.visible { transform: translate(-50%, 0); }
tbody tr:has(input.sel:checked) { background: var(--accent-soft) !important; }
@media (max-width: 767px) { .barra-seleccion { left: 12px; right: 12px; transform: translate(0, 120%); flex-wrap: wrap; } .barra-seleccion.visible { transform: none; } .sel-celda { justify-content: flex-start !important; } .sel-celda::before { display: none; } }


/* Filas destacadas (candidatos de conciliación): proveedor + importe exacto, o solo importe exacto */
table tbody tr.fila-match > td, table tbody tr.fila-match:hover > td { background: #e3f6ea; box-shadow: inset 4px 0 0 var(--accent2); }
table tbody tr.fila-match-suave > td, table tbody tr.fila-match-suave:hover > td { background: #f1f9f4; }
html[data-tema="oscuro"] table tbody tr.fila-match > td, html[data-tema="auto"] table tbody tr.fila-match > td { background: #14301f; }
html[data-tema="oscuro"] table tbody tr.fila-match-suave > td, html[data-tema="auto"] table tbody tr.fila-match-suave > td { background: #13231a; }
@media (max-width: 767px) { tr.fila-match { border-left: 4px solid var(--accent2); } tr.fila-match > td { box-shadow: none; } }

/* Cola de facturas: nombres de fichero largos y observaciones de la IA no deben deformar la tabla */
#cola td.col-fichero { max-width: 300px; }
#cola td.col-fichero .celda, #cola td.col-estado .celda { display: block; }
#cola td.col-fichero .note, #cola td.col-estado .note { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 280px; }
#cola td.col-estado { max-width: 230px; }
#cola td.acciones { width: 1%; min-width: 160px; }
#cola td.acciones .btn, #cola td.acciones form { display: inline-flex; margin: 2px 2px 2px 0; }
@media (max-width: 767px) {
  #cola td.col-fichero, #cola td.col-estado { max-width: none; }
  #cola td.col-fichero .note, #cola td.col-estado .note { white-space: normal; max-width: none; }
  #cola td.acciones { width: auto; min-width: 0; }
}
