/* The Mo tester page (beta v1 SPEC §5). Colour and type tokens copied from app/src/styles/tokens.css (--ui-*).
   Mobile first: 390 x 844, nothing wider than 320 px is fixed, every tap target is at least 44 px (.tap). Phone to
   1920 desktop, both ways round (brief P1): one centred column (--col), font sizes in rem so browser and OS text size
   apply, and the layout wraps at 200% text. */
:root {
  /* the tab bar's height with its bottom safe area; app.js writes the measured height (it grows when text wraps) */
  --tabs-h: calc(56px + env(safe-area-inset-bottom));
  /* the keyboard's height over the page on iOS (chat.js, from visualViewport); 0 where the page itself resizes */
  --kb: 0px;
  --col: 480px; /* the sign-in screens: a narrower readable column */
  --ui-ink: rgba(0, 0, 0, 0.9);
  --ui-ink-60: rgba(0, 0, 0, 0.6);
  --ui-ink-40: rgba(0, 0, 0, 0.4);
  --ui-fill-6: rgba(0, 0, 0, 0.06);
  --ui-fill-10: rgba(0, 0, 0, 0.1);
  --ui-fill-12: rgba(0, 0, 0, 0.12);
  --ui-line: rgba(0, 0, 0, 0.06);
  --ui-scrim: rgba(0, 0, 0, 0.6);
  --ui-surface: #fff;
  --ui-red-ink: #ae1b14;
  --ui-care-bg: #fff3eb; /* the crisis card: a calm warm fill, never an alarm red */
  --ui-care-line: #efc3ad;
  --ui-font: Helvetica, Arial, system-ui, sans-serif;
  --ui-fs-14: 0.875rem;
  --ui-fs-16: 1rem;
  --ui-fs-20: 1.25rem;
  --ui-fs-28: 1.75rem;
  --ui-r-12: 12px;
  --ui-r-32: 32px;
}

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

html,
body {
  margin: 0;
  background: var(--ui-surface);
  color: var(--ui-ink);
  font-family: var(--ui-font);
  font-size: var(--ui-fs-16);
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

/* on html only: overflow on body too would make body a scroll box, and the chat's sticky input would stop sticking */
html {
  overflow-x: hidden;
}

/* the top safe area is on .top (sticky), so the bar sits flush at the top of the screen while the page scrolls */
body {
  min-height: 100dvh;
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

[hidden] {
  display: none !important;
}

/* the signed-in screens (chat, journal, day, its chats, settings): a wider centred column */
body.tabbed {
  --col: 680px;
}

/* Support stays one tap from every screen (SPEC §5): the bar sticks at the top however far the page scrolls (F16) */
.top {
  position: sticky;
  top: 0;
  z-index: 6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  /* the bar's background is full width (out under the side safe areas too); its contents line up with the column */
  margin: 0 calc(-1 * env(safe-area-inset-right)) 0 calc(-1 * env(safe-area-inset-left));
  padding: calc(4px + env(safe-area-inset-top))
    calc(env(safe-area-inset-right) + max(16px, calc((100% - var(--col)) / 2 + 16px))) 4px
    calc(env(safe-area-inset-left) + max(16px, calc((100% - var(--col)) / 2 + 16px)));
  border-bottom: 1px solid var(--ui-line);
  background: var(--ui-surface);
}

.brand {
  font-size: var(--ui-fs-20);
  font-weight: 700;
}

main {
  width: 100%;
  max-width: var(--col);
  margin: 0 auto;
  padding: 16px;
}

h1 {
  font-size: var(--ui-fs-28);
  line-height: 1.2;
  margin: 8px 0 16px;
}

h2 {
  font-size: var(--ui-fs-20);
  margin: 0 0 12px;
}

p {
  margin: 0 0 16px;
  overflow-wrap: anywhere;
}

/* every button, link, label and text field */
.tap {
  min-height: 44px;
  min-width: 44px;
  font: inherit;
  color: inherit;
}

button.tap {
  border: 0;
  border-radius: var(--ui-r-12);
  padding: 10px 16px;
  cursor: pointer;
  background: var(--ui-fill-6);
}

button.primary {
  display: block;
  width: 100%;
  margin: 8px 0;
  background: var(--ui-ink);
  color: var(--ui-surface);
  font-weight: 700;
}

button.primary:disabled {
  background: var(--ui-fill-12);
  color: var(--ui-ink-60);
  cursor: default;
}

button.link {
  display: block;
  width: 100%;
  background: none;
  text-decoration: underline;
  color: var(--ui-ink-60);
}

.support-btn {
  font-weight: 700;
}

label.field {
  display: flex;
  align-items: flex-end;
  font-size: var(--ui-fs-14);
  color: var(--ui-ink-60);
}

input.tap {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--ui-fill-12);
  border-radius: var(--ui-r-12);
  font-size: max(16px, var(--ui-fs-16)); /* at least 16 px: iOS does not zoom the page on focus */
  background: var(--ui-surface);
}

#code-input {
  letter-spacing: 0.3em;
  font-size: var(--ui-fs-20);
}

