/* ========================================================================
   Climograph — Global Monthly Temperature Atlas
   Design language: a quiet, scientific dashboard. Restrained ink + one
   accent for highlights. Mono numerals for all data values.
   ======================================================================== */

:root {
  /* Type */
  --font-sans: "Inter", "Helvetica Neue", system-ui, -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", ui-monospace, Menlo, monospace;

  /* Spacing scale */
  --s-1: 4px; --s-2: 8px; --s-3: 12px; --s-4: 16px;
  --s-5: 20px; --s-6: 24px; --s-7: 32px; --s-8: 48px;

  /* Radii */
  --r-sm: 6px; --r-md: 10px; --r-lg: 14px; --r-pill: 999px;

  /* Motion */
  --ease: cubic-bezier(.2,.8,.2,1);
  --ease-out: cubic-bezier(.16,1,.3,1);
  --ease-spring: cubic-bezier(.34,1.56,.64,1);
  --t-fast: 120ms; --t-base: 220ms; --t-slow: 320ms;

  /* Layout */
  --rail-w: 320px;
  --detail-w: 360px;
  --header-h: 56px;
}

[data-theme="dark"] {
  --bg: #0b1014;
  --bg-elev: #11181f;
  --bg-elev-2: #161e26;
  --line: #1f2932;
  --line-2: #2a3640;
  --ink: #e8eef4;
  --ink-2: #aebac4;
  --ink-3: #6b7884;
  --accent: #6cb6ff;
  --accent-ink: #0b1014;
  --warn: #ffb454;
  --danger: #ff6b6b;
  --map-shade: 0;
}

[data-theme="light"] {
  --bg: #f6f7f8;
  --bg-elev: #ffffff;
  --bg-elev-2: #f1f3f5;
  --line: #e3e6ea;
  --line-2: #ced3d9;
  --ink: #0d141a;
  --ink-2: #4a5663;
  --ink-3: #7a8590;
  --accent: #1f6feb;
  --accent-ink: #ffffff;
  --warn: #b35900;
  --danger: #c92a2a;
  --map-shade: 1;
}

/* Web fonts */
@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500;600&display=swap");

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--ink);
  font-feature-settings: "ss01", "cv11", "tnum";
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
  font-size: 13.5px;
  line-height: 1.45;
}

button { font-family: inherit; font-size: inherit; color: inherit; cursor: pointer; }
input, select { font-family: inherit; font-size: inherit; color: inherit; }

/* ----------- Header -------------------------------------------------- */
.app-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--s-5);
  background: var(--bg-elev);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 1000;
}
.brand {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  text-decoration: none;
  color: var(--ink);
}
.brand-mark { color: var(--accent); flex: none; }
.brand-name {
  font-weight: 700;
  letter-spacing: -0.01em;
  font-size: 15px;
}
.brand-tag {
  font-size: 11.5px;
  color: var(--ink-3);
  border-left: 1px solid var(--line-2);
  padding-left: var(--s-3);
  margin-left: var(--s-1);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.header-nav { display: flex; gap: var(--s-2); align-items: center; }

/* ----------- Layout grid -------------------------------------------- */
.app {
  display: grid;
  grid-template-columns: var(--rail-w) 1fr var(--detail-w);
  height: calc(100vh - var(--header-h));
  min-height: 0;
}

.rail, .detail {
  background: var(--bg-elev);
  overflow-y: auto;
  padding: var(--s-5);
  display: flex;
  flex-direction: column;
  gap: var(--s-4);
  scrollbar-width: thin;
  scrollbar-color: var(--line-2) transparent;
}
.rail { border-right: 1px solid var(--line); }
.detail { border-left: 1px solid var(--line); }
.rail::-webkit-scrollbar, .detail::-webkit-scrollbar { width: 8px; }
.rail::-webkit-scrollbar-thumb, .detail::-webkit-scrollbar-thumb { background: var(--line-2); border-radius: 4px; }

.map-wrap { position: relative; min-width: 0; }
#map { position: absolute; inset: 0; background: var(--bg); }

/* Leaflet baseline tweaks */
.leaflet-container { font-family: var(--font-sans); background: var(--bg) !important; outline: none; }
.leaflet-control-attribution { background: rgba(11,16,20,0.65) !important; color: var(--ink-2) !important; font-size: 10.5px !important; }
.leaflet-control-attribution a { color: var(--ink-2) !important; }
[data-theme="light"] .leaflet-control-attribution { background: rgba(255,255,255,0.85) !important; color: var(--ink-2) !important; }
.leaflet-control-zoom a {
  background: var(--bg-elev) !important;
  color: var(--ink) !important;
  border: 1px solid var(--line) !important;
}
.leaflet-control-zoom a:hover { background: var(--bg-elev-2) !important; }

/* ----------- Buttons & cards ---------------------------------------- */
.rail-card {
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: var(--s-4);
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
}
.card-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s-3);
}
.card-title {
  margin: 0;
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-2);
}
.card-meta {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-3);
}

