/* Mobile-first. The map is the page; everything else floats over it. */
:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --panel-2: #f0f2f6;
  --text: #14171c;
  --muted: #5b6472;
  --line: #dde1e8;
  --accent: #0b6bcb;
  --accent-text: #ffffff;
  --warn-bg: #fff4e0; --warn-line: #e0a33a; --warn-text: #6b4a10;
  --err-bg: #fdecec;  --err-line: #d84a4a;  --err-text: #7c1f1f;
  --shadow: 0 2px 16px rgba(0,0,0,0.16);
  --pin-ring: #ffffff;
  --map-bg: #eef0f4;
  --r: 12px;
  --tap: 44px;                       /* minimum comfortable touch target */
  --sheet-peek: 94px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
/* Dark values live in two places on purpose: once for the system default and
   once for an explicit choice, so the toggle can override either way. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1116; --panel: #161b22; --panel-2: #212934;
    --text: #e6edf3; --muted: #99a3b0; --line: #2c333d;
    --accent: #4d9dfd; --accent-text: #06121f;
    --warn-bg: #2e2410; --warn-line: #8a6a22; --warn-text: #f0d69a;
    --err-bg: #2c1516;  --err-line: #8c3a3a;  --err-text: #f3b6b6;
    --shadow: 0 2px 16px rgba(0,0,0,0.5);
    --pin-ring: #10151b;
    --map-bg: #12161c;
  }
}
:root[data-theme="dark"] {
    --bg: #0e1116; --panel: #161b22; --panel-2: #212934;
    --text: #e6edf3; --muted: #99a3b0; --line: #2c333d;
    --accent: #4d9dfd; --accent-text: #06121f;
    --warn-bg: #2e2410; --warn-line: #8a6a22; --warn-text: #f0d69a;
    --err-bg: #2c1516;  --err-line: #8c3a3a;  --err-text: #f3b6b6;
    --shadow: 0 2px 16px rgba(0,0,0,0.5);
    --pin-ring: #10151b;
    --map-bg: #12161c;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; overscroll-behavior: none; }
body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text); background: var(--bg);
}
#map { position: fixed; inset: 0; background: var(--map-bg); }

/* ------------------------------------------------------------ topbar --- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 500;
  padding: 8px 8px 0; padding-top: calc(8px + env(safe-area-inset-top, 0px));
  pointer-events: none;
}
#search-form {
  display: flex; gap: 0; max-width: 520px; margin: 0 auto;
  pointer-events: auto; background: var(--panel); border-radius: var(--r);
  box-shadow: var(--shadow); overflow: hidden; border: 1px solid var(--line);
}
#place {
  flex: 1; min-width: 0; font: inherit; padding: 0 12px; height: var(--tap);
  border: none; background: transparent; color: var(--text); outline: none;
}
#search-go {
  width: var(--tap); flex: none; border: none; background: transparent;
  color: var(--accent); font-size: 20px; cursor: pointer;
}

/* ------------------------------------------------------------- tools --- */
#tools {
  position: fixed; right: 8px; z-index: 500;
  top: calc(var(--tap) + 20px + env(safe-area-inset-top, 0px));
  display: flex; flex-direction: column; gap: 6px;
}
.tool {
  width: var(--tap); height: var(--tap); border-radius: var(--r);
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
  font-size: 18px; line-height: 1; cursor: pointer; box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center;
}
.tool.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.hint-bubble {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 600;
  top: calc(var(--tap) + 20px + env(safe-area-inset-top, 0px));
  background: var(--accent); color: var(--accent-text); padding: 7px 14px;
  border-radius: 999px; font-size: 13px; box-shadow: var(--shadow);
  max-width: calc(100vw - 120px); text-align: center;
}

/* ------------------------------------------------------------- sheet --- */
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 550;
  background: var(--panel); border-top: 1px solid var(--line);
  border-radius: var(--r) var(--r) 0 0; box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  max-height: 88vh; transition: transform 0.22s ease;
  padding-bottom: var(--safe-b);
}
#sheet.peek { transform: translateY(calc(100% - var(--sheet-peek) - var(--safe-b))); }
#sheet.peek #sheet-body { overflow: hidden; }

#grip {
  border: none; background: transparent; padding: 8px 0 4px; cursor: pointer;
  display: flex; justify-content: center; flex: none; touch-action: none;
}
#grip span { width: 38px; height: 4px; border-radius: 2px; background: var(--line); display: block; }

#actionbar {
  display: flex; align-items: center; gap: 10px; padding: 0 14px 12px;
  flex: none; min-height: 52px;
}
#count { flex: 1; min-width: 0; line-height: 1.25; }
#count b { font-size: 20px; display: block; font-variant-numeric: tabular-nums; }
#count span { font-size: 12px; color: var(--muted); display: block;
              overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#dl-main { flex: none; min-height: var(--tap); padding: 0 20px; font-weight: 600; }

