/* base.css — layout, components, and structural styles.
   All colours come from CSS custom properties so themes only need to
   redefine the variables; no structural rules are repeated per-theme. */

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

/* Form elements don't inherit font by default; without this iOS Safari
   zooms in on focus when font-size < 16px. */
input, textarea, select, button { font: inherit; }

:root {
  --font-sans: system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: 'Cascadia Code', 'Fira Code', 'JetBrains Mono', monospace;
  --font-size: 17px;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 16px;

  --sidebar-width: 240px;
  --composer-height: 80px;
  --call-bar-height: 0px; /* expands when a call is active */

  --transition: 150ms ease;
}

html, body { height: 100%; font-family: var(--font-sans); }
body { background: var(--bg-base); color: var(--text-primary); }

/* Reset font size */
html, body, h1, h2, h3, h4, h5, h6, p, a, button, input, select, time {
  font-size: var(--font-size);
}

details summary {
  cursor: pointer;
}

/* ── App shell ───────────────────────────────────────────────────────────── */
.app-shell {
  display: flex;
  height: 100%;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  height: 100vh;
  position: sticky;
  top: 0;
  z-index: 1;
  padding-top: env(safe-area-inset-top);
}

.hub-group { padding: 8px 0; }
.hub-header {
  padding: 6px 16px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
}
.channel-list {
  list-style: none;
  text-transform: none;
}