.primary-btn, .ghost-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  height: 32px;
  padding: 0 var(--s-3);
  border-radius: var(--r-sm);
  border: 1px solid var(--line-2);
  background: var(--bg-elev);
  color: var(--ink);
  font-weight: 500;
  font-size: 12.5px;
  transition: background var(--t-base) var(--ease-out),
              border-color var(--t-base) var(--ease-out),
              color var(--t-fast) var(--ease-out),
              box-shadow var(--t-base) var(--ease-out),
              transform var(--t-fast) var(--ease-spring),
              filter var(--t-fast) var(--ease-out);
  text-decoration: none;
  will-change: transform;
}
.primary-btn {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
  font-weight: 600;
  box-shadow: 0 4px 14px -4px color-mix(in srgb, var(--accent) 55%, transparent);
}
.primary-btn:hover { filter: brightness(1.08); transform: translateY(-1px); box-shadow: 0 8px 22px -6px color-mix(in srgb, var(--accent) 70%, transparent); }
.ghost-btn:hover { background: var(--bg-elev-2); border-color: var(--line-2); transform: translateY(-1px); }
.primary-btn:active, .ghost-btn:active { transform: translateY(0) scale(0.97); filter: brightness(0.96); }
.ghost-btn:focus-visible, .primary-btn:focus-visible, .seg-btn:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Segmented controls */
.seg {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 2px;
  padding: 2px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
}
.seg-btn {
  height: 28px;
  border: 0;
  background: transparent;
  border-radius: 4px;
  color: var(--ink-2);
  font-size: 11.5px;
  font-weight: 500;
  letter-spacing: 0.02em;
  transition: background var(--t-base) var(--ease-out),
              color var(--t-fast) var(--ease-out),
              box-shadow var(--t-base) var(--ease-out),
              transform var(--t-fast) var(--ease-spring);
  white-space: nowrap;
  position: relative;
}
.seg-btn.is-active {
  background: var(--bg-elev-2);
  color: var(--ink);
  box-shadow: 0 0 0 1px var(--line-2), 0 4px 14px -6px rgba(0,0,0,0.5);
  transform: scale(1);
}
.seg-btn:hover:not(.is-active) { color: var(--ink); background: rgba(127,127,127,0.06); }
.seg-btn:active { transform: scale(0.96); }

/* Month grid */
.month-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
}
.month-grid button {
  height: 32px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: var(--bg-elev);
  color: var(--ink-2);
  font-weight: 500;
  font-size: 11.5px;
  letter-spacing: 0.02em;
  transition: background var(--t-base) var(--ease-out),
              color var(--t-fast) var(--ease-out),
              border-color var(--t-base) var(--ease-out),
              box-shadow var(--t-base) var(--ease-out),
              transform var(--t-fast) var(--ease-spring);
  position: relative;
}
.month-grid button.is-active {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
  font-weight: 600;
  box-shadow: 0 6px 18px -6px color-mix(in srgb, var(--accent) 60%, transparent),
              0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
  transform: translateY(-1px);
}
.month-grid button:hover:not(.is-active) {
  border-color: var(--line-2);
  color: var(--ink);
  background: var(--bg-elev-2);
  transform: translateY(-1px);
}
.month-grid button:active { transform: translateY(0) scale(0.97); }

.play-row {
  display: flex;
  gap: var(--s-2);
}
.play-row .primary-btn,
.play-row .ghost-btn { flex: 1; justify-content: center; }

