/* ============================================================
   Chat Room - design tokens
   Both themes are checked for WCAG AA contrast on body text.
   ============================================================ */

:root {
  --bg: #0b1020;
  --bg-glow: #16204a;
  --surface: #151b30;
  --surface-2: #1d2540;
  --surface-3: #263056;
  --text: #eaeefb;
  --muted: #a8b2ce;
  --border: #2a3355;
  --accent: #77a9ff;
  --accent-ink: #071227;
  --accent-solid: #3b82f6;
  --own: #24336b;
  --own-border: #3a4d97;
  --danger: #ff8b8b;
  --danger-bg: #3a1620;
  --warn: #ffd28a;
  --warn-bg: #3a2c12;
  --ok: #6ee7a8;
  --focus: #ffd166;
  --radius: 14px;
  --radius-lg: 20px;
  --shadow: 0 10px 30px rgb(0 0 0 / 35%);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

[data-theme="light"] {
  --bg: #eef1f8;
  --bg-glow: #dbe4fb;
  --surface: #ffffff;
  --surface-2: #f2f5fc;
  --surface-3: #e4eaf7;
  --text: #101526;
  --muted: #4d5673;
  --border: #ccd5ea;
  --accent: #1b4fd8;
  --accent-ink: #ffffff;
  --accent-solid: #1b4fd8;
  --own: #d9e5ff;
  --own-border: #9dbaf5;
  --danger: #a3121f;
  --danger-bg: #fde8ea;
  --warn: #6b4a05;
  --warn-bg: #fdf3d8;
  --ok: #08694a;
  --focus: #7a3b00;
  --shadow: 0 8px 24px rgb(16 21 38 / 12%);
}

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

/* The browser default `[hidden] { display: none }` is beaten by any author
   `display` declaration, and several elements here set one (.app is a grid,
   .recording-bar is a flex row). Restate it so `hidden` actually hides. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  color: var(--text);
  background:
    radial-gradient(1100px 600px at 12% -10%, var(--bg-glow), transparent 60%),
    var(--bg);
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { line-height: 1.25; margin: 0; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 6px;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: 12px; top: -60px;
  z-index: 50;
  padding: 10px 16px;
  background: var(--accent-solid);
  color: #fff;
  font-weight: 600;
  border-radius: 0 0 var(--radius) var(--radius);
  transition: top .15s ease-in-out;
}
.skip-link:focus { top: 0; }

/* ============================ Buttons ============================ */

.btn {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 10px 18px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: background .15s, border-color .15s, transform .1s;
}
.btn:hover { background: var(--surface-3); }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: .55; cursor: not-allowed; transform: none; }

.btn-primary {
  background: var(--accent-solid);
  border-color: var(--accent-solid);
  color: #fff;
}
.btn-primary:hover { filter: brightness(1.1); background: var(--accent-solid); }

.btn-ghost { background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-ghost[aria-pressed="true"] {
  background: var(--surface-3);
  border-color: var(--accent);
  color: var(--accent);
}

.btn-block { width: 100%; justify-content: center; }
.btn-small { padding: 5px 12px; font-size: .875rem; }

/* Icon-only buttons keep a 44px target and carry their name in aria-label. */
.btn-icon {
  width: 48px;
  height: 48px;
  padding: 0;
  justify-content: center;
  flex: none;
}
.icon { width: 22px; height: 22px; fill: currentColor; display: block; }

.btn-record .icon-stop { display: none; }
.btn-record[aria-pressed="true"] .icon-mic { display: none; }
.btn-record[aria-pressed="true"] .icon-stop { display: block; }

.btn-record { position: relative; }
.btn-record .rec-dot {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
  display: none;
}
.btn-record[aria-pressed="true"] {
  border-color: var(--danger);
  color: var(--danger);
  background: var(--danger-bg);
}
.btn-record[aria-pressed="true"] .rec-dot {
  display: block;
  animation: pulse 1.2s ease-in-out infinite;
}
.rec-dot-live {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--danger);
  flex: none;
  animation: pulse 1.2s ease-in-out infinite;
}

@keyframes pulse { 50% { opacity: .25; } }

/* ============================ Sign-in ============================ */

.join {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px 18px 48px;
}

.join-card {
  width: min(560px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: clamp(22px, 4vw, 38px);
}

.join-badge { font-size: 2.6rem; margin: 0 0 6px; line-height: 1; }
.join-card h1 { font-size: clamp(1.6rem, 4vw, 2.1rem); }
.join-lead { color: var(--muted); margin: 10px 0 26px; }

.field { margin-bottom: 22px; }
.field > label {
  display: block;
  font-weight: 650;
  margin-bottom: 8px;
}

input[type="text"], textarea {
  font: inherit;
  width: 100%;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 15px;
}
input[type="text"]:focus, textarea:focus { border-color: var(--accent); }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 1; }

.hint { color: var(--muted); font-size: .875rem; margin: 10px 0 0; }
.hint strong { color: var(--text); }

.error {
  margin: 12px 0 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--danger-bg);
  border: 1px solid var(--danger);
  color: var(--danger);
  font-weight: 600;
}

/* ============================ App shell ============================ */