.hub-name {
  display: flex;
  align-items: center;
  gap: 2px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.hub-name > span { flex: 1; }

.channel-item {
  display: flex;
  align-items: center;
  border-bottom: solid 2px transparent;
  border-top: solid 2px transparent;
}

.channel-item a {
  color: var(--text-secondary);
  text-decoration: none;
  flex: 1;
  height: 2.3rem;
  display: flex;
  align-items: center;
  padding-left: .7rem;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.channel-item a:hover {
  color: var(--text-primary);
}

.channel-item.active a {
  color: var(--text-primary);
}

/* ── Channel + hub drag-and-drop reordering (desktop) ───────────────────── */
@media (min-width: 1025px) {
  .channel-item[draggable="true"] { cursor: grab; }
  .channel-item.dragging { opacity: 0.4; }
  .channel-item.drop-before { border-top: 2px solid var(--accent, #5865f2); }
  .channel-item.drop-after { border-bottom: 2px solid var(--accent, #5865f2); }

  .hub-header[draggable="true"] { cursor: grab; }
  .hub-header.dragging { opacity: 0.4; }
  .hub-header.drop-before { border-top: 2px solid var(--accent, #5865f2); }
  .hub-header.drop-after { border-bottom: 2px solid var(--accent, #5865f2); }
}

/* ── Sidebar gear buttons ────────────────────────────────────────────────── */
.btn-hub-gear,
.btn-channel-gear {
  display: none;
  flex-shrink: 0;
}

/* Desktop (≥ 1025px): reveal gear on row hover */
@media (min-width: 1025px) {
  .hub-name:hover .btn-hub-gear,
  .channel-item:hover .btn-channel-gear {
    display: flex;
  }
}

/* ── Direct Messages section ─────────────────────────────────────────────── */
.dm-section {
  margin-top: 4px;
  padding: 8px 0 4px;
  border-top: 1px solid var(--border);
}

.dm-section-title {
  padding: 4px 16px 6px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Avatar-style bullet before "Direct Messages" label */
.dm-section-title::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.6;
  flex-shrink: 0;
}

.dm-list {
  list-style: none;
}

.dm-item {
  display: flex;
  align-items: center;
}

.dm-link {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  height: 2rem;
  padding: 0 16px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.9em;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  transition: color var(--transition), background var(--transition);
}

.dm-link:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.dm-item.dm-selected .dm-link {
  color: var(--text-primary);
  background: var(--bg-active);
  font-weight: 600;
}

.dm-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Unread dot on DM/channel items — blue for normal mentions, red for urgent.
   Uses data attributes instead of classes so rdbljs className re-renders
   (which only touch el.className) never wipe the dot state. */
.dm-item[data-mention] .dm-link::after,
.channel-item[data-mention] .channel-link::after {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  margin-left: auto;
  flex-shrink: 0;
}
.dm-item[data-urgent] .dm-link::after,
.channel-item[data-urgent] .channel-link::after {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-danger);
  margin-left: auto;
  flex-shrink: 0;
}

/* ── Action sheet (mobile + iPad) ────────────────────────────────────────── */
.action-sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition);
}
.action-sheet-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.action-sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 201;
  background: var(--bg-sidebar);
  border-top: 1px solid var(--border);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 12px 0;
  padding-bottom: env(safe-area-inset-bottom, 16px);
  transform: translateY(100%);
  transition: transform var(--transition);
  max-width: 640px;
  margin: 0 auto;
}
.action-sheet.open { transform: translateY(0); }

.action-sheet-handle {
  width: 36px; height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 12px;
}

.action-sheet-label {
  padding: 4px 20px 8px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
}

.action-sheet-item {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 14px 20px;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 1rem;
  text-align: left;
  cursor: pointer;
}
.action-sheet-item:hover,
.action-sheet-item:active { background: var(--bg-hover); }
.action-sheet-item.danger { color: var(--color-danger); }
.action-sheet-item:disabled { color: var(--text-muted); cursor: default; opacity: 0.45; pointer-events: none; }

/* ── Modal (desktop) ─────────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition);
}
.modal-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 480px;
  padding: 28px 32px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  transform: scale(0.97);
  transition: transform var(--transition);
}
.modal-backdrop.open .modal { transform: scale(1); }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-title { font-size: 1.1rem; font-weight: 700; }
.modal-close {
  background: none; border: none; cursor: pointer;
  color: var(--text-muted); font-size: 1.2rem; padding: 4px;
}
.modal-close:hover { color: var(--text-primary); }

.modal-body { display: flex; flex-direction: column; gap: 16px; }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  border-top: 1px solid var(--border);
  padding-top: 20px;
  margin-top: 4px;
}

.modal-danger-zone {
  border-top: 1px solid var(--border);
  padding-top: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.modal-danger-zone p { font-size: 13px; color: var(--text-muted); }
.btn-danger {
  align-self: flex-start;
  padding: 8px 16px;
  border: 1px solid var(--color-danger);
  border-radius: var(--radius-sm);
  background: none;
  color: var(--color-danger);
  font-weight: 600;
  cursor: pointer;
}
.btn-danger:hover { background: color-mix(in srgb, var(--color-danger) 12%, transparent); }

.sidebar-footer {
  margin-top: auto;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-username {
  font-size: 12px;
  color: var(--text-muted);
  padding: 0 2px;
}

.sidebar-footer-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}
.sidebar-footer-controls select {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-secondary);
  cursor: pointer;
}
.btn-sm {
  font-size: 12px;
  padding: 4px 8px;
  white-space: nowrap;
}

.btn-new-hub {
  width: 100%;
  padding: 7px 10px;
  border-radius: 6px;
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  text-align: left;
}
.btn-new-hub:hover {
  background: var(--bg-input);
  color: var(--text-primary);
  border-color: var(--accent);
}

.btn-enable-push {
  width: 100%;
  padding: 7px 10px;
  border-radius: 6px;
  border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  color: var(--text-primary);
  font-size: 13px;
  cursor: pointer;
  text-align: left;
  margin-bottom: 6px;
}
.btn-enable-push:hover {
  background: color-mix(in srgb, var(--accent) 15%, transparent);
  color: var(--text-primary);
  border-color: var(--accent);
}
.btn-enable-push[data-blocked] {
  opacity: 0.55;
  cursor: default;
  border-style: dashed;
}
.btn-enable-push[data-blocked]:hover {
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  color: var(--text-secondary);
  border-color: color-mix(in srgb, var(--accent) 40%, transparent);
}

.user-avatar {
  width: 30px; height: 30px; border-radius: 50%; background: var(--accent); display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; color: white; flex-shrink: 0;
}
.user-name { 
  font-weight: 600;
  color: var(--text-primary);
}

/* ── Main content ────────────────────────────────────────────────────────── */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100vh;
  z-index: 2;
}

/* ── Call bar ────────────────────────────────────────────────────────────── */
.call-bar {
  background: var(--bg-call);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.call-controls { display: flex; align-items: center; gap: 8px; }
.streams { display: flex; flex-wrap: wrap; gap: 8px; }
.stream-tile { position: relative; border-radius: var(--radius-md); overflow: hidden; background: var(--bg-sidebar); }
.stream-tile video { width: 160px; height: 90px; object-fit: cover; display: block; }

/* ── Chat panel ──────────────────────────────────────────────────────────── */
.chat-panel { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-header { padding: 14px 20px; padding-top: calc(14px + env(safe-area-inset-top)); border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 12px; }
.chat-title {
  font-size: 1rem;
  margin-bottom: 7px;
}
.chat-topic { color: var(--text-secondary); }
.chat-header-actions { margin-left: auto; display: flex; gap: 6px; }

.search-bar { padding: 8px 20px; background: var(--bg-topbar); border-bottom: 1px solid var(--border); }
.search-bar input {
  width: 100%; padding: 8px 12px; border-radius: var(--radius-md); border: 1px solid var(--border); background: var(--bg-input); color: var(--text-primary);
}
.search-results { list-style: none; margin-top: 8px; display: flex; flex-direction: column; gap: 4px; }
.search-result {
  padding: 8px 12px; border-radius: var(--radius-sm); background: var(--bg-hover);
}
.result-handle { font-weight: 600; margin-right: 8px; }

.messages {
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}

.message { padding: 4px 0; display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; column-gap: 10px; }
.message-handle { font-weight: 600; color: var(--accent); grid-column: 1; grid-row: 1; }
.message-handle.dm-trigger { cursor: pointer; }
.message-handle.dm-trigger:hover { text-decoration: underline; }
.message-time { font-size: 11px; color: var(--text-muted); grid-column: 2; grid-row: 1; align-self: end; }
.message-text { grid-column: 1 / -1; grid-row: 2; line-height: 1.5; color: var(--text-primary); white-space: pre-wrap; word-break: break-word; }

.composer {
  margin-top: auto;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px;
  padding: 0 20px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-base);
  position: relative;
}
.composer textarea {
  flex: 1;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-primary);
  font-family: var(--font-sans);
  resize: none;
  line-height: 1.5;
  max-height: 160px;
  overflow-y: auto;
}
.composer textarea:focus { outline: none; border-color: var(--accent); }

/* Urgent toggle button */
.btn-urgent-toggle {
  background: none;
  border: none;
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  opacity: 0.4;
  transition: opacity var(--transition);
  flex-shrink: 0;
  align-self: center;
}
.btn-urgent-toggle:hover { opacity: 0.7; }
.btn-urgent-toggle.is-urgent { opacity: 1; }

/* Composer in urgent mode — textarea border turns red */
.composer.composer-urgent textarea,
.composer.composer-urgent textarea:focus { border-color: var(--color-danger); }

/* ── Load-more sentinel ──────────────────────────────────────────────────── */
.load-more-sentinel {
  height: 1px;
  margin: 0;
  padding: 0;
}

/* ── Date separator ──────────────────────────────────────────────────────── */
.date-separator {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 16px 0 8px;
  color: var(--text-muted);
  font-size: 12px;
}
.date-separator::before,
.date-separator::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}
.date-separator-label {
  white-space: nowrap;
  padding: 0 6px;
  font-weight: 500;
}

/* ── @mention inline highlight ───────────────────────────────────────────── */
.mention      { background: color-mix(in srgb, var(--accent) 15%, transparent); color: var(--accent); border-radius: 3px; padding: 0 2px; font-weight: 500; }
.mention-self { background: color-mix(in srgb, var(--accent) 30%, transparent); font-weight: 600; }

/* ── @mention picker popup ───────────────────────────────────────────────── */
.mention-picker {
  position: absolute;
  bottom: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 4px 24px rgba(0,0,0,.4);
  z-index: 200;
  max-height: 240px;
  overflow-y: auto;
}
.mention-option {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 12px;
  background: none;
  border: none;
  cursor: pointer;
  text-align: left;
  color: var(--text-primary);
  font-size: 14px;
}
.mention-option:hover,
.mention-option.selected { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.mention-option-name   { font-weight: 600; color: var(--text-primary); }
.mention-option-handle { color: var(--text-muted); font-size: 13px; }

/* ── Auth page ───────────────────────────────────────────────────────────── */
.auth-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.auth-shell { width: 100%; max-width: 420px; padding: 40px 32px; background: var(--bg-sidebar); border-radius: var(--radius-lg); border: 1px solid var(--border); }
.auth-brand h1 { font-weight: 800; color: var(--accent); letter-spacing: -0.5px; }
.auth-brand p { color: var(--text-muted); margin-top: 4px; margin-bottom: 28px; }
.auth-tabs { display: flex; gap: 4px; margin-bottom: 24px; border-bottom: 1px solid var(--border); }
.auth-tabs a { background: none; border: none; padding: 8px 16px; font-size: 14px; font-weight: 600; cursor: pointer; color: var(--text-muted); border-bottom: 2px solid transparent; margin-bottom: -1px; transition: color var(--transition), border-color var(--transition); }
.auth-tabs a[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }

.auth-form { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-weight: 600; color: var(--text-secondary); }
.field input {
  padding: 10px 14px; border-radius: var(--radius-md); border: 1px solid var(--border); background: var(--bg-input); color: var(--text-primary);
}
.field input:focus { outline: none; border-color: var(--accent); }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn-primary {
  padding: 10px 20px; border: none; border-radius: var(--radius-md);
  background: var(--accent); color: white; font-weight: 700;
  cursor: pointer; transition: opacity var(--transition);
}
.btn-primary:hover { opacity: 0.85; }
.btn-ghost {
  background: none; border: none; padding: 6px 12px; border-radius: var(--radius-sm);
  color: var(--text-muted); cursor: pointer;
}
.btn-ghost:hover { background: var(--bg-hover); color: var(--text-primary); }
.btn-icon {
  background: none; border: none; padding: 8px; border-radius: var(--radius-sm); color: var(--text-muted); cursor: pointer;
}
.btn-icon:hover { background: var(--bg-hover); color: var(--text-primary); }
.btn-icon.active { color: var(--accent); }
.btn-call-join {
  padding: 6px 16px; background: var(--color-success); color: white; border: none; border-radius: var(--radius-sm);
  font-weight: 600; cursor: pointer;
}
.btn-call-leave {
  padding: 6px 16px; background: var(--color-danger); color: white; border: none; border-radius: var(--radius-sm);
  font-weight: 600; cursor: pointer;
}
.btn-send { padding: 10px 18px; background: var(--accent); color: white; border: none; border-radius: var(--radius-md); font-size: 14px; font-weight: 700; cursor: pointer; }

/* ── Alerts ──────────────────────────────────────────────────────────────── */
.alert {
  padding: 10px 14px;
  border-radius: var(--radius-md);
  margin: 0 auto;
  position: absolute;
  top: 3.5rem;
}

.alert-error {
  background: color-mix(in srgb, var(--color-danger) 15%, transparent); 
  color: var(--color-danger);
  border: 1px solid var(--color-danger);
  opacity: 1;
  animation: fadeOut 2s forwards;
}

@keyframes fadeOut {
  0% { opacity: 1;}
  10% { opacity: .9;}
  20% { opacity: .8;}
  30% { opacity: .7;}
  40% { opacity: .6;}
  50% { opacity: .5;}
  60% { opacity: .4;}
  70% { opacity: .3;}
  80% { opacity: .2;}
  90% { opacity: .1;}
  100% { opacity: 0;}
}

/* ── Theme picker ────────────────────────────────────────────────────────── */
select#theme-picker {
  padding: 4px 8px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--bg-input); color: var(--text-primary);
  cursor: pointer;
}

/* ── Utilities ───────────────────────────────────────────────────────────── */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; }
.inline { display: inline; }
mark { background: color-mix(in srgb, var(--accent) 30%, transparent); color: var(--text-primary); border-radius: 2px; }

/* ── Icon stand-ins (replace with SVG sprite or icon font) ───────────────── */
.icon-menu::before { content: '☰'; }
.icon-search::before { content: '🔍'; font-size: 14px; }
.icon-mic::before { content: '🎙'; }
.icon-cam::before { content: '📷'; }
.icon-screen::before { content: '🖥'; }

.signin {
  display: flex;
  flex-direction: column;
  margin: auto;
}

.signin footer {
  position: fixed;
  bottom: 0;
  margin: auto;
}


/* ── Back button (mobile only) ───────────────────────────────────────────── */
.btn-back-mobile {
  display: none;
}

/* ── Mobile ──────────────────────────────────────────────────────────────── */
@media (max-width: 700px) {
  /* Both panels are full-screen and fixed; only one is on-screen at a time.
     No z-index needed — they slide in opposite directions so they never overlap. */

  /* dvh = dynamic viewport height — shrinks when the keyboard opens,
     so the panels resize and the composer/footer stay above the keyboard. */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 100dvh;
    width: 100%;
    transform: translateX(0);
    transition: transform var(--transition);
    overflow-y: auto;
  }

  .main-content {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 100dvh;
    width: 100%;
    transform: translateX(100%);
    transition: transform var(--transition);
    overflow: hidden;
  }

  /* On channel pages (.main-content exists): panel visible, sidebar off-screen left.
     :has() fires based on DOM structure — no JS class needed for the default state. */
  body:has(.main-content) .sidebar {
    transform: translateX(-100%);
  }
  body:has(.main-content) .main-content {
    transform: translateX(0);
  }

  /* User tapped back / swiped: show sidebar, hide panel */
  body.sidebar-open .sidebar {
    transform: translateX(0);
  }
  body.sidebar-open .main-content {
    transform: translateX(100%);
  }

  /* Show back button */
  .btn-back-mobile {
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    padding: 6px 10px 6px 4px;
    font-size: 1.2rem;
    border-radius: var(--radius-sm);
    flex-shrink: 0;
  }
  .btn-back-mobile:hover { color: var(--text-primary); }
}

/* ── Tile panel ───────────────────────────────────────────────────────────── */

.tile-panel {
  display: none;
  flex-direction: column;
  background: var(--bg-sidebar);
  border-left: 1px solid var(--border);
  overflow: hidden;
}
.tile-panel.active { display: flex; }

.tile-panel-header {
  display: flex;
  align-items: center;
  padding: 6px 10px;
  gap: 6px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-topbar);
  flex-shrink: 0;
}
.tile-panel-title {
  flex: 1;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

/* ── Mode A: sidebar (desktop ≥ 1025px) ─────────────────────────────────── */

@media (min-width: 1025px) {
  .main-content.has-call {
    flex-direction: row;
  }
  .main-content.has-call .chat-panel {
    flex: 1;
    min-width: 0;
    height: 100%;
  }
  .tile-panel {
    width: 280px;
    flex-shrink: 0;
    height: 100vh;
    overflow-y: auto;
  }
  .tile-panel.collapsed {
    width: 40px;
    overflow: hidden;
  }
  .tile-panel.collapsed .tile-grid,
  .tile-panel.collapsed .tile-panel-title { display: none; }
  .tile-panel.collapsed .tile-panel-header {
    flex-direction: column;
    padding: 6px 4px;
    height: 100%;
    justify-content: flex-start;
    border-left: none;
  }
}

/* ── Mode B: overlay (mobile < 1025px) ──────────────────────────────────── */

/* Only tablet needs position:relative — mobile .main-content is already
   position:fixed which also creates a containing block for the absolute tile panel. */
@media (min-width: 701px) and (max-width: 1024px) {
  .main-content.has-call {
    position: relative;
  }
}

@media (max-width: 1024px) {
  .tile-panel {
    position: absolute;
    top: 0; right: 0;
    width: min(220px, 45vw);
    max-height: 40vh;
    z-index: 20;
    border-left: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    border-radius: 0 0 0 var(--radius-md);
    background: color-mix(in srgb, var(--bg-sidebar) 92%, transparent);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    overflow-y: auto;
  }
  .tile-panel.collapsed {
    width: auto;
    height: auto;
    max-height: none;
    border-radius: var(--radius-md);
    top: 8px; right: 8px;
  }
  .tile-panel.collapsed .tile-grid,
  .tile-panel.collapsed .tile-panel-title { display: none; }
  .tile-panel.collapsed .tile-panel-header {
    padding: 4px 8px;
    border-bottom: none;
  }
}

/* ── Tile grid (inside .tile-panel in both modes) ─────────────────────────  */

.tile-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px;
  flex: 1;
  overflow-y: auto;
  align-content: flex-start;
}

