/* Mobile-first, high contrast: this is read outdoors, in sunlight, one-handed. */

:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --text: #14181c;
  --muted: #5f6b74;
  --line: #dfe4e2;
  --brand: #14532d;
  --ok: #15803d;
  --warn: #b45309;
  --danger: #b91c1c;
  --radius: 12px;
  --tap: 48px; /* minimum touch target */
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10140f;
    --surface: #1a201b;
    --text: #eef2ee;
    --muted: #9aa8a0;
    --line: #2c352e;
    --brand: #4ade80;
    --ok: #4ade80;
    --warn: #fbbf24;
    --danger: #f87171;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 16px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: env(safe-area-inset-bottom);
}

body.centered {
  display: grid;
  place-items: center;
  min-height: 100dvh;
  padding: 16px;
}

/* --- top bar --- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: max(env(safe-area-inset-top), 8px) 12px 8px;
  background: var(--brand);
  color: #fff;
}
.topbar h1 { flex: 1; margin: 0; font-size: 19px; }
.icon-btn {
  min-width: var(--tap);
  min-height: var(--tap);
  font-size: 22px;
  background: transparent;
  border: 0;
  color: inherit;
  cursor: pointer;
}
.conn { font-size: 26px; line-height: 1; color: #9ca3af; }
.conn.live { color: #86efac; }

main { padding: 12px; max-width: 720px; margin: 0 auto; }
h2 { font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 22px 0 8px; }

/* --- trap list --- */
.trap-list, .cameras, .events { list-style: none; margin: 0; padding: 0; }
.trap-list li {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 10px;
}
.trap-list button {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 64px;
  padding: 12px 14px;
  background: none;
  border: 0;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.trap-name { flex: 1; font-weight: 600; }
.trap-sub { display: block; font-weight: 400; font-size: 14px; color: var(--muted); }

.dot { width: 12px; height: 12px; border-radius: 50%; background: #9ca3af; flex: none; }
.dot.online { background: var(--ok); }
.dot.offline { background: var(--danger); }
.dot.stale { background: var(--warn); }

/* --- status card --- */
.status-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}
.status-card .headline { font-size: 24px; font-weight: 700; margin: 0 0 4px; }
.status-card .headline.triggered { color: var(--danger); }
.status-card dl { display: grid; grid-template-columns: minmax(0, auto) minmax(0, 1fr); gap: 6px 12px; margin: 12px 0 0; }
.status-card dt { color: var(--muted); }
.status-card dd { margin: 0; text-align: right; overflow-wrap: anywhere; }
.status-card .headline { overflow-wrap: anywhere; }

/* --- buttons --- */
.actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.btn {
  min-height: var(--tap);
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; width: 100%; }
.btn.danger { border-color: var(--danger); color: var(--danger); }
.btn.warn { border-color: var(--warn); color: var(--warn); }
.btn.ghost { color: var(--muted); }

.status-card dd.accent { color: var(--danger); font-weight: 600; }

/* Connection state is the first thing read on a phone in the field. */
.status-card dd.presence-online { color: var(--ok); font-weight: 600; }
.status-card dd.presence-offline { color: var(--danger); font-weight: 600; }
.status-card dd.presence-stale { color: var(--warn); font-weight: 600; }

/* --- toggles --- */
.toggles { list-style: none; margin: 0; padding: 0; }
.toggles li {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 12px;
  margin-bottom: 8px;
}
.toggles li span { flex: 1; min-width: 0; }
.toggles small { display: block; font-size: 13px; color: var(--muted); }
.switch { flex: none; }

.hint {
  margin: -4px 0 8px;
  padding: 8px 12px;
  font-size: 14px;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}
.unmonitored { color: var(--danger); }

/* --- outputs --- */
.cameras li.editing { border-color: var(--brand); }

.cam-name-input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 6px 10px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.cam-name-input:focus { outline: 2px solid var(--brand); outline-offset: -1px; }

.cam-volt-select {
  min-height: 44px;
  padding: 6px 8px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
}

/* Square icon buttons keep a row of controls readable on a narrow screen. */
.btn.icon {
  min-width: 44px;
  width: 44px;
  min-height: 44px;
  height: 44px;
  padding: 0;
  font-size: 18px;
  flex: none;
}
.btn.primary.icon { width: 44px; }
.cameras li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 8px 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 12px;
  margin-bottom: 8px;
}

