/* The phone page: a light, calm page with one short question, a hint line
   and large buttons; the long instruction waits behind "Show instructions".
   Every text pair clears WCAG AA (4.5:1). */

:root {
  color-scheme: light;
  --color-page: #f6f4ee;
  --color-surface: #ffffff;
  --color-surface-muted: #efece3;
  --color-surface-pressed: #eae6db;
  --color-line: #e4e0d6;
  --color-control-line: #ddd8cc;
  --color-text: #1f2b2e;
  --color-text-muted: #5a676b;
  --color-text-soft: #4a5659;
  --color-disabled-text: #6b7477;
  --color-accent: #2f6f68;
  --color-accent-soft: #e4efec;
  --color-accent-text: #22524c;
  --color-on-accent: #ffffff;
  /* The screen's guide lines (FOG_COLOURS.alongSmear and FOG_COLOURS.acrossSmear
     in public/js/stimuli/fog/fog-canvas.js) in darker shades of the same hues:
     the screen draws them on black, the phone on a light page. */
  --color-guide-blue: #2f7fd9;
  --color-guide-blue-surface: #e8f1fc;
  --color-guide-blue-line: #bcd6f5;
  --color-guide-orange: #e08a0b;
  --color-guide-orange-surface: #fdf0de;
  --color-guide-orange-line: #f1cf9e;
  --color-success: #2e7550;
  --color-success-surface: #e5f1e9;
  --color-success-line: #bfdcc9;
  --color-success-text: #1f5a3a;
  --color-warning: #c8871c;
  --color-warning-surface: #fbf0da;
  --color-warning-line: #ebd9b4;
  --color-warning-text: #6f4708;
  --color-danger-line: #c9a89f;
  --color-danger-text: #7a4a40;
  --color-info-surface: #eaf1f7;
  --color-info-line: #cfddea;
  --color-info-text: #2f5274;
  --color-focus: #2f6f68;
  --target-min: 56px;
  --space: 20px;
  --gap: 12px;
  --grid-gap: 10px;
  --radius: 14px;
  --radius-large: 18px;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--color-page);
  color: var(--color-text);
  font-family: var(--font-body);
  font-size: 17px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  margin: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 3px;
}

/* Focus lands here by script so screen readers start at the question; it is not a control. */
.screen__heading:focus {
  outline: none;
}

.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;
}

/* ─── Top bar: the link's state on the left, the eye (or the lab's name) on the right ─── */

.top-bar {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--color-page);
  padding: max(12px, env(safe-area-inset-top)) max(var(--space), env(safe-area-inset-right)) 12px
    max(var(--space), env(safe-area-inset-left));
}

.top-bar__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 1040px;
  margin: 0 auto;
  min-height: 36px;
}

.link-status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--color-surface-muted);
  color: var(--color-text-soft);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.25;
}

.link-status__dot {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-text-muted);
}

.link-status[data-status="connected"] {
  background: var(--color-success-surface);
  color: var(--color-success);
}

.link-status[data-status="connected"] .link-status__dot {
  background: var(--color-success);
}

.link-status[data-status="computer-away"],
.link-status[data-status="reconnecting"] {
  background: var(--color-warning-surface);
  color: var(--color-warning-text);
}

.link-status[data-status="computer-away"] .link-status__dot,
.link-status[data-status="reconnecting"] .link-status__dot {
  background: var(--color-warning);
}

.link-status[data-status="expired"] .link-status__dot {
  background: var(--color-danger-text);
}

.screen-badge {
  flex: none;
  max-width: 45%;
  font-size: 14px;
  color: var(--color-text-muted);
  text-align: right;
}

.screen-badge[data-kind="chip"] {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--color-accent-soft);
  color: var(--color-accent-text);
  font-size: 13px;
  font-weight: 700;
}

/* ─── Page frame ─── */

.phone {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 12px max(var(--space), env(safe-area-inset-right)) max(28px, env(safe-area-inset-bottom))
    max(var(--space), env(safe-area-inset-left));
}

#screen {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.feedback:not(:empty) {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  margin-bottom: var(--space);
}

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.screen--question {
  gap: 24px;
}

