/* ===== Design Tokens ===== */
:root {
  --text-xs:   clamp(0.75rem, 0.7rem + 0.25vw, 0.8125rem);
  --text-sm:   clamp(0.8125rem, 0.78rem + 0.2vw, 0.875rem);
  --text-base: clamp(0.875rem, 0.85rem + 0.15vw, 0.9375rem);
  --text-lg:   clamp(1rem, 0.95rem + 0.3vw, 1.125rem);
  --text-xl:   clamp(1.125rem, 1rem + 0.5vw, 1.375rem);

  --space-1:  0.25rem;
  --space-2:  0.5rem;
  --space-3:  0.75rem;
  --space-4:  1rem;
  --space-5:  1.25rem;
  --space-6:  1.5rem;
  --space-8:  2rem;

  --radius-sm: 0.25rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;

  --transition-interactive: 180ms cubic-bezier(0.16, 1, 0.3, 1);

  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', monospace;

  --sidebar-width: 360px;
  --header-height: 56px;
  --footer-height: 36px;
}

/* ===== Light Mode ===== */
:root, [data-theme="light"] {
  --color-bg: #f5f5f0;
  --color-surface: #ffffff;
  --color-surface-2: #fafaf8;
  --color-border: #e0ddd8;
  --color-divider: #eae8e3;
  --color-text: #1a1a1a;
  --color-text-muted: #6b6b68;
  --color-text-faint: #a0a09b;
  --color-primary: #01696f;
  --color-primary-hover: #0c4e54;
  --color-gold: #d19900;
  --color-gold-hover: #b07a00;
  --color-orange: #da7101;
  --color-over: #c0392b;
  --color-under: #27ae60;
  --color-neutral-val: #888;
  --color-map-bg: #e8e6e1;
  --color-map-stroke: #ccc;
  --color-map-nodata: #d9d9d9;
  --color-tooltip-bg: rgba(255,255,255,0.96);
  --color-tooltip-border: rgba(0,0,0,0.08);
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
}

/* ===== Dark Mode ===== */
[data-theme="dark"] {
  --color-bg: #111111;
  --color-surface: #1a1a1a;
  --color-surface-2: #222222;
  --color-border: #333333;
  --color-divider: #2a2a2a;
  --color-text: #e0e0de;
  --color-text-muted: #8a8a87;
  --color-text-faint: #5a5a57;
  --color-primary: #4f98a3;
  --color-primary-hover: #3a7a84;
  --color-gold: #e8af34;
  --color-gold-hover: #fdc551;
  --color-orange: #fdab43;
  --color-over: #e74c3c;
  --color-under: #2ecc71;
  --color-neutral-val: #777;
  --color-map-bg: #1a1a1a;
  --color-map-stroke: #333;
  --color-map-nodata: #2a2a2a;
  --color-tooltip-bg: rgba(26,26,26,0.96);
  --color-tooltip-border: rgba(255,255,255,0.08);
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.2);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.4);
}

/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

html, body { height: 100%; overflow: hidden; }

body {
  font-family: var(--font-body);
  font-size: var(--text-base);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.5;
}

button { cursor: pointer; background: none; border: none; font: inherit; color: inherit; }
a { color: var(--color-primary); text-decoration: none; }
a:hover { color: var(--color-primary-hover); }
table { border-collapse: collapse; width: 100%; }

/* ===== App Layout ===== */
.app {
  display: grid;
  grid-template-rows: var(--header-height) 1fr var(--footer-height);
  height: 100dvh;
  overflow: hidden;
}

.main {
  display: grid;
  grid-template-columns: 1fr var(--sidebar-width);
  overflow: hidden;
}

/* ===== Header ===== */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-5);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  z-index: 20;
  gap: var(--space-4);
}

.header-left {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-shrink: 0;
}

.logo { flex-shrink: 0; }

