/* Kestrel design system — "Fieldwork", Estuary teal accent.
   Decided in C2a (docs/DECISION-LOG.md, 2026-08-06); the proposal page
   is docs/design-proposals/2026-08-05-c2a-design-systems.html.

   Two rules this file exists to keep:
   1. Colour is a TOKEN, never a literal. The accent moved four times
      during C2a; it must stay a one-line change.
   2. Every text/background pair is MEASURED against WCAG AA (4.5:1
      body, 3:1 large/UI). Ratios are noted beside each token. Dan
      caught a 4.1:1 pair by eye in review — don't reintroduce one.
   No webfonts: a system stack means no external requests, no CSP
   surprises and no flash of unstyled text on a builder's 4G phone. */

:root {
  color-scheme: light dark;

  /* --- Fieldwork palette (light) ------------------------------------ */
  --ground: #F6F4F1;        /* warm stone */
  --surface: #FFFFFF;
  --surface-sunk: #EFECE7;
  --ink: #33393E;           /* 10.7:1 on ground */
  --muted: #5C5852;         /* 6.4:1 on ground, 7.1:1 on surface */
  --line: #E5E1DA;
  --line-strong: #D2CCC3;

  --accent: #14675F;        /* Estuary teal — white on it 6.7:1 */
  --accent-ink: #FFFFFF;
  --accent-hover: #0F534C;
  --accent-soft: #DCEDEA;
  --accent-soft-ink: #0E5049;   /* 8.9:1 on accent-soft */
  --structure: #5A6B7C;     /* kestrel slate: counts, badges, steps */

  --ok-bg: #E7F0E9;
  --ok-ink: #33683F;        /* 5.3:1 on ok-bg */
  --warn-bg: #FBEEDC;
  --warn-ink: #7A5216;      /* 5.6:1 on warn-bg */
  --danger-bg: #FBE7E3;
  --danger-ink: #96351F;    /* 5.4:1 on danger-bg */
  --danger: #96351F;

  /* --- shape + depth ------------------------------------------------- */
  --radius-card: 14px;
  --radius-pill: 999px;
  --radius-field: 10px;
  --shadow-card: 0 1px 4px rgba(51, 57, 62, .08);
  --shadow-raised: 0 2px 10px rgba(51, 57, 62, .10);
  --shadow-focus: 0 0 0 3px rgba(20, 103, 95, .28);

  /* --- spacing scale (4px base) -------------------------------------- */
  --s1: 4px;  --s2: 8px;  --s3: 12px; --s4: 16px;
  --s5: 24px; --s6: 32px; --s7: 48px; --s8: 64px;

  /* --- type ----------------------------------------------------------- */
  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
  --text-xs: 12px; --text-sm: 13.5px; --text-base: 15px;
  --text-lg: 18px; --text-xl: 22px; --text-2xl: 28px;
}

/* Dark theme: derived from the Nightshift rules in the C2a proposal —
   deep blue-slate ground, lifted surfaces, accent LIGHTENED so it keeps
   contrast on a dark ground (naive inversion would drop teal to ~2:1). */
@media (prefers-color-scheme: dark) {
  :root {
    --ground: #1C232C;
    --surface: #242D38;
    --surface-sunk: #1F2731;
    --ink: #E8ECEF;         /* 12.6:1 on ground */
    --muted: #A8B0B8;       /* 7.2:1 on ground */
    --line: #333E4A;
    --line-strong: #46525F;

    --accent: #5FC3B6;      /* on dark: 8.3:1 as text on ground */
    --accent-ink: #10231F;  /* dark text on the light accent = 9.1:1 */
    --accent-hover: #7BD3C7;
    --accent-soft: #1E3C39;
    --accent-soft-ink: #8FD8CD;
    --structure: #93A6B8;

    --ok-bg: #1F3A2C;   --ok-ink: #7FD79E;
    --warn-bg: #3C2F1A; --warn-ink: #F0C078;
    --danger-bg: #40241F; --danger-ink: #F2A493;
    --danger: #F2A493;

    --shadow-card: 0 1px 4px rgba(0, 0, 0, .35);
    --shadow-raised: 0 2px 10px rgba(0, 0, 0, .45);
    --shadow-focus: 0 0 0 3px rgba(95, 195, 182, .32);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--font);
  font-size: var(--text-base);
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { line-height: 1.2; text-wrap: balance; margin: 0 0 var(--s3); }
h1 { font-size: var(--text-2xl); font-weight: 700; }
h2 { font-size: var(--text-xl); font-weight: 700; margin-top: var(--s6); }
h3 { font-size: var(--text-lg); font-weight: 650; }
p { margin: 0 0 var(--s3); }
a { color: var(--accent); }
a:hover { color: var(--accent-hover); }
small, .text-sm { font-size: var(--text-sm); }
.muted { color: var(--muted); }
.tabular { font-variant-numeric: tabular-nums; }

:where(a, button, input, select, textarea, summary):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--s1);
}

