:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #1f2421;
  --muted: #65706a;
  --border: #dcdcd4;
  --accent: #2f6f4f;
  --accent-text: #ffffff;
  --danger: #b3362f;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161917;
    --surface: #202522;
    --text: #e9ece9;
    --muted: #9aa49e;
    --border: #343b37;
    --accent: #5fb38a;
    --accent-text: #0e1410;
    --danger: #e3726b;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  height: 100dvh; /* visible viewport on iOS, not the toolbar-hidden 100vh */
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); }
::selection { background: color-mix(in srgb, var(--accent) 28%, transparent); }
button, input, textarea {
  font: inherit;
  color: inherit;
}
/* 16px minimum: iOS Safari force-zooms any focused field below 16px. */
input, textarea {
  width: 100%;
  min-width: 0;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  caret-color: var(--accent);
  -webkit-appearance: none;
  appearance: none;
}
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 1; }
input[type="search"]::-webkit-search-decoration { -webkit-appearance: none; }
input[type="number"] { -moz-appearance: textfield; }
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
textarea { resize: vertical; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button {
  color: var(--text);
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
}
button:disabled { opacity: .6; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.danger { color: var(--danger); }
button.link { border: none; background: none; color: var(--muted); padding: 6px 8px; }

/* ---------- top bar & tabs ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: max(8px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 17px; margin: 0; }
.tabs { display: flex; gap: 4px; margin-right: auto; }
.tabs button { border: none; background: none; padding: 8px 12px; border-radius: 8px; color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--bg); color: var(--text); font-weight: 600; }
.badge { font-size: 12px; color: var(--muted); }
.me { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 30vw; }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.tab-panel { flex: 1; min-height: 0; }
.tab-panel[hidden] { display: none; }
#tab-map { display: flex; }
#tab-map[hidden] { display: none; }
#map { flex: 1; min-height: 300px; }

/* ---------- list ---------- */
#tab-list, #tab-users {
  overflow-y: auto;
  overscroll-behavior-y: contain;
  padding: 16px max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
/* No scrolling of the page behind an open dialog. */
html:has(dialog[open]) #tab-list { overflow: hidden; }
.users-intro { max-width: 900px; margin: 0 auto 12px; font-size: 14px; }
.list-toolbar { display: flex; gap: 8px; max-width: 900px; margin: 0 auto 12px; }
.list-toolbar input { flex: 1; }
.people { list-style: none; margin: 0 auto; padding: 0; max-width: 900px; display: grid; gap: 8px; }
.person {
  display: flex;
  gap: 12px;
  justify-content: space-between;
  align-items: flex-start;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.person-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; overflow-wrap: anywhere; }
.person-actions { display: flex; gap: 6px; flex-shrink: 0; }
.person-actions button { padding: 8px 12px; font-size: 14px; }
.muted { color: var(--muted); }
.note { color: var(--muted); font-style: italic; white-space: pre-wrap; }
.empty { text-align: center; color: var(--muted); margin-top: 40px; }
.you, .status {
  display: inline-block;
  padding: 0 7px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  vertical-align: 1px;
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent);
}
.status.pending { background: var(--bg); color: var(--muted); border: 1px solid var(--border); }
.status.blocked { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }

/* ---------- children ---------- */
.kids { list-style: none; margin: 2px 0; padding: 0; display: flex; flex-wrap: wrap; gap: 4px; }
.kids li {
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 13px;
}
.kids .age { color: var(--muted); }
.children-field { border: none; margin: 0; padding: 0; min-width: 0; }
.children-field legend { font-size: 13px; color: var(--muted); padding: 0; margin-bottom: 4px; }
.children-edit { list-style: none; margin: 0 0 6px; padding: 0; display: grid; gap: 6px; }
.children-edit li { display: grid; grid-template-columns: 1fr 120px 64px auto; gap: 6px; align-items: center; }
.children-edit .child-age { font-size: 13px; color: var(--muted); }
.children-edit .remove-child { padding: 6px 10px; color: var(--danger); }
button.small { padding: 8px 12px; font-size: 14px; }

/* ---------- map chrome (Leaflet themed from app tokens) ----------
   Leaflet ships a fixed white popup and controls; without this the app's
   dark tokens land on Leaflet's white surfaces. */
.leaflet-container { font-family: inherit; background: var(--bg); }
.leaflet-container a { color: var(--accent); }
.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 6px 20px rgb(0 0 0 / .22), 0 1px 3px rgb(0 0 0 / .12);
}
.leaflet-popup-content-wrapper { border-radius: var(--radius); border: 1px solid var(--border); }
.leaflet-popup-content { margin: 14px 18px 14px 16px; font-size: 14px; line-height: 1.45; }
.leaflet-container a.leaflet-popup-close-button {
  color: var(--muted);
  width: 32px;
  height: 32px;
  font: 22px/30px system-ui, sans-serif;
}
.leaflet-container a.leaflet-popup-close-button:hover,
.leaflet-container a.leaflet-popup-close-button:focus-visible { color: var(--text); }
.popup { display: grid; gap: 3px; min-width: 180px; padding-right: 12px; }
.popup strong { font-size: 15px; }

