:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #1c2430;
  --muted: #6b7785;
  --line: #e2e7ec;
  --brand: #1f7a5a;
  --brand-d: #155f45;
  --crit: #d64545;
  --major: #e08a1e;
  --nc: #4a78c2;
  --ok: #2f9e6f;
  --shadow: 0 1px 3px rgba(20,30,45,.08), 0 1px 2px rgba(20,30,45,.06);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: rgba(0,0,0,.06);
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--brand); color: #fff; z-index: 20; position: sticky; top: 0;
  padding: 12px 16px;
  padding-top: calc(12px + var(--safe-top));
  padding-left: calc(16px + var(--safe-left));
  padding-right: calc(16px + var(--safe-right));
}
.topbar .brand { color: #fff; font-weight: 700; font-size: 17px; }
.topbar nav a { color: #d9efe6; margin-left: 16px; }

.container {
  max-width: 880px; margin: 0 auto;
  padding: 20px 16px calc(150px + var(--safe-bottom));
  padding-left: calc(16px + var(--safe-left));
  padding-right: calc(16px + var(--safe-right));
}

h1 { font-size: 24px; margin: 0 0 6px; }
h2 { font-size: 18px; margin: 26px 0 12px; }
.muted { color: var(--muted); }

.btn-primary {
  display: inline-block; background: var(--brand); color: #fff; border: 0;
  padding: 13px 20px; border-radius: 10px; font-size: 16px; font-weight: 600;
  cursor: pointer; text-align: center; min-height: 48px;
}
.btn-primary:hover { background: var(--brand-d); text-decoration: none; }
.btn-primary:active { background: var(--brand-d); }
.btn-primary.big { padding: 15px 24px; font-size: 17px; }

.home-hero { display: flex; align-items: center; justify-content: space-between; gap: 16px; }

.grid { width: 100%; border-collapse: collapse; background: var(--card); box-shadow: var(--shadow); border-radius: 10px; overflow: hidden; }
.grid th, .grid td { text-align: left; padding: 11px 12px; border-bottom: 1px solid var(--line); }
.grid th { background: #fafbfc; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.grid tr:last-child td { border-bottom: 0; }
.score-cell { font-weight: 700; }

/* form inputs — 16px min prevents iOS focus-zoom */
input[type=text], input[type=date], textarea, select {
  width: 100%; padding: 12px; border: 1px solid var(--line); border-radius: 10px;
  font-size: 16px; font-family: inherit; background: #fff; color: var(--ink);
  -webkit-appearance: none; appearance: none; min-height: 48px;
}
textarea { min-height: 64px; line-height: 1.4; }

/* facility card */
.card { background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; }
.facility-search { position: relative; }
.facility-search label, .audit-date label { display: block; font-weight: 600; margin-bottom: 6px; }
.results {
  list-style: none; margin: 4px 0 0; padding: 4px; position: absolute; z-index: 30; width: 100%;
  background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
  max-height: 50vh; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.results li { padding: 13px 12px; border-radius: 8px; cursor: pointer; }
.results li:active { background: #eef6f2; }
@media (hover: hover) { .results li:hover { background: #eef6f2; } }
.facility-meta { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 16px 0; }
.facility-meta .k { display: block; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.facility-meta .v { font-weight: 600; word-break: break-word; }
.audit-date { max-width: 240px; }

/* questions */
.q { background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 15px; margin-bottom: 12px; border-left: 4px solid transparent; }
.q.is-violation { border-left-color: var(--crit); }
.q.is-na { opacity: .55; }
.q-head { display: flex; align-items: flex-start; gap: 10px; flex-wrap: wrap; }
.q-num { flex: 0 0 26px; height: 26px; line-height: 26px; text-align: center; background: #eef1f4; border-radius: 6px; font-weight: 700; font-size: 13px; color: var(--muted); }
.q-text { flex: 1 1 60%; font-weight: 500; }
.badge { flex: 0 0 auto; font-size: 11px; font-weight: 700; padding: 4px 9px; border-radius: 999px; color: #fff; white-space: nowrap; }
.sev-C { background: var(--crit); }
.sev-M { background: var(--major); }
.sev-NC { background: var(--nc); }

.q-status { display: flex; gap: 8px; margin-top: 14px; }
.seg { flex: 1 1 0; min-width: 0; position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.seg span { display: flex; align-items: center; justify-content: center; text-align: center; padding: 13px 6px; min-height: 50px; border: 1.5px solid var(--line); border-radius: 10px; font-weight: 600; font-size: 14px; color: var(--muted); user-select: none; white-space: nowrap; }
.seg.ok input:checked + span { background: var(--ok); border-color: var(--ok); color: #fff; }
.seg.bad input:checked + span { background: var(--crit); border-color: var(--crit); color: #fff; }
.seg.na input:checked + span { background: #8b95a1; border-color: #8b95a1; color: #fff; }

.q-violation { margin-top: 14px; padding-top: 14px; border-top: 1px dashed var(--line); display: grid; gap: 12px; }
.field { display: block; font-weight: 600; font-size: 14px; }
.field textarea, .field select, .field input { margin-top: 6px; font-weight: 400; }
.picklist { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin: 0; }
.picklist legend { font-weight: 600; font-size: 14px; padding: 0 6px; }
.pick-items { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px; margin-bottom: 8px; }
.pick-items label { font-weight: 400; display: flex; gap: 8px; align-items: center; min-height: 40px; }
.pick-items input { width: auto; min-height: 0; }

.notes { display: block; margin: 16px 0; font-weight: 600; }
.notes textarea { margin-top: 6px; font-weight: 400; }

/* sticky score bar */
.scorebar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
  background: #fff; border-top: 1px solid var(--line); box-shadow: 0 -2px 10px rgba(20,30,45,.08);
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--safe-bottom));
  padding-left: calc(16px + var(--safe-left));
  padding-right: calc(16px + var(--safe-right));
  max-width: 880px; margin: 0 auto;
}
.scorebar .score { font-size: 15px; }
.scorebar .score strong { font-size: 24px; color: var(--crit); }
.counts { display: flex; gap: 6px; flex: 1 1 auto; justify-content: flex-end; }
.chip { font-size: 13px; font-weight: 600; padding: 5px 10px; border-radius: 999px; color: #fff; white-space: nowrap; }
.scorebar .btn-primary { margin-left: auto; }

/* detail */
.detail-head { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.score-box { text-align: center; background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 14px 22px; }
.big-score { font-size: 40px; font-weight: 800; color: var(--crit); line-height: 1; }
.finding { background: var(--card); border-radius: 12px; box-shadow: var(--shadow); padding: 14px; margin-bottom: 10px; border-left: 4px solid var(--crit); }
.finding-head { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 8px; flex-wrap: wrap; }
.finding p { margin: 4px 0; }
.photos { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.photos img { width: 110px; height: 110px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); }
.detail-actions { margin-top: 24px; display: flex; gap: 14px; align-items: center; flex-wrap: wrap; }

/* phone layout */
@media (max-width: 600px) {
  .container { padding-left: calc(12px + var(--safe-left)); padding-right: calc(12px + var(--safe-right)); }
  .facility-meta, .pick-items { grid-template-columns: 1fr; }
  .home-hero { flex-direction: column; align-items: stretch; }
  .home-hero .btn-primary { width: 100%; }
  .seg span { font-size: 13px; padding: 13px 2px; }
  .scorebar .btn-primary { flex: 1 0 100%; margin-left: 0; }
  .scorebar .score .muted { display: none; }
  .score-box { align-self: stretch; }
}

/* dashboard */
.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; background: var(--card); padding: 14px; border-radius: 12px; box-shadow: var(--shadow); margin-bottom: 16px; }
.filters .f { display: flex; flex-direction: column; gap: 4px; min-width: 150px; }
.filters .f label { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.filters .f.end { min-width: 0; margin-left: auto; flex-direction: row; align-items: center; gap: 14px; }
.filters select, .filters input[type=date] { min-height: 44px; }
.clearlink { color: var(--muted); }
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--card); border-radius: 12px; box-shadow: var(--shadow); padding: 16px; text-align: center; }
.kpi-n { font-size: 30px; font-weight: 800; line-height: 1; }
.kpi-n.crit { color: var(--crit); }
.kpi-l { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); margin-top: 6px; }
.charts { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 8px; }
.chart-card { background: var(--card); border-radius: 12px; box-shadow: var(--shadow); padding: 16px; }
.chart-card h2 { margin: 0 0 10px; font-size: 15px; }
.chart-card canvas { max-height: 300px; }
.q-row { cursor: pointer; }
.q-row:active { background: #eef6f2; }
@media (hover: hover) { .q-row:hover { background: #f3f7fa; } }
.drill { margin: 16px 0; }

@media (max-width: 600px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .charts { grid-template-columns: 1fr; }
  .filters .f { flex: 1 1 45%; min-width: 0; }
  .filters .f.end { width: 100%; margin-left: 0; }
}

/* CAR actions, secondary button, flash messages */
.btn-secondary { display: inline-block; background: #fff; color: var(--brand); border: 1.5px solid var(--brand); padding: 12px 18px; border-radius: 10px; font-size: 15px; font-weight: 600; cursor: pointer; min-height: 48px; }
.btn-secondary:hover { background: #eef6f2; }
.btn-secondary:disabled { color: var(--muted); border-color: var(--line); cursor: not-allowed; background: #fff; }
.car-actions { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; background: var(--card); border-radius: 12px; box-shadow: var(--shadow); padding: 14px; margin: 14px 0; }
.inline-form { display: inline; margin: 0; }
.car-status { flex-basis: 100%; }
.messages { list-style: none; padding: 0; margin: 0 0 16px; display: grid; gap: 8px; }
.messages .msg { padding: 12px 14px; border-radius: 10px; font-weight: 600; }
.messages .success { background: #e4f5ec; color: #155f45; border: 1px solid #b9e3cd; }
.messages .error { background: #fde8e8; color: #9b2c2c; border: 1px solid #f5c2c2; }

/* auth */
.login-card { max-width: 380px; margin: 8vh auto 0; background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 26px; }
.login-card h1 { margin-top: 0; }
.login-card .field { margin-bottom: 14px; }
.login-card .btn-primary { width: 100%; }
.login-card .error { background: #fde8e8; color: #9b2c2c; border: 1px solid #f5c2c2; padding: 10px 12px; border-radius: 8px; }
.topbar .who { color: #cdebe0; font-size: 14px; margin-left: 16px; }
.topbar .inline-form { display: inline; }
.linkbtn { background: none; border: 0; color: #d9efe6; font: inherit; cursor: pointer; padding: 0; margin-left: 16px; text-decoration: underline; }

/* photo capture */
.photo-title { display: block; font-weight: 600; font-size: 14px; margin-bottom: 6px; }
.photo-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.photo-btn { display: inline-flex; align-items: center; gap: 6px; padding: 11px 14px; border: 1.5px solid var(--brand); color: var(--brand); background: #fff; border-radius: 10px; font-weight: 600; font-size: 14px; cursor: pointer; min-height: 46px; }
.photo-btn.secondary { border-color: var(--line); color: var(--muted); }
.photo-btn:active { background: #eef6f2; }
.photo-previews { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; }
.thumb { position: relative; width: 84px; height: 84px; }
.thumb img { width: 84px; height: 84px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); }
.thumb-x { position: absolute; top: -8px; right: -8px; width: 24px; height: 24px; border-radius: 50%; border: 0; background: var(--crit); color: #fff; font-size: 14px; line-height: 1; cursor: pointer; padding: 0; }
