:root {
  --bg: #0e1116;
  --bg-2: #161b22;
  --bg-3: #1c2229;
  --line: #2a323c;
  --text: #dfe6ee;
  --muted: #8b98a8;
  --accent: #4c8dff;
  --accent-2: #35c48f;
  --danger: #ef5e5e;
  --warn: #e8b64c;
  --root: #d977f0;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

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

button {
  font: inherit;
  color: inherit;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
}
button:hover { border-color: #3d4855; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { background: #3d7cf0; }
button.ghost { background: transparent; }
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.icon { padding: 2px 8px; line-height: 1.2; }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 11px;
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(76, 141, 255, 0.4); border-color: var(--accent); }

label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
label.checkbox { display: flex; align-items: center; gap: 8px; color: var(--text); }
label.checkbox input { width: auto; }

.hint { font-size: 12.5px; color: var(--muted); margin: 8px 0 0; }
.error { color: var(--danger); min-height: 20px; font-size: 13.5px; margin: 10px 0 0; }
.status { color: var(--muted); font-size: 13px; min-height: 18px; margin: 4px 0 0; }

/* ------------------------------------------------------------------- gate */
.gate {
  display: grid;
  place-items: center;
  min-height: 100%;
  padding: 24px;
  background: radial-gradient(1000px 600px at 50% -10%, #1a2434, var(--bg));
}
.gate[hidden] { display: none; }
.gate-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 28px;
}
.gate-card h1 { margin: 0; font-size: 28px; letter-spacing: -0.5px; }
.tagline { margin: 4px 0 20px; color: var(--muted); font-size: 14px; }

.tabs { display: flex; gap: 6px; background: var(--bg); padding: 4px; border-radius: 10px; border: 1px solid var(--line); }
.tab { flex: 1; background: transparent; border: none; padding: 8px; border-radius: 7px; font-size: 13.5px; color: var(--muted); }
.tab.active { background: var(--bg-3); color: var(--text); font-weight: 600; }

.pane { display: none; }
.pane.active { display: block; }
.pane button[type="submit"] { width: 100%; margin-top: 18px; padding: 11px; }

/* -------------------------------------------------------------------- app */
.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-rows: 52px 1fr;
  grid-template-areas: "top top" "side main";
  height: 100%;
}
.app[hidden] { display: none; }

.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; letter-spacing: -0.3px; }
.me { display: flex; align-items: center; gap: 6px; }
.nick { font-weight: 600; }
.spacer { flex: 1; }
.conn { font-size: 12px; color: var(--muted); font-family: var(--mono); }
.conn.open { color: var(--accent-2); }
.conn.closed { color: var(--danger); }

.badge {
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 20px;
  border: 1px solid var(--line);
  color: var(--muted);
}
.badge.root { color: var(--root); border-color: var(--root); }
.badge.op { color: var(--accent-2); border-color: var(--accent-2); }
.badge.user { color: var(--accent); border-color: var(--accent); }
.badge.guest { color: var(--muted); }

/* ---------------------------------------------------------------- sidebar */
.sidebar {
  grid-area: side;
  display: flex;
  flex-direction: column;
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  overflow: hidden;
}
.panel { padding: 12px; border-bottom: 1px solid var(--line); min-height: 0; }
.panel.grow { flex: 1; display: flex; flex-direction: column; }
.panel h2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 8px;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.count { font-family: var(--mono); }

.room-list, .member-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.member-list { flex: 1; }
.room-list li, .member-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 14px;
}
.room-list li:hover, .member-list li:hover { background: var(--bg-3); }
.room-list li.active { background: var(--bg-3); font-weight: 600; }
.room-list li .n { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-list li .c { font-size: 11.5px; color: var(--muted); font-family: var(--mono); }
.room-list li button.x {
  background: none;
  border: none;
  padding: 0 4px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
  border-radius: 4px;
}
.room-list li button.x:hover { color: var(--danger); }
.room-list li:focus-visible,
.member-list li:focus-visible,
.room-list li button.x:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
/* Unread activity in a room the user is not currently looking at. */
.room-list li .unread {
  min-width: 18px;
  padding: 0 5px;
  border-radius: 20px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  text-align: center;
  font-family: var(--mono);
}
.room-list li.has-unread .n { font-weight: 700; color: var(--text); }

.member-list li .sigil { width: 10px; color: var(--muted); font-family: var(--mono); }
.member-list li .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-list li.root .name { color: var(--root); }
.member-list li.operator .name { color: var(--accent-2); }
.member-list li .mediaflags { font-size: 11px; letter-spacing: 1px; }

/* ------------------------------------------------------------------- main */
.main { grid-area: main; display: flex; flex-direction: column; min-width: 0; overflow: hidden; }
.roombar { padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--bg-2); }
.roombar h1 { margin: 0; font-size: 16px; }
.topic { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }

/* Tiles are sized by height (width follows from the aspect ratio) and the height
   is computed in layoutVideoGrid() so the whole grid always fits: no clipped
   tiles and nothing to scroll. */
.video-grid {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 8px;
  padding: 10px;
  background: #0a0d12;
  border-bottom: 1px solid var(--line);
  max-height: 46vh;
  overflow: hidden;
}
.video-grid[hidden] { display: none; }
.tile {
  position: relative;
  flex: 0 0 auto;
  height: var(--tile-height, 200px);
  aspect-ratio: 16 / 9;
  background: #000;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
/* The picture fills its cell: cropped, never letterboxed or stretched.
   `--video-fit` switches to `contain` from the settings dialog. */
.tile video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: var(--video-fit, cover);
  object-position: center;
  display: block;
  background: #000;
}
.tile video[hidden] { display: none; }
.tile.audio-only { display: grid; place-items: center; }
.tile .avatar { font-size: 34px; opacity: 0.5; }
.tile .label {
  position: absolute;
  left: 6px;
  bottom: 6px;
  background: rgba(0, 0, 0, 0.65);
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11.5px;
  display: flex;
  gap: 6px;
  align-items: center;
}
.tile.speaking { border-color: var(--accent-2); }
.tile .mute-dot { color: var(--danger); }

