/* ============ DESIGN TOKENS ============ */
:root,
[data-theme='light'] {
  --color-bg: #f5f2ea;
  --color-surface: #faf8f2;
  --color-surface-2: #ffffff;
  --color-surface-offset: #eee9dc;
  --color-divider: #e0dac8;
  --color-border: #d6cfb8;

  --color-text: #232019;
  --color-text-muted: #6e6656;
  --color-text-faint: #a89f88;
  --color-text-inverse: #f9f8f4;

  --color-primary: #2f6b4f;
  --color-primary-hover: #24523d;
  --color-primary-highlight: #d9e6dc;

  --color-alert: #c1571f;
  --color-alert-hover: #9c451a;
  --color-alert-highlight: #f3ded0;

  --color-gold: #a97e26;
  --color-gold-highlight: #ecdfbb;

  --color-warn: #8c3b2e;
  --color-warn-hover: #702e24;
  --color-warn-highlight: #ecdcd4;

  --radius-sm: 0.375rem;
  --radius-md: 0.625rem;
  --radius-lg: 1rem;
  --radius-full: 9999px;

  --shadow-sm: 0 1px 2px oklch(0.2 0.02 90 / 0.08);
  --shadow-md: 0 6px 20px oklch(0.2 0.02 90 / 0.12);
  --shadow-lg: 0 16px 40px oklch(0.15 0.02 90 / 0.22);

  --transition-interactive: 180ms cubic-bezier(0.16, 1, 0.3, 1);
  --font-display: 'Cabinet Grotesk', 'General Sans', sans-serif;
  --font-body: 'Satoshi', 'Inter', sans-serif;
}

[data-theme='dark'] {
  --color-bg: #12160f;
  --color-surface: #171c14;
  --color-surface-2: #1c2118;
  --color-surface-offset: #1e241a;
  --color-divider: #2a3123;
  --color-border: #343d2c;

  --color-text: #e8e6da;
  --color-text-muted: #a3a08c;
  --color-text-faint: #6b6957;
  --color-text-inverse: #14170f;

  --color-primary: #5fb890;
  --color-primary-hover: #7cc9a4;
  --color-primary-highlight: #223629;

  --color-alert: #e2814a;
  --color-alert-hover: #ec9a6b;
  --color-alert-highlight: #3a2a1e;

  --color-gold: #d1a94f;
  --color-gold-highlight: #362c17;

  --color-warn: #c17a63;
  --color-warn-hover: #d6957e;
  --color-warn-highlight: #33231d;

  --shadow-sm: 0 1px 2px oklch(0 0 0 / 0.3);
  --shadow-md: 0 6px 20px oklch(0 0 0 / 0.4);
  --shadow-lg: 0 16px 40px oklch(0 0 0 / 0.55);
}

:root { --text-xs: clamp(0.75rem, 0.72rem + 0.15vw, 0.8125rem); }

* , *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; }
body {
  font-family: var(--font-body);
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 15px;
  line-height: 1.5;
  height: 100dvh;
  overflow: hidden;
}
button { cursor: pointer; background: none; border: none; font: inherit; color: inherit; }
a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }
ul { list-style: none; }
:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 2px; border-radius: var(--radius-sm); }

/* ============ APP SHELL ============ */
.app { height: 100dvh; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  z-index: 40;
  flex: 0 0 auto;
}
.topbar__brand { display: flex; align-items: center; gap: 0.65rem; min-width: 0; flex: 1 1 auto; }
.brand-mark { color: var(--color-primary); flex: 0 0 auto; }
.brand-text { min-width: 0; flex: 1 1 auto; overflow: hidden; }
.brand-text h1 {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--font-display);
  font-size: clamp(1rem, 0.6rem + 1vw, 1.25rem);
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.15;
}
.brand-text p {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar__actions { display: flex; align-items: center; gap: 0.5rem; flex: 0 0 auto; }
@media (max-width: 420px) { .brand-text p { display: none; } }

.pill-link {
  display: none;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.8rem;
  border-radius: var(--radius-full);
  border: 1px solid var(--color-border);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  white-space: nowrap;
}
.pill-link:hover { border-color: var(--color-primary); color: var(--color-primary); text-decoration: none; }
.pill-link i { width: 14px; height: 14px; }
@media (min-width: 860px) { .pill-link { display: flex; } }

.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  color: var(--color-text);
  transition: border-color var(--transition-interactive), color var(--transition-interactive), background var(--transition-interactive);
}
.icon-btn:hover { border-color: var(--color-primary); color: var(--color-primary); background: var(--color-primary-highlight); }
.icon-btn i { width: 18px; height: 18px; }