.screen__text {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.screen__title {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-text-muted);
}

.screen__heading {
  margin: 0;
  font-size: 23px;
  font-weight: 600;
  line-height: 1.3;
}

.screen__paragraph {
  display: block;
  white-space: pre-line;
}

.screen__paragraph + .screen__paragraph {
  margin-top: 0.6em;
}

.screen__hint {
  margin: 0;
  font-size: 15px;
  line-height: 1.5;
  color: var(--color-text-muted);
}

.screen__feedback:empty,
.screen__next:empty {
  display: none;
}

.screen__feedback {
  margin-top: 8px;
}

.screen__answers {
  container-type: inline-size;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.screen__next {
  display: flex;
  flex-direction: column;
  margin-top: auto;
}

/* ─── The long instruction ─── */

.screen__details {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  margin-top: auto;
}

.screen__next:not(:empty) + .screen__details {
  margin-top: 0;
}

.details-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--color-accent-text);
  font-size: 15px;
  font-weight: 600;
}

.details-toggle__icon {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: transform 0.15s ease;
}

.details-toggle[aria-expanded="true"] .details-toggle__icon {
  transform: rotate(180deg);
}

.details {
  width: 100%;
  padding: 16px 18px;
  border: 1px solid var(--color-line);
  border-radius: var(--radius-large);
  background: var(--color-surface);
  font-size: 16px;
  line-height: 1.55;
}

/* A text-only screen reads its instruction straight away, under the heading. */
.details--open {
  margin-top: 8px;
  padding: 0;
  border: 0;
  background: transparent;
  font-size: 17px;
  color: var(--color-text-soft);
}

.details__paragraph {
  margin: 0;
  white-space: pre-line;
}

.details__paragraph + .details__paragraph {
  margin-top: 0.7em;
}

/* ─── Waiting, idle, end, expired: a short centred message and nothing to press ─── */

.screen--message {
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 0 12px 80px;
  text-align: center;
}

.screen--message .screen__heading {
  font-size: 24px;
  line-height: 1.25;
}

.screen__seal {
  display: grid;
  place-items: center;
  width: 84px;
  height: 84px;
  border-radius: 50%;
  background: var(--color-accent-soft);
  color: var(--color-accent);
}

.screen--end .screen__seal {
  background: var(--color-success-surface);
  color: var(--color-success);
}

.screen--expired .screen__seal,
.screen--unreadable .screen__seal {
  background: var(--color-warning-surface);
  color: var(--color-warning-text);
}

.screen__seal-icon {
  width: 40px;
  height: 40px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.screen__message {
  margin: 0;
  max-width: 26ch;
  font-size: 16px;
  line-height: 1.5;
  color: var(--color-text-muted);
}

.screen__tip {
  margin: 8px 0 0;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--color-surface-muted);
  color: var(--color-text-soft);
  font-size: 14px;
}

.notice {
  margin: 0;
}

.notice:not(:empty) {
  margin-top: 8px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--color-info-line);
  background: var(--color-info-surface);
  color: var(--color-info-text);
  font-size: 15px;
  font-weight: 600;
}

.notice[data-tone="error"] {
  border-color: var(--color-warning-line);
  background: var(--color-warning-surface);
  color: var(--color-warning-text);
}

/* ─── Feedback banner (practice) ─── */

.banner {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 1px solid;
}

.banner--correct {
  background: var(--color-success-surface);
  border-color: var(--color-success-line);
  color: var(--color-success-text);
}

.banner--wrong {
  background: var(--color-warning-surface);
  border-color: var(--color-warning-line);
  color: var(--color-warning-text);
}

.banner__icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  font-size: 15px;
  font-weight: 800;
  color: var(--color-on-accent);
}

.banner--correct .banner__icon {
  background: var(--color-success);
}

.banner--wrong .banner__icon {
  background: var(--color-warning-text);
}

.banner__text {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 1px 0 0;
}

.banner__lead {
  font-size: 16px;
  font-weight: 700;
}

.banner__detail {
  font-size: 15px;
  line-height: 1.45;
}

