:root {
  --bg: #f3f5f9;
  --card: #ffffff;
  --text: #1c2330;
  --muted: #5d6879;
  --border: #dbe1ea;
  --accent: #1d4ed8;
  --accent-text: #ffffff;
  --accent-soft: #e9f0ff;
  --warn-bg: #fff6d6;
  --warn: #8a6100;
  --good: #0f7a3d;
  --bad: #b42318;
  --logo-bg: transparent;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 2px 8px rgba(16, 24, 40, 0.04);
  color-scheme: light;
}

/* Modo oscuro: letras blancas y detalles en el naranja del logotipo.
   Se activa con el botón de la cabecera, o solo si el ordenador está en modo oscuro y se eligió «Automático». */
:root[data-tema="oscuro"] {
    color-scheme: dark;
    --bg: #121110;
    --card: #1c1a18;
    --text: #f4f1ec;
    --muted: #b9b1a7;
    --border: #3a2f25;
    --accent: #ff8a1f;
    --accent-text: #1a0f04;
    --accent-soft: #33230f;
    --warn-bg: #3d2c0b;
    --warn: #ffd37a;
    --good: #7ddc8f;
    --bad: #ff8a80;
    --logo-bg: #ffffff;
    --shadow: none;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-tema="claro"]) {
      color-scheme: dark;
      --bg: #121110;
      --card: #1c1a18;
      --text: #f4f1ec;
      --muted: #b9b1a7;
      --border: #3a2f25;
      --accent: #ff8a1f;
      --accent-text: #1a0f04;
      --accent-soft: #33230f;
      --warn-bg: #3d2c0b;
      --warn: #ffd37a;
      --good: #7ddc8f;
      --bad: #ff8a80;
      --logo-bg: #ffffff;
      --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.nowrap { white-space: nowrap; }
.right-text { text-align: right; }
.mono { font-family: ui-monospace, Consolas, monospace; font-size: 0.85rem; }
code { background: var(--accent-soft); padding: 0 4px; border-radius: 4px; }
a { color: var(--accent); }
h2 { font-size: 1.05rem; margin: 0 0 10px; }

/* ---- Cabecera ---- */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
  background: var(--card); border-bottom: 1px solid var(--border);
  padding: 8px 16px;
}
.brand img { max-width: 150px; object-fit: contain; }
.brand span { color: var(--muted); font-weight: 600; border-left: 1px solid var(--border); padding-left: 10px; }
.brand img, .logo-row img { background: var(--logo-bg); border-radius: 6px; padding: 2px 6px; }
.logo-row { display: flex; gap: 10px 16px; align-items: center; margin-bottom: 6px; flex-wrap: wrap; }
.logo-row img { max-height: 70px; max-width: 220px; border: 1px solid var(--border); border-radius: 8px; padding: 6px; background: #fff; }
.brand { text-decoration: none; color: inherit; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; }
.tabs { display: flex; gap: 2px; flex-wrap: wrap; }
.tabs a {
  text-decoration: none; color: var(--muted); padding: 7px 12px; border-radius: 8px; font-weight: 500;
}
.tabs a:hover { background: var(--accent-soft); color: var(--text); }
.tabs a.active { background: var(--accent); color: var(--accent-text); }
.banner { background: var(--warn-bg); color: var(--warn); padding: 8px 16px; font-size: 0.9rem; }
.banner a { color: inherit; font-weight: 600; }

.screen { padding: 16px; max-width: 1500px; margin: 0 auto; }
.pane {
  background: var(--card); border: 1px solid var(--border); border-radius: 12px;
  padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.pane-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.pane-head h2 { margin: 0; }

/* ---- Pantalla principal: dos zonas ---- */
.split { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.split .pane { margin: 0; }
.right { position: sticky; top: 64px; max-height: calc(100vh - 80px); overflow: auto; }
.left.dropping { outline: 2px dashed var(--accent); outline-offset: 4px; }
.drop-hint {
  border: 1px dashed var(--border); border-radius: 10px; padding: 10px; text-align: center;
  color: var(--muted); font-size: 0.85rem; margin: 10px 0;
}
.left.dropping .drop-hint { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

/* ---- Formularios ---- */
label { display: flex; flex-direction: column; gap: 3px; font-size: 0.8rem; color: var(--muted); margin-bottom: 8px; }
label.check { flex-direction: row; align-items: center; gap: 6px; margin: 0; font-size: 0.9rem; }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 9px; width: 100%; min-width: 0;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: var(--card); }
input:disabled { opacity: 0.5; }
input[type="checkbox"] { width: auto; }
textarea { resize: vertical; }
output { display: block; padding: 7px 0; font-weight: 600; color: var(--text); font-size: 0.95rem; }
.grid { display: grid; gap: 0 12px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.s2 { grid-column: span 2; }
.cab { grid-template-columns: 2fr 1fr 1fr 1fr; }
.mini { width: 64px; padding: 2px 6px; display: inline-block; }
.search { font-size: 1rem; padding: 10px 12px; }
.search.big { font-size: 1.2rem; padding: 14px 16px; }
.narrow { max-width: 280px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.filters > * { width: auto; flex: 1 1 140px; }
.filters input[data-f] { flex: 0 1 120px; }
.warn { background: var(--warn-bg); color: var(--warn); padding: 8px 10px; border-radius: 8px; }

/* ---- Botones ---- */
.btn {
  font: inherit; font-weight: 500; border: 1px solid var(--accent); background: var(--accent); color: var(--accent-text);
  padding: 7px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.btn:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.small { padding: 3px 9px; font-size: 0.82rem; }
.btn:disabled { opacity: 0.45; cursor: default; }
.btns { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.actions { margin-top: 12px; }
.icon { border: none; background: transparent; color: var(--muted); cursor: pointer; font-size: 1rem; padding: 2px 6px; border-radius: 6px; }
.icon:hover { background: var(--accent-soft); color: var(--text); }
.file-btn { display: inline-block; margin: 0; font-size: inherit; color: var(--accent); }
.file-btn input { display: none; }

/* ---- Partidas del presupuesto ---- */
.lines { display: flex; flex-direction: column; gap: 10px; margin-top: 6px; }
.line { border: 1px solid var(--border); border-radius: 10px; padding: 10px; background: var(--card); }
.line.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.line-head { display: flex; gap: 8px; align-items: flex-start; margin-bottom: 8px; }
.line-head .n, .line-head .tools { margin-top: 7px; } /* a la altura de la primera línea del artículo */
.line-head .n { font-weight: 700; color: var(--muted); width: 1.4em; text-align: center; }
.line-head .art { flex: 4; font-weight: 600; resize: none; overflow: hidden; line-height: 1.35; min-height: 0; }
.line-head .cat { flex: 1; }
.tools { display: flex; }
.line-grid { display: grid; grid-template-columns: 2fr repeat(3, minmax(0, 1fr)); gap: 0 8px; }
.line-grid .w2 { grid-column: span 1; }
.line-grid.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); margin-top: 6px; }
.line-grid input { padding: 6px 7px; }
.line-grid > label { justify-content: flex-end; } /* si una etiqueta ocupa dos líneas, las casillas siguen alineadas */
.line textarea { margin-top: 2px; }
.line details summary { cursor: pointer; color: var(--muted); font-size: 0.82rem; margin-top: 6px; }
.refnote { margin-top: 6px; font-size: 0.8rem; color: var(--muted); }
.foot { margin-top: 14px; align-items: start; }
.totals div { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--border); }
.totals .grand { font-size: 1.25rem; border-bottom: none; }

/* ---- Resultados del histórico ---- */
.results { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.results.wide { display: grid; grid-template-columns: repeat(auto-fill, minmax(460px, 1fr)); }
.res {
  display: flex; gap: 12px; justify-content: space-between; border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; background: var(--card); cursor: grab;
}
.res:hover { border-color: var(--accent); }
.res-main { min-width: 0; flex: 1; }
.res-title { font-weight: 600; }
.res-desc { color: var(--muted); font-size: 0.85rem; margin-top: 2px; }
.res-meta { color: var(--muted); font-size: 0.8rem; margin-top: 4px; }
.res-side { text-align: right; display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.res-side .btns { margin-top: 6px; justify-content: flex-end; }
.price { font-size: 1.25rem; font-weight: 700; }
.pm2 { font-size: 0.85rem; font-weight: 600; color: var(--accent); }
.sim { font-size: 0.75rem; color: var(--muted); }
.tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 5px; }
.tag { font-size: 0.75rem; padding: 1px 7px; border-radius: 999px; background: var(--accent); color: var(--accent-text); }
.tag.soft { background: var(--accent-soft); color: var(--text); }
.tag.warn { background: var(--warn-bg); color: var(--warn); }
.tag.fact { background: var(--good); color: var(--card); font-weight: 600; }

/* ---- Comparación de precios ---- */
.stats { border: 1px solid var(--border); border-radius: 10px; padding: 12px; background: var(--accent-soft); margin-top: 6px; }
.stats-head { font-weight: 600; margin-bottom: 8px; }
.stats-nota { font-weight: 400; font-size: 0.8rem; margin-left: 6px; }
/* Precios y evolución por años en una sola franja (se parte en varias líneas si no cabe). */
.stats-fila { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: stretch; }
.kpis { display: flex; flex-wrap: wrap; gap: 8px; }
.kpis > div { min-width: 104px; }
.kpis div { background: var(--card); border-radius: 8px; padding: 7px 9px; display: flex; flex-direction: column; }
.kpis span { font-size: 0.75rem; color: var(--muted); }
.kpis strong { font-size: 1.1rem; }
.kpis small { font-size: 0.72rem; color: var(--muted); }
.kpis .ref { outline: 1px dashed var(--accent); }
.evo { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.evo > div { display: flex; flex-direction: column; background: var(--card); border-radius: 8px; padding: 5px 9px; }
.evo .y { font-size: 0.75rem; color: var(--muted); }
.evo small { font-size: 0.72rem; color: var(--muted); }
.evo .arrow { color: var(--muted); }
.evo .var { color: var(--accent); font-weight: 700; }
.stats p { margin: 8px 0 0; }

/* ---- Tablas ---- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
table.data th { text-align: left; font-weight: 600; color: var(--muted); border-bottom: 2px solid var(--border); padding: 6px 8px; white-space: nowrap; font-size: 0.8rem; }
table.data td { border-bottom: 1px solid var(--border); padding: 6px 8px; vertical-align: top; }
table.data tr.flag td { background: var(--warn-bg); }
.num { text-align: right; }
table.data th.num { text-align: right; }
.clamp { max-width: 420px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
table.edit td { padding: 3px; }
table.edit input { padding: 5px 6px; font-size: 0.88rem; }
table.edit td.num input { width: 80px; text-align: right; }
table.edit td.wide { min-width: 300px; }
table.edit input.sub { margin-top: 3px; font-size: 0.8rem; color: var(--muted); }
table.edit td[data-o] { white-space: nowrap; padding-top: 9px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.chip { font: inherit; border: 1px solid var(--border); background: var(--card); color: var(--text); border-radius: 999px; padding: 4px 12px; cursor: pointer; }
.chip.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.chip small { opacity: 0.7; }

/* ---- Importar ---- */
.dropzone {
  border: 2px dashed var(--border); border-radius: 12px; padding: 30px; text-align: center; cursor: pointer;
  align-items: center; gap: 6px; font-size: 1rem; color: var(--text); margin-bottom: 12px;
}
.dropzone.over, .dropzone:hover { border-color: var(--accent); background: var(--accent-soft); }
.dropzone input { display: none; }
details summary { cursor: pointer; }
pre { white-space: pre-wrap; font-size: 0.8rem; max-height: 320px; overflow: auto; background: var(--bg); padding: 8px; border-radius: 8px; }

/* ---- Modal y avisos ---- */
dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 18px; background: var(--card); color: var(--text);
  width: min(860px, 94vw); max-height: 90vh; overflow: auto; box-sizing: border-box;
}
@media (max-width: 700px) { dialog { padding: 14px; } dialog .pane-head { flex-wrap: wrap; } }
dialog::backdrop { background: rgba(10, 15, 25, 0.45); }
dialog.visor { width: min(1000px, 96vw); }
.visor-paginas { display: flex; flex-direction: column; gap: 12px; align-items: center; max-height: 72vh; overflow: auto; background: var(--bg); padding: 12px; border-radius: 8px; }
.visor-paginas canvas { width: 100%; max-width: 860px; height: auto; background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.18); }
.visor-estado { min-height: 1.2em; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--card); padding: 10px 16px; border-radius: 10px; font-size: 0.9rem;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

@media (max-width: 1100px) {
  .split { grid-template-columns: 1fr; }
  .right { position: static; max-height: none; }
  .line-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .results.wide { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .g2 { grid-template-columns: 1fr; }
  .g4 { grid-template-columns: 1fr 1fr; }
  .g4 .s2 { grid-column: span 2; }
  .cab { grid-template-columns: 1fr 1fr 1fr; }
  .cab > :first-child { grid-column: 1 / -1; }
  .g2 .s2 { grid-column: auto; }
  .line-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .res { flex-direction: column; }
  .res-side { align-items: flex-start; text-align: left; }
}

/* ---- Tarifa ---- */
table.tarifa tr.cat td { background: var(--accent-soft); font-weight: 700; padding: 0; }
.cat-btn {
  width: 100%; display: flex; align-items: center; gap: 8px; text-align: left; cursor: pointer;
  background: transparent; border: none; color: var(--text); font: inherit; font-weight: 700; padding: 10px 8px;
}
.cat-btn:hover { color: var(--accent); }
.cat-btn .flecha { display: inline-block; transition: transform 0.15s; color: var(--accent); }
tr.cat.abierta .flecha { transform: rotate(90deg); }
.cat-n { margin-left: auto; font-weight: 400; font-size: 0.85rem; color: var(--muted); }
table.tarifa tr.cat + tr.cat td { border-top: 1px solid var(--card); }
table.tarifa tr.ej td { background: var(--bg); font-size: 0.82rem; }
table.tarifa input { width: 100px; text-align: right; }
table.tarifa input.fijado { border-color: var(--accent); font-weight: 700; }
table.tarifa .precio-tarifa input { font-weight: 600; color: var(--text); }
.nota-precio { display: block; font-size: 0.72rem; color: var(--muted); margin-top: 2px; white-space: nowrap; }
.nota-precio.fijo { color: var(--accent); font-weight: 600; }
.mini-icon { min-width: 0 !important; min-height: 0 !important; padding: 0 4px; font-size: 0.9rem; color: inherit; }
.aviso-navegador { display: none; background: var(--warn-bg); color: var(--warn); border-bottom: 1px solid var(--border); padding: 8px 16px; font-size: 0.88rem; }
body.modo-navegador .aviso-navegador { display: block; } /* solo cuando ya se sabe que no hay servidor */
.aviso-ajuste { background: var(--accent-soft); border-left: 3px solid var(--accent); padding: 8px 12px; border-radius: 6px; font-weight: 600; margin: 6px 0; }
table.tarifa .nombre { cursor: pointer; }
table.tarifa .nombre:hover { color: var(--accent); }

/* ---- Impresión ---- */
.print-only { display: none; }
@media print {
  /* Sin margen de página: el navegador no tiene dónde poner su fecha, título y dirección. */
  @page { size: A4; margin: 0; }
  html, body { margin: 0; padding: 0; }
  .p-pagina { width: 100%; border-collapse: collapse; }
  .p-pagina > thead > tr > td, .p-pagina > tfoot > tr > td, .p-pagina > tbody > tr > td { padding: 0 15mm; border: none; }
  .p-margen { height: 14mm; }
  .p-originales img { display: block; width: 210mm; height: 296mm; object-fit: contain; break-after: page; }
  .p-originales img:last-child { break-after: auto; }
  .p-firma { margin-top: 10mm; width: 75mm; margin-left: auto; border-top: 1px solid #555; padding-top: 2mm; display: flex; flex-direction: column; gap: 1mm; break-inside: avoid; }
  .p-firma span { font-size: 9pt; color: #555; }
  .p-firma img { max-width: 70mm; max-height: 28mm; object-fit: contain; align-self: flex-start; }
  .p-firma small { color: #555; }
  body { background: #fff; color: #000; font-size: 10.5pt; }
  .app, dialog, .toast { display: none !important; }
  .print-only { display: block; }
  .p-head { display: flex; justify-content: space-between; border-bottom: 2px solid #1d4ed8; padding-bottom: 8px; margin-bottom: 14px; }
  .p-head h1 { font-size: 12pt; margin: 0 0 4px; }
  .p-logo { max-height: 22mm; max-width: 70mm; display: block; margin-bottom: 6px; }
  .p-head p { margin: 0; color: #444; }
  .p-meta { text-align: right; }
  .p-meta h2 { color: #1d4ed8; margin: 0 0 4px; font-size: 14pt; }
  .p-client { margin-bottom: 14px; display: flex; flex-direction: column; color: #444; }
  .p-client span { font-size: 8.5pt; text-transform: uppercase; color: #777; }
  .p-client strong { color: #000; font-size: 11.5pt; }
  .p-table { width: 100%; border-collapse: collapse; }
  .p-table th { background: #e9f0ff; text-align: left; padding: 6px; }
  .p-table th.num, .p-table td.num { text-align: right; white-space: nowrap; }
  .p-table td { border-bottom: 1px solid #ddd; padding: 6px; vertical-align: top; }
  .p-table tr { break-inside: avoid; }
  .p-det { color: #555; font-size: 9pt; margin-top: 2px; }
  .p-totals { margin: 12px 0 0 auto; border-collapse: collapse; min-width: 250px; }
  .p-totals td { padding: 4px 6px; }
  .p-totals td.num { text-align: right; }
  .p-totals .grand td { font-weight: 700; font-size: 12.5pt; border-top: 2px solid #000; }
  .p-notes { margin-top: 14px; color: #333; }
  .t-cat { margin: 14px 0 4px; font-size: 11.5pt; color: #1d4ed8; border-bottom: 1px solid #1d4ed8; break-after: avoid; }
  .t-table { width: 100%; border-collapse: collapse; }
  .t-table td { border-bottom: 1px solid #e3e3e3; padding: 3px 6px; }
  .t-table td.num { text-align: right; white-space: nowrap; width: 90px; }
  .t-table td.ud { color: #555; width: 50px; }
  .t-table tr { break-inside: avoid; }
}

/* ---- Entrada y sesión ---- */
.top-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.sesion { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 0.85rem; }
.login { position: fixed; inset: 0; z-index: 100; background: var(--bg); display: grid; place-items: center; padding: 16px; }
.login-box {
  width: min(360px, 100%); background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 24px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 4px; text-align: center;
}
.login-box img { align-self: center; max-width: 200px; object-fit: contain; }
.login-box h1 { font-size: 1.2rem; margin: 6px 0 12px; }
.login-box label { text-align: left; }
.login-box .btn { margin-top: 8px; padding: 10px; }
.login-box img { background: var(--logo-bg); border-radius: 6px; padding: 4px 8px; }
body:not(.modo-servidor) .solo-servidor, body.modo-servidor .solo-navegador { display: none; }

/* ---- Verificación en dos pasos ---- */
.qr { background: #fff; padding: 10px; border-radius: 10px; display: inline-block; line-height: 0; }
.qr svg { width: 200px; height: 200px; }
.mfa-pasos { display: grid; grid-template-columns: auto 1fr; gap: 16px; align-items: start; }
.secreto { font-family: ui-monospace, Consolas, monospace; font-size: 1rem; letter-spacing: 1px; background: var(--bg); padding: 6px 10px; border-radius: 8px; word-break: break-all; }
.codigos { display: grid; grid-template-columns: repeat(2, auto); gap: 6px 24px; font-family: ui-monospace, Consolas, monospace; font-size: 1.05rem; background: var(--bg); padding: 12px 16px; border-radius: 8px; justify-content: start; }
.mfa-politica { margin-top: 12px; }
#loginCodigo { font-size: 1.3rem; letter-spacing: 3px; text-align: center; }
@media (max-width: 640px) { .mfa-pasos { grid-template-columns: 1fr; } }

/* ---- Cabecera y menú en el móvil ---- */
[hidden] { display: none !important; }
.btn-menu, .seccion-movil, .solo-movil { display: none; }
.btn-menu {
  margin-left: auto; align-items: center; gap: 8px; font: inherit; font-weight: 600; cursor: pointer;
  background: var(--accent-soft); color: var(--text); border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px;
}
.hamburguesa, .hamburguesa::before, .hamburguesa::after {
  display: block; width: 18px; height: 2px; background: currentColor; border-radius: 2px; position: relative;
}
.hamburguesa::before, .hamburguesa::after { content: ""; position: absolute; left: 0; }
.hamburguesa::before { top: -6px; }
.hamburguesa::after { top: 6px; }
.menu-abierto .hamburguesa { background: transparent; }
.menu-abierto .hamburguesa::before { top: 0; transform: rotate(45deg); }
.menu-abierto .hamburguesa::after { top: 0; transform: rotate(-45deg); }

.menu-movil { position: fixed; inset: 0; top: var(--alto-cabecera, 56px); z-index: 40; background: rgba(10, 12, 18, 0.45); }
.menu-panel {
  background: var(--card); border-bottom: 1px solid var(--border); padding: 8px 12px 16px;
  max-height: calc(100vh - var(--alto-cabecera, 56px)); overflow-y: auto; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}
.menu-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.menu-nav a {
  display: flex; align-items: center; min-height: 48px; padding: 10px 12px; border-radius: 10px; text-decoration: none;
  color: var(--text); background: var(--bg); border: 1px solid var(--border); font-weight: 600;
}
.menu-nav a.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.menu-bloque { margin-top: 14px; display: flex; flex-direction: column; gap: 6px; }
.menu-titulo { font-size: 0.8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.menu-sesion { flex-direction: row; align-items: center; justify-content: space-between; color: var(--muted); }
.segmentado { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.segmentado button { font: inherit; min-height: 44px; border: none; background: var(--bg); color: var(--text); cursor: pointer; }
.segmentado button + button { border-left: 1px solid var(--border); }
.segmentado button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); font-weight: 700; }

@media (max-width: 860px) {
  .top { flex-wrap: nowrap; gap: 10px; padding: 8px 12px; min-height: 56px; }
  .top .tabs, .top .top-right { display: none; }
  .btn-menu { display: flex; }
  .brand span { display: none; }
  .brand img { max-height: 30px; max-width: 110px; }
  .seccion-movil {
    display: block; min-width: 0; flex: 1; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    border-left: 1px solid var(--border); padding-left: 10px;
  }
  .solo-movil { display: inline-flex; }
  .screen { padding: 10px; }
  .pane { padding: 12px; border-radius: 10px; margin-bottom: 12px; }
  .split { gap: 12px; }
  .pane-head h2 { font-size: 1rem; }
  /* Partida: artículo a lo ancho; categoría y botones debajo. */
  .line { padding: 8px; }
  .line-head { flex-wrap: wrap; }
  .line-head .art { flex: 1 1 calc(100% - 2.2em); order: 1; }
  .line-head .n { order: 0; }
  .line-head .cat { flex: 1 1 0; width: auto; min-width: 0; order: 2; margin-left: 2em; }
  .line-head .tools { order: 3; }
  .icon { min-width: 36px; min-height: 36px; }
  .btn { min-height: 40px; }
  .btn.small { min-height: 34px; }
  .filters > * { flex: 1 1 45%; }
  .results.wide { grid-template-columns: 1fr; }
  table.data { font-size: 0.85rem; }
  .totals .grand { font-size: 1.1rem; }
  .ver-refs { width: 100%; justify-content: center; margin-top: 8px; }
}
@media (min-width: 861px) { .menu-movil { display: none !important; } }
.pane, .line { scroll-margin-top: calc(var(--alto-cabecera, 56px) + 10px); }

/* ---- Aviso si la aplicación no arranca ---- */
.aviso-arranque { position: fixed; left: 12px; right: 12px; bottom: 12px; z-index: 300; background: var(--warn-bg); color: var(--warn);
  border: 1px solid var(--warn); border-radius: 10px; padding: 12px 14px; box-shadow: 0 6px 24px rgba(0,0,0,.25); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.aviso-arranque small { opacity: 0.8; }

/* ---- Buscador histórico en tabla ---- */
.tabla-hist { margin-top: 6px; }
.tabla-hist td { vertical-align: top; }
.tabla-hist tbody tr:hover td { background: var(--accent-soft); }
.tabla-hist .col-trabajo { min-width: 260px; max-width: 520px; }
.tabla-hist .t-titulo { font-weight: 600; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.tabla-hist .t-desc { color: var(--muted); font-size: 0.82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 520px; }
.tabla-hist .t-meta { display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: center; margin-top: 3px; color: var(--muted); font-size: 0.78rem; }
.tabla-hist .t-meta .tag { font-size: 0.72rem; }
.tabla-hist .precio-ud { font-weight: 700; font-size: 1.02rem; }
.tabla-hist .col-sim { width: 90px; white-space: nowrap; }
.tabla-hist.sin-parecido .col-sim { display: none; }
.sim-barra { display: inline-block; width: 46px; height: 6px; border-radius: 3px; background: var(--border); vertical-align: middle; margin-right: 6px; overflow: hidden; }
.sim-barra i { display: block; height: 100%; background: var(--accent); }
.tabla-hist .acciones .btn { padding: 4px 10px; }

/* ---- Permisos: lo que el usuario no puede usar no se ve ---- */
body.sin-editar a[data-tab="nuevo"], body.sin-editar [data-use], body.sin-editar [data-anadir], body.sin-editar [data-abrir],
body.sin-editar [data-dup], body.sin-editar [data-edit], body.sin-editar [data-nuevo], body.sin-editar #aRecalcular,
body.sin-editar #cNuevo,
body.sin-borrar [data-del],
body.sin-importar #iInicio, body.sin-importar #iRevision, body.sin-importar #iReparar, body.sin-borrar #iReparar,
body.sin-copias #iCopia,
body.sin-importar-ni-copias a[data-tab="importar"],
body.sin-facturas a[data-tab="facturas"], body.sin-facturas #pTipos [data-t="factura"], body.sin-facturas [data-f="tipo"],
body.sin-facturas option[value="factura"],
body.sin-tarifa [data-auto], body.sin-tarifa [data-ocultar], body.sin-tarifa label:has(#tAjuste), body.sin-tarifa label:has(#tOcultos),
body.sin-ajustes #ajEmpresa, body.sin-ajustes #ajSinonimos, body.sin-ajustes [data-ir="ajEmpresa"], body.sin-ajustes [data-ir="ajSinonimos"] { display: none !important; }
/* Sin permiso de tarifa, el precio se ve pero no se puede cambiar. */
body.sin-tarifa [data-precio] { pointer-events: none; border-color: transparent; background: transparent; }
.permisos { border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px 10px; margin: 4px 0 8px; }
.permisos legend { font-weight: 600; padding: 0 4px; }
.lista-permisos { display: grid; gap: 6px; }
.lista-permisos .admin-permiso { border-top: 1px dashed var(--border); padding-top: 6px; margin-top: 2px; }
.plantillas { margin-bottom: 4px; }

/* ---- Firmas y partes de trabajo ---- */
.tag.firma-ok { background: #dff3e4; color: #17663a; font-weight: 600; }
.tag.firma-pend { background: var(--warn-bg); color: var(--warn); font-weight: 600; }
.tag.firma-no { background: #fde2e2; color: #a12020; font-weight: 600; }
:root[data-tema="oscuro"] .tag.firma-ok { background: #173b25; color: #8fe0ad; }
:root[data-tema="oscuro"] .tag.firma-no { background: #4a1c1c; color: #ffb3b3; }
@media (prefers-color-scheme: dark) { :root:not([data-tema="claro"]) .tag.firma-ok { background: #173b25; color: #8fe0ad; } :root:not([data-tema="claro"]) .tag.firma-no { background: #4a1c1c; color: #ffb3b3; } }
body:not(.modo-servidor) [data-firmas], body:not(.modo-servidor) a[data-tab="partes"], body:not(.modo-servidor) .solo-servidor-celda,
body.sin-firmas [data-firmas], body.sin-firmas a[data-tab="partes"], body.sin-firmas [data-pt-firmar], body.sin-firmas [data-pt-enviar],
body.sin-editar [data-editar-boton] { display: none !important; }
/* Operario: solo partes de trabajo (y su cuenta en Ajustes). */
body.operario a[data-tab]:not([data-tab="partes"]):not([data-tab="ajustes"]) { display: none !important; }
body.operario #ajIndice, body.operario .mfa-politica { display: none !important; }

dialog.firma-dlg { width: min(760px, 96vw); }
.firma-doc { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin: 8px 0 12px; background: var(--bg); }
.firma-doc-cab { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
.firma-lineas { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.firma-lineas th { text-align: left; color: var(--muted); font-size: 0.78rem; font-weight: 600; border-bottom: 1px solid var(--border); padding: 4px; }
.firma-lineas td { padding: 6px 4px; border-bottom: 1px solid var(--border); vertical-align: top; }
.firma-lineas .num { text-align: right; white-space: nowrap; }
.firma-lineas tfoot td { border: none; text-align: right; }
.firma-zona { border: 2px dashed var(--accent); border-radius: 12px; padding: 6px 8px 8px; background: var(--card); }
.firma-pad-cab { display: flex; justify-content: space-between; align-items: center; font-size: 0.85rem; color: var(--muted); margin-bottom: 4px; }
.firma-pad { display: block; width: 100%; height: 190px; background: #fff; border-radius: 8px; cursor: crosshair; }
.btn.grande { padding: 12px 26px; font-size: 1.05rem; }
.firma-hecha { text-align: center; padding: 10px 0; }
.firma-icono { font-size: 3rem; width: 80px; height: 80px; line-height: 80px; border-radius: 50%; margin: 0 auto 8px; background: #dff3e4; color: #17663a; }
.firma-hecha.no .firma-icono { background: #fde2e2; color: #a12020; }
.ok-msg { color: var(--good); font-weight: 600; }
.enlace-firma { word-break: break-all; user-select: all; }
.panel-firma { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin: 10px 0; background: var(--bg); }
.panel-firma.firmado { border-color: #8fd3a5; }
.panel-firma.rechazado { border-color: #f1a5a5; }
.panel-firma-cab { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; }
.panel-firma-cuerpo { display: flex; gap: 14px; flex-wrap: wrap; align-items: flex-start; }
.panel-firma-cuerpo p { margin: 0 0 4px; }
.firma-img { width: 220px; max-width: 100%; background: #fff; border: 1px solid var(--border); border-radius: 8px; padding: 4px; }
.huella { font-family: ui-monospace, monospace; }
.partes { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 10px; margin-top: 8px; }
.parte { border: 1px solid var(--border); border-left: 4px solid var(--border); border-radius: 10px; padding: 10px 12px; background: var(--card); display: flex; flex-direction: column; gap: 4px; }
.parte.pendiente { border-left-color: var(--warn); }
.parte.firmado { border-left-color: var(--good); }
.parte.rechazado { border-left-color: #c03030; }
.parte-cab { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.parte-num { font-weight: 700; }
.parte-cliente { font-weight: 600; font-size: 1.05rem; }
.parte-lineas { margin: 0; padding-left: 18px; color: var(--muted); font-size: 0.88rem; }
.parte .btns { margin-top: 4px; }
@media (max-width: 700px) {
  dialog.firma-dlg { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; border: none; padding: 14px; }
  .firma-pad { height: 34vh; min-height: 170px; }
  .firma-form .btns .btn { flex: 1 1 auto; }
  .partes { grid-template-columns: 1fr; }
  .parte .btns .btn { flex: 1 1 auto; padding: 10px; }
}
@media (max-width: 700px) and (orientation: landscape) { .firma-pad { height: 60vh; } }

.whatsapp { display: flex; flex-direction: column; gap: 8px; border: 1px solid #25d366; border-radius: 10px; padding: 10px 12px; margin: 8px 0; }
.btn.btn-whatsapp { background: #25d366; border-color: #25d366; color: #fff; text-align: center; font-weight: 600; }
.btn.btn-whatsapp-borde { border-color: #25d366; color: #128c4a; }
:root[data-tema="oscuro"] .btn.btn-whatsapp-borde { color: #5be38f; }

/* ---- Tarifa oficial ---- */
.vistas-tarifa { margin: 0 0 10px; }
.to-seccion { border: 1px solid var(--border); border-radius: 10px; margin: 8px 0; background: var(--card); }
.to-seccion > summary { cursor: pointer; padding: 10px 12px; font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.to-seccion > summary small { color: var(--muted); font-weight: 400; }
.to-seccion[open] > summary { border-bottom: 1px solid var(--border); }
.to-seccion .table-wrap { padding: 0 8px 8px; }
.to-precio { font-weight: 700; white-space: nowrap; }
.to-hist { font-size: 0.82rem; color: var(--muted); white-space: nowrap; }
.to-hist.distinto { color: var(--warn); font-weight: 600; }
.mono { font-family: ui-monospace, monospace; font-size: 0.82rem; }
.warn-txt { color: var(--warn); }
.visor-tarifa { max-height: 46vh; overflow: auto; border: 1px solid var(--border); border-radius: 8px; margin: 8px 0; }
.ref-catalogo { border: 1px solid var(--accent); border-radius: 10px; padding: 8px 10px; margin: 6px 0 10px; background: var(--accent-soft); }
.ref-catalogo h3 { margin: 0 0 6px; font-size: 0.9rem; }
.ref-catalogo .fila-cat { display: flex; gap: 8px; align-items: center; justify-content: space-between; padding: 4px 0; border-top: 1px solid var(--border); }
.ref-catalogo .fila-cat:first-of-type { border-top: none; }
body.sin-tarifa #toBorrar, body.sin-editar [data-to-anadir], body.sin-editar [data-cat-usar] { display: none !important; }

/* ---- Página pública de firma ---- */
.firma-publica { max-width: 820px; margin: 0 auto; padding: 16px; }
.fp-cab { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; border-bottom: 2px solid var(--accent); padding-bottom: 10px; margin-bottom: 12px; }
.fp-logo { display: block; max-height: 50px; max-width: 200px; margin-bottom: 6px; background: var(--logo-bg, #fff); border-radius: 6px; padding: 2px 6px; }
.fp-titulo { text-align: right; }
.fp-titulo h1 { margin: 0; font-size: 1.3rem; }
.fp-cliente { margin: 0 0 10px; }
.fp-h2 { margin: 18px 0 8px; }
@media print { .pagina-firma .no-print { display: none !important; } .pagina-firma .firma-publica { padding: 0; } .pagina-firma .pane { border: none; box-shadow: none; } }

/* ---- Usuarios ---- */
.indice-ajustes { margin: 0 0 12px; }
.enlace-usuario { background: none; border: none; color: inherit; font: inherit; cursor: pointer; padding: 2px 4px; border-radius: 6px; text-decoration: underline dotted; text-underline-offset: 3px; }
.enlace-usuario:hover, .enlace-usuario:focus-visible { color: var(--accent); outline: none; }
.pane.resaltar { outline: 2px solid var(--accent); outline-offset: 2px; transition: outline-color 1.5s; }
body:not(.es-admin) .solo-admin { display: none !important; }
#usuTabla tr.apagado td { opacity: 0.6; }
.fila-clave { display: flex; gap: 6px; align-items: center; }
/* Campo de contraseña que el navegador no autocompleta (no es type=password) pero no se lee de reojo. */
input.oculta { -webkit-text-security: disc; text-security: disc; }
.co-auth.desactivado { opacity: 0.45; }
.btn.bloqueado { opacity: 0.4; }
.fila-clave input { font-family: ui-monospace, monospace; }
.btn.peligro { color: var(--bad); border-color: var(--bad); }
pre.secreto { white-space: pre-wrap; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; font-size: 0.95rem; }

/* ---- Ordenar tablas ---- */
th[data-orden] { cursor: pointer; user-select: none; }
th[data-orden]:hover, th[data-orden]:focus-visible { color: var(--text); outline: none; }
th[data-orden]::after { content: ' ↕'; opacity: 0.35; }
th[aria-sort="ascending"]::after { content: ' ↑'; opacity: 1; color: var(--accent); }
th[aria-sort="descending"]::after { content: ' ↓'; opacity: 1; color: var(--accent); }
th[aria-sort] { color: var(--text); }
.orden-movil { display: none; }

/* ---- Tablas como tarjetas en el móvil ---- */
@media (max-width: 700px) {
  .orden-movil { display: flex; }
  table.cards, table.cards tbody { display: block; width: 100%; }
  table.cards thead { display: none; }
  table.cards tr {
    display: grid; grid-template-columns: 1fr 1fr; gap: 2px 12px; padding: 10px 12px; margin-bottom: 8px;
    border: 1px solid var(--border); border-radius: 10px; background: var(--card);
  }
  table.cards td { border: none; padding: 2px 0; text-align: left; min-width: 0; }
  table.cards td.num { text-align: left; }
  table.cards td[data-l]::before { content: attr(data-l); display: block; font-size: 0.72rem; color: var(--muted); font-weight: 600; }
  /* La primera columna con texto largo (artículo, nº…) y la de botones van a lo ancho. */
  table.cards td:first-child { grid-column: 1 / -1; }
  table.cards td:empty { display: none; }
  table.cards td.acciones { white-space: normal; display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; grid-column: 1 / -1; }
  table.cards td.acciones::before { display: none; }
  table.cards tr.cat { display: block; background: var(--accent-soft); padding: 0 4px; }
  table.cards tr.cat td { display: block; }
  table.cards tr.cat td::before { display: none; }
  table.cards tr.ej { display: block; font-size: 0.8rem; background: var(--bg); }
  table.cards tr.ej td { display: inline; }
  table.cards tr.ej td::before { display: none; }
  table.cards tr.flag { background: var(--warn-bg); }
  table.cards tr.flag td { background: transparent; }
  table.tarifa input { width: 100%; }
  table.data.cards .clamp { max-width: none; }
  /* Buscador: fecha y parecido a media anchura, el trabajo a lo ancho. */
  .tabla-hist td:first-child { grid-column: auto; }
  .tabla-hist td.col-trabajo { grid-column: 1 / -1; order: -1; max-width: none; }
  .tabla-hist .t-desc { white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
  .tabla-hist tbody tr:hover td { background: transparent; }
}

/* ---- Ayuda («?») ---- */
.ayuda {
  display: inline-flex; align-items: center; justify-content: center; vertical-align: middle;
  width: 18px; height: 18px; margin: 0 4px; padding: 0; border-radius: 50%; flex: none;
  border: 1px solid var(--muted); background: transparent; color: var(--muted);
  font: 700 11px/1 system-ui, sans-serif; cursor: help;
}
.ayuda:hover, .ayuda:focus-visible, .ayuda[aria-expanded="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); outline: none; }
.burbuja {
  position: fixed; z-index: 200; background: var(--text); color: var(--card); padding: 10px 12px; border-radius: 10px;
  font-size: 0.85rem; line-height: 1.4; font-weight: 400; text-transform: none; letter-spacing: normal;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25); pointer-events: none;
}
label > .ayuda, .etq > .ayuda, th > .ayuda, h2 > .ayuda { margin-left: 6px; }
@media (pointer: coarse) { .ayuda { width: 24px; height: 24px; font-size: 13px; } }

.etq { display: inline-flex; align-items: center; }
/* El «?» no agranda la línea: así los campos con y sin ayuda quedan alineados. */
.etq > .ayuda { margin-block: -4px; }
@media (pointer: coarse) { .etq > .ayuda { margin-block: -6px; } }

/* ---- Facturas ---- */
.resumen-fact { display: flex; gap: 8px; flex-wrap: wrap; margin: 6px 0 10px; }
.resumen-fact div { background: var(--accent-soft); border-radius: 10px; padding: 8px 12px; display: flex; flex-direction: column; min-width: 120px; }
.resumen-fact span { font-size: 0.75rem; color: var(--muted); }
.resumen-fact strong { font-size: 1.1rem; }
