:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #15191f;
  --muted: #5d6673;
  --line: #e2e6eb;
  --brand: #0f766e;
  --brand-ink: #ffffff;
  --brand-soft: #e6f4f2;
  --warn: #b42318;
  --warn-soft: #fdecea;
  --info: #1d4ed8;
  --info-soft: #e8eefc;
  --money: #8a5a00;
  --money-soft: #fdf3dc;
  --new: #c2410c;
  --radius: 12px;
  --tabs-h: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1116; --surface: #171b22; --text: #e7eaee; --muted: #9aa3ae; --line: #2a3039;
    --brand: #2dd4bf; --brand-ink: #062a26; --brand-soft: #123532;
    --warn: #ff8a80; --warn-soft: #3a1c1a; --info: #8fb0ff; --info-soft: #1b2440;
    --money: #f5c46b; --money-soft: #3a2e14; --new: #fb923c;
    color-scheme: dark;
  }
  :root:not([data-theme="light"]) .leaflet-tile-pane { filter: brightness(.75) contrast(1.1) saturate(.8); }
}
:root[data-theme="dark"] {
  --bg: #0e1116; --surface: #171b22; --text: #e7eaee; --muted: #9aa3ae; --line: #2a3039;
  --brand: #2dd4bf; --brand-ink: #062a26; --brand-soft: #123532;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
}
a { color: var(--brand); }
h2 { font-size: 1.2rem; margin: 0 0 6px; line-height: 1.25; }
h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 20px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.warn { color: var(--warn); }

/* header */
.top {
  position: sticky; top: 0; z-index: 1000;
  display: flex; gap: 10px; align-items: center;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; white-space: nowrap; }
@media (max-width: 480px) { .brand span { display: none; } }
#search { flex: 1; display: flex; min-width: 0; }
#search input {
  flex: 1; min-width: 0; padding: 10px 12px; font-size: 16px;
  border: 1px solid var(--line); border-right: 0; border-radius: 10px 0 0 10px;
  background: var(--bg); color: var(--text);
}
#search button {
  padding: 0 14px; border: 1px solid var(--brand); border-radius: 0 10px 10px 0;
  background: var(--brand); color: var(--brand-ink); font-size: 18px; cursor: pointer;
}
#results {
  position: absolute; left: 16px; right: 16px; top: 100%; margin: 4px 0 0; padding: 4px;
  list-style: none; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0,0,0,.15); max-height: 60vh; overflow: auto;
}
#results li.muted { padding: 10px 12px; }
#results button {
  display: block; width: 100%; text-align: left; padding: 10px 12px; border: 0; border-radius: 8px;
  background: none; color: var(--text); cursor: pointer; font: inherit;
}
#results button:hover { background: var(--bg); }
#results small { display: block; color: var(--muted); }

