:root {
  --navy: #16324f;
  --navy-2: #22476e;
  --teal: #1f8a7e;
  --teal-soft: #e3f3f1;
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #1b2430;
  --muted: #5f6b7a;
  --line: #dde3ea;
  --red: #c0392b;
  --red-soft: #fdecea;
  --amber: #b26a00;
  --amber-soft: #fff4e0;
  --green: #1e7d3c;
  --green-soft: #e6f4ea;
  --radius: 10px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-head: Bahnschrift, "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  --mono: Consolas, ui-monospace, Menlo, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 var(--font); -webkit-text-size-adjust: 100%; }
a { color: var(--navy-2); }
h1, h2, h3 { font-family: var(--font-head); font-weight: 600; margin: 0 0 .5rem; letter-spacing: .01em; }
h1 { font-size: 1.45rem; } h2 { font-size: 1.15rem; } h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.pad { padding: 1rem; }
[hidden] { display: none !important; }

/* ---------- header ---------- */
header#top { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 1rem; padding: .55rem 1rem;
  background: var(--navy); color: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.2); flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: .5rem; color: #fff; text-decoration: none; font: 600 1.05rem var(--font-head); }
.logo { width: 24px; height: 24px; background: url(/favicon.svg) center/contain no-repeat; }
#nav { display: flex; gap: .25rem; flex: 1; flex-wrap: wrap; }
#nav a { color: #d6e2ee; text-decoration: none; padding: .35rem .65rem; border-radius: 6px; }
#nav a.on, #nav a:hover { background: rgba(255,255,255,.12); color: #fff; }
#who { display: flex; align-items: center; gap: .5rem; font-size: .9rem; color: #d6e2ee; }
#who button { background: transparent; color: #fff; border: 1px solid rgba(255,255,255,.35); }
#who button:hover { background: rgba(255,255,255,.12); }
.role-chip { font-size: .75rem; padding: .1rem .45rem; border-radius: 99px; background: rgba(255,255,255,.15); text-transform: capitalize; }

main { max-width: 1180px; margin: 0 auto; padding: 1rem; }

/* ---------- controls ---------- */
button, .btn { font: inherit; border: 1px solid var(--line); background: #fff; color: var(--ink); padding: .5rem .9rem; border-radius: 8px;
  cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: .35rem; min-height: 40px; }
