:root {
  --bar: #075e54;
  --bar-text: #ffffff;
  --wall: #efeae2;
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --text: #111b21;
  --muted: #667781;
  --danger: #b3261e;
  --accent: #00a884;
  --composer: #f0f2f5;
  --field: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bar: #202c33;
    --wall: #0b141a;
    --bubble-in: #202c33;
    --bubble-out: #005c4b;
    --text: #e9edef;
    --muted: #8696a0;
    --danger: #f28b82;
    --composer: #202c33;
    --field: #2a3942;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--wall);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.app {
  display: flex;
  flex-direction: column;
  /* 100vh for browsers without dynamic viewport units (iOS < 15.4). */
  height: 100vh;
  height: 100dvh;
  max-width: 820px;
  margin: 0 auto;
  background: var(--wall);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    10px max(16px, env(safe-area-inset-left));
  background: var(--bar);
  color: var(--bar-text);
}

.avatar {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  font-weight: 700;
}

.identity { flex: 1; min-width: 0; }
.identity h1 { margin: 0; font-size: 16px; }
.identity p { margin: 0; font-size: 13px; opacity: .85; }

.new-conversation {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 6px 12px;
  border: 1px solid rgba(255, 255, 255, .6);
  border-radius: 18px;
  background: transparent;
  color: var(--bar-text);
  font: inherit;
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
}

.new-conversation:disabled { opacity: .5; cursor: default; }

/* Small phones: icon only, so the name in the bar does not wrap. */
@media (max-width: 359px) {
  .new-conversation { padding: 8px; }
  .new-conversation span { display: none; }
}

