/* ===== 变量 ===== */
:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-tertiary: #252542;
  --accent: #6366f1;
  --accent-light: #818cf8;
  --accent-gradient: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
  --text-primary: #fff;
  --text-secondary: #a0a0c0;
  --text-muted: #6b6b8a;
  --success: #10b981;
  --warning: #f59e0b;
  --error: #ef4444;
  --glass-bg: rgba(255, 255, 255, 0.05);
  --glass-border: rgba(255, 255, 255, 0.1);
  --radius: 12px;
  --radius-lg: 16px;
}

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

html,
body {
  height: 100%;
}

body {
  font-family: 'Inter', -apple-system, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
}

body::before {
  content: '';
  position: fixed;
  inset: -50%;
  background:
    radial-gradient(circle at 20% 20%, rgba(99, 102, 241, 0.15) 0%, transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(168, 85, 247, 0.15) 0%, transparent 40%);
  animation: float 20s ease-in-out infinite;
  pointer-events: none;
  z-index: -1;
}

@keyframes float {

  0%,
  100% {
    transform: translate(0, 0);
  }

  50% {
    transform: translate(-20px, -20px);
  }
}

.glass {
  background: var(--glass-bg);
  backdrop-filter: blur(20px);
  border: 1px solid var(--glass-border);
}

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

.screen {
  display: none;
  width: 100%;
  height: 100%;
}

.screen.active {
  display: flex;
  animation: fadeIn .25s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

.hidden {
  display: none !important;
}

/* ===== 启动界面 ===== */
#start-screen {
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.start-container {
  width: 100%;
  max-width: 400px;
  padding: 32px;
  border-radius: var(--radius-lg);
  text-align: center;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 8px;
}

.logo-icon {
  font-size: 40px;
}

.logo h1 {
  font-size: 2rem;
  background: var(--accent-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.tagline {
  color: var(--text-secondary);
  margin-bottom: 24px;
}

/* 模式切换 */
.mode-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 24px;
}

.mode-tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 12px;
  background: var(--bg-tertiary);
  border: 2px solid transparent;
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all .15s;
}

.mode-tab:hover {
  border-color: var(--accent);
}

.mode-tab.active {
  background: rgba(99, 102, 241, 0.15);
  border-color: var(--accent);
  color: var(--text-primary);
}

.tab-icon {
  font-size: 24px;
}

.mode-form {
  display: none;
}

.mode-form.active {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  text-align: left;
}

.form-group label {
  display: block;
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.form-group input {
  width: 100%;
  padding: 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 1rem;
}

.form-group input:focus {
  outline: none;
  border-color: var(--accent);
}

.room-input-row {
  display: flex;
  gap: 8px;
}

.room-input-row input {
  flex: 1;
}

.form-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 8px;
}

/* 房间号显示 */
.lan-room-info {
  text-align: center;
  margin-bottom: 16px;
  padding: 16px;
  background: rgba(99, 102, 241, 0.1);
  border-radius: var(--radius);
  border: 1px dashed var(--accent);
  cursor: pointer;
  transition: all .15s;
}

.lan-room-info:hover {
  background: rgba(99, 102, 241, 0.2);
  border-color: var(--accent-light);
}

.lan-room-info label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.room-id-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.room-id-display span {
  font-size: 1.5rem;
  font-weight: 700;
  font-family: monospace;
  color: var(--accent-light);
  letter-spacing: 2px;
}

.btn-copy,
.btn-copy-small {
  background: var(--bg-tertiary);
  border: none;
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 1rem;
  transition: all .15s;
}

.btn-copy:hover,
.btn-copy-small:hover {
  background: var(--accent);
}

.btn-copy-small {
  padding: 4px 8px;
  font-size: 0.8rem;
}

.room-hint {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 8px;
}

.room-badge {
  padding: 4px 10px;
  background: var(--accent-gradient);
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  font-family: monospace;
}

/* 可点击房间号 */
.room-id-clickable {
  font-size: 2.5rem;
  font-weight: 700;
  font-family: monospace;
  color: var(--accent-light);
  letter-spacing: 4px;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: var(--radius);
  transition: all .15s;
}

.room-id-clickable:hover {
  background: rgba(99, 102, 241, 0.2);
  transform: scale(1.05);
}

/* 二维码弹窗 */
.qr-modal-content {
  padding: 32px;
  text-align: center;
}

.qr-code {
  margin: 20px auto;
  padding: 16px;
  background: white;
  border-radius: 12px;
  display: inline-block;
}

.qr-code img,
.qr-code canvas {
  display: block;
}

.qr-room-id {
  font-size: 1.1rem;
  margin: 12px 0;
}

.qr-room-id strong {
  font-family: monospace;
  font-size: 1.5rem;
  color: var(--accent-light);
  letter-spacing: 2px;
}

.qr-hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 16px;
}

/* ===== 按钮 ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all .15s;
}

.btn-primary {
  background: var(--accent-gradient);
  color: white;
}

.btn-primary:hover {
  transform: translateY(-2px);
}

.btn-large {
  padding: 14px 24px;
}

.btn-sm {
  padding: 8px 16px;
  font-size: 0.875rem;
}

.btn-icon {
  padding: 8px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 1.25rem;
}

.btn-icon:hover {
  color: var(--accent);
}

.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--glass-border);
}

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

.btn-row {
  display: flex;
  gap: 12px;
  margin-top: 8px;
}

.btn-row .btn {
  flex: 1;
  padding: 14px 16px;
}

.start-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ===== 主界面 ===== */
.main-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  padding: 12px;
  gap: 12px;
}

