:root {
  --bg: #0f1117;
  --surface: #191d27;
  --surface-2: #222736;
  --border: #2b3142;
  --text: #e8eaf0;
  --muted: #8f97ad;
  --accent: #6c8cff;
  --accent-ink: #0b0e18;
  --danger: #ff7b7b;

  --radius: 12px;
  --radius-sm: 8px;

  /* One spacing scale, used everywhere. Ad-hoc margins are what made the
     previous layout drift. */
  --s1: 0.25rem;
  --s2: 0.5rem;
  --s3: 0.75rem;
  --s4: 1rem;
  --s5: 1.5rem;
  --s6: 2rem;

  --content: 40rem;
  --topbar-h: 3.5rem;
}

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

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

body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.55 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

/* The `hidden` attribute must win over any display rule. Without this, an
   element toggled with `hidden` but carrying a class that sets `display`
   stays stubbornly visible. */
[hidden] { display: none !important; }

/* ---------- Topbar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar__inner {
  /* Same column as the content, so the brand and the sign-out button line up
     with everything below them instead of hugging the window edges. */
  max-width: var(--content);
  min-height: var(--topbar-h);
  margin: 0 auto;
  padding: 0 var(--s4);
  display: flex;
  align-items: center;
  gap: var(--s3);
}

.brand {
  color: var(--accent);
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: .01em;
  text-decoration: none;
}

.topbar__user {
  margin-left: auto;
  color: var(--muted);
  font-size: .9rem;
  max-width: 45%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- Layout ---------- */

main {
  flex: 1;
  width: 100%;
  max-width: var(--content);
  margin: 0 auto;
  padding: var(--s5) var(--s4);
  display: flex;
  flex-direction: column;
  gap: var(--s5);
  min-height: 0;
}

h1 { font-size: 1.35rem; line-height: 1.25; margin: 0; }
h2 { font-size: 1rem; margin: 0; }

.muted { color: var(--muted); }
.small { font-size: .875rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--s4);
  display: flex;
  flex-direction: column;
  gap: var(--s3);
}

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

/* ---------- Controls ---------- */

input[type=text],
input[type=search] {
  width: 100%;
  min-width: 0;
  padding: var(--s3);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  /* 16px keeps iOS from zooming the viewport when the field takes focus. */
  font-size: 1rem;
  font-family: inherit;
}

input::placeholder { color: var(--muted); }

button,
input[type=submit],
.button {
  /* input[type=submit] has to be here explicitly: form.submit renders one, and
     leaving it out is what left the primary actions as unstyled white bars.
     .button is for link_to styled as a call to action on the tenant landing. */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.75rem;
  padding: var(--s3) var(--s4);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--accent-ink);
  font: 600 1rem/1.2 inherit;
  text-decoration: none;
  cursor: pointer;
  appearance: none;
}

button:disabled,
input[type=submit]:disabled { opacity: .45; cursor: not-allowed; }

:where(a, button, input, [tabindex]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.button--ghost,
.button--ghost input[type=submit] {
  background: none;
  border-color: var(--border);
  color: var(--muted);
  font-weight: 500;
}

/* button_to wraps its button in a form; collapse the form only, never the
   button, or the button loses its box and the label spills out. */
form.button-to { display: contents; }

.button--compact {
  min-height: 2.25rem;
  padding: var(--s2) var(--s3);
  font-size: .875rem;
  flex: none;
}

/* ---------- Rooms ---------- */

.rooms { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--s2); }

.rooms a,
.place-result {
  width: 100%;
  min-height: 3rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s3);
  padding: var(--s3) var(--s4);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-weight: 400;
  text-align: left;
  text-decoration: none;
}

.rooms a:hover,
.place-result:hover { border-color: var(--accent); }

.rooms .count { color: var(--muted); font-size: .85rem; flex: none; }

.results { display: block; }
.results:empty { display: none; }

/* ---------- Chat ---------- */

/* The composer is sticky at the bottom, so main's bottom padding would leave
   it floating a notch above the viewport edge. The composer supplies its own
   breathing room (and the safe-area inset) instead. Browsers without :has()
   simply keep the padding, which is a smaller gap, not a broken layout. */
main:has(.chat) { padding-bottom: 0; }

.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: var(--s4);
  min-height: 0;
}

.messages {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  /* Conversations grow upward from the composer, the way every chat does.
     Previously a short thread sat at the top with a void beneath it. */
  justify-content: flex-end;
  gap: var(--s2);
}

.message {
  padding: var(--s2) var(--s3);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  /* Long unbroken strings must not widen the page. */
  overflow-wrap: anywhere;
}

.message--mine { background: var(--surface-2); border-left: 3px solid var(--accent); }

/* A reply from the business, relayed in through the bridge. Distinct from
   --mine (a left accent bar) so a visitor can tell staff apart at a glance. */
.message--staff { background: var(--surface-2); border-color: var(--accent); }
.message--staff .author { color: var(--accent); }

.message--system {
  align-self: center;
  background: none;
  border: 0;
  color: var(--muted);
  font-size: .85rem;
  text-align: center;
  padding: var(--s1) 0;
}

.message .author { display: block; font-size: .78rem; color: var(--muted); }

.composer {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: var(--s2);
  padding: var(--s3) 0;
  background: var(--bg);
  /* Clear the iOS home indicator. */
  padding-bottom: max(var(--s3), env(safe-area-inset-bottom));
}

.composer { flex-wrap: wrap; }
.composer input[type=text] { flex: 1; }
.composer input[type=submit] { flex: none; }
/* The name field only appears for a visitor who has not claimed a name yet;
   it sits on its own row above the message input. */
.composer__name { flex: 1 1 100%; }

/* ---------- Flash ---------- */

.flash {
  padding: var(--s3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}

.flash--alert { color: var(--danger); border-color: var(--danger); }

/* ---------- Tenant landing ---------- */

/* The tenant layout redefines the design tokens two ways: the light scheme
   sets a whole palette here, and the organization's own colours arrive as an
   inline style attribute on <body> that overrides individual tokens on top. */
.tenant[data-scheme="light"] {
  --bg: #f7f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --border: #d9dce4;
  --text: #1a1d26;
  --muted: #5b6172;
  --accent: #3a5bd9;
  --accent-ink: #ffffff;
  --danger: #c0392b;
}

.hero {
  display: flex;
  flex-direction: column;
  gap: var(--s4);
}

.hero__image {
  width: 100%;
  height: clamp(9rem, 32vw, 15rem);
  object-fit: cover;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.hero__body { display: flex; flex-direction: column; gap: var(--s2); }
.hero__tagline { margin: 0; font-size: 1.05rem; }
.hero__actions { display: flex; flex-wrap: wrap; gap: var(--s2); margin-top: var(--s2); }

/* On the landing the chat is one section among others, not the whole screen,
   so its composer scrolls with the page instead of sticking to the viewport. */
.tenant .card .composer { position: static; padding-bottom: var(--s3); }

.tenant-footer {
  width: 100%;
  max-width: var(--content);
  margin: 0 auto;
  padding: var(--s4);
  text-align: center;
}

.tenant-footer a { color: var(--muted); font-size: .8rem; text-decoration: none; }

/* ---------- Narrow screens ---------- */

@media (max-width: 26rem) {
  main { padding: var(--s4) var(--s3); gap: var(--s4); }
  .topbar__inner { padding: 0 var(--s3); }
  h1 { font-size: 1.2rem; }
  /* Stop the send button from squeezing the input down to a stub. */
  .composer input[type=submit] { padding: var(--s3); }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
