/* === Nebraska LHD Health Dashboard — Tokens & Styles === */
:root {
  --text-xs:   clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
  --text-sm:   clamp(0.875rem, 0.8rem + 0.35vw, 1rem);
  --text-base: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
  --text-lg:   clamp(1.125rem, 1rem + 0.75vw, 1.5rem);
  --text-xl:   clamp(1.5rem, 1.2rem + 1.25vw, 2.25rem);
  --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; --space-10: 2.5rem; --space-12: 3rem;
  --font-body: 'General Sans', 'Inter', sans-serif;
  --font-display: 'Cabinet Grotesk', 'General Sans', sans-serif;
  --radius-sm: 0.375rem; --radius-md: 0.5rem; --radius-lg: 0.75rem; --radius-xl: 1rem; --radius-full: 9999px;
  --transition-interactive: 180ms cubic-bezier(0.16, 1, 0.3, 1);
  --sidebar-w: 390px;
}

/* === LIGHT === */
:root, [data-theme="light"] {
  --color-bg: #f5f3ef;
  --color-surface: #ffffff;
  --color-surface-2: #faf9f7;
  --color-surface-offset: #edeae5;
  --color-divider: #dcd8d0;
  --color-border: #ccc8bf;
  --color-text: #2a2520;
  --color-text-muted: #6b6358;
  --color-text-faint: #a09889;
  --color-text-inverse: #ffffff;
  --color-primary: #8b5e3c;
  --color-primary-hover: #6e4a2f;
  --color-primary-active: #533822;
  --color-primary-highlight: #eddfd3;
  --color-blue: #3a7ca5;
  --color-success: #4a7c39;
  --color-warning: #b8702a;
  --color-error: #a13535;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.07);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.10);
  --chart-1: #8b5e3c; --chart-2: #3a7ca5; --chart-3: #4a7c39;
  --chart-4: #b8702a; --chart-5: #a13535; --chart-6: #6b6358;
  /* Choropleth — 5-class earth tones */
  --choro-1: #f5e6d8; --choro-2: #d4b398; --choro-3: #b8825c; --choro-4: #8b5e3c; --choro-5: #5a3520;
  --choro-nodata: #e5e2dd;
}

/* === DARK === */
[data-theme="dark"] {
  --color-bg: #141210;
  --color-surface: #1c1a17;
  --color-surface-2: #211f1b;
  --color-surface-offset: #191714;
  --color-divider: #2e2b26;
  --color-border: #3d3932;
  --color-text: #d4cfc8;
  --color-text-muted: #8c857a;
  --color-text-faint: #5c564e;
  --color-text-inverse: #141210;
  --color-primary: #c49270;
  --color-primary-hover: #a97b59;
  --color-primary-active: #8d6447;
  --color-primary-highlight: #2e261f;
  --color-blue: #5da3ca;
  --color-success: #6ba852;
  --color-warning: #d9924a;
  --color-error: #d96060;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.35);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.4);
  --chart-1: #c49270; --chart-2: #5da3ca; --chart-3: #6ba852;
  --chart-4: #d9924a; --chart-5: #d96060; --chart-6: #8c857a;
  --choro-1: #2e261f; --choro-2: #4a3828; --choro-3: #7a5a3a; --choro-4: #c49270; --choro-5: #e8c4a0;
  --choro-nodata: #1c1a17;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --color-bg: #141210; --color-surface: #1c1a17; --color-surface-2: #211f1b;
    --color-surface-offset: #191714; --color-divider: #2e2b26; --color-border: #3d3932;
    --color-text: #d4cfc8; --color-text-muted: #8c857a; --color-text-faint: #5c564e;
    --color-text-inverse: #141210; --color-primary: #c49270;
    --color-primary-hover: #a97b59; --color-primary-active: #8d6447;
    --color-primary-highlight: #2e261f; --color-blue: #5da3ca;
    --color-success: #6ba852; --color-warning: #d9924a; --color-error: #d96060;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3); --shadow-md: 0 4px 12px rgba(0,0,0,0.35);
    --shadow-lg: 0 12px 32px rgba(0,0,0,0.4);
    --chart-1: #c49270; --chart-2: #5da3ca; --chart-3: #6ba852;
    --chart-4: #d9924a; --chart-5: #d96060; --chart-6: #8c857a;
    --choro-1: #2e261f; --choro-2: #4a3828; --choro-3: #7a5a3a; --choro-4: #c49270; --choro-5: #e8c4a0;
    --choro-nodata: #1c1a17;
  }
}

