/*
 * Mobile first, and mobile mostly: this form is filled in on a phone, one
 * thumb, often standing up. Everything below follows from that — full-width
 * tap targets no smaller than 44px, no horizontal scrolling anywhere (a matrix
 * reflows to one question per row rather than becoming a grid), and a colour
 * scheme that follows the device rather than fighting it.
 */

:root {
  --bg: #f6f7f7;
  --surface: #fff;
  --line: #dcdfdf;
  --ink: #16211f;
  --muted: #5c6b68;
  --accent: #00695f;
  --accent-ink: #fff;
  --accent-soft: #e3f0ee;
  --danger: #a4302a;
  --radius: 12px;
  --pad: 1rem;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1211;
    --surface: #141d1c;
    --line: #2a3634;
    --ink: #e8efed;
    --muted: #93a5a1;
    --accent: #4db6a4;
    --accent-ink: #06201c;
    --accent-soft: #17302c;
    --danger: #f0928c;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

/* --------------------------------------------------------------- app frame */

.app-head {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: max(0.6rem, env(safe-area-inset-top)) var(--pad) 0;
}

.app-head-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
}

.app-title { margin: 0; font-size: 0.9rem; font-weight: 600; }
/* Sits between the title and the picker, and holds its line whether or not it
   has anything to say, so the header does not jump when it appears. */
.saved {
  flex: 1;
  margin: 0;
  min-height: 1.2em;
  text-align: end;
  font-size: 0.75rem;
  color: var(--muted);
}

.progress { height: 3px; background: var(--line); margin-top: 0.6rem; }
.progress-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.25s ease;
}
/* htmx flips this class on for the duration of a fetch. */
.progress.htmx-request { opacity: 0.5; }

main {
  max-width: 40rem;
  margin: 0 auto;
  padding: var(--pad) var(--pad) 5rem;
}

.loading { color: var(--muted); text-align: center; padding: 3rem 0; }

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

