:root {
  --bg-app: #0b0e14;
  --bg-sidebar: #111520;
  --bg-surface: #171c2b;
  --bg-elevated: #1f263a;
  --bg-hover: rgba(255, 255, 255, 0.055);
  --bg-active: rgba(99, 102, 241, 0.18);
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.15);
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --accent-soft: rgba(99, 102, 241, 0.15);
  --e2ee-green: #10b981;
  --e2ee-soft: rgba(16, 185, 129, 0.14);
  --danger: #ef4444;
  --warning: #f59e0b;
  --online: #22c55e;
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.55);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --font-sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

[data-theme="light"] {
  --bg-app: #ffffff;
  --bg-sidebar: #f4f6fb;
  --bg-surface: #eef2f9;
  --bg-elevated: #ffffff;
  --bg-hover: rgba(15, 23, 42, 0.05);
  --bg-active: rgba(79, 70, 229, 0.12);
  --border: rgba(15, 23, 42, 0.09);
  --border-strong: rgba(15, 23, 42, 0.18);
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #64748b;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: rgba(79, 70, 229, 0.1);
  --e2ee-green: #059669;
  --e2ee-soft: rgba(5, 150, 105, 0.11);
  --shadow-lg: 0 20px 45px rgba(15, 23, 42, 0.16);
}

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

html, body {
  height: 100%;
  font-family: var(--font-sans);
  background: var(--bg-app);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

input, textarea {
  font-family: inherit;
  color: var(--text-primary);
}

/* Auth Screen */
.auth-screen {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background:
    radial-gradient(circle at 20% 20%, rgba(99, 102, 241, 0.16), transparent 45%),
    radial-gradient(circle at 80% 80%, rgba(16, 185, 129, 0.12), transparent 45%),
    var(--bg-app);
}

.auth-card {
  width: 100%;
  max-width: 430px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  padding: 32px;
  box-shadow: var(--shadow-lg);
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}

.auth-brand img {
  width: 42px;
  height: 42px;
}

.auth-brand h1 {
  font-size: 21px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.auth-brand p {
  font-size: 12.5px;
  color: var(--text-secondary);
}

.e2ee-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--e2ee-soft);
  color: var(--e2ee-green);
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 20px;
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--bg-surface);
  padding: 4px;
  border-radius: var(--radius-md);
  margin-bottom: 20px;
}

.auth-tab {
  padding: 8px 12px;
  border-radius: 7px;
  font-weight: 600;
  font-size: 13.5px;
  color: var(--text-secondary);
  transition: all 0.15s ease;
}

.auth-tab.active {
  background: var(--accent);
  color: #fff;
}

.form-field {
  margin-bottom: 15px;
}

.form-field label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-field input,
.form-field textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--bg-surface);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
}

.form-field input:focus,
.form-field textarea:focus {
  border-color: var(--accent);
}

.form-hint {
  margin-top: 5px;
  font-size: 11.5px;
  color: var(--text-muted);
}

.error-box {
  padding: 10px 12px;
  border-radius: var(--radius-md);
  background: rgba(239, 68, 68, 0.14);
  border: 1px solid rgba(239, 68, 68, 0.35);
  color: #f87171;
  font-size: 13px;
  margin-bottom: 14px;
}

.btn-primary {
  width: 100%;
  padding: 11px 16px;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  transition: background 0.15s;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  padding: 8px 14px;
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  font-weight: 600;
  font-size: 13px;
  color: var(--text-primary);
}

.btn-secondary:hover {
  background: var(--bg-hover);
}

/* Main App Layout */
.app-shell {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

/* Left Sidebar */
.sidebar {
  width: 272px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 30;
}

.sidebar-header {
  height: 58px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.workspace-title {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 15.5px;
}

.workspace-title img {
  width: 26px;
  height: 26px;
}

.ws-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warning);
  display: inline-block;
}

.ws-dot.connected {
  background: var(--online);
}

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.sidebar-search {
  padding: 12px 12px 6px;
}

.search-trigger {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 11px;
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 13px;
}

.search-trigger:hover {
  border-color: var(--border-strong);
  color: var(--text-primary);
}

.kbd-hint {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
}

