:root {
  --bg-outer: #eef2f6;
  --bg-surface: #ffffff;
  --bg-chat: #f8fafc;
  --bg-elevated: #f1f5f9;
  --bg-bubble-other: #ffffff;
  --bg-bubble-self: #2563eb;
  --text-primary: #0f172a;
  --text-secondary: #64748b;
  --text-self: #ffffff;
  --border-color: #e2e8f0;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --warning: #d97706;
  --shadow-shell: 0 20px 50px rgba(15, 23, 42, 0.08);
  --shadow-bubble: 0 1px 2px rgba(15, 23, 42, 0.06);
  --radius-lg: 18px;
  --radius-md: 14px;
  --radius-sm: 10px;
}

:root[data-theme="dark"] {
  --bg-outer: #020617;
  --bg-surface: #0f172a;
  --bg-chat: #0b1120;
  --bg-elevated: #1e293b;
  --bg-bubble-other: #1e293b;
  --bg-bubble-self: #2563eb;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-self: #ffffff;
  --border-color: #1e293b;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #f87171;
  --success: #4ade80;
  --warning: #fbbf24;
  --shadow-shell: 0 24px 60px rgba(0, 0, 0, 0.45);
  --shadow-bubble: 0 1px 3px rgba(0, 0, 0, 0.28);
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-outer);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.hidden { display: none !important; }

.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100dvh;
  background: var(--bg-surface);
  position: relative;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
  z-index: 10;
}

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

.brand-badge {
  width: 38px; height: 38px; border-radius: 12px;
  background: linear-gradient(135deg, #3b82f6, #6366f1);
  display: flex; align-items: center; justify-content: center;
  font-size: 19px; flex-shrink: 0;
}

.header-titles { min-width: 0; }
.header-titles h1 {
  margin: 0; font-size: 16px; font-weight: 700; line-height: 1.25;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.status-row {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-secondary); margin-top: 2px;
}

.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warning); flex-shrink: 0; }
.status-dot.connected { background: var(--success); }
.status-dot.disconnected { background: var(--danger); }
.divider { opacity: 0.5; }

.header-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.identity-pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 10px 5px 6px; border-radius: 999px;
  border: 1px solid var(--border-color); background: var(--bg-elevated);
  color: var(--text-primary); font-size: 13px; cursor: pointer;
  max-width: 160px; transition: transform 0.15s ease, border-color 0.15s ease;
}
.identity-pill:hover { border-color: var(--accent); }
.identity-pill:active { transform: scale(0.97); }

.identity-avatar {
  width: 24px; height: 24px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 14px; flex-shrink: 0;
}
.identity-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.shuffle-icon { font-size: 13px; color: var(--text-secondary); flex-shrink: 0; }

.icon-btn {
  width: 36px; height: 36px; border-radius: 10px;
  border: 1px solid var(--border-color); background: var(--bg-elevated);
  color: var(--text-primary); display: inline-flex; align-items: center;
  justify-content: center; font-size: 16px; cursor: pointer;
  transition: border-color 0.15s ease;
}
.icon-btn:hover { border-color: var(--accent); }

.chat-main {
  flex: 1; position: relative; min-height: 0;
  background: var(--bg-chat); display: flex; flex-direction: column;
}

.chat-messages {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 16px 12px 20px; display: flex; flex-direction: column; gap: 14px;
  -webkit-overflow-scrolling: touch; scroll-behavior: smooth;
}

.empty-state { margin: auto; text-align: center; padding: 32px 20px; max-width: 320px; color: var(--text-secondary); }
.empty-icon { font-size: 40px; margin-bottom: 10px; }
.empty-title { margin: 0 0 6px; font-size: 16px; font-weight: 600; color: var(--text-primary); }
.empty-sub { margin: 0; font-size: 13px; line-height: 1.5; }

.message-row {
  display: flex; align-items: flex-end; gap: 8px;
  max-width: 88%; animation: fadeSlideIn 0.18s ease-out;
}
.message-row.self { align-self: flex-end; flex-direction: row-reverse; }
.message-row.other { align-self: flex-start; }

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

.msg-avatar {
  width: 34px; height: 34px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 17px; flex-shrink: 0; color: #ffffff; user-select: none;
}

.msg-body { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.message-row.self .msg-body { align-items: flex-end; }

.msg-meta {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-secondary); padding: 0 4px;
}
.message-row.self .msg-meta { flex-direction: row-reverse; }
.msg-nickname { font-weight: 500; }
.msg-time { font-size: 11px; opacity: 0.8; }

.msg-bubble {
  padding: 10px 13px; border-radius: var(--radius-lg);
  box-shadow: var(--shadow-bubble); word-break: break-word; overflow-wrap: anywhere;
  line-height: 1.5; font-size: 15px; display: flex; flex-direction: column; gap: 8px;
}
.message-row.other .msg-bubble {
  background: var(--bg-bubble-other); color: var(--text-primary);
  border-bottom-left-radius: 5px; border: 1px solid var(--border-color);
}
.message-row.self .msg-bubble {
  background: var(--bg-bubble-self); color: var(--text-self);
  border-bottom-right-radius: 5px;
}
.msg-bubble.image-only { padding: 5px; }
.msg-text { white-space: pre-wrap; margin: 0; }

