/* Davids Engineering brand: green #006c33, blue #00569f. */
:root {
  --accent: #006c33;
  --accent-weak: #e6f0ea;
  --brand-blue: #00569f;
  --brand-blue-ink: #0b3f73;
  --brand-blue-weak: #e4eef7;
  --text: #16211c;
  --text-muted: #5d6a64;
  --border: #dde3df;
  --border-strong: #c9d1cc;
  --canvas: #eef1ef;
  --surface: #fff;
  --surface-hover: #f5f7f6;
  --warn: #c98a00;
  --warn-ink: #6b4a00;
  --warn-weak: #fff7e6;
  --warn-border: #f1d699;
  --radius: 10px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 20, 0.06), 0 1px 3px rgba(16, 24, 20, 0.04);
  --shadow-md: 0 4px 12px rgba(16, 24, 20, 0.12), 0 1px 3px rgba(16, 24, 20, 0.08);
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  font-family: "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
  color: var(--text);
  background: var(--canvas);
}

[hidden] { display: none !important; }

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

/* ---- Header ------------------------------------------------------------ */
.app-page { display: flex; flex-direction: column; }

.app-header {
  position: relative;
  z-index: 1100;
  display: flex;
  flex: 0 0 56px;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 3px 1.1rem 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

/* Brand stripe along the top edge */
.app-header::before {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent), var(--brand-blue));
}

.brand { display: flex; flex: 1 1 auto; align-items: center; gap: 0.85rem; min-width: 0; }

.brand-logo { display: block; flex: 0 0 auto; height: 56px; width: auto; }

.brand-divider { flex: 0 0 1px; height: 24px; background: var(--border-strong); }

/* Wraps on a narrow window rather than being cut off */
.app-header h1 { flex: 0 1 auto; margin: 0; font-size: 1rem; font-weight: 600; line-height: 1.2; }

.app-nav { display: flex; flex: 0 0 auto; align-items: center; gap: 0.35rem; font-size: 0.85rem; }

.app-nav form { margin: 0; }

.nav-button {
  padding: 0.35rem 0.7rem;
  color: var(--text-muted);
  font: inherit;
  text-decoration: none;
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.nav-button:hover { color: var(--text); background: var(--canvas); }

/* ---- Layout: left controls, map, collapsible right panel --------------- */
.app-main { flex: 1; display: flex; min-height: 0; }

.panel {
  overflow-y: auto;
  padding: 0.85rem;
  font-size: 0.875rem;
  background: var(--canvas);
  scrollbar-width: thin;
}

.panel-left { flex: 0 0 282px; border-right: 1px solid var(--border); }

.panel-right { flex: 0 0 400px; border-left: 1px solid var(--border); }

.is-right-collapsed .panel-right { display: none; }

.map-wrap { position: relative; flex: 1; min-width: 0; }

#map { position: absolute; inset: 0; background: #e6e9e7; }

/* ---- Left panel controls ----------------------------------------------- */
.control-group {
  margin-bottom: 0.75rem;
  padding: 0.75rem 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.control-group h2 {
  margin: 0 0 0.45rem;
  color: var(--text-muted);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}

.choice {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin: 0 -0.45rem;
  padding: 0.35rem 0.45rem;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.12s;
}

.choice:hover { background: var(--surface-hover); }

.choice input { margin: 0; accent-color: var(--brand-blue); cursor: inherit; }

.choice:has(input[type="radio"]:checked) {
  color: var(--brand-blue-ink);
  font-weight: 600;
  background: var(--brand-blue-weak);
}

.choice:has(input:disabled) { color: var(--text-muted); cursor: default; }

.choice:has(input:disabled):hover { background: none; }

.year-value {
  color: var(--brand-blue);
  font-size: 2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.year-row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }

.play-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  color: var(--brand-blue);
  background: var(--brand-blue-weak);
  border: 1px solid transparent;
  border-radius: 50%;
  cursor: pointer;
}

.play-button:hover { border-color: var(--brand-blue); }

.play-button:focus-visible { outline: 2px solid var(--brand-blue); outline-offset: 2px; }

.play-button[aria-pressed="true"] { color: #fff; background: var(--brand-blue); }

.play-button svg { width: 14px; height: 14px; fill: currentColor; }

.play-button .icon-pause, .play-button[aria-pressed="true"] .icon-play { display: none; }

.play-button[aria-pressed="true"] .icon-pause { display: block; }

.year-slider { position: relative; padding-bottom: 1.3rem; }

/* A fixed thumb size, so the decade ticks in panels.js line up with it */
.year-slider input, .opacity-control input {
  width: 100%;
  height: 16px;
  margin: 0.45rem 0 0;
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}

.year-slider input::-webkit-slider-runnable-track, .opacity-control input::-webkit-slider-runnable-track {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
}

.year-slider input::-moz-range-track, .opacity-control input::-moz-range-track {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
}

.year-slider input::-webkit-slider-thumb, .opacity-control input::-webkit-slider-thumb {
  width: 16px;
  height: 16px;
  margin-top: -6px;
  background: var(--brand-blue);
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--brand-blue), var(--shadow-sm);
  -webkit-appearance: none;
}

.year-slider input::-moz-range-thumb, .opacity-control input::-moz-range-thumb {
  box-sizing: border-box;
  width: 16px;
  height: 16px;
  background: var(--brand-blue);
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--brand-blue), var(--shadow-sm);
}

