:root {
  --bg: #ffffff;
  --fg: #0f172a;
  --muted: #64748b;
  --accent: #0e3a8a;
  --accent-light: #1d4ed8;
  --primary: #2563eb;
  --border: #e2e8f0;
  --card-bg: #f8fafc;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  --success: #10b981;
  --danger: #ef4444;
  --text: #0f172a;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--fg);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
.container { max-width: 960px; margin: 0 auto; padding: 0 1rem; }

/* Hero */
.hero {
  padding: 2.5rem 0 2rem;
  text-align: center;
  border-bottom: 1px solid var(--border);
}
.hero h1 {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: -0.025em;
  margin-bottom: 0.5rem;
}
.hero .accent { color: var(--accent); }
.hero .subtitle {
  color: var(--muted);
  font-size: 1rem;
  max-width: 520px;
  margin: 0 auto 1rem;
}
.credit {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--muted);
  font-size: 0.8rem;
  padding: 0.4rem 0.8rem;
  background: var(--card-bg);
  border-radius: 999px;
  border: 1px solid var(--border);
}
.credit a { color: var(--accent); text-decoration: none; font-weight: 600; }
.credit a:hover { text-decoration: underline; }

/* Sections */
.section {
  padding: 2rem 0;
  border-bottom: 1px solid var(--border);
}
.section:last-of-type { border-bottom: none; }
.section h2 {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}
.section-desc {
  color: var(--muted);
  font-size: 0.875rem;
  margin-bottom: 1rem;
}

/* Card */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}

/* Input */
.input-row {
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
  align-items: flex-end;
}
.input-col {
  flex: 1;
  min-width: 200px;
}
.input-col label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.35rem;
}
.input-col input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}
.input-col input[type="number"] {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  color: var(--fg);
}
.salary-display {
  font-size: 1.8rem;
  font-weight: 800;
  color: var(--accent);
  margin-top: 0.25rem;
}

/* Config row */
.config-row {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  margin-top: 1.25rem;
}
.config-col {
  flex: 1;
  min-width: 120px;
}
.config-col label {
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 0.3rem;
}
.config-col select {
  width: 100%;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--fg);
  background: var(--bg);
  cursor: pointer;
}
.config-summary {
  margin-top: 0.75rem;
  font-size: 0.8rem;
  color: var(--muted);
  padding: 0.5rem 0.75rem;
  background: var(--card-bg);
  border-radius: 8px;
  border: 1px solid var(--border);
  text-align: center;
}
.config-summary strong {
  color: var(--fg);
}

/* Table */
.table-wrap { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}
thead th {
  text-align: left;
  padding: 0.6rem 0.5rem;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}
