:root {
  --bg: #f0f4f8;
  --surface: #ffffff;
  --surface-soft: #eaf2f0;
  --line: #d0dde6;
  --text: #0f1f2d;
  --muted: #4e6780;
  --blue: #1a6faa;
  --green: #1a8c6e;
  --red: #b03f3f;
  --amber: #a06010;
  --shadow: 0 1px 3px rgba(15, 35, 55, 0.06), 0 6px 20px rgba(15, 35, 55, 0.07);
  --radius: 10px;
  --transition: 140ms ease;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", Arial, Helvetica, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select {
  font: inherit;
}

/* Slim modern scrollbars */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: #b0bfca; }

/* Global focus ring */
*:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}

.app-shell {
  width: min(1480px, calc(100% - 32px));
  margin: 0 auto;
  padding: 24px 0 48px;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.top-actions,
.language-toggle,
.chart-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.language-toggle .icon-button.is-active {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}

.eyebrow,
.metric-label,
.legend-note,
.footnote {
  color: var(--muted);
  font-size: 0.82rem;
}

.eyebrow {
  margin: 0 0 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 1.9rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

h2 {
  margin-bottom: 0;
  font-size: 1rem;
  font-weight: 600;
}

.status-pill {
  min-width: 160px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 9px 14px;
  color: var(--muted);
  text-align: center;
  font-size: 0.85rem;
  transition: color var(--transition), border-color var(--transition), background var(--transition);
}

.status-pill.is-error {
  color: var(--red);
  border-color: #e6b6b6;
  background: #fff7f7;
}

.toolbar,
.summary-strip,
.top-movers,
.chart-block,
.category-panel,
.table-section,
.disclaimer {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 12px;
  padding: 14px;
  margin-bottom: 14px;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 500;
}

select,
input[type="search"] {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  padding: 7px 30px 7px 9px;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%234e6780' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='4,6 8,10 12,6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 9px center;
  background-size: 14px;
  transition: border-color var(--transition), box-shadow var(--transition);
  cursor: pointer;
}

input[type="search"] {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%234e6780' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='7' cy='7' r='4'/%3E%3Cline x1='10.5' y1='10.5' x2='14' y2='14'/%3E%3C/svg%3E");
  background-position: left 9px center;
  padding: 7px 9px 7px 30px;
}

select:focus,
input[type="search"]:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(26, 111, 170, 0.12);
  outline: none;
}

.workspace {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 14px;
  align-items: start;
}

.category-panel {
  position: sticky;
  top: 14px;
  padding: 14px;
}

.panel-head,
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.icon-button {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--muted);
  padding: 7px 11px;
  cursor: pointer;
  font-size: 0.83rem;
  font-weight: 500;
  transition: background var(--transition), border-color var(--transition), color var(--transition);
}

.icon-button:hover {
  border-color: var(--blue);
  color: var(--blue);
  background: #f0f7fd;
}

.icon-button:active {
  background: #e3f0f9;
}

.category-list {
  display: grid;
  gap: 4px;
  max-height: calc(100vh - 220px);
  overflow: auto;
  padding-top: 10px;
}

.category-row {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  border: 1px solid transparent;
  border-radius: 7px;
  background: #fff;
  padding: 6px;
  margin-left: calc(var(--depth, 0) * 16px);
  transition: background var(--transition), border-color var(--transition);
}

.category-row.is-selected {
  border-color: #9cc6d8;
  background: #eef6fb;
}

.category-row:focus-within {
  border-color: var(--blue);
  box-shadow: 0 0 0 2px rgba(26, 111, 170, 0.12);
}

.category-row.is-compared {
  background: var(--surface-soft);
}

.category-main {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr);
  align-items: center;
  gap: 6px;
  min-width: 0;
  width: 100%;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: inherit;
  cursor: pointer;
  padding: 4px;
  text-align: left;
  transition: background var(--transition);
}

.category-main:hover {
  background: #e8f3f9;
}

.category-main:focus-visible,
.compare-check:focus-within {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}

.category-label {
  min-width: 0;
}

.expand-button,
.expand-spacer {
  width: 24px;
  height: 24px;
}

.expand-button {
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #fff;
  color: var(--muted);
  display: inline-grid;
  place-items: center;
  line-height: 1;
  font-size: 0.9rem;
  transition: background var(--transition), border-color var(--transition);
}

.expand-button:hover {
  background: #f0f7fd;
  border-color: var(--blue);
}

.category-children {
  display: grid;
  gap: 4px;
}

.category-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
  font-size: 0.875rem;
}

.category-code {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 400;
}