.header-title h1 {
  font-size: var(--text-lg);
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

.header-subtitle {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  letter-spacing: 0.02em;
  text-transform: uppercase;
  font-weight: 500;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: var(--space-5);
}

.year-control {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.year-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.year-value {
  font-family: var(--font-mono);
  font-size: var(--text-lg);
  font-weight: 600;
  min-width: 3.5ch;
  text-align: center;
  color: var(--color-primary);
}

/* Slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 200px;
  height: 6px;
  background: var(--color-border);
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--color-primary);
  border: 2px solid var(--color-surface);
  box-shadow: var(--shadow-sm);
  transition: transform var(--transition-interactive);
}

input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.15);
}

input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--color-primary);
  border: 2px solid var(--color-surface);
  box-shadow: var(--shadow-sm);
}

.theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  transition: background var(--transition-interactive), color var(--transition-interactive);
}

.theme-toggle:hover {
  background: var(--color-surface-2);
  color: var(--color-text);
}

/* ===== Map Container ===== */
.map-container {
  position: relative;
  overflow: hidden;
  background: var(--color-map-bg);
}

#map {
  width: 100%;
  height: 100%;
}

#map svg {
  width: 100%;
  height: 100%;
}

/* ===== Legend ===== */
.legend {
  position: absolute;
  bottom: var(--space-4);
  left: var(--space-4);
  background: var(--color-tooltip-bg);
  border: 1px solid var(--color-tooltip-border);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow-md);
  z-index: 5;
}

.legend-title {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: var(--space-2);
}

.legend-bar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.legend-gradient {
  width: 160px;
  height: 12px;
  border-radius: 2px;
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  width: 160px;
  margin-top: 2px;
}

.legend-label {
  font-size: 10px;
  font-family: var(--font-mono);
  color: var(--color-text-muted);
}

/* ===== Tooltip ===== */
.tooltip {
  position: fixed;
  pointer-events: none;
  background: var(--color-tooltip-bg);
  border: 1px solid var(--color-tooltip-border);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  box-shadow: var(--shadow-lg);
  backdrop-filter: blur(12px);
  z-index: 100;
  opacity: 0;
  transition: opacity 120ms ease;
  max-width: 260px;
}

.tooltip.visible { opacity: 1; }

.tooltip-name {
  font-weight: 600;
  font-size: var(--text-sm);
  margin-bottom: var(--space-1);
}

.tooltip-row {
  display: flex;
  justify-content: space-between;
  gap: var(--space-4);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  line-height: 1.7;
}

.tooltip-val {
  font-family: var(--font-mono);
  font-weight: 500;
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
}

.tooltip-valuation {
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: var(--text-sm);
  margin-top: var(--space-1);
  padding-top: var(--space-1);
  border-top: 1px solid var(--color-divider);
}

.tooltip-valuation.over { color: var(--color-over); }
.tooltip-valuation.under { color: var(--color-under); }

/* ===== Side Panel ===== */
.side-panel {
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  overflow: hidden;
}

.panel-tabs {
  display: flex;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.tab {
  flex: 1;
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-text-muted);
  border-bottom: 2px solid transparent;
  transition: color var(--transition-interactive), border-color var(--transition-interactive);
}

.tab:hover { color: var(--color-text); }

.tab.active {
  color: var(--color-primary);
  border-bottom-color: var(--color-primary);
}

.panel-content {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.tab-content {
  display: none;
  position: absolute;
  inset: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.tab-content.active { display: flex; flex-direction: column; }

/* ===== Table ===== */
.table-wrapper {
  flex: 1;
}

#country-table {
  font-size: var(--text-xs);
}

#country-table thead {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--color-surface);
}

#country-table th {
  padding: var(--space-2) var(--space-3);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
  border-bottom: 1px solid var(--color-border);
  text-align: left;
  white-space: nowrap;
  user-select: none;
}

#country-table th.num { text-align: right; }

.sortable { cursor: pointer; }
.sortable:hover { color: var(--color-text); }

.sort-arrow {
  font-size: 9px;
  opacity: 0.3;
  margin-left: 2px;
}

