:root {
  color-scheme: light dark;
  --bg: #f7f5f2; --surface: #ffffff; --ink: #1c1f22; --muted: #5c6166; --line: #e3ded7;
  --accent: #2f6f5e; --accent-ink: #ffffff; --client-bg: #2f6f5e; --client-ink: #ffffff;
  --client-soft: #e4ede8; --therapist-bg: #ece8e1; --therapist-ink: #1c1f22;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101418; --surface: #181d22; --ink: #e9e7e3; --muted: #9aa3ab; --line: #262d34;
    --accent: #6fbfa5; --accent-ink: #0b0f12; --client-bg: #2c5a4d; --client-ink: #eaf4ef;
    --client-soft: #1e2a26; --therapist-bg: #212830; --therapist-ink: #e9e7e3;
  }
}

* { box-sizing: border-box; }
/* The chat scrolls inside #messages: the page itself must never grow past the viewport. */
html, body { height: 100%; overflow: hidden; }
html { background: var(--bg); }

body {
  margin: 0 auto; max-width: 640px;
  /* The tab bar is fixed over the bottom edge: its strip is kept free here. */
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column;
  background: var(--bg); color: var(--ink);
  font: 17px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-text-size-adjust: 100%;
}

main { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; padding: 0 24px; }

/* The bar is the app's own element: Ionic's custom elements draw inside a shadow root the page
   cannot size, and the CDN may be blocked, so the shell never depends on either. */
#tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  max-width: 640px; margin: 0 auto;
  display: flex; flex-direction: row;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--line);
}

#tabbar .tab {
  flex: 1 1 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; min-width: 44px; min-height: 56px; padding: 6px 4px;
  background: none; border: 0; border-radius: 0; color: var(--muted);
  font: inherit; font-size: 12px; cursor: pointer;
}
#tabbar .tab[aria-selected="true"] { color: var(--accent); font-weight: 600; }
#tabbar svg {
  width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}

h2 { margin: 0; font-size: 1rem; font-weight: 600; }
h3 { margin: 16px 0 0; font-size: 0.8rem; font-weight: 700; letter-spacing: 0.06em; color: var(--muted); }

button {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px;
  min-height: 44px; cursor: pointer;
}
button:disabled { opacity: 0.55; cursor: default; }
button[type="submit"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.ghost { background: transparent; border-color: transparent; color: var(--muted); min-height: 40px; padding: 6px 8px; }

#status {
  /* A standalone iOS session draws the top of the page under the status bar. */
  position: fixed; top: max(8px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); z-index: 20;
  width: calc(100% - 32px); max-width: 608px; margin: 0; padding: 8px 12px;
  border-radius: 10px; background: var(--therapist-bg); font-size: 0.9rem;
  pointer-events: none; /* It floats over the header: taps must reach what it covers. */
}

.screen { display: none; }
.screen:not([hidden]) {
  display: flex; flex-direction: column; gap: 12px; flex: 1; min-height: 0;
  padding: max(12px, env(safe-area-inset-top)) 0 12px;
}
#chat:not([hidden]) { overflow: hidden; padding-bottom: 0; }
/* These screens have no inner scroller: the screen owns the scroll. */
#you:not([hidden]), #progress:not([hidden]) { overflow-y: auto; }
#you > p, #facts p { margin: 0; }

.bar { display: flex; justify-content: flex-end; flex: none; }

.banner {
  flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
}
.banner p { margin: 0; font-size: 0.9rem; }
.banner button {
  min-height: 36px; padding: 6px 12px; font-weight: 600;
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink);
}
/* One primary action per section: Later (and a banner's Discard) is a text button, not a second accent. */
.banner .ghost { background: transparent; border-color: transparent; color: var(--muted); }
.banner[hidden] { display: none; }

/* The way back into a postponed check-in: a button, so it can take focus. */
#checkin-badge {
  align-self: flex-start; border-radius: 999px; padding: 10px 16px;
  color: var(--accent); font-weight: 600;
}
#checkin-badge[hidden] { display: none; }