/* ============ LAYOUT ============ */
.layout { flex: 1 1 auto; display: flex; min-height: 0; position: relative; }

.sidebar {
  width: 340px;
  flex: 0 0 auto;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  overflow: hidden;
  transition: width var(--transition-interactive), transform var(--transition-interactive);
}
.sidebar__scroll {
  width: 340px;
  height: 100%;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}
.app.sidebar-collapsed .sidebar { width: 0; }

@media (max-width: 760px) {
  .sidebar { position: absolute; inset: 0 auto 0 0; width: 88vw; max-width: 360px; z-index: 30; box-shadow: var(--shadow-lg); transform: translateX(-100%); }
  .app:not(.sidebar-collapsed) .sidebar { transform: translateX(0); }
  .app.sidebar-collapsed .sidebar { width: 88vw; max-width: 360px; }
}

/* ============ CARDS ============ */
.card {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 1rem;
  box-shadow: var(--shadow-sm);
}
.card--alert {
  border-color: color-mix(in oklab, var(--color-alert) 40%, var(--color-border));
  background: linear-gradient(160deg, var(--color-alert-highlight), var(--color-surface-2) 60%);
}
.card--warn {
  border-color: color-mix(in oklab, var(--color-warn) 40%, var(--color-border));
  background: linear-gradient(160deg, var(--color-warn-highlight), var(--color-surface-2) 60%);
}
.card__head { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.6rem; }
.card__head i { width: 17px; height: 17px; color: var(--color-alert); flex: 0 0 auto; }
.card:not(.card--alert) .card__head i { color: var(--color-primary); }
.card--warn .card__head i { color: var(--color-warn); }
.card--warn .toggle-row__sub { color: var(--color-warn); }

.btn-link {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--color-warn);
  margin-top: 0.6rem;
  padding: 0.1rem 0;
}
.btn-link:hover { color: var(--color-warn-hover); text-decoration: underline; }
.btn-link i { width: 14px; height: 14px; }
.card__head h2 {
  font-family: var(--font-display);
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: -0.005em;
}
.card__body { font-size: 0.85rem; color: var(--color-text-muted); margin-bottom: 0.85rem; }
.card__footnote { font-size: var(--text-xs); color: var(--color-text-faint); margin-top: 0.75rem; line-height: 1.5; }
.card__footnote a { color: var(--color-text-muted); }

/* toggle switch */
.toggle-row { display: flex; align-items: flex-start; gap: 0.75rem; padding: 0.7rem; border-radius: var(--radius-md); background: var(--color-surface-offset); }
.toggle-row__label { display: flex; flex-direction: column; gap: 0.2rem; font-size: 0.83rem; font-weight: 500; }
.toggle-row__sub { font-size: var(--text-xs); color: var(--color-alert); font-weight: 600; }

.switch { position: relative; flex: 0 0 auto; }
.switch input { position: absolute; opacity: 0; width: 44px; height: 24px; margin: 0; cursor: pointer; }
.switch__track {
  display: block;
  width: 44px;
  height: 24px;
  border-radius: var(--radius-full);
  background: var(--color-border);
  position: relative;
  transition: background var(--transition-interactive);
}
.switch__thumb {
  position: absolute;
  top: 3px; left: 3px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--color-surface-2);
  box-shadow: var(--shadow-sm);
  transition: transform var(--transition-interactive);
}
.switch input:checked ~ .switch__track { background: var(--color-alert); }
.switch input:checked ~ .switch__track .switch__thumb { transform: translateX(20px); }
.switch input:focus-visible ~ .switch__track { outline: 2px solid var(--color-alert); outline-offset: 2px; }

/* stat grid */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; margin-top: 0.75rem; }
.stat-grid.is-hidden { display: none; }
.stat-box { padding: 0.6rem 0.7rem; border-radius: var(--radius-md); background: var(--color-surface-offset); border: 1px solid var(--color-border); }
.stat-box__value { font-family: var(--font-display); font-size: 1.15rem; font-weight: 700; color: var(--color-alert); line-height: 1.1; }
.stat-box__label { font-size: var(--text-xs); color: var(--color-text-muted); margin-top: 0.15rem; }

/* layer rows */
.layer-row { display: flex; align-items: center; gap: 0.6rem; padding: 0.45rem 0.1rem; cursor: pointer; border-radius: var(--radius-sm); }
.layer-row:hover { background: var(--color-surface-offset); }
.layer-row input { width: 15px; height: 15px; accent-color: var(--color-primary); flex: 0 0 auto; }
.layer-row__text { font-size: 0.83rem; }
.layer-row__text em { color: var(--color-text-faint); font-style: italic; }
.layer-divider { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: 0.05em; color: var(--color-text-faint); margin: 0.6rem 0 0.15rem; padding-top: 0.5rem; border-top: 1px solid var(--color-divider); }