/* Custom range */
.custom-range {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--s-2) var(--s-3);
  margin-top: var(--s-1);
}
.range-label {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 11px;
  color: var(--ink-3);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.range-label input {
  height: 30px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  border-radius: var(--r-sm);
  padding: 0 var(--s-2);
  font-family: var(--font-mono);
  font-size: 12.5px;
}
.custom-range .hint { grid-column: 1 / -1; }
.hint { margin: 0; font-size: 11px; color: var(--ink-3); line-height: 1.45; }
.window-summary {
  margin: 0;
  font-size: 11.5px;
  color: var(--ink-3);
  font-family: var(--font-mono);
}

/* Search */
.search-row {
  display: flex;
  gap: var(--s-2);
}
.search-row input {
  flex: 1;
  height: 32px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  border-radius: var(--r-sm);
  padding: 0 var(--s-3);
}
.search-row input::placeholder { color: var(--ink-3); }
.search-results {
  list-style: none;
  margin: 0;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: var(--bg);
  max-height: 200px;
  overflow-y: auto;
}
.search-results li {
  padding: 6px 8px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12.5px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--s-2);
}
.search-results li:hover { background: var(--bg-elev-2); }
.search-results li .badge {
  font-size: 10px;
  text-transform: uppercase;
  color: var(--ink-3);
  letter-spacing: 0.06em;
  font-weight: 600;
}

/* Legend */
.legend-card .card-meta { font-weight: 500; }
.legend-bar {
  height: 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
}
.legend-ticks {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-3);
  margin-top: 4px;
}

.rail-foot { font-size: 11px; color: var(--ink-3); margin: auto 0 0; line-height: 1.5; }

/* ----------- Detail panel ------------------------------------------- */
.detail-head {
  border-bottom: 1px solid var(--line);
  padding-bottom: var(--s-3);
}
.detail-title {
  margin: 0 0 4px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.detail-sub {
  margin: 0;
  color: var(--ink-3);
  font-size: 12px;
}

.detail-stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--s-2);
}
.stat {
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: var(--s-3);
  grid-column: span 2;
}
.stat.small { grid-column: span 1; }
.stat-key {
  font-size: 10.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-3);
}
.stat-val {
  font-family: var(--font-mono);
  font-size: 18px;
  font-weight: 600;
  color: var(--ink);
  margin-top: 4px;
  letter-spacing: -0.01em;
}
.stat.small .stat-val { font-size: 14.5px; }
.stat-foot { font-size: 11px; color: var(--ink-3); margin-top: 2px; }
.mono { font-family: var(--font-mono); }

.detail-section { display: flex; flex-direction: column; gap: var(--s-2); }
.section-h {
  margin: 0;
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-2);
}
.formula {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 12.5px;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  padding: var(--s-3);
  border-radius: var(--r-md);
  color: var(--ink);
  line-height: 1.55;
}
.muted { color: var(--ink-3); font-size: 11.5px; line-height: 1.55; margin: 0; }

.meta-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  overflow: hidden;
}
.meta-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--s-2) var(--s-3);
  font-size: 12px;
  border-bottom: 1px solid var(--line);
}
.meta-list li:last-child { border-bottom: 0; }
.meta-list li span { color: var(--ink-3); }
.meta-list li strong { font-weight: 500; color: var(--ink); }

/* ----------- Hover tooltip ------------------------------------------ */
.hover-tip {
  position: absolute;
  pointer-events: none;
  z-index: 600;
  background: rgba(11,16,20,0.92);
  border: 1px solid var(--line-2);
  color: var(--ink);
  padding: 8px 10px;
  border-radius: var(--r-sm);
  font-size: 12px;
  font-family: var(--font-mono);
  line-height: 1.4;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transform: translate(-50%, calc(-100% - 12px));
  white-space: nowrap;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
}
[data-theme="light"] .hover-tip {
  background: rgba(255,255,255,0.96);
  color: var(--ink);
  border-color: var(--line-2);
}
.hover-tip .tip-temp {
  font-weight: 600;
  color: var(--accent);
  font-size: 14px;
  display: block;
  margin-bottom: 2px;
}
.hover-tip .tip-row { color: var(--ink-2); }