/* Collapsed: hide video, keep audio alive */
.tile-panel.collapsed .tile-grid video { visibility: hidden; }

/* Stream tiles: fill panel width, natural height */
.stream-tile {
  position: relative;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--bg-sidebar);
  cursor: pointer;
  flex: 1 1 100%;
  aspect-ratio: 16 / 9;
}
.stream-tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
.stream-tile .tile-label {
  position: absolute; bottom: 4px; left: 6px;
  font-size: 11px; font-weight: 600; color: white;
  text-shadow: 0 1px 3px rgba(0,0,0,0.6); pointer-events: none;
}

/* 2+ tiles in sidebar mode: side-by-side if panel is wide enough */
@media (min-width: 1025px) {
  .tile-grid:has(.stream-tile:nth-child(2)) .stream-tile {
    flex: 1 1 calc(50% - 3px);
  }
}

/* 5+ tiles: avatar strip */
.tile-grid.avatars-only .stream-tile video { display: none; }
.tile-grid.avatars-only .stream-tile {
  flex: 0 0 36px; height: 36px; border-radius: 50%; overflow: hidden; aspect-ratio: unset;
}

/* Pinned tile */
.tile-grid.pinned .stream-tile.pinned-tile { flex: 1 1 100%; order: -1; }
.tile-grid.pinned .stream-tile:not(.pinned-tile) { flex: 1 1 calc(50% - 3px); }

