/* ===== 基础重置 ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-tertiary: #0f3460;
  --text-primary: #e0e0e0;
  --text-secondary: #8899aa;
  --text-muted: #5a6a7a;
  --accent: #e94560;
  --accent-hover: #d63850;
  --border: #0f3460;
  --bubble-self: #0f3460;
  --bubble-other: #1e2a48;
  --success: #2ed573;
  --danger: #e94560;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans SC", sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.5;
}

/* ===== 布局 ===== */
#appContainer {
  height: 100%;
  display: flex;
  flex-direction: column;
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
  position: relative;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  min-width: 0;
}

/* ===== 头部 ===== */
#header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.site-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
}

.online-count {
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--bg-tertiary);
  padding: 2px 8px;
  border-radius: 10px;
}

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

.nickname-display {
  font-size: 14px;
  color: var(--text-secondary);
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===== 消息容器 ===== */
#messageContainer {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
}

#messageContainer::-webkit-scrollbar {
  width: 4px;
}

#messageContainer::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary);
  border-radius: 2px;
}

#messageList {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: auto;
}

/* ===== 消息气泡 ===== */
.message {
  display: flex;
  flex-direction: column;
  max-width: 85%;
  animation: fadeIn 0.2s ease;
  position: relative;
}

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

.message-self {
  align-self: flex-end;
}

.message-other {
  align-self: flex-start;
}

.message-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 2px;
  padding: 0 4px;
}

.message-self .message-meta {
  text-align: right;
}

.message-bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  word-break: break-word;
  white-space: pre-wrap;
}

.message-self .message-bubble {
  background: var(--bubble-self);
  border-bottom-right-radius: 4px;
}

.message-other .message-bubble {
  background: var(--bubble-other);
  border-bottom-left-radius: 4px;
}

/* ===== 置顶按钮 ===== */
.btn-pin {
  position: absolute;
  top: -4px;
  right: -4px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 26px;
  height: 26px;
  font-size: 12px;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
  transition: background 0.15s;
  z-index: 2;
}

.message:hover .btn-pin {
  display: flex;
}

.btn-pin:hover {
  background: var(--accent);
}

.message-self .btn-pin {
  right: auto;
  left: -4px;
}

/* ===== 乐观发送（临时消息）===== */
.message-optimistic {
  opacity: 0.7;
}

.message-optimistic .message-bubble::after {
  content: '发送中...';
  display: block;
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ===== 发送失败 ===== */
.message-failed {
  opacity: 0.5;
}

.message-failed .message-bubble {
  border: 1px solid var(--danger);
}

.btn-retry {
  margin-top: 4px;
  padding: 3px 10px;
  background: var(--danger);
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
  align-self: flex-start;
}

.btn-retry:hover {
  opacity: 0.85;
}

.message-self .btn-retry {
  align-self: flex-end;
}

.message-text {
  font-size: 14px;
  line-height: 1.6;
}

/* ===== 文件消息 ===== */
.file-message {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 200px;
}

.file-icon {
  font-size: 28px;
  flex-shrink: 0;
}

.file-info {
  flex: 1;
  min-width: 0;
}

.file-name {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-size {
  font-size: 11px;
  color: var(--text-secondary);
}

.file-download {
  flex-shrink: 0;
  padding: 6px 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 12px;
  transition: background 0.15s;
}

.file-download:hover {
  background: var(--accent-hover);
}

/* ===== 图片预览 ===== */
.image-preview {
  max-width: 280px;
  max-height: 320px;
  border-radius: var(--radius-sm);
  cursor: zoom-in;
  display: block;
}

/* ===== 视频/音频 ===== */
.video-player {
  max-width: 100%;
  max-height: 360px;
  border-radius: var(--radius-sm);
}

.audio-player {
  width: 260px;
  height: 40px;
}

/* ===== 输入区域 ===== */
#inputArea {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  max-height: 130px;
}

.input-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  transition: border-color 0.15s;
  max-height: 90px;
  overflow-y: auto;
}

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

#messageInput {
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 14px;
  resize: none;
  outline: none;
  min-height: 24px;
  max-height: 65px;
  line-height: 1.5;
  font-family: inherit;
  overflow-y: hidden;
}

#messageInput::placeholder {
  color: var(--text-muted);
}

.upload-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 12px;
  color: var(--text-secondary);
  border-top: 1px solid var(--border);
  margin-top: 4px;
  padding-top: 6px;
}

.upload-preview.hidden {
  display: none;
}

.upload-preview span {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#cancelUploadBtn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 14px;
  padding: 2px 4px;
}