/* === LAYOUT === */
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; }
body { font-family: var(--font-body); background: var(--color-bg); color: var(--color-text); display: flex; flex-direction: column; }
*, *::before, *::after { box-sizing: border-box; }

/* Header */
.header { height: 54px; background: var(--color-surface); border-bottom: 1px solid var(--color-divider); flex-shrink: 0; z-index: 100; }
.header-inner { max-width: 100%; height: 100%; display: flex; align-items: center; justify-content: space-between; padding: 0 var(--space-5); }
.header-brand { display: flex; align-items: center; gap: var(--space-3); }
.header-logo { color: var(--color-primary); }
.header-title { font-family: var(--font-display); font-size: var(--text-base); font-weight: 700; margin: 0; line-height: 1.2; }
.header-subtitle { font-size: var(--text-xs); color: var(--color-text-muted); margin: 0; line-height: 1.3; }
.header-actions { display: flex; align-items: center; gap: var(--space-3); }
.header-link { display: flex; align-items: center; gap: 4px; font-size: var(--text-xs); color: var(--color-text-muted); text-decoration: none; transition: color var(--transition-interactive); }
.header-link:hover { color: var(--color-primary); }
.theme-toggle { background: none; border: none; color: var(--color-text-muted); cursor: pointer; padding: var(--space-1); border-radius: var(--radius-sm); transition: color var(--transition-interactive); }
.theme-toggle:hover { color: var(--color-primary); }

/* Main */
.main-layout { flex: 1; display: flex; min-height: 0; position: relative; }
.map-area { flex: 1; position: relative; min-width: 0; }
#map { width: 100%; height: 100%; background: var(--color-bg); }

/* View Toggle (LHD vs County) */
.view-toggle { position: absolute; top: 12px; left: 56px; z-index: 600; display: flex; background: var(--color-surface); border: 1px solid var(--color-divider); border-radius: var(--radius-full); overflow: hidden; box-shadow: var(--shadow-md); }
.view-btn { padding: 6px 14px; font-family: var(--font-body); font-size: var(--text-xs); font-weight: 500; border: none; background: transparent; color: var(--color-text-muted); cursor: pointer; transition: all var(--transition-interactive); white-space: nowrap; }
.view-btn.active { background: var(--color-primary); color: var(--color-text-inverse); }
.view-btn:hover:not(.active) { color: var(--color-text); background: var(--color-surface-offset); }

/* Legend */
.map-legend { position: absolute; bottom: 24px; left: 12px; z-index: 500; background: var(--color-surface); border: 1px solid var(--color-divider); border-radius: var(--radius-md); padding: var(--space-3); box-shadow: var(--shadow-md); min-width: 130px; }
.legend-title { font-size: var(--text-xs); font-weight: 600; margin-bottom: var(--space-2); }
.legend-scale { display: flex; height: 12px; border-radius: 2px; overflow: hidden; margin-bottom: 4px; }
.legend-scale span { flex: 1; }
.legend-labels { display: flex; justify-content: space-between; font-size: 11px; color: var(--color-text-muted); }
.legend-no-data { display: flex; align-items: center; gap: 6px; margin-top: 6px; font-size: 11px; color: var(--color-text-faint); }
.legend-swatch { display: inline-block; width: 14px; height: 10px; border-radius: 2px; }
.no-data-swatch { background: var(--choro-nodata); }

/* Sidebar */
.sidebar { width: var(--sidebar-w); background: var(--color-surface); border-left: 1px solid var(--color-divider); flex-shrink: 0; display: flex; flex-direction: column; overflow: hidden; }
.sidebar-scroll { overflow-y: auto; flex: 1; padding: var(--space-4); display: flex; flex-direction: column; gap: var(--space-5); }
.sidebar-toggle { display: none; position: absolute; bottom: 16px; right: 16px; z-index: 600; background: var(--color-primary); color: var(--color-text-inverse); border: none; width: 48px; height: 48px; border-radius: var(--radius-full); cursor: pointer; box-shadow: var(--shadow-lg); }

