:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --ink: #0b1220;
  --muted: #4d5768;
  --line: #cfd6e0;
  --field: #ffffff;
  --chip: #e6ebf2;
  --accent: #0a58f5;
  --accent-ink: #ffffff;
  --accent-soft: #e3ecff;
  --red: #c21f1f;
  --red-soft: #fde8e8;
  --green: #0f7a55;
  --hot: #e5383b;
  --warm: #e08a00;
  --cold: #2f7fe0;
  --shadow: 0 1px 2px rgba(11, 18, 32, .08), 0 4px 14px rgba(11, 18, 32, .06);
  --nav-h: 68px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #090d14;
    --card: #151b26;
    --ink: #f3f6fa;
    --muted: #a7b1c1;
    --line: #2c3545;
    --field: #0f141d;
    --chip: #222b3a;
    --accent: #5b96ff;
    --accent-ink: #06101f;
    --accent-soft: #16264a;
    --red: #ff7070;
    --red-soft: #3b1518;
    --green: #40d197;
    --hot: #ff5c5f;
    --warm: #ffb02e;
    --cold: #62a8ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
a { color: inherit; text-decoration: none; touch-action: manipulation; }
h1, h2, h3, p { margin: 0; }

/* ---------- header ---------- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: var(--bg);
}
.top-text { min-width: 0; }
.top h1 { font-size: 30px; line-height: 1.1; font-weight: 800; letter-spacing: -.02em; }
.top p { color: var(--muted); font-size: 15px; font-weight: 600; margin-top: 2px; }
.top p b { color: var(--accent); }
.icon-btn {
  flex: none;
  width: 48px; height: 48px;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: var(--chip); color: var(--ink);
}

/* ---------- layout ---------- */
main {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 4px max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
}
.view { max-width: 560px; margin: 0 auto; padding-bottom: 24px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 52px; padding: 0 16px;
  border: 2px solid var(--line); border-radius: 14px;
  background: var(--card); color: var(--ink);
  font-weight: 700; font-size: 17px; text-align: center;
  user-select: none; -webkit-user-select: none;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { border-color: var(--red); color: var(--red); }
.btn.alert { background: var(--red); border-color: var(--red); color: #fff; }
.btn.ghost { background: transparent; color: var(--accent); border-color: transparent; min-height: 48px; }
.btn.big { min-height: 58px; font-size: 19px; }
.btn.full { width: 100%; }
.btn[disabled] { opacity: .55; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---------- Today cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 14px;
  overflow: hidden;
}
.card.rescue { border: 2px solid var(--red); }
.banner {
  padding: 8px 14px;
  font-size: 14px; font-weight: 800; letter-spacing: .02em; text-transform: uppercase;
  background: var(--accent-soft); color: var(--accent);
}
.card.rescue .banner { background: var(--red); color: #fff; }
.card-body { padding: 14px; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; width: 100%; padding: 0; border: 0; background: none; text-align: left; }
.who { display: block; min-width: 0; }
.who .name, .who .addr { display: block; }
.name { font-size: 21px; font-weight: 800; line-height: 1.2; overflow-wrap: anywhere; }
.addr { color: var(--muted); font-size: 16px; overflow-wrap: anywhere; }
.temp { flex: none; display: inline-flex; align-items: center; gap: 6px; font-size: 14px; font-weight: 700; padding: 4px 10px; border-radius: 999px; background: var(--chip); }
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--warm); }
.dot.hot { background: var(--hot); }
.dot.cold { background: var(--cold); }
.meta { margin-top: 8px; font-size: 15px; font-weight: 600; color: var(--muted); }
.meta .late { color: var(--red); }
.note { margin-top: 6px; font-size: 16px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.note::before { content: "\201C"; }
.note::after { content: "\201D"; }
.card .pair { margin-top: 12px; }
.did { margin-top: 10px; font-size: 14px; font-weight: 700; color: var(--green); }
.outcomes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 10px; }
.outcomes button {
  min-height: 50px; padding: 4px 2px;
  border: 0; border-radius: 12px;
  background: var(--chip); color: var(--ink);
  font-size: 13.5px; font-weight: 700; line-height: 1.15;
}
.outcomes button:active { background: var(--accent-soft); }
.outcomes button.no { color: var(--red); }
.outlabel { margin-top: 12px; font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

.section-h { margin: 18px 2px 8px; font-size: 14px; font-weight: 800; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.empty { text-align: center; padding: 36px 12px 12px; }
.empty .check {
  width: 88px; height: 88px; margin: 0 auto 16px;
  display: grid; place-items: center; border-radius: 50%;
  background: var(--green); color: #fff;
}
.empty h2 { font-size: 26px; font-weight: 800; }
.empty p { color: var(--muted); margin: 6px 0 20px; }
.setup { padding: 14px; }
.setup p { font-weight: 700; margin-bottom: 8px; }
.setup .inline { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.warn { background: var(--red-soft); color: var(--red); border-radius: 12px; padding: 10px 12px; font-size: 15px; font-weight: 600; margin-bottom: 12px; }

/* ---------- forms ---------- */
.field { display: block; margin-bottom: 14px; }
.field > span, .field .lbl, .field > label > span { display: block; font-size: 15px; font-weight: 700; margin-bottom: 6px; }
.field em { font-style: normal; font-weight: 500; color: var(--muted); font-size: 13px; margin-left: 4px; }
input[type="text"], input[type="tel"], input[type="search"], input[type="date"], input[type="datetime-local"], select, textarea {
  display: block; width: 100%; min-width: 0;
  min-height: 52px; padding: 12px 14px;
  border: 2px solid var(--line); border-radius: 14px;
  background: var(--field); color: var(--ink);
  font-size: 18px;
  -webkit-appearance: none; appearance: none;
}
textarea { min-height: 88px; resize: vertical; line-height: 1.35; }
select {
  padding-right: 40px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%237a8496' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; background-size: 20px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
::placeholder { color: var(--muted); opacity: .7; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.err { color: var(--red); font-weight: 700; font-size: 15px; margin: -6px 0 12px; }
.field .btn.ghost { margin-top: 4px; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid2 button, .seg button {
  min-height: 52px; padding: 6px 8px;
  border: 2px solid var(--line); border-radius: 14px;
  background: var(--card); color: var(--ink);
  font-size: 15.5px; font-weight: 700; line-height: 1.15;
}
.grid2 button[aria-checked="true"], .seg button[aria-checked="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.grid2 button:last-child:nth-child(odd) { grid-column: 1 / -1; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg button { display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-size: 17px; }
.seg button[aria-checked="true"] .dot { box-shadow: 0 0 0 2px #fff; }

.savebar {
  position: sticky; bottom: 0; z-index: 2;
  margin: 8px -16px 0; padding: 10px 16px 12px;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

/* ---------- leads list ---------- */
.searchbar { position: sticky; top: 0; z-index: 2; background: var(--bg); margin: 0 -16px; padding: 0 16px 10px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 10px 16px 2px; margin: 0 -16px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 48px; padding: 0 16px;
  border: 2px solid var(--line); border-radius: 999px;
  background: var(--card); color: var(--ink);
  font-size: 15.5px; font-weight: 700; white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.chips.wrap { flex-wrap: wrap; overflow: visible; margin: 0; padding: 0; }
.rowlead {
  display: flex; align-items: center; gap: 12px; width: 100%;
  min-height: 68px; padding: 12px 14px; margin-bottom: 8px;
  border: 1px solid var(--line); border-radius: 16px;
  background: var(--card); text-align: left;
}
.rowlead .who { flex: 1; min-width: 0; }
.rowlead .name { font-size: 18px; }
.rowlead .addr { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rowlead .when { flex: none; max-width: 40%; text-align: right; font-size: 14px; font-weight: 700; color: var(--muted); }
.rowlead .when.due { color: var(--accent); }
.rowlead .when.late { color: var(--red); }
.count { font-size: 14px; font-weight: 600; color: var(--muted); margin: 0 2px 8px; }

/* ---------- bottom nav ---------- */
.tabs {
  flex: none;
  display: grid; grid-template-columns: 1fr 1fr 1fr; align-items: end;
  height: calc(var(--nav-h) + var(--safe-b));
  padding: 0 max(8px, env(safe-area-inset-right)) var(--safe-b) max(8px, env(safe-area-inset-left));
  background: var(--card);
  border-top: 1px solid var(--line);
}
.tab {
  position: relative;
  height: var(--nav-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border: 0; background: none;
  color: var(--muted); font-size: 13px; font-weight: 700;
}
.tab[aria-current="page"] { color: var(--accent); }
.tab.add { justify-content: flex-end; padding-bottom: 7px; color: var(--ink); }
.tab.add i {
  position: absolute; top: -22px; left: 50%; transform: translateX(-50%);
  width: 64px; height: 64px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink);
  border: 4px solid var(--card);
  box-shadow: 0 4px 12px rgba(10, 88, 245, .35);
}
.tab.add[aria-current="page"] { color: var(--accent); }
.badge {
  position: absolute; top: 6px; left: calc(50% + 8px);
  min-width: 22px; height: 22px; padding: 0 6px;
  display: grid; place-items: center; border-radius: 999px;
  background: var(--red); color: #fff; font-size: 13px; font-weight: 800;
}
@media (prefers-color-scheme: dark) { .badge { color: #1a0405; } }

/* ---------- sheet ---------- */
.scrim { position: fixed; inset: 0; z-index: 20; background: rgba(5, 9, 16, .55); }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  max-width: 600px; margin: 0 auto;
  max-height: calc(100dvh - env(safe-area-inset-top, 0px) - 20px);
  display: flex; flex-direction: column;
  background: var(--bg);
  border-radius: 22px 22px 0 0;
  box-shadow: 0 -8px 30px rgba(0, 0, 0, .25);
}
.sheet-head {
  flex: none; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 14px 12px 10px 18px;
}
.sheet-head h2 { font-size: 23px; font-weight: 800; min-width: 0; overflow-wrap: anywhere; }
.sheet-body { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 16px calc(24px + var(--safe-b)); }
.sheet h3 { margin: 22px 2px 10px; font-size: 14px; font-weight: 800; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.sheet h3:first-child { margin-top: 6px; }
.nextline { font-size: 18px; font-weight: 800; margin-bottom: 10px; }
.hint { color: var(--muted); font-size: 14px; margin: 6px 2px 0; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 14px 20px; border-left: 2px solid var(--line); margin-left: 6px; }
.timeline li:last-child { border-left-color: transparent; padding-bottom: 0; }
.timeline li::before { content: ""; position: absolute; left: -7px; top: 5px; width: 12px; height: 12px; border-radius: 50%; background: var(--accent); }
.timeline time { display: block; font-size: 13px; font-weight: 700; color: var(--muted); }
.stack { display: grid; gap: 10px; }
.tplmeta { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); margin-top: 4px; }
.tplmeta .over { color: var(--red); font-weight: 700; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 12px; right: 12px; z-index: 40;
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  max-width: 560px; margin: 0 auto;
  display: flex; align-items: center; gap: 10px;
  padding: 12px 12px 12px 16px;
  border-radius: 16px;
  background: var(--ink); color: var(--bg);
  font-size: 16px; font-weight: 700;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .3);
}
.toast span { flex: 1; min-width: 0; }
.toast button {
  flex: none; min-height: 48px; padding: 0 14px;
  border: 0; border-radius: 10px;
  background: var(--accent); color: var(--accent-ink); font-weight: 800;
}

@media (max-width: 359px) {
  .outcomes { grid-template-columns: 1fr 1fr; }
  .row2 { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: no-preference) {
  .sheet { animation: up .18s ease-out; }
  .toast { animation: down .15s ease-out; }
  @keyframes up { from { transform: translateY(16px); opacity: 0; } }
  @keyframes down { from { transform: translateY(-16px); opacity: 0; } }
}
