/* ── CSS Custom Properties ──────────────────────────────────────────── */
:root {
  --bg: #f8f9fa;
  --bg-surface: #ffffff;
  --text: #1a1a2e;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --accent: #3b82f6;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.15);
  --radius: 8px;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --bg-surface: #1e293b;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --border: #334155;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.4);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background-color 0.3s, color 0.3s;
  min-height: 100vh;
  min-height: 100dvh;
}

/* Desktop: fixed viewport, no scroll */
@media (min-width: 901px) {
  body {
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
}

/* ── Header ────────────────────────────────────────────────────────── */
header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
}

.header-title h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.header-title .subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.controls {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.controls input[type="search"] {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  width: 220px;
  outline: none;
}

.controls input[type="search"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.layer-buttons {
  display: flex;
  gap: 4px;
}

.layer-btn {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s;
}

.layer-btn:hover { border-color: var(--accent); }
.layer-btn.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.theme-toggle {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
}

.info-btn {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
}

/* ── Main ──────────────────────────────────────────────────────────── */
main {
  flex: 1;
  display: flex;
  gap: 0;
  overflow: hidden;
  min-height: 0;
}

#treemap-container {
  flex: 1;
  position: relative;
  min-height: 0;
}

#treemap-container svg {
  width: 100%;
  height: 100%;
}

/* ── Sidebar ───────────────────────────────────────────────────────── */
.sidebar {
  width: 320px;
  padding: 16px;
  border-left: 1px solid var(--border);
  background: var(--bg-surface);
  overflow-y: auto;
  font-size: 0.85rem;
}

.sidebar h3 {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text);
}

.stat-card {
  background: var(--bg);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
}

.stat-card .stat-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--accent);
}

.stat-card .stat-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.top-list {
  list-style: none;
  margin-bottom: 16px;
}

.top-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
}

.top-list .score-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
  color: white;
  flex-shrink: 0;
}

/* ── Tooltip (desktop: follows cursor) ─────────────────────────────── */
.tooltip {
  position: fixed;
  pointer-events: none;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  box-shadow: var(--shadow-lg);
  max-width: 340px;
  z-index: 1000;
  font-size: 0.82rem;
  line-height: 1.5;
  transition: opacity 0.15s;
}

.tooltip.hidden { opacity: 0; display: none; }

.tooltip .tt-name {
  font-weight: 700;
  font-size: 0.9rem;
  margin-bottom: 6px;
}

.tooltip .tt-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
}

.tooltip .tt-label { color: var(--text-muted); }

.tooltip .tt-bar {
  height: 6px;
  border-radius: 3px;
  background: var(--border);
  margin: 6px 0;
  overflow: hidden;
}

.tooltip .tt-bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.2s;
}

.tooltip .tt-reason {
  margin-top: 6px;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-style: italic;
}

.tooltip .tt-hint {
  margin-top: 8px;
  font-size: 0.72rem;
  color: var(--accent);
}

/* ── Mobile detail sheet (replaces tooltip on mobile) ──────────────── */
.mobile-sheet-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 1500;
}

.mobile-sheet-overlay.visible { display: block; }

.mobile-sheet {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--bg-surface);
  border-radius: 16px 16px 0 0;
  padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
  box-shadow: 0 -4px 24px rgba(0,0,0,0.2);
  z-index: 2000;
  max-height: 65vh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform 0.25s ease-out;
}

.mobile-sheet.visible {
  display: block;
  transform: translateY(0);
}

.mobile-sheet .sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 12px;
}

.mobile-sheet .tt-name {
  font-weight: 700;
  font-size: 1rem;
  margin-bottom: 10px;
}

.mobile-sheet .tt-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 0.85rem;
}

.mobile-sheet .tt-label { color: var(--text-muted); }

.mobile-sheet .tt-bar {
  height: 8px;
  border-radius: 4px;
  background: var(--border);
  margin: 8px 0;
  overflow: hidden;
}

.mobile-sheet .tt-bar-fill {
  height: 100%;
  border-radius: 4px;
}

.mobile-sheet .tt-reason {
  margin-top: 8px;
  font-size: 0.82rem;
  color: var(--text-muted);
  font-style: italic;
  line-height: 1.5;
}