.year-slider input:focus-visible, .opacity-control input:focus-visible {
  outline: 2px solid var(--brand-blue-weak);
  outline-offset: 2px;
}

.year-ticks { position: absolute; top: 1.6rem; left: 0; right: 0; height: 1rem; }

.tick {
  position: absolute;
  padding-top: 4px;
  color: var(--text-muted);
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
  transform: translateX(-50%);
}

.tick::before {
  content: "";
  position: absolute;
  top: -2px;
  left: 50%;
  height: 5px;
  border-left: 1px solid var(--border-strong);
}

.raster-options { margin-top: 0.6rem; padding-top: 0.6rem; border-top: 1px solid var(--border); }

.opacity-control { display: block; }

.opacity-head { display: flex; justify-content: space-between; color: var(--text-muted); font-size: 0.8rem; }

#opacity-value { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

/* Long categorical legends, such as crop groups, scroll instead of pushing the controls down */
.legend { margin-top: 0.65rem; max-height: 16rem; overflow-y: auto; }

.legend-row { display: flex; align-items: center; gap: 0.5rem; padding: 0.1rem 0; font-size: 0.82rem; }

.swatch { width: 14px; height: 14px; border: 1px solid rgba(0, 0, 0, 0.15); border-radius: 3px; }

.gradient { height: 10px; border: 1px solid rgba(0, 0, 0, 0.12); border-radius: 5px; }

.gradient-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 0.2rem;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-variant-numeric: tabular-nums;
}

/* ---- Map overlays ------------------------------------------------------ */
.map-badge {
  position: absolute;
  top: 12px;
  left: 50%;
  z-index: 1000;
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  padding: 0.35rem 0.9rem;
  color: var(--text-muted);
  font-size: 0.8rem;
  white-space: nowrap;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 999px;
  box-shadow: var(--shadow-md);
  transform: translateX(-50%);
  pointer-events: none;
  backdrop-filter: blur(4px);
}

.map-badge strong { color: var(--text); font-size: 0.95rem; font-variant-numeric: tabular-nums; }

.panel-toggle {
  position: absolute;
  top: 50%;
  right: 0;
  z-index: 1000;
  width: 22px;
  height: 52px;
  margin-top: -26px;
  padding: 0;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-right: none;
  border-radius: 8px 0 0 8px;
  box-shadow: var(--shadow-md);
  cursor: pointer;
}

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

.panel-toggle::before { content: "\203A"; font-size: 1.2rem; line-height: 1; }

.is-right-collapsed .panel-toggle::before { content: "\2039"; }

.leaflet-container { font-family: inherit; }

.leaflet-bar { border: none !important; border-radius: 8px !important; box-shadow: var(--shadow-md) !important; overflow: hidden; }

.leaflet-bar a { color: var(--text) !important; border-bottom-color: var(--border) !important; }

.leaflet-tooltip {
  padding: 4px 9px;
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 600;
  border: none;
  border-radius: 6px;
  box-shadow: var(--shadow-md);
}

/* ---- Right panel ------------------------------------------------------- */
.feature-header { position: relative; padding: 0.15rem 0.15rem 0.85rem; }

.feature-kicker-row { display: flex; align-items: center; justify-content: space-between; min-height: 24px; }