/* Controls */
.ctrl-section { display: flex; flex-direction: column; gap: var(--space-4); }
.ctrl-group { display: flex; flex-direction: column; gap: var(--space-2); }
.ctrl-label { font-size: var(--text-xs); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--color-text-muted); }
.ctrl-select { appearance: none; width: 100%; padding: 8px 32px 8px 12px; border: 1px solid var(--color-border); border-radius: var(--radius-md); background: var(--color-surface-2) url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%236b6358' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") right 12px center no-repeat; font-family: var(--font-body); font-size: var(--text-sm); color: var(--color-text); transition: border-color var(--transition-interactive); cursor: pointer; }
.ctrl-select:focus { outline: 2px solid var(--color-primary); outline-offset: -1px; }

/* Category Tabs */
.category-tabs { display: flex; flex-wrap: wrap; gap: 6px; }
.cat-tab { padding: 6px 12px; border: 1px solid var(--color-border); border-radius: var(--radius-full); background: transparent; font-family: var(--font-body); font-size: var(--text-xs); font-weight: 500; color: var(--color-text-muted); cursor: pointer; transition: all var(--transition-interactive); white-space: nowrap; }
.cat-tab.active { background: var(--color-primary); border-color: var(--color-primary); color: var(--color-text-inverse); }
.cat-tab:hover:not(.active) { border-color: var(--color-primary); color: var(--color-primary); }

/* Search */
.search-group { position: relative; }
.search-icon { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--color-text-faint); pointer-events: none; }
.search-input { width: 100%; padding: 8px 12px 8px 32px; border: 1px solid var(--color-border); border-radius: var(--radius-md); background: var(--color-surface-2); font-family: var(--font-body); font-size: var(--text-sm); color: var(--color-text); }
.search-input:focus { outline: 2px solid var(--color-primary); outline-offset: -1px; }
.search-results { max-height: 160px; overflow-y: auto; display: none; }
.search-results.visible { display: block; }
.search-result-item { padding: 8px 10px; font-size: var(--text-xs); cursor: pointer; border-bottom: 1px solid var(--color-divider); transition: background var(--transition-interactive); }
.search-result-item:hover { background: var(--color-surface-offset); }
.search-result-item .result-type { font-size: 10px; color: var(--color-text-faint); text-transform: uppercase; letter-spacing: 0.05em; margin-left: 6px; }

/* Stats */
.stats-section { background: var(--color-surface-2); border-radius: var(--radius-lg); padding: var(--space-4); border: 1px solid var(--color-divider); }
.section-title { font-family: var(--font-display); font-size: var(--text-sm); font-weight: 700; margin: 0 0 var(--space-3) 0; }
.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--space-2); }
.stat-card { text-align: center; padding: var(--space-3) var(--space-2); background: var(--color-surface); border: 1px solid var(--color-divider); border-radius: var(--radius-md); }
.stat-value { display: block; font-family: var(--font-display); font-size: var(--text-lg); font-weight: 700; color: var(--color-primary); }
.stat-label { display: block; font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--color-text-faint); margin-top: 2px; }
.stat-context { font-size: var(--text-xs); color: var(--color-text-muted); margin: var(--space-3) 0 0 0; }

/* Download Button */
.download-btn { display: flex; align-items: center; gap: 6px; margin-top: var(--space-3); padding: 8px 16px; width: 100%; justify-content: center; background: var(--color-primary); color: var(--color-text-inverse); border: none; border-radius: var(--radius-md); font-family: var(--font-body); font-size: var(--text-xs); font-weight: 600; cursor: pointer; transition: background var(--transition-interactive); }
.download-btn:hover { background: var(--color-primary-hover); }
.download-btn:active { background: var(--color-primary-active); }

