:root {
  --bg: #f6f5f2;
  --panel: #ffffff;
  --ink: #18181b;
  --ink-soft: #3f3f46;
  --muted: #71717a;
  --line: #e4e4e7;
  --line-strong: #d4d4d8;

  /* Brand */
  --madrid-red: #c8102e;
  --madrid-red-dark: #9b0a23;
  --madrid-red-soft: #fde7eb;

  /* Categorical */
  --espanol: #c8102e;
  --espanol-soft: #fde7eb;
  --hispano: #f59e0b;
  --hispano-soft: #fef3c7;
  --otro: #6366f1;
  --otro-soft: #e0e7ff;
  --desconocido: #94a3b8;
  --desconocido-soft: #f1f5f9;

  --ok: #047857;
  --ok-soft: #d1fae5;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, .04), 0 1px 1px rgba(15, 23, 42, .03);
  --shadow-md: 0 4px 16px rgba(15, 23, 42, .06), 0 1px 2px rgba(15, 23, 42, .04);
  --shadow-lg: 0 10px 40px rgba(15, 23, 42, .08), 0 4px 12px rgba(15, 23, 42, .05);

  --radius: 10px;
  --radius-lg: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.55 "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-feature-settings: "cv11", "ss01";
  -webkit-font-smoothing: antialiased;
}

a { color: var(--madrid-red-dark); text-decoration: none; font-weight: 500; }
a:hover { text-decoration: underline; }

.wrap { max-width: 1180px; margin: 0 auto; padding: 0 1.25rem; }
.muted { color: var(--muted); }

/* ============ Hero ============ */
.hero {
  position: relative;
  background:
    radial-gradient(circle at 100% 0%, rgba(200, 16, 46, 0.07), transparent 45%),
    radial-gradient(circle at 0% 100%, rgba(245, 158, 11, 0.06), transparent 50%),
    var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 2.5rem 0 2rem;
  overflow: hidden;
}
.hero-flag {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 4px;
  background: linear-gradient(90deg, var(--madrid-red) 0%, var(--madrid-red-dark) 100%);
}
.hero h1 {
  margin: 0 0 .35rem;
  font-size: clamp(1.75rem, 4vw, 2.5rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
  color: var(--ink);
}
.hero h1 .year {
  display: inline-block;
  font-weight: 500;
  color: var(--madrid-red);
}
.hero-sub {
  font-size: clamp(1rem, 1.6vw, 1.15rem);
  color: var(--ink-soft);
  margin: 0 0 1.5rem;
  max-width: 60ch;
  line-height: 1.5;
}

/* Hero search */
.hero-search {
  position: relative;
  background: var(--panel);
  border: 2px solid var(--line-strong);
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  padding: 0 .25rem 0 1.25rem;
  box-shadow: var(--shadow-md);
  transition: border-color .15s, box-shadow .15s;
}
.hero-search:focus-within {
  border-color: var(--madrid-red);
  box-shadow: 0 0 0 4px rgba(200, 16, 46, .12), var(--shadow-lg);
}
.hero-search-icon {
  width: 1.25rem; height: 1.25rem;
  color: var(--muted);
  flex-shrink: 0;
}
#q {
  border: 0;
  background: transparent;
  padding: 1.1rem 1rem;
  font-size: 1.05rem;
  width: 100%;
  outline: none;
  font-family: inherit;
  color: var(--ink);
}
#q::placeholder { color: var(--muted); }

.hero-hint {
  margin: .75rem 0 0;
  font-size: .9rem;
  color: var(--muted);
}
.suggest {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: .15rem .65rem;
  font: inherit;
  font-size: .85rem;
  color: var(--ink-soft);
  cursor: pointer;
  transition: all .12s;
}
.suggest:hover {
  border-color: var(--madrid-red);
  color: var(--madrid-red);
}

/* ============ Main ============ */
.main { padding: 1.75rem 1.25rem 2.5rem; }

/* ============ Overview cards ============ */
.overview {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.ov-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 1.1rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  box-shadow: var(--shadow-sm);
  transition: transform .12s, box-shadow .12s;
}
.ov-card:hover { transform: translateY(-1px); box-shadow: var(--shadow-md); }
.ov-icon {
  width: 3rem; height: 3rem;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.35rem;
  flex-shrink: 0;
}
.ov-icon-bene { background: var(--ok-soft); color: var(--ok); }
.ov-icon-eur  { background: var(--madrid-red-soft); color: var(--madrid-red); }
.ov-icon-exc  { background: var(--bad-soft); color: var(--bad); }
.ov-num {
  font-size: 1.55rem;
  font-weight: 700;
  letter-spacing: -.02em;
  line-height: 1.1;
}
.ov-lbl {
  font-size: .88rem;
  color: var(--muted);
  margin-top: .15rem;
}