/* layout */
main { display: flex; flex-direction: column; flex: 1; min-height: 0; }
#map { height: 38vh; min-height: 220px; background: #dfe3e8; z-index: 1; }
@media (min-width: 900px) {
  body { height: 100vh; }
  main { display: grid; grid-template-columns: 440px 1fr; grid-template-rows: auto 1fr; overflow: hidden; }
  .gpsbar { grid-column: 1 / -1; grid-row: 1; }
  .panel { grid-column: 1; grid-row: 2; overflow-y: auto; min-height: 0; }
  #map { grid-column: 2; grid-row: 2; height: auto; min-height: 0; }
}
.gpsbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 16px;
  background: var(--surface); border-bottom: 1px solid var(--line); font-size: .88rem;
}
.gps-text { flex: 1; min-width: 0; }
.gps-ind { width: 10px; height: 10px; border-radius: 50%; background: var(--line); flex: none; }
.gps-ind.wait { background: #f59e0b; animation: pulse 1.2s infinite; }
.gps-ind.live { background: #16a34a; box-shadow: 0 0 0 4px rgba(22,163,74,.2); }
@keyframes pulse { 50% { opacity: .35; } }

.panel { padding: 16px; }

/* buttons */
.pill {
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 999px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.pill.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.pill.saved { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); }
.pill.big { padding: 12px 22px; font-size: 1rem; }
.pill:disabled { opacity: .5; cursor: default; }
.link { background: none; border: 0; padding: 0; color: var(--brand); font: inherit; cursor: pointer; text-decoration: underline; }
.link.danger { color: var(--warn); }
.link:disabled { color: var(--muted); cursor: default; }

/* empty */
.empty { text-align: center; padding: 12px 4px 24px; }
.empty p { color: var(--muted); max-width: 46ch; margin: 8px auto 18px; }

/* property */
.prop-head { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; }
.badge { display: inline-block; font-size: .78rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; }
.badge.ok { background: var(--brand-soft); color: var(--brand); }
.badge.no { background: var(--warn-soft); color: var(--warn); }
.note { background: var(--bg); border: 1px solid var(--line); padding: 10px 12px; border-radius: 10px; font-size: .9rem; }
.note.warn { background: var(--warn-soft); border-color: transparent; }
.note.info { background: var(--info-soft); border-color: transparent; color: var(--info); }
.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 18px 0; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 14px 0 8px; }
.summary div { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px; text-align: center; }
.summary b { display: block; font-size: 1rem; }
.summary span { font-size: .75rem; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.chip { font-size: .74rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--line); color: var(--muted); }
.chip.work { color: var(--text); }
.chip.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.chip.info { background: var(--info-soft); color: var(--info); border-color: transparent; }
.chip.money { background: var(--money-soft); color: var(--money); border-color: transparent; }
.chip.new { background: var(--new); color: #fff; border-color: transparent; }

.permits { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.permit { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.permit.new { border-color: var(--new); box-shadow: 0 0 0 1px var(--new); }
.permit-top { display: flex; justify-content: space-between; gap: 10px; font-weight: 600; }
.permit-date { color: var(--muted); font-weight: 500; white-space: nowrap; font-size: .88rem; }
.permit-desc { margin: 6px 0; font-size: .92rem; overflow-wrap: anywhere; }
.permit-meta { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 6px 12px; margin: 8px 0 0; font-size: .82rem; }
.permit-meta dt { color: var(--muted); }
.permit-meta dd { margin: 0; overflow-wrap: anywhere; }

.nearby { display: flex; flex-wrap: wrap; gap: 8px; }
.nearby button {
  border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 10px;
  padding: 6px 10px; font: inherit; font-size: .85rem; cursor: pointer; text-align: left;
}
.nearby small { display: block; color: var(--muted); font-size: .74rem; }

/* CRM */
.crm { background: var(--brand-soft); border-radius: var(--radius); padding: 12px; margin: 12px 0; display: grid; gap: 10px; }
.crm-row { display: flex; flex-wrap: wrap; gap: 8px; }
.crm-row > * { flex: 1 1 140px; }
.crm label { display: grid; gap: 4px; font-size: .78rem; font-weight: 600; color: var(--muted); }
.crm input, .crm select {
  width: 100%; padding: 8px 10px; font: inherit; font-size: 16px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--surface); color: var(--text);
}
.crm summary { cursor: pointer; font-weight: 600; font-size: .88rem; margin-bottom: 6px; }
.note-form { display: flex; gap: 8px; }
.note-form input { flex: 1; }
.notes { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.notes li { background: var(--surface); border-radius: 8px; padding: 8px 10px; font-size: .9rem; }
.notes small { display: block; color: var(--muted); font-size: .76rem; }
.crm-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }

/* saved */
.saved-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.filters { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0 12px; scrollbar-width: none; }
.filters button { flex: none; border: 1px solid var(--line); background: var(--surface); color: var(--text); padding: 5px 12px; border-radius: 999px; font: inherit; font-size: .84rem; cursor: pointer; }
.filters button.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.cards { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.card { display: block; width: 100%; text-align: left; font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; cursor: pointer; }
.card-top { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.card-meta { display: flex; flex-wrap: wrap; gap: 6px 10px; font-size: .82rem; color: var(--muted); margin-top: 6px; align-items: center; }
.card-note { margin: 6px 0 0; font-size: .86rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status { font-weight: 700; color: var(--brand); }
.status.s-dead { color: var(--muted); }
.status.s-closed { color: var(--info); }
.tag { color: var(--text); }
.io { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 20px; font-size: .9rem; }
.cities { padding-left: 18px; line-height: 1.9; }

/* tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1000;
  display: flex; height: calc(var(--tabs-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabs button { flex: 1; border: 0; background: none; color: var(--muted); font: inherit; font-size: .75rem; font-weight: 600; cursor: pointer; display: grid; place-content: center; gap: 2px; }
.tabs button span { font-size: 1.2rem; line-height: 1; }
.tabs button.active { color: var(--brand); }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabs-h) + 16px + env(safe-area-inset-bottom));
  z-index: 1100; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px;
  font-size: .9rem; max-width: calc(100% - 32px); box-shadow: 0 6px 20px rgba(0,0,0,.2);
}

/* map markers */
.you-dot { background: #2563eb; border: 3px solid #fff; border-radius: 50%; box-shadow: 0 0 0 2px rgba(37,99,235,.35), 0 1px 4px rgba(0,0,0,.4); }
.you-accuracy { fill: #2563eb; fill-opacity: .1; stroke: #2563eb; stroke-opacity: .4; stroke-width: 1; }
.prop-pin span { display: block; width: 28px; height: 28px; background: var(--brand); border: 3px solid #fff; border-radius: 50% 50% 50% 0; transform: rotate(-45deg); box-shadow: 0 2px 6px rgba(0,0,0,.35); }
.permit-dot { fill: #f59e0b; fill-opacity: .85; stroke: #fff; stroke-width: 2; }
.permit-dot.main { fill: var(--brand); }
.saved-pin { color: #f59e0b; font-size: 20px; line-height: 22px; text-align: center; text-shadow: 0 0 3px #000; }
.saved-pin.has-new { color: var(--new); }
