:root {
  --bg: #0f1720;
  --panel: #16202b;
  --panel-2: #1d2a38;
  --text: #e7eef5;
  --muted: #92a4b5;
  --accent: #4aa3ff;
  --border: #26323f;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
body { display: flex; flex-direction: column; height: 100vh; background: var(--bg); color: var(--text); overflow: hidden; }

#topbar {
  display: flex; align-items: center; gap: 24px; padding: 10px 18px;
  background: var(--panel); border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.brand h1 { font-size: 17px; margin: 0; letter-spacing: .2px; }
.brand #meta-sub { font-size: 12px; color: var(--muted); }
.tabs { display: flex; gap: 6px; }
.tab {
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--border);
  padding: 7px 16px; border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600;
}
.tab.active { background: var(--accent); color: #04223f; border-color: var(--accent); }
.controls { display: flex; align-items: center; gap: 14px; margin-left: auto; }
.controls label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 3px; }
.controls input, .controls select {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 5px 8px; font-size: 13px;
}
#reset-btn, #export-btn {
  align-self: flex-end; background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 7px 12px; cursor: pointer; font-size: 12px;
}
#reset-btn:hover, #export-btn:hover { border-color: var(--accent); }

/* Sowing-tab parameter controls — only shown on that tab (toggled via [hidden]). */
#sowing-controls { display: contents; }
#sowing-controls[hidden] { display: none; }
.sow-num { width: 58px; }

.banner {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 9px 18px; background: var(--panel-2); border-bottom: 1px solid var(--border);
  font-size: 13px; color: var(--text);
}
.banner .b-title { font-weight: 700; }
.banner .b-sep { color: var(--border); }
.banner .b-muted { color: var(--muted); }
.banner .b-chip {
  display: inline-block; padding: 2px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 700; color: #06121f;
}

main { flex: 1; display: flex; min-height: 0; }
#map { flex: 1; background: #0b1117; }

#panel {
  width: 320px; background: var(--panel); border-left: 1px solid var(--border);
  padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 18px;
}
#legend h4 { margin: 0 0 8px; font-size: 13px; }
.legend-row { display: flex; align-items: center; gap: 8px; margin: 5px 0; font-size: 12.5px; }
.legend-swatch { width: 18px; height: 14px; border-radius: 3px; flex: none; border: 1px solid rgba(255,255,255,.15); }
.legend-note { font-size: 11px; color: var(--muted); margin-top: 6px; }

#detail h3 { margin: 0 0 8px; font-size: 14px; }
#detail .hint { font-size: 12px; color: var(--muted); }
.detail-name { font-size: 15px; font-weight: 700; }
.detail-state { font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.detail-grid { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 13px; }
.detail-grid .k { color: var(--muted); }
.detail-grid .v { text-align: right; font-variant-numeric: tabular-nums; }
.cat-pill { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; color: #06121f; }

#summary { font-size: 12px; color: var(--muted); }
.bar-row { display: flex; align-items: center; gap: 8px; margin: 3px 0; }
.bar-row .lab { width: 92px; font-size: 11.5px; color: var(--text); }
.bar-track { flex: 1; background: var(--panel-2); border-radius: 4px; height: 12px; overflow: hidden; }
.bar-fill { height: 100%; }
.bar-row .cnt { width: 34px; text-align: right; font-variant-numeric: tabular-nums; }

#src { font-size: 11px; color: var(--muted); border-top: 1px solid var(--border); padding-top: 10px; }

.tooltip {
  position: fixed; pointer-events: none; z-index: 20; background: rgba(10,16,22,.94);
  border: 1px solid var(--border); border-radius: 7px; padding: 8px 10px; font-size: 12.5px;
  max-width: 240px; box-shadow: 0 4px 14px rgba(0,0,0,.4);
}
.tooltip b { font-size: 13px; }
.tooltip .tt-state { color: var(--muted); font-size: 11px; }
.tooltip .tt-metric { margin-top: 4px; }
.maplibregl-popup { display: none; }

/* Handle is desktop-hidden; it only appears as the bottom-sheet grip on mobile. */
#panel-handle { display: none; }

/* ---------------- Mobile: map-first, panel becomes a bottom sheet ---------- */
@media (max-width: 768px) {
  #topbar { gap: 8px 10px; padding: 8px 12px; }
  .brand h1 { font-size: 15px; }
  #meta-sub { display: none; }
  .tabs { order: 3; width: 100%; }
  .tab { flex: 1; padding: 8px 4px; font-size: 12px; text-align: center; }
  .controls { order: 4; width: 100%; margin-left: 0; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
  .controls label { font-size: 11px; }
  #state-control { flex: 1 1 130px; }
  #state-select { width: 100%; }
  .controls input, .controls select { padding: 5px 6px; font-size: 12px; }

  .banner { gap: 8px; padding: 6px 12px; font-size: 11.5px; overflow-x: auto; white-space: nowrap; flex-wrap: nowrap; }

  /* Map is the central, full-width element. */
  main { flex-direction: column; }
  #map { width: 100%; flex: 1 1 auto; min-height: 0; }

  /* Side panel -> bottom sheet overlaying the map; collapsed to just its handle. */
  #panel {
    position: fixed; left: 0; right: 0; bottom: 0; width: auto;
    height: 46px; max-height: 82vh; overflow: hidden;
    border-left: none; border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0; padding: 0 16px 16px;
    box-shadow: 0 -6px 22px rgba(0, 0, 0, .5);
    transition: height .25s ease; z-index: 30;
  }
  body.sheet-open #panel { height: 80vh; overflow-y: auto; }

  #panel-handle {
    display: flex; align-items: center; justify-content: center; gap: 8px;
    height: 46px; margin: 0 -16px; padding: 0 16px;
    background: var(--panel); color: var(--text); cursor: pointer;
    border: none; border-bottom: 1px solid var(--border);
    font-size: 13px; font-weight: 600; position: sticky; top: 0; z-index: 1;
  }
  #panel-handle .chev { font-size: 11px; color: var(--muted); transition: transform .25s; }
  body.sheet-open #panel-handle .chev { transform: rotate(180deg); }

  /* Hide panel contents while collapsed; reveal when the sheet is open. */
  #legend, #detail, #summary, #src { display: none; }
  body.sheet-open #legend,
  body.sheet-open #detail,
  body.sheet-open #summary,
  body.sheet-open #src { display: block; }

  /* Touch uses tap -> sheet, so the hover tooltip is unnecessary. */
  .tooltip { display: none !important; }
}
