/* ==========================================================================
   P2P WebRTC Modern Dark-Mode Theme & Mobile-First Responsive Styles
   ========================================================================== */

:root {
  --bg-main: #0b0f19;
  --bg-surface: #111827;
  --bg-surface-elevated: #1e293b;
  --bg-surface-glass: rgba(17, 24, 39, 0.85);
  
  --border-subtle: #1f293d;
  --border-focus: #38bdf8;
  
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  
  --accent-primary: #0284c7;
  --accent-hover: #0369a1;
  --accent-cyan: #06b6d4;
  --accent-emerald: #10b981;
  --accent-emerald-soft: rgba(16, 185, 129, 0.15);
  --accent-danger: #ef4444;
  --accent-danger-soft: rgba(239, 68, 68, 0.15);
  --accent-warning: #f59e0b;
  --accent-purple: #8b5cf6;
  
  --bubble-sent: #0369a1;
  --bubble-received: #1e293b;
  
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-full: 9999px;
  
  --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.25);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 10px 30px rgba(0, 0, 0, 0.6);
  --shadow-glow: 0 0 15px rgba(6, 182, 212, 0.35);
  
  --font-sans: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
}

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

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  background-color: var(--bg-main);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

/* Base App Layout */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  background: var(--bg-main);
  position: relative;
  overflow: hidden;
  padding-top: env(safe-area-inset-top, 0);
  padding-bottom: env(safe-area-inset-bottom, 0);
}

/* ==========================================================================
   Header Section
   ========================================================================== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
  z-index: 10;
}

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

.brand-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, #0284c7 0%, #6366f1 100%);
  color: #ffffff;
  box-shadow: 0 2px 10px rgba(2, 132, 199, 0.4);
}

.brand-title {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text-primary);
  line-height: 1.2;
}

.brand-subtitle {
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-secondary);
}

/* Status Indicator Badge */
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  border-radius: var(--radius-full);
  font-size: 0.75rem;
  font-weight: 600;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid transparent;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.status-badge.disconnected {
  background: rgba(100, 116, 139, 0.15);
  color: var(--text-secondary);
  border-color: rgba(100, 116, 139, 0.3);
}
.status-badge.disconnected .status-dot {
  background: var(--text-muted);
}

.status-badge.connecting {
  background: rgba(245, 158, 11, 0.15);
  color: var(--accent-warning);
  border-color: rgba(245, 158, 11, 0.3);
}
.status-badge.connecting .status-dot {
  background: var(--accent-warning);
  animation: pulse-dot 1.2s infinite;
}

.status-badge.connected {
  background: var(--accent-emerald-soft);
  color: var(--accent-emerald);
  border-color: rgba(16, 185, 129, 0.35);
  box-shadow: 0 0 10px rgba(16, 185, 129, 0.25);
}
.status-badge.connected .status-dot {
  background: var(--accent-emerald);
  box-shadow: 0 0 8px var(--accent-emerald);
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(1.3); }
}

/* ==========================================================================
   Connection Management Card
   ========================================================================== */
.conn-panel {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

@media (max-width: 580px) {
  .conn-panel {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 10px 14px;
  }
}

.peer-box {
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.box-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.box-label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  text-transform: uppercase;
}

.tag-badge {
  font-size: 0.65rem;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: var(--radius-full);
  background: rgba(14, 165, 233, 0.15);
  color: #38bdf8;
}

.id-display-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--bg-main);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.peer-id-text {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  font-weight: 600;
  color: #38bdf8;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  user-select: all;
  -webkit-user-select: all;
}

.btn-icon {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.btn-icon:hover, .btn-icon:focus-visible {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-primary);
}

.connect-input-group {
  display: flex;
  gap: 6px;
}

.connect-input-group input {
  flex: 1;
  background: var(--bg-main);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 0.8rem;
  outline: none;
  min-width: 0;
  transition: border-color 0.2s ease;
}

.connect-input-group input:focus {
  border-color: var(--border-focus);
}

.btn-primary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: var(--accent-primary);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0 12px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.1s ease;
  flex-shrink: 0;
  min-height: 32px;
}

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

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

