:root {
  --bg: #faf6f0;
  --panel: #fff;
  --border: #e6dccc;
  --text: #2d2e31;
  --muted: #6e7681;
  --primary: #6d1228;
  --primary-dark: #4a0c1b;
  --beige: #f5e6d0;
  --ok: #0f766e;
  --ok-bg: #c6efce;
  --warn: #8a5a00;
  --warn-bg: #ffd966;
  --err: #9c0006;
  --err-bg: #ffc7ce;
  font-family: -apple-system, "Segoe UI", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-size: 14px; }

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 22px;
  background: white;
  border-bottom: 1px solid var(--border);
}
header h1 { margin: 0; font-size: 18px; color: var(--primary); }
.hdr-actions { display: flex; gap: 10px; align-items: center; font-size: 13px; }
.hdr-actions label { display: flex; gap: 6px; align-items: center; color: var(--muted); }
.hdr-actions input[type="number"] {
  width: 80px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 4px;
}
.hdr-actions button {
  padding: 6px 12px; border: 1px solid var(--border); background: white;
  border-radius: 6px; cursor: pointer; font-size: 13px;
}
.hdr-actions button:hover { border-color: var(--primary); }

main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 18px 22px 60px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }
.grid-4 { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 12px; }
@media (max-width: 1000px) { .grid-4 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 760px) { .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; } }
.grid-4 .dropzone { padding: 18px 14px; }
.grid-4 .dropzone strong { font-size: 13px; }
.grid-4 .dropzone span { font-size: 11px; }

.card.compact { padding: 12px 16px; }
.card.compact h2 { font-size: 14px; margin: 0 0 10px; }
.contab-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
}
.contab-grid label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; color: var(--muted);
}
.contab-grid input {
  padding: 5px 7px; border: 1px solid var(--border);
  border-radius: 4px; text-align: right;
  font-family: ui-monospace, SFMono-Regular, monospace; font-size: 13px;
}
.contab-grid input:focus { border-color: var(--primary); outline: none; }

.detalles-facturas {
  margin-top: 14px;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}
