:root {
  --bg: #0e0f13;
  --panel: #16181f;
  --panel-alt: #1c1f28;
  --border: #262a36;
  --text: #e7e9ee;
  --text-dim: #8b90a0;
  --accent: #6c8dff;
  --accent-dim: #3d4b8c;
  --bubble-me: #6c8dff;
  --bubble-them: #22252f;
  --radius: 16px;
  --radius-sm: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

* {
  scrollbar-color: var(--border) transparent;
  scrollbar-width: thin;
}

*::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

*::-webkit-scrollbar-track {
  background: transparent;
}

*::-webkit-scrollbar-thumb {
  background-color: var(--border);
  border-radius: 999px;
  border: 2px solid transparent;
  background-clip: padding-box;
}

*::-webkit-scrollbar-thumb:hover {
  background-color: var(--text-dim);
  background-clip: padding-box;
}

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }

.screen {
  height: 100vh;
  display: flex;
}

/* Auth screen */
#auth-screen {
  align-items: center;
  justify-content: center;
}

.auth-card {
  width: 340px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.brand {
  font-size: 22px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: -8px 0 8px;
  color: var(--text-dim);
  font-size: 13px;
}

input {
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s ease;
}

.captcha-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.captcha-frame-wrap {
  position: relative;
  flex: 1;
  height: 70px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  overflow: hidden;
  background: var(--panel-alt);
}

.captcha-frame-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.captcha-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: flex-end;
  background: rgba(22, 24, 31, 0.55);
  opacity: 0;
  transition: opacity 0.2s ease;
  pointer-events: none;
}
.captcha-loading.visible { opacity: 1; }

.captcha-progress-bar {
  height: 4px;
  width: 0%;
  background: var(--accent);
  transition: width 0.15s ease;
}

.captcha-instruction {
  margin: -4px 0 0;
}

input:focus {
  border-color: var(--accent);
}

.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, opacity 0.15s ease;
}

.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: white; }
.btn.ghost { background: transparent; color: var(--text-dim); border: 1px solid var(--border); }
.btn.small { padding: 8px 12px; font-size: 13px; }

.auth-actions {
  display: flex;
  gap: 8px;
}
.auth-actions .btn { flex: 1; }

.error {
  color: #ff6b6b;
  font-size: 13px;
  min-height: 16px;
  margin: 0;
}

/* App screen */
#app-screen { align-items: stretch; }

.sidebar {
  width: 280px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

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

.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
}
.icon-btn:hover { color: var(--text); }

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

.avatar {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
}

.avatar.small {
  width: 26px;
  height: 26px;
  font-size: 11px;
}

.my-username {
  font-weight: 600;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.list-section-title {
  padding: 10px 16px 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.conversation-item .conversation-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-item .pending-tag {
  font-size: 11px;
  color: var(--text-dim);
}

.pin-icon {
  font-size: 11px;
  margin-right: 4px;
}

.context-menu {
  position: fixed;
  z-index: 20;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px;
  min-width: 160px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
}

.context-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--text);
  font-size: 13px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.context-menu-item:hover { background: var(--panel); }
.context-menu-item.danger { color: #ff6b6b; }

.conversation-item.pinned {
  background: rgba(216, 165, 61, 0.08);
  border: 1px solid rgba(216, 165, 61, 0.25);
}
.conversation-item.pinned:hover { background: rgba(216, 165, 61, 0.14); }
.conversation-item.pinned.active { background: var(--accent-dim); border-color: transparent; }

.unread-badge {
  flex: none;
}

.accept-btn {
  flex: none;
}

.requests-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: none;
  border: none;
  color: var(--text);
  padding: 10px 16px 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  cursor: pointer;
}
.requests-toggle:hover { color: var(--accent); }

.badge {
  background: var(--accent);
  color: white;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  padding: 3px 7px;
  border-radius: 999px;
  text-transform: none;
  letter-spacing: normal;
}

.requests-list {
  max-height: 180px;
  overflow-y: auto;
}

.new-chat {
  display: flex;
  gap: 6px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.new-chat input { flex: 1; min-width: 0; }

.conversation-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  flex: 1;
}

.conversation-item {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  color: var(--text-dim);
  font-size: 14px;
  margin-bottom: 2px;
  transition: background 0.12s ease;
}
.conversation-item:hover { background: var(--panel-alt); }
.conversation-item.active { background: var(--accent-dim); color: var(--text); }

.chat-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 14px;
}