/* Per-feed volume, revealed on hover or keyboard focus. */
.tile-controls {
  position: absolute;
  right: 6px;
  bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 6px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.65);
  opacity: 0;
  transition: opacity 120ms ease;
}
.tile:hover .tile-controls,
.tile:focus-within .tile-controls { opacity: 1; }
.tile-controls[hidden] { display: none; }
.tile-controls button.tile-mute {
  background: none;
  border: none;
  padding: 0;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}
input[type="range"].volume { width: 78px; accent-color: var(--accent); }
.tile.audio-only .tile-controls { opacity: 1; position: static; margin-top: 8px; }

.volume-row { display: flex; align-items: center; gap: 10px; }
.volume-row input[type="range"] { flex: 1; accent-color: var(--accent); }

.roombar { display: flex; align-items: center; gap: 10px; }
.roombar-text { flex: 1; min-width: 0; }
.roombar-text h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#btn-room-actions { font-size: 18px; line-height: 1; padding: 4px 10px; }

.messages { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 2px; }
/* flex: none is required: as a flex child in an overflowing column, a message
   would otherwise be shrunk below its content height and its text would spill
   over the next line. */
.msg { flex: none; padding: 2px 0; word-wrap: break-word; overflow-wrap: anywhere; }
.msg .time { color: #5a6675; font-size: 11px; font-family: var(--mono); margin-right: 8px; }
.msg .who { font-weight: 600; }
.msg .who.root { color: var(--root); }
.msg .who.operator { color: var(--accent-2); }
.msg .who.user { color: var(--accent); }
.msg .who.guest { color: var(--muted); }
.msg .body { white-space: pre-wrap; }
.msg.history { opacity: 0.68; }
.msg.me .body { font-style: italic; color: #b9c6d6; }
.msg.system .body { color: var(--muted); font-size: 13.5px; }
.msg.system.moderation .body { color: var(--warn); }
.msg.system.error .body { color: var(--danger); }
.msg.system.announce .body {
  color: var(--warn);
  border-left: 3px solid var(--warn);
  padding-left: 8px;
  font-weight: 600;
}
.msg.private .body { color: #c9a6f0; }
.msg pre.block {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  white-space: pre;
  margin: 4px 0;
}

.composer { border-top: 1px solid var(--line); background: var(--bg-2); padding: 10px 12px; }
.media-controls { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
button.media { display: flex; align-items: center; gap: 6px; font-size: 13px; }
button.media.on { background: var(--accent-2); border-color: var(--accent-2); color: #06231a; font-weight: 600; }
button.media.paused { background: var(--warn); border-color: var(--warn); color: #2b1f00; }
#form-send { display: flex; gap: 8px; }
#form-send input { flex: 1; }

/* ------------------------------------------------------------------ modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(4, 7, 11, 0.7);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 50;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  width: 100%;
  max-width: 420px;
  max-height: 86vh;
  overflow-y: auto;
}
.modal.wide { max-width: 780px; }
.modal h2 { margin: 0 0 12px; font-size: 17px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.admin-tabs { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.admin-tabs .tab { flex: none; padding: 6px 12px; border: 1px solid var(--line); border-radius: 8px; }
.admin-body { min-height: 220px; }
.admin-body table { width: 100%; border-collapse: collapse; font-size: 13px; }
.admin-body th, .admin-body td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.admin-body th { color: var(--muted); font-weight: 600; font-size: 11.5px; text-transform: uppercase; }
.admin-body td.actions { text-align: right; white-space: nowrap; }
.admin-body .row-form { display: flex; gap: 8px; align-items: flex-end; margin-bottom: 14px; flex-wrap: wrap; }
.admin-body .row-form > div { flex: 1; min-width: 140px; }
.admin-body .row-form label { margin-top: 0; }
.admin-body pre {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  overflow: auto;
  max-height: 340px;
}
.member-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }

/* ---------------------------------------------------------------- settings */
.switches {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 16px 0 0;
}
.switches legend { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); padding: 0 4px; }
.switches label.checkbox { margin: 6px 0; font-size: 13.5px; }
.settings-actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.meter { height: 8px; background: var(--bg); border: 1px solid var(--line); border-radius: 20px; margin-top: 10px; overflow: hidden; }
.meter-bar { height: 100%; width: 0%; background: var(--accent-2); transition: width 80ms linear; }
.member-list li.hidden-member .name { opacity: 0.65; font-style: italic; }

/* ------------------------------------------------------- connection banner */
.banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 7px 12px;
  font-size: 13px;
  background: var(--warn);
  color: #2b1f00;
  font-weight: 600;
}
.banner[hidden] { display: none; }
.banner.offline { background: var(--danger); color: #fff; }
.banner button {
  background: rgba(0, 0, 0, 0.18);
  border-color: transparent;
  color: inherit;
  padding: 3px 10px;
  font-size: 12px;
  font-weight: 600;
}

/* The sidebar toggle only exists on narrow screens. */
#btn-sidebar { display: none; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; grid-template-areas: "top" "main"; }
  #btn-sidebar { display: block; }
  .sidebar { display: none; }
  .sidebar.open {
    display: flex;
    position: fixed;
    inset: 52px 0 0 0;
    z-index: 20;
    border-right: none;
  }
  .brand { display: none; }
  .video-grid { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); max-height: 38vh; }
  .media-controls button.media span { display: none; }
  .modal { padding: 16px; }
}