.vault-banner {
  margin: 8px 12px 0;
  padding: 9px 11px;
  border-radius: var(--radius-md);
  background: rgba(245, 158, 11, 0.15);
  border: 1px solid rgba(245, 158, 11, 0.35);
  color: var(--warning);
  font-size: 12px;
  font-weight: 600;
  text-align: left;
}

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 10px 8px;
}

.nav-section {
  margin-bottom: 18px;
}

.nav-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 8px 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.nav-section-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.mini-btn {
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
}

.mini-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.room-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13.5px;
  text-align: left;
  transition: background 0.12s, color 0.12s;
  margin-bottom: 2px;
}

.room-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

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

.room-item.unread {
  color: var(--text-primary);
  font-weight: 700;
}

.room-prefix {
  width: 18px;
  text-align: center;
  font-size: 14px;
  flex-shrink: 0;
  opacity: 0.85;
}

.room-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unread-badge {
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
  flex-shrink: 0;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  color: #fff;
  flex-shrink: 0;
  position: relative;
  user-select: none;
}

.avatar.sm {
  width: 24px;
  height: 24px;
  border-radius: 7px;
  font-size: 11px;
}

.presence-dot {
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
  border: 2px solid var(--bg-sidebar);
}

.presence-dot.online {
  background: var(--online);
}

.sidebar-footer {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-sidebar);
}

.self-meta {
  flex: 1;
  min-width: 0;
}

.self-name {
  font-weight: 600;
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.self-handle {
  font-size: 11.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Center Chat Area */
.chat-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-app);
}

.chat-header {
  height: 58px;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.mobile-only {
  display: none;
}

.room-title {
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
}

.room-e2ee-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: var(--e2ee-soft);
  color: var(--e2ee-green);
  white-space: nowrap;
}

.room-e2ee-badge.public {
  background: var(--bg-surface);
  color: var(--text-secondary);
}

.room-topic {
  color: var(--text-muted);
  font-size: 12.5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 11px;
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary);
}

.pill-btn:hover {
  color: var(--text-primary);
  border-color: var(--border-strong);
}

/* Message Stream */
.message-viewport {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px 8px;
  display: flex;
  flex-direction: column;
}

.load-older-wrap {
  text-align: center;
  margin-bottom: 14px;
}

.date-divider {
  display: flex;
  align-items: center;
  margin: 16px 0 12px;
  color: var(--text-muted);
  font-size: 11.5px;
  font-weight: 600;
}

.date-divider::before,
.date-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.date-divider span {
  padding: 0 12px;
}

.empty-state {
  margin: auto;
  text-align: center;
  max-width: 380px;
  color: var(--text-secondary);
  padding: 32px 16px;
}

.empty-state h3 {
  color: var(--text-primary);
  font-size: 17px;
  margin-bottom: 6px;
}

.msg-row {
  display: flex;
  gap: 12px;
  padding: 8px 10px;
  border-radius: var(--radius-md);
  position: relative;
  transition: background 0.12s;
}

.msg-row:hover {
  background: var(--bg-hover);
}

.msg-row.highlight {
  background: var(--accent-soft);
}

.msg-body {
  flex: 1;
  min-width: 0;
}

.msg-meta {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 2px;
  flex-wrap: wrap;
}

.msg-author {
  font-weight: 700;
  font-size: 14px;
  color: var(--text-primary);
}

.msg-handle {
  font-size: 12px;
  color: var(--text-muted);
}

.msg-time {
  font-size: 11.5px;
  color: var(--text-muted);
}

.msg-edited {
  font-size: 11px;
  color: var(--text-muted);
  font-style: italic;
}

.msg-lock {
  font-size: 11px;
  color: var(--e2ee-green);
}

