/* ═══════════════════════════════════════════════════════════
   WebRTC Voice Call v2 — SFU Multi-Peer
   Dark theme, mobile-first.
   ═══════════════════════════════════════════════════════════ */

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

:root {
  --bg: #0a0a0f; --surface: #14141f; --surface-hover: #1e1e2e;
  --border: #2a2a3e; --text: #e8e8f0; --text-dim: #8888a0;
  --primary: #6c5ce7; --primary-hover: #7d6ff0;
  --success: #00c853; --success-hover: #00e676;
  --danger: #ff5252; --danger-hover: #ff6e6e;
  --warning: #ffc107;
  --meter-left: #6c5ce7; --meter-right: #00c853;
  --radius: 12px; --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
           "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

html, body { height: 100%; font-family: var(--font); background: var(--bg); color: var(--text);
             -webkit-font-smoothing: antialiased; }

#app { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.screen { width: 100%; max-width: 480px; padding: 20px; }

@media (min-width: 900px) {
  .screen { max-width: 1024px; }
}
.screen.hidden { display: none; }
.container { background: var(--surface); border: 1px solid var(--border); border-radius: 20px;
             padding: 40px 32px; text-align: center; }

/* ── Hero ── */
.hero { margin-bottom: 28px; }
.logo { color: var(--primary); display: inline-block; margin-bottom: 14px; }
.hero h1 { font-size: 1.6rem; font-weight: 700; margin-bottom: 6px; }
.subtitle { font-size: 0.85rem; color: var(--text-dim); }

/* ── Error ── */
.error-message { background: rgba(255,82,82,0.12); border: 1px solid rgba(255,82,82,0.3);
                 color: var(--danger); padding: 10px 16px; border-radius: var(--radius-sm);
                 margin-bottom: 20px; font-size: 0.9rem; }
.error-message.hidden { display: none; }

/* ── Buttons ── */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px;
       font-family: var(--font); font-size: 1rem; font-weight: 600;
       padding: 14px 28px; border: none; border-radius: var(--radius);
       cursor: pointer; transition: all 0.15s; white-space: nowrap; }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; width: 100%; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface-hover); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }
.btn-ghost { background: transparent; color: var(--text-dim); padding: 8px 12px; }
.btn-ghost:hover { color: var(--text); background: rgba(255,255,255,0.05); }
.btn-sm { font-size: 0.85rem; padding: 8px 16px; }
.btn-icon { font-size: 1.2em; }

/* ── Inputs ── */
.input-name, .input-code {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 18px; color: var(--text);
  font-family: var(--font); font-size: 1rem; outline: none; transition: border-color 0.15s;
}
.input-name:focus, .input-code:focus {
  border-color: var(--primary); box-shadow: 0 0 0 3px rgba(108,92,231,0.2);
}

/* Room name input */
.input-code {
  width: 100%;
  font-size: 1.1rem; font-weight: 600; text-align: center;
  padding: 16px 18px;
}
.input-code::placeholder {
  font-weight: 400; font-size: 0.9rem; color: var(--text-dim);
}

/* Nickname input — same style as room name */
.input-name {
  width: 100%;
  font-size: 1.1rem; font-weight: 600; text-align: center;
  padding: 16px 18px;
}
.input-name::placeholder {
  font-weight: 400; font-size: 0.9rem; color: var(--text-dim);
}

.actions { display: flex; flex-direction: column; gap: 12px; }

.hint { margin-top: 18px; font-size: 0.8rem; color: var(--text-dim); }

/* ── Room Screen ── */
.room-container { padding: 24px; display: flex; flex-direction: column; }
.room-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; flex-wrap: wrap; gap: 8px; }
.room-code-area { display: flex; align-items: center; gap: 8px; }
.room-label { font-size: 0.75rem; color: var(--text-dim); }
.room-code { font-size: 1.3rem; font-weight: 700; letter-spacing: 3px; color: var(--primary); }
.conn-status { font-size: 0.8rem; padding: 4px 12px; border-radius: 20px; font-weight: 600; }
.status-connecting { color: var(--warning); background: rgba(255,193,7,0.1); }
.status-connected { color: var(--success); background: rgba(0,200,83,0.1); }
.status-disconnected { color: var(--danger); background: rgba(255,82,82,0.1); }

/* Desktop: two-column grid; mobile: single column */
.room-body { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 16px; }

.room-columns {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;
  max-width: 480px;
}

.room-left {
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  width: 100%;
}

.room-right {
  width: 100%;
}

@media (min-width: 900px) {
  .room-columns {
    display: grid;
    grid-template-columns: 340px 1fr;
    gap: 24px;
    max-width: 960px;
    align-items: start;
  }

  .room-left {
    position: sticky;
    top: 24px;
  }

  .room-right {
    position: sticky;
    top: 24px;
  }

  .room-right .chat-section {
    height: calc(100vh - 160px);
    min-height: 500px;
  }

  .room-container {
    max-width: 1024px;
    margin: 0 auto;
  }
}

/* ── Meters ── */
.meters { display: flex; gap: 16px; align-items: flex-end; height: 100px; width: 140px; }
.meter-channel { display: flex; flex-direction: column; align-items: center; gap: 4px; flex: 1; }
.meter-label { font-size: 0.7rem; font-weight: 700; color: var(--text-dim); width: 22px; height: 22px;
               border-radius: 50%; display: flex; align-items: center; justify-content: center;
               background: var(--surface-hover); }