/* --- app shell -------------------------------------------------------- */
.skip-link {
  position: absolute; left: -9999px; top: var(--s2);
  background: var(--surface); color: var(--ink);
  padding: var(--s2) var(--s4); border-radius: var(--radius-pill);
  box-shadow: var(--shadow-raised); z-index: 20;
}
.skip-link:focus { left: var(--s4); }

.app-header {
  background: var(--ground);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.app-header .bar {
  max-width: 1180px; margin: 0 auto;
  padding: var(--s3) var(--s4);
  display: flex; align-items: center; gap: var(--s4);
}
.brand {
  font-weight: 700; font-size: var(--text-lg); color: var(--accent);
  text-decoration: none; letter-spacing: .005em;
}
.brand:hover { color: var(--accent-hover); }

.nav { display: flex; gap: var(--s1); flex-wrap: wrap; }
.nav a {
  color: var(--muted); text-decoration: none; font-weight: 500;
  font-size: var(--text-sm); padding: var(--s2) var(--s3);
  border-radius: var(--radius-pill);
}
.nav a:hover { color: var(--ink); background: var(--surface-sunk); }
.nav a[aria-current="page"] {
  color: var(--ink); background: var(--surface); box-shadow: var(--shadow-card);
}

.header-end { margin-left: auto; display: flex; align-items: center; gap: var(--s3); }
.who { font-size: var(--text-sm); color: var(--muted); }
.nav-toggle { display: none; }

main { max-width: 1180px; margin: 0 auto; padding: var(--s5) var(--s4) var(--s8); }
.page-head { margin-bottom: var(--s5); }
.page-head p { color: var(--muted); margin: 0; }

.app-footer {
  max-width: 1180px; margin: 0 auto; padding: 0 var(--s4) var(--s6);
  color: var(--muted); font-size: var(--text-xs);
}

/* --- surfaces --------------------------------------------------------- */
.card {
  background: var(--surface); border-radius: var(--radius-card);
  box-shadow: var(--shadow-card); padding: var(--s4) var(--s5);
  margin-bottom: var(--s4);
}
.card > :last-child { margin-bottom: 0; }
.card-title { font-size: var(--text-sm); font-weight: 650; color: var(--muted);
  text-transform: uppercase; letter-spacing: .06em; margin-bottom: var(--s3); }

.stats { display: grid; gap: var(--s3); margin-bottom: var(--s5);
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.stat {
  background: var(--surface); border-radius: var(--radius-card);
  box-shadow: var(--shadow-card); padding: var(--s3) var(--s4);
  text-decoration: none; color: inherit; display: block;
}
a.stat:hover { box-shadow: var(--shadow-raised); }
.stat b { display: block; font-size: var(--text-2xl); font-weight: 700;
  color: var(--structure); font-variant-numeric: tabular-nums; line-height: 1.15; }
.stat.hot b { color: var(--accent); }
.stat span { font-size: var(--text-xs); color: var(--muted); }

/* --- buttons ---------------------------------------------------------- */
.btn, button.btn, a.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: var(--s2); border: 0; cursor: pointer; text-decoration: none;
  font-family: inherit; font-size: var(--text-sm); font-weight: 650;
  padding: var(--s3) var(--s5); border-radius: var(--radius-pill);
  min-height: 44px;  /* thumb-sized: the van test */
  background: var(--accent); color: var(--accent-ink);
}
.btn:hover { background: var(--accent-hover); color: var(--accent-ink); }
.btn.quiet {
  background: var(--surface); color: var(--ink);
  box-shadow: inset 0 0 0 1.5px var(--line-strong);
}
.btn.quiet:hover { background: var(--surface-sunk); color: var(--ink); }
.btn.danger { background: var(--danger-bg); color: var(--danger-ink); }
.btn.small { min-height: 34px; padding: var(--s2) var(--s4); font-size: var(--text-xs); }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.actions { display: flex; gap: var(--s3); align-items: center;
  flex-wrap: wrap; margin-top: var(--s4); }

/* --- pills ------------------------------------------------------------ */
.pill {
  display: inline-block; border-radius: var(--radius-pill);
  padding: 2px var(--s3); font-size: var(--text-xs); font-weight: 650;
  background: var(--surface-sunk); color: var(--muted); white-space: nowrap;
}
.pill.ok { background: var(--ok-bg); color: var(--ok-ink); }
.pill.new { background: var(--accent-soft); color: var(--accent-soft-ink); }
.pill.warn { background: var(--warn-bg); color: var(--warn-ink); }
.pill.bad { background: var(--danger-bg); color: var(--danger-ink); }
/* Quiet qualifier pill: sits beside a status pill to narrow what it means
   (e.g. "No objection" + "Consultee response"). Deliberately unsaturated so
   it reads as a caveat, not a second status. Reuses existing tokens —
   measured 6.0:1 light, 6.9:1 dark, both above AA's 4.5:1. */
.pill.note { background: var(--surface-sunk); color: var(--muted);
  font-weight: 600; border: 1px solid var(--line); }
/* Lead-type pill (C7 review): sits on the row below the status pill, so
   the Details cell reads "what happened" then "why you'd care". Outlined
   rather than filled — a row can carry three of these and filled pills at
   that density turn the table into confetti. */
.pill.lead { background: transparent; color: var(--muted);
  font-weight: 600; border: 1px solid var(--line); }
.lead-pills { margin-top: 4px; display: flex; flex-wrap: wrap; gap: 3px; }
.badge { display: inline-block; background: var(--structure); color: #fff;
  border-radius: var(--radius-pill); padding: 2px var(--s3);
  font-size: var(--text-xs); font-weight: 700; font-variant-numeric: tabular-nums; }

/* --- tables ----------------------------------------------------------- */
.table-wrap { overflow-x: auto; background: var(--surface);
  border-radius: var(--radius-card); box-shadow: var(--shadow-card); }
table.data { border-collapse: collapse; width: 100%; font-size: var(--text-sm); }
table.data th {
  text-align: left; font-size: var(--text-xs); font-weight: 650;
  color: var(--muted); text-transform: uppercase; letter-spacing: .05em;
  padding: var(--s3) var(--s4); border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
table.data td { padding: var(--s3) var(--s4); border-bottom: 1px solid var(--line);
  vertical-align: top; }
table.data tbody tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: var(--surface-sunk); }
table.data td.tabular, table.data td.num { font-variant-numeric: tabular-nums; }

/* --- forms ------------------------------------------------------------ */
form p { margin: 0 0 var(--s3); }
label { font-size: var(--text-sm); font-weight: 600; color: var(--ink);
  display: inline-block; margin-bottom: var(--s1); }
input[type="text"], input[type="email"], input[type="password"], input[type="date"],
input[type="search"], input[type="url"], input[type="tel"], input[type="number"],
input:not([type]),   /* a bare <input> defaults to text but matches no
                        [type=] selector — caught in C2 review */
select, textarea {
  font-family: inherit; font-size: var(--text-base); color: var(--ink);
  background: var(--surface); border: 1.5px solid var(--line-strong);
  border-radius: var(--radius-field); padding: var(--s3);
  width: 100%; max-width: 460px; min-height: 44px;
}
textarea { min-height: 180px; line-height: 1.5; resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: var(--shadow-focus);
}
input[type="checkbox"], input[type="radio"] {
  width: 20px; height: 20px; min-height: 0; accent-color: var(--accent);
  vertical-align: middle;
}
.helptext, .helptext li { font-size: var(--text-xs); color: var(--muted); }
ul.errorlist { list-style: none; margin: 0 0 var(--s2); padding: 0;
  color: var(--danger-ink); font-size: var(--text-sm); font-weight: 600; }

.filters { display: flex; flex-wrap: wrap; gap: var(--s3); align-items: flex-end; }
.filters label { display: flex; flex-direction: column; gap: var(--s1);
  font-size: var(--text-xs); text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); }