tbody td {
  padding: 0.5rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
tbody tr:hover { background: rgba(14,58,138,0.03); }
tbody tr.best { background: rgba(16,185,129,0.06); }
tbody tr.best td:first-child { font-weight: 700; color: #059669; }
tbody tr.worst { background: rgba(239,68,68,0.06); }
tbody tr.worst td:first-child { font-weight: 700; color: #dc2626; }
.pos { color: var(--muted); font-size: 0.75rem; }
.money { font-weight: 700; text-align: right; }
.pct { color: var(--muted); text-align: right; }
.delta { font-weight: 600; text-align: right; }
.delta.positive { color: #059669; }
.delta.negative { color: #dc2626; }

/* Bar chart */
#bar-chart { width: 100%; min-height: 200px; /* dynamic height set by JS */ }
#scale-chart { width: 100%; height: 380px; margin-top: 1rem; }

/* Pills */
.ccaa-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
}
.pill {
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.1s;
}
.pill:hover { border-color: var(--accent); }
.pill.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* Bar chart */
.bars { display: flex; flex-direction: column; gap: 4px; }
.bar-row { display: flex; align-items: center; gap: 8px; cursor: pointer; transition: background 0.15s; border-radius: 4px; padding: 1px 4px; }
.bar-row:hover { background: rgba(15,23,42,0.04); }
.bar-row.selected { background: rgba(59,130,246,0.08); outline: 1.5px solid var(--accent); }
.bar-label { width: 140px; font-size: 0.75rem; color: var(--muted); text-align: right; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: 1; }
.bar-track { flex: 1; height: 20px; background: var(--border); border-radius: 4px; overflow: hidden; position: relative; }
.bar-fill { height: 100%; border-radius: 4px; transition: width 0.3s ease; }
.bar-row:hover .bar-fill { filter: brightness(1.12); }

/* Table row highlight */
#ccaa-table tbody tr { cursor: pointer; transition: background 0.15s; }
#ccaa-table tbody tr.highlighted {
  background: rgba(59,130,246,0.10) !important;
  outline: 1.5px solid var(--accent);
  outline-offset: -1.5px;
}
.bar-val { width: 80px; font-size: 0.8rem; font-weight: 700; text-align: right; flex-shrink: 0; }
/* Tooltip */
.bar-tooltip {
  display: none;
  position: absolute;
  z-index: 100;
  background: #0f172a;
  color: #f1f5f9;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.78rem;
  line-height: 1.55;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  min-width: 200px;
  pointer-events: none;
}
/* Mobile: centered fixed tooltip */
@media (max-width: 640px) {
  .bar-tooltip.visible {
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    right: auto !important;
    transform: translate(-50%, -50%) !important;
    min-width: 260px;
    max-width: 90vw;
  }
}
.bar-tooltip.visible { display: block; }
.bar-tooltip .tt-name { font-weight: 700; font-size: 0.85rem; margin-bottom: 4px; }
.bar-tooltip .tt-row { display: flex; justify-content: space-between; gap: 12px; }
.bar-tooltip .tt-label { color: #94a3b8; }
.bar-tooltip .tt-val { font-weight: 600; }
.bar-tooltip .tt-delta.positive { color: #34d399; }
.bar-tooltip .tt-delta.negative { color: #f87171; }

.bar-baseline {
  font-size: 0.65rem;
  color: var(--muted);
  text-align: right;
  margin-bottom: 4px;
  padding-right: 88px;
  font-style: italic;
}
.bar-track::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 2px;
  background: repeating-linear-gradient(
    to bottom,
    var(--muted) 0px,
    var(--muted) 3px,
    transparent 3px,
    transparent 6px
  );
  opacity: 0.4;
}

/* Scale SVG */
.scale-svg { width: 100%; height: auto; }

/* Audit notice */
.audit-notice {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-top: 1rem;
  padding: 0.6rem 0.75rem;
  background: rgba(14, 58, 138, 0.04);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.8rem;
  color: var(--muted);
}
.audit-icon { font-size: 1rem; flex-shrink: 0; }
.audit-notice a,
.link-button {
  color: var(--accent);
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
  background: none;
  border: none;
  font: inherit;
  padding: 0;
  display: inline;
}
.audit-notice a:hover,
.link-button:hover { color: var(--accent-light); }

/* Modal */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1000;
}
.modal.open { display: block; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(4px);
}
.modal-content {
  position: relative;
  margin: 3vh auto;
  width: 92%;
  max-width: 720px;
  max-height: 90vh;
  background: var(--bg);
  border-radius: var(--radius);
  box-shadow: 0 25px 50px rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
  animation: modalIn 0.2s ease-out;
}
@keyframes modalIn {
  from { opacity: 0; transform: translateY(20px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 {
  font-size: 1rem;
  font-weight: 700;
  margin: 0;
}
.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--muted);
  cursor: pointer;
  line-height: 1;
  padding: 0.2rem;
}
.modal-close:hover { color: var(--fg); }
.modal-body {
  padding: 1.25rem;
  overflow-y: auto;
  font-size: 0.875rem;
  line-height: 1.7;
}
.modal-body h1, .modal-body h2, .modal-body h3 {
  margin-top: 1.5rem;
  margin-bottom: 0.5rem;
}
.modal-body h1 { font-size: 1.25rem; }
.modal-body h2 { font-size: 1.1rem; }
.modal-body h3 { font-size: 1rem; }
.modal-body pre {
  background: #f1f5f9;
  border-radius: 8px;
  padding: 1rem;
  overflow-x: auto;
}
.modal-body code {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.8rem;
}
.modal-body p { margin: 0.5rem 0; }
.modal-body ul, .modal-body ol { margin: 0.5rem 0 0.5rem 1.2rem; }
.modal-body li { margin: 0.2rem 0; }
.modal-body table {
  width: 100%;
  font-size: 0.8rem;
  margin: 0.75rem 0;
}
.modal-body th, .modal-body td {
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
}
.modal-body th { background: var(--card-bg); }

/* Footer */
.footer {
  text-align: center;
  padding: 2rem 0 2.5rem;
  color: var(--muted);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
}
.footer a { color: var(--accent); text-decoration: none; }
.footer a:hover { text-decoration: underline; }

@media (max-width: 640px) {
  .hero h1 { font-size: 1.5rem; }
  .salary-display { font-size: 1.4rem; }
  .input-row { flex-direction: column; gap: 1rem; }
  .config-row { flex-direction: column; gap: 0.75rem; }
  .section { padding: 1.5rem 0; }
  #bar-chart, #scale-chart { height: auto; }
  .bar-label { width: 90px; font-size: 0.65rem; }
  .bar-val { width: 65px; font-size: 0.7rem; }
  .bar-tooltip { min-width: 170px; font-size: 0.72rem; }
  .bar-tooltip .tt-name { font-size: 0.78rem; }

  /* Tables: scroll + compact — no min-width to avoid forced horizontal scroll */
  .table-wrap { margin: 0 -0.5rem; padding: 0 0.5rem; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tramos-table-wrapper { margin: 0 -0.5rem; padding: 0 0.5rem; }
  table { font-size: 0.75rem; width: 100%; }
  thead th { padding: 0.4rem 0.3rem; font-size: 0.62rem; letter-spacing: 0.02em; }
  tbody td { padding: 0.35rem 0.3rem; }

  /* CCAA table: allow scroll for 8 cols but make it as compact as possible */
  #ccaa-table { min-width: 520px; }

  /* Card tables (verify, year-compare) */
  .card table { min-width: 440px; }

  /* Tramos table: compact on mobile */
  .tramos-table { min-width: 420px; }
  .tramos-table th, .tramos-table td { padding: 0.35rem 0.3rem; font-size: 0.72rem; }

  /* SVG chart scales properly */
  .scale-svg { min-width: 480px; }

  /* Pills scroll */
  .ccaa-pills { overflow-x: auto; flex-wrap: nowrap; padding-bottom: 0.5rem; -webkit-overflow-scrolling: touch; }
  .pill { flex-shrink: 0; font-size: 0.75rem; }

  /* Audit notice stack */
  .audit-notice { flex-direction: column; gap: 0.25rem; font-size: 0.75rem; }

  /* IPC: compact on mobile */
  .ipc-big-number { font-size: 2.5rem; }
  .ipc-label { font-size: 0.8rem; }
  .ipc-detail-row { font-size: 0.82rem; }
  .year-btn { padding: 0.25rem 0.6rem; font-size: 0.78rem; }
  .ipc-timeline-bars { height: 80px; }
  .ipc-timeline-bar .bar-year { font-size: 0.55rem; }

  /* Radiografía: compact on mobile */
  .donut-chart { width: 160px; height: 160px; }
  .donut-legend { font-size: 0.78rem; }
  .coste-value { font-size: 0.85rem; }
  .coste-value.total-empresa { font-size: 1rem; }
  .total-empresa-row { font-size: 1rem; }
  .coste-mini-info { gap: 0.3rem; }
  .mini-pill { font-size: 0.7rem; padding: 0.2rem 0.5rem; }

  /* Tramos bars: compact */
  .tramo-bar-fill { font-size: 0.6rem; }
  .tramo-label { font-size: 0.72rem; }
  .tramo-bar-bg { height: 20px; }
  .tramos-legend { font-size: 0.78rem; }
}

/* ============================================
   RADIOGRAFÍA DEL COSTE (v2)
   ============================================ */

.radiografia-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    align-items: start;
}

@media (max-width: 768px) {
    .radiografia-grid {
        grid-template-columns: 1fr;
        gap: 1.5rem;
    }
}

.donut-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.donut-chart {
    width: 200px;
    height: 200px;
}

.donut-center {
    fill: var(--bg);
}

.donut-segment {
    transition: opacity 0.2s ease;
    cursor: pointer;
}

.donut-segment:hover {
    opacity: 0.85;
}

.donut-legend {
    margin-top: 1rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
    justify-content: center;
    font-size: 0.85rem;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.legend-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    display: inline-block;
}

.coste-desglose {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.coste-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.35rem 0;
    border-bottom: 1px solid var(--border);
}

.coste-row:last-child {
    border-bottom: none;
}

.total-row {
    font-weight: 600;
    border-top: 2px solid var(--primary);
    margin-top: 0.3rem;
    padding-top: 0.5rem;
}

.total-empresa-row {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--primary);
    border-top: 2px solid var(--primary);
    margin-top: 0.3rem;
    padding-top: 0.5rem;
}

.coste-value {
    font-family: monospace;
    font-weight: 600;
    white-space: nowrap;
}

.coste-value.neto { color: #10b981; }
.coste-value.irpf { color: #ef4444; }
.coste-value.ss-trab { color: #f59e0b; }
.coste-value.bruto { color: #374151; }
.coste-value.ss-empresa { color: #ec4899; }
.coste-value.total-empresa { color: var(--primary); font-size: 1.15rem; }

.empresa-row {
    background: rgba(236, 72, 153, 0.05);
    padding: 0.35rem 0.5rem;
    border-radius: 4px;
    margin: 0.15rem 0;
}

.coste-mini-info {
    margin-top: 1rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
}

.mini-pill {
    background: var(--bg);
    border: 1px solid var(--border);
    padding: 0.25rem 0.6rem;
    border-radius: 999px;
    font-size: 0.78rem;
    color: var(--muted);
}

.separator {
    border-top: 1px dashed var(--border);
    margin: 0.5rem 0;
}

/* ============================================
   COMPARATIVA IPC (v2)
   ============================================ */

.ipc-controls {
    margin-bottom: 1.5rem;
}

.ipc-controls label {
    display: block;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--muted);
}

.year-selector {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.year-btn {
    background: var(--bg);
    border: 1px solid var(--border);
    padding: 0.35rem 0.8rem;
    border-radius: 999px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.15s ease;
    font-family: inherit;
    color: var(--text);
}

.year-btn:hover {
    background: var(--border);
}

.year-btn.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.year-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.ipc-result {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    align-items: start;
}

@media (max-width: 768px) {
    .ipc-result {
        grid-template-columns: 1fr;
        gap: 1.5rem;
    }
}

.ipc-main {
    text-align: center;
}

.ipc-big-number {
    font-size: 3.5rem;
    font-weight: 800;
    color: var(--danger);
    line-height: 1;
    margin-bottom: 0.25rem;
}

.ipc-big-number.ganancia {
    color: var(--success);
}

.ipc-label {
    color: var(--muted);
    font-size: 0.9rem;
}

.ipc-details {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.ipc-detail-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.4rem 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
}

.ipc-detail-row.ipc-source {
    border-bottom: none;
    font-size: 0.75rem;
    color: var(--muted);
    margin-top: 0.5rem;
}

.ipc-calc-explanation {
    font-size: 0.78rem;
    color: var(--muted);
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.6rem 0.75rem;
    margin: 0.5rem 0;
    line-height: 1.5;
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

.ipc-detail-row a {
    color: var(--accent);
    text-decoration: underline;
    text-decoration-style: dotted;
}
.ipc-detail-row a:hover {
    text-decoration-style: solid;
}

.ipc-timeline {
    margin-top: 1.5rem;
}

.ipc-timeline-title {
    font-size: 0.85rem;
    color: var(--muted);
    margin-bottom: 0.75rem;
    text-align: center;
}

.ipc-timeline-bars {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 0.3rem;
    height: 120px;
    padding: 0 0.5rem;
}

.ipc-timeline-bar {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    min-width: 0;
    cursor: pointer;
}

.ipc-timeline-bar .bar-fill {
    width: 100%;
    border-radius: 3px 3px 0 0;
    transition: opacity 0.2s ease;
    min-height: 2px;
}

.ipc-timeline-bar:hover .bar-fill {
    opacity: 0.8;
}

.ipc-timeline-bar .bar-year {
    font-size: 0.65rem;
    color: var(--muted);
    margin-top: 0.25rem;
    white-space: nowrap;
}

.ipc-timeline-bar.active .bar-fill {
    border: 2px solid var(--primary);
}

/* Tramos table wrapper — overflow for mobile */
.tramos-table-wrapper { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* ============================================
   DESGLOSE POR TRAMOS IRPF (v2)
   ============================================ */

.tramos-legend {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;
    font-size: 0.85rem;
}

.tramo-dot {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.tramo-dot::before {
    content: '';
    width: 12px;
    height: 12px;
    border-radius: 3px;
    display: inline-block;
}

.tramo-dot.estatal::before {
    background: #3b82f6;
}

.tramo-dot.autonomico::before {
    background: #8b5cf6;
}

.tramos-chart {
    margin-bottom: 1.5rem;
}

.tramo-bar-container {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin-bottom: 0.6rem;
}

.tramo-label {
    font-size: 0.8rem;
    color: var(--muted);
    display: flex;
    justify-content: space-between;
}

.tramo-bar-bg {
    height: 24px;
    background: var(--bg);
    border-radius: 4px;
    overflow: hidden;
    display: flex;
}

.tramo-bar-fill {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    color: white;
    font-weight: 600;
    transition: width 0.3s ease;
    white-space: nowrap;
    overflow: hidden;
}

.tramo-bar-fill.estatal {
    background: #3b82f6;
}

.tramo-bar-fill.autonomico {
    background: #8b5cf6;
}

.tramos-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82rem;
}

.tramos-table th,
.tramos-table td {
    padding: 0.5rem 0.6rem;
    text-align: right;
    border-bottom: 1px solid var(--border);
}

.tramos-table th:first-child,
.tramos-table td:first-child {
    text-align: left;
    font-weight: 600;
}

.tramos-table th {
    background: var(--bg);
    font-weight: 600;
    color: var(--muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.tramos-table tr:hover {
    background: rgba(59, 130, 246, 0.04);
}
