:root {
  --bg-primary: #0a0a0f;
  --bg-secondary: #12121a;
  --bg-tertiary: #1a1a26;
  --bg-hover: #22222e;
  --text-primary: #e8e8ed;
  --text-secondary: #8888a0;
  --text-muted: #55556a;
  --accent: #6c5ce7;
  --accent-hover: #7c6cf7;
  --accent-glow: rgba(108, 92, 231, 0.3);
  --green: #2ed573;
  --red: #ff4757;
  --orange: #ffa502;
  --border: #1e1e2e;
  --msg-own: #1a1540;
  --msg-peer: #1a1a26;
  --radius: 12px;
  --radius-sm: 8px;
  --transition: 0.2s ease;
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

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

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

.screen.active {
  display: flex;
  flex-direction: column;
}

/* ===== Login Screen ===== */

#login-screen {
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse at 20% 50%, rgba(108, 92, 231, 0.08) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 20%, rgba(108, 92, 231, 0.05) 0%, transparent 50%),
    var(--bg-primary);
}

.login-container {
  width: 100%;
  max-width: 360px;
  padding: 0 24px;
  text-align: center;
}

.login-logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 80px;
  border-radius: 20px;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  color: white;
  margin-bottom: 24px;
  box-shadow: 0 8px 32px var(--accent-glow);
}

.login-container h1 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 4px;
}

.login-subtitle {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 32px;
}

.input-group {
  margin-bottom: 12px;
}

.input-group input {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 15px;
  outline: none;
  transition: border-color var(--transition);
}

.input-group input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.input-group input::placeholder {
  color: var(--text-muted);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 14px 24px;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  width: 100%;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  color: white;
  margin-top: 8px;
  box-shadow: 0 4px 16px var(--accent-glow);
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 24px var(--accent-glow);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
}

.error-message {
  color: var(--red);
  font-size: 13px;
  margin-top: 16px;
  padding: 10px 14px;
  background: rgba(255, 71, 87, 0.1);
  border-radius: var(--radius-sm);
  border: 1px solid rgba(255, 71, 87, 0.2);
}

/* ===== Chat Header ===== */

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  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;
  min-width: 0;
}

.header-info h2 {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-indicator.online {
  background: var(--green);
  box-shadow: 0 0 8px rgba(46, 213, 115, 0.5);
}

.status-indicator.offline {
  background: var(--text-muted);
}

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

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  background: rgba(46, 213, 115, 0.1);
  color: var(--green);
  border: 1px solid rgba(46, 213, 115, 0.2);
  margin-right: 8px;
}

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

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

.icon-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* ===== Messages ===== */

.messages-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
  scroll-behavior: smooth;
}

.messages-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: auto;
}

.message {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  animation: messageIn 0.2s ease;
}

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

.message.own {
  align-self: flex-end;
  background: var(--msg-own);
  border-bottom-right-radius: 4px;
}

.message.peer {
  align-self: flex-start;
  background: var(--msg-peer);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.message-text {
  white-space: pre-wrap;
}

.message-meta {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-muted);
}

.system-message {
  align-self: center;
  font-size: 12px;
  color: var(--text-muted);
  padding: 6px 14px;
  background: var(--bg-tertiary);
  border-radius: 20px;
  max-width: 90%;
  text-align: center;
}

.typing-indicator {
  padding: 8px 0 0;
  font-size: 13px;
  color: var(--text-secondary);
}

.typing-dots span {
  animation: blink 1.4s infinite;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes blink {
  0%, 20% { opacity: 1; }
  50% { opacity: 0.2; }
  100% { opacity: 1; }
}

/* ===== Message Input ===== */

.message-input-container {
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.message-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.message-form textarea {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  resize: none;
  outline: none;
  max-height: 120px;
  transition: border-color var(--transition);
}

.message-form textarea:focus {
  border-color: var(--accent);
}

.message-form textarea::placeholder {
  color: var(--text-muted);
}

.send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent) !important;
  color: white !important;
  flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-hover) !important;
  box-shadow: 0 4px 16px var(--accent-glow);
}

.send-btn:disabled {
  background: var(--bg-tertiary) !important;
  color: var(--text-muted) !important;
}

/* ===== Hidden override ===== */

[hidden] {
  display: none !important;
}

/* ===== Call Overlay ===== */

.call-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg-primary);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.call-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  position: relative;
}

.call-status {
  text-align: center;
  z-index: 5;
}

.call-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  margin-bottom: 16px;
}

.call-status h3 {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 4px;
}

#call-state-text {
  color: var(--text-secondary);
  font-size: 14px;
}

#call-timer {
  font-size: 16px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  margin-top: 4px;
}

.call-videos {
  position: absolute;
  inset: 0;
  z-index: 1;
}

#remote-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
  display: none;
}

#local-video {
  position: absolute;
  bottom: 100px;
  right: 16px;
  width: 120px;
  height: 160px;
  object-fit: cover;
  border-radius: var(--radius);
  border: 2px solid var(--border);
  background: #000;
  z-index: 10;
  display: none;
}

.call-controls {
  position: absolute;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 16px;
  z-index: 20;
}

.call-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  border: none;
  border-radius: 50%;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  cursor: pointer;
  transition: all var(--transition);
}

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

.call-btn.muted {
  background: var(--red);
  color: white;
}

.call-btn-end {
  background: var(--red);
  color: white;
}

.call-btn-end:hover {
  background: #e8384f;
}

.call-btn-accept {
  background: var(--green);
  color: white;
}

.call-btn-accept:hover {
  background: #27c065;
}

/* ===== Incoming Call ===== */

.incoming-call-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.incoming-call-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 32px;
  text-align: center;
  min-width: 280px;
  animation: slideUp 0.3s ease;
}

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

.incoming-call-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  margin-bottom: 16px;
}

.incoming-call-card h3 {
  font-size: 20px;
  margin-bottom: 4px;
}

.incoming-call-card p {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 24px;
}

.incoming-call-actions {
  display: flex;
  justify-content: center;
  gap: 24px;
}

/* ===== Toasts ===== */

.toasts-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  color: var(--text-primary);
  font-size: 13px;
  max-width: 320px;
  animation: slideIn 0.3s ease;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

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

.toast.error {
  border-color: rgba(255, 71, 87, 0.3);
  background: rgba(255, 71, 87, 0.1);
}

.toast.success {
  border-color: rgba(46, 213, 115, 0.3);
  background: rgba(46, 213, 115, 0.1);
}

/* ===== Scrollbar ===== */

::-webkit-scrollbar {
  width: 6px;
}

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

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

::-webkit-scrollbar-thumb:hover {
  background: var(--bg-hover);
}

/* ===== Mobile ===== */

@media (max-width: 480px) {
  .message {
    max-width: 90%;
  }

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

  .header-right .badge {
    display: none;
  }

  #local-video {
    width: 90px;
    height: 120px;
    bottom: 90px;
    right: 12px;
  }

  .call-controls {
    bottom: 24px;
  }
}