.msg-image {
  display: block; max-width: min(260px, 64vw); max-height: 320px;
  width: auto; height: auto; border-radius: 12px; cursor: zoom-in;
  object-fit: cover; background: rgba(148, 163, 184, 0.15);
}

.scroll-bottom-btn {
  position: absolute; right: 16px; bottom: 14px;
  padding: 8px 14px; border-radius: 999px;
  border: 1px solid var(--border-color); background: var(--bg-surface);
  color: var(--text-primary); font-size: 13px; font-weight: 500;
  box-shadow: 0 6px 18px rgba(15, 23, 42, 0.14); cursor: pointer; z-index: 5;
}
.scroll-bottom-btn:hover { border-color: var(--accent); }

.drop-overlay {
  position: absolute; inset: 0; background: rgba(37, 99, 235, 0.14);
  backdrop-filter: blur(4px); display: flex; align-items: center;
  justify-content: center; z-index: 20; pointer-events: none;
}
.drop-card {
  padding: 24px 36px; border-radius: 18px; border: 2px dashed var(--accent);
  background: var(--bg-surface); text-align: center; font-weight: 600;
  box-shadow: var(--shadow-shell);
}
.drop-icon { font-size: 36px; margin-bottom: 8px; }

.chat-footer {
  background: var(--bg-surface); border-top: 1px solid var(--border-color);
  padding: 8px 12px calc(10px + env(safe-area-inset-bottom)); flex-shrink: 0;
}

.pending-image-bar {
  display: flex; align-items: center; gap: 12px; padding: 8px 10px;
  margin-bottom: 8px; border-radius: var(--radius-md);
  background: var(--bg-elevated); border: 1px solid var(--border-color);
}
.pending-thumb-wrap { position: relative; width: 56px; height: 56px; flex-shrink: 0; }
.pending-thumb-wrap img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; }
.remove-pending-btn {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px;
  border-radius: 50%; border: none; background: rgba(15, 23, 42, 0.85);
  color: #ffffff; font-size: 12px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.pending-meta { display: flex; flex-direction: column; gap: 2px; font-size: 13px; min-width: 0; }
.pending-tip { font-size: 12px; color: var(--text-secondary); }

.toast-banner {
  padding: 7px 12px; margin-bottom: 8px; border-radius: var(--radius-sm);
  background: rgba(220, 38, 38, 0.12); color: var(--danger);
  font-size: 13px; font-weight: 500;
}

.composer { display: flex; align-items: flex-end; gap: 8px; }

.composer-input {
  flex: 1; resize: none; border: 1px solid var(--border-color);
  background: var(--bg-elevated); color: var(--text-primary);
  border-radius: 20px; padding: 10px 14px; font-family: inherit;
  font-size: 16px; line-height: 1.4; max-height: 132px;
  overflow-y: auto; outline: none; transition: border-color 0.15s ease;
}
.composer-input:focus { border-color: var(--accent); }

.composer-btn {
  width: 42px; height: 42px; border-radius: 50%; border: none;
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0;
  transition: background-color 0.15s ease, transform 0.12s ease, opacity 0.15s ease;
}
.composer-btn:active:not(:disabled) { transform: scale(0.95); }
.attach-btn { background: var(--bg-elevated); color: var(--text-primary); border: 1px solid var(--border-color); }
.attach-btn:hover { border-color: var(--accent); color: var(--accent); }
.send-btn { background: var(--accent); color: #ffffff; }
.send-btn:hover:not(:disabled) { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.lightbox {
  position: fixed; inset: 0; background: rgba(2, 6, 23, 0.88);
  backdrop-filter: blur(6px); display: flex; align-items: center;
  justify-content: center; padding: 20px; z-index: 100;
}
.lightbox img {
  max-width: 100%; max-height: 90dvh; border-radius: 12px;
  object-fit: contain; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.lightbox-close {
  position: fixed; top: calc(16px + env(safe-area-inset-top)); right: 16px;
  width: 40px; height: 40px; border-radius: 50%; border: none;
  background: rgba(255, 255, 255, 0.16); color: #ffffff; font-size: 18px; cursor: pointer;
}
.lightbox-close:hover { background: rgba(255, 255, 255, 0.28); }

@media (min-width: 640px) {
  .chat-header { padding: 12px 20px; }
  .chat-messages { padding: 20px 22px 24px; }
  .chat-footer { padding: 12px 20px calc(14px + env(safe-area-inset-bottom)); }
  .message-row { max-width: 75%; }
  .msg-image { max-width: 340px; max-height: 360px; }
  .identity-pill { max-width: 220px; }
}

@media (min-width: 1024px) {
  body { display: flex; align-items: center; justify-content: center; padding: 20px; }
  .app-shell {
    max-width: 880px; height: min(92dvh, 880px);
    border-radius: 22px; border: 1px solid var(--border-color);
    box-shadow: var(--shadow-shell);
  }
  .message-row { max-width: 68%; }
  .msg-image { max-width: 380px; max-height: 400px; }
}