.detalles-facturas summary {
  cursor: pointer;
  color: var(--primary);
  font-size: 12px;
  font-weight: 600;
}
.detalles-facturas summary:hover { color: var(--primary-dark); }
.lista-detalle { width: 100%; font-size: 12px; }
.lista-detalle th { background: #fafbfd; position: sticky; top: 0; }
.lista-detalle th, .lista-detalle td { padding: 4px 8px; border-bottom: 1px solid var(--border); }

/* Badge de riesgo y botón de export */
.card h2 { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.badge-riesgo {
  padding: 4px 12px; border-radius: 999px;
  font-size: 11px; font-weight: 700; letter-spacing: .04em;
}
.badge-riesgo.bajo { background: #92D050; color: #fff; }
.badge-riesgo.moderado { background: #FFD966; color: #5c3d00; }
.badge-riesgo.alto { background: #FF6666; color: #fff; }
.btn-export {
  margin-left: auto;
  padding: 6px 14px; border-radius: 6px;
  background: var(--ok); color: white; border: none;
  font-size: 12px; font-weight: 600; cursor: pointer;
}
.btn-export:hover { background: #0e6b65; }

/* Preview cruda del Excel en el modal */
.raw-preview h4 { margin: 0 0 6px; }
.raw-table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 4px; max-height: 200px; overflow-y: auto; }
table.raw-table {
  width: max-content; min-width: 100%;
  border-collapse: collapse; font-size: 11px;
  font-family: ui-monospace, SFMono-Regular, monospace;
}
table.raw-table th, table.raw-table td {
  padding: 4px 8px; border: 1px solid var(--border);
  text-align: left; white-space: nowrap;
  max-width: 200px; overflow: hidden; text-overflow: ellipsis;
}
table.raw-table th {
  background: #fafbfd; position: sticky; top: 0; z-index: 1;
  font-weight: 600; font-size: 10px;
}
table.raw-table th small { font-size: 9px; }

.dropzone {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 24px;
  border: 2px dashed var(--primary);
  border-radius: 10px;
  background: var(--beige);
  cursor: pointer;
  text-align: center;
  transition: background .15s, border-color .15s;
}
.dropzone:hover, .dropzone.drag { background: #ebd2ac; border-color: var(--primary-dark); }
.dropzone strong { color: var(--primary); font-size: 14px; }
.dropzone span { color: var(--muted); font-size: 12px; }
.dropzone { position: relative; }
.dropzone.loading { pointer-events: none; }
.dz-loading {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  background: rgba(245, 230, 208, .92);
  border-radius: 10px;
  color: var(--primary); font-weight: 600; font-size: 13px;
}

/* Spinner reutilizable */
.spinner {
  display: inline-block;
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  flex-shrink: 0;
}
.spinner.big { width: 32px; height: 32px; border-width: 3px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Overlay global de procesamiento (multi-archivo) */
#busy {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, .35);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
}
#busy[hidden] { display: none; }
.busy-card {
  background: white; padding: 22px 28px; border-radius: 10px;
  display: flex; align-items: center; gap: 16px;
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
  font-size: 14px; min-width: 260px;
}
.busy-card #busy-msg { line-height: 1.4; color: var(--text); }
.busy-card #busy-progress {
  font-size: 12px; color: var(--muted); margin-top: 4px;
  font-family: ui-monospace, monospace;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px;
  box-shadow: 0 1px 2px rgba(0,0,0,.03), 0 4px 10px rgba(0,0,0,.05);
}
.card h2 { margin: 0 0 12px; font-size: 15px; }
.card h2 small { color: var(--muted); font-weight: normal; font-size: 12px; }
.hint { font-size: 12px; color: var(--muted); margin: 8px 0 0; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; }
th { background: #fafbfd; font-weight: 600; font-size: 12px; color: var(--muted); }
.num { text-align: right; font-family: ui-monospace, SFMono-Regular, monospace; }
tr.fila-resultado td { background: var(--beige); font-weight: 700; font-size: 14px; }
tr.fila-resultado td:not(:first-child) { color: var(--primary); }
tr.fila-fuerte td { font-weight: 700; background: #fafbfd; }
tr.sep td { padding: 4px; border-bottom: none; }
.table-wrap { overflow-x: auto; }
.card h2 small { color: var(--muted); font-weight: normal; font-size: 12px; margin-left: 8px; }

input.celda-303 {
  width: 100%; padding: 4px 6px; border: 1px solid var(--border); border-radius: 4px;
  text-align: right; font-family: ui-monospace, SFMono-Regular, monospace;
}
input.celda-303:focus { border-color: var(--primary); outline: none; }

/* Modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(15,23,42,.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal[hidden] { display: none; }
.modal-card {
  background: white; border-radius: 10px; width: min(640px, 92vw);
  max-height: 88vh; overflow: auto; padding: 18px;
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
}
.modal-card h3 { margin: 0 0 12px; font-size: 16px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.modal-foot button {
  padding: 7px 14px; border: 1px solid var(--border); background: white;
  border-radius: 6px; cursor: pointer; font-size: 13px;
}
.modal-foot button#modal-ok {
  background: var(--primary); color: white; border-color: var(--primary);
}
.modal-foot button#modal-ok:hover { background: var(--primary-dark); }

.field-row { display: grid; grid-template-columns: 140px 1fr; gap: 8px; margin-bottom: 8px; align-items: center; font-size: 13px; }
.field-row select { padding: 5px 6px; border: 1px solid var(--border); border-radius: 4px; width: 100%; }
.field-row .auto { font-size: 11px; color: var(--ok); margin-left: 6px; }
.preview { font-size: 12px; margin-top: 12px; }
.preview h4 { margin: 0 0 6px; font-size: 12px; color: var(--muted); font-weight: 600; }
.preview table { font-family: ui-monospace, SFMono-Regular, monospace; }
.preview td, .preview th { padding: 4px 6px; }

/* Conciliación: cuadro libro/declarado/dif por trimestre */
table.conc th.tri-header { text-align: center; border-left: 2px solid var(--border); }
table.conc td.lib { background: #fafbfd; }
table.conc td.dec { background: #fff; }
table.conc td.dif.ok { color: var(--ok); }
table.conc td.dif.warn { color: var(--err); font-weight: 700; }
table.conc td:first-child, table.conc th:first-child { border-left: none; }
table.conc td.lib, table.conc th.col-lib { border-left: 2px solid var(--border); }

#toast {
  position: fixed; bottom: 22px; right: 22px;
  background: var(--ok); color: white; padding: 10px 16px;
  border-radius: 6px; box-shadow: 0 4px 12px rgba(0,0,0,.2); font-size: 13px;
  z-index: 50;
}
#toast.err { background: var(--err); }