.filters input:not([type="checkbox"]):not([type="radio"]),
.filters select { min-width: 150px; max-width: 220px; }
.filters .check { flex-direction: row; align-items: center; gap: var(--s2);
  text-transform: none; letter-spacing: 0; font-size: var(--text-sm);
  color: var(--ink); }

/* --- messages, notes, empty states ------------------------------------ */
.flash { border-radius: var(--radius-card); padding: var(--s3) var(--s4);
  margin-bottom: var(--s4); font-size: var(--text-sm); font-weight: 600;
  background: var(--ok-bg); color: var(--ok-ink); }
.flash.error { background: var(--danger-bg); color: var(--danger-ink); }
.flash.warning { background: var(--warn-bg); color: var(--warn-ink); }
.note { background: var(--surface); border-left: 4px solid var(--accent);
  border-radius: var(--radius-field); padding: var(--s3) var(--s4);
  box-shadow: var(--shadow-card); margin-bottom: var(--s4);
  font-size: var(--text-sm); }
.note.warn { border-left-color: var(--warn-ink); }
.empty { text-align: center; color: var(--muted); padding: var(--s7) var(--s4); }

/* --- send stepper ----------------------------------------------------- */
.steps { display: flex; gap: var(--s2); flex-wrap: wrap; list-style: none;
  padding: 0; margin: 0 0 var(--s5); font-size: var(--text-xs); }