.meter-track { flex: 1; width: 10px; background: var(--bg); border-radius: 5px;
               position: relative; overflow: hidden; border: 1px solid var(--border); }
.meter-bar { position: absolute; bottom: 0; left: 0; right: 0; border-radius: 5px;
             transition: height 0.1s ease; min-height: 2px; }
.meter-bar-left { background: linear-gradient(to top, var(--meter-left), #a29bfe); }
.meter-bar-right { background: linear-gradient(to top, var(--meter-right), #69f0ae); }
.meter-val { font-size: 0.65rem; color: var(--text-dim); font-variant-numeric: tabular-nums;
             min-width: 45px; text-align: center; }

/* ── User List ── */
.user-section { width: 100%; }
.user-section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.user-section-header span { font-size: 0.8rem; color: var(--text-dim); font-weight: 600; }
.user-list { display: flex; flex-direction: column; gap: 6px; max-height: 200px; overflow-y: auto; }
.user-item { display: flex; align-items: center; gap: 10px; padding: 8px 12px;
             background: var(--bg); border-radius: var(--radius-sm); border: 1px solid transparent; }
.user-item.is-me { border-color: var(--primary); }
.user-avatar { width: 32px; height: 32px; border-radius: 50%;
               display: flex; align-items: center; justify-content: center;
               font-weight: 700; font-size: 0.9rem; color: #fff; flex-shrink: 0; }
.user-name { flex: 1; font-size: 0.9rem; text-align: left; }
.user-item.is-me .user-name { color: var(--primary); font-weight: 600; }
.user-indicator { width: 8px; height: 8px; border-radius: 50%; background: var(--success); flex-shrink: 0; }
.user-bitrate { font-size: 0.7rem; color: var(--text-dim); font-variant-numeric: tabular-nums;
                min-width: 52px; text-align: right; flex-shrink: 0; }
.stats-outbound { font-size: 0.75rem; color: var(--text-dim); font-weight: 600;
                  font-variant-numeric: tabular-nums; }

/* ── Room Controls ── */
.room-controls { display: flex; gap: 10px; width: 100%; max-width: 300px; }
.room-controls .btn { flex: 1; }
.quality-badge { font-size: 0.7rem; color: var(--text-dim); background: rgba(108,92,231,0.08);
                 padding: 6px 14px; border-radius: 20px; border: 1px solid rgba(108,92,231,0.15); }

/* ── Toast ── */
.toast { position: fixed; bottom: 40px; left: 50%; transform: translateX(-50%) translateY(20px);
         background: var(--primary); color: #fff; padding: 12px 24px; border-radius: 50px;
         font-size: 0.9rem; font-weight: 600; opacity: 0; transition: all 0.3s ease;
         pointer-events: none; z-index: 1000; }
.toast-visible { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Chat ── */
.chat-section {
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  height: 320px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 0.85rem;
}

.chat-empty {
  text-align: center;
  color: var(--text-dim);
  padding: 30px;
  font-size: 0.8rem;
}

/* Message row: avatar + bubble */
.chat-msg-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  max-width: 90%;
}

.chat-msg-row.msg-me {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.chat-msg-row.msg-other {
  align-self: flex-start;
}

.chat-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.8rem;
  color: #fff;
  flex-shrink: 0;
  user-select: none;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

.chat-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  max-width: 100%;
  word-break: break-word;
  line-height: 1.45;
  position: relative;
  box-shadow: 0 1px 4px rgba(0,0,0,0.12);
  animation: msgIn 0.25s ease-out;
}

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

.msg-me .chat-bubble {
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg-other .chat-bubble {
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.chat-bubble .bubble-text {
  font-size: 0.9rem;
}

.chat-input-row {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.chat-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 16px;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.88rem;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.chat-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(108,92,231,0.15);
}
.chat-input::placeholder { color: var(--text-dim); }

/* ── Audio processing toggles ── */
.toggles-section {
  width: 100%;
  max-width: 300px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.toggle-label {
  font-size: 0.85rem;
  color: var(--text-dim);
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
  cursor: pointer;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--border);
  border-radius: 24px;
  transition: background 0.2s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background: var(--text-dim);
  border-radius: 50%;
  transition: transform 0.2s;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--primary);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(20px);
  background: #fff;
}

/* ── Mic gain slider ── */
.gain-section {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 300px;
  padding: 10px 16px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.gain-label {
  font-size: 0.85rem;
  color: var(--text-dim);
  white-space: nowrap;
}

.gain-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  border-radius: 3px;
  background: var(--border);
  outline: none;
  cursor: pointer;
}

.gain-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  transition: transform 0.1s;
}

.gain-slider::-webkit-slider-thumb:active {
  transform: scale(1.2);
}

.gain-value {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--primary);
  min-width: 32px;
  text-align: right;
}

/* ── Responsive ── */
@media (max-width: 480px) {
  .screen { padding: 10px; }
  .container { padding: 24px 16px; border-radius: 16px; }
  .room-container { padding: 16px; }
  .hero h1 { font-size: 1.3rem; }
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f0f0f5; --surface: #ffffff; --surface-hover: #f5f5fa;
    --border: #e0e0e8; --text: #1a1a2e; --text-dim: #8888a0;
  }
}