.ticks,
.ticks-optional,
.country {
  margin: 0 0 16px;
}

.country {
  border: 0;
  padding: 0;
}

legend {
  font-weight: 700;
  margin-bottom: 4px;
}

label.tick {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 0;
  cursor: pointer;
}

label.tick input {
  flex: none;
  width: 22px;
  height: 22px;
  margin: 1px 0 0;
}

.error {
  color: var(--ui-red-ink);
  min-height: 0;
}

.error:empty {
  display: none;
}

.lines p {
  margin-bottom: 4px;
}

.support-country {
  margin-bottom: 16px;
}

.scrim {
  position: fixed;
  inset: 0;
  background: var(--ui-scrim);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 10;
  padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

.sheet {
  width: 100%;
  max-width: 560px;
  background: var(--ui-surface);
  border-radius: var(--ui-r-32) var(--ui-r-32) 0 0;
  padding: 24px 16px calc(16px + env(safe-area-inset-bottom));
}

/* ---- tab bar (Chat · Journal · Settings) ---- */
.tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-wrap: wrap; /* at 200% text on a narrow phone a label takes a row of its own rather than being cut off */
  min-height: var(--tabs-h);
  padding: 6px max(calc(8px + env(safe-area-inset-right)), calc((100% - var(--col)) / 2 + 8px))
    env(safe-area-inset-bottom) max(calc(8px + env(safe-area-inset-left)), calc((100% - var(--col)) / 2 + 8px));
  background: var(--ui-surface);
  border-top: 1px solid var(--ui-line);
  z-index: 5;
}

button.tab {
  flex: 1 1 auto;
  background: none;
  color: var(--ui-ink-60);
}

button.tab[aria-current='page'] {
  color: var(--ui-ink);
  font-weight: 700;
}

body.tabbed main {
  padding-bottom: var(--tabs-h);
}

/* a crisis card is pinned in view (chat.js, brief W1b): the tab bar steps aside so the card has the room (200% text);
   it comes back when the card scrolls out of view, the composer is tapped or Support opens */
body.carded .tabs {
  display: none;
}

body.carded.tabbed main {
  padding-bottom: env(safe-area-inset-bottom);
}

body.carded .composer {
  bottom: env(safe-area-inset-bottom);
}

/* the composer has focus on a touch screen (chat.js): the keyboard is up, so the tab bar steps aside */
body.composing .tabs {
  display: none;
}

body.composing.tabbed main {
  padding-bottom: env(safe-area-inset-bottom);
}

body.composing .composer {
  bottom: max(var(--kb), env(safe-area-inset-bottom));
}

/* ---- chat ---- */
.log {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-bottom: 8px;
}

.msg {
  max-width: 80%;
  padding: 8px 12px;
  border-radius: var(--ui-r-12);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.me {
  align-self: flex-end;
  background: var(--ui-ink);
  color: var(--ui-surface);
}

.msg.ai {
  max-width: 100%;
  background: var(--ui-fill-6);
}

.mo-row {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  max-width: 90%;
}

.mo-col {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  /* the 500 ms long-press flags the message: no iOS text selection or callout over the sheet */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

button.flag-btn {
  flex: none;
  padding: 0;
  background: none;
  color: var(--ui-ink-40);
  font-size: var(--ui-fs-20);
  line-height: 1;
}

.typing {
  display: flex;
  gap: 4px;
  align-self: flex-start;
  padding: 14px 12px;
}

.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ui-ink-40);
  animation: blink 1.2s infinite ease-in-out;
}

