:root {
  --bg: #0f1720;
  --surface: #17212b;
  --surface-2: #1f2b38;
  --border: #2a3742;
  --text: #e8edf2;
  --text-dim: #93a2b1;
  --primary: #1a5fb4;
  --primary-dim: #14488a;
  --success: #1e7e34;
  --danger: #c0392b;
  --warn: #b8860b;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button, input, select, textarea { font-family: inherit; font-size: 1rem; }

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Top bar ---------- */
.topbar {
  padding: calc(12px + var(--safe-top)) 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.topbar h1 { font-size: 1.05rem; margin: 0; font-weight: 700; }
.topbar .user-chip { font-size: 0.8rem; color: var(--text-dim); }
.icon-btn {
  background: transparent; border: none; color: var(--text);
  font-size: 1.3rem; padding: 6px 10px; border-radius: 8px; cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }

/* ---------- Main content ---------- */
.view { flex: 1; overflow-y: auto; padding: 12px 12px calc(16px + var(--safe-bottom)); }
.view.hidden { display: none; }

/* ---------- Bottom nav ---------- */
.bottom-nav {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  flex-shrink: 0;
}
.bottom-nav button {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 10px 4px 8px;
  font-size: 0.72rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}
.bottom-nav button .nav-icon { font-size: 1.3rem; }
.bottom-nav button.active { color: var(--primary); }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.card h3 { margin: 0 0 6px; font-size: 1rem; }
.card .meta { font-size: 0.78rem; color: var(--text-dim); margin-bottom: 8px; }
.card-stale { filter: grayscale(1); opacity: 0.75; }

.card-top-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 4px;
}
.card-top-tags { display: flex; align-items: center; flex-wrap: wrap; }
.time-since-tag {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-dim);
  white-space: nowrap;
  margin-left: auto;
  padding-top: 3px;
}