.category-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.category-badge {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 500;
  padding: 2px 7px;
  white-space: nowrap;
}

.category-badge.positive {
  color: var(--green);
  border-color: #a8d8c8;
  background: #f0faf6;
}

.category-badge.negative {
  color: var(--red);
  border-color: #e0b8b8;
  background: #fff5f5;
}

.compare-check {
  display: grid;
  align-items: center;
  place-items: center;
  justify-content: center;
  width: 28px;
  height: 32px;
  cursor: pointer;
  margin: 0;
}

.compare-check input {
  width: 16px;
  height: 16px;
  accent-color: var(--blue);
  cursor: pointer;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.dashboard {
  display: grid;
  gap: 14px;
}

/* Summary strip: divide with vertical rules */
.summary-strip {
  display: grid;
  grid-template-columns: 2fr repeat(4, 1fr);
  gap: 0;
  padding: 0;
  overflow: hidden;
}

.summary-strip > div {
  padding: 16px;
  border-right: 1px solid var(--line);
}

.summary-strip > div:last-child {
  border-right: none;
}

.category-path {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.35;
}

.metric-label {
  margin-bottom: 4px;
}

.metric-value {
  margin-bottom: 0;
  font-weight: 700;
  font-size: 1.3rem;
  letter-spacing: -0.01em;
}

.chart-block,
.table-section,
.top-movers {
  padding: 16px;
}

.mover-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.mover-card {
  display: grid;
  gap: 4px;
  min-height: 86px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  padding: 12px;
  text-align: left;
  transition: border-color var(--transition), background var(--transition), box-shadow var(--transition);
}

.mover-card:hover {
  border-color: var(--blue);
  background: #f0f7fd;
  box-shadow: 0 2px 8px rgba(26, 111, 170, 0.10);
}

.mover-card span,
.mover-card small {
  color: var(--muted);
  font-size: 0.8rem;
}

.mover-card strong {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

canvas {
  width: 100% !important;
  height: 360px !important;
  max-height: 440px;
}

.range-slider {
  margin-top: 14px;
  display: grid;
  gap: 7px;
}

.range-slider span {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.83rem;
  color: var(--muted);
}

.range-slider strong {
  color: var(--text);
  font-weight: 600;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--blue);
  height: 4px;
  cursor: pointer;
}

.split-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 14px;
}

.toggle-line {
  display: flex;
  grid-auto-flow: column;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  font-size: 0.83rem;
}

.density-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.density-controls label:last-child {
  min-width: 140px;
}

.density-controls select {
  min-height: 32px;
  padding: 5px 28px 5px 8px;
}

.empty-state {
  display: grid;
  min-height: 280px;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
  padding: 18px;
  font-size: 0.88rem;
  line-height: 1.55;
}

.is-hidden {
  display: none !important;
}

.table-wrap {
  overflow-x: auto;
  max-height: 480px;
  overflow-y: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

thead {
  position: sticky;
  top: 0;
  z-index: 2;
}

th {
  background: var(--surface);
  border-bottom: 2px solid var(--line);
  padding: 9px 10px;
  text-align: left;
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
}

td {
  border-bottom: 1px solid var(--line);
  padding: 9px 10px;
  text-align: left;
  font-size: 0.88rem;
}

tbody tr:nth-child(even) {
  background: #f8fafc;
}

.sort-button {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
  padding: 0;
  transition: color var(--transition);
}

.sort-button:hover {
  color: var(--blue);
}

.sort-button.is-active {
  color: var(--text);
}

.sort-button.is-active::after {
  content: "↓";
  color: var(--blue);
  font-size: 0.82rem;
}

.sort-button.is-active.is-asc::after {
  content: "↑";
}

tbody tr {
  cursor: pointer;
  transition: background var(--transition);
}

tbody tr:hover {
  background: #edf4fa !important;
}

tbody tr.is-selected {
  background: #deeef8 !important;
}

.positive {
  color: var(--green);
  font-weight: 600;
}

.negative {
  color: var(--red);
  font-weight: 600;
}

.muted {
  color: var(--muted);
}

.disclaimer {
  margin-top: 14px;
  padding: 14px 16px;
  color: var(--muted);
  font-size: 0.83rem;
  line-height: 1.55;
}

.disclaimer strong {
  color: var(--text);
}

/* Month navigation (KDE section) */
.density-head-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.month-nav {
  display: flex;
  align-items: center;
  gap: 4px;
}

.month-nav .icon-button {
  padding: 4px 9px;
  font-size: 1.1rem;
  line-height: 1;
}

.month-nav .legend-note {
  min-width: 56px;
  text-align: center;
  font-size: 0.83rem;
}

.icon-button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  pointer-events: none;
}