.typing span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes blink {
  0%,
  80%,
  100% {
    opacity: 0.3;
  }
  40% {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .typing span {
    animation: none;
  }
}

/* emoji reactions (spec 2026-11-21 §5, only with cfg.reactions): a small badge on the bubble's bottom edge, WhatsApp
   style: Mo's on the tester's bubble at its left corner, the tester's on Mo's at its right. Sizes in em follow the text. */
.msg.reacted {
  position: relative;
  margin-bottom: 1rem;
}

.react-badge {
  position: absolute;
  bottom: -1.1em;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.75em;
  height: 1.75em;
  padding: 0 0.25em;
  border-radius: 999px;
  background: var(--ui-surface);
  box-shadow: 0 0 0 1px var(--ui-fill-12);
  font-size: var(--ui-fs-14);
  line-height: 1;
  white-space: nowrap;
}

.msg.me .react-badge {
  left: 8px;
}

.msg.ai .react-badge {
  right: 8px;
}

.react-badge.pop {
  animation: react-pop 220ms ease-out;
}

@keyframes react-pop {
  0% {
    transform: scale(0.6);
  }
  60% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

@media (prefers-reduced-motion: reduce) {
  .react-badge.pop {
    animation: none;
  }
}

/* the reaction sheet: six emoji buttons, 44 px each, on one row at 320 px (they wrap at larger text) */
.react-picks {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 4px;
  margin-bottom: 16px;
}

button.react-pick {
  padding: 0;
  font-size: var(--ui-fs-28);
  line-height: 1;
}

button.react-pick[aria-pressed='true'] {
  background: var(--ui-fill-12);
  box-shadow: inset 0 0 0 2px var(--ui-ink);
}

#react-report {
  width: 100%;
  margin-bottom: 8px;
}

/* app lines (the AI line, notes, errors): never styled as a Mo bubble */
.ai-line,
.note {
  align-self: center;
  margin: 4px 0;
  font-size: var(--ui-fs-14);
  color: var(--ui-ink-60);
  text-align: center;
}

.note.err {
  color: var(--ui-red-ink);
}

.card {
  padding: 12px;
  border: 1px solid var(--ui-fill-12);
  border-radius: var(--ui-r-12);
  overflow-wrap: anywhere;
}

.card.resources {
  background: var(--ui-care-bg);
  border: 2px solid var(--ui-care-line);
  scroll-margin-top: 8px; /* chat.js sets it to the header's height when the card arrives */
}

.card > div + div {
  margin-top: 12px;
}

/* tap-to-call / text buttons on a row of their own (the chat card, the support sheet, "not me") */
.links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}

.links a.tap {
  display: inline-flex;
  align-items: center;
  padding: 0 14px;
  border: 1px solid var(--ui-care-line);
  border-radius: var(--ui-r-32);
  background: var(--ui-surface);
  color: var(--ui-ink);
  font-weight: 700;
  text-decoration: none;
}

.card > a.tap {
  display: inline-flex;
  align-items: center;
  padding: 0 6px;
  color: inherit;
}

.composer {
  position: sticky;
  bottom: var(--tabs-h);
  padding: 8px 0;
  background: var(--ui-surface);
}

.adding {
  margin: 0 0 8px;
  font-size: var(--ui-fs-14);
  color: var(--ui-ink-60);
}

.compose-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

textarea.tap {
  display: block;
  width: 100%;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--ui-fill-12);
  border-radius: var(--ui-r-12);
  font-size: max(16px, var(--ui-fs-16)); /* at least 16 px: iOS does not zoom the page on focus */
  background: var(--ui-surface);
  resize: none;
}

#chat-input {
  flex: 1;
  max-height: 160px;
}

/* the placeholder never wraps out of the one-row box; at 200% text on a 320 px phone it is smaller so "Say something" is
   whole (brief W1c). 12em = 384 px at 200% text, 192 px at normal text, so normal text keeps its size */
#chat-input::placeholder {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@media (max-width: 12em) {
  #chat-input::placeholder {
    font-size: 0.65em;
  }
}

button.send {
  flex: none;
  background: var(--ui-ink);
  color: var(--ui-surface);
  font-weight: 700;
}

.count {
  margin: 4px 0 8px;
  font-size: var(--ui-fs-14);
  color: var(--ui-ink-60);
  text-align: right;
}

#flag-ui:empty {
  display: none;
}

#flag-ui {
  margin-top: 12px;
}

/* the flag sheet is taller than a 320 x 568 phone: it scrolls inside, with Send and Cancel always in view */
.sheet {
  max-height: 90dvh;
  overflow-y: auto;
}

.sheet-actions {
  position: sticky;
  bottom: 0;
  padding-top: 4px;
  background: var(--ui-surface);
}

