:root {
  --brand: #0b6e8a;
  --brand-dark: #08566c;
  --bg: #f4f7f9;
  --card: #ffffff;
  --text: #1b2a33;
  --muted: #5d6f7a;
  --line: #dde5ea;
  --bad: #c62828;
  --bad-bg: #fdecea;
  --warn: #b85c00;
  --warn-bg: #fff3e0;
  --ok: #2e7d32;
  --ok-bg: #e8f5e9;
  --info-bg: #e6f3f7;
  --up: #1f6fb2;
  --up-bg: #e8f1fa;
  --radius: 12px;
  --nav-h: 62px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}
a { color: var(--brand); }
h1 { font-size: 1.35rem; margin: .4rem 0 .8rem; }
h2 { font-size: 1.08rem; margin: 1.2rem 0 .6rem; }
code { background: #eef2f4; padding: 1px 5px; border-radius: 4px; }
.loading { padding: 40vh 16px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .86rem; }
.warn-text { color: var(--warn); }
.error-text { color: var(--bad); font-weight: 600; min-height: 1.2em; margin: .5rem 0; }
[hidden] { display: none !important; }

/* ---------- Shell ---------- */
.top {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--brand); color: #fff;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; }
.brand img { border-radius: 7px; background: #fff; }
.pill {
  border: 0; border-radius: 99px; padding: 5px 11px; font: inherit; font-size: .82rem; font-weight: 600;
  background: rgba(255,255,255,.18); color: #fff; cursor: pointer; white-space: nowrap;
}
.pill-ok { background: rgba(255,255,255,.18); }
.pill-warn { background: #ffb74d; color: #3e2200; }
.pill-bad { background: #ef5350; color: #fff; }
.alertbar {
  display: block; padding: 10px 16px; background: var(--bad); color: #fff; font-weight: 700; text-decoration: none;
  animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse { 50% { background: #a31515; } }
@media (prefers-reduced-motion: reduce) { .alertbar { animation: none; } }

.nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; display: flex; overflow-x: auto;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: #fff; border-top: 1px solid var(--line); scrollbar-width: none;
}
.nav a {
  flex: 1 0 64px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .72rem; font-weight: 600;
}
.nav a.active { color: var(--brand); box-shadow: inset 0 3px 0 var(--brand); }
.nav-i { font-size: 1.25rem; line-height: 1; }
.main { max-width: 1100px; margin: 0 auto; padding: 12px 16px calc(var(--nav-h) + 28px); }

@media (min-width: 900px) {
  .nav {
    top: 52px; bottom: 0; right: auto; width: 190px; height: auto; flex-direction: column; overflow: visible;
    border-top: 0; border-right: 1px solid var(--line); padding: 10px 0;
  }
  .nav a { flex: 0 0 auto; flex-direction: row; justify-content: flex-start; gap: 10px; padding: 11px 18px; font-size: .95rem; }
  .nav a.active { box-shadow: inset 4px 0 0 var(--brand); background: var(--info-bg); }
  .main { margin-left: 190px; padding: 18px 28px 40px; max-width: 1180px; }
  #alertbar { margin-left: 190px; }
}

/* ---------- Cards & layout ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 0 0 12px; }
.card-link { display: block; color: inherit; text-decoration: none; }
.card-link:active { background: #f0f5f7; }
.card-info { background: var(--info-bg); border-color: #b9dde8; }
.card-warn { background: var(--warn-bg); border-color: #ffd59e; }
.card-alert { display: block; background: var(--bad-bg); border-color: #f5b4ae; color: var(--bad); text-decoration: none; }
.card-late { border-left: 4px solid var(--warn); }
.card.error { color: var(--bad); }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.row-end { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.row-gap { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 8px; }
.grid2 { display: grid; gap: 0 14px; grid-template-columns: 1fr; }
@media (min-width: 600px) { .grid2 { grid-template-columns: 1fr 1fr; } }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 6px 0; border-bottom: 1px dashed var(--line); }
.kv:last-of-type { border-bottom: 0; }
.kv span { color: var(--muted); }
.kv b { text-align: right; font-weight: 600; }
.back { display: inline-block; margin: 2px 0 6px; text-decoration: none; font-weight: 600; }
.empty { padding: 26px 14px; text-align: center; color: var(--muted); background: var(--card); border: 1px dashed var(--line); border-radius: var(--radius); margin-bottom: 12px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 42px; padding: 8px 14px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--text);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-small { min-height: 36px; padding: 5px 11px; font-size: .88rem; }
.btn-block { width: 100%; margin-top: 8px; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-warn { background: var(--warn-bg); border-color: #ffcc80; color: var(--warn); }
.btn-ok { background: var(--ok-bg); border-color: #a5d6a7; color: var(--ok); }
.btn-bad { background: var(--bad-bg); border-color: #f5b4ae; color: var(--bad); }
.link { border: 0; background: none; color: var(--brand); font: inherit; font-size: .85rem; cursor: pointer; text-decoration: underline; }

/* ---------- Forms ---------- */
label { display: block; margin: 0 0 12px; font-weight: 600; font-size: .93rem; }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; min-height: 44px; padding: 9px 11px;
  border: 1px solid #c3d0d8; border-radius: 9px; background: #fff; color: var(--text); font: inherit; font-weight: 400;
}
input:focus, select:focus, textarea:focus { outline: 3px solid #9fd4e3; border-color: var(--brand); }
fieldset { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px 2px; margin: 0 0 14px; }
legend { font-weight: 700; padding: 0 4px; }
label.check, label.radio { display: flex; align-items: flex-start; gap: 10px; font-weight: 400; margin-bottom: 10px; }
label.radio { display: inline-flex; margin-right: 18px; }
label.check input, label.radio input, .toggle input { width: 22px; height: 22px; min-height: 0; margin: 0; flex: 0 0 22px; accent-color: var(--brand); }
.toggle { display: flex; align-items: center; gap: 10px; font-weight: 600; background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.consent { background: #fffbea; border-color: #f1df9a; }
.gps { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; margin-bottom: 14px; border: 1px solid var(--line); border-radius: 10px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.filters > * { flex: 1 1 180px; margin: 0; }
.vgroup { margin-bottom: 8px; }
.vgroup-h { font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); margin: 6px 0 4px; }
.vrow { display: grid; grid-template-columns: 1fr 160px; gap: 8px; align-items: center; }
.vrow label { margin: 0; }
.vrow input[type=date] { margin: 0; min-height: 38px; padding: 5px 8px; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 6px; overflow-x: auto; margin: 0 0 12px; padding-bottom: 2px; scrollbar-width: none; }
.tab {
  flex: 0 0 auto; padding: 8px 13px; border-radius: 99px; background: #fff; border: 1px solid var(--line);
  color: var(--text); text-decoration: none; font-weight: 600; font-size: .9rem;
}
.tab b { margin-left: 4px; padding: 0 7px; border-radius: 99px; background: #eef2f4; font-size: .8rem; }
.tab.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.tab.active b { background: rgba(255,255,255,.25); }
.tab-overdue:not(.active) b { background: var(--bad-bg); color: var(--bad); }
.tab-week:not(.active) b { background: var(--warn-bg); color: var(--warn); }

/* ---------- Chips & due items ---------- */
.chip { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: .76rem; font-weight: 700; background: #eef2f4; color: var(--muted); white-space: nowrap; margin: 2px 2px 2px 0; }
.chip-overdue { background: var(--bad-bg); color: var(--bad); }
.chip-week { background: var(--warn-bg); color: var(--warn); }
.chip-upcoming { background: var(--up-bg); color: var(--up); }
.chip-later, .chip-ok { background: var(--ok-bg); color: var(--ok); }
.chip-displaced { background: #ede7f6; color: #5e35b1; }
.chip-alert { background: var(--bad); color: #fff; }
.chip-ref-created { background: var(--up-bg); color: var(--up); }
.chip-ref-completed { background: var(--ok-bg); color: var(--ok); }
.chip-ref-missed { background: var(--bad-bg); color: var(--bad); }
.chip-sms-demo { background: var(--warn-bg); color: var(--warn); }
.chip-sms-sent { background: var(--ok-bg); color: var(--ok); }
.chip-sms-failed { background: var(--bad-bg); color: var(--bad); }
.case-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.items { list-style: none; margin: 8px 0 0; padding: 0; }
.item { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; padding: 6px 0 6px 10px; border-left: 3px solid var(--line); margin-bottom: 4px; }
.item-overdue { border-left-color: var(--bad); }
.item-week { border-left-color: #ef8a00; }
.item-upcoming { border-left-color: var(--up); }

/* ---------- Dashboard ---------- */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 12px; }
@media (min-width: 600px) { .tiles { grid-template-columns: repeat(5, 1fr); } }
.tile { display: flex; flex-direction: column; padding: 12px; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); color: var(--text); text-decoration: none; }
.tile b { font-size: 1.6rem; line-height: 1.1; }
.tile span { color: var(--muted); font-size: .84rem; font-weight: 600; }
.tile-bad b { color: var(--bad); }
.tile-warn b { color: var(--warn); }
.tile-ok b { color: var(--ok); }
.map-card { padding: 0; overflow: hidden; }
.map { height: 380px; background: #e5eef2; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; padding: 8px 12px; }
.legend i { display: inline-block; width: 11px; height: 11px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }
.legend .ring-alert { border: 2px solid var(--bad); background: rgba(239,83,80,.3); }
.map-fac { font-size: 18px; line-height: 22px; text-align: center; }
.table-wrap { overflow-x: auto; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 12px; }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.stats td { text-align: right; white-space: nowrap; }
.stats td:first-child, .stats th:first-child { text-align: left; }
.stats thead th { background: #f0f5f7; font-size: .78rem; text-transform: uppercase; letter-spacing: .02em; color: var(--muted); white-space: nowrap; }
.stats tbody th { font-weight: 600; white-space: nowrap; }
table.mini td { padding: 7px 6px; }
.num-bad { color: var(--bad); font-weight: 700; }
.num-warn { color: var(--warn); font-weight: 700; }
.num-ok { color: var(--ok); font-weight: 700; }

/* ---------- SMS ---------- */
.sms-list { display: grid; gap: 12px; }
@media (min-width: 900px) { .sms-list { grid-template-columns: 1fr 1fr; } }
.sms { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.sms-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.bubble { margin: 8px 0; padding: 10px 12px; background: #e9f7ee; border-radius: 4px 14px 14px 14px; white-space: pre-wrap; font-size: .93rem; }

/* ---------- Login, modal, toast ---------- */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; background: linear-gradient(160deg, #0b6e8a, #0f8fb0 60%, #7fd3e6); }
.login-card { width: 100%; max-width: 380px; background: #fff; border-radius: 16px; padding: 24px 20px; text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,.15); }
.login-card form { text-align: left; margin-top: 14px; }
.login-card h1 { margin: 8px 0 4px; }
.modal-back { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(0,0,0,.45); }
.modal { width: 100%; max-width: 420px; background: #fff; border-radius: 14px; padding: 18px; }
.modal p { margin: 0 0 8px; font-size: 1.02rem; }
.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + 18px); z-index: 60; transform: translate(-50%, 20px); opacity: 0;
  max-width: calc(100vw - 32px); padding: 11px 16px; border-radius: 10px; background: #1b2a33; color: #fff; font-weight: 600;
  transition: all .25s; box-shadow: 0 6px 20px rgba(0,0,0,.2);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast-bad { background: var(--bad); }