.steps li { background: var(--surface-sunk); color: var(--muted);
  border-radius: var(--radius-pill); padding: var(--s2) var(--s4); font-weight: 600; }
.steps li[aria-current="step"] { background: var(--structure); color: #fff; }
.steps li.done { background: var(--accent-soft); color: var(--accent-soft-ink); }

.summary { background: var(--surface); border-radius: var(--radius-card);
  box-shadow: var(--shadow-raised); padding: var(--s4) var(--s5);
  max-width: 520px; margin-bottom: var(--s4); }
.summary .line { display: flex; justify-content: space-between; gap: var(--s4);
  padding: var(--s2) 0; border-bottom: 1px solid var(--line);
  font-size: var(--text-sm); font-variant-numeric: tabular-nums; }
.summary .line:last-child { border-bottom: 0; }
.summary .line.total { font-size: var(--text-lg); font-weight: 700;
  color: var(--accent); padding-top: var(--s3); }

/* --- lists of saved things -------------------------------------------- */
.tiles { display: grid; gap: var(--s3); grid-template-columns: 1fr;
  max-width: 560px; }
.tile { display: flex; justify-content: space-between; align-items: center;
  gap: var(--s3); background: var(--surface); border-radius: var(--radius-card);
  box-shadow: var(--shadow-card); padding: var(--s3) var(--s4);
  text-decoration: none; color: inherit; font-size: var(--text-sm); }
.tile:hover { box-shadow: var(--shadow-raised); color: inherit; }

.pager { display: flex; gap: var(--s4); align-items: center;
  margin-top: var(--s4); font-size: var(--text-sm); color: var(--muted); }

/* the save-this-search row stays on one line where there's room */
@media (min-width: 821px) {
  form[data-testid="save-filter"] { flex-wrap: nowrap; }
}

/* --- responsive ------------------------------------------------------- */
@media (max-width: 820px) {
  .app-header .bar { flex-wrap: wrap; gap: var(--s2); }
  .nav-toggle {
    display: inline-flex; margin-left: auto; align-items: center;
    background: var(--surface); color: var(--ink); border: 0;
    box-shadow: inset 0 0 0 1.5px var(--line-strong);
    border-radius: var(--radius-pill); min-height: 40px; padding: 0 var(--s4);
    font: inherit; font-size: var(--text-sm); font-weight: 600; cursor: pointer;
  }
  .nav { flex-basis: 100%; flex-direction: column; gap: var(--s1);
    display: none; padding-bottom: var(--s2); }
  .nav.open { display: flex; }
  .nav a { padding: var(--s3) var(--s4); font-size: var(--text-base); }
  .header-end { flex-basis: 100%; margin-left: 0; }
  main { padding: var(--s4) var(--s3) var(--s7); }
  h1 { font-size: var(--text-xl); }
  .filters { flex-direction: column; align-items: stretch; }
  .filters input:not([type="checkbox"]):not([type="radio"]),
  .filters select { max-width: none; }
  .btn { width: 100%; }
  .actions .btn { width: auto; flex: 1 1 auto; }

  /* Phone tables become cards — the browse screen is checked in a van. */
  table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }
  table.data thead { display: none; }
  table.data tr { background: var(--surface); border-radius: var(--radius-card);
    box-shadow: var(--shadow-card); margin-bottom: var(--s3); padding: var(--s2); }
  table.data td { border-bottom: 0; padding: var(--s1) var(--s3); }
  table.data td[data-label]::before {
    content: attr(data-label) " ";
    display: block; font-size: var(--text-xs); color: var(--muted);
    text-transform: uppercase; letter-spacing: .05em;
  }
  .table-wrap { background: none; box-shadow: none; }
}

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

