/* app.css — officer + admin styling (§12.5).
   Mobile-first; primary acceptance target is current iOS Safari (§12.6).
   Strict CSP (§14.5): system fonts only, no external stylesheets or CDNs.
   Honours prefers-color-scheme; every interactive target keeps the 44px
   minimum and a visible focus ring. */

:root {
  --touch-min: 44px;        /* minimum touch target (§12.5) */
  --maxw: 720px;            /* reading column */
  --maxw-wide: 1140px;      /* admin tables + form builder need more room */

  /* Spacing scale (4px base) — prefer these over ad-hoc values. */
  --s1: .25rem; --s2: .5rem; --s3: .75rem; --s4: 1rem;
  --s5: 1.5rem; --s6: 2rem; --s7: 3rem;

  --radius: 8px;
  --radius-sm: 5px;
  --radius-pill: 999px;

  /* Light theme. */
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --fg: #16191f;
  --muted: #5b6472;
  --line: #dfe3ea;
  --line-strong: #c3cad6;
  --accent: #1f4fd8;
  --accent-fg: #ffffff;
  --accent-soft: #eaf0ff;
  --danger: #b42318;
  --danger-soft: #fef3f2;
  --ok: #027a48;
  --ok-soft: #ecfdf3;
  --shadow-sm: 0 1px 2px rgb(16 24 40 / .06);
  --shadow: 0 1px 3px rgb(16 24 40 / .08), 0 1px 2px rgb(16 24 40 / .04);

  /* Let native controls (checkbox, select arrow, date picker) follow the theme. */
  color-scheme: light dark;

  --font: -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #161a21;
    --surface-2: #1c212a;
    --fg: #e7eaef;
    --muted: #98a2b3;
    --line: #272d38;
    --line-strong: #3a4250;
    --accent: #7aa2ff;
    --accent-fg: #0e1116;
    --accent-soft: #1a2237;
    --danger: #ff9b91;
    --danger-soft: #2a1614;
    --ok: #6ee7b7;
    --ok-soft: #12241d;
    --shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
    --shadow: 0 1px 3px rgb(0 0 0 / .5);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 16px/1.55 var(--font);
  color: var(--fg);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

h1 { font-size: 1.5rem; line-height: 1.25; letter-spacing: -.01em; margin: 0 0 var(--s4); }
h2 { font-size: 1.125rem; line-height: 1.3; margin: 0 0 var(--s3); }
h3 { font-size: 1rem; margin: 0 0 var(--s2); }
a { color: var(--accent); }
.hint { color: var(--muted); font-size: .875rem; }

/* The shell is wide enough for admin tables and the form builder; the officer
   pages narrow themselves back to the reading column. (A child cannot exceed
   its parent, so the wide case has to be the default.) */
.app-main { max-width: var(--maxw-wide); margin: 0 auto; padding: var(--s5) var(--s4) var(--s7); }
.checklist-list, .checklist, .login, .admin-login, .admin-import, .error-page {
  max-width: var(--maxw); margin-inline: auto;
}

/* --- Header --------------------------------------------------------------- */
.app-header {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: var(--s4);
  padding: var(--s3) var(--s4);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.app-title { font-weight: 650; letter-spacing: -.01em; color: var(--fg); text-decoration: none; }
.app-title:hover { color: var(--accent); }
.app-header form { margin: 0; }

/* --- Controls ------------------------------------------------------------- */
button, input, select, textarea, a.button {
  min-height: var(--touch-min);
  font-family: inherit;
  font-size: 1rem;              /* 16px keeps iOS from zooming on focus */
}

input[type="text"], input[type="password"], input[type="search"],
input[type="datetime-local"], select, textarea {
  width: 100%;
  padding: var(--s2) var(--s3);
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  outline: none;
}
textarea { min-height: 9rem; line-height: 1.5; resize: vertical; }
::placeholder { color: var(--muted); opacity: .8; }

input[type="checkbox"] {
  width: 1.3rem; height: 1.3rem; min-height: 0;
  accent-color: var(--accent); flex: none;
}

button, a.button {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  padding: 0 var(--s4);
  font-weight: 550;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  text-decoration: none;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background .15s, border-color .15s, transform .05s;
}
button:hover, a.button:hover { background: var(--surface-2); border-color: var(--muted); }
button:active, a.button:active { transform: translateY(1px); }

/* Primary action: form submits, and the dashboard's create link. */
button[type="submit"], a.button.primary {
  color: var(--accent-fg); background: var(--accent); border-color: var(--accent);
}
button[type="submit"]:hover, a.button.primary:hover { background: var(--accent); filter: brightness(1.08); }

/* Visible focus states (§12.5). */
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* Screen-reader-only helper for the aria-live status region. */
.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* --- Messages ------------------------------------------------------------- */
.flash, .errors, .local-warning, .preview-banner {
  max-width: var(--maxw); margin: var(--s4) auto 0;
  padding: var(--s3) var(--s4);
  border-radius: var(--radius);
  font-size: .9375rem;
}
.flash { list-style: none; color: var(--ok); background: var(--ok-soft); border: 1px solid currentColor; }
.errors { color: var(--danger); background: var(--danger-soft); border: 1px solid currentColor; }
.flash li, .errors li { margin-left: var(--s4); }
.flash li:only-child, .errors li:only-child { margin-left: 0; list-style: none; }
.local-warning, .preview-banner {
  color: var(--muted); background: var(--surface-2);
  border: 1px solid var(--line); border-left: 3px solid var(--accent);
}

/* --- Login ---------------------------------------------------------------- */
.login, .admin-login {
  max-width: 24rem; margin: var(--s7) auto;
  padding: var(--s5);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.login label, .admin-login label {
  display: block; margin-bottom: var(--s1);
  font-size: .875rem; color: var(--muted);
}
.login button, .admin-login button { width: 100%; margin-top: var(--s4); }

/* --- Officer: checklist list ---------------------------------------------- */
#checklist-search { margin-bottom: var(--s3); }

.role-filters { display: flex; gap: var(--s2); margin-bottom: var(--s4); }
.role-filters button { flex: 1; }
.role-filters button[aria-pressed="true"] {
  color: var(--accent); background: var(--accent-soft); border-color: var(--accent);
}

#checklist-items { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--s2); }
.checklist-item {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  transition: border-color .15s, box-shadow .15s;
}
.checklist-item:hover { border-color: var(--line-strong); box-shadow: var(--shadow); }
.checklist-item > a {
  display: flex; align-items: center; gap: var(--s3);
  min-height: var(--touch-min);
  padding: var(--s3) var(--s4);
  color: var(--fg); text-decoration: none;
}
.checklist-title { font-weight: 550; }

.role-badge {
  flex: none;
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 2.5rem; padding: var(--s1) var(--s2);
  font-size: .75rem; font-weight: 650; letter-spacing: .02em;
  border-radius: var(--radius-pill);
  color: var(--accent); background: var(--accent-soft);
}
.role-supervisor { color: var(--danger); background: var(--danger-soft); }
.role-both { color: var(--ok); background: var(--ok-soft); }

.tags { display: flex; flex-wrap: wrap; gap: var(--s1); padding: 0 var(--s4) var(--s3); }
.tag-chip {
  padding: 2px var(--s2);
  font-size: .75rem; color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-pill);
}
.empty-state { padding: var(--s6) 0; text-align: center; color: var(--muted); }

/* --- Officer: checklist fields (shared with admin preview, §13.6) --------- */
.checklist h1 { margin-bottom: var(--s2); }
#checklist-form { margin-top: var(--s5); }

.field { margin-bottom: var(--s4); }
.field > label {
  display: block; margin-bottom: var(--s1);
  font-size: .875rem; font-weight: 550; color: var(--muted);
}
.field-checkbox, .field-checkbox-note {
  padding: var(--s2) var(--s3);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}
.field-checkbox > label, .field-checkbox-note > label {
  display: flex; align-items: center; gap: var(--s3);
  min-height: var(--touch-min);
  margin: 0; font-size: 1rem; font-weight: 450; color: var(--fg);
  cursor: pointer;
}
.field-checkbox-note .note { margin-top: var(--s2); }

h2.section {
  margin: var(--s6) 0 var(--s3);
  padding-bottom: var(--s2);
  font-size: .8125rem; font-weight: 650;
  text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
  border-bottom: 1px solid var(--line);
}

/* Sticky action bar on the checklist page (Copy / Clear). */
.action-bar {
  position: sticky; bottom: 0;
  display: flex; gap: var(--s2);
  padding: var(--s3) 0;
  margin-top: var(--s5);
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.action-bar button { flex: 1; }
#copy-btn { color: var(--accent-fg); background: var(--accent); border-color: var(--accent); }

/* --- Admin: dashboard ----------------------------------------------------- */
.admin-actions {
  display: flex; flex-wrap: wrap; align-items: center; gap: var(--s3);
  margin-bottom: var(--s5);
}
/* The lone a.button here is "Create New Checklist" — the page's primary action. */
.admin-actions > a.button {
  color: var(--accent-fg); background: var(--accent); border-color: var(--accent);
}
.admin-actions > a.button:hover { background: var(--accent); filter: brightness(1.08); }
.admin-actions .filter { margin-left: auto; font-size: .875rem; color: var(--muted); }
.admin-actions .filter a { margin-left: var(--s2); text-decoration: none; }
.admin-actions .filter a[aria-current] { font-weight: 650; text-decoration: underline; }

/* Wide table scrolls inside its own box rather than the page. */
.admin-table {
  width: 100%; display: block; overflow-x: auto;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: .9375rem;
}
.admin-table thead { background: var(--surface-2); }
.admin-table th, .admin-table td {
  padding: var(--s3);
  text-align: left; white-space: nowrap;
  border-bottom: 1px solid var(--line);
}
/* Timestamps are the widest column by far; shrink them so the Actions column
   stays on screen instead of being pushed into the horizontal scroll. */
.admin-table td:nth-child(5) {
  font-family: var(--mono); font-size: .8125rem; color: var(--muted);
}
.admin-table td:nth-child(4), .admin-table th:nth-child(4) { text-align: right; }
.admin-table th {
  font-size: .75rem; font-weight: 650;
  text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
}
.admin-table tbody tr:last-child td { border-bottom: 0; }
.admin-table tbody tr:hover { background: var(--surface-2); }
.admin-table tr.deleted td { color: var(--muted); }
.admin-table tr.deleted td:first-child { text-decoration: line-through; }

.admin-table .actions { display: flex; align-items: center; gap: var(--s3); }
.admin-table .actions form { margin: 0; }
.admin-table .actions button {
  min-height: 2rem; padding: 0 var(--s3);
  font-size: .8125rem; box-shadow: none;
  color: var(--fg); background: var(--surface); border-color: var(--line-strong);
}
.admin-table .actions form[action*="delete"] button { color: var(--danger); border-color: var(--danger); }

/* --- Admin: Form Builder (§13.4) ------------------------------------------ */
.fb-columns {
  display: grid; gap: var(--s5);
  grid-template-columns: minmax(0, 1fr);
  align-items: start;
}
@media (min-width: 64rem) {
  .fb-columns { grid-template-columns: minmax(0, 1fr) 20rem; }
  .fb-preview-wrap { position: sticky; top: 5rem; }
}

/* Checklist metadata. Each child is a .fb-field label wrapping its control,
   so the grid lays out whole field groups rather than orphaned labels. */
.fb-meta {
  display: grid; gap: var(--s4);
  grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr));
  padding: var(--s4);
  margin-bottom: var(--s4);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.fb-items { display: flex; flex-direction: column; gap: var(--s3); counter-reset: fb; }

.fb-item {
  position: relative;
  display: grid; gap: var(--s3) var(--s4);
  grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr));
  align-items: start;
  padding: var(--s4) var(--s4) var(--s4) var(--s6);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  transition: border-color .15s, box-shadow .15s;
}
.fb-item:hover { border-color: var(--line-strong); box-shadow: var(--shadow); }
/* Field number via a CSS counter, so the builder JS stays untouched. */
.fb-item::before {
  counter-increment: fb; content: counter(fb);
  position: absolute; left: var(--s3); top: var(--s4);
  font-size: .75rem; font-weight: 650; color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.fb-field {
  display: flex; flex-direction: column; gap: var(--s1);
  font-size: .8125rem; font-weight: 550; color: var(--muted);
}
.fb-field input[type="checkbox"] { margin-top: var(--s2); }

[data-fb-id] { font-family: var(--mono); font-size: .9375rem; }
[data-fb-id].dup, [data-fb-id].invalid {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px var(--danger-soft);
}

.fb-controls {
  grid-column: 1 / -1;
  display: flex; gap: var(--s2);
  padding-top: var(--s3);
  border-top: 1px dashed var(--line);
}
.fb-controls button {
  min-height: 2.25rem; padding: 0 var(--s3);
  font-size: .875rem; box-shadow: none;
}
.fb-controls button:first-child, .fb-controls button:nth-child(2) { min-width: 2.75rem; }
.fb-controls button:last-child { margin-left: auto; color: var(--danger); }
.fb-controls button:last-child:hover { border-color: var(--danger); background: var(--danger-soft); }

.fb-add { width: 100%; margin-top: var(--s4); border-style: dashed; }

.fb-errors {
  margin: var(--s3) 0;
  padding: var(--s3) var(--s4);
  color: var(--danger); background: var(--danger-soft);
  border: 1px solid currentColor; border-radius: var(--radius);
  font-size: .875rem;
}

.fb-preview-wrap {
  padding: var(--s4);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.fb-preview-wrap h2 {
  margin: 0 0 var(--s3);
  font-size: .75rem; font-weight: 650;
  text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
}
#builder-preview:empty::after {
  content: "Add a field to see it here.";
  color: var(--muted); font-size: .875rem;
}
.pv-field { padding: var(--s2) 0; border-bottom: 1px solid var(--line); font-size: .9375rem; }
.pv-field:last-child { border-bottom: 0; }
#builder-preview h3 {
  margin: var(--s3) 0 var(--s1);
  font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
}

/* --- Admin: raw JSON + editor actions ------------------------------------- */
.raw-json { margin: var(--s5) 0; }
.raw-json summary {
  cursor: pointer; padding: var(--s2) 0;
  font-size: .875rem; font-weight: 550; color: var(--muted);
}
.raw-json summary:hover { color: var(--accent); }
.raw-json textarea { font-family: var(--mono); font-size: .875rem; }

.editor-actions {
  display: flex; align-items: center; gap: var(--s4);
  padding-top: var(--s4);
  margin-top: var(--s5);
  border-top: 1px solid var(--line);
}
.editor-actions button { padding: 0 var(--s6); }

/* --- Misc ----------------------------------------------------------------- */
.admin-audit table { width: 100%; }
.error-page { margin-block: var(--s7); text-align: center; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