/* Highlight Banner */
.highlight-banner { position: absolute; top: 56px; left: 50%; transform: translateX(-50%); z-index: 800; pointer-events: auto; }
.highlight-banner-inner { display: flex; align-items: center; gap: 8px; padding: 8px 14px; background: var(--color-surface); border: 2px solid #d4881c; border-radius: var(--radius-full); box-shadow: 0 2px 12px rgba(0,0,0,0.15); font-size: var(--text-xs); font-weight: 600; color: var(--color-text); white-space: nowrap; }
.highlight-swatch { display: inline-block; width: 14px; height: 14px; border-radius: 3px; background: #d4881c; border: 1.5px solid #b8651a; flex-shrink: 0; }
[data-theme="dark"] .highlight-swatch { background: #e6a32e; border-color: #ffc857; }
[data-theme="dark"] .highlight-banner-inner { border-color: #e6a32e; }
.highlight-label { max-width: 280px; overflow: hidden; text-overflow: ellipsis; }
.highlight-clear { display: flex; align-items: center; gap: 4px; padding: 4px 10px; background: none; border: 1px solid var(--color-border); border-radius: var(--radius-full); font-family: var(--font-body); font-size: 11px; color: var(--color-text-muted); cursor: pointer; transition: all var(--transition-interactive); }
.highlight-clear:hover { background: var(--color-surface-offset); color: var(--color-text); border-color: var(--color-text-muted); }

/* AI Chat */
.chat-section { background: var(--color-surface-2); border-radius: var(--radius-lg); padding: var(--space-4); border: 1px solid var(--color-divider); }
.chat-messages { max-height: 360px; min-height: 120px; overflow-y: auto; margin-bottom: var(--space-3); display: flex; flex-direction: column; gap: var(--space-3); }
.chat-welcome { font-size: var(--text-xs); color: var(--color-text-muted); line-height: 1.5; }
.chat-welcome p { margin: 0 0 var(--space-2) 0; }
.chat-example { display: block; width: 100%; text-align: left; padding: 8px 12px; margin-bottom: 6px; background: var(--color-surface); border: 1px solid var(--color-divider); border-radius: var(--radius-md); font-family: var(--font-body); font-size: var(--text-xs); color: var(--color-text-muted); cursor: pointer; transition: all var(--transition-interactive); }
.chat-example:hover { background: var(--color-surface-offset); color: var(--color-text); border-color: var(--color-primary); }
.chat-input-row { display: flex; gap: var(--space-2); }
.chat-input { flex: 1; padding: 10px 12px; border: 1px solid var(--color-border); border-radius: var(--radius-md); background: var(--color-surface); color: var(--color-text); font-family: var(--font-body); font-size: var(--text-xs); outline: none; transition: border-color var(--transition-interactive); }
.chat-input:focus { border-color: var(--color-primary); }
.chat-input::placeholder { color: var(--color-text-faint); }
.chat-send { flex-shrink: 0; width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; background: var(--color-primary); color: var(--color-text-inverse); border: none; border-radius: var(--radius-md); cursor: pointer; transition: background var(--transition-interactive); }
.chat-send:hover { background: var(--color-primary-hover); }
.chat-send:disabled { opacity: 0.5; cursor: not-allowed; }
.chat-bubble { padding: var(--space-3); border-radius: var(--radius-md); font-size: var(--text-xs); line-height: 1.6; max-width: 100%; word-wrap: break-word; }
.chat-bubble.user { background: var(--color-primary); color: var(--color-text-inverse); align-self: flex-end; border-bottom-right-radius: 4px; }
.chat-bubble.assistant { background: var(--color-surface); border: 1px solid var(--color-divider); color: var(--color-text); align-self: flex-start; border-bottom-left-radius: 4px; }
.chat-bubble.assistant p { margin: 0 0 0.5em 0; }
.chat-bubble.assistant p:last-child { margin-bottom: 0; }
.chat-bubble.assistant table { width: 100%; border-collapse: collapse; margin: 0.5em 0; font-size: inherit; }
.chat-bubble.assistant th, .chat-bubble.assistant td { padding: 4px 8px; text-align: left; border-bottom: 1px solid var(--color-divider); }
.chat-bubble.assistant th { font-weight: 600; background: var(--color-surface-offset); }
.chat-bubble.assistant strong { color: var(--color-primary); }
.chat-bubble.assistant ol, .chat-bubble.assistant ul { margin: 0.4em 0; padding-left: 1.4em; }
.chat-bubble.assistant li { margin-bottom: 0.2em; }
.chat-loading { display: flex; gap: 5px; padding: var(--space-3); align-self: flex-start; }
.chat-loading span { width: 8px; height: 8px; background: var(--color-text-faint); border-radius: 50%; animation: chatBounce 1.2s ease-in-out infinite; }
.chat-loading span:nth-child(2) { animation-delay: 0.2s; }
.chat-loading span:nth-child(3) { animation-delay: 0.4s; }
@keyframes chatBounce { 0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; } 40% { transform: scale(1); opacity: 1; } }

/* Detail */
.detail-section { background: var(--color-surface-2); border-radius: var(--radius-lg); padding: var(--space-4); border: 1px solid var(--color-divider); }
.detail-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: var(--space-3); }
.detail-title { font-family: var(--font-display); font-size: var(--text-sm); font-weight: 700; margin: 0; }
.detail-subtitle { font-size: var(--text-xs); color: var(--color-text-muted); margin: 2px 0 0 0; }
.detail-close { background: none; border: none; color: var(--color-text-faint); cursor: pointer; padding: var(--space-1); border-radius: var(--radius-sm); }
.detail-close:hover { color: var(--color-text); background: var(--color-surface-offset); }

.detail-chart-wrap { height: 220px; margin-bottom: var(--space-4); }
.detail-chart-wrap canvas { width: 100% !important; height: 100% !important; }

.detail-table-wrap { max-height: 260px; overflow-y: auto; border-radius: var(--radius-sm); border: 1px solid var(--color-divider); }
.detail-table { width: 100%; border-collapse: collapse; font-size: var(--text-xs); }
.detail-table th { position: sticky; top: 0; background: var(--color-surface-offset); padding: 8px 10px; text-align: left; font-weight: 600; border-bottom: 1px solid var(--color-divider); }
.detail-table td { padding: 7px 10px; border-bottom: 1px solid var(--color-divider); }
.detail-table tr:last-child td { border-bottom: none; }
.detail-table tr:hover td { background: var(--color-primary-highlight); }

/* Compare */
.compare-section { background: var(--color-surface-2); border-radius: var(--radius-lg); padding: var(--space-4); border: 1px solid var(--color-divider); }
.compare-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--space-3); }
.compare-clear { background: none; border: 1px solid var(--color-border); font-family: var(--font-body); font-size: var(--text-xs); color: var(--color-text-muted); padding: 4px 10px; border-radius: var(--radius-full); cursor: pointer; }
.compare-clear:hover { border-color: var(--color-primary); color: var(--color-primary); }
.compare-names { display: flex; align-items: center; gap: var(--space-2); margin-bottom: var(--space-3); font-size: var(--text-xs); }
.compare-name { padding: 3px 10px; border-radius: var(--radius-full); font-weight: 600; }
.compare-a { background: var(--color-primary); color: var(--color-text-inverse); }
.compare-b { background: var(--color-blue); color: var(--color-text-inverse); }
.compare-vs { color: var(--color-text-faint); font-weight: 500; }
.compare-chart-wrap { height: 300px; }
.compare-chart-wrap canvas { width: 100% !important; height: 100% !important; }

