/* ===== CSS Variables ===== */
:root {
  --primary: #4f6ef7;
  --primary-dark: #3b5bdb;
  --primary-light: #eef1ff;
  --bg: #f0f2f5;
  --header-bg: #ffffff;
  --bubble-self: #4f6ef7;
  --bubble-other: #ffffff;
  --text-self: #ffffff;
  --text-other: #1a1a2e;
  --text-muted: #8e8e93;
  --text-light: #aeaeb2;
  --border: #e5e5ea;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.1);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.15);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 20px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ===== Reset ===== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 15px;
  color: var(--text-other);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ===== App Layout ===== */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  background: var(--bg);
  position: relative;
}

/* ===== Header ===== */
.header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(12px + var(--safe-top)) 16px 10px;
  background: var(--header-bg);
  box-shadow: var(--shadow-sm);
  z-index: 10;
}

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

.logo {
  font-size: 18px;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: 0.5px;
}

.room-badge {
  font-size: 12px;
  color: var(--text-muted);
  background: var(--primary-light);
  padding: 3px 10px;
  border-radius: 20px;
  font-weight: 500;
}

.room-badge span {
  color: var(--primary);
  font-weight: 600;
  font-family: "SF Mono", Consolas, monospace;
}

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

.online-badge {
  font-size: 12px;
  color: var(--text-muted);
  background: #f0f0f5;
  padding: 3px 10px;
  border-radius: 20px;
  white-space: nowrap;
}

/* ===== User Bar ===== */
.user-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.user-bar-label {
  color: var(--text-light);
  flex-shrink: 0;
}

.user-name-input {
  color: var(--text-other);
  font-weight: 600;
  outline: none;
  border-bottom: 1px dashed transparent;
  padding: 1px 2px;
  min-width: 60px;
  cursor: text;
  transition: border-color 0.2s;
}

.user-name-input:hover {
  border-bottom-color: var(--text-light);
}

.user-name-input:focus {
  border-bottom-color: var(--primary);
}

.edit-icon {
  color: var(--text-light);
  flex-shrink: 0;
}

/* ===== Buttons ===== */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--primary);
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.2s;
  flex-shrink: 0;
}

.icon-btn:hover, .icon-btn:active {
  background: var(--primary-light);
}

.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 24px;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s;
}

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

.btn-primary:active {
  transform: scale(0.97);
}

.btn-secondary {
  padding: 10px 24px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text-other);
  font-size: 15px;
  font-weight: 500;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s;
}

.btn-secondary:hover {
  background: #f5f5f5;
}

/* ===== Chat Area ===== */
.chat-area {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 16px 12px;
  scroll-behavior: smooth;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 100%;
}

/* ===== Welcome Card ===== */
.welcome-card {
  text-align: center;
  padding: 40px 24px;
  margin: auto 0;
}

.welcome-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.welcome-card h3 {
  font-size: 20px;
  color: var(--text-other);
  margin-bottom: 8px;
}

.welcome-card > p {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 24px;
}

.welcome-tips {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 280px;
  margin: 0 auto;
  text-align: left;
}

.tip {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--text-muted);
  background: #fff;
  padding: 10px 14px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.tip-num {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}

/* ===== Messages ===== */
.message {
  display: flex;
  gap: 8px;
  margin-bottom: 6px;
  animation: fadeIn 0.25s ease;
}

.message.self {
  flex-direction: row-reverse;
}

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

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  flex-shrink: 0;
  text-transform: uppercase;
}

.message-content {
  display: flex;
  flex-direction: column;
  max-width: 75%;
  gap: 2px;
}

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

.message-sender {
  font-size: 12px;
  color: var(--text-muted);
  padding: 0 4px;
}

.message-bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  word-break: break-all;
  white-space: pre-wrap;
  line-height: 1.5;
  font-size: 15px;
  position: relative;
  box-shadow: var(--shadow-sm);
}

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

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