.swatch { width: 15px; height: 15px; border-radius: 3px; flex: 0 0 auto; background: color-mix(in oklab, var(--swatch-color) 55%, transparent); border: 1.5px solid var(--swatch-color); }
.swatch--line { background: none; border: none; border-top: 3px solid var(--swatch-color); height: 0; margin-top: 7px; }
.swatch--dash { background: none; border: none; border-top: 2.5px dashed var(--swatch-color); height: 0; margin-top: 7px; }
.swatch--hatch { background-image: repeating-linear-gradient(45deg, var(--swatch-color) 0 2px, transparent 2px 5px); border: 1.5px solid var(--swatch-color); }

/* legend */
.legend-list { display: flex; flex-direction: column; gap: 0.45rem; }
.legend-list li { display: flex; align-items: center; gap: 0.55rem; font-size: 0.8rem; color: var(--color-text-muted); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c); flex: 0 0 auto; }
.dot--alert { box-shadow: 0 0 0 3px color-mix(in oklab, var(--c) 30%, transparent); }

/* sources */
.src-details summary { cursor: pointer; font-size: 0.83rem; font-weight: 600; margin-bottom: 0.5rem; }
.src-details p { font-size: 0.8rem; color: var(--color-text-muted); margin-bottom: 0.5rem; line-height: 1.55; }
.src-list { display: flex; flex-direction: column; gap: 0.35rem; margin-top: 0.6rem; }
.src-list li { font-size: 0.78rem; }
.src-list a { color: var(--color-text-muted); }
.src-list a:hover { color: var(--color-primary); }

/* ============ MAP ============ */
.map-wrap { flex: 1 1 auto; position: relative; min-width: 0; }
#map { position: absolute; inset: 0; }
[data-theme='dark'] #map .maplibregl-canvas { filter: brightness(0.78) contrast(1.05) saturate(0.75) invert(0.92) hue-rotate(180deg); }

.map-badge {
  position: absolute;
  top: 1rem; left: 1rem;
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.45rem 0.85rem;
  background: color-mix(in oklab, var(--color-surface) 88%, transparent);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(6px);
  z-index: 5;
}
.badge-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--color-primary); box-shadow: 0 0 0 3px color-mix(in oklab, var(--color-primary) 25%, transparent); }

.map-attribution {
  position: absolute; bottom: 0.5rem; right: 0.6rem;
  font-size: 10px; color: var(--color-text-faint);
  background: color-mix(in oklab, var(--color-surface) 75%, transparent);
  padding: 2px 6px; border-radius: 4px; z-index: 5;
}
.map-attribution a { color: var(--color-text-faint); }

.feature-panel {
  position: absolute;
  bottom: 1rem; left: 1rem;
  width: min(360px, calc(100% - 2rem));
  max-height: min(50vh, 420px);
  overflow-y: auto;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 1rem 1.1rem;
  z-index: 10;
}
.feature-panel__close { position: absolute; top: 0.6rem; right: 0.6rem; width: 26px; height: 26px; display: grid; place-items: center; border-radius: var(--radius-sm); color: var(--color-text-faint); }
.feature-panel__close:hover { color: var(--color-text); background: var(--color-surface-offset); }
.feature-panel__close i { width: 15px; height: 15px; }
.fp-title { font-family: var(--font-display); font-weight: 700; font-size: 0.95rem; margin-bottom: 0.35rem; padding-right: 1.5rem; }
.fp-kicker { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: 0.05em; color: var(--color-alert); font-weight: 600; margin-bottom: 0.3rem; }
.fp-body { font-size: 0.82rem; color: var(--color-text-muted); line-height: 1.6; }
.fp-row { display: flex; justify-content: space-between; gap: 0.5rem; font-size: 0.8rem; padding: 0.3rem 0; border-bottom: 1px solid var(--color-divider); }
.fp-row:last-child { border-bottom: none; }
.fp-row span:first-child { color: var(--color-text-faint); }
.fp-row span:last-child { font-weight: 600; text-align: right; }

/* maplibre popup override */
.maplibregl-popup-content { background: var(--color-surface-2); color: var(--color-text); border-radius: var(--radius-md); box-shadow: var(--shadow-md); font-family: var(--font-body); }
.maplibregl-popup-tip { border-top-color: var(--color-surface-2) !important; border-bottom-color: var(--color-surface-2) !important; }

/* scrollbar */
.sidebar__scroll::-webkit-scrollbar, .feature-panel::-webkit-scrollbar { width: 6px; }
.sidebar__scroll::-webkit-scrollbar-thumb, .feature-panel::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 4px; }