/* ── Call: status row (shown when a call is active, user hasn't joined) ──── */
.call-status {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 16px;
  background: var(--bg-topbar);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.call-status[hidden] { display: none; }
.call-status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--color-success, #3ba55c); flex-shrink: 0;
}
.call-status-info { flex: 1; color: var(--text-secondary); }
.call-status-avatars { display: flex; }
.call-status-avatar {
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--bg-topbar);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 700; color: white; margin-left: -4px;
}
.call-status-avatar:first-child { margin-left: 0; }

/* ── Call: in-call controls bar ─────────────────────────────────────────── */
.call-controls-bar {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  background: var(--bg-topbar);
  border-bottom: 1px solid var(--border);
}
.call-controls-bar.active { display: flex; }
.call-peer-count { color: var(--text-muted); font-size: 13px; }
.btn-leave {
  margin-left: auto;
  padding: 4px 12px;
  background: var(--color-danger, #ed4245);
  color: white; border: none; border-radius: var(--radius-sm);
  font-weight: 600; cursor: pointer; font-size: 13px;
}
.btn-leave:hover { opacity: 0.85; }

/* ── Device picker panel ──────────────────────────────────────────────────── */
.device-picker {
  display: none;
  flex-direction: column;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg-topbar);
  border-bottom: 1px solid var(--border);
}
.device-picker.open { display: flex; }