.status-tag {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 3px 8px;
  border-radius: 999px;
}
.status-open { background: #234; color: #7fb3ff; }
.status-assigned { background: #2a3300; color: #d9c200; }
.status-needs_additional { background: #3a1a00; color: #ff9d42; }
.status-completed { background: #123a1a; color: #4fd07d; }
.status-cancelled { background: #3a1212; color: #ff7373; }
.status-unable_pending { background: #3a2e00; color: #f5c518; }
.status-unable_closed { background: #2a2a2a; color: #bbb; }
.status-expired { background: #2a2a2a; color: #999; }

.area-tag {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  padding: 3px 10px;
  border-radius: 999px;
  margin-left: 6px;
  background: #fce588;
  color: #4a3c00;
}

.emergency-tag {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 3px 10px;
  border-radius: 999px;
  margin-left: 6px;
  background: var(--danger);
  color: #fff;
  animation: emergency-flash 1s ease-in-out infinite;
}
@keyframes emergency-flash {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}
@media (prefers-reduced-motion: reduce) {
  .emergency-tag { animation: none; }
}

.additional-responder-line {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--warn);
  margin-top: 6px;
}

/* ---------- Emergency toggle switch ---------- */
.emergency-toggle-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 12px;
  border-radius: 10px;
  margin-bottom: 14px;
}
.emergency-toggle-row .toggle-label { font-weight: 700; color: var(--text); letter-spacing: 0.02em; }
.toggle-switch { position: relative; display: inline-block; width: 50px; height: 28px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.toggle-slider {
  position: absolute; inset: 0; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; transition: background 0.15s, border-color 0.15s;
}
.toggle-slider::before {
  content: "";
  position: absolute; height: 20px; width: 20px; left: 3px; top: 3px;
  background: #fff; border-radius: 50%; transition: transform 0.15s;
}
.toggle-switch input:checked + .toggle-slider { background: var(--danger); border-color: var(--danger); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(22px); }

.field-row { display: flex; justify-content: space-between; gap: 8px; font-size: 0.9rem; padding: 3px 0; }
.field-row .k { color: var(--text-dim); }
/* Mobile Safari/Chrome apply their own default blue styling to tel: links regardless of
   author CSS unless overridden explicitly at this specificity, with -webkit-text-fill-color
   set too (color alone isn't always enough on iOS). */
a.phone-link,
a.phone-link:link,
a.phone-link:visited,
a.phone-link:active {
  color: var(--text) !important;
  -webkit-text-fill-color: var(--text);
  font-weight: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.phone-link:active { opacity: 0.7; }

.notes-log { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.note-entry { margin-bottom: 8px; font-size: 0.85rem; }
.note-entry:last-child { margin-bottom: 0; }
.note-meta { display: block; color: var(--text-dim); font-size: 0.72rem; margin-bottom: 2px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-block;
  border: none;
  border-radius: 10px;
  padding: 12px 16px;
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  width: 100%;
  margin-top: 8px;
  color: #fff;
}
.btn-primary { background: var(--primary); }
.btn-success { background: var(--success); }
.btn-warn { background: var(--warn); }
.btn-danger { background: var(--danger); }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-yellow { background: #f5c518; color: #3a2e00; }
.btn:disabled { opacity: 0.5; }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { margin-top: 8px; }

/* ---------- Forms ---------- */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 0.85rem; color: var(--text-dim); margin-bottom: 5px; }
.field input[type=text], .field input[type=number], .field input[type=tel],
.field input[type=password], .field input[type=email], .field select, .field textarea {
  width: 100%;
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
}
.field textarea { min-height: 80px; resize: vertical; }
.field.checkbox { display: flex; align-items: center; gap: 8px; }
.field.checkbox input { width: 18px; height: 18px; }

/* ---------- Login ---------- */
.login-wrap {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 24px;
  max-width: 420px;
  margin: 0 auto;
  width: 100%;
}
.login-wrap h1 { text-align: center; margin-bottom: 24px; }
.error-box { background: #3a1212; color: #ff9c9c; padding: 10px 12px; border-radius: 10px; margin-bottom: 12px; font-size: 0.9rem; }
.notice-box { background: #123a1a; color: #8fe0a8; padding: 10px 12px; border-radius: 10px; margin-bottom: 12px; font-size: 0.9rem; }

.password-input-wrap { position: relative; }
.password-input-wrap input { padding-right: 42px !important; }
.password-toggle {
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
  background: none;
  border: none;
  padding: 4px;
  color: var(--text-dim);
  cursor: pointer;
  display: flex;
}
.password-toggle:hover { color: var(--text); }

.login-links { text-align: center; margin-top: 14px; }
.login-links a { color: var(--primary); font-size: 0.85rem; text-decoration: none; }
.login-links a:hover { text-decoration: underline; }

/* ---------- Chat: conversation list ---------- */
.chat-list-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}
.chat-list-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 4px; border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.chat-list-row:active { background: var(--surface-2); }
.chat-avatar {
  flex-shrink: 0; width: 44px; height: 44px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: 0.9rem;
}
.chat-avatar-icon { background: var(--surface-2); font-size: 1.3rem; }
.chat-list-row-body { flex: 1; min-width: 0; }
.chat-list-row-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.chat-list-name { font-weight: 600; font-size: 0.95rem; }
.chat-list-time { font-size: 0.72rem; color: var(--text-dim); flex-shrink: 0; }
.chat-list-row-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 2px; }
.chat-list-preview {
  font-size: 0.82rem; color: var(--text-dim); white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; flex: 1;
}
.chat-unread-badge {
  flex-shrink: 0; background: var(--primary); color: #fff; font-size: 0.68rem; font-weight: 700;
  min-width: 18px; height: 18px; border-radius: 999px; display: flex; align-items: center;
  justify-content: center; padding: 0 5px;
}

/* ---------- Chat: thread view ---------- */
/* The .view element itself is the scrolling region (see its own rule) rather than a flex
   column, so the header/compose here stick to .view's own scroll via position:sticky, and
   the message list just flows between them instead of being an independently-scrolling pane. */
.chat-thread-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; margin: -12px -12px 10px; padding: calc(10px + var(--safe-top)) 12px 10px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: -12px; z-index: 5;
}
.chat-thread-title { font-weight: 700; flex: 1; text-align: center; }
.chat-thread-messages {
  display: flex; flex-direction: column; gap: 6px;
  padding: 4px 0 8px; min-height: calc(100% - 60px);
}
.chat-bubble-row { display: flex; }
.chat-bubble-row.mine { justify-content: flex-end; }
.chat-bubble {
  max-width: 78%; padding: 8px 12px; border-radius: 14px;
  background: var(--surface); border: 1px solid var(--border);
}
.chat-bubble.mine { background: var(--primary-dim); border-color: transparent; }
.chat-bubble-sender { font-size: 0.72rem; font-weight: 700; color: var(--primary); margin-bottom: 2px; }
.chat-bubble-text { font-size: 0.92rem; white-space: pre-wrap; word-break: break-word; }
.chat-bubble-time { font-size: 0.65rem; color: var(--text-dim); text-align: right; margin-top: 3px; }

.chat-compose { display: flex; gap: 8px; position: sticky; bottom: 0; background: var(--bg); padding-top: 8px; }
.chat-compose textarea { flex: 1; min-height: 42px; max-height: 100px; }
.chat-compose .btn { width: auto; margin-top: 0; }

/* ---------- Admin tabs ---------- */
.subtabs { display: flex; gap: 6px; margin-bottom: 12px; overflow-x: auto; }
.subtabs button {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text-dim);
  padding: 8px 12px; border-radius: 999px; font-size: 0.82rem; white-space: nowrap; cursor: pointer;
}
.subtabs button.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.list-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 0; border-bottom: 1px solid var(--border);
}
.list-row:last-child { border-bottom: none; }
.list-row .name { font-weight: 600; }
.list-row .sub { font-size: 0.78rem; color: var(--text-dim); }
.pill { font-size: 0.68rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-dim); }
.pill.on { background: var(--success); color: #fff; }

.dyn-field-row { display: flex; gap: 6px; align-items: flex-end; margin-bottom: 8px; }
.dyn-field-row .field { margin-bottom: 0; flex: 1; }
.dyn-field-row .remove-field { background: var(--danger); color: #fff; border: none; border-radius: 8px; padding: 10px 12px; cursor: pointer; }

.empty-state { text-align: center; color: var(--text-dim); padding: 40px 20px; font-size: 0.9rem; }

.spinner-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center; z-index: 999;
}
.spinner { width: 32px; height: 32px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.confirm-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 1100; padding: 20px;
}
.confirm-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; max-width: 360px; width: 100%;
}
.confirm-message { font-size: 0.92rem; line-height: 1.45; white-space: pre-wrap; }
.confirm-actions { display: flex; gap: 8px; margin-top: 18px; }
.confirm-actions-stacked { flex-direction: column; }

.settings-qr-card { display: flex; justify-content: center; }
.settings-qr-image { max-width: 220px; width: 100%; border-radius: 10px; }
.confirm-actions .btn { margin-top: 0; }

.address-autocomplete-wrap { position: relative; }
.address-suggestions {
  display: none;
  position: absolute; left: 0; right: 0; top: 100%;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  margin-top: 4px; max-height: 220px; overflow-y: auto; z-index: 50;
  box-shadow: 0 8px 20px rgba(0,0,0,0.35);
}
.address-suggestions.open { display: block; }
.address-suggestion-row {
  padding: 10px 12px; font-size: 0.85rem; border-bottom: 1px solid var(--border); cursor: pointer;
}
.address-suggestion-row:last-child { border-bottom: none; }
.address-suggestion-row:active { background: var(--surface); }

.toast {
  position: fixed; left: 50%; bottom: calc(80px + var(--safe-bottom)); transform: translateX(-50%);
  background: var(--surface-2); color: var(--text); padding: 10px 16px; border-radius: 10px;
  font-size: 0.85rem; z-index: 1000; border: 1px solid var(--border);
}

/* ---------- Reports ---------- */
.report-stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
}
.report-stat { text-align: center; }
.report-stat-value { font-size: 1.4rem; font-weight: 700; }
.report-stat-label { font-size: 0.72rem; color: var(--text-dim); margin-top: 2px; }

.table-scroll { overflow-x: auto; margin-bottom: 12px; }
.report-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.8rem;
  white-space: nowrap;
}
.report-table th, .report-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.report-table th {
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.report-table tbody tr:last-child td { border-bottom: none; }