button:hover, .btn:hover { border-color: #b9c4d0; }
button.primary, .btn.primary { background: var(--teal); border-color: var(--teal); color: #fff; font-weight: 600; }
button.primary:hover { background: #18766c; }
button.danger { color: var(--red); border-color: #e9b8b2; }
button.link { border: 0; background: none; color: var(--navy-2); padding: .2rem .3rem; min-height: 0; text-decoration: underline; }
button:disabled { opacity: .55; cursor: default; }
input, select, textarea { font: inherit; color: var(--ink); border: 1px solid #c8d1db; border-radius: 8px; padding: .5rem .6rem; background: #fff; width: 100%; min-height: 40px; }
textarea { min-height: 90px; resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--teal); outline-offset: 1px; }
label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .2rem; }
.field { margin-bottom: .8rem; }
.row { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .2rem 1rem; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.card > h2:first-child { margin-bottom: .75rem; }
.crumbs { font-size: .85rem; color: var(--muted); margin-bottom: .35rem; }
.crumbs a { color: var(--muted); }
.page-head { display: flex; gap: .75rem; align-items: flex-start; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head .grow { flex: 1; min-width: 220px; }

/* ---------- badges ---------- */
.badge { display: inline-block; font-size: .75rem; font-weight: 600; padding: .1rem .5rem; border-radius: 99px; background: #eef1f4; color: var(--muted); white-space: nowrap; }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.amber { background: var(--amber-soft); color: var(--amber); }
.badge.green { background: var(--green-soft); color: var(--green); }
.badge.teal { background: var(--teal-soft); color: var(--teal); }

/* ---------- unit cards (home) ---------- */
.client-block { margin-bottom: 1.5rem; }
.site-title { font-size: .9rem; color: var(--muted); margin: .4rem 0 .5rem; text-transform: uppercase; letter-spacing: .05em; }
.units { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: .75rem; }
.unit-card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: .85rem 1rem; text-decoration: none; color: inherit;
  display: flex; flex-direction: column; gap: .35rem; transition: border-color .15s, box-shadow .15s; }
.unit-card:hover { border-color: var(--teal); box-shadow: 0 2px 8px rgba(22,50,79,.08); }
.unit-card .name { font: 600 1.05rem var(--font-head); }
.unit-card .meta { font-size: .85rem; color: var(--muted); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { text-align: left; padding: .45rem .55rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .78rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; white-space: nowrap; background: #fafbfc; }
tr.click { cursor: pointer; }
tr.click:hover td { background: #f6faf9; }
td.num { font-family: var(--mono); white-space: nowrap; }
td.bad, span.bad { color: var(--red); font-weight: 600; }

/* ---------- visit view ---------- */
.readings { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1rem; }
.readings dl { margin: 0; }
.readings .r { display: flex; justify-content: space-between; gap: .75rem; padding: .3rem 0; border-bottom: 1px dashed var(--line); font-size: .92rem; }
.readings .r dt { color: var(--ink); }
.readings .r dd { margin: 0; font-weight: 600; white-space: nowrap; }
.readings .r.flag dd { color: var(--red); }
.readings .r.flag dt::after { content: " ⚠"; color: var(--red); }
.notes { white-space: pre-wrap; }

.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .6rem; }
.photo { position: relative; border-radius: 8px; overflow: hidden; background: #e9edf1; aspect-ratio: 4/3; }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.photo .tag { position: absolute; left: 6px; top: 6px; }
.photo .x { position: absolute; right: 4px; top: 4px; min-height: 0; padding: .15rem .45rem; font-size: .8rem; background: rgba(255,255,255,.9); }
.photo .cap { position: absolute; left: 0; right: 0; bottom: 0; padding: .2rem .4rem; font-size: .75rem; color: #fff; background: linear-gradient(transparent, rgba(0,0,0,.6)); }
.lightbox { position: fixed; inset: 0; background: rgba(10,15,20,.92); z-index: 50; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 6px; }

/* ---------- comments ---------- */
.comment { padding: .6rem .75rem; border-left: 3px solid var(--line); background: #fafbfc; border-radius: 0 8px 8px 0; margin-bottom: .55rem; }
.comment.client { border-left-color: var(--amber); background: #fffaf2; }
.comment .by { font-size: .82rem; color: var(--muted); margin-bottom: .2rem; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.comment .by strong { color: var(--ink); }
.comment .body { white-space: pre-wrap; }

/* ---------- visit form (phone-first) ---------- */
.form-group { margin-bottom: 1rem; }
.form-group h2 { padding-bottom: .4rem; border-bottom: 2px solid var(--teal-soft); }
.q { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .55rem 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.q .ql { flex: 1 1 200px; }
.q .ql small { display: block; color: var(--muted); font-size: .78rem; }
.q.flag .ql { color: var(--red); }
.seg { display: inline-flex; border: 1px solid #c8d1db; border-radius: 9px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; min-width: 56px; min-height: 44px; padding: .4rem .7rem; font-weight: 600; color: var(--muted); }
.seg button + button { border-left: 1px solid #c8d1db; }
.seg button.on[data-v="Y"] { background: var(--green); color: #fff; }
.seg button.on[data-v="N"] { background: #5f6b7a; color: #fff; }
.seg button.on[data-v="NA"] { background: #9aa5b1; color: #fff; }
.seg button.on.bad { background: var(--red) !important; color: #fff; }
.q input.numin { width: 130px; text-align: right; font-family: var(--mono); font-size: 1.05rem; min-height: 44px; }
.q input.numin.bad { border-color: var(--red); color: var(--red); background: var(--red-soft); }
.sticky-save { position: sticky; bottom: 0; background: linear-gradient(transparent, var(--bg) 30%); padding: 1rem 0 .5rem; display: flex; gap: .6rem; justify-content: flex-end; }
.draft-note { background: var(--amber-soft); color: var(--amber); padding: .6rem .8rem; border-radius: 8px; margin-bottom: 1rem; display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }

/* ---------- chart ---------- */
.chart { width: 100%; height: auto; display: block; }
.chart .axis { stroke: #c8d1db; stroke-width: 1; }
.chart .grid { stroke: #eef1f4; stroke-width: 1; }
.chart .max { stroke: var(--red); stroke-dasharray: 4 4; stroke-width: 1; }
.chart text { font: 11px var(--font); fill: var(--muted); }
.chart .l1 { stroke: var(--teal); fill: none; stroke-width: 2; }
.chart .l2 { stroke: var(--navy-2); fill: none; stroke-width: 2; }
.chart .d1 { fill: var(--teal); } .chart .d2 { fill: var(--navy-2); }
.legend { display: flex; gap: 1rem; font-size: .82rem; color: var(--muted); flex-wrap: wrap; }
.legend i { display: inline-block; width: 14px; height: 3px; vertical-align: middle; margin-right: .3rem; }

/* ---------- login ---------- */
.login { max-width: 380px; margin: 8vh auto 0; }
.login .card { padding: 1.5rem; }
.login .logo-lg { width: 48px; height: 48px; background: url(/favicon.svg) center/contain no-repeat; margin-bottom: .75rem; }
.err { background: var(--red-soft); color: var(--red); padding: .55rem .75rem; border-radius: 8px; margin-bottom: .8rem; font-size: .9rem; }
.ok-box { background: var(--green-soft); color: var(--green); padding: .7rem .8rem; border-radius: 8px; margin: .8rem 0; word-break: break-all; }

.chips { display: flex; gap: .35rem; flex-wrap: wrap; }
.chips button { min-height: 32px; padding: .2rem .7rem; border-radius: 99px; font-size: .85rem; }
.chips button.on { background: var(--navy); color: #fff; border-color: var(--navy); }

#toast { position: fixed; left: 50%; bottom: 1.2rem; transform: translateX(-50%); background: var(--ink); color: #fff; padding: .6rem 1rem; border-radius: 8px;
  font-size: .92rem; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 60; max-width: 90vw; }
#toast.show { opacity: 1; }
#toast.bad { background: var(--red); }

.log-old { color: var(--red); text-decoration: line-through; word-break: break-word; }
.log-new { color: var(--green); word-break: break-word; }

@media (max-width: 640px) {
  main { padding: .75rem; }
  header#top { display: grid; grid-template-columns: 1fr auto; gap: .35rem .5rem; padding: .5rem .75rem .35rem; }
  #who { order: 2; }
  #nav { order: 3; grid-column: 1 / -1; flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -.25rem; }
  #nav a { white-space: nowrap; }
  #who .uname, #who .role-chip { display: none; }
  #who .btn, #who button { min-height: 32px; padding: .25rem .6rem; font-size: .85rem; }
  .card { padding: .85rem; }
  .hide-sm { display: none; }
}
@media print {
  header#top, button, .no-print { display: none !important; }
  body { background: #fff; }
  .card { border: 0; padding: 0; }
}