/* ==========================================================================
   Floating Audio Call Bar
   ========================================================================== */
.audio-call-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: linear-gradient(90deg, #111827 0%, #1e1b4b 100%);
  border-bottom: 2px solid var(--accent-emerald);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  animation: slide-down 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 20;
  flex-shrink: 0;
}

@keyframes slide-down {
  from { opacity: 0; transform: translateY(-15px); }
  to { opacity: 1; transform: translateY(0); }
}

.call-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.call-pulsing-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent-emerald);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 14px rgba(16, 185, 129, 0.6);
  animation: pulse-avatar 2s infinite;
}

@keyframes pulse-avatar {
  0%, 100% { box-shadow: 0 0 10px rgba(16, 185, 129, 0.5); }
  50% { box-shadow: 0 0 20px rgba(16, 185, 129, 0.9); }
}

.call-text-meta {
  display: flex;
  flex-direction: column;
}

.call-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

#callStatusTitle {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-primary);
}

/* Audio wave animation */
.voice-wave-container {
  display: flex;
  align-items: center;
  gap: 2px;
  height: 14px;
}

.wave-bar {
  width: 3px;
  height: 100%;
  background: var(--accent-cyan);
  border-radius: 2px;
  animation: audio-wave 1s ease-in-out infinite alternate;
}
.wave-bar:nth-child(1) { animation-delay: 0.1s; height: 40%; }
.wave-bar:nth-child(2) { animation-delay: 0.3s; height: 80%; }
.wave-bar:nth-child(3) { animation-delay: 0.2s; height: 100%; }
.wave-bar:nth-child(4) { animation-delay: 0.4s; height: 60%; }

@keyframes audio-wave {
  0% { transform: scaleY(0.3); opacity: 0.4; }
  100% { transform: scaleY(1); opacity: 1; }
}

.call-timer {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--accent-emerald);
  font-weight: 600;
}

.call-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.call-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  min-width: 40px;
  min-height: 40px;
}