#sheet-body { overflow-y: auto; padding: 0 14px 16px;
              -webkit-overflow-scrolling: touch; }

/* ------------------------------------------------------------ groups --- */
.grp { border-top: 1px solid var(--line); }
.grp > summary {
  padding: 12px 2px; cursor: pointer; font-size: 13px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted);
  list-style: none; display: flex; align-items: center; min-height: var(--tap);
}
.grp > summary::-webkit-details-marker { display: none; }
.grp > summary::after { content: '＋'; margin-left: auto; font-size: 15px; }
.grp[open] > summary::after { content: '－'; }
.grp-body { padding: 0 2px 14px; display: flex; flex-direction: column; gap: 10px; }

button {
  font: inherit; min-height: var(--tap); padding: 8px 14px; border-radius: 9px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  cursor: pointer;
}
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.link { border: none; background: none; color: var(--accent); min-height: 0;
              padding: 4px 2px; font-size: 13px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.rowline { display: flex; align-items: center; gap: 8px; }

input[type=number], input[type=text] {
  font: inherit; padding: 9px 10px; border-radius: 9px; width: 100%;
  min-height: var(--tap);
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
}
input[type=range] { width: 100%; height: var(--tap); accent-color: var(--accent); }
label.field { display: flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--muted); }
label.field > span { display: flex; justify-content: space-between; }
label.field b { color: var(--text); font-variant-numeric: tabular-nums; }
label.chk { display: flex; align-items: center; gap: 10px; min-height: var(--tap); font-size: 14px; }
label.chk input { width: 20px; height: 20px; accent-color: var(--accent); }

.hint { font-size: 12px; color: var(--muted); margin: 0; }
.muted { color: var(--muted); font-size: 13px; }
.note { padding: 8px 10px; border-radius: 8px; font-size: 13px;
        border: 1px solid var(--line); background: var(--panel-2); }
.note.warn { background: var(--warn-bg); border-color: var(--warn-line); color: var(--warn-text); }
.note.err  { background: var(--err-bg);  border-color: var(--err-line);  color: var(--err-text); }
.note:empty { display: none; }

.vendor-list { display: flex; flex-direction: column; }
.vendor-row { display: flex; align-items: center; gap: 10px; font-size: 14px;
              cursor: pointer; min-height: var(--tap); }
.vendor-row input { width: 20px; height: 20px; flex: none; accent-color: var(--accent); }
.swatch { width: 12px; height: 12px; border-radius: 50%; flex: none;
          border: 1px solid rgba(128,128,128,0.5); }
.vendor-row .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.vendor-row .ct { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 13px; }

.prose h3 { font-size: 13px; margin: 10px 0 4px; }
.prose h3:first-child { margin-top: 0; }
.prose p { margin: 5px 0; font-size: 13px; color: var(--muted); }
.prose code { background: var(--panel-2); padding: 1px 5px; border-radius: 4px;
              font-size: 12px; color: var(--text); }
.prose a, .hint a { color: var(--accent); }

/* ------------------------------------------------------------ markers --- */
.leaflet-container { font: inherit; background: var(--map-bg); }
.leaflet-popup-content { font: 13px/1.45 system-ui, sans-serif; margin: 12px 14px; }
.leaflet-popup-content dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 8px 0 0; }
.leaflet-popup-content dt { color: #6a7280; }
.leaflet-popup-content dd { margin: 0; }
.leaflet-control-attribution { font-size: 10px; }
.leaflet-bottom.leaflet-right { bottom: calc(var(--sheet-peek) + var(--safe-b)); }
.leaflet-control-zoom { display: none; }        /* pinch on touch; wheel on desktop */
#map.picking { cursor: crosshair; }

/* ------------------------------------------------------------ desktop --- */
@media (min-width: 820px) {
  :root { --sheet-peek: 0px; }
  #sheet {
    left: auto; right: 12px; top: 12px; bottom: 12px; width: 390px;
    max-height: none; border-radius: var(--r); border: 1px solid var(--line);
    transform: none !important;
  }
  #sheet.peek #sheet-body { overflow-y: auto; }
  #grip { display: none; }
  #actionbar { padding-top: 14px; }
  #topbar { right: 418px; }
  #tools { right: 418px; top: calc(var(--tap) + 20px); }
  .hint-bubble { left: calc(50% - 200px); }
  .leaflet-control-zoom { display: block; }
  .leaflet-bottom.leaflet-right { bottom: 0; right: 402px; }
}
