:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --ink: #16202c;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #1d4ed8;
  --accent-dark: #1e40af;
  --danger: #dc2626;

  /* status colors — kept in sync with map.js STATUS_COLORS */
  --st-todo: #cbd5e1;
  --st-signed: #16a34a;
  --st-wants_sign: #f59e0b;
  --st-sign_delivered: #2563eb;
  --st-not_home: #eab308;
  --st-declined: #64748b;
  --st-hostile: #dc2626;

  --topbar-h: 52px;
  --tabbar-h: 64px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }  /* the attribute must win over .btn/.terr-editor display rules */
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--ink); background: var(--bg);
  overscroll-behavior: none;
}

/* ---- top bar ---- */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; height: var(--topbar-h);
  background: var(--accent); color: #fff; z-index: 1000;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px; padding-top: env(safe-area-inset-top);
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
}
.topbar-title { font-weight: 700; font-size: 17px; letter-spacing: .2px; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.who { font-size: 12px; opacity: .85; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.linkbtn { background: rgba(255,255,255,.16); color: #fff; border: 0; border-radius: 8px; padding: 6px 10px; font-size: 13px; }

/* ---- content ---- */
.content {
  padding-top: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  min-height: 100%;
}
body.map-body .content { padding-bottom: 0; }

/* ---- tab bar ---- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; height: var(--tabbar-h);
  background: var(--card); border-top: 1px solid var(--line); z-index: 1000;
  display: flex; padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; text-decoration: none; color: var(--muted); font-size: 11px; font-weight: 600;
}
.tab-ico { font-size: 20px; }
.tab.is-active { color: var(--accent); }

/* ---- auto-update banner ---- */
.update-banner {
  position: fixed; left: 0; right: 0; z-index: 3000;
  top: calc(var(--topbar-h) + env(safe-area-inset-top));
  background: #16a34a; color: #fff; text-align: center; font-weight: 700;
  padding: 12px 16px; font-size: 14px; box-shadow: 0 4px 14px rgba(0,0,0,.2); cursor: pointer;
}

/* ---- flash toasts ---- */
.flash-wrap {
  position: fixed; top: calc(var(--topbar-h) + env(safe-area-inset-top) + 8px);
  left: 0; right: 0; z-index: 2000; display: flex; flex-direction: column; align-items: center; gap: 6px;
  pointer-events: none;
}
.toast {
  background: #16202c; color: #fff; padding: 10px 16px; border-radius: 10px;
  font-size: 14px; max-width: 90vw; box-shadow: 0 6px 20px rgba(0,0,0,.2);
  animation: toastin .2s ease;
}
.toast-error { background: var(--danger); }
.toast-info { background: #16202c; }
@keyframes toastin { from { opacity: 0; transform: translateY(-8px);} to {opacity:1;transform:none;} }

/* ---- buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--card); color: var(--ink); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 16px; font-size: 15px; font-weight: 600;
  text-decoration: none; cursor: pointer;
}
.btn:active { transform: scale(.98); }
.btn-block { width: 100%; margin-bottom: 10px; }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-danger { background: #fff; color: var(--danger); border-color: #f3c4c4; }
.btn-small { padding: 7px 12px; font-size: 13px; border-radius: 9px; }

/* ---- login ---- */
.login-body { background: linear-gradient(160deg, #1d4ed8, #1e3a8a); }
.login-card {
  max-width: 420px; margin: 8vh auto 0; background: var(--card);
  border-radius: 18px; padding: 28px 22px; box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.login-card h1 { margin: 0 0 6px; font-size: 24px; }
.login-form { display: flex; flex-direction: column; gap: 12px; margin-top: 18px; }
.login-form label { font-size: 13px; font-weight: 600; color: var(--muted); }
.login-form input {
  padding: 14px; border: 1px solid var(--line); border-radius: 12px; font-size: 16px;
}
.dev-link { margin-top: 16px; padding: 12px; background: #fef9c3; border-radius: 10px; word-break: break-all; font-size: 13px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.muted.small { margin-top: 16px; }

/* ---- map ---- */
#map { position: fixed; top: calc(var(--topbar-h) + env(safe-area-inset-top)); left: 0; right: 0;
       bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); }
.search-overlay { position: fixed; top: calc(var(--topbar-h) + env(safe-area-inset-top) + 8px);
                  left: 10px; right: 10px; z-index: 900; }
#search { width: 100%; padding: 12px 14px; border-radius: 12px; border: 0;
          box-shadow: 0 4px 14px rgba(0,0,0,.18); font-size: 15px; }
.search-results { background: #fff; border-radius: 12px; margin-top: 6px; overflow: hidden;
                  box-shadow: 0 6px 20px rgba(0,0,0,.18); }
.search-results div { padding: 11px 14px; border-bottom: 1px solid var(--line); font-size: 14px; }
.search-results div:last-child { border-bottom: 0; }

.legend { position: fixed; right: 10px; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px);
          z-index: 850; background: rgba(255,255,255,.94); border-radius: 10px; padding: 8px 10px;
          font-size: 11px; box-shadow: 0 3px 10px rgba(0,0,0,.15); }
.legend .lg { display: flex; align-items: center; gap: 6px; margin: 2px 0; }
.legend .sw { width: 11px; height: 11px; border-radius: 50%; }

/* ---- bottom sheet ---- */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1500;
  background: var(--card); border-radius: 20px 20px 0 0;
  box-shadow: 0 -8px 30px rgba(0,0,0,.25); padding: 10px 16px calc(18px + env(safe-area-inset-bottom));
  transform: translateY(110%); transition: transform .22s ease; max-height: 82vh; overflow-y: auto;
}
.sheet.open { transform: none; }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 2px auto 8px; }
.sheet-close { position: absolute; top: 8px; right: 12px; border: 0; background: none; font-size: 26px; color: var(--muted); line-height: 1; }
.sheet-addr { font-size: 20px; font-weight: 700; padding-right: 30px; }
.sheet-last { font-size: 13px; color: var(--muted); margin: 4px 0 12px; min-height: 16px; }

.outcome-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.outcome-btn {
  border: 0; border-radius: 14px; padding: 18px 12px; font-size: 15px; font-weight: 700;
  color: #fff; text-align: left; cursor: pointer; line-height: 1.2;
}
.outcome-btn:active { transform: scale(.97); }
.oc-signed { background: var(--st-signed); }
.oc-wants_sign { background: var(--st-wants_sign); }
.oc-sign_delivered { background: var(--st-sign_delivered); }
.oc-not_home { background: var(--st-not_home); color: #3a2f00; }
.oc-declined { background: var(--st-declined); }
.oc-hostile { background: var(--st-hostile); }
.outcome-btn.busy { opacity: .5; }
/* an active (recorded) state: bright + ringed + a check */
.outcome-btn.chosen { box-shadow: 0 0 0 3px var(--ink); opacity: 1; }
.outcome-btn.chosen::after { content: " ✓"; }
.outcome-btn:not(.chosen) { opacity: .82; }

.sheet-hint { font-size: 12.5px; color: var(--muted); margin: 4px 0 12px; }
.sheet-hist { margin-top: 14px; }
.hist-title { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 6px; }
.hist-row { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--ink); padding: 5px 0; border-top: 1px solid var(--line); }
.hist-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.hist-empty { font-size: 13px; color: var(--muted); padding: 4px 0; }

.note-input { width: 100%; margin-top: 12px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; font-size: 15px; }
.sheet-status { text-align: center; font-weight: 700; margin-top: 12px; min-height: 20px; }
.next-btn {
  width: 100%; margin-top: 10px; background: var(--accent); color: #fff; border: 0;
  border-radius: 14px; padding: 16px; font-size: 16px; font-weight: 700;
}

/* ---- list ---- */
.list-wrap { max-width: 640px; margin: 0 auto; padding: 12px; }
.segmented { display: flex; background: var(--line); border-radius: 12px; padding: 4px; margin-bottom: 14px; }
.seg { flex: 1; border: 0; background: none; padding: 10px; border-radius: 9px; font-weight: 600; font-size: 14px; color: var(--muted); }
.seg.is-active { background: var(--card); color: var(--ink); box-shadow: 0 1px 4px rgba(0,0,0,.1); }
.street-head { margin: 16px 0 6px; font-size: 15px; color: var(--muted); }
.addr-list { list-style: none; margin: 0; padding: 0; }
.addr-row { background: var(--card); border: 1px solid var(--line); border-radius: 12px; margin-bottom: 8px; }
.addr-link { display: flex; align-items: center; gap: 12px; padding: 14px; text-decoration: none; color: var(--ink); }
.addr-no { font-weight: 700; font-size: 16px; min-width: 52px; }
.status-dot { width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0; }
.status-txt { font-size: 14px; color: var(--muted); }
.dot-todo { background: var(--st-todo); } .dot-signed { background: var(--st-signed); }
.dot-wants_sign { background: var(--st-wants_sign); } .dot-sign_delivered { background: var(--st-sign_delivered); }
.dot-not_home { background: var(--st-not_home); } .dot-declined { background: var(--st-declined); }
.dot-hostile { background: var(--st-hostile); }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; }

.addr-row.followup { padding: 14px; display: block; }
.fu-main { display: flex; align-items: center; gap: 10px; }
.fu-addr { font-weight: 700; }
.fu-sub { font-size: 13px; color: var(--muted); margin: 4px 0 0 24px; }
.chip { font-size: 11px; padding: 2px 8px; border-radius: 20px; background: var(--line); color: var(--muted); font-weight: 600; }
.chip-mine { background: #dbeafe; color: var(--accent-dark); }
.mark-delivered { margin-top: 10px; }

/* ---- admin ---- */
.admin-wrap, .terr-wrap { max-width: 640px; margin: 0 auto; padding: 14px; }
.back { display: inline-block; margin-bottom: 6px; color: var(--accent); text-decoration: none; font-size: 14px; font-weight: 600; }
.stat-row { display: flex; gap: 10px; margin: 14px 0 20px; }
.stat { flex: 1; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px; text-align: center; }
.stat-n { display: block; font-size: 26px; font-weight: 800; }
.stat-l { font-size: 12px; color: var(--muted); }
.stack-form { display: flex; flex-direction: column; gap: 10px; margin: 12px 0 20px; }
.stack-form input, .stack-form select, .terr-editor input, .terr-editor select {
  padding: 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 15px; width: 100%;
}
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.user-list, .addr-admin-list, .terr-list { list-style: none; margin: 0; padding: 0; }
.user-row { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 8px;
            display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.user-row.inactive { opacity: .55; }
.user-main { flex: 1; min-width: 140px; display: flex; flex-direction: column; }
.user-email { font-weight: 600; }
.user-controls { display: flex; gap: 6px; align-items: center; }
.user-controls select { padding: 7px; border-radius: 9px; border: 1px solid var(--line); }
.addr-admin-row { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 6px;
                  display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.add-addr { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin-bottom: 16px; }
.add-addr summary { font-weight: 600; cursor: pointer; }

/* ---- territories ---- */
#terr-map, #terr-cfg { }
#terr-map { height: 46vh; border-radius: 14px; margin: 10px 0; border: 1px solid var(--line); }
.terr-editor { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px; margin-bottom: 16px; display: flex; flex-direction: column; gap: 10px; }
.terr-editor input[type=color] { flex: 0 0 48px; padding: 4px; height: 44px; }
.terr-list li { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 8px; display: flex; align-items: center; gap: 10px; }
.terr-swatch { width: 16px; height: 16px; border-radius: 5px; flex-shrink: 0; }
.terr-li-main { flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.terr-li-name { font-weight: 700; }
.terr-li-sub { font-size: 12px; color: var(--muted); }
.terr-edit-btn { flex-shrink: 0; }