#cancelUploadBtn:hover {
  color: var(--accent);
}

/* ===== 按钮 ===== */
.btn-icon {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 20px;
  cursor: pointer;
  padding: 6px;
  border-radius: 50%;
  transition: background 0.15s, color 0.15s;
  line-height: 1;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.btn-send {
  padding: 8px 18px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s, opacity 0.15s;
  height: 38px;
}

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

.btn-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ===== 上传进度条 ===== */
.upload-progress {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

.upload-progress.hidden {
  display: none;
}

.progress-bar {
  flex: 1;
  height: 6px;
  background: var(--bg-primary);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.2s ease;
}

.progress-text {
  font-size: 12px;
  color: var(--text-secondary);
  min-width: 40px;
  text-align: right;
}

/* ===== 弹窗 ===== */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 90%;
  max-width: 360px;
  box-shadow: var(--shadow);
}

.modal-content h3 {
  font-size: 16px;
  margin-bottom: 16px;
  color: var(--text-primary);
}

.modal-content input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  margin-bottom: 16px;
}

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

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.btn-cancel {
  padding: 8px 16px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
}

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

.btn-primary {
  padding: 8px 16px;
  background: var(--accent);
  border: none;
  color: white;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
}

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

/* ===== Toast ===== */
.toast {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-radius: var(--radius);
  font-size: 13px;
  z-index: 200;
  box-shadow: var(--shadow);
  transition: opacity 0.3s ease;
}

.toast.hidden {
  opacity: 0;
  pointer-events: none;
}

/* ===== 置顶面板 ===== */
#pinnedPanel {
  position: fixed;
  top: 0;
  right: 0;
  width: 280px;
  height: 100vh;
  z-index: 50;
  background: var(--bg-secondary);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.25s ease;
}

#pinnedPanel.open {
  transform: translateX(0);
}

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

.pinned-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

#pinnedList {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

#pinnedList::-webkit-scrollbar {
  width: 4px;
}

#pinnedList::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary);
  border-radius: 2px;
}

.pinned-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background 0.15s;
  border-bottom: 1px solid rgba(255,255,255,0.04);
}

.pinned-item:hover {
  background: var(--bg-tertiary);
}

.pinned-item:active {
  background: rgba(233,69,96,0.1);
}

.pinned-item-icon {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  background: var(--bg-primary);
  border-radius: 50%;
}

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

.pinned-item-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.pinned-item-text {
  font-size: 13px;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.4;
}

.pinned-item-file {
  font-size: 12px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pinned-empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

/* 置顶切换按钮活跃状态 */
#pinnedToggleBtn.active {
  color: var(--accent);
  background: rgba(233,69,96,0.15);
}

/* 遮罩层 */
#pinnedOverlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 49;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

#pinnedOverlay.open {
  opacity: 1;
  pointer-events: auto;
}

/* 消息高亮（从置顶跳转到原消息） */
.message-highlight {
  animation: highlightPulse 2s ease;
}

@keyframes highlightPulse {
  0%, 100% { opacity: 1; }
  10% { opacity: 0.6; }
  20% { opacity: 1; }
  30% { opacity: 0.6; }
  40% { opacity: 1; }
}

/* ===== 响应式 ===== */
@media (max-width: 767px) {
  #appContainer {
    max-width: 100%;
  }

  #header {
    padding: 10px 12px;
  }

  #messageContainer {
    padding: 8px 10px;
  }

  #inputArea {
    padding: 8px 10px;
  }

  .message {
    max-width: 90%;
  }

  .image-preview {
    max-width: 220px;
    max-height: 260px;
  }
}

@media (min-width: 768px) {
  #appContainer {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}

/* 平板：置顶面板半屏宽度 */
@media (min-width: 768px) and (max-width: 1023px) {
  #pinnedPanel.open {
    width: 320px;
  }
}

/* 桌面版：置顶面板作为侧栏常驻 */
@media (min-width: 1024px) {
  body {
    display: flex;
    justify-content: center;
  }

  #appContainer {
    flex-direction: row;
    max-width: min(2800px, calc(100vw - 48px));
    border-right: none;
  }

  #app {
    max-width: none;
    flex: 1;
  }

  #pinnedPanel {
    position: relative;
    transform: none;
    width: 280px;
    flex-shrink: 0;
    border-right: 1px solid var(--border);
    transition: none;
  }

  #pinnedPanel.open {
    transform: none;
  }

  #pinnedOverlay {
    display: none;
  }

  #pinnedToggleBtn {
    display: none;
  }
}