/* Loading */
.data-loading { display: flex; flex-direction: column; align-items: center; gap: var(--space-3); padding: var(--space-8) 0; }
.loading-spinner { width: 28px; height: 28px; border: 3px solid var(--color-divider); border-top-color: var(--color-primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { font-size: var(--text-sm); font-weight: 500; }
.loading-subtext { font-size: var(--text-xs); color: var(--color-text-faint); }

/* Empty */
.empty-hint { display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--space-3); padding: var(--space-8) var(--space-4); }
.empty-hint p { font-size: var(--text-sm); color: var(--color-text-muted); line-height: 1.5; max-width: 260px; margin: 0; }

/* Footer */
.footer { height: auto; min-height: 36px; background: var(--color-surface); border-top: 1px solid var(--color-divider); flex-shrink: 0; }
.footer-inner { display: flex; justify-content: space-between; align-items: center; padding: var(--space-2) var(--space-5); gap: var(--space-4); flex-wrap: wrap; }
.footer-left { min-width: 0; flex: 1; }
.footer-source { font-size: 11px; color: var(--color-text-muted); margin: 0; }
.footer-source a { color: var(--color-primary); text-decoration: none; }
.footer-source a:hover { text-decoration: underline; }
.footer-note { font-size: 10px; color: var(--color-text-faint); margin: 2px 0 0 0; }
.footer-right { flex-shrink: 0; }
.footer-attribution { font-size: 11px; color: var(--color-text-faint); text-decoration: none; }
.footer-attribution:hover { color: var(--color-primary); }

/* Tooltip */
.lhd-tooltip { font-family: var(--font-body); font-size: 13px; line-height: 1.4; padding: 8px 12px !important; border-radius: var(--radius-md) !important; box-shadow: var(--shadow-md) !important; border: 1px solid var(--color-divider) !important; background: var(--color-surface) !important; color: var(--color-text) !important; }
.lhd-tooltip .tooltip-name { font-weight: 600; }
.lhd-tooltip .tooltip-value { color: var(--color-primary); font-weight: 700; }
.lhd-tooltip .tooltip-sub { font-size: 11px; color: var(--color-text-muted); }

/* Leaflet overrides */
.leaflet-control-zoom { border: 1px solid var(--color-divider) !important; box-shadow: var(--shadow-sm) !important; border-radius: var(--radius-md) !important; overflow: hidden; }
.leaflet-control-zoom a { background: var(--color-surface) !important; color: var(--color-text) !important; border-color: var(--color-divider) !important; }
.leaflet-control-zoom a:hover { background: var(--color-surface-2) !important; }

/* === MOBILE === */
@media (max-width: 768px) {
  .header-link:not(:first-child) { display: none; }
  .main-layout { flex-direction: column; }
  .map-area { height: 55vh; }
  .sidebar { width: 100%; height: 45vh; display: none; border-left: none; border-top: 1px solid var(--color-divider); }
  .sidebar.open { display: flex; }
  .sidebar-toggle { display: flex; align-items: center; justify-content: center; }
  .view-toggle { left: 12px; top: 10px; }
}
