/* Purrty client styles. Palette carried over from Test Chat (indigo on navy),
   with a light theme under [data-theme="light"]. */
:root {
  --primary: #6366f1;
  --primary-light: #818cf8;
  --primary-dark: #4f46e5;
  --surface: #1e1e2e;
  --surface-alt: #282840;
  --text: #e0e0e0;
  --text-muted: #888;
  --border: #3a3a5c;
  --danger: #ef4444;
  --success: #22c55e;
  --owner: #ca8a04;
  --admin: #dc2626;
  --mod: #2563eb;
  color-scheme: dark;
}

[data-theme="light"] {
  --primary: #4f46e5;
  --primary-light: #6366f1;
  --primary-dark: #3730a3;
  --surface: #f8f8fc;
  --surface-alt: #eeeef5;
  --text: #1a1a2e;
  --text-muted: #666;
  --border: #d0d0e0;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--surface);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

h1, h2 { margin: 0; font-weight: 700; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.mono { font-family: Consolas, "Cascadia Mono", monospace; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.error-text { color: var(--danger); }

button, select, input {
  font: inherit;
  color: var(--text);
}

button {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s, opacity 0.15s;
}
button:hover:not(:disabled) { background: var(--surface); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover:not(:disabled) { background: var(--primary-dark); }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
button.danger:hover:not(:disabled), button.danger.armed { background: var(--danger); color: #fff; }
button.ghost { background: transparent; border-color: transparent; color: var(--text-muted); }
button.ghost:hover:not(:disabled) { background: var(--surface-alt); color: var(--text); }
button.small { padding: 3px 9px; font-size: 12px; }
button.wide { width: 100%; padding: 10px; font-weight: 600; border-radius: 8px; }
button.link { background: none; border: none; color: var(--primary-light); padding: 0; }
button.link:hover:not(:disabled) { background: none; text-decoration: underline; }
.icon-button { background: none; border: none; padding: 2px 6px; color: var(--text-muted); }
.icon-button:hover:not(:disabled) { background: var(--surface); color: var(--text); }

input, select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}
select { padding: 4px 8px; font-size: 12px; border-radius: 6px; }
input:focus, select:focus { outline: none; border-color: var(--primary); }

.error-banner {
  padding: 8px 10px;
  border-radius: 6px;
  background: rgba(239, 68, 68, 0.2);
  color: #f87171;
  font-size: 13px;
}
[data-theme="light"] .error-banner { color: #b91c1c; }

.splash {
  height: 100%;
  display: grid;
  place-items: center;
  color: var(--text-muted);
}

/* --- Sign in / register --- */
.auth-screen {
  height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  overflow-y: auto;
}
.auth-card {
  width: 100%;
  max-width: 384px;
  background: var(--surface-alt);
  border-radius: 12px;
  padding: 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
.auth-card h1 { text-align: center; font-size: 24px; color: var(--primary); margin-top: -4px; }
.auth-card > p { text-align: center; margin: -10px 0 0; font-size: 13px; }
.auth-logo { width: 56px; height: 56px; align-self: center; }

/* --- Chat layout --- */
.chat-layout { display: flex; height: 100%; }

.sidebar {
  width: 256px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface-alt);
  border-right: 1px solid var(--border);
}
.sidebar-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--primary); }
.brand img { width: 22px; height: 22px; }
.sidebar-scroll { flex: 1; overflow-y: auto; padding-bottom: 8px; }
.section-header {
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}
.section-header.spaced { margin-top: 16px; }
.section-note { padding: 4px 16px; font-size: 12px; color: var(--text-muted); }
.sidebar-item {
  display: flex;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-radius: 0;
  padding: 8px 16px;
}
.sidebar-item:hover:not(:disabled) { background: var(--surface); }
.sidebar-item.active { background: var(--surface); color: var(--primary-light); }

.profile {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}
.avatar {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
}
.profile-text { flex: 1; min-width: 0; }
.profile-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-role { font-size: 10px; letter-spacing: 0.04em; color: var(--primary-light); }
.logout:hover:not(:disabled) { color: var(--danger); background: none; }

.add-button {
  background: none;
  border: none;
  padding: 0 4px;
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
  color: var(--primary);
}
.add-button:hover:not(:disabled) { background: none; color: var(--primary-light); }
.item-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-item { align-items: center; gap: 8px; }
.sidebar-item.unread { font-weight: 600; }
.unread-badge {
  flex-shrink: 0;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.profile { cursor: pointer; }
.profile:hover, .profile.menu-open { background: var(--surface); }

/* Right-click menu */
.menu-backdrop { position: fixed; inset: 0; z-index: 50; outline: none; }
.context-menu {
  position: fixed;
  min-width: 170px;
  padding: 6px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.menu-item {
  text-align: left;
  background: none;
  border: none;
  border-radius: 4px;
  padding: 7px 10px;
}
.menu-item:hover:not(:disabled) { background: var(--primary); color: #fff; }
.menu-item:disabled { color: var(--text-muted); opacity: 0.5; }
.menu-item.danger { color: var(--danger); }
.menu-item.danger:hover:not(:disabled) { background: var(--danger); color: #fff; }

.menu-separator { height: 1px; margin: 4px 2px; background: var(--border); }
.menu-header { display: flex; align-items: center; gap: 10px; padding: 6px 8px 4px; min-width: 220px; }
.menu-header-text { flex: 1; min-width: 0; }
.menu-header-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-header .status-dot { border-color: var(--surface); }

.dm-dot { width: 8px; height: 8px; flex-shrink: 0; border-radius: 50%; background: var(--text-muted); }
.dm-dot.online { background: var(--success); }

/* Create Room form */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.55);
  display: grid;
  place-items: center;
  padding: 16px;
}
.modal {
  width: 100%;
  max-width: 440px;
  background: var(--surface-alt);
  border-radius: 12px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}
.modal h2 { font-size: 18px; }
.modal > p { margin: -8px 0 0; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; font-weight: 600; color: var(--text-muted); }
.field input, .field textarea { font-size: 14px; font-weight: 400; }
textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  resize: vertical;
}
textarea:focus { outline: none; border-color: var(--primary); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

.content-surface { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.surface-toolbar {
  padding: 12px 16px;
  min-height: 53px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
}
.surface-title { font-weight: 600; white-space: nowrap; }
.toolbar-description { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.surface-toolbar button.active { color: var(--primary); }
.live-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}
.live-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-muted); }
.live-indicator.live { color: var(--success); }
.live-indicator.live .live-dot { background: var(--success); box-shadow: 0 0 6px var(--success); }
.surface-body { flex: 1; min-height: 0; display: flex; }

/* Messages */
.chat-view { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.list-note { text-align: center; font-size: 12px; color: var(--text-muted); padding: 4px 0; }
.message { display: flex; align-items: flex-start; gap: 12px; }
.message.pending { opacity: 0.6; }
.message-main { flex: 1; min-width: 0; }
.message-avatar {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.message-avatar:hover:not(:disabled) { background: var(--primary-dark); }
.message-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; }
.message-author { font-weight: 600; cursor: pointer; border-radius: 3px; }
.message-author:hover, .message-author.menu-open { text-decoration: underline; }
.message-author.own { color: var(--primary-light); }
.message-time { font-size: 12px; color: var(--text-muted); }
.message-failed { font-size: 12px; color: var(--danger); }
.message-body { white-space: pre-wrap; word-break: break-word; }
.message-form {
  padding: 4px 16px 16px;
  display: flex;
  gap: 8px;
}
.message-form input { flex: 1; border-radius: 12px; background: var(--surface-alt); }
.message-form button { border-radius: 8px; padding: 6px 16px; font-weight: 600; }

/* Members */
.member-list {
  width: 256px;
  flex-shrink: 0;
  overflow-y: auto;
  padding: 4px 0;
  background: var(--surface-alt);
  border-left: 1px solid var(--border);
}
.member {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 6px 12px;
  text-align: left;
  background: none;
  border: none;
  border-radius: 0;
}
.member:hover:not(:disabled), .member.menu-open { background: var(--surface); }
.member .role-badge { font-size: 9px; padding: 1px 5px; flex-shrink: 0; }
.member.offline { opacity: 0.5; }
.member-avatar {
  position: relative;
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.status-dot {
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 2px solid var(--surface-alt);
  background: var(--text-muted);
}
.status-dot.online { background: var(--success); }
.member-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty-surface {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}

.role-badge {
  display: inline-block;
  vertical-align: middle;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.03em;
  white-space: nowrap;
  color: #fff;
}
.role-badge.server-owner { background: var(--owner); }
.role-badge.server-admin { background: var(--admin); }
.role-badge.server-mod { background: var(--mod); }

/* --- Admin Panel (full page) --- */
.admin { height: 100%; display: flex; flex-direction: column; background: var(--surface); }
.admin-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
}
.admin-header h1 { font-size: 20px; }
.tabs { display: flex; padding: 0 24px; border-bottom: 1px solid var(--border); }
.tab {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  padding: 8px 16px;
  color: var(--text-muted);
}
.tab:hover:not(:disabled) { background: transparent; color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.admin-body { flex: 1; overflow-y: auto; padding: 24px; }
.admin-body .error-banner { margin-bottom: 16px; }

.stat-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; max-width: 640px; }
.stat-card { background: var(--surface-alt); border: 1px solid var(--border); border-radius: 6px; padding: 16px; }
.stat-label { font-size: 12px; text-transform: uppercase; color: var(--text-muted); }
.stat-value { font-size: 30px; font-weight: 700; margin-top: 4px; }

.toolbar { display: flex; align-items: center; gap: 16px; margin-bottom: 14px; }
label.inline { display: flex; align-items: center; gap: 6px; color: var(--text-muted); }

.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 8px;
  border-bottom: 1px solid var(--border);
}
.data-table th.right { text-align: right; }
.data-table td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.actions { white-space: nowrap; }
.actions > * { margin-left: 6px; }
.message-cell { max-width: 480px; word-break: break-word; }

/* "Alice is typing…" line between the messages and the message box */
.typing-indicator {
  height: 26px;
  border-top: 1px solid var(--border);
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  white-space: nowrap;
}
.typing-dots { display: inline-flex; gap: 3px; }
.typing-dots span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typing-bounce 1.2s infinite ease-in-out;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-3px); opacity: 1; }
}

/* --- Voice channels --- */
.voice-members { padding: 0 0 4px 30px; }
.voice-member { display: flex; align-items: center; gap: 6px; padding: 3px 12px 3px 4px; font-size: 13px; color: var(--text-muted); }
.voice-member-avatar {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.sidebar-item.in-call .item-name { color: var(--success); }

.voice-connected {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
}
.voice-connected-text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  background: none;
  border: none;
  padding: 0;
  text-align: left;
}
.voice-connected-text:hover:not(:disabled) { background: none; }
.voice-connected-title { color: var(--success); font-weight: 600; font-size: 13px; }
.hang-up { color: var(--danger); font-size: 16px; }

.voice-body { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 16px; gap: 12px; }
.voice-error { flex-shrink: 0; }
.join-card {
  margin: auto;
  width: 100%;
  max-width: 420px;
  padding: 32px;
  border-radius: 12px;
  background: var(--surface-alt);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
}
.join-card p { margin: 0; }
.join-icon { font-size: 40px; }
.join-members { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; min-height: 32px; }
.join-member {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.join-button { padding: 10px 28px; font-weight: 600; border-radius: 8px; }

.call-stage { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 12px; }
.video-grid {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
  align-content: start;
}
.video-tile {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: 10px;
  overflow: hidden;
  background: var(--surface-alt);
  display: grid;
  place-items: center;
}
.video-tile video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
.hidden { display: none !important; }
.tile-avatar {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 30px;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.tile-name {
  position: absolute;
  left: 8px;
  bottom: 8px;
  padding: 2px 8px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 12px;
}
.call-controls { display: flex; justify-content: center; gap: 10px; flex-shrink: 0; }
.call-button { padding: 8px 16px; border-radius: 20px; font-weight: 600; }
.call-button.off { background: var(--surface); color: var(--text-muted); }
.call-button.leave { background: var(--danger); border-color: var(--danger); color: #fff; }
.call-button.leave:hover:not(:disabled) { background: #dc2626; }

.device-picker { max-width: 720px; }
.device-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.device-fields { display: flex; flex-direction: column; gap: 10px; }
.device-fields select { width: 100%; padding: 8px 10px; font-size: 13px; }
.mic-meter { height: 8px; margin-top: -4px; border-radius: 4px; background: var(--surface); overflow: hidden; }
.mic-meter-fill { height: 100%; width: 0; background: var(--success); transition: width 80ms linear; }
.test-sound { align-self: flex-start; margin-top: -4px; }
.device-preview {
  position: relative;
  aspect-ratio: 16 / 9;
  align-self: start;
  border-radius: 10px;
  overflow: hidden;
  background: #000;
  display: grid;
  place-items: center;
}
.device-preview video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); }
.preview-empty { color: #aaa; font-size: 13px; }

.warning-banner {
  padding: 8px 10px;
  border-radius: 6px;
  background: rgba(234, 179, 8, 0.15);
  color: #facc15;
  font-size: 13px;
}
[data-theme="light"] .warning-banner { color: #854d0e; }

/* Login: Remember Me */
.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  align-self: flex-start;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}
.remember-me input { width: auto; margin: 0; accent-color: var(--primary); cursor: pointer; }

/* Settings page */
.settings { height: 100%; display: flex; flex-direction: column; background: var(--surface); }
.settings-main { flex: 1; display: flex; min-height: 0; }
.settings-nav {
  width: 220px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 16px 10px;
  border-right: 1px solid var(--border);
  background: var(--surface-alt);
}
.settings-link {
  text-align: left;
  padding: 8px 12px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font-size: 14px;
}
.settings-link:hover:not(:disabled) { background: rgba(99, 102, 241, 0.15); color: var(--text); }
.settings-link.active { background: rgba(99, 102, 241, 0.15); color: var(--text); font-weight: 600; }
.settings-link:disabled { opacity: 0.45; cursor: not-allowed; }
.settings-body { flex: 1; overflow-y: auto; padding: 24px 32px; }
.settings-body h2 { font-size: 18px; margin-bottom: 12px; }
.settings-tabs { padding: 0; margin-bottom: 20px; }
.settings-fields { display: flex; flex-direction: column; gap: 12px; max-width: 480px; }
.settings-fields select { width: 100%; padding: 8px 10px; font-size: 13px; }
.settings-fields > .ghost.small { align-self: flex-start; }
.mic-test { display: flex; align-items: center; gap: 12px; }
.mic-test .mic-meter { flex: 1; margin: 0; }
.settings-preview { width: 100%; }