.chat-active {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.bubble-row {
  display: flex;
  flex-direction: column;
  animation: bubble-in 0.18s ease;
}
.bubble-row.me { align-items: flex-end; }
.bubble-row.them { align-items: flex-start; }

@keyframes bubble-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.bubble {
  max-width: 60%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.4;
  word-wrap: break-word;
}

.bubble-row.me .bubble {
  background: var(--bubble-me);
  color: white;
  border-bottom-right-radius: 4px;
}

.bubble-row.them .bubble {
  background: var(--bubble-them);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.bubble-meta {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.bubble-wrap {
  position: relative;
  max-width: 100%;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.bubble-row.me .bubble-wrap { align-items: flex-end; }
.bubble-row.them .bubble-wrap { align-items: flex-start; }
.bubble-wrap .bubble { max-width: 100%; }

/* The edit UI needs more room than a typical bubble — let it grow instead
   of being clipped by the normal 60%-of-column bubble width. Both the wrap
   AND its parent row need the wider cap: the row is what actually carries
   the percentage-based width (see .message-avatar-row below), so relaxing
   only the wrap would still get clipped by the row around it. */
.bubble-wrap.editing { max-width: 100%; }
.bubble-wrap.editing .bubble { max-width: 100%; width: 100%; }
.message-avatar-row.editing { max-width: min(360px, 80vw); }

.bubble-actions {
  position: absolute;
  top: -14px;
  display: none;
  gap: 4px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
}
.bubble-row.me .bubble-actions { right: 4px; }
.bubble-row.them .bubble-actions { left: 4px; }
.bubble-wrap:hover .bubble-actions,
.bubble-wrap.actions-open .bubble-actions { display: flex; }
.bubble-wrap.actions-open .bubble { outline: 1px solid var(--accent); }

.bubble-action-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 12px;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
}
.bubble-action-btn:hover { color: var(--text); background: var(--panel); }

.edited-tag {
  font-style: italic;
}

.attachment-image {
  max-width: 260px;
  max-height: 260px;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 12px;
  display: block;
  cursor: pointer;
}
.attachment-image.extreme-ratio {
  width: 260px;
  height: 140px;
  object-fit: cover;
}

.attachment-file {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  text-decoration: none;
  color: inherit;
  font-size: 13px;
  max-width: 220px;
  min-width: 0;
}
.attachment-file .file-icon { flex: none; font-size: 20px; }
.attachment-file .file-info {
  min-width: 0;
  overflow: hidden;
}
.attachment-file .file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachment-file .file-meta { color: var(--text-dim); font-size: 11px; }
.bubble-row.me .attachment-file { background: rgba(0, 0, 0, 0.18); }
.bubble-row.me .attachment-file .file-meta { color: rgba(255, 255, 255, 0.75); }

.profile-btn {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-align: left;
  color: inherit;
  font: inherit;
}

.avatar.large {
  width: 64px;
  height: 64px;
  font-size: 22px;
}
.avatar img, .avatar-img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.modal {
  width: 360px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}

.modal-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.profile-avatar-row {
  display: flex;
  align-items: center;
  gap: 16px;
}

.hint {
  color: var(--text-dim);
  font-size: 12px;
  margin: 6px 0 0;
}

.modal-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0;
}

.field-label {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 4px;
  display: block;
}

#password-form input,
#username-form input {
  width: 100%;
  margin-bottom: 10px;
}

.message-form {
  display: flex;
  gap: 10px;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
}
.message-form input { flex: 1; }

.back-btn { display: none; }

@media (max-width: 720px) {
  #app-screen { position: relative; overflow: hidden; }
  .sidebar {
    width: 100%;
    position: absolute;
    inset: 0;
    z-index: 2;
    transition: transform 0.2s ease;
  }
  .chat-pane { width: 100%; }
  .back-btn { display: inline-block; font-size: 22px; margin-right: 4px; }

  #app-screen.chat-open .sidebar {
    transform: translateX(-100%);
    pointer-events: none;
  }

  .auth-card { width: calc(100vw - 32px); max-width: 340px; }
  .modal { width: calc(100vw - 32px); max-width: 360px; }
  .message-avatar-row { max-width: 90%; }
}

