:root {
  --ink: #142424;
  --ink-2: #4b5d5d;
  --muted: #7a8a8a;
  --line: #e2e8e6;
  --bg: #f5f7f6;
  --card: #ffffff;
  --brand: #0f3d3e;
  --brand-2: #1d6b6c;
  --accent: #e8a33d;
  --created: #5b6b8c;   --created-bg: #eceff6;
  --assigned: #b06d00;  --assigned-bg: #fdf1dc;
  --completed: #1d7a46; --completed-bg: #e3f4ea;
  --pending: #c62828;   --pending-bg: #fdeaea;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 40, 40, .06), 0 4px 14px rgba(16, 40, 40, .05);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); font-size: 15px; line-height: 1.45; }
a { color: var(--brand-2); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 10px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 14px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--brand); color: #fff;
}
.brand { display: flex; align-items: center; gap: 8px; color: #fff; text-decoration: none; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-mark { color: var(--accent); }
.nav { display: flex; gap: 4px; margin-left: auto; }
.nav a { color: #cfe3e3; text-decoration: none; padding: 7px 11px; border-radius: 8px; font-size: 14px; font-weight: 500; }
.nav a.active, .nav a:hover { background: rgba(255,255,255,.12); color: #fff; }
.menu-btn { background: none; border: 0; padding: 0; cursor: pointer; }
.avatar { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: var(--brand); font-weight: 700; }
.menu { position: absolute; right: 12px; top: 56px; background: #fff; color: var(--ink); border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.18); min-width: 200px; overflow: hidden; }
.menu-head { display: flex; flex-direction: column; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.menu a, .menu button { display: block; width: 100%; text-align: left; padding: 11px 14px; background: none; border: 0; font: inherit; color: var(--ink); text-decoration: none; cursor: pointer; }
.menu a:hover, .menu button:hover { background: var(--bg); }

/* ---------- layout ---------- */
.app { max-width: 920px; margin: 0 auto; padding: 18px 14px 90px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 620px) {
  .grid2 { grid-template-columns: 1fr; }
  .nav { position: fixed; bottom: 0; left: 0; right: 0; margin: 0; justify-content: space-around; background: var(--brand); padding: 6px 6px max(6px, env(safe-area-inset-bottom)); box-shadow: 0 -2px 10px rgba(0,0,0,.12); }
  .nav a { flex: 1; text-align: center; }
  .menu-btn { margin-left: auto; }
}

/* ---------- buttons & forms ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 16px; border-radius: 10px; border: 1px solid var(--line); background: #fff; color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; min-height: 42px; }
.btn:hover { border-color: #c8d2cf; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-2); border-color: var(--brand-2); }
.btn-success { background: var(--completed); border-color: var(--completed); color: #fff; }
.btn-wa { background: #25d366; border-color: #25d366; color: #fff; }
.btn-danger { color: var(--pending); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 11px; min-height: 34px; font-size: 13px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
label { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); margin: 0 0 5px; }
.field { margin-bottom: 14px; }
input, select, textarea {
  width: 100%; padding: 11px 12px; border: 1px solid #d3dbd9; border-radius: 10px; font: inherit; color: var(--ink); background: #fff;
  font-size: 16px; /* stops iOS zoom */
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(29,107,108,.25); border-color: var(--brand-2); }
textarea { min-height: 90px; resize: vertical; }
.pin-input { letter-spacing: .4em; font-size: 20px; text-align: center; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.error { background: var(--pending-bg); color: var(--pending); padding: 10px 12px; border-radius: 10px; font-size: 14px; margin-bottom: 12px; }

/* ---------- auth screens ---------- */
.auth { max-width: 380px; margin: 8vh auto 0; }
.auth .logo { text-align: center; margin-bottom: 20px; }
.auth .logo .mark { display: inline-grid; place-items: center; width: 56px; height: 56px; border-radius: 16px; background: var(--brand); color: var(--accent); font-size: 26px; margin-bottom: 10px; }

/* ---------- badges ---------- */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 700; text-transform: capitalize; white-space: nowrap; }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.b-created { color: var(--created); background: var(--created-bg); }
.b-assigned { color: var(--assigned); background: var(--assigned-bg); }
.b-completed { color: var(--completed); background: var(--completed-bg); }
.b-pending { color: #fff; background: var(--pending); }
.b-pending::before { background: #fff; }
.role { display: inline-block; padding: 2px 8px; border-radius: 6px; font-size: 12px; font-weight: 600; background: #eef2f1; color: var(--ink-2); }
.role-superadmin { background: #0f3d3e; color: #fff; }
.role-admin { background: #d8ecec; color: var(--brand); }

/* ---------- filters ---------- */
.filters { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 10px; scrollbar-width: none; }
.chip { flex: none; padding: 7px 12px; border-radius: 999px; border: 1px solid var(--line); background: #fff; font: inherit; font-size: 13px; font-weight: 600; color: var(--ink-2); cursor: pointer; }
.chip .n { margin-left: 4px; color: var(--muted); font-weight: 500; }
.chip.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.chip.active .n { color: #b9d6d6; }
.chip.chip-pending.has { border-color: var(--pending); color: var(--pending); }
.chip.chip-pending.active { background: var(--pending); border-color: var(--pending); color: #fff; }
.toolbar { display: flex; gap: 8px; margin-bottom: 12px; align-items: center; }
.toolbar input[type=search] { flex: 1; }
.toggle { display: flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600; color: var(--ink-2); white-space: nowrap; margin: 0; }
.toggle input { width: auto; }

/* ---------- visit cards ---------- */
.visit { display: block; text-decoration: none; color: inherit; background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 14px 12px 16px; margin-bottom: 10px; border-left: 4px solid transparent; }
.visit:hover { box-shadow: 0 2px 4px rgba(16,40,40,.08), 0 8px 20px rgba(16,40,40,.08); }
.visit.is-pending { border-left-color: var(--pending); background: linear-gradient(90deg, #fff6f6, #fff 40%); }
.visit.is-completed { opacity: .82; }
.visit-top { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.visit-addr { font-weight: 700; font-size: 15px; }
.visit-meta { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 6px; font-size: 13px; color: var(--ink-2); }
.pending-reason { margin-top: 6px; font-size: 13px; font-weight: 600; color: var(--pending); }
.empty { text-align: center; padding: 50px 20px; color: var(--muted); }
.fab { position: fixed; right: 18px; bottom: 22px; z-index: 15; width: 56px; height: 56px; border-radius: 50%; background: var(--accent); color: var(--brand); font-size: 30px; font-weight: 400; display: grid; place-items: center; text-decoration: none; box-shadow: 0 6px 18px rgba(0,0,0,.2); }
@media (max-width: 620px) { .fab { bottom: calc(76px + env(safe-area-inset-bottom)); } }

/* ---------- visit detail ---------- */
.back { display: inline-block; margin-bottom: 10px; text-decoration: none; font-weight: 600; font-size: 14px; }
.detail-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 4px; }
.info-row { display: flex; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.info-row:first-of-type { border-top: 0; }
.info-row .k { width: 92px; flex: none; color: var(--muted); font-size: 13px; font-weight: 600; padding-top: 1px; }
.info-row .v { flex: 1; white-space: pre-wrap; word-break: break-word; }
.alert-pending { background: var(--pending-bg); color: var(--pending); border-radius: 10px; padding: 10px 12px; font-weight: 600; font-size: 14px; margin: 10px 0 0; }
.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; }
.photo { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: #e9eeed; cursor: zoom-in; }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo .del { position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; border-radius: 50%; border: 0; background: rgba(0,0,0,.55); color: #fff; cursor: pointer; font-size: 13px; }
.photo .by { position: absolute; left: 0; right: 0; bottom: 0; padding: 14px 6px 4px; font-size: 11px; color: #fff; background: linear-gradient(transparent, rgba(0,0,0,.6)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.upload-zone { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 16px; border: 2px dashed #c9d4d1; border-radius: 12px; color: var(--brand-2); font-weight: 600; cursor: pointer; text-align: center; margin-top: 10px; }
.upload-zone:hover { background: #f0f5f4; }
.previews { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 6px; margin-top: 8px; }
.previews img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 12px 18px; font-size: 14px; }
.timeline li::before { content: ""; position: absolute; left: 3px; top: 7px; width: 7px; height: 7px; border-radius: 50%; background: var(--brand-2); }
.timeline li::after { content: ""; position: absolute; left: 6px; top: 16px; bottom: 0; width: 1px; background: var(--line); }
.timeline li:last-child::after { display: none; }

/* ---------- users table ---------- */
.list-row { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); }
.list-row:first-child { border-top: 0; }
.list-row .grow { flex: 1; min-width: 0; }
.list-row.inactive { opacity: .5; }
.tag-locked { color: var(--pending); font-size: 12px; font-weight: 700; }

/* ---------- toast / modal / lightbox ---------- */
.toast { position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 60; background: #172626; color: #fff; padding: 11px 18px; border-radius: 10px; font-size: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.25); max-width: calc(100% - 32px); }
.toast.err { background: var(--pending); }
@media (max-width: 620px) { .toast { bottom: calc(84px + env(safe-area-inset-bottom)); } }
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(10, 25, 25, .45); display: flex; align-items: flex-end; justify-content: center; }
.modal-card { background: #fff; width: 100%; max-width: 460px; max-height: 92vh; overflow: auto; border-radius: 16px 16px 0 0; padding: 20px 18px max(20px, env(safe-area-inset-bottom)); }
@media (min-width: 621px) { .modal { align-items: center; } .modal-card { border-radius: 16px; } }
.lightbox { position: fixed; inset: 0; z-index: 70; background: rgba(0,0,0,.92); display: flex; align-items: center; justify-content: center; }
.lightbox img { max-width: 94vw; max-height: 84vh; object-fit: contain; }
.lightbox button { position: absolute; background: rgba(255,255,255,.12); color: #fff; border: 0; border-radius: 50%; width: 44px; height: 44px; font-size: 26px; cursor: pointer; }
.lb-close { top: max(14px, env(safe-area-inset-top)); right: 14px; font-size: 18px !important; }
.lb-prev { left: 10px; } .lb-next { right: 10px; }
.lb-caption { position: absolute; bottom: max(16px, env(safe-area-inset-bottom)); left: 0; right: 0; text-align: center; color: #ddd; font-size: 13px; }
.spinner { width: 22px; height: 22px; border: 3px solid #d6dfdd; border-top-color: var(--brand-2); border-radius: 50%; animation: spin .8s linear infinite; margin: 40px auto; }
@keyframes spin { to { transform: rotate(360deg); } }