.device-picker-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--text-secondary);
}
.device-picker-row label { width: 80px; flex-shrink: 0; font-weight: 600; }
.device-picker-row select {
  flex: 1;
  padding: 5px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 13px;
}
.device-picker-row video {
  width: 80px; height: 45px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  background: var(--bg-sidebar);
  flex-shrink: 0;
}
.device-picker-row canvas {
  width: 80px; height: 12px;
  border-radius: 3px;
  background: var(--bg-sidebar);
  flex-shrink: 0;
}
.device-picker-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Warning indicator on gear button */
.btn-icon.device-warning { color: var(--color-danger); }
.btn-icon.device-warning::after {
  content: '!';
  font-size: 9px;
  font-weight: 700;
  color: var(--color-danger);
  vertical-align: super;
}

/* Device disconnected toast */
.device-warning-toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-danger);
  color: white;
  padding: 8px 16px;
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 600;
  z-index: 200;
  pointer-events: none;
  animation: toast-in 0.2s ease;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ── Call: mini-bar (persists while navigating away during a call) ────────── */
.call-mini-bar {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-topbar);
  border-top: 1px solid var(--border);
  font-size: 13px;
}
.call-mini-bar.active { display: flex; }
.call-mini-bar-channel {
  flex: 1; font-weight: 600; color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  cursor: pointer;
}
.call-mini-bar-channel:hover { color: var(--accent); }

