:root {
  --green: #1f4d2e;
  --green-light: #2d6b41;
  --green-pale: #eef3ef;
  --ink: #14181a;
  --muted: #6b7280;
  --line: #dde1de;
  --bg: #f4f6f4;
  --card: #ffffff;
  --danger: #b3261e;
  --warn: #9a6700;
  --ok: #1f7a3d;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* An explicit `display` in a rule below outranks the UA stylesheet's
   `[hidden] { display: none }`, so state the intent once, loudly. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  overscroll-behavior-y: none;
}

/* ------------------------------------------------------------- app bar */

#appbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(var(--safe-top) + 10px) 12px 10px;
  background: var(--green);
  color: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.18);
}

.bar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#barTitle { font-size: 17px; font-weight: 650; letter-spacing: -.01em; }
#barSub { font-size: 11.5px; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.icon-btn {
  background: rgba(255,255,255,.14);
  border: none;
  color: #fff;
  width: 34px; height: 34px;
  border-radius: 9px;
  font-size: 19px;
  line-height: 1;
  display: grid; place-items: center;
  cursor: pointer;
  flex: none;
}
.icon-btn:active { background: rgba(255,255,255,.28); }

/* ------------------------------------------------------------- banners */

#offlineBanner, #queueBanner {
  padding: 8px 14px;
  font-size: 13px;
  text-align: center;
  background: #fff4d6;
  color: var(--warn);
  border-bottom: 1px solid #f0e0b0;
}
#queueBanner { background: var(--green-pale); color: var(--green); border-color: #cfe0d4; }
#queueBanner button {
  margin-left: 8px; border: none; background: var(--green); color: #fff;
  padding: 4px 10px; border-radius: 6px; font-size: 12px; font-weight: 600;
}

/* -------------------------------------------------------------- layout */

main {
  padding: 12px 12px calc(78px + var(--safe-bottom));
  max-width: 900px;
  margin: 0 auto;
}

#tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 40;
  display: flex;
  background: rgba(255,255,255,.94);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
}
#tabbar button {
  flex: 1;
  border: none; background: none;
  padding: 7px 0 8px;
  font-size: 10.5px; font-weight: 600;
  color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  cursor: pointer;
}
#tabbar button span { font-size: 21px; line-height: 1; }
#tabbar button.active { color: var(--green); }

/* --------------------------------------------------------------- cards */

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 13px;
  margin-bottom: 10px;
  overflow: hidden;
}

.activity-card {
  display: flex; align-items: center; gap: 13px;
  padding: 15px 14px;
  cursor: pointer;
  width: 100%;
  border: none; background: var(--card);
  text-align: left;
  font: inherit; color: inherit;
}
.activity-card:active { background: var(--green-pale); }
.activity-card .emoji { font-size: 27px; flex: none; width: 36px; text-align: center; }
.activity-card .meta { flex: 1; min-width: 0; }
.activity-card .name { font-weight: 640; font-size: 15.5px; }
.activity-card .desc { font-size: 12.5px; color: var(--muted); margin-top: 1px; }
.activity-card .chev { color: #c3c8c4; font-size: 22px; flex: none; }

.section-label {
  font-size: 11.5px; font-weight: 700; letter-spacing: .07em;
  text-transform: uppercase; color: var(--muted);
  margin: 18px 4px 7px;
}
.section-label:first-child { margin-top: 4px; }

/* ------------------------------------------------------------ sections */

.sec { background: var(--card); border: 1px solid var(--line); border-radius: 13px; margin-bottom: 10px; }
.sec-head {
  display: flex; align-items: center; gap: 9px;
  padding: 13px 14px;
  cursor: pointer; user-select: none;
}
.sec-head .caret { color: var(--muted); font-size: 13px; transition: transform .16s; flex: none; width: 12px; }
.sec.open .sec-head .caret { transform: rotate(90deg); }
.sec-head .t { flex: 1; font-weight: 640; font-size: 15px; }
.sec-head .badge {
  font-size: 11px; font-weight: 700;
  background: var(--green-pale); color: var(--green);
  padding: 2px 8px; border-radius: 20px;
}
.sec-head .badge.req { background: #fdecea; color: var(--danger); }
.sec-body { display: none; padding: 0 14px 14px; }
.sec.open .sec-body { display: block; }
.sec-help {
  font-size: 12px; color: var(--muted); font-style: italic;
  margin: -2px 0 12px; padding: 8px 10px;
  background: #fafbfa; border-left: 2px solid var(--line); border-radius: 0 6px 6px 0;
}

/* -------------------------------------------------------------- fields */

.field { margin-bottom: 13px; }
.field > label {
  display: block;
  font-size: 12.5px; font-weight: 620; color: #3a4046;
  margin-bottom: 5px;
}
.field > label .req { color: var(--danger); margin-left: 2px; }

input[type=text], input[type=number], input[type=date], input[type=time],
input[type=tel], input[type=email], input[type=password], select, textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 16px; /* 16px keeps iOS from zooming on focus */
  font-family: inherit;
  background: #fff;
  color: var(--ink);
  appearance: none;
}
textarea { resize: vertical; min-height: 84px; line-height: 1.45; }
select {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath fill='%236b7280' d='M6 8.5 2 4.5h8z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 11px center;
  background-size: 12px;
  padding-right: 32px;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--green-light);
  box-shadow: 0 0 0 3px rgba(45,107,65,.13);
}
input.invalid, select.invalid, textarea.invalid { border-color: var(--danger); }