.btn-call {
  background: var(--accent-emerald);
  color: #ffffff;
}
.btn-call:hover { background: #059669; }

.btn-mic {
  background: var(--bg-surface-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
}
.btn-mic:hover { background: #334155; }
.btn-mic.muted {
  background: var(--accent-danger-soft);
  color: var(--accent-danger);
  border-color: rgba(239, 68, 68, 0.4);
}

.btn-end-call {
  background: var(--accent-danger);
  color: #ffffff;
}
.btn-end-call:hover { background: #dc2626; }
.btn-end-call:active, .call-btn:active { transform: scale(0.92); }

/* ==========================================================================
   Incoming Call Modal Overlay
   ========================================================================== */
.modal-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  animation: fade-in 0.2s ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.incoming-card {
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 28px 24px;
  width: 100%;
  max-width: 340px;
  text-align: center;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.incoming-ring-animation {
  position: relative;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: rgba(16, 185, 129, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
}

.ring-pulse {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 50%;
  border: 2px solid var(--accent-emerald);
  animation: ring-grow 1.5s cubic-bezier(0.2, 0.8, 0.2, 1) infinite;
}

@keyframes ring-grow {
  0% { transform: scale(0.8); opacity: 1; }
  100% { transform: scale(1.6); opacity: 0; }
}

.incoming-card h3 {
  font-size: 1.15rem;
  font-weight: 700;
}

.incoming-peer-id {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: #38bdf8;
  word-break: break-all;
  max-width: 260px;
}

.incoming-btn-row {
  display: flex;
  gap: 12px;
  width: 100%;
  margin-top: 10px;
}

.btn-reject, .btn-accept {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 48px;
  transition: transform 0.1s ease;
}

.btn-reject {
  background: rgba(239, 68, 68, 0.2);
  color: var(--accent-danger);
  border: 1px solid rgba(239, 68, 68, 0.4);
}
.btn-reject:hover { background: var(--accent-danger); color: #fff; }

.btn-accept {
  background: var(--accent-emerald);
  color: #ffffff;
}
.btn-accept:hover { background: #059669; }

/* ==========================================================================
   Chat Section & Scrollable Feed
   ========================================================================== */
.chat-section {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  background: var(--bg-main);
  position: relative;
}

.chat-header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: rgba(17, 24, 39, 0.5);
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}

.active-peer-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.active-peer-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
}
.active-peer-dot.active {
  background: var(--accent-emerald);
  box-shadow: 0 0 6px var(--accent-emerald);
}

.active-peer-name {
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.btn-ghost-sm {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  padding: 4px 8px;
  font-size: 0.72rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.2s ease;
}

.btn-ghost-sm:hover {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-secondary);
}

.message-feed {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  user-select: text;
  -webkit-user-select: text;
  scroll-behavior: smooth;
}

/* Custom Scrollbar */
.message-feed::-webkit-scrollbar {
  width: 5px;
}
.message-feed::-webkit-scrollbar-track {
  background: transparent;
}
.message-feed::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: var(--radius-full);
}
.message-feed::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.25);
}

/* Welcome Card */
.system-welcome-card {
  margin: auto;
  max-width: 380px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 24px 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  box-shadow: var(--shadow-sm);
}

.welcome-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: rgba(56, 189, 248, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
}

.system-welcome-card h3 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
}

.system-welcome-card p {
  font-size: 0.82rem;
  color: var(--text-secondary);
  line-height: 1.4;
}

.security-pills {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  margin-top: 8px;
}

.security-pills span {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: var(--radius-full);
  background: rgba(255, 255, 255, 0.05);
  color: var(--accent-cyan);
  border: 1px solid rgba(6, 182, 212, 0.2);
}

/* Message Bubbles */
.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 82%;
  position: relative;
  animation: pop-bubble 0.15s ease;
}

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

.msg-row.sent {
  align-self: flex-end;
  align-items: flex-end;
}

.msg-row.received {
  align-self: flex-start;
  align-items: flex-start;
}

.msg-bubble {
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 0.92rem;
  line-height: 1.45;
  word-break: break-word;
  position: relative;
}

.msg-row.sent .msg-bubble {
  background: var(--bubble-sent);
  color: #ffffff;
  border-bottom-right-radius: 4px;
  box-shadow: 0 2px 8px rgba(3, 105, 161, 0.3);
}

.msg-row.received .msg-bubble {
  background: var(--bubble-received);
  color: var(--text-primary);
  border: 1px solid rgba(255, 255, 255, 0.05);
  border-bottom-left-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}

.msg-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.68rem;
  color: var(--text-muted);
  margin-top: 3px;
  padding: 0 4px;
}

/* System notice message */
.system-notice-bubble {
  align-self: center;
  max-width: 90%;
  padding: 4px 12px;
  border-radius: var(--radius-full);
  background: rgba(30, 41, 59, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.06);
  font-size: 0.75rem;
  color: var(--text-secondary);
  text-align: center;
}

/* ==========================================================================
   Chat Input Bar
   ========================================================================== */
.chat-input-bar {
  padding: 10px 16px;
  background: var(--bg-surface);
  border-top: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.chat-form {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-form input {
  flex: 1;
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-full);
  padding: 12px 18px;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.92rem;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  min-height: 46px;
}

.chat-form input:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.2);
}

.chat-form input:disabled {
  background: rgba(17, 24, 39, 0.6);
  color: var(--text-muted);
  cursor: not-allowed;
}

.btn-send {
  width: 46px;
  height: 46px;
  min-width: 46px;
  min-height: 46px;
  border-radius: 50%;
  background: var(--accent-primary);
  color: #ffffff;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 2px 10px rgba(2, 132, 199, 0.4);
}

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

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

.btn-send:disabled {
  background: var(--bg-surface-elevated);
  color: var(--text-muted);
  cursor: not-allowed;
  box-shadow: none;
}

/* ==========================================================================
   Toast Popup Notification
   ========================================================================== */
.toast-popup {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(30, 41, 59, 0.95);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-primary);
  padding: 8px 16px;
  border-radius: var(--radius-full);
  font-size: 0.82rem;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  z-index: 150;
  pointer-events: none;
  animation: toast-in 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  white-space: nowrap;
}

@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}