@media (max-width: 1024px) {
  .call-mini-bar {
    position: sticky; bottom: 0; z-index: 10;
    border-bottom: 1px solid var(--border);
  }
}

/* ── Call: sidebar badge (shows N in call on channel row) ─────────────────── */
.channel-item .call-badge {
  display: none;
  margin-left: auto;
  font-size: 11px; font-weight: 700;
  color: var(--color-success, #3ba55c);
  align-items: center; gap: 3px;
}
.channel-item.call-active .call-badge { display: flex; }
.channel-item.call-active .call-badge::before {
  content: '';
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--color-success, #3ba55c);
  display: inline-block;
}

/* ── Admin pages ──────────────────────────────────────────────────────────── */

.admin-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  height: 52px;
  padding-top: env(safe-area-inset-top);
  height: calc(52px + env(safe-area-inset-top));
  background: var(--bg-topbar);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.admin-topbar-brand {
  font-weight: 700;
  font-size: 15px;
  color: var(--text-primary);
  text-decoration: none;
}
.admin-topbar-brand:hover { color: var(--accent); }

.admin-topbar-user {
  font-size: 13px;
  color: var(--text-muted);
}

.admin-topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

body:has(.admin-topbar) main {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow-y: auto;
}

.admin-page {
  max-width: 900px;
  margin: 0 auto;
  padding: 32px 24px;
}

.admin-nav {
  display: flex;
  gap: 4px;
  margin-bottom: 32px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
}

.admin-nav-link {
  font-size: 14px;
  color: var(--text-secondary);
  text-decoration: none;
  padding: 6px 14px;
  border-radius: 6px 6px 0 0;
}
.admin-nav-link:hover { color: var(--text-primary); }
.admin-nav-link.active {
  color: var(--text-primary);
  background: var(--bg-input);
  font-weight: 600;
}

.admin-section {
  margin-bottom: 40px;
}
.admin-section h2 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.admin-notice {
  padding: 12px 16px;
  border-radius: 8px;
  margin-bottom: 24px;
  font-size: 14px;
  line-height: 1.5;
}
.admin-notice--success {
  background: color-mix(in srgb, var(--color-success, #3ba55c) 15%, transparent);
  border: 1px solid color-mix(in srgb, var(--color-success, #3ba55c) 40%, transparent);
  color: var(--text-primary);
}

.admin-empty {
  color: var(--text-secondary);
  font-size: 14px;
  font-style: italic;
}

.admin-hint {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 8px;
}

.admin-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 480px;
}
.admin-form--inline {
  flex-direction: row;
  align-items: center;
  max-width: none;
  margin-bottom: 16px;
}

.form-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.form-row label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}
.form-row input[type="text"],
.form-row input[type="password"],
.form-row input[type="number"] {
  padding: 7px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 14px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  cursor: pointer;
}

.token-display {
  display: inline-block;
  margin-top: 8px;
  padding: 8px 12px;
  background: var(--bg-input);
  border-radius: 6px;
  font-family: monospace;
  font-size: 13px;
  word-break: break-all;
  user-select: all;
}

.invite-link {
  display: inline-block;
  margin-top: 8px;
  font-size: 14px;
  word-break: break-all;
  color: var(--accent);
  user-select: all;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.data-table th {
  text-align: left;
  padding: 8px 12px;
  color: var(--text-secondary);
  font-weight: 500;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
}
.data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
}
.data-table tr:last-child td { border-bottom: none; }

.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 100px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.tag--admin { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent); }
.tag--bot   { background: color-mix(in srgb, #9b59b6 20%, transparent); color: #9b59b6; }
.tag--warn  { background: color-mix(in srgb, #f39c12 20%, transparent); color: #f39c12; }

.btn-danger { color: var(--color-danger, #e74c3c); }
.btn-danger:hover { background: color-mix(in srgb, var(--color-danger, #e74c3c) 15%, transparent); }

form.inline { display: inline; }

/* ── File uploads ─────────────────────────────────────────────────────────── */

.attachment-chips {
  width: 100%;
  order: -1;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 4px 0 2px;
}
.attachment-chips[hidden] { display: none; }

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-secondary);
  max-width: 200px;
}
.attachment-chip-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachment-chip-remove {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1;
  padding: 0 2px;
}
.attachment-chip-remove:hover { color: var(--color-danger, #e74c3c); }
.attachment-chip-error {
  background: color-mix(in srgb, var(--color-danger, #e74c3c) 15%, transparent);
  border-color: var(--color-danger, #e74c3c);
  color: var(--color-danger, #e74c3c);
  max-width: none;
}

/* Drop overlay shown when dragging a file over the composer */
.drop-overlay {
  position: absolute;
  inset: 0;
  background: color-mix(in srgb, var(--accent) 15%, transparent);
  border: 2px dashed var(--accent);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  color: var(--accent);
  pointer-events: none;
  z-index: 10;
}
.drop-overlay[hidden] { display: none; }

/* Attach button */
.btn-attach {
  flex-shrink: 0;
  font-size: 18px;
}

/* Attachment rendering inside messages */
.attachment-image-link { display: block; margin-top: 6px; }
.attachment-image {
  max-width: min(400px, 100%);
  max-height: 300px;
  border-radius: var(--radius-md);
  object-fit: contain;
  display: block;
}
.attachment-file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  padding: 6px 12px;
  border-radius: var(--radius-md);
  background: var(--bg-input);
  border: 1px solid var(--border);
  text-decoration: none;
  color: var(--text-secondary);
  font-size: 13px;
  max-width: 340px;
}
.attachment-file:hover { background: var(--bg-hover); color: var(--text-primary); }
.attachment-file-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-file-size { color: var(--text-muted); flex-shrink: 0; }

/* Sidebar: channel link highlighted when a file is hovered over it */
.channel-link.file-drop-hover {
  background: color-mix(in srgb, var(--accent) 20%, transparent);
  border-radius: var(--radius-sm);
  outline: 2px dashed var(--accent);
  outline-offset: -2px;
}

/* Sidebar toast notification */
.sidebar-toast {
  position: absolute;
  bottom: 60px;
  left: 12px;
  right: 12px;
  padding: 8px 12px;
  background: var(--bg-topbar);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 13px;
  color: var(--text-primary);
  z-index: 200;
  animation: toast-in 0.15s ease;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