/* ============ Origen section ============ */
.origen-section {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.5rem;
  box-shadow: var(--shadow-sm);
}
.origen-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  gap: 1rem;
  flex-wrap: wrap;
}
.origen-head h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -.01em;
}
.ghost-btn {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: .35rem .85rem;
  font: inherit;
  font-size: .85rem;
  color: var(--muted);
  cursor: pointer;
  transition: all .12s;
}
.ghost-btn:hover {
  background: var(--bg);
  color: var(--ink);
}
.origen-bar {
  display: flex;
  height: 14px;
  border-radius: 999px;
  overflow: hidden;
  background: var(--line);
  margin-bottom: .85rem;
}
.origen-bar .seg { display: block; height: 100%; transition: flex .3s; }
.seg-espanol     { background: var(--espanol); }
.seg-hispano     { background: var(--hispano); }
.seg-otro        { background: var(--otro); }
.seg-desconocido { background: var(--desconocido); }
.origen-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  font-size: .9rem;
}
.origen-legend > div {
  display: flex;
  align-items: center;
  gap: .5rem;
}
.origen-legend strong { font-weight: 600; }
.origen-legend .num { color: var(--ink); font-weight: 700; }
.origen-legend .pct { color: var(--muted); font-size: .8rem; }

.dot {
  display: inline-block;
  width: .75rem; height: .75rem;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot-espanol     { background: var(--espanol); }
.dot-hispano     { background: var(--hispano); }
.dot-otro        { background: var(--otro); }
.dot-desconocido { background: var(--desconocido); }

.info-panel {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
  font-size: .9rem;
  color: var(--ink-soft);
}
.info-panel summary {
  display: none;
}
.info-panel[open] {
  display: block;
}
.info-panel p { margin: .5rem 0; }
.info-panel ul { padding-left: 0; margin: .75rem 0; list-style: none; }
.info-panel ul li {
  padding: .35rem 0;
  display: flex;
  align-items: flex-start;
  gap: .5rem;
}
.info-panel ul li .dot { margin-top: .35rem; }

/* ============ Filters ============ */
.filters-section {
  margin-bottom: 1rem;
}
.filters-row {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  align-items: flex-start;
}
.filter-group {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .4rem;
}
.filter-label {
  font-size: .78rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .05em;
  font-weight: 600;
  margin-right: .35rem;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  padding: .4rem .8rem;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: .88rem;
  cursor: pointer;
  transition: all .12s;
  user-select: none;
  font-weight: 500;
}
.chip:hover { border-color: var(--line-strong); }
.chip input { margin: 0; cursor: pointer; }
.chip:has(input:not(:checked)) {
  opacity: .45;
}
.chip-ok:has(input:checked)         { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.chip-bad:has(input:checked)        { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.chip-warn:has(input:checked)       { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.chip-espanol:has(input:checked)    { background: var(--espanol-soft); color: var(--espanol); border-color: transparent; }
.chip-hispano:has(input:checked)    { background: var(--hispano-soft); color: var(--hispano); border-color: transparent; }
.chip-otro:has(input:checked)       { background: var(--otro-soft); color: var(--otro); border-color: transparent; }
.chip-desconocido:has(input:checked){ background: var(--desconocido-soft); color: var(--ink-soft); border-color: transparent; }

.select {
  font: inherit;
  font-size: .9rem;
  padding: .45rem .85rem;
  border: 1px solid var(--line-strong);
  background: var(--panel);
  border-radius: 8px;
  color: var(--ink);
  cursor: pointer;
  max-width: 100%;
  min-width: 0;
}
.select:focus { outline: 2px solid var(--madrid-red); outline-offset: 1px; }
.filter-group-block { flex-basis: 100%; }
.filter-group-block .select { flex: 1; min-width: 0; max-width: 36rem; }

.result-count {
  margin-top: 1rem;
  font-size: .9rem;
  color: var(--muted);
  font-weight: 500;
}
.result-count strong { color: var(--ink); }

/* ============ Results table ============ */
.table-wrap {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left;
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  background: #fafafa;
  padding: .85rem 1rem;
  border-bottom: 1px solid var(--line);
  font-weight: 600;
}
.num-col { text-align: right; }

tbody tr {
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  transition: background .08s;
}
tbody tr:last-child { border-bottom: 0; }
tbody tr:hover { background: #fafafa; }
tbody td {
  padding: .85rem 1rem;
  vertical-align: middle;
  font-variant-numeric: tabular-nums;
}
tbody td.num-col { text-align: right; }
tbody td.exp { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .82rem; color: var(--muted); }
tbody td.nif { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .82rem; }
tbody td.name { font-weight: 600; color: var(--ink); }

.tag {
  display: inline-block;
  padding: .15rem .55rem;
  border-radius: 999px;
  font-size: .78rem;
  font-weight: 600;
  white-space: nowrap;
}
.tag.admitido     { background: var(--ok-soft);   color: var(--ok); }
.tag.excluido     { background: var(--bad-soft);  color: var(--bad); }
.tag.excluido_presupuesto { background: var(--warn-soft); color: var(--warn); }
.tag.jovenes      { background: #e0e7ff; color: #3730a3; }
.tag.sectores     { background: #f3e8ff; color: #6b21a8; }
.tag.origen-espanol     { background: var(--espanol-soft); color: var(--espanol); }
.tag.origen-hispano     { background: var(--hispano-soft); color: #92400e; }
.tag.origen-otro        { background: var(--otro-soft); color: #4338ca; }
.tag.origen-desconocido { background: var(--desconocido-soft); color: var(--muted); }

.amount {
  font-weight: 700;
  color: var(--ok);
  font-variant-numeric: tabular-nums;
}
.motivos-inline {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: .82rem;
  color: var(--muted);
}

/* Empty state */
.empty-state {
  padding: 3rem 1rem;
  text-align: center;
}
.empty-icon { font-size: 2.5rem; margin-bottom: .5rem; }
.empty-title { font-weight: 600; font-size: 1.05rem; margin-bottom: .25rem; }
.empty-hint { color: var(--muted); font-size: .9rem; }

/* Pager */
.pager {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--line);
  background: #fafafa;
}
.pager-btn {
  border: 1px solid var(--line-strong);
  background: var(--panel);
  border-radius: 8px;
  padding: .5rem 1rem;
  font: inherit;
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--ink-soft);
  transition: all .12s;
}
.pager-btn:hover:not(:disabled) {
  border-color: var(--madrid-red);
  color: var(--madrid-red);
}
.pager-btn:disabled { opacity: .35; cursor: not-allowed; }
#page-info {
  color: var(--muted);
  font-size: .9rem;
  font-variant-numeric: tabular-nums;
}

/* ============ Modal ============ */
dialog {
  border: 0;
  border-radius: var(--radius-lg);
  padding: 0;
  max-width: 540px;
  width: calc(100% - 2rem);
  box-shadow: 0 25px 80px rgba(0,0,0,.22);
  background: var(--panel);
}
dialog::backdrop { background: rgba(15, 23, 42, .4); backdrop-filter: blur(4px); }
dialog .close {
  position: absolute;
  top: .65rem; right: .85rem;
  border: 0;
  background: transparent;
  font-size: 1.7rem;
  cursor: pointer;
  color: var(--muted);
  width: 2.25rem; height: 2.25rem;
  border-radius: 50%;
  line-height: 1;
}
dialog .close:hover { background: var(--bg); color: var(--ink); }
.detail-content { padding: 1.5rem; }
.detail-name {
  margin: 0 0 1rem;
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -.01em;
  padding-right: 2rem;
}
.detail-row {
  display: flex;
  gap: 1rem;
  padding: .55rem 0;
  border-bottom: 1px solid var(--line);
}
.detail-row:last-of-type { border-bottom: 0; }
.detail-row dt {
  width: 9rem;
  flex-shrink: 0;
  color: var(--muted);
  font-size: .85rem;
}
.detail-row dd {
  margin: 0;
  font-weight: 500;
}
.detail-amount {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--ok);
}
.motivos-list {
  list-style: none;
  padding: 0;
  margin: .75rem 0 0;
}
.motivos-list li {
  padding: .65rem .85rem;
  background: var(--bad-soft);
  border-left: 3px solid var(--bad);
  border-radius: 6px;
  margin-bottom: .4rem;
  font-size: .88rem;
  line-height: 1.45;
}
.motivos-list li code {
  font-weight: 700;
  margin-right: .4rem;
  color: var(--bad);
  font-family: ui-monospace, monospace;
}
.motivos-heading {
  margin: 1.2rem 0 .25rem;
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  font-weight: 600;
}

/* ============ Footer ============ */
footer {
  padding: 2.5rem 0 3rem;
  margin-top: 1rem;
  border-top: 1px solid var(--line);
  font-size: .85rem;
  color: var(--ink-soft);
  background: var(--panel);
}
footer p { margin: 0 0 .5rem; max-width: 70ch; }

/* ============ Responsive ============ */
@media (max-width: 720px) {
  .hero { padding: 1.75rem 0 1.5rem; }
  .hero h1 { font-size: 1.75rem; }
  .hide-mobile { display: none; }
  thead th, tbody td { padding: .65rem .65rem; font-size: .85rem; }
  .pager { flex-direction: row; }
  .filters-row { gap: 1rem; }
}