/* Brief flash so it's obvious which fields the app filled in for you. */
@keyframes autofill-flash {
  from { background: #d8ecdf; border-color: var(--green-light); }
  to   { background: #fff;    border-color: var(--line); }
}
input.autofilled, select.autofilled, textarea.autofilled {
  animation: autofill-flash 1.6s ease-out;
}

.check {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; cursor: pointer;
}
.check input { width: 21px; height: 21px; accent-color: var(--green); flex: none; }
.check span { font-size: 14.5px; }

.chips { display: flex; flex-wrap: wrap; gap: 7px; }
.chip {
  padding: 7px 13px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: #fff;
  font-size: 13.5px;
  cursor: pointer;
  user-select: none;
}
.chip.on { background: var(--green); color: #fff; border-color: var(--green); font-weight: 600; }

/* --------------------------------------------------------------- table */

.tbl-rows { display: flex; flex-direction: column; gap: 9px; }
.tbl-row {
  border: 1px solid var(--line);
  border-radius: 11px;
  overflow: hidden;
  background: #fcfdfc;
}
.tbl-row-head {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 11px;
  background: var(--green-pale);
  font-size: 12.5px; font-weight: 660; color: var(--green);
}
.tbl-row-head .n { flex: 1; }
.tbl-row-head button {
  border: none; background: none; color: var(--danger);
  font-size: 17px; padding: 0 4px; cursor: pointer; line-height: 1;
}
.tbl-row-body { padding: 11px; display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.tbl-row-body .field { margin: 0; }
.tbl-row-body .field.wide { grid-column: 1 / -1; }
.tbl-row-body input, .tbl-row-body select { padding: 9px 10px; font-size: 15px; }
.tbl-row-body select { background-position: right 9px center; }
.tbl-row-body > .field > label { font-size: 11.5px; margin-bottom: 3px; }

.btn-add {
  width: 100%;
  margin-top: 9px;
  padding: 11px;
  border: 1.5px dashed #c2cbc5;
  border-radius: 10px;
  background: #fff;
  color: var(--green);
  font-size: 14px; font-weight: 640;
  cursor: pointer;
}
.btn-add:active { background: var(--green-pale); }
.dup-btn { color: var(--muted) !important; font-size: 15px !important; }

/* -------------------------------------------------------------- photos */

.photo-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.photo-cell { position: relative; aspect-ratio: 4/3; border-radius: 9px; overflow: hidden; border: 1px solid var(--line); background: #eee; }
.photo-cell img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-cell .del {
  position: absolute; top: 3px; right: 3px;
  width: 22px; height: 22px; border-radius: 50%;
  background: rgba(0,0,0,.6); color: #fff; border: none;
  font-size: 14px; line-height: 1; cursor: pointer;
  display: grid; place-items: center;
}
.photo-cell .cap {
  position: absolute; bottom: 0; left: 0; right: 0;
  background: linear-gradient(transparent, rgba(0,0,0,.72));
  color: #fff; font-size: 10px; padding: 12px 5px 4px;
  border: none; width: 100%; text-align: left;
}
.photo-add {
  aspect-ratio: 4/3;
  border: 1.5px dashed #c2cbc5; border-radius: 9px;
  background: #fff; color: var(--green);
  font-size: 25px; cursor: pointer;
  display: grid; place-items: center;
}

/* ----------------------------------------------------------- signature */

.sig-block { margin-bottom: 16px; }
.sig-canvas-wrap { position: relative; border: 1px solid var(--line); border-radius: 10px; background: #fff; overflow: hidden; }
.sig-canvas-wrap canvas { display: block; width: 100%; height: 150px; touch-action: none; }
.sig-canvas-wrap .hint {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: #b8bfba; font-size: 13.5px; pointer-events: none;
}
.sig-canvas-wrap.signed .hint { display: none; }
.sig-actions { display: flex; gap: 8px; margin-top: 7px; align-items: center; }
.sig-actions button {
  padding: 7px 13px; border: 1px solid var(--line); border-radius: 8px;
  background: #fff; font-size: 13px; cursor: pointer; color: var(--ink);
}
.sig-unavailable { flex: 1; display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--muted); justify-content: flex-end; }
.sig-unavailable input { width: 19px; height: 19px; accent-color: var(--green); }
.sig-disabled { opacity: .42; pointer-events: none; }

/* ----------------------------------------------------- computed panel */

.computed { font-variant-numeric: tabular-nums; }

.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 7px; margin-bottom: 11px; }
.stat {
  background: var(--green-pale); border-radius: 10px;
  padding: 9px 6px; text-align: center;
}
.stat-v { font-size: 19px; font-weight: 700; color: var(--green); line-height: 1.1; }
.stat-l { font-size: 10.5px; font-weight: 650; color: #4b5563; margin-top: 2px; }
.stat-s { font-size: 9.5px; color: var(--muted); }

.tract-total {
  background: var(--green); color: #fff;
  border-radius: 10px; padding: 11px 13px; margin-bottom: 11px;
}
.tract-total strong { font-size: 18px; }
.tract-total .err { font-size: 11.5px; opacity: .85; margin-top: 3px; }

table.ctbl {
  width: 100%; border-collapse: collapse;
  font-size: 12px; margin-bottom: 11px;
}
table.ctbl th {
  text-align: right; font-size: 10px; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted); font-weight: 700;
  padding: 4px 5px; border-bottom: 1px solid var(--line);
}
table.ctbl th:first-child { text-align: left; }
table.ctbl td {
  text-align: right; padding: 5px; border-bottom: 1px solid #f1f3f1;
}
table.ctbl td:first-child { text-align: left; font-weight: 600; }
table.ctbl tr:last-child td { border-bottom: none; }

.reliability {
  font-size: 12px; font-weight: 600; line-height: 1.4;
  padding: 9px 11px; border-radius: 8px; border-left: 3px solid;
  margin-bottom: 9px;
}
.reliability.good         { background: #e4f0e8; color: #1f5c33; border-color: #1f5c33; }
.reliability.fair         { background: #fdf3dc; color: #7a5a12; border-color: #b98b1c; }
.reliability.poor         { background: #fbe6e4; color: #8e2b23; border-color: #b3261e; }
.reliability.insufficient { background: #eef0ee; color: #5b6560; border-color: #9aa39d; }

.basis { font-size: 10.5px; color: var(--muted); font-style: italic; }

/* --------------------------------------------------------------- misc */

.gps-box { display: flex; align-items: center; gap: 11px; }
.gps-box .val { flex: 1; font-size: 13.5px; font-variant-numeric: tabular-nums; color: var(--muted); }
.gps-box .val strong { color: var(--ink); display: block; font-size: 14px; }

.btn {
  display: block; width: 100%;
  padding: 15px;
  border: none; border-radius: 12px;
  background: var(--green); color: #fff;
  font-size: 16.5px; font-weight: 680;
  cursor: pointer;
  margin-top: 10px;
}
.btn:active { background: var(--green-light); }
.btn[disabled] { opacity: .5; }
.btn.secondary { background: #fff; color: var(--green); border: 1.5px solid var(--line); }
.btn.danger { background: #fff; color: var(--danger); border: 1.5px solid #f0d4d1; }
.btn-row { display: flex; gap: 9px; }
.btn-row .btn { margin-top: 0; }

.recip-list { display: flex; flex-direction: column; gap: 7px; margin-bottom: 9px; }
.recip {
  display: flex; align-items: center; gap: 9px;
  padding: 10px 12px;
  background: var(--green-pale); border-radius: 9px;
  font-size: 14px;
}
.recip .addr { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recip button { border: none; background: none; color: var(--danger); font-size: 17px; cursor: pointer; padding: 0 3px; }

.hist-item {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  background: var(--card);
}
.hist-item:last-child { border-bottom: none; }
.hist-item:active { background: var(--green-pale); }
.hist-item .emoji { font-size: 22px; flex: none; }
.hist-item .info { flex: 1; min-width: 0; }
.hist-item .ref { font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.hist-item .ttl { font-weight: 620; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hist-item .sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.status-dot.ok { background: var(--ok); }
.status-dot.fail { background: var(--danger); }
.status-dot.none { background: #c8cec9; }
.status-dot.queued { background: var(--warn); }

.sample-tag {
  display: inline-block;
  background: #fdecea; color: #b3261e;
  font-size: 9px; font-weight: 800; letter-spacing: .06em;
  padding: 1px 5px; border-radius: 4px;
  margin-right: 6px; vertical-align: 1px;
}

.empty { text-align: center; padding: 46px 20px; color: var(--muted); }
.empty .big { font-size: 42px; margin-bottom: 10px; }
.empty .msg { font-size: 14.5px; }

.hint-text { font-size: 12.5px; color: var(--muted); margin: 6px 2px 0; }
.hint-text a { color: var(--green); }

/* -------------------------------------------------------------- toast */

#toast {
  position: fixed;
  left: 50%; transform: translateX(-50%);
  bottom: calc(84px + var(--safe-bottom));
  z-index: 90;
  background: #16281d; color: #fff;
  padding: 11px 18px; border-radius: 22px;
  font-size: 14px; font-weight: 550;
  max-width: 88vw; text-align: center;
  box-shadow: 0 4px 16px rgba(0,0,0,.28);
}
#toast.err { background: var(--danger); }

/* -------------------------------------------------------------- modal */

#modal {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.42);
  display: grid; place-items: center;
  padding: 20px;
}
.modal-card {
  background: #fff; border-radius: 15px;
  width: 100%; max-width: 420px;
  max-height: 82vh; display: flex; flex-direction: column;
  overflow: hidden;
}
.modal-title { padding: 17px 18px 4px; font-size: 17px; font-weight: 680; }
.modal-body { padding: 6px 18px 14px; overflow-y: auto; font-size: 14.5px; color: #3a4046; }
.modal-actions { display: flex; gap: 9px; padding: 12px 18px calc(16px + var(--safe-bottom)); border-top: 1px solid var(--line); }
.modal-actions .btn { margin: 0; padding: 12px; font-size: 15px; }

.spin {
  display: inline-block; width: 15px; height: 15px;
  border: 2px solid rgba(255,255,255,.35); border-top-color: #fff;
  border-radius: 50%; animation: spin .7s linear infinite;
  vertical-align: -2px; margin-right: 7px;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 640px) {
  .tbl-row-body { grid-template-columns: repeat(3, 1fr); }
  .photo-grid { grid-template-columns: repeat(4, 1fr); }
}