@media print {
  .app-header, .app-footer, .actions, .nav-toggle { display: none; }
  body { background: #fff; }
}

/* --- browse map (C7) --------------------------------------------------- */
#applications-map {
  height: 420px; border-radius: var(--radius); border: 1px solid var(--line);
  margin-bottom: var(--s3); z-index: 0;   /* keep tiles under the nav */
}
@media (max-width: 640px) { #applications-map { height: 300px; } }

/* --- impersonation banner (E4) ----------------------------------------
   Deliberately loud and full-bleed: an operator who forgets they are
   inside a customer's account can do real damage in that account's name. */
.impersonation-bar {
  background: var(--warn-bg); color: var(--warn-ink);
  border-bottom: 2px solid var(--warn-ink);
  padding: var(--s3) var(--s4);
  display: flex; align-items: center; gap: var(--s4); flex-wrap: wrap;
  font-size: var(--text-sm); font-weight: 600;
  position: sticky; top: 0; z-index: 15;
}
.impersonation-bar form { margin: 0; }

/* --- coverage map (A2) -------------------------------------------------- */
#coverage-map {
  height: 560px; border-radius: var(--radius); border: 1px solid var(--line);
  margin-bottom: var(--s3); z-index: 0;
}
@media (max-width: 640px) { #coverage-map { height: 380px; } }

/* --- legal pages (E5) --------------------------------------------------- */
.legal-body { max-width: 68ch; }
.legal-body h2 { margin-top: var(--s5); }
.legal-body h3 { margin-top: var(--s4); }
.app-footer a { color: var(--muted); }

/* --- letter template editor (D1 review) --------------------------------- */
.editor-layout { display: grid; gap: var(--s4); grid-template-columns: 1fr; }
@media (min-width: 900px) {
  .editor-layout { grid-template-columns: minmax(0, 1.6fr) minmax(260px, 1fr); }
}
.editor-layout textarea { min-height: 320px; width: 100%; }
.merge-field-list { list-style: none; margin: 0; padding: 0; }
.merge-field-list li { margin-bottom: var(--s3); }
.merge-field-list .btn { font-family: var(--font-mono); margin-right: var(--s2); }
.letter-preview {
  white-space: pre-wrap; font-family: var(--font); margin: 0;
  background: var(--surface-sunk); border-radius: var(--radius-field);
  padding: var(--s4); min-height: 6em;
}

/* Selection + action bar above and below the applications list (D4). */
.send-bar { flex-wrap: wrap; gap: var(--s3); margin: var(--s3) 0; align-items: center; }