.feature-kicker {
  color: var(--accent);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.chip-button {
  padding: 0.2rem 0.65rem;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  cursor: pointer;
}

.chip-button::before { content: "\00D7"; margin-right: 0.3rem; font-weight: 700; }

.chip-button:hover { color: var(--text); border-color: var(--text-muted); }

.feature-label { margin: 0.15rem 0 0.45rem; font-size: 1.4rem; font-weight: 700; line-height: 1.2; }

.year-pill {
  display: inline-block;
  padding: 0.15rem 0.65rem;
  color: var(--brand-blue-ink);
  font-size: 0.78rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  background: var(--brand-blue-weak);
  border-radius: 999px;
}

.year-pill:empty { display: none; }

.panel-message {
  margin: 0 0 0.75rem;
  padding: 1.25rem;
  color: var(--text-muted);
  text-align: center;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
}

/* A year with a note: a chip beside the year pill opens the note over the
   panel, so the cards and graphs never move as the slider crosses it. */
.feature-year-row { display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem; }

.note-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.12rem 0.55rem 0.12rem 0.2rem;
  color: var(--warn-ink);
  font: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--warn-weak);
  border: 1px solid var(--warn-border);
  border-radius: 999px;
  cursor: pointer;
}

.note-chip:hover, .note-chip[aria-expanded="true"] { border-color: var(--warn); }

.note-chip::after { content: "\25BE"; font-size: 0.7rem; }

.note-chip::before, .note-popover::before, .badge-flag {
  content: "!";
  display: grid;
  flex: 0 0 16px;
  place-items: center;
  width: 16px;
  height: 16px;
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1;
  background: var(--warn);
  border-radius: 50%;
}

.note-popover {
  position: absolute;
  top: calc(100% - 0.5rem);
  right: 0;
  left: 0;
  z-index: 20;
  display: flex;
  gap: 0.6rem;
  padding: 0.65rem 0.75rem;
  color: var(--warn-ink);
  font-size: 0.82rem;
  line-height: 1.4;
  background: var(--warn-weak);
  border: 1px solid var(--warn-border);
  border-radius: 8px;
  box-shadow: var(--shadow-md);
}

.note-popover::before { flex-basis: 18px; width: 18px; height: 18px; }

.map-badge.is-flagged strong { color: var(--warn-ink); }

.badge-flag { align-self: center; }

.cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.card {
  padding: 0.6rem 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-sm);
}

/* The first card is the headline value, usually the graphed stat */
.card:first-child { grid-column: 1 / -1; padding: 0.8rem 0.95rem; border-left: 4px solid var(--accent); }

.cards.is-flagged .card:first-child { border-left-color: var(--warn); }

.card-label { margin-bottom: 0.15rem; color: var(--text-muted); font-size: 0.72rem; }

