:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #e3e6eb;
  --text: #1c1f24;
  --muted: #6b7280;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --bot-bg: #eef2ff;
  --error: #c0262d;
  --ok: #15803d;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --panel: #1a1d24;
    --border: #2a2e37;
    --text: #e6e8ec;
    --muted: #9097a3;
    --accent: #818cf8;
    --accent-text: #0b0d12;
    --bot-bg: #1f2340;
    --error: #f87171;
    --ok: #4ade80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
.muted { color: var(--muted); font-weight: normal; }
.small { font-size: 12px; line-height: 1.4; }
dialog #privacy-note { margin-top: 16px; }
.error { color: var(--error); margin: 0; }

input, button { font: inherit; color: inherit; }
input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
button {
  padding: 8px 16px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
}

/* Join dialog */
dialog {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  padding: 24px;
  width: min(360px, calc(100vw - 32px));
}
/* Solid, so nothing of the chat shows behind the login screen. Literal
   colours (matching --bg): not every browser lets ::backdrop see :root
   variables. */
dialog::backdrop { background: #f6f7f9; }
@media (prefers-color-scheme: dark) {
  dialog::backdrop { background: #111318; }
}
dialog h1 { margin: 0 0 16px; font-size: 22px; }
dialog form, dialog section { display: grid; gap: 14px; }
dialog p { margin: 0; }
dialog label { display: grid; gap: 4px; font-size: 13px; font-weight: 600; }
dialog .error { margin-top: 14px; }
.notice {
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--bot-bg);
  font-size: 13px;
  line-height: 1.45;
}
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }
.row > input { flex: 2; }
button.secondary {
  background: transparent;
  color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}
button.link {
  background: none;
  color: var(--accent);
  padding: 4px 6px;
  font-weight: 500;
  font-size: 13px;
}
.divider {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  margin: 18px 0 14px !important;
}
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }
#messages .guest-tag, #users .guest-tag {
  color: var(--muted);
  font-size: 11px;
  font-weight: normal;
  margin-left: -2px;
  margin-right: 6px;
}

/* Layout */
.app {
  height: 100dvh;
  display: grid;
  grid-template: "head head" auto "side main" 1fr / 220px 1fr;
}
header {
  grid-area: head;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.logo { font-weight: 700; }
.room-title { color: var(--muted); }
.status {
  margin-left: auto;
  font-size: 13px;
  color: var(--muted);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.room-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  display: none;
  align-items: center;
  justify-content: center;
  padding: 6px;
  margin-left: -6px;
  background: none;
  color: var(--text);
}
#drawer-backdrop { display: none; }
.status.ok::before { content: "● "; color: var(--ok); }
.status.bad::before { content: "● "; color: var(--error); }

aside {
  grid-area: side;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 12px;
  overflow-y: auto;
}
aside h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 8px 0;
}
aside ul { list-style: none; margin: 0 0 12px; padding: 0; }
aside li { padding: 4px 8px; border-radius: 6px; display: flex; justify-content: space-between; }
#rooms li { cursor: pointer; }
#rooms li:hover { background: var(--bg); }
#rooms li.active { background: var(--accent); color: var(--accent-text); }
#rooms li .count { opacity: 0.7; font-size: 13px; }

main {
  grid-area: main;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}
#messages {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: 12px 16px;
}
#messages li { padding: 3px 8px; border-radius: 6px; overflow-wrap: anywhere; }
#messages .time { color: var(--muted); font-size: 12px; margin-right: 8px; font-variant-numeric: tabular-nums; }
#messages .from { font-weight: 600; margin-right: 6px; }
#messages .system { color: var(--muted); font-style: italic; white-space: pre-wrap; }
#messages .bot { background: var(--bot-bg); }
#messages .badge,
#users .badge {
  font-size: 10px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--accent);
  color: var(--accent-text);
  margin-right: 6px;
  vertical-align: 1px;
}

#composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}

@media (max-width: 640px) {
  .app { grid-template: "head" auto "main" 1fr / 1fr; }
  header { gap: 8px; }
  .icon-btn { display: inline-flex; }
  /* The sidebar becomes a slide-over panel opened from the header. */
  aside {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(280px, 85vw);
    z-index: 20;
    padding-top: 16px;
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 0.2s ease, visibility 0.2s;
    box-shadow: 4px 0 24px rgb(0 0 0 / 0.25);
  }
  .app.drawer-open aside { transform: none; visibility: visible; }
  #drawer-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 15;
    background: rgb(0 0 0 / 0.4);
  }
  #rooms li { padding: 10px 8px; } /* comfortable tap targets */
  #composer, #messages { padding-left: 16px; padding-right: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  aside { transition: none; }
}