/* ----------- Loader & error ----------------------------------------- */
.loader {
  position: absolute;
  top: var(--s-4);
  left: 50%;
  transform: translate(-50%, 0);
  background: rgba(11,16,20,0.78);
  border: 1px solid color-mix(in srgb, var(--accent) 22%, var(--line-2));
  color: var(--ink);
  padding: 7px 14px 7px 12px;
  border-radius: var(--r-pill);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.01em;
  display: flex;
  gap: var(--s-2);
  align-items: center;
  z-index: 700;
  backdrop-filter: blur(10px) saturate(140%);
  -webkit-backdrop-filter: blur(10px) saturate(140%);
  box-shadow:
    0 6px 22px -8px rgba(0,0,0,0.5),
    0 0 0 4px color-mix(in srgb, var(--accent) 8%, transparent);
  animation: loaderPulse 2.4s ease-in-out infinite, loaderEnter 320ms var(--ease-out);
  transition: opacity var(--t-slow) var(--ease-out),
              transform var(--t-slow) var(--ease-out);
}
.loader[hidden] {
  display: flex !important;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -8px);
}
[data-theme="light"] .loader { background: rgba(255,255,255,0.92); }
@keyframes loaderEnter {
  from { opacity: 0; transform: translate(-50%, -8px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}
@keyframes loaderPulse {
  0%, 100% { box-shadow: 0 6px 22px -8px rgba(0,0,0,0.5), 0 0 0 4px color-mix(in srgb, var(--accent) 6%, transparent); }
  50%      { box-shadow: 0 6px 22px -8px rgba(0,0,0,0.5), 0 0 0 8px color-mix(in srgb, var(--accent) 12%, transparent); }
}
.spin {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  position: relative;
  background: conic-gradient(from 0deg,
    color-mix(in srgb, var(--accent) 0%, transparent) 0deg,
    var(--accent) 300deg,
    color-mix(in srgb, var(--accent) 0%, transparent) 360deg);
  -webkit-mask: radial-gradient(circle, transparent 4px, #000 5px);
          mask: radial-gradient(circle, transparent 4px, #000 5px);
  animation: spin 0.9s linear infinite;
  flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-toast {
  position: absolute;
  top: var(--s-3);
  left: 50%;
  transform: translateX(-50%);
  background: var(--danger);
  color: white;
  padding: 6px 14px;
  border-radius: var(--r-pill);
  font-size: 12px;
  z-index: 700;
}

/* ----------- Splash entrance ---------------------------------------- */
.app-header {
  animation: headerEnter 520ms var(--ease-out) both;
}
@keyframes headerEnter {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.rail {
  animation: panelLeft 560ms var(--ease-out) both;
  animation-delay: 60ms;
}
@keyframes panelLeft {
  from { opacity: 0; transform: translateX(-12px); }
  to   { opacity: 1; transform: translateX(0); }
}
.detail {
  animation: panelRight 560ms var(--ease-out) both;
  animation-delay: 140ms;
}
@keyframes panelRight {
  from { opacity: 0; transform: translateX(12px); }
  to   { opacity: 1; transform: translateX(0); }
}
.map-wrap {
  animation: mapEnter 700ms var(--ease-out) both;
  animation-delay: 100ms;
}
@keyframes mapEnter {
  from { opacity: 0; transform: scale(0.985); }
  to   { opacity: 1; transform: scale(1); }
}
/* Stagger rail-cards in */
.rail .rail-card {
  animation: cardRise 520ms var(--ease-out) both;
}
.rail .rail-card:nth-of-type(1) { animation-delay: 180ms; }
.rail .rail-card:nth-of-type(2) { animation-delay: 240ms; }
.rail .rail-card:nth-of-type(3) { animation-delay: 300ms; }
.rail .rail-card:nth-of-type(4) { animation-delay: 360ms; }
.rail .rail-card:nth-of-type(5) { animation-delay: 420ms; }
@keyframes cardRise {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ----------- Stat value swap animations ----------------------------- */
.stat-val {
  transition: opacity 180ms var(--ease-out),
              transform 240ms var(--ease-spring),
              color 240ms var(--ease-out);
  will-change: opacity, transform;
}
.stat-val.is-changing {
  opacity: 0;
  transform: translateY(-4px);
}
/* Brief accent flash when value updates */
.stat-val.is-flash {
  animation: statFlash 720ms var(--ease-out);
}
@keyframes statFlash {
  0%   { color: var(--accent); }
  100% { color: var(--ink); }
}

/* Card meta and window summary share the swap motion */
.card-meta, .window-summary {
  transition: opacity 180ms var(--ease-out), transform 220ms var(--ease-out);
}
.card-meta.is-changing, .window-summary.is-changing {
  opacity: 0; transform: translateY(-3px);
}

/* ----------- Hover/tip enter --------------------------------------- */
.hover-tip {
  transition: opacity 120ms var(--ease-out);
}
.hover-tip[hidden] {
  display: block !important;
  opacity: 0;
  pointer-events: none;
}

/* Reduce motion preference */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

/* ----------- Selected location pin marker --------------------------- */
.pin-marker {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid white;
  box-shadow: 0 0 0 2px rgba(108,182,255,0.35), 0 4px 10px rgba(0,0,0,0.4);
  animation: pinDrop 380ms var(--ease-spring);
}
@keyframes pinDrop {
  0%   { transform: translateY(-12px) scale(0.4); opacity: 0; }
  60%  { transform: translateY(2px)   scale(1.15); opacity: 1; }
  100% { transform: translateY(0)     scale(1);    opacity: 1; }
}

/* Soft pulse ring around the pin */
.pin-marker::before {
  content: "";
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  opacity: 0.6;
  animation: pinPulse 1.8s var(--ease-out) infinite;
  pointer-events: none;
}
@keyframes pinPulse {
  0%   { transform: scale(1);   opacity: 0.55; }
  100% { transform: scale(2.2); opacity: 0; }
}

/* ----------- Mobile-only / desktop-only utility classes -------------- */
.mobile-only { display: none; }
.icon-only { width: 32px; padding: 0; justify-content: center; }

/* Mobile floating temperature chip (bottom of map) */
.mobile-stat-chip {
  display: none;
  position: absolute;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
  transform: translateX(-50%);
  background: rgba(11,16,20,0.86);
  color: var(--ink);
  border: 1px solid color-mix(in srgb, var(--accent) 28%, var(--line-2));
  border-radius: var(--r-pill);
  padding: 8px 18px;
  z-index: 600;
  backdrop-filter: blur(12px) saturate(140%);
  -webkit-backdrop-filter: blur(12px) saturate(140%);
  box-shadow:
    0 8px 28px -10px rgba(0,0,0,0.55),
    0 0 0 4px color-mix(in srgb, var(--accent) 8%, transparent);
  font-family: var(--font-sans);
  text-align: center;
  cursor: pointer;
  align-items: center;
  flex-direction: column;
  gap: 0;
  line-height: 1.15;
  animation: chipEnter 320ms var(--ease-out);
  max-width: calc(100vw - 32px);
}
[data-theme="light"] .mobile-stat-chip {
  background: rgba(255,255,255,0.94);
  color: var(--ink);
}
.chip-temp {
  font-family: var(--font-mono);
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--accent);
}
.chip-meta {
  font-size: 10.5px;
  color: var(--ink-3);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 240px;
}
@keyframes chipEnter {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

/* Sheet backdrop (mobile drawer scrim) */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 1500;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--t-base) var(--ease-out);
  -webkit-tap-highlight-color: transparent;
}
.sheet-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}
.sheet-close {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 5;
  background: var(--bg-elev-2);
  border-color: var(--line);
}

/* ----------- Responsive --------------------------------------------- */
@media (max-width: 1280px) {
  :root { --rail-w: 260px; --detail-w: 300px; }
  body { font-size: 13px; }
}

/* Tablet: stack detail below map, keep left rail */
@media (max-width: 1080px) and (min-width: 721px) {
  .app {
    grid-template-columns: 280px 1fr;
    grid-template-rows: 1fr auto;
  }
  .rail { grid-row: 1 / 3; max-height: none; }
  .map-wrap { grid-column: 2; grid-row: 1; min-height: 60vh; }
  .detail {
    grid-column: 2;
    grid-row: 2;
    border-left: 0;
    border-top: 1px solid var(--line);
    max-height: 40vh;
  }
}

/* Phone layout: map is hero, controls + details slide up as drawers */
@media (max-width: 720px) {
  :root {
    --header-h: 52px;
  }
  .hide-mobile { display: none !important; }
  .mobile-only { display: inline-flex; }
  .mobile-stat-chip { display: flex; }

  body { font-size: 14px; overflow: hidden; }

  .app-header {
    padding: 0 14px;
    z-index: 1100;
  }
  .brand-tag { display: none; }
  .brand-name { font-size: 16px; }
  .header-nav { gap: 6px; }

  .app {
    display: block;
    height: calc(100dvh - var(--header-h));
    height: calc(100vh - var(--header-h));
    position: relative;
    overflow: hidden;
  }

  /* Map fills the entire viewport */
  .map-wrap {
    position: absolute;
    inset: 0;
    height: 100%;
    min-height: 0;
  }
  #map { position: absolute; inset: 0; }

  /* Left rail becomes a slide-up sheet from the bottom */
  .rail {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: 100%;
    max-width: 100%;
    max-height: 82dvh;
    max-height: 82vh;
    border-right: 0;
    border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0;
    padding: 56px 16px calc(env(safe-area-inset-bottom, 0px) + 22px);
    transform: translateY(100%);
    transition: transform 320ms var(--ease-out);
    z-index: 1600;
    box-shadow: 0 -16px 48px -16px rgba(0,0,0,0.55);
    animation: none;
    overscroll-behavior: contain;
    gap: 14px;
  }
  .rail.is-open { transform: translateY(0); }
  /* Sheet-close button placed inline at top of rail */
  .sheet-close { top: 16px; right: 16px; width: 30px; height: 30px; }
  /* Drag handle at top */
  .rail::before {
    content: "";
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 38px;
    height: 4px;
    border-radius: 2px;
    background: var(--line-2);
  }

  /* Right detail panel becomes a slide-up sheet from the bottom */
  .detail {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: 100%;
    max-height: 88dvh;
    max-height: 88vh;
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0;
    padding: 28px 16px calc(env(safe-area-inset-bottom, 0px) + 22px);
    transform: translateY(100%);
    transition: transform 320ms var(--ease-out);
    z-index: 1600;
    box-shadow: 0 -16px 48px -16px rgba(0,0,0,0.55);
    animation: none;
    overscroll-behavior: contain;
    gap: 14px;
  }
  .detail.is-open { transform: translateY(0); }
  .detail::before {
    content: "";
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 38px;
    height: 4px;
    border-radius: 2px;
    background: var(--line-2);
  }

  /* Reset rail-card stagger animation on mobile so they appear instantly when sheet opens */
  .rail .rail-card { animation: none; }

  /* Compact controls inside drawer */
  .month-grid { grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .month-grid button { height: 38px; font-size: 13px; }
  .seg-btn { height: 32px; font-size: 12.5px; }
  .primary-btn, .ghost-btn { height: 36px; font-size: 13px; padding: 0 12px; }
  .play-row .primary-btn, .play-row .ghost-btn { height: 38px; }
  .rail-card { padding: 14px; gap: 10px; }
  .rail-foot { display: none; }

  /* Detail stat grid: compact 2-col */
  .detail-stat-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat { padding: 10px; }
  .stat:not(.small) { grid-column: span 2; }
  .stat.small { grid-column: span 1; }
  .stat-val { font-size: 17px; }
  .stat.small .stat-val { font-size: 14px; }
  .detail-title { font-size: 17px; }

  /* Hide hover tooltip on touch devices (tap-to-pin handles it) */
  .hover-tip { display: none !important; }

  /* Loader sits below the header to avoid stat chip overlap */
  .loader { top: 12px; font-size: 11.5px; }

  /* Leaflet zoom controls smaller and out of the way */
  .leaflet-control-zoom { margin: 12px !important; }
  .leaflet-control-zoom a { width: 32px !important; height: 32px !important; line-height: 30px !important; font-size: 18px !important; }
  .leaflet-control-attribution { font-size: 9.5px !important; padding: 2px 6px !important; }
}

/* Very small phones */
@media (max-width: 380px) {
  .month-grid { grid-template-columns: repeat(3, 1fr); }
  .seg { grid-auto-flow: row; grid-auto-columns: auto; grid-template-columns: repeat(3, 1fr); }
  .brand-name { font-size: 15px; }
}