.card-value { font-size: 0.98rem; font-weight: 600; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

.card:first-child .card-value { font-size: 1.75rem; font-weight: 700; line-height: 1.15; }

.graph-card {
  margin-bottom: 0.75rem;
  padding: 0.75rem 0.5rem 0.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.graph-title { margin: 0 0.4rem 0.2rem; font-size: 0.9rem; font-weight: 600; }

/* Only shown on narrow screens */
.panel-toggle-left, .drawer-scrim { display: none; }

/* ---- Narrow screens: full-screen map, panels as drawers over it -------- */
/* The breakpoint matches SMALL_SCREEN_QUERY in panels.js */
@media (max-width: 800px) {
  :root {
    --drawer-left-w: min(85vw, 300px);
    --drawer-right-w: min(88vw, 400px);
    --drawer-ease: 0.22s ease;
  }

  /* dvh keeps the bottom clear of the mobile browser bars, where supported */
  .app-page { height: 100dvh; }

  .app-header { flex-basis: 48px; }

  .app-header h1 { font-size: 0.9rem; }

  .brand-logo, .brand-divider { display: none; }

  .app-main { position: relative; }

  /* Closed drawers keep their size, so the graphs lay out while hidden;
     visibility keeps them out of the tab order once they have slid away. */
  .panel-left, .panel-right {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: 1050;
    visibility: hidden;
    border: none;
    box-shadow: var(--shadow-md);
    overscroll-behavior: contain;
    transition: transform var(--drawer-ease), visibility 0s linear 0.22s;
  }

  .panel-left { left: 0; width: var(--drawer-left-w); transform: translateX(-100%); }

  .panel-right { right: 0; width: var(--drawer-right-w); transform: translateX(100%); }

  .is-right-collapsed .panel-right { display: block; }

  .is-left-open .panel-left, .is-right-open .panel-right {
    visibility: visible;
    transform: none;
    transition: transform var(--drawer-ease), visibility 0s;
  }

  .is-left-open .drawer-scrim, .is-right-open .drawer-scrim {
    display: block;
    position: absolute;
    inset: 0;
    z-index: 1040;
    background: rgba(16, 24, 20, 0.2);
  }

  /* Handles ride along the drawer edge, so the same handle closes it */
  .panel-toggle {
    z-index: 1060;
    width: 30px;
    height: 64px;
    margin-top: -32px;
    transition: transform var(--drawer-ease);
  }

  .panel-toggle-left {
    display: block;
    left: 0;
    right: auto;
    border: 1px solid var(--border);
    border-left: none;
    border-radius: 0 8px 8px 0;
  }

  .is-left-open .panel-toggle-left { transform: translateX(var(--drawer-left-w)); }

  .is-right-open .panel-toggle-right { transform: translateX(calc(-1 * var(--drawer-right-w))); }

  /* Chevrons point the way the drawer will move */
  .panel-toggle.panel-toggle-left::before { content: "\203A"; }

  .panel-toggle.panel-toggle-right::before, .is-left-open .panel-toggle-left::before { content: "\2039"; }

  .is-right-open .panel-toggle-right::before { content: "\203A"; }

  /* A feature was selected while the statistics drawer was closed */
  .panel-toggle-right.has-update { color: var(--accent); background: var(--accent-weak); border-color: var(--accent); }

  .panel-toggle-right.has-update::after {
    content: "";
    position: absolute;
    top: 6px;
    left: 50%;
    width: 8px;
    height: 8px;
    margin-left: -4px;
    background: var(--accent);
    border-radius: 50%;
    animation: handle-pulse 1.6s ease-out 3;
  }

  /* Long layer names shorten rather than run under the map controls */
  .map-badge { max-width: calc(100% - 140px); }

  #badge-layer { min-width: 0; overflow: hidden; text-overflow: ellipsis; }

  /* One scroll per drawer: a nested scrolling legend traps swipes on touch */
  .legend { max-height: none; overflow: visible; }

  /* Finger-sized rows */
  .choice { padding-top: 0.55rem; padding-bottom: 0.55rem; }
}

@keyframes handle-pulse {
  0% { box-shadow: 0 0 0 0 rgba(0, 108, 51, 0.55); }
  100% { box-shadow: 0 0 0 8px rgba(0, 108, 51, 0); }
}

@media (prefers-reduced-motion: reduce) {
  .panel-left, .panel-right, .panel-toggle { transition: none !important; }
  .panel-toggle-right.has-update::after { animation: none; }
}

/* ---- Login ------------------------------------------------------------- */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: linear-gradient(135deg, var(--brand-blue-weak), #fff 55%, var(--accent-weak));
}

.login-card {
  width: 100%;
  max-width: 360px;
  padding: 1.75rem 1.75rem 2rem;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.08);
}

.login-logo { display: block; height: 40px; width: auto; margin: 0 auto 1.1rem; }

.login-card h1 { margin: 0 0 0.2rem; font-size: 1.05rem; font-weight: 600; text-align: center; }

.login-sub { margin: 0 0 1.3rem; color: var(--text-muted); font-size: 0.82rem; text-align: center; }

.login-card label {
  display: block;
  margin: 0.7rem 0 0.25rem;
  color: var(--text-muted);
  font-size: 0.8rem;
}

.login-card input[type="text"], .login-card input[type="password"] {
  width: 100%;
  padding: 0.55rem 0.65rem;
  font-size: 0.95rem;
  border: 1px solid var(--border);
  border-radius: 4px;
}

.login-card input:focus {
  outline: none;
  border-color: var(--brand-blue);
  box-shadow: 0 0 0 2px var(--brand-blue-weak);
}

.login-card button {
  width: 100%;
  margin-top: 1.3rem;
  padding: 0.6rem;
  color: #fff;
  font-size: 0.95rem;
  font-weight: 600;
  background: var(--accent);
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.login-card button:hover { filter: brightness(1.06); }

.form-error, .form-notice {
  margin-bottom: 0.6rem;
  padding: 0.55rem 0.65rem;
  font-size: 0.82rem;
  border-radius: 4px;
}

.form-error { color: #8a1f1f; background: #fbeaea; border: 1px solid #ecc7c7; }

.form-notice { color: #17456d; background: var(--brand-blue-weak); border: 1px solid #bcd4e8; }