.sheet {
  /* Above the header: a full-screen sheet still covers everything, including the tabs. */
  position: fixed; inset: 0; z-index: 40; display: none; flex-direction: column; gap: 12px;
  padding: max(12px, env(safe-area-inset-top)) 24px calc(12px + env(safe-area-inset-bottom));
  background: var(--bg); overflow-y: auto;
}
.sheet:not([hidden]) { display: flex; }
.sheet h2, .sheet form { width: 100%; max-width: 608px; margin: 0 auto; }
.sheet form { display: flex; flex-direction: column; flex: 1; }
.sheet-actions {
  position: sticky; bottom: 0; display: flex; gap: 8px; margin-top: 16px;
  padding: 8px 0; background: var(--bg);
}
.sheet-actions #checkin-submit { flex: 1; }

#messages {
  list-style: none; margin: 0; padding: 0; flex: 1; min-height: 0;
  display: flex; flex-direction: column; gap: 18px;
  overflow-y: auto; overscroll-behavior: contain;
}
/* An empty conversation leaves the whole screen to the voice control. */
#messages:empty { flex: none; }

.bubble { max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
/* The therapist speaks in plain text: the words are the interface, not a card around them. */
.bubble.therapist { align-self: stretch; max-width: 100%; font-size: 17px; line-height: 1.5; color: var(--ink); }
.bubble.client { align-self: flex-end; padding: 10px 14px; border-radius: 18px; background: var(--client-soft); color: var(--ink); }
.bubble.client.pending { opacity: 0.65; }
.bubble.client.failed { border: 1px dashed var(--muted); }
.bubble.note { align-self: center; max-width: 100%; color: var(--muted); font-size: 0.85rem; text-align: center; }

#composer {
  display: flex; align-items: flex-end; gap: 8px; flex: none;
  padding: 10px 0;
  background: var(--bg); border-top: 1px solid var(--line);
}
#composer button { flex: none; }

textarea {
  flex: 1; min-width: 0; font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 14px; padding: 12px;
  min-height: 48px; max-height: 140px; resize: none;
}

.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; }

.item { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; }
.item select {
  width: 100%; min-height: 44px; padding: 10px 12px; font: inherit;
  color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px;
}

#facts-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.fact p { margin: 0 0 6px; }
.fact button { margin-top: 8px; }
blockquote { margin: 0; padding-left: 10px; border-left: 3px solid var(--line); color: var(--muted); font-size: 0.9rem; }

.charts { display: flex; flex-direction: column; gap: 12px; }
figure.score { margin: 0; }
figure.score figcaption { font-size: 0.9rem; font-weight: 600; }
.chart { display: block; width: 100%; height: auto; }
.chart .band { fill: var(--line); opacity: 0.4; }
.chart .axis { stroke: var(--line); stroke-width: 1; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--accent); }
.chart text { fill: var(--muted); font-size: 9px; font-family: inherit; }
.weeks, .counts { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; }
.counts li { display: flex; justify-content: space-between; gap: 12px; }

.muted { color: var(--muted); }