/* On a phone the name, the voltage and three controls do not fit on one line
   without squeezing the name down to a few characters per line. Give the name
   its own row and right-align the controls underneath it. */
@media (max-width: 560px) {
  .cameras .cam-name,
  .cameras .cam-name-input { flex: 1 1 100%; }
}
/* Without min-width:0 a flex item never shrinks below its longest word, so a
   long output name pushes the switch off the right edge of a phone. */
.cameras .cam-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.cameras .cam-volt { color: var(--muted); font-size: 14px; white-space: nowrap; }
.switch { min-width: 84px; min-height: 40px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font: inherit; font-weight: 600; cursor: pointer; }
.switch.on { background: var(--ok); border-color: var(--ok); color: #fff; }

/* --- events --- */
.events li { display: flex; gap: 10px; padding: 8px 2px; border-bottom: 1px solid var(--line); font-size: 15px; }
.events li > :last-child { min-width: 0; overflow-wrap: anywhere; }
.events time { color: var(--muted); white-space: nowrap; flex: none; }

/* --- forms --- */
.card {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
}
/* Only the centred login card is narrow; admin forms fill their column. */
body.centered .card { max-width: 360px; }
.card h1 { margin: 0 0 8px; text-align: center; color: var(--brand); }
/* Block, not inline: a label wrapped in a plain div (rather than sitting
   directly in the form grid) would otherwise share a line with its input. */
label { display: block; font-size: 14px; color: var(--muted); }
input {
  min-height: var(--tap);
  padding: 10px 12px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
}

.error { color: var(--danger); margin: 4px 0 0; }
.empty { color: var(--muted); text-align: center; padding: 32px 0; }

/* --- admin --- */
/* Wraps to a second row rather than scrolling sideways: a tab that sits off
   the right edge is a tab nobody finds. */
.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 4px;
  padding: 8px 12px 0;
  max-width: 720px;
  margin: 0 auto;
}
.tab {
  flex: 1 1 auto;
  min-height: 44px;
  padding: 8px 10px;
  white-space: nowrap;
  font: inherit;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-bottom: 3px solid transparent;
  cursor: pointer;
}
.tab.active { color: var(--text); border-bottom-color: var(--brand); }

.admin-list { list-style: none; margin: 0; padding: 0; }
.admin-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 8px;
}
.admin-list li.stacked { flex-direction: column; align-items: stretch; gap: 12px; }
.admin-main { flex: 1; min-width: 0; }
.admin-sub { display: block; font-size: 14px; color: var(--muted); }
/* Ids and e-mail addresses are long, unbreakable-looking strings. */
.admin-main b, .admin-sub { overflow-wrap: anywhere; }

/* One column of full-width controls. `.inline` is also used bare (without
   `.card`) for the settings forms nested inside a list row — without the grid
   those fall back to inline flow and the labels overlap their inputs. */
form.card,
.inline {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 6px;
}
/* Grid items size to their content by default, so a `select` with a long
   option label would push the whole form wider than its container. */
.inline > *,
form.card > * { min-width: 0; }
/* Air between field groups, so a label reads as belonging to the input below
   it rather than to the input above. */
.inline > label:not(:first-child),
form.card > label:not(:first-child) { margin-top: 10px; }
.inline > .btn,
form.card > .btn { margin-top: 14px; }
.inline > .admin-sub { margin: 2px 0 0; }

/* A single column of inputs stretched over the full 720px column is hard to
   scan on a desktop; the nested `.inline` forms inside a list row are exempt. */
.card.inline { margin-bottom: 16px; max-width: 460px; }
#org-row { display: grid; gap: 6px; }
.checkline { display: flex; align-items: center; gap: 8px; color: var(--text); }
.checkline input { min-height: auto; width: 20px; height: 20px; flex: none; }

.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip {
  min-height: 40px;
  padding: 6px 12px;
  font: inherit;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
}
.chip.on { background: var(--brand); border-color: var(--brand); color: #fff; }

select {
  min-height: var(--tap);
  padding: 10px 12px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
}

.status-sent { color: var(--ok); font-weight: 700; }
.status-failed { color: var(--danger); font-weight: 700; }
.status-suppressed { color: var(--warn); font-weight: 700; }

/* --- toast --- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: min(92vw, 480px);
  background: #111827;
  color: #fff;
  padding: 12px 16px;
  border-radius: 10px;
  box-shadow: 0 6px 24px rgb(0 0 0 / .25);
  z-index: 50;
}
.toast.error { background: var(--danger); }