.section-head { margin: 0.5rem 0 1.25rem; }
.section-head h2 { margin: 0.15rem 0 0; font-size: 1.4rem; line-height: 1.25; }
.eyebrow {
  margin: 0;
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.lead { color: var(--muted); margin: 0 0 1.25rem; }
.note { color: var(--muted); font-size: 0.85rem; }

.badge {
  display: inline-block;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  padding: 0.1rem 0.5rem;
  font-size: 0.7rem;
  text-transform: none;
  letter-spacing: 0;
}

.group {
  border: none;
  border-top: 1px solid var(--line);
  margin: 0 0 1.5rem;
  padding: 1rem 0 0;
}
.group legend {
  padding: 0;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.field {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--pad);
  margin: 0.75rem 0;
}
.field[hidden] { display: none; }
.field.invalid { border-color: var(--danger); }

.label { margin: 0 0 0.15rem; font-weight: 500; }
.req { color: var(--danger); text-decoration: none; }
.hint { margin: 0 0 0.5rem; font-size: 0.85rem; color: var(--muted); }
.error { margin: 0.5rem 0 0; font-size: 0.85rem; color: var(--danger); }

/* ---------------------------------------------------------------- controls */

input[type="text"], input[type="email"], input[type="tel"], input[type="date"],
input[type="number"], input[type="file"], textarea, select {
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.7rem 0.75rem;
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  /* 16px keeps iOS Safari from zooming the page on focus. */
  font-size: 16px;
}
textarea { resize: vertical; min-height: 3rem; }
select { appearance: none; }

input:focus-visible, textarea:focus-visible, select:focus-visible,
button:focus-visible, .pill:focus-within {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.row { display: flex; gap: 0.5rem; align-items: flex-start; }
.row select { flex: 0 0 6rem; }

/*
 * Choice inputs are visually hidden and driven through their labels, so the
 * whole pill is the tap target rather than a 20px circle.
 */
.choices {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.6rem;
}
.choices input { position: absolute; opacity: 0; pointer-events: none; }

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0.4rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
  cursor: pointer;
  user-select: none;
}
.choices input:checked + .pill {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
}

/* A numeric scale gets equal-width cells so the row reads as a scale. */
.scale .pill { flex: 1; justify-content: center; min-width: 2.75rem; padding: 0.4rem; }

.consent { display: flex; gap: 0.75rem; align-items: center; margin-top: 0.6rem; }
.consent input { width: 24px; height: 24px; accent-color: var(--accent); flex: none; }
.consent label { min-height: 44px; display: flex; align-items: center; }

.calc {
  display: inline-block;
  margin-top: 0.5rem;
  font-size: 1.25rem;
  font-variant-numeric: tabular-nums;
}

/* ----------------------------------------------------------------- matrix */

/*
 * One question per row, always. question-patterns.md requires a matrix to
 * reflow on mobile, and the same stacked shape is fine on a desktop, so there
 * is no grid variant to get wrong.
 */
.matrix-row { padding: 0.6rem 0; border-top: 1px dashed var(--line); }
.matrix-row:first-child { border-top: none; }
.matrix-label { margin: 0; font-size: 0.95rem; }
.matrix .choices { margin-top: 0.35rem; }
.matrix .pill { min-height: 38px; font-size: 0.85rem; }

/* -------------------------------------------------------------- repeating */

.repeat-item {
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 0.75rem;
  margin-top: 0.6rem;
}
.repeat-item .sub { font-size: 0.8rem; color: var(--muted); }
.repeat-head { display: flex; justify-content: space-between; align-items: center; }

.sig canvas {
  display: block;
  width: 100%;
  height: 140px;
  margin-top: 0.5rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  touch-action: none;
}

/* ---------------------------------------------------------------- buttons */

button {
  font: inherit;
  border-radius: 999px;
  min-height: 48px;
  padding: 0.6rem 1.4rem;
  cursor: pointer;
  border: 1px solid transparent;
}
.primary { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.ghost { background: transparent; color: var(--muted); border-color: var(--line); }
.link {
  background: none;
  border: none;
  color: var(--accent);
  min-height: 44px;
  padding: 0.5rem 0;
  text-decoration: underline;
}

.pager {
  display: flex;
  gap: 0.75rem;
  justify-content: space-between;
  margin-top: 1.5rem;
  padding-bottom: env(safe-area-inset-bottom);
}
.pager .primary { flex: 1; }

/* ----------------------------------------------------------------- review */

.review-group { margin-bottom: 1.25rem; }
.review-group h3 { font-size: 0.8rem; text-transform: uppercase; color: var(--muted); margin: 0 0 0.4rem; letter-spacing: 0.05em; }

.answer {
  display: block;
  width: 100%;
  text-align: start;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.75rem var(--pad);
  margin: 0.4rem 0;
  min-height: 0;
}
.answer .q { display: block; font-size: 0.85rem; color: var(--muted); }
.answer .a { display: block; margin-top: 0.15rem; }
.answer.missing { border-color: var(--danger); }
.answer.missing .a { color: var(--danger); }

.blocking {
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  padding: var(--pad);
  margin: 1rem 0;
}
.blocking h3 { margin: 0 0 0.5rem; font-size: 1rem; color: var(--danger); }

#done { border-top: 1px solid var(--line); margin-top: 1.5rem; padding-top: 1rem; }

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

/* ------------------------------------------------------------ start screen */

.toc {
  list-style: none;
  margin: 0 0 1.5rem;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}
.toc li {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem var(--pad);
  border-top: 1px solid var(--line);
}
.toc li:first-child { border-top: none; }
.toc .n {
  flex: none;
  display: grid;
  place-items: center;
  width: 1.65rem;
  height: 1.65rem;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
}
.toc .badge { margin-inline-start: auto; }

/* ------------------------------------------------------- language and RTL */

.lang { display: flex; align-items: center; }
.lang select {
  width: auto;
  margin: 0;
  padding: 0.15rem 1.5rem 0.15rem 0.4rem;
  font-size: 0.8rem;
  border-radius: 999px;
  background: var(--bg) no-repeat right 0.5rem center;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-size: 5px 5px, 5px 5px;
  background-position: right 0.75rem center, right 0.5rem center;
}
[dir="rtl"] .lang select {
  padding: 0.15rem 0.4rem 0.15rem 1.5rem;
  background-position: left 0.5rem center, left 0.75rem center;
  background-image: linear-gradient(45deg, var(--muted) 50%, transparent 50%),
                    linear-gradient(135deg, transparent 50%, var(--muted) 50%);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/*
 * Hebrew.
 *
 * The layout itself needs nothing: everything above is flexbox or a symmetric
 * shorthand, and `dir="rtl"` on the root element flips it. What does need
 * saying is typographic. `text-transform: uppercase` is a no-op in Hebrew, and
 * the letter-spacing that goes with it in the Latin headings actively breaks
 * the script — Hebrew is not letterspaced, and doing it costs legibility for
 * no visual gain.
 */
[lang="he"] {
  font-family: "Segoe UI", "Arial Hebrew", Arial, system-ui, sans-serif;
}
[lang="he"] .eyebrow,
[lang="he"] .group legend,
[lang="he"] .review-group h3 {
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.85rem;
}

/* An answer is stored in English and shown translated; the value itself can
   still be Latin (a tracker's brand name, a unit), so let the browser order
   each run by its own direction rather than forcing the paragraph's. */
.answer .a, .calc { unicode-bidi: plaintext; }