.active-sort .sort-arrow { opacity: 1; color: var(--color-primary); }

#country-table td {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--color-divider);
  font-variant-numeric: tabular-nums lining-nums;
  white-space: nowrap;
}

#country-table td:nth-child(2),
#country-table td:nth-child(3) {
  text-align: right;
  font-family: var(--font-mono);
  font-size: 12px;
}

#country-table tbody tr {
  cursor: pointer;
  transition: background var(--transition-interactive);
}

#country-table tbody tr:hover {
  background: var(--color-surface-2);
}

#country-table tbody tr.selected {
  background: color-mix(in srgb, var(--color-primary) 10%, transparent);
}

.val-over { color: var(--color-over); }
.val-under { color: var(--color-under); }
.val-neutral { color: var(--color-neutral-val); }

.country-flag {
  display: inline-block;
  width: 18px;
  margin-right: var(--space-1);
  vertical-align: middle;
}

/* ===== Trend Chart ===== */
#trend-view { padding: var(--space-4); }

#trend-header {
  margin-bottom: var(--space-3);
}

#trend-header h3 {
  font-size: var(--text-lg);
  font-weight: 600;
}

#trend-header .trend-subtitle {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-top: var(--space-1);
}

.trend-hint {
  color: var(--color-text-faint);
  font-size: var(--text-sm);
  font-style: italic;
}

#trend-chart {
  flex: 1;
  min-height: 0;
}

#trend-chart svg {
  width: 100%;
  height: 100%;
}

.trend-line {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.trend-area {
  opacity: 0.08;
}

.trend-dot {
  stroke-width: 2;
}

.trend-zero {
  stroke: var(--color-text-faint);
  stroke-dasharray: 4, 4;
  stroke-width: 1;
}

.trend-axis text {
  font-family: var(--font-mono);
  font-size: 10px;
  fill: var(--color-text-muted);
}

.trend-axis path,
.trend-axis line {
  stroke: var(--color-divider);
}

.trend-grid line {
  stroke: var(--color-divider);
  stroke-dasharray: 2, 4;
}

.trend-tooltip-line {
  stroke: var(--color-text-faint);
  stroke-dasharray: 3, 3;
}

/* ===== Footer ===== */
.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-5);
  font-size: var(--text-xs);
  color: var(--color-text-faint);
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
}

.footer a { color: var(--color-text-muted); }
.footer a:hover { color: var(--color-primary); }

/* ===== Mobile Toggle ===== */
.panel-toggle {
  display: none;
  position: fixed;
  bottom: calc(var(--footer-height) + var(--space-3));
  right: var(--space-3);
  z-index: 30;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--color-primary);
  color: #fff;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg);
  font-size: 20px;
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
  :root {
    --sidebar-width: 100%;
  }

  .main {
    grid-template-columns: 1fr;
    position: relative;
  }

  .map-container {
    grid-row: 1;
  }

  .side-panel {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 45%;
    border-left: none;
    border-top: 1px solid var(--color-border);
    transform: translateY(calc(100% - 48px));
    transition: transform 300ms cubic-bezier(0.16, 1, 0.3, 1);
    z-index: 15;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: var(--shadow-lg);
  }

  .side-panel.open {
    transform: translateY(0);
  }

  .panel-tabs {
    cursor: grab;
  }

  .panel-tabs::before {
    content: '';
    display: block;
    width: 32px;
    height: 4px;
    background: var(--color-border);
    border-radius: 2px;
    margin: var(--space-2) auto;
  }

  input[type="range"] { width: 120px; }

  .header-subtitle { display: none; }

  .legend {
    bottom: calc(48px + var(--space-3));
  }
}

@media (max-width: 480px) {
  .header { padding: 0 var(--space-3); }
  .year-label { display: none; }
  input[type="range"] { width: 80px; }
  .header-title h1 { font-size: var(--text-sm); }
}

/* ===== Focus styles ===== */
:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