.thread {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Privacy notice accepted before the first message (T187.5): it takes the thread's place. */
.privacy {
  flex: 0 1 auto;
  min-height: 0;
  /* Sized by the text; the composer stays at the bottom (margin below). */
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 16px 16px auto;
  padding: 14px 16px;
  border-radius: 8px;
  background: var(--bubble-in);
  box-shadow: 0 1px .5px rgba(11, 20, 26, .13);
}
.privacy h2 { margin: 0; font-size: 17px; }
.privacy-version { margin: 0; font-size: 13px; color: var(--muted); }
.privacy-text {
  flex: 0 1 auto;
  min-height: 4em;
  overflow-y: auto;
  font-size: 14px;
}
/* Pre-wrapped children only: the template indentation is not shown. */
.privacy-text > div { white-space: pre-wrap; }
.privacy-text:focus { outline: 2px solid var(--accent); }
.privacy-details { margin-top: 10px; }
.privacy-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.privacy-more, .notice-more {
  padding: 10px 18px;
  border: 1px solid var(--accent);
  border-radius: 20px;
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.notice-more { margin-top: 6px; padding: 4px 12px; font-size: 13px; }
.contact-form .notice-details { margin-top: 8px; }
.privacy-details[hidden], .notice-details[hidden], .privacy-more[hidden] { display: none; }
.privacy-accept {
  padding: 10px 18px;
  border: 0;
  border-radius: 20px;
  background: var(--accent);
  color: #ffffff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.thread[hidden], .privacy[hidden] { display: none; }

.row { display: flex; flex-direction: column; }
.row.user { align-items: flex-end; }
.row.assistant { align-items: flex-start; }

.bubble {
  max-width: min(78%, 560px);
  padding: 6px 9px 4px;
  border-radius: 8px;
  background: var(--bubble-in);
  box-shadow: 0 1px .5px rgba(11, 20, 26, .13);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.row.user .bubble { background: var(--bubble-out); }

.meta {
  display: block;
  margin-top: 2px;
  text-align: right;
  font-size: 11px;
  color: var(--muted);
}

.failure {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  font-size: 13px;
  color: var(--danger);
}

.failure button {
  border: 1px solid currentColor;
  border-radius: 14px;
  padding: 2px 10px;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

.typing .bubble { color: var(--muted); font-style: italic; }

.notice {
  margin: 0 16px 8px;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--bubble-in);
  color: var(--danger);
  font-size: 13px;
  text-align: center;
}

/* Chat locked by the panic button (T187.16, D143). */
.locked {
  margin: 24px 16px;
  padding: 16px;
  border-radius: 8px;
  background: var(--bubble-in);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  text-align: center;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  /* Keeps the composer above the iPhone home indicator. */
  padding: 8px max(12px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  background: var(--composer);
}

.composer textarea {
  flex: 1;
  max-height: 140px;
  padding: 10px 12px;
  border: 0;
  border-radius: 8px;
  background: var(--field);
  color: var(--text);
  font: inherit;
  /* Below 16px, iOS Safari zooms the page in when the field is focused. */
  font-size: 16px;
  resize: none;
}

.composer textarea:focus { outline: 2px solid var(--accent); }
.composer textarea:disabled { opacity: .6; cursor: not-allowed; }

.composer button {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #ffffff;
  cursor: pointer;
}

.composer button:disabled { opacity: .5; cursor: default; }

/* Agent quick replies and forms (tasks 154 and 155) */

.quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-width: min(78%, 560px);
  margin-top: 6px;
}

.quick-reply {
  padding: 6px 12px;
  border: 1px solid var(--accent);
  border-radius: 16px;
  background: var(--bubble-in);
  color: var(--accent);
  font: inherit;
  cursor: pointer;
}

.quick-reply:disabled { opacity: .5; cursor: default; }

/* Grey hint after the agent options (T194, D149): free text is accepted too. */
.quick-reply-write {
  padding: 6px 12px;
  border: 1px solid var(--muted);
  border-radius: 16px;
  background: var(--composer);
  color: var(--muted);
  font: inherit;
  cursor: text;
}

.quick-reply-write:disabled { opacity: .5; cursor: default; }

.contact-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(100%, 560px);
  margin-top: 6px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--bubble-in);
  box-shadow: 0 1px .5px rgba(11, 20, 26, .13);
}

.contact-form .form-title { margin: 0; font-weight: 600; }

.contact-form .notice-text {
  max-height: 9em;
  overflow-y: auto;
  padding: 6px 8px;
  border: 1px solid var(--muted);
  border-radius: 6px;
  font-size: 13px;
  white-space: pre-wrap;
}

.contact-form .field { display: flex; flex-direction: column; gap: 2px; font-size: 13px; color: var(--muted); }

.contact-form input[type="text"],
.contact-form input[type="tel"],
.contact-form input[type="email"],
.contact-form textarea {
  padding: 7px 9px;
  border: 1px solid var(--muted);
  border-radius: 6px;
  background: var(--field);
  color: var(--text);
  font: inherit;
  font-size: 16px;
}

.contact-form .consent { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; }
.contact-form .consent input { margin-top: 2px; }

.contact-form .form-error { margin: 0; font-size: 13px; color: var(--danger); }

.contact-form .form-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.contact-form .form-actions button {
  padding: 7px 14px;
  border-radius: 16px;
  font: inherit;
  cursor: pointer;
}

.contact-form .primary { border: 0; background: var(--accent); color: #ffffff; }
.contact-form .secondary { border: 1px solid var(--muted); background: transparent; color: var(--text); }
.contact-form button:disabled { opacity: .5; cursor: default; }

/* Agent forms (task 155): help and error per field, collapsible notice, closed state. */
.contact-form textarea { min-height: 4.5em; resize: vertical; }
.contact-form .field-help {
  display: block;
  opacity: 0.75;
}
.contact-form .field-error {
  display: block;
  color: var(--danger);
}
.contact-form [aria-invalid='true'] {
  border-color: var(--danger);
}
.contact-form .notice-box summary {
  cursor: pointer;
  font-weight: 600;
}
.form-state {
  margin: 4px 0 0;
  font-size: 0.85em;
  opacity: 0.75;
}