/* Seasonal history section */
.seasonal-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.seasonal-controls select {
  min-width: 200px;
  min-height: 34px;
  padding: 5px 28px 5px 9px;
}

@media (max-width: 1020px) {
  .toolbar,
  .workspace,
  .mover-grid,
  .split-grid {
    grid-template-columns: 1fr;
  }

  .summary-strip {
    grid-template-columns: 1fr 1fr;
  }

  .summary-strip > div {
    border-bottom: 1px solid var(--line);
  }

  .summary-strip > div:nth-child(even) {
    border-right: none;
  }

  .summary-strip > div:last-child,
  .summary-strip > div:nth-last-child(2):nth-child(odd) {
    border-bottom: none;
  }

  .category-panel {
    position: static;
  }

  .category-list {
    max-height: 360px;
  }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100% - 20px, 1480px);
    padding-top: 14px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions,
  .chart-actions {
    align-items: stretch;
    flex-direction: column;
  }

  h1 {
    font-size: 1.5rem;
  }

  .status-pill {
    text-align: left;
  }

  .summary-strip {
    grid-template-columns: 1fr;
  }

  .summary-strip > div {
    border-right: none;
  }

  canvas {
    height: 280px !important;
    max-height: 320px;
  }

  .table-wrap {
    max-height: 360px;
  }
}

/* ===== Tabular numbers: digits align, values don't jiggle ===== */
td, th, .metric-value, .category-badge, .legend-note, .footnote, .status-pill, .range-slider strong {
  font-variant-numeric: tabular-nums;
}

/* ===== Sparklines ===== */
.spark { display: block; }
.spark polyline { fill: none; stroke: var(--blue); stroke-width: 1.5; }
.spark .spark-dot { fill: var(--amber); }
.spark-cell { min-width: 72px; }

/* ===== Skeleton loading ===== */
.skeleton-line {
  display: block;
  height: 10px;
  border-radius: 6px;
  margin: 4px 0;
  background: linear-gradient(90deg, rgba(125, 145, 165, 0.16) 25%, rgba(125, 145, 165, 0.32) 50%, rgba(125, 145, 165, 0.16) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s linear infinite;
}
.skeleton-line.w40 { width: 40%; }
.skeleton-line.w60 { width: 60%; }
.skeleton-line.w80 { width: 80%; }
@keyframes shimmer { to { background-position: -200% 0; } }
tr.is-skeleton td .skeleton-line { margin: 2px 0; }
.mover-card.is-skeleton { cursor: default; }

/* ===== Inflation heatmap ===== */
.heatmap-wrap {
  overflow: auto;
  max-height: 420px;
}
.heatmap-table {
  display: grid;
  grid-template-columns: 44px repeat(12, minmax(20px, 1fr));
  gap: 3px;
  align-items: center;
}
.heatmap-year,
.heatmap-head {
  font-size: 0.72rem;
  color: var(--muted);
}
.heatmap-year { text-align: right; padding-right: 4px; }
.heatmap-head { text-align: center; }
.heatmap-cell {
  display: block;
  width: 100%;
  height: 20px;
  border: 0;
  border-radius: 4px;
  padding: 0;
  cursor: pointer;
  background: transparent;
}
.heatmap-cell:hover { outline: 2px solid var(--blue); }
.heatmap-cell.is-focus { outline: 2px solid var(--amber); }
span.heatmap-cell.is-empty {
  border: 1px dashed var(--line);
  opacity: 0.3;
  cursor: default;
}

/* ===== Dark theme ===== */
:root[data-theme="dark"] {
  --bg: #0e1620;
  --surface: #16222e;
  --surface-soft: #1a2a33;
  --line: #2a3b4a;
  --text: #e3edf5;
  --muted: #93a7ba;
  --blue: #4da3dd;
  --green: #43b892;
  --red: #e07a7a;
  --amber: #d9a04a;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);
}

[data-theme="dark"] select,
[data-theme="dark"] input[type="search"] {
  background-color: var(--surface);
  color: var(--text);
  border-color: var(--line);
}

[data-theme="dark"] select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%2393a7ba' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='4,6 8,10 12,6'/%3E%3C/svg%3E");
}

[data-theme="dark"] input[type="search"] {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%2393a7ba' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='7' cy='7' r='4'/%3E%3Cline x1='10.5' y1='10.5' x2='14' y2='14'/%3E%3C/svg%3E");
}