.reply-quote {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 9px;
  margin-bottom: 5px;
  border-left: 3px solid var(--accent);
  background: var(--bg-surface);
  border-radius: 4px;
  font-size: 12.5px;
  color: var(--text-secondary);
  cursor: pointer;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.reply-quote strong {
  color: var(--text-primary);
}

.msg-text {
  font-size: 14.2px;
  line-height: 1.55;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-text.deleted {
  color: var(--text-muted);
  font-style: italic;
}

.msg-text code {
  font-family: var(--font-mono);
  font-size: 12.5px;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
}

.msg-text pre {
  margin: 6px 0;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 12.5px;
}

.msg-text a {
  color: var(--accent);
  text-decoration: underline;
}

/* Attachments */
.msg-attachment {
  margin-top: 8px;
}

.att-image {
  max-width: min(380px, 100%);
  max-height: 280px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  cursor: zoom-in;
  display: block;
}

.att-card {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 9px 13px;
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  max-width: 360px;
}

.att-card-info {
  min-width: 0;
}

.att-card-name {
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.att-card-size {
  font-size: 11.5px;
  color: var(--text-muted);
}

/* Reactions & Hover Actions */
.msg-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 6px;
}

.reaction-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

.reaction-chip.mine {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--text-primary);
}

.msg-actions {
  position: absolute;
  top: -12px;
  right: 12px;
  display: none;
  align-items: center;
  gap: 2px;
  padding: 3px 5px;
  border-radius: var(--radius-md);
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.28);
  z-index: 5;
}

.msg-row:hover .msg-actions {
  display: inline-flex;
}

.action-btn {
  padding: 4px 6px;
  border-radius: 5px;
  font-size: 13px;
  color: var(--text-secondary);
}

.action-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.seen-receipt {
  font-size: 11px;
  color: var(--text-muted);
  text-align: right;
  padding: 2px 12px 4px;
}

/* Composer */
.typing-bar {
  min-height: 20px;
  padding: 0 22px;
  font-size: 12px;
  color: var(--text-secondary);
  font-style: italic;
}

.composer-wrap {
  padding: 6px 18px 16px;
  position: relative;
}

.composer-box {
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  overflow: visible;
  transition: border-color 0.15s;
}

.composer-box:focus-within {
  border-color: var(--accent);
}

.composer-banner,
.pending-attachment {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 12.5px;
  background: rgba(99, 102, 241, 0.08);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 10px;
}

.composer-row textarea {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  max-height: 160px;
  min-height: 24px;
  padding: 4px 4px;
  font-size: 14px;
  line-height: 1.45;
  outline: none;
}

.send-btn {
  padding: 7px 14px;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 13px;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.emoji-popover {
  position: absolute;
  bottom: 68px;
  left: 52px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  box-shadow: var(--shadow-lg);
  z-index: 40;
}

.emoji-pick {
  font-size: 18px;
  padding: 5px;
  border-radius: 6px;
}

.emoji-pick:hover {
  background: var(--bg-hover);
}

/* Right Details Drawer */
.details-drawer {
  width: 310px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.drawer-header {
  height: 58px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 700;
}

.drawer-content {
  padding: 16px;
}

.security-card {
  padding: 13px;
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  margin-bottom: 16px;
}

.security-card h4 {
  font-size: 13.5px;
  margin-bottom: 5px;
}

.security-card p {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.45;
}

.member-card {
  padding: 9px 10px;
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  margin-bottom: 8px;
}

.member-card-top {
  display: flex;
  align-items: center;
  gap: 9px;
}

.fingerprint-code {
  margin-top: 7px;
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--bg-app);
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--e2ee-green);
  word-break: break-all;
  line-height: 1.4;
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}

.modal-card {
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  background: var(--bg-sidebar);
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  padding: 22px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.modal-header h3 {
  font-size: 17px;
  font-weight: 700;
}

.modal-scroll {
  overflow-y: auto;
  max-height: 360px;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-bottom: 12px;
  font-size: 13.5px;
  cursor: pointer;
}

.list-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-surface);
  margin-bottom: 8px;
}

.search-hit {
  padding: 10px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-surface);
  margin-bottom: 8px;
  cursor: pointer;
}

.search-hit:hover {
  border-color: var(--accent);
}

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 150;
  cursor: zoom-out;
}

.lightbox img {
  max-width: 94vw;
  max-height: 90vh;
  border-radius: 10px;
}

.sidebar-backdrop {
  display: none;
}

@media (max-width: 900px) {
  .details-drawer {
    position: fixed;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 50;
    box-shadow: var(--shadow-lg);
  }
}

@media (max-width: 720px) {
  .mobile-only {
    display: inline-flex;
  }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .sidebar-backdrop.open {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 25;
  }
  .room-topic {
    display: none;
  }
}
