/* FysioBuddy demo: calm, clinical look. System fonts only, no external files.
   Colours are set once as variables, then again for dark mode. */

:root {
  color-scheme: light;
  --bg: #f4f7f8;
  --surface: #ffffff;
  --surface-2: #eef3f4;
  --text: #17262d;
  --muted: #56666e;
  --border: #d5dfe3;
  --accent: #0f766e;
  --accent-soft: #e0f2ef;
  --accent-text: #ffffff;
  --user-bubble: #0f766e;
  --user-text: #ffffff;
  --info-bg: #fff7e0;
  --info-border: #e2c46a;
  --info-text: #4f3d05;
  --flag-bg: #fdeceb;
  --flag-border: #c62828;
  --flag-text: #7f1515;
  --ok: #1b7a3a;
  --problem: #b54708;
  --quote-bar: #0f766e;
  --translation: #3a5560;
  --shadow: 0 1px 2px rgba(16, 40, 48, .06), 0 4px 14px rgba(16, 40, 48, .06);
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

/* Dark mode: follows the device, unless the theme button picked one. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0e1619;
    --surface: #152126;
    --surface-2: #1b2a30;
    --text: #e3ecee;
    --muted: #9db0b7;
    --border: #2a3c43;
    --accent: #3fb8aa;
    --accent-soft: #173a37;
    --accent-text: #05211e;
    --user-bubble: #1f6f67;
    --user-text: #f2fbfa;
    --info-bg: #2f2710;
    --info-border: #7a6421;
    --info-text: #f3e2a8;
    --flag-bg: #3a1618;
    --flag-border: #ef5350;
    --flag-text: #ffc9c5;
    --ok: #5fd08a;
    --problem: #f5a25d;
    --quote-bar: #3fb8aa;
    --translation: #a9c4cc;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1619;
  --surface: #152126;
  --surface-2: #1b2a30;
  --text: #e3ecee;
  --muted: #9db0b7;
  --border: #2a3c43;
  --accent: #3fb8aa;
  --accent-soft: #173a37;
  --accent-text: #05211e;
  --user-bubble: #1f6f67;
  --user-text: #f2fbfa;
  --info-bg: #2f2710;
  --info-border: #7a6421;
  --info-text: #f3e2a8;
  --flag-bg: #3a1618;
  --flag-border: #ef5350;
  --flag-text: #ffc9c5;
  --ok: #5fd08a;
  --problem: #f5a25d;
  --quote-bar: #3fb8aa;
  --translation: #a9c4cc;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
}

/* ---------- Base ---------- */

* { box-sizing: border-box; }
html { font-size: 17px; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}
button, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Top bar and banner ---------- */

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { flex: none; }
.brand-name { font-weight: 700; font-size: 1.15rem; letter-spacing: .01em; }
.brand-sub { color: var(--muted); font-size: .82rem; }
.controls { display: flex; align-items: center; gap: 8px; flex: none; }

.switch {
  display: inline-flex; border: 1px solid var(--border); border-radius: 999px; overflow: hidden;
}
.switch button {
  border: 0; background: transparent; padding: 6px 14px; font-weight: 600; font-size: .85rem;
  min-height: 36px;
}
.switch button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }

.icon-button {
  border: 1px solid var(--border); background: transparent; border-radius: 999px;
  width: 38px; height: 38px; display: grid; place-items: center; font-size: 1rem;
}

.replay-banner {
  margin: 0;
  padding: 10px 24px;
  background: var(--info-bg);
  color: var(--info-text);
  border-bottom: 1px solid var(--info-border);
  font-size: .9rem;
}
.replay-banner strong { font-weight: 700; }
/* Live mode: the same banner in the calm accent colour */
.replay-banner.live { background: var(--accent-soft); color: var(--text); border-bottom-color: var(--accent); }
.status-line {
  margin: 0; padding: 8px 24px; font-size: .85rem;
  background: var(--surface-2); color: var(--muted); border-bottom: 1px solid var(--border);
}

