:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece4;
  --text: #22251f;
  --muted: #6c7063;
  --line: #d9d5c9;
  --accent: #3f7a4a;
  --accent-text: #ffffff;
  --accent-soft: #dcebd9;
  --danger: #c2412d;
  --danger-soft: #f8ddd6;
  --warn: #b7791f;
  --warn-soft: #f7e7c8;
  --info: #2f6c9e;
  --info-soft: #d8e7f3;
  --enc-empty: #fbfaf6;
  --enc-1: #cfe5c9;
  --enc-2: #9fcb96;
  --enc-3: #6faa65;
  --enc-stroke: #6f8a68;
  --shed: #e3dfd3;
  --path: #d3cfc2;
  --radius: 12px;
  --shadow: 0 6px 24px rgba(30, 30, 20, .12);
  --nav-h: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171915;
    --surface: #212420;
    --surface-2: #2a2e28;
    --text: #e8eae3;
    --muted: #9aa092;
    --line: #3a3f37;
    --accent: #79b984;
    --accent-text: #10210f;
    --accent-soft: #2b3d2c;
    --danger: #ef7a65;
    --danger-soft: #4a2620;
    --warn: #e5b45c;
    --warn-soft: #463715;
    --info: #79b0dc;
    --info-soft: #1f3445;
    --enc-empty: #262a24;
    --enc-1: #2f4a2f;
    --enc-2: #3f6a3e;
    --enc-3: #55904f;
    --enc-stroke: #7c9a74;
    --shed: #33362f;
    --path: #3a3e36;
    --shadow: 0 6px 24px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.app { display: flex; flex-direction: column; min-height: 100%; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 700; letter-spacing: .2px; }
.brand span { color: var(--accent); }
.topbar .spacer { flex: 1; }
.nav { display: flex; gap: 4px; }
.nav a {
  text-decoration: none; color: var(--muted); padding: 8px 12px; border-radius: 999px;
  display: flex; align-items: center; gap: 6px; white-space: nowrap;
}
.nav a.active { background: var(--accent-soft); color: var(--text); font-weight: 600; }
.nav .ico { font-size: 18px; line-height: 1; }
.nav .count {
  background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px;
  padding: 1px 6px; font-weight: 700;
}
main { flex: 1; padding: 16px; max-width: 1200px; width: 100%; margin: 0 auto; }

@media (max-width: 799px) {
  .topbar .nav { display: none; }
  .bottomnav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    display: flex; justify-content: space-around;
    background: var(--surface); border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .bottomnav a { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 4px; border-radius: 10px; flex: 1; justify-content: center; position: relative; }
  .bottomnav .count { position: absolute; top: 2px; left: 55%; }
  main { padding: 12px 12px calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); }
}
@media (min-width: 800px) { .bottomnav { display: none; } }

/* ---------- controls ---------- */
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  padding: 9px 14px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
  min-height: 40px; justify-content: center;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { min-height: 32px; padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.block { width: 100%; }
.iconbtn { border: 0; background: transparent; font-size: 22px; line-height: 1; padding: 6px 8px; cursor: pointer; color: var(--muted); }

input[type=text], input[type=email], input[type=password], input[type=search], select, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); min-height: 42px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }

.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 7px 12px; cursor: pointer; min-height: 36px;
}
.seg button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.seg button.on.v-urgent { background: var(--danger); border-color: var(--danger); color: #fff; }
.seg button.on.v-needed { background: var(--warn); border-color: var(--warn); color: #fff; }
.seg button.on.v-treating { background: var(--info); border-color: var(--info); color: #fff; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.chip { background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; font-size: 13px; }
.chip b { font-size: 15px; }

.badge { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.urgent { background: var(--danger-soft); color: var(--danger); font-weight: 600; }
.badge.needed { background: var(--warn-soft); color: var(--warn); font-weight: 600; }
.badge.treating { background: var(--info-soft); color: var(--info); }
.badge.loc { background: var(--accent-soft); color: var(--text); }

/* ---------- map ---------- */
.mapview { display: grid; grid-template-columns: 1fr; gap: 16px; }
@media (min-width: 900px) {
  .mapview { grid-template-columns: minmax(0, 1fr) 380px; align-items: start; }
  .mapview .panel { position: sticky; top: 76px; max-height: calc(100vh - 92px); overflow: auto; }
}
.mapbox {
  position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; height: calc(100vh - 190px); min-height: 380px;
}
@media (max-width: 799px) { .mapbox { height: calc(100dvh - 220px - var(--nav-h)); } }
.mapbox svg { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; cursor: grab; }
.mapbox svg.dragging { cursor: grabbing; }
.mapctl { position: absolute; right: 10px; top: 10px; display: flex; flex-direction: column; gap: 6px; }
.mapctl button { width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-size: 20px; cursor: pointer; box-shadow: var(--shadow); }
.maplegend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 8px; align-items: center; }
.maplegend i { display: inline-block; width: 14px; height: 14px; border-radius: 3px; border: 1px solid var(--enc-stroke); vertical-align: -3px; margin-right: 4px; }

.enc { cursor: pointer; }
.enc rect, .enc polygon { fill: var(--enc-empty); stroke: var(--enc-stroke); stroke-width: 1.2; transition: fill .15s; }
.enc.c1 rect, .enc.c1 polygon { fill: var(--enc-1); }
.enc.c2 rect, .enc.c2 polygon { fill: var(--enc-2); }
.enc.c3 rect, .enc.c3 polygon { fill: var(--enc-3); }
.enc.sel rect, .enc.sel polygon { stroke: var(--text); stroke-width: 3; }
.enc text.num { font-size: 13px; font-weight: 700; fill: var(--text); text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.enc .cnt circle { fill: var(--text); }
.enc .cnt text { fill: var(--bg); font-size: 10px; font-weight: 700; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.enc .vet { fill: var(--danger); stroke: var(--surface); stroke-width: 1.5; }
.enc .vet.needed { fill: var(--warn); }
.shed { fill: var(--shed); stroke: var(--line); }
.shed-label, .map-label { font-size: 11px; fill: var(--muted); text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.map-path { fill: none; stroke: var(--path); stroke-width: 10; stroke-linecap: round; stroke-linejoin: round; }
.map-fence { fill: none; stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 5 4; }
.gate { stroke: var(--accent); stroke-width: 3; stroke-linecap: round; }

/* ---------- panel / sheet ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.panel h2 { margin: 0; font-size: 20px; }
.panel-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.panel-head .spacer { flex: 1; }
.panel-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
@media (max-width: 899px) {
  .mapview .panel.sheet {
    position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); z-index: 25;
    border-radius: 18px 18px 0 0; max-height: 62vh; overflow: auto; box-shadow: var(--shadow);
    animation: up .18s ease-out;
  }
}
@media (min-width: 800px) and (max-width: 899px) { .mapview .panel.sheet { bottom: 0; } }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.panel.empty { color: var(--muted); }
@media (max-width: 899px) { .mapview .panel.empty { display: none; } }

/* ---------- dog list ---------- */
.doglist { display: flex; flex-direction: column; gap: 6px; }
.dogrow {
  display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer; text-align: left; width: 100%;
}
.dogrow:hover { background: var(--surface-2); }
.dogrow .thumb { width: 52px; height: 52px; border-radius: 10px; object-fit: cover; background: var(--surface-2); flex: none; display: grid; place-items: center; font-size: 24px; }
.dogrow .info { flex: 1; min-width: 0; }
.dogrow .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dogrow .meta { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 3px; }
.dogrow input[type=checkbox] { width: 22px; height: 22px; flex: none; accent-color: var(--accent); }
.dogrow.checked { border-color: var(--accent); background: var(--accent-soft); }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar input[type=search] { flex: 1; min-width: 200px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 8px; }
h1.page { font-size: 22px; margin: 4px 0 12px; }
h3.group { font-size: 15px; margin: 18px 0 8px; color: var(--muted); font-weight: 600; }

/* ---------- modal ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 50;
  display: flex; align-items: flex-start; justify-content: center; padding: 24px 12px; overflow: auto;
}
.modal {
  background: var(--bg); border-radius: 16px; width: 100%; max-width: 640px; box-shadow: var(--shadow);
  animation: up .18s ease-out;
}
.modal-head { display: flex; align-items: center; gap: 8px; padding: 12px 12px 12px 18px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--bg); border-radius: 16px 16px 0 0; z-index: 2; }
.modal-head h2 { margin: 0; font-size: 19px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal-body { padding: 16px 18px 20px; }
.modal-foot { display: flex; gap: 8px; padding: 0 18px 18px; flex-wrap: wrap; }
.modal-foot .spacer { flex: 1; }
@media (max-width: 640px) {
  .overlay { padding: 0; align-items: stretch; }
  .modal { border-radius: 0; min-height: 100%; max-width: none; }
  .modal-head { border-radius: 0; padding-top: max(12px, env(safe-area-inset-top)); }
}
.section { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.section h4 { margin: 0 0 10px; font-size: 14px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.locline { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.locline .where { font-size: 17px; font-weight: 600; flex: 1; }

.photos { display: flex; gap: 8px; overflow-x: auto; scroll-snap-type: x mandatory; padding-bottom: 4px; }
.photo { position: relative; flex: none; scroll-snap-align: start; }
.photo img { height: 180px; border-radius: 10px; display: block; background: var(--surface-2); }
.photo .del { position: absolute; top: 6px; right: 6px; width: 28px; height: 28px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; cursor: pointer; font-size: 16px; }
.addphoto {
  flex: none; width: 140px; height: 180px; border: 2px dashed var(--line); border-radius: 10px;
  display: grid; place-items: center; text-align: center; color: var(--muted); cursor: pointer; padding: 8px;
}
.addphoto input { display: none; }
.preview-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.preview-row img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; }

.history { list-style: none; padding: 0; margin: 0; }
.history li { padding: 8px 0; border-bottom: 1px solid var(--line); }
.history li:last-child { border-bottom: 0; }
.history .when { font-size: 12px; color: var(--muted); }

.encpick { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 6px; margin-top: 8px; max-height: 220px; overflow: auto; }
.encpick button { border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 6px 4px; cursor: pointer; line-height: 1.1; }
.encpick button small { display: block; color: var(--muted); font-size: 11px; }
.encpick button.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.encpick button.on small { color: var(--accent-text); }
.encpick button.current { opacity: .45; }

.fosterpick { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.fosterpick label { display: flex; gap: 8px; align-items: center; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); cursor: pointer; }
.fosterpick input { accent-color: var(--accent); width: 18px; height: 18px; }

/* ---------- log ---------- */
.logrow { display: flex; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--surface); align-items: baseline; flex-wrap: wrap; }
.logrow:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.logrow:last-child { border-radius: 0 0 var(--radius) var(--radius); border-bottom: 0; }
.logbox { border: 1px solid var(--line); border-radius: var(--radius); }
.logrow .when { color: var(--muted); font-size: 13px; width: 92px; flex: none; }
.logrow .what { flex: 1; min-width: 200px; }
.logrow .who { color: var(--muted); font-size: 13px; }
.logrow a { cursor: pointer; font-weight: 600; text-decoration: none; }

/* ---------- login ---------- */
.login { max-width: 360px; margin: 12vh auto 0; background: var(--surface); padding: 24px; border-radius: 16px; border: 1px solid var(--line); }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login p { margin: 0 0 18px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + 20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 100;
  box-shadow: var(--shadow); max-width: calc(100% - 32px); opacity: 0; pointer-events: none; transition: opacity .2s;
}
.toast.show { opacity: 1; }
.toast.err { background: var(--danger); color: #fff; }
.empty-note { padding: 24px; text-align: center; color: var(--muted); background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); }