[data-theme="dark"] .icon-button,
[data-theme="dark"] .expand-button,
[data-theme="dark"] .category-badge {
  background: var(--surface);
  border-color: var(--line);
}

[data-theme="dark"] .icon-button:hover,
[data-theme="dark"] .expand-button:hover,
[data-theme="dark"] .category-main:hover,
[data-theme="dark"] .mover-card:hover {
  background: #1d3141;
}

[data-theme="dark"] .icon-button:active { background: #234058; }

[data-theme="dark"] .category-row,
[data-theme="dark"] .mover-card {
  background: var(--surface);
}

[data-theme="dark"] .category-row.is-selected {
  background: #1c3040;
  border-color: #35597a;
}

[data-theme="dark"] .category-row.is-compared {
  background: var(--surface-soft);
}

[data-theme="dark"] .category-badge.positive {
  background: rgba(67, 184, 146, 0.12);
  border-color: rgba(67, 184, 146, 0.4);
}

[data-theme="dark"] .category-badge.negative {
  background: rgba(224, 122, 122, 0.12);
  border-color: rgba(224, 122, 122, 0.4);
}

[data-theme="dark"] tbody tr:nth-child(even) { background: rgba(255, 255, 255, 0.02); }
[data-theme="dark"] tbody tr:hover { background: #1d3141 !important; }
[data-theme="dark"] tbody tr.is-selected { background: #234058 !important; }

[data-theme="dark"] .status-pill.is-error {
  background: #3a1d1d;
  border-color: #6e3535;
}

[data-theme="dark"] .language-toggle .icon-button.is-active {
  background: var(--blue);
  border-color: var(--blue);
  color: #0e1620;
}

[data-theme="dark"] ::-webkit-scrollbar-thumb { background: #33475a; }
[data-theme="dark"] ::-webkit-scrollbar-thumb:hover { background: #44596e; }

/* ===== Mobile refinements ===== */
.panel-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.category-toggle {
  display: none;
  min-width: 34px;
}

@media (max-width: 1020px) {
  /* Controls in a 2x2 grid instead of a tall stack */
  .toolbar {
    grid-template-columns: 1fr 1fr;
  }

  /* Top movers as a 2x2 grid */
  .mover-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Collapsible category browser: data first, categories on demand */
  .category-toggle {
    display: inline-grid;
    place-items: center;
  }

  .category-panel.is-collapsed #categorySearch,
  .category-panel.is-collapsed .category-list {
    display: none;
  }

  .category-panel.is-collapsed .panel-head {
    margin-bottom: 0;
  }
}

@media (max-width: 640px) {
  /* Header buttons in a row, status pill filling the rest */
  .top-actions,
  .chart-actions {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
  }

  .status-pill {
    flex: 1;
    min-width: 0;
    text-align: left;
  }

  /* 16px inputs stop iOS from zooming in when a select is tapped */
  select,
  input[type="search"] {
    font-size: 16px;
  }

  .section-head {
    flex-wrap: wrap;
  }

  .density-controls,
  .seasonal-controls {
    flex-wrap: wrap;
    width: 100%;
  }

  .seasonal-controls select {
    min-width: 0;
    flex: 1;
  }

  /* Bigger tap targets for the month steppers */
  .month-nav .icon-button {
    padding: 6px 12px;
  }

  th,
  td {
    padding: 7px 6px;
    font-size: 0.84rem;
  }

  /* Phones: hide Code and Trend so the important columns fit */
  .table-section th:nth-child(1),
  .table-section td:nth-child(1),
  .table-section th:nth-child(7),
  .table-section td:nth-child(7) {
    display: none;
  }

  /* Summary: category name full width, metrics in 2x2 */
  .summary-strip {
    grid-template-columns: 1fr 1fr;
  }

  .summary-strip > div {
    padding: 12px;
    border-right: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
  }

  .summary-strip > div:first-child {
    grid-column: 1 / -1;
    border-right: none;
  }

  .summary-strip > div:nth-child(odd):not(:first-child) {
    border-right: none;
  }

  .summary-strip > div:nth-child(4),
  .summary-strip > div:nth-child(5) {
    border-bottom: none;
  }

  .metric-value {
    font-size: 1.1rem;
  }

  .mover-card {
    min-height: 72px;
    padding: 10px;
  }

  .mover-card strong {
    font-size: 1.05rem;
  }

  .chart-block,
  .table-section,
  .top-movers,
  .category-panel,
  .toolbar {
    padding: 12px;
  }

  .heatmap-wrap {
    max-height: 320px;
  }

  canvas {
    height: 260px !important;
  }
}