.main-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-radius: var(--radius);
}

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

.logo-small {
  font-size: 20px;
}

.header-left h2 {
  font-size: 1.1rem;
  background: var(--accent-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.mode-badge {
  padding: 2px 8px;
  background: var(--accent);
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
}

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

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
}

.main-content {
  flex: 1;
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 12px;
  min-height: 0;
}

/* ===== 用户列表 ===== */
.users-panel,
.chat-panel {
  display: flex;
  flex-direction: column;
  border-radius: var(--radius);
  overflow: hidden;
}

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

.panel-header h3 {
  font-size: 0.9rem;
}

.badge {
  padding: 2px 8px;
  background: var(--accent-gradient);
  border-radius: 999px;
  font-size: 0.7rem;
}

.users-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.user-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all .15s;
  margin-bottom: 4px;
}

.user-card:hover {
  background: var(--bg-tertiary);
}

.user-card.active {
  background: rgba(99, 102, 241, 0.2);
}

.user-card.is-me {
  opacity: 0.5;
  cursor: default;
}

.user-avatar {
  width: 36px;
  height: 36px;
  background: var(--accent-gradient);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 0.9rem;
}

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

.user-name {
  font-weight: 500;
  font-size: 0.9rem;
}

.user-ip {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  text-align: center;
}

.empty-icon {
  font-size: 36px;
  margin-bottom: 8px;
}

/* ===== 聊天面板 ===== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message {
  max-width: 70%;
  animation: msgIn .2s ease;
}

@keyframes msgIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

.message-bubble {
  padding: 10px 14px;
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  word-wrap: break-word;
  font-size: 0.9rem;
}

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

.message.is-mine .message-bubble {
  background: var(--accent-gradient);
}

.message-time {
  font-size: 0.65rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.message.is-mine .message-time {
  text-align: right;
}

/* 文件消息 */
.message-file {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  background: var(--bg-secondary);
  border-radius: var(--radius);
  border: 1px solid var(--glass-border);
}

.file-icon {
  width: 40px;
  height: 40px;
  background: var(--accent-gradient);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

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

.file-name {
  font-weight: 500;
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-size {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.file-download {
  padding: 6px 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}

.file-download:disabled {
  background: var(--bg-tertiary);
  color: var(--text-muted);
}

/* ===== 输入区 ===== */
.chat-input {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid var(--glass-border);
}

#msg-input {
  flex: 1;
  padding: 10px;
  background: var(--bg-tertiary);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.9rem;
}

#msg-input:focus {
  outline: none;
  border-color: var(--accent);
}

/* ===== 模态框 ===== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal.active {
  display: flex;
}

.modal-content {
  padding: 24px;
  border-radius: var(--radius-lg);
  text-align: center;
  min-width: 280px;
}

.progress-bar {
  height: 6px;
  background: var(--bg-tertiary);
  border-radius: 999px;
  overflow: hidden;
  margin: 16px 0 8px;
}

.progress-fill {
  height: 100%;
  background: var(--accent-gradient);
  width: 0%;
  transition: width .1s;
}

/* ===== Toast ===== */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 1001;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 16px;
  background: var(--glass-bg);
  backdrop-filter: blur(20px);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  animation: slideIn .2s ease;
  max-width: 280px;
  font-size: 0.9rem;
}

.toast.success {
  border-color: var(--success);
}

.toast.error {
  border-color: var(--error);
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(100%);
  }
}

/* ===== 滚动条 ===== */
::-webkit-scrollbar {
  width: 4px;
}

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

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

/* ===== 响应式 ===== */
@media (max-width: 768px) {
  .main-container {
    padding: 8px;
    gap: 8px;
  }

  .main-header {
    padding: 10px 12px;
  }

  .header-left h2 {
    display: none;
  }

  .main-content {
    grid-template-columns: 1fr;
  }

  /* 默认只显示用户列表 */
  .chat-panel {
    display: none;
  }

  /* 点击用户后全屏显示聊天 */
  .chat-panel.active {
    display: flex;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 100;
    border-radius: 0;
    margin: 0;
    background: var(--bg-primary);
  }

  .chat-panel.active .panel-header {
    padding: 16px;
    background: var(--bg-secondary);
  }

  .chat-panel.active #close-chat {
    display: flex !important;
    font-size: 1.2rem;
  }

  .users-panel {
    min-height: 0;
    flex: 1;
  }

  .users-list {
    padding: 4px;
  }

  .user-card {
    padding: 12px 10px;
  }

  .chat-input {
    padding: 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  #msg-input {
    font-size: 16px;
    /* 防止 iOS 自动缩放 */
  }

  .panel-header h3 {
    font-size: 1rem;
  }

  /* 优化模态框 */
  .modal-content {
    margin: 16px;
    max-width: calc(100vw - 32px);
  }

  .qr-modal-content {
    padding: 24px 16px;
  }

  .qr-code img {
    max-width: 180px;
    height: auto;
  }

  /* Toast 位置调整 */
  .toast-container {
    top: auto;
    bottom: 80px;
    right: 8px;
    left: 8px;
  }

  .toast {
    max-width: 100%;
  }
}