/* ---------- Page layout: cases left, chat right ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(260px, 340px) minmax(0, 1fr);
  gap: 20px;
  max-width: 1320px;
  margin: 0 auto;
  padding: 20px 24px;
}

/* The case list stays in view while the page scrolls through an answer. */
.cases details { position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow-y: auto; padding-right: 4px; }
.cases summary {
  font-weight: 700; font-size: .95rem; list-style: none; cursor: default; padding: 2px 0 6px;
}
.cases summary::-webkit-details-marker { display: none; }
.cases-help { margin: 0 0 10px; color: var(--muted); font-size: .85rem; }
.case-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.side-heading { font-size: .95rem; margin: 16px 0 6px; }
.new-chat-button {
  width: 100%; text-align: left; font-weight: 700; color: var(--accent);
  background: var(--accent-soft); border: 1px solid var(--accent); border-radius: 10px; padding: 9px 12px;
}
.new-chat-button:disabled, .chat-button:disabled, .case-button:disabled { opacity: .55; cursor: not-allowed; }
.chat-list { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 4px; }
.chat-button {
  width: 100%; text-align: left; font-size: .86rem; line-height: 1.35;
  background: transparent; border: 1px solid transparent; border-radius: 8px; padding: 6px 10px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-button:hover { border-color: var(--border); }
.chat-button[aria-current="true"] { background: var(--surface-2); border-color: var(--border); font-weight: 700; }
.case-button {
  width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 12px; display: grid; gap: 2px;
  transition: border-color .15s, background-color .15s;
}
.case-button:hover { border-color: var(--accent); }
.case-button[aria-current="true"] { border-color: var(--accent); background: var(--accent-soft); }
.case-top { display: flex; align-items: center; gap: 8px; font-size: .8rem; font-weight: 700; }
.case-title { font-size: .88rem; line-height: 1.35; }

/* Case type tags */
.tag {
  display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .72rem; font-weight: 700;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.tag.red_flag { background: var(--flag-bg); color: var(--flag-text); border-color: var(--flag-border); }

/* ---------- Chat ---------- */

/* The whole page scrolls (more room on a projector); the message box sticks
   to the bottom of the screen. */
.chat {
  display: flex; flex-direction: column; min-width: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-height: calc(100vh - 150px);
}
.messages { flex: 1; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.messages > * {
  flex-shrink: 0;
  /* when scrolled into view, stay clear of the sticky message box */
  /* --composer-height is set by app.js from the real height of the fields */
  scroll-margin-top: 16px; scroll-margin-bottom: calc(var(--composer-height, 300px) + 16px);
}

.welcome { color: var(--muted); max-width: 62ch; margin: auto; text-align: center; }
.welcome h2 { color: var(--text); font-size: 1.2rem; margin: 0 0 6px; }
.welcome p { margin: 6px 0; }

/* The therapist's message */
.msg-user { align-self: flex-end; max-width: min(78%, 720px); }
.bubble-user {
  background: var(--user-bubble); color: var(--user-text);
  padding: 12px 16px; border-radius: 16px 16px 4px 16px; white-space: pre-wrap;
}
.msg-note { color: var(--muted); font-size: .78rem; margin-top: 4px; text-align: right; }
.bubble-label { display: block; font-size: .75rem; font-weight: 700; opacity: .8; margin-bottom: 2px; }
.faq-label { font-size: .75rem; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
.welcome-try { font-style: italic; }
.suggestions { align-self: flex-start; display: flex; flex-wrap: wrap; gap: 8px; }
.suggestion {
  background: var(--surface); color: var(--accent); border: 1px solid var(--accent); border-radius: 999px;
  padding: 6px 14px; font-size: .88rem; text-align: left;
}
.suggestion:hover { background: var(--accent-soft); }
.suggestion:disabled { opacity: .55; cursor: not-allowed; }
.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;
}

/* System steps: searching, answering */
.step {
  align-self: flex-start; display: flex; gap: 10px; align-items: flex-start;
  color: var(--muted); font-size: .9rem; max-width: 100%;
}
.step-icon {
  flex: none; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center;
  background: var(--surface-2); border: 1px solid var(--border); font-size: .72rem; margin-top: 1px;
}
.step.busy .step-icon { border-color: var(--accent); border-right-color: transparent; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.step-body { min-width: 0; }
.step-title { color: var(--text); font-weight: 600; }
.step ol { margin: 6px 0 0; padding-left: 0; list-style: none; display: grid; gap: 2px; }
.step li { display: flex; gap: 8px; }
.pid { font-weight: 700; color: var(--accent); flex: none; min-width: 1.6em; }

.badge {
  display: inline-block; padding: 1px 9px; border-radius: 999px; font-size: .75rem; font-weight: 700;
  border: 1px solid currentColor; vertical-align: 1px;
}
.badge.hit { color: var(--ok); }
.badge.miss { color: var(--problem); }
.badge.none { color: var(--muted); }

/* ---------- The answer card ---------- */

.answer {
  align-self: stretch; max-width: 900px;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg);
  overflow: hidden;
}
.answer-head {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
  padding: 12px 16px; background: var(--surface-2); border-bottom: 1px solid var(--border);
}
.answer-head .who { font-weight: 700; }
.answer-head .meta { color: var(--muted); font-size: .82rem; margin-left: auto; }
.status {
  padding: 2px 10px; border-radius: 999px; font-size: .78rem; font-weight: 700;
  background: var(--accent-soft); color: var(--accent); border: 1px solid var(--accent);
}
.status-needs_hypothesis, .status-not_enough_evidence, .status-none {
  background: var(--info-bg); color: var(--info-text); border-color: var(--info-border);
}
.answer-body { padding: 4px 16px 16px; }
.legend {
  margin: 12px 0 0; font-size: .8rem; color: var(--translation);
  border: 1px dashed var(--border); border-radius: 8px; padding: 6px 10px;
}

.section { margin-top: 16px; }
.section h3 {
  margin: 0 0 6px; font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
}
.section p { margin: 0; }
.label { font-size: .8rem; color: var(--muted); margin: 8px 0 2px; }

/* Red flags: first and impossible to miss */
.flags {
  margin-top: 14px; border: 2px solid var(--flag-border); background: var(--flag-bg); color: var(--flag-text);
  border-radius: 12px; padding: 12px 14px;
}
.flags h3 { color: var(--flag-text); margin: 0 0 8px; font-size: .9rem; display: flex; gap: 8px; align-items: center; }
.flag + .flag { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--flag-border); }
.flag-name { font-weight: 700; color: var(--flag-text); }
.flags .quote { border-left-color: var(--flag-border); background: transparent; color: var(--text); }
.flags .label, .flags .translation { color: var(--flag-text); }
.fixed-warning { font-size: .78rem; font-weight: 600; }

/* Text copied word for word from a guideline passage */
.quote {
  margin: 4px 0 0; padding: 8px 12px;
  border-left: 4px solid var(--quote-bar); background: var(--surface);
  border-radius: 0 8px 8px 0; font-style: normal;
}
.quote-source { font-size: .78rem; color: var(--muted); margin-top: 4px; }
.own-words { font-size: .75rem; color: var(--muted); }

.alt {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 10px 12px;
}
.alt + .alt { margin-top: 8px; }
.alt-name { font-weight: 700; }

.message-box {
  background: var(--accent-soft); border: 1px solid var(--accent); border-radius: 10px; padding: 10px 12px;
}

/* English translation under a Dutch text */
.translation { margin-top: 4px; color: var(--translation); font-size: .92rem; font-style: italic; }
.translation .en-pill {
  font-style: normal; font-size: .66rem; font-weight: 700; padding: 0 5px; border-radius: 4px;
  border: 1px solid currentColor; margin-right: 6px; vertical-align: 1px;
}
.translation.missing { font-style: normal; font-size: .8rem; }

/* Inline citation like [P2] */
.cite {
  display: inline-block; font-size: .72rem; font-weight: 700; color: var(--accent);
  background: var(--accent-soft); border-radius: 4px; padding: 0 4px; margin: 0 1px;
  text-decoration: none; font-style: normal;
}

/* Source list */
.sources { display: grid; gap: 6px; margin: 0; padding: 0; list-style: none; }
.source { border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.source.cited { border-color: var(--accent); }
.source summary { padding: 6px 10px; cursor: pointer; display: flex; gap: 8px; align-items: baseline; }
.source-title { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.source-kind { font-size: .72rem; color: var(--muted); flex: none; }
.cited-mark { font-size: .72rem; font-weight: 700; color: var(--accent); flex: none; }
.source-text {
  margin: 0; padding: 8px 12px 12px; font-size: .85rem; white-space: pre-wrap; color: var(--muted);
  border-top: 1px solid var(--border); max-height: 320px; overflow-y: auto;
}
.source:target, .source.flash { box-shadow: 0 0 0 3px var(--accent-soft); }
/* Each source row: the fold-out passage, and a small link button next to it */
.sources > li { display: flex; gap: 6px; align-items: flex-start; }
.sources > li > details { flex: 1; min-width: 0; }
.source-quicklink {
  flex: none; display: grid; place-items: center; width: 34px; height: 34px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  color: var(--accent); text-decoration: none; font-weight: 700;
}
.source-quicklink:hover { border-color: var(--accent); }
.source-link { margin: 0; padding: 6px 12px 0; font-size: .82rem; border-top: 1px solid var(--border); }
.source-link a { color: var(--accent); font-weight: 600; }
.source-url { color: var(--muted); overflow-wrap: anywhere; }
.source-link + .source-text { border-top: 0; }

/* Automatic checks */
.checks { margin: 0; padding: 0; list-style: none; display: grid; gap: 3px; font-size: .85rem; }
.checks li { display: flex; gap: 8px; }
.ok { color: var(--ok); font-weight: 700; }
.problem { color: var(--problem); font-weight: 700; }
.not-checked { color: var(--muted); font-weight: 700; }

/* Replies from the demo itself (not from the model) */
.demo-reply {
  align-self: flex-start; max-width: min(90%, 720px);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 16px 16px 16px 4px;
  padding: 12px 16px;
}
.demo-reply p { margin: 0 0 8px; }
.demo-reply p:last-child { margin-bottom: 0; }
.demo-reply.warning { background: var(--info-bg); color: var(--info-text); border-color: var(--info-border); }
.demo-reply .case-list { gap: 6px; }

/* ---------- Composer ---------- */

.composer {
  position: sticky; bottom: 0; z-index: 2;
  border-top: 1px solid var(--border); padding: 12px; background: var(--surface);
  border-radius: 0 0 var(--radius) var(--radius);
  box-shadow: 0 -6px 16px -10px rgba(0, 0, 0, .25);
}
.composer textarea, .code-dialog input {
  width: 100%; resize: vertical; max-height: 40vh;
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; background: var(--bg);
  line-height: 1.45;
}
#message-input { min-height: 48px; }
.field-label { display: block; font-size: .8rem; font-weight: 700; color: var(--muted); margin: 6px 0 3px; }
.privacy-note { margin: 0 0 4px; font-size: .78rem; color: var(--info-text); }

/* The access code screen */
.code-dialog {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 20px;
  background: var(--surface); color: var(--text); width: min(420px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
.code-dialog::backdrop { background: rgba(10, 20, 24, .55); }
.code-dialog h2 { margin: 0 0 6px; font-size: 1.1rem; }
.code-dialog p { margin: 0 0 8px; }
.code-dialog .composer-buttons { justify-content: flex-end; margin-top: 10px; }
.code-error { color: var(--problem); font-weight: 600; min-height: 1.4em; font-size: .85rem; }
.composer-row { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.composer-hint { color: var(--muted); font-size: .78rem; flex: 1; }
.composer-buttons { display: flex; gap: 8px; }
.send-button {
  background: var(--accent); color: var(--accent-text); border: 0; border-radius: 10px;
  padding: 9px 22px; font-weight: 700; min-height: 42px;
}
.send-button:disabled { opacity: .5; cursor: not-allowed; }
.quiet-button {
  background: transparent; border: 1px solid var(--border); border-radius: 10px; padding: 9px 14px; min-height: 42px;
}


/* ---------- Phone and small screens ---------- */

@media (max-width: 860px) {
  html { font-size: 16px; }
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  .brand-sub { display: none; }
  .replay-banner { padding: 8px 16px; font-size: .82rem; }
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; gap: 12px; }
  .cases details { position: static; max-height: none; overflow: visible; padding-right: 0; }
  .cases summary {
    cursor: pointer; background: var(--surface); border: 1px solid var(--border);
    border-radius: 10px; padding: 10px 12px; display: flex; justify-content: space-between;
  }
  .cases summary::after { content: "+"; font-weight: 700; color: var(--muted); }
  .cases details[open] summary::after { content: "\2212"; }
  .cases-help { margin-top: 8px; }
  .chat { min-height: 60vh; }
  .messages { padding: 14px 12px; }
  .messages > * { scroll-margin-bottom: 16px; }
  .msg-user { max-width: 92%; }
  .answer-body { padding: 2px 12px 14px; }
  .answer-head { padding: 10px 12px; }
  .answer-head .meta { margin-left: 0; width: 100%; }
  /* On a phone the fields sit at the end of the chat instead of sticking:
     two fields would cover too much of a small screen. */
  .composer { position: static; box-shadow: none; }
  .status-line { padding: 8px 16px; }
  .controls { gap: 6px; }
  .switch button { padding: 6px 10px; }
  .source summary { flex-wrap: wrap; }
  .source-kind { order: 3; width: 100%; padding-left: 2.1em; }
  .composer-hint { display: none; }
  .composer-buttons { margin-left: auto; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

/* ---------- Start screen of a new chat (fields on the whole space) ---------- */

/* Before the first chat there is no side panel: the start screen gets the full width. */
.layout.no-side { grid-template-columns: minmax(0, 1fr); max-width: 1040px; }

.start-screen { padding: 28px 32px 32px; display: grid; gap: 22px; }
.start-form { display: grid; gap: 6px; }
.start-title { margin: 0; font-size: 1.6rem; line-height: 1.2; }
.start-intro { margin: 0 0 10px; color: var(--muted); max-width: 70ch; }
.start-label { font-weight: 700; font-size: .95rem; margin-top: 8px; }
.start-form textarea {
  width: 100%; resize: vertical; max-height: 60vh;
  border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; background: var(--bg);
  line-height: 1.5; font-size: 1rem;
}
.start-form textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
#start-case { min-height: 170px; }
#start-hyp { min-height: 56px; }
.start-error {
  margin: 4px 0 0; padding: 8px 12px; border-radius: 10px; font-size: .9rem;
  background: var(--info-bg); color: var(--info-text); border: 1px solid var(--info-border);
}
.start-row { display: flex; align-items: center; gap: 12px; margin-top: 8px; }
.start-row .send-button { padding: 11px 30px; font-size: 1rem; }
.start-cases .side-heading { margin-top: 0; }
.start-case-grid { grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.start-try { margin: 0; color: var(--muted); font-size: .88rem; font-style: italic; }

/* The case bubble: the case, then what the therapist thinks it is. */
.bubble-label-gap { margin-top: 10px; }

/* The start screen turns into the chat: a short fade and rise. */
.chat.entering .messages, .chat.entering .composer { animation: enter-chat .35s ease-out; }
@keyframes enter-chat { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  .chat.entering .messages, .chat.entering .composer { animation: none; }
}

@media (max-width: 860px) {
  .start-screen { padding: 18px 14px 20px; gap: 18px; }
  .start-title { font-size: 1.3rem; }
  #start-case { min-height: 150px; }
  .start-row { flex-wrap: wrap; }
  .start-row .send-button { width: 100%; }
  .start-case-grid { grid-template-columns: minmax(0, 1fr); }
}

/* The hidden attribute always wins over display rules above. */
[hidden] { display: none !important; }
.start-form .field-help { margin: -4px 0 2px; }