.message-bubble.image-bubble {
  padding: 4px;
  overflow: hidden;
}

.message-image {
  max-width: 240px;
  max-height: 300px;
  border-radius: 8px;
  cursor: pointer;
  display: block;
  object-fit: contain;
  transition: opacity 0.2s;
}

.message-image:active {
  opacity: 0.85;
}

.message-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px;
}

.message-time {
  font-size: 11px;
  color: var(--text-light);
}

.action-btn {
  font-size: 11px;
  color: var(--primary);
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition: background 0.2s;
}

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

/* ===== System Messages ===== */
.system-message {
  text-align: center;
  font-size: 12px;
  color: var(--text-light);
  padding: 6px 0;
  margin: 4px 0;
}

/* ===== Input Area ===== */
.input-area {
  flex-shrink: 0;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: #fff;
  border-top: 1px solid var(--border);
  z-index: 10;
}

.upload-label {
  cursor: pointer;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--primary-light);
}

#messageInput {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 14px;
  font-size: 15px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 120px;
  min-height: 40px;
  line-height: 1.4;
  transition: border-color 0.2s;
  background: #fafafa;
}

#messageInput:focus {
  border-color: var(--primary);
  background: #fff;
}

.send-btn {
  padding: 0 20px;
  height: 40px;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  flex-shrink: 0;
}

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

.send-btn:active {
  transform: scale(0.95);
}

.send-btn:disabled {
  background: var(--text-light);
  cursor: not-allowed;
}

/* ===== Upload Progress ===== */
.upload-progress {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0,0,0,0.75);
  color: #fff;
  padding: 8px 18px;
  border-radius: 20px;
  font-size: 13px;
  z-index: 200;
}

.upload-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.modal-overlay.show {
  display: flex;
  animation: fadeIn 0.2s ease;
}

.modal-card {
  background: #fff;
  border-radius: var(--radius-lg);
  padding: 28px 24px;
  text-align: center;
  max-width: 340px;
  width: 100%;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.3s ease;
}

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

.modal-card h3 {
  font-size: 18px;
  margin-bottom: 16px;
}

.qr-image {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 12px;
  width: 280px;
  height: 280px;
}

.qr-image img {
  width: 100%;
  height: 100%;
  border-radius: var(--radius);
}

.qr-loading {
  color: var(--text-muted);
  font-size: 14px;
}

.qr-hint {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.qr-url-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f5f5f5;
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  margin-bottom: 16px;
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
}

.qr-url-box span {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: "SF Mono", Consolas, monospace;
}

.copy-url-btn {
  border: none;
  background: var(--primary);
  color: #fff;
  padding: 4px 12px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

.copy-url-btn:hover {
  background: var(--primary-dark);
}

/* ===== Image Preview ===== */
.image-preview-wrap {
  max-width: 90vw;
  max-height: 70vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.image-preview-wrap img {
  max-width: 100%;
  max-height: 70vh;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  object-fit: contain;
}

.image-toolbar {
  display: flex;
  gap: 12px;
  margin-top: 16px;
  justify-content: center;
}

/* ===== Toast ===== */
.toast-container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2000;
  pointer-events: none;
}

.toast {
  background: rgba(0,0,0,0.8);
  color: #fff;
  padding: 10px 24px;
  border-radius: 10px;
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.25s ease;
  white-space: nowrap;
}

.toast.show {
  opacity: 1;
}

/* ===== Scrollbar ===== */
.chat-area::-webkit-scrollbar {
  width: 4px;
}

.chat-area::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.15);
  border-radius: 4px;
}

.chat-area::-webkit-scrollbar-track {
  background: transparent;
}

/* ===== Desktop ===== */
@media (min-width: 768px) {
  #app {
    box-shadow: 0 0 40px rgba(0,0,0,0.08);
  }

  .message-image {
    max-width: 320px;
    max-height: 400px;
  }

  .message-content {
    max-width: 60%;
  }
}