/* Chat message avatar. The percentage max-width lives HERE, on the row that's
   a direct flex child of .bubble-row (which has a real, definite width) —
   not on .bubble-wrap, which is one level deeper and would resolve its own
   percentage against this row's shrink-to-fit width, collapsing to nothing. */
.message-avatar-row {
  display: flex;
  gap: 8px;
  max-width: 60%;
}
.bubble-row.me .message-avatar-row { flex-direction: row-reverse; }
.message-avatar {
  flex: none;
  align-self: flex-end;
}

/* Attachment grid for multi-file messages */
.attachment-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}
.attachment-grid.single-item { grid-template-columns: 1fr; }
.attachment-grid .attachment-image { width: 100%; max-width: none; height: 120px; object-fit: cover; }
.attachment-grid.single-item .attachment-image { width: 100%; height: auto; max-height: 260px; object-fit: contain; }
/* An extremely elongated single image (banner-like) would be unreadable at
   natural aspect ratio, so crop it to a fixed box instead — matches the
   deliberate crop already used for the multi-image grid. */
.attachment-grid.single-item .attachment-image.extreme-ratio {
  height: 160px;
  max-height: 160px;
  object-fit: cover;
}

.staged-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 24px 0;
}

.staged-chip {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 12px;
  max-width: 160px;
}
.staged-chip img.staged-thumb {
  width: 28px;
  height: 28px;
  object-fit: cover;
  border-radius: 6px;
}
.staged-chip .staged-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.staged-chip .staged-remove {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  padding: 0;
}
.staged-chip .staged-remove:hover { color: #ff6b6b; }

/* Lightbox */
.lightbox-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  max-width: 90vw;
  max-height: 90vh;
}
.lightbox-img {
  max-width: 90vw;
  max-height: 78vh;
  border-radius: var(--radius-sm);
  object-fit: contain;
}
.lightbox-actions {
  display: flex;
  gap: 8px;
}

/* Profile username copy */
.username-copy-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
}
.username-copy-row:hover { border-color: var(--accent); }
.username-copy-row .copy-icon { color: var(--text-dim); font-size: 14px; }

/* Edit-in-place: mirrors the send bar's layout, with a distinct accent so
   it reads clearly as "editing" rather than "sending". */
.edit-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  min-width: 220px;
}
.edit-attachment-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.edit-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid #d8a53d;
  border-radius: var(--radius-sm);
  padding: 6px 8px;
}
.edit-row input[type="text"] {
  flex: 1;
  min-width: 0;
  background: transparent;
  border: none;
  padding: 4px 2px;
}
.edit-row input[type="text"]:focus { border: none; }

.btn.save {
  background: #d8a53d;
  color: #2b1c00;
  padding: 8px 14px;
  font-size: 13px;
}

.messages.drag-active {
  outline: 2px dashed var(--accent);
  outline-offset: -8px;
  background: rgba(108, 141, 255, 0.06);
}

.date-separator {
  align-self: center;
  color: var(--text-dim);
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 11px;
  font-weight: 600;
  margin: 4px 0;
}

.notice-card {
  width: 320px;
  max-width: calc(100vw - 32px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: center;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.notice-message {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
}