.today { flex: none; display: flex; flex-direction: column; gap: 8px; }
.today[hidden] { display: none; }
#mood-form { display: flex; flex-direction: column; gap: 8px; }
#mood-form[hidden] { display: none; }
#mood-question { font-weight: 600; }
#mood-range { width: 100%; min-height: 44px; margin: 0; accent-color: var(--accent); }
.mood-ends {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  color: var(--muted); font-size: 0.85rem;
}
.mood-ends output { color: var(--ink); font-weight: 700; }
#mood-note, #task-form input {
  width: 100%; min-height: 44px; padding: 10px 12px; font: inherit;
  color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px;
}
#task-card h2 { margin: 0; font-size: 1rem; }
#task-card p { margin: 0; }
#task-form { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
#task-form[hidden] { display: none; }
.row { display: flex; flex-wrap: wrap; gap: 8px; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

#exercises:not([hidden]) { overflow-y: auto; }
#exercises-home, #exercise-flow, #exercise-detail { display: flex; flex-direction: column; gap: 12px; }
#exercise-flow[hidden], #exercise-detail[hidden], #exercises-home[hidden] { display: none; }
#exercise-menu, #exercise-history, #exercise-step-body, #exercise-detail-body,
.exercise-list { display: flex; flex-direction: column; gap: 10px; }
#exercise-history { list-style: none; margin: 0; padding: 0; }

.exercise-card { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; text-align: left; }
.exercise-card strong { font-size: 1rem; }
.exercise-history-open {
  display: flex; flex-direction: column; gap: 2px; width: 100%;
  text-align: left; background: var(--surface);
}
.exercise-history-open strong { font-weight: 600; }

.exercise-field { display: flex; flex-direction: column; gap: 6px; width: 100%; }
.exercise-field > span { color: var(--muted); font-size: 0.9rem; }
.exercise-control {
  width: 100%; min-height: 44px; padding: 10px 12px; font: inherit;
  color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px;
}
.exercise-area { min-height: 88px; resize: vertical; }
input[type="range"].exercise-control { padding: 0; accent-color: var(--accent); }
.exercise-prompt { margin: 0; font-size: 1.2rem; font-weight: 700; }
.exercise-read { margin: 0 0 8px; overflow-wrap: anywhere; white-space: pre-wrap; }
.exercise-done { color: var(--accent); font-weight: 600; margin: 4px 0; }
.exercise-activity { display: flex; flex-direction: column; gap: 8px; }

.step-actions {
  position: sticky; bottom: 0; display: flex; gap: 8px; margin-top: 12px;
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom)); background: var(--bg);
}
.step-actions button { flex: 1; }

/* Unit 4: one section style on You, Practice and Progress. */
.group-title {
  margin: 0 0 8px; font-size: 0.75rem; font-weight: 700;
  letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted);
}
.muted.empty { margin: 0; padding: 16px 8px; text-align: center; font-size: 0.9rem; }
/* "2 min" belongs on the line of the question it times. */
#checkin-banner p { min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* The voice control is the default view in every Talk state: a prompt that hides the section
   while nothing is being said has no say here. A device without usable speech keeps only the
   composer, and the inline style in index.html owns that case. */
html:not([data-speech="no"]) #voice[hidden][data-phase="idle"] { display: flex; }
html:not([data-speech="no"]) #voice[data-phase="idle"] #mic[hidden] { display: inline-flex; }

/* The overflow action belongs to the reply above it: a quiet icon at the message's right edge,
   never a line of its own. The row costs no height (the 44px target and the 18px gap #messages
   leaves are both pulled back up), so it sits over the reply's last line and stays a thumb-sized
   target with no hover at all. */
#messages > .flag-row {
  position: relative; z-index: 1;
  /* #messages may align its items to the start; the row must still span the whole column so its
     icon can sit at the right edge. */
  align-self: stretch; width: 100%; box-sizing: border-box;
  display: flex; justify-content: flex-end;
  height: 44px; margin-top: -62px; /* the 44px target plus the 18px gap #messages leaves */
}
.bubble.therapist:has(+ .flag-row) { padding-right: 44px; }
#more {
  margin-left: auto;
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; min-width: 44px; height: 44px; padding: 0;
  font-size: 0; color: var(--muted); opacity: 0.5;
  background: transparent; border: 0; border-radius: 999px;
}
#more svg { width: 20px; height: 20px; fill: currentColor; }
#more:hover, #more:focus-visible, #more:active, #more[aria-expanded="true"] { opacity: 1; }
@media (prefers-reduced-motion: no-preference) {
  #more { transition: opacity 120ms ease-out; }
}
.flag-menu {
  position: absolute; top: 44px; right: 0; z-index: 5; padding: 4px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.12);
}
.flag-menu[hidden] { display: none; }