.mobile-sheet .sheet-link {
  display: block;
  margin-top: 12px;
  padding: 10px 16px;
  background: var(--accent);
  color: white;
  text-align: center;
  text-decoration: none;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 600;
}

/* ── Legend ─────────────────────────────────────────────────────────── */
.legend {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 24px;
  font-size: 0.75rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.legend-gradient {
  width: 200px;
  height: 12px;
  border-radius: 6px;
  flex-shrink: 0;
}

/* ── Modal ─────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.modal-overlay.hidden { display: none; }

.modal {
  background: var(--bg-surface);
  border-radius: 12px;
  padding: 24px;
  max-width: 560px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  margin: 16px;
}

.modal h2 {
  font-size: 1.1rem;
  margin-bottom: 12px;
}

.modal p, .modal li {
  font-size: 0.85rem;
  line-height: 1.6;
  color: var(--text-muted);
}

.modal ul { margin-left: 20px; margin-bottom: 12px; }

.modal-close {
  margin-top: 16px;
  padding: 8px 16px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
}

/* ── Footer ────────────────────────────────────────────────────────── */
footer {
  padding: 12px 24px;
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
  flex-shrink: 0;
}

/* ── Treemap cells ─────────────────────────────────────────────────── */
.cell rect {
  stroke: var(--bg);
  stroke-width: 1px;
  cursor: pointer;
  transition: opacity 0.2s;
}

.cell rect:hover { opacity: 0.85; }

.cell.dimmed rect { opacity: 0.15; }
.cell.dimmed text { opacity: 0.15; }

.cell.selected rect {
  stroke: var(--accent);
  stroke-width: 2.5px;
}

.cell text {
  font-size: 10px;
  fill: white;
  pointer-events: none;
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

.group-label {
  font-size: 11px;
  font-weight: 700;
  fill: var(--text);
  pointer-events: none;
}

/* ── Search results ────────────────────────────────────────────────── */
.search-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-height: 200px;
  overflow-y: auto;
  z-index: 500;
  box-shadow: var(--shadow);
}

.search-results.hidden { display: none; }

.search-result-item {
  padding: 6px 12px;
  cursor: pointer;
  font-size: 0.82rem;
  border-bottom: 1px solid var(--border);
}

.search-result-item:hover { background: var(--bg); }

.search-wrapper {
  position: relative;
}

/* ── Responsive: Tablet ────────────────────────────────────────────── */
@media (max-width: 900px) {
  body {
    display: block;
    overflow-y: auto;
    height: auto;
  }
  main {
    flex-direction: column;
    overflow: visible;
    min-height: auto;
  }
  #treemap-container {
    height: 55vw;
    min-height: 320px;
    max-height: 500px;
  }
  .sidebar {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
  }
  /* Hide desktop tooltip on touch devices */
  .tooltip { display: none !important; }
}

/* ── Responsive: Mobile ────────────────────────────────────────────── */
@media (max-width: 600px) {
  header { padding: 12px 16px; }
  .header-top { flex-direction: column; gap: 10px; }
  .header-title h1 { font-size: 1.15rem; }
  .header-title .subtitle { font-size: 0.75rem; }
  .controls { width: 100%; }
  .controls input[type="search"] { width: 100%; }
  .layer-buttons { width: 100%; }
  .layer-btn { flex: 1; text-align: center; padding: 8px 6px; font-size: 0.72rem; }
  .sidebar { padding: 12px 16px; }
  .sidebar h3 { font-size: 0.85rem; }
  .stat-grid {
    grid-template-columns: 1fr 1fr 1fr;
  }
  .stat-card { padding: 8px; }
  .stat-card .stat-value { font-size: 1.1rem; }
  .stat-card .stat-label { font-size: 0.68rem; }
  .top-list li { font-size: 0.75rem; }
  #treemap-container {
    height: 70vw;
    min-height: 280px;
    max-height: 400px;
  }
  .legend { padding: 6px 16px; font-size: 0.7rem; gap: 6px; justify-content: center; }
  .legend-gradient { width: 120px; }
  footer { padding: 10px 16px; font-size: 0.68rem; }
  .group-label { font-size: 9px; }
  .cell text { font-size: 8px; }
}