/* ─── Choice buttons ─── */

.choice {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  min-width: 0;
  min-height: var(--target-min);
  padding: 12px 18px;
  border: 1px solid var(--color-control-line);
  border-radius: var(--radius);
  background: var(--color-surface);
  font-size: 17px;
  font-weight: 600;
  line-height: 1.25;
  text-align: center;
  user-select: none;
  -webkit-user-select: none;
}

.choice--row {
  width: 100%;
  min-height: 60px;
}

.choice--square {
  width: 100%;
  min-height: 72px;
  padding: 4px;
}

.choice--large {
  flex-direction: column;
  gap: 6px;
  width: 100%;
  min-height: 150px;
  border-radius: var(--radius-large);
}

.choice--numeral {
  font-size: 24px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.choice--large.choice--numeral {
  font-size: 40px;
}

.tone-neutral {
  background: var(--color-surface);
  border-color: var(--color-control-line);
  color: var(--color-text);
}

.tone-primary {
  min-height: 60px;
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: var(--color-on-accent);
  font-size: 18px;
}

/* The staircase: blue and orange like the guide lines, label first to read, swatch to match. */
.tone-blue,
.tone-orange {
  justify-content: flex-start;
  min-height: 68px;
  padding: 0 20px;
  border-radius: 16px;
  color: var(--color-text);
  font-size: 18px;
  text-align: left;
}

.tone-blue {
  background: var(--color-guide-blue-surface);
  border-color: var(--color-guide-blue-line);
}

.tone-orange {
  background: var(--color-guide-orange-surface);
  border-color: var(--color-guide-orange-line);
}

/* The staircase's "It's round" keeps the height of the coloured rows beside it. */
.choice-stack:has(.tone-blue) .choice {
  min-height: 68px;
  border-radius: 16px;
}

.choice__swatch {
  flex: none;
  width: 30px;
  height: 6px;
  border-radius: 3px;
}

.tone-blue .choice__swatch {
  background: var(--color-guide-blue);
}

.tone-orange .choice__swatch {
  background: var(--color-guide-orange);
}

/* "Todos iguais" and "Iguais": a quiet way out below the rings. */
.screen--question[data-layout="grid"] .choice-stack .tone-neutral,
.screen--question[data-layout="pair"] .choice-stack .tone-neutral {
  min-height: 56px;
  border-color: transparent;
  background: var(--color-surface-pressed);
}

.choice:active:not(:disabled),
.step-button:active:not(:disabled),
.adjust:active:not(:disabled) {
  transform: scale(0.98);
}

.choice:disabled,
.adjust:disabled,
.step-button:disabled {
  cursor: default;
  opacity: 0.5;
}

/* The patient must still read what they sent and what the feedback marked. */
.choice.is-chosen,
.choice.is-correct,
.choice.is-wrong {
  opacity: 1;
}

.choice.is-chosen {
  border-color: var(--color-text);
  box-shadow: inset 0 0 0 1px var(--color-text);
}

.choice.is-correct {
  border: 2px solid var(--color-success);
  background: var(--color-success-surface);
  color: var(--color-success-text);
  box-shadow: none;
}

.choice.is-wrong {
  border: 2px dashed var(--color-danger-line);
  background: var(--color-surface);
  color: var(--color-danger-text);
  box-shadow: none;
}

.choice__mark {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.2;
}

.choice__mark-symbol {
  display: none;
}

.is-correct .choice__mark-symbol {
  display: inline;
}

/* A mark on a small button (a ring number, a row) sits in the corner. */
.choice--square .choice__mark,
.choice--row .choice__mark {
  position: absolute;
  top: 4px;
  right: 6px;
  font-size: 12px;
}

.choice-stack {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.choice-pair {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--gap);
}

/* ─── Grid of numbered rings ─── */

.choice-grid {
  --grid-columns: 4;
  display: grid;
  grid-template-columns: repeat(var(--grid-columns), minmax(0, 1fr));
  gap: var(--grid-gap);
}

.choice-grid[data-columns="1"] {
  --grid-columns: 1;
}

.choice-grid[data-columns="2"] {
  --grid-columns: 2;
}

.choice-grid[data-columns="3"] {
  --grid-columns: 3;
}

.choice-grid[data-columns="4"] {
  --grid-columns: 4;
}

.choice-grid[data-columns="5"] {
  --grid-columns: 5;
}

.choice-grid[data-columns="6"] {
  --grid-columns: 6;
}

/* Too narrow for 56 px targets: fewer columns, same reading order. The widths
   are n × 56 px + (n − 1) × 10 px gap. Six falls to three so each screen row
   still fills whole phone rows. */
@container (max-width: 385.98px) {
  .choice-grid[data-columns="6"] {
    --grid-columns: 3;
  }
}

@container (max-width: 319.98px) {
  .choice-grid[data-columns="5"] {
    --grid-columns: 4;
  }
}

@container (max-width: 253.98px) {
  .choice-grid[data-columns="4"] {
    --grid-columns: 2;
  }
}

/* ─── Distance field ─── */

.distance-field {
  display: grid;
  gap: 12px;
  padding: 18px;
  border: 1px solid var(--color-line);
  border-radius: var(--radius-large);
  background: var(--color-surface);
}

.distance-field__label {
  font-size: 14px;
  font-weight: 600;
  color: var(--color-text-soft);
}

.distance-field__row {
  display: grid;
  grid-template-columns: var(--target-min) minmax(0, 1fr) var(--target-min);
  gap: 10px;
}

.distance-field__box {
  display: flex;
  align-items: center;
  min-width: 0;
  min-height: 64px;
  border: 2px solid var(--color-accent);
  border-radius: var(--radius);
  background: var(--color-surface);
}

.distance-field__box:focus-within {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}

.distance-field__input {
  flex: 1;
  min-width: 0;
  padding: 6px 4px 6px 10px;
  border: 0;
  background: transparent;
  font-size: 32px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.distance-field__input:focus-visible {
  outline: none;
}

.distance-field__input:disabled {
  opacity: 0.5;
}

.distance-field__input[aria-invalid="true"] {
  color: var(--color-warning-text);
}

/* The unit keeps its width; the number takes the rest, right against it. */
.distance-field__unit {
  flex: none;
  padding: 0 14px 0 6px;
  font-size: 18px;
  color: var(--color-text-muted);
}

.step-button {
  display: grid;
  place-items: center;
  min-height: 64px;
  padding: 0;
  border: 1px solid var(--color-control-line);
  border-radius: var(--radius);
  background: var(--color-page);
  font-size: 26px;
  font-weight: 500;
}

.field-error {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  color: var(--color-warning-text);
}

.field-error:empty {
  display: none;
}

/* ─── Adjust: press and hold ─── */

.adjust-pad {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--gap);
}

/* The page must not scroll, select or open a menu under a held finger. */
.adjust {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 0;
  min-height: 104px;
  padding: 10px;
  border: 1px solid var(--color-control-line);
  border-radius: var(--radius-large);
  background: var(--color-surface);
  font-size: 16px;
  font-weight: 600;
  line-height: 1.25;
  text-align: center;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.adjust__icon {
  width: 30px;
  height: 30px;
  fill: none;
  stroke: var(--color-accent-text);
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.adjust.is-holding {
  background: var(--color-accent-soft);
  border-color: var(--color-accent);
}

/* ─── Landscape: the question on the left, the answers on the right ─── */

@media (orientation: landscape) and (min-width: 640px) {
  .phone {
    max-width: 1040px;
  }

  .screen--question {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr);
    gap: 20px 28px;
    align-items: start;
  }

  .screen--question .screen__answers,
  .screen--question .screen__next,
  .screen--question .screen__details {
    grid-column: 2;
  }

  .screen--question .screen__text {
    grid-row: 1 / span 3;
  }

  .screen--message {
    padding-bottom: 24px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .choice:active:not(:disabled),
  .step-button:active:not(:disabled),
  .adjust:active:not(:disabled) {
    transform: none;
  }

  .details-toggle__icon {
    transition: none;
  }
}