.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 330px;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  grid-template-areas:
    "top     top"
    "warn    warn"
    "conn    conn"
    "room    members";
}
.app:has(#members-panel[hidden]) { grid-template-columns: minmax(0, 1fr); }

.topbar {
  grid-area: top;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 16px;
  padding: 12px clamp(12px, 2vw, 22px);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-title { font-size: 1.15rem; margin-right: 4px; }
.topbar-me { margin: 0; color: var(--muted); font-size: .9375rem; }
.topbar-me strong { color: var(--text); }
.topbar-actions {
  margin-left: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.topbar-actions .btn { padding: 7px 14px; font-size: .9375rem; }

.banner {
  margin: 0;
  padding: 11px clamp(12px, 2vw, 22px);
  font-size: .9375rem;
  border-bottom: 1px solid var(--border);
}
.banner-warn { grid-area: warn; background: var(--warn-bg); color: var(--warn); }
.banner-error { grid-area: conn; background: var(--danger-bg); color: var(--danger); }
.banner strong { font-weight: 700; }

/* ============================ Messages ============================ */

.room {
  grid-area: room;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: 0;
  padding: 20px clamp(12px, 2vw, 22px);
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Sit the conversation on the bottom edge like a chat client. An auto margin
   on the first item absorbs the spare room and collapses to nothing once the
   list overflows, which `justify-content: flex-end` cannot do without
   breaking scrollback. */
.messages > :first-child { margin-top: auto; }

.empty { color: var(--muted); text-align: center; padding: 0 20px 20px; margin: 0; }

.msg { display: flex; gap: 11px; max-width: min(720px, 92%); }
.msg-own { margin-left: auto; flex-direction: row-reverse; }

.avatar {
  flex: none;
  width: 38px; height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: .95rem;
  color: #0a0f1e;
  background: hsl(var(--hue, 220) 70% 72%);
}

.bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 9px 15px 11px;
  min-width: 0;
}
.msg-own .bubble { background: var(--own); border-color: var(--own-border); }

.msg-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 10px;
  font-size: .8125rem;
  color: var(--muted);
  margin-bottom: 2px;
}
.msg-author { font-weight: 700; color: var(--text); font-size: .9375rem; }

.msg-text {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg-text a { color: var(--accent); }

.msg-system {
  max-width: 100%;
  justify-content: center;
}
.msg-system .bubble {
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: .875rem;
  text-align: center;
  padding: 2px 0;
}

.voice { margin-top: 6px; }
.voice audio { display: block; width: min(320px, 100%); height: 40px; }

/* ============================ Composer ============================ */

.composer {
  flex: none;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px 12px;
  padding: 14px clamp(12px, 2vw, 22px) 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.composer textarea {
  grid-column: 1;
  resize: none;
  max-height: 180px;
  min-height: 48px;
}
.composer-actions {
  grid-column: 2;
  display: flex;
  align-items: flex-end;
  gap: 10px;
}
.recording-bar {
  grid-column: 1 / -1;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  color: var(--danger);
}
.counter {
  grid-column: 1 / -1;
  margin: 0;
  font-size: .75rem;
  color: var(--muted);
  text-align: right;
}

/* ============================ Members ============================ */

.members {
  grid-area: members;
  overflow-y: auto;
  padding: 18px;
  background: var(--surface);
  border-left: 1px solid var(--border);
}
.members h2 {
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
}
.members-hint { color: var(--muted); font-size: .8125rem; margin: 0 0 14px; }

.member-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }

.member {
  width: 100%;
  font: inherit;
  text-align: left;
  cursor: pointer;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 2px 10px;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
}
.member:hover { background: var(--surface-3); }
.member-name { font-weight: 650; }
.member-seen { grid-column: 2; font-size: .8125rem; color: var(--muted); }

.dot {
  grid-row: 1 / 3;
  align-self: center;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--border);
}
.dot-online { background: var(--ok); }

/* ============================ Dialogs ============================ */

.sheet {
  width: min(440px, calc(100vw - 32px));
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  padding: 24px;
}
.sheet::backdrop { background: rgb(4 8 20 / 62%); }
.sheet h2 { font-size: 1.2rem; margin-bottom: 16px; }

.profile {
  margin: 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 8px 18px;
}
.profile dt { color: var(--muted); font-size: .9375rem; }
.profile dd { margin: 0; font-weight: 600; }

.sheet-actions { display: flex; justify-content: flex-end; margin-top: 22px; }

/* ============================ Responsive ============================ */

@media (max-width: 860px) {
  .app,
  .app:has(#members-panel[hidden]) {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "top"
      "warn"
      "conn"
      "room"
      "members";
    grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  }
  .members {
    border-left: 0;
    border-top: 1px solid var(--border);
    max-height: 45vh;
  }
  .topbar-me { order: 3; width: 100%; }
  .topbar-actions { margin-left: 0; width: 100%; }
  .msg { max-width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
    scroll-behavior: auto !important;
  }
}

@media (forced-colors: active) {
  .btn, .bubble, .member, .sheet { border: 1px solid CanvasText; }
  .dot-online { background: Highlight; }
}

.count {
  font-size: .8125rem;
  font-weight: 700;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--text);
}