/* ---- journal + settings (Task 23) ---- */
.entries {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

button.entry {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  padding: 12px;
  text-align: left;
  border: 1px solid var(--ui-fill-12);
  background: var(--ui-surface);
  overflow-wrap: anywhere;
}

.meta,
.empty,
.na {
  color: var(--ui-ink-60);
  font-size: var(--ui-fs-14);
}

/* the last button clears the fixed tab bar at the bottom of the scroll */
#day {
  padding-bottom: 24px;
}

button.back {
  background: none;
  padding: 0;
  color: var(--ui-ink-60);
}

.summary {
  white-space: pre-wrap;
}

.day-links {
  margin: 0 0 8px;
}

.day-links button.link {
  text-align: left;
  padding: 10px 0;
}

.tag-head {
  display: flex;
  flex-wrap: wrap; /* at 200% text the heading and its Edit button wrap rather than squeeze */
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 16px;
}

.tag-head h2 {
  margin: 0;
}

button.edit {
  flex: none;
  background: none;
  text-decoration: underline;
  color: var(--ui-ink-60);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 8px 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 6px 12px;
  border-radius: var(--ui-r-32);
  background: var(--ui-fill-6);
  font-size: var(--ui-fs-14);
  overflow-wrap: anywhere;
}

button.chip {
  border-radius: var(--ui-r-32);
  font-size: var(--ui-fs-14);
}

button.chip[aria-pressed='true'] {
  background: var(--ui-ink);
  color: var(--ui-surface);
}

.chip.person {
  padding-right: 0;
}

button.chip-x {
  padding: 0 12px;
  background: none;
  color: var(--ui-ink-60);
}

#person-input {
  flex: 1;
  min-width: 0;
}

button.link.danger {
  margin-top: 24px;
  color: var(--ui-red-ink);
}

button.primary.danger {
  background: var(--ui-red-ink);
}

#report-ui:empty {
  display: none;
}

#report-ui {
  margin-top: 12px;
}

/* ---- wider screens: a sheet sits centred as a dialog (brief P1) ---- */
@media (min-width: 769px) {
  .scrim {
    align-items: center;
    padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
      max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  }

  .sheet {
    max-height: 100%;
    border-radius: var(--ui-r-32);
    padding-bottom: 16px;
  }
}

/* ---- short screens (a phone on its side): the tabs move up into the top bar, so the chat keeps its height ----
   Sizes here are in em: in a media query 1em is the browser's text size, so larger text (200%: 1em = 32 px) counts
   as a shorter, narrower screen. 31.25em = 500 px, 35em = 560 px, 22.5em = 360 px, 12.5em = 200 px at 16 px text. */
@media (max-height: 31.25em) and (min-width: 35em) {
  .tabs {
    top: env(safe-area-inset-top);
    bottom: auto;
    left: 50%;
    right: auto;
    z-index: 7; /* over the top bar, between Mo and Support */
    flex-wrap: nowrap;
    min-height: 0;
    padding: 0;
    border-top: 0;
    background: none;
    transform: translateX(-50%);
  }

  button.tab {
    flex: none;
  }

  body.tabbed main {
    padding-bottom: env(safe-area-inset-bottom);
  }

  .composer {
    bottom: env(safe-area-inset-bottom);
  }
}

/* short screens (and a phone with its keyboard up): slimmer bars, so the chat keeps a few lines */
@media (max-height: 31.25em) {
  .top {
    padding-top: env(safe-area-inset-top);
    padding-bottom: 0;
  }

  .composer {
    padding: 4px 0;
  }
}

/* a phone on its side with the keyboard up leaves about 120 px: the "Adding to" line waits until the keyboard closes */
@media (max-height: 12.5em) {
  .adding {
    display: none;
  }
}

/* the crisis card on a short or narrow screen: each service on as few rows as it can, so the card fits between the
   top bar and the composer (568 x 320 on its side, 320 x 568 upright); its call buttons stay 44 px */
@media (max-height: 31.25em), (max-width: 22.5em) {
  .card.resources {
    padding: 6px 10px;
    font-size: var(--ui-fs-14);
    line-height: 1.3;
  }

  .card.resources > div + div {
    margin-top: 6px;
  }

  .card.resources .service {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    column-gap: 8px;
  }

  .card.resources .links {
    margin-top: 0;
    gap: 6px;
  }

  .card.resources .links a.tap {
    padding: 0 10px;
  }
}