.leaflet-bar { border: 1px solid var(--border) !important; box-shadow: 0 2px 8px rgb(0 0 0 / .14) !important; }
.leaflet-bar a,
.leaflet-bar a:hover {
  background: var(--surface);
  color: var(--text);
  border-bottom-color: var(--border);
}
.leaflet-bar a:hover { background: var(--bg); }
.leaflet-bar a.leaflet-disabled { background: var(--surface); color: var(--muted); opacity: .5; }
.leaflet-control-attribution.leaflet-control {
  background: color-mix(in srgb, var(--surface) 85%, transparent);
  color: var(--muted);
}

/* Pin: same silhouette as the favicon, in the accent colour. */
.pin { background: none; border: none; }
.pin svg { display: block; filter: drop-shadow(0 2px 2px rgb(0 0 0 / .3)); }
.pin .pin-body { fill: var(--accent); stroke: var(--surface); stroke-width: 1.5; }
.pin .pin-dot { fill: var(--surface); }

/* Clusters: accent rings instead of Leaflet's green/yellow/orange. */
.marker-cluster-small,
.marker-cluster-medium,
.marker-cluster-large { background: color-mix(in srgb, var(--accent) 30%, transparent); }
.marker-cluster-small div,
.marker-cluster-medium div,
.marker-cluster-large div {
  background: var(--accent);
  color: var(--accent-text);
  font: 600 13px/30px system-ui, sans-serif;
}
.marker-cluster span { line-height: 30px; }

@media (prefers-color-scheme: dark) {
  /* Take the glare off OSM's light tiles without losing legibility. */
  .leaflet-tile-pane { filter: brightness(.82) contrast(1.06) saturate(.85); }
}

/* ---------- dialog ----------
   Header and action bar stay put; only .form-body scrolls. */
dialog {
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 32px); /* fallback */
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  overflow: hidden;
}
dialog[open] { display: flex; flex-direction: column; }
dialog:focus { outline: none; }
dialog::backdrop { background: rgb(0 0 0 / .45); }
#person-form { display: flex; flex-direction: column; flex: 1; min-height: 0; }
#person-form h2 { margin: 0; padding: 18px 20px 12px; font-size: 18px; flex-shrink: 0; }
.form-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 2px 20px 8px;
}
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid > * { min-width: 0; }
.grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.full { grid-column: 1 / -1; }
/* Checkbox row: the whole label is the tap target. */
.grid label.check {
  flex-direction: row;
  align-items: flex-start;
  gap: 10px;
  align-self: end;
  padding: 6px 0;
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}
.check input {
  width: 20px;
  height: 20px;
  min-height: 0;
  margin: 1px 0 0;
  padding: 0;
  flex-shrink: 0;
  appearance: auto;
  -webkit-appearance: auto;
  accent-color: var(--accent);
}
.check small { display: block; font-size: 12px; color: var(--muted); }
.private { font-size: 12px; color: var(--muted); white-space: nowrap; }
.row { display: flex; gap: 6px; }
.row input { flex: 1; }
#form-map { height: 220px; border-radius: 8px; border: 1px solid var(--border); }
.hint { font-size: 13px; color: var(--muted); margin: 6px 0 0; overflow-wrap: anywhere; }
.actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-shrink: 0;
  padding: 12px 20px max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.error { color: var(--danger); margin: 8px 0 0; }
.error:empty { display: none; }

/* ---------- login ---------- */
.login-page { align-items: center; justify-content: center; padding: 16px; }
.login-card {
  width: min(340px, 100%);
  display: grid;
  gap: 14px;
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.login-card p { margin: 0; }
.login-card .error { margin: 0; }
.hint-inline { font-size: 12px; }
.notice {
  padding: 10px 12px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  font-size: 14px;
}
.notice[hidden] { display: none; }
.auth-links { font-size: 14px; color: var(--muted); text-align: center; }

/* Touch: 44px targets. */
@media (pointer: coarse) {
  button, input { min-height: 44px; }
  .tabs button, button.link { min-height: 40px; }
  .person-actions button, button.small { padding: 8px 14px; }
  .children-edit .remove-child { min-width: 44px; }
}

@media (max-width: 600px) {
  .topbar { gap: 8px; padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .topbar h1, .me { display: none; }
  .grid { grid-template-columns: 1fr; }
  .person { flex-direction: column; }
  .person-actions { width: 100%; }
  .person-actions button { flex: 1; }
  .list-toolbar { flex-direction: column; }
  .children-edit li { grid-template-columns: 1fr 96px auto; grid-template-areas: "name year remove" "age age age"; row-gap: 2px; }
  .children-edit .child-name { grid-area: name; }
  .children-edit .child-year { grid-area: year; }
  .children-edit .child-age { grid-area: age; padding-left: 2px; }
  .children-edit .child-age:empty { display: none; }
  .children-edit .remove-child { grid-area: remove; }

  /* Dialog becomes a full-screen sheet. */
  dialog {
    width: 100%;
    max-width: 100%;
    height: 100%; /* fallback */
    height: 100dvh;
    max-height: 100%;
    max-height: 100dvh;
    margin: 0;
    border: none;
    border-radius: 0;
  }
  #person-form h2 {
    padding: max(14px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
    border-bottom: 1px solid var(--border);
  }
  .form-body { padding: 14px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left)); }
  .actions { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
  .actions button { flex: 1; }
  #form-map { height: 200px; }
}
