:root {
  --bg-primary: #17212b;
  --bg-secondary: #0e1621;
  --bg-sidebar: #17212b;
  --bg-input: #242f3d;
  --bg-message-own: #2b5278;
  --bg-message-other: #182533;
  --bg-hover: #202b36;
  --text-primary: #f5f5f5;
  --text-secondary: #7e919e;
  --text-link: #6ab2f2;
  --accent: #2AABEE;
  --accent-hover: #229ed9;
  --danger: #e53935;
  --border: #1c2a38;
  --shadow: rgba(0, 0, 0, 0.3);
  --radius: 12px;
  --radius-sm: 8px;
  --transition: 0.2s ease;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-secondary);
  color: var(--text-primary);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  width: 100%;
}

.screen {
  display: none;
  width: 100%;
  height: 100vh;
  height: 100dvh;
}

.screen.active {
  display: flex;
}

/* ===== Login ===== */
#gate-screen,
#login-screen {
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: var(--bg-secondary);
  overflow-y: auto;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
  padding: 40px 32px;
  text-align: center;
}

.login-logo {
  margin-bottom: 24px;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 8px;
}

.login-subtitle {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 28px;
}

/* Tab switcher */
.tab-switcher {
  display: flex;
  background: var(--bg-input);
  border-radius: var(--radius);
  padding: 4px;
  margin-bottom: 24px;
}

.tab-btn {
  flex: 1;
  padding: 10px 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: all var(--transition);
}

.tab-btn.active {
  background: var(--accent);
  color: white;
}

/* Auth forms */
.auth-form {
  display: none;
}

.auth-form.active {
  display: block;
}

.input-label {
  display: block;
  font-size: 12px;
  color: var(--text-secondary);
  text-align: left;
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.input-group {
  margin-bottom: 16px;
}

.input-group input {
  width: 100%;
  padding: 14px 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 15px;
  font-family: inherit;
  outline: none;
  transition: border-color var(--transition);
  -webkit-appearance: none;
  appearance: none;
}

.input-group input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.input-group input::placeholder {
  color: var(--text-secondary);
}

.btn-primary {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: white;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition);
  margin-top: 8px;
}

.btn-primary:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.error-text {
  color: var(--danger);
  font-size: 13px;
  margin-top: 12px;
  min-height: 20px;
}

/* ===== Modal ===== */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px 32px;
  text-align: center;
  max-width: 360px;
  width: 90%;
  animation: fadeIn 0.3s ease;
}

.modal-icon {
  margin-bottom: 16px;
}

.modal-content h3 {
  font-size: 20px;
  margin-bottom: 8px;
}

.modal-hint {
  color: var(--text-secondary);
  font-size: 13px;
  margin-bottom: 20px;
}

.room-id-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: var(--bg-input);
  border-radius: var(--radius-sm);
  padding: 14px 20px;
  margin-bottom: 20px;
}

.room-id-label {
  color: var(--text-secondary);
  font-size: 13px;
}

.room-id-value {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 3px;
  color: var(--accent);
}

/* ===== Chat Screen ===== */
#chat-screen {
  flex-direction: row;
  height: 100vh;
  height: 100dvh;
  width: 100%;
}

/* ===== Sidebar ===== */
.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.sidebar-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sidebar-header h2 {
  font-size: 17px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.room-info-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 20px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}

.room-info-id {
  font-size: 12px;
  color: var(--text-secondary);
}

.room-info-id span {
  color: var(--accent);
  font-weight: 600;
  letter-spacing: 1px;
}

.icon-btn-sm {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--transition);
}

.icon-btn-sm:hover {
  color: var(--accent);
}

.sidebar-section {
  padding: 12px 16px;
}

.sidebar-section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
  padding: 0 4px;
}

.user-list {
  list-style: none;
  max-height: calc(100vh - 340px);
  overflow-y: auto;
}

.user-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition);
  font-size: 14px;
}

.user-list li:hover {
  background: var(--bg-hover);
}

.user-list li .user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 14px;
  color: white;
  flex-shrink: 0;
}

.user-list li .user-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-list li .call-user-btn {
  opacity: 0;
  transition: opacity var(--transition);
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
}

.user-list li:hover .call-user-btn {
  opacity: 1;
}

.user-list li.is-me {
  opacity: 0.6;
  cursor: default;
}

.user-list li.is-me:hover {
  background: transparent;
}

.btn-secondary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}

.btn-secondary:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
}

.btn-leave {
  color: var(--danger);
  border-color: rgba(229, 57, 53, 0.3);
}

.btn-leave:hover {
  background: rgba(229, 57, 53, 0.1);
  border-color: var(--danger);
}

.full-width {
  width: 100%;
}

.sidebar-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}

.encryption-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
}

/* ===== Chat Main ===== */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-width: 0;
  overflow: hidden;
}

.chat-room-id-mobile {
  display: none;
}

.chat-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-primary);
  z-index: 10;
}

.chat-header-info {
  flex: 1;
}

.chat-header-info h3 {
  font-size: 16px;
  font-weight: 600;
}

.typing-indicator {
  font-size: 12px;
  color: var(--accent);
  min-height: 16px;
  display: block;
}

.chat-header-actions {
  display: flex;
  gap: 4px;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), color var(--transition);
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.search-bar {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-primary);
}

.search-bar input {
  flex: 1;
  padding: 8px 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  outline: none;
}

.search-bar input::placeholder {
  color: var(--text-secondary);
}

/* ===== Messages ===== */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--bg-secondary);
  scroll-behavior: smooth;
}

.messages::-webkit-scrollbar {
  width: 6px;
}

.messages::-webkit-scrollbar-track {
  background: transparent;
}

.messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.message {
  max-width: 65%;
  padding: 8px 14px;
  border-radius: 12px 12px 12px 4px;
  background: var(--bg-message-other);
  position: relative;
  word-wrap: break-word;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.own {
  align-self: flex-end;
  background: var(--bg-message-own);
  border-radius: 12px 12px 4px 12px;
}

.message .msg-author {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 2px;
  color: var(--accent);
}

.message.own .msg-author {
  display: none;
}

.message .msg-text {
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.message .msg-time {
  font-size: 11px;
  color: var(--text-secondary);
  text-align: right;
  margin-top: 4px;
}

.system-message {
  text-align: center;
  font-size: 13px;
  color: var(--text-secondary);
  padding: 8px 16px;
  align-self: center;
}

.message .msg-text a {
  color: var(--text-link);
  text-decoration: none;
}

.message .msg-text a:hover {
  text-decoration: underline;
}

.search-highlight {
  background: rgba(42, 171, 238, 0.3);
  border-radius: 2px;
  padding: 0 2px;
}

/* ===== Message Input ===== */
.message-input-area {
  padding: 10px 16px;
  background: var(--bg-primary);
  border-top: 1px solid var(--border);
}

.input-container {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  background: var(--bg-input);
  border-radius: var(--radius);
  padding: 4px 8px;
}

.input-container textarea {
  flex: 1;
  padding: 10px 8px;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  resize: none;
  max-height: 120px;
  line-height: 1.4;
}

.input-container textarea::placeholder {
  color: var(--text-secondary);
}

.send-btn {
  color: var(--accent) !important;
}

.send-btn:hover {
  color: var(--accent-hover) !important;
}

/* ===== Call Overlay ===== */
.call-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.95);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.call-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.call-header {
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  color: white;
  z-index: 10;
  display: flex;
  gap: 16px;
  align-items: center;
  background: rgba(0, 0, 0, 0.6);
  padding: 8px 20px;
  border-radius: 20px;
  font-size: 14px;
}

.call-videos {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-wrapper {
  position: relative;
}

.remote-video-wrapper {
  width: 100%;
  height: 100%;
}

.remote-video-wrapper video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.local-video-wrapper {
  position: absolute;
  bottom: 100px;
  right: 20px;
  width: 200px;
  height: 150px;
  border-radius: var(--radius);
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.2);
  z-index: 10;
}

.local-video-wrapper video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.video-name {
  position: absolute;
  bottom: 8px;
  left: 8px;
  background: rgba(0, 0, 0, 0.6);
  padding: 4px 10px;
  border-radius: 8px;
  font-size: 12px;
  color: white;
}

.call-controls {
  position: absolute;
  bottom: 30px;
  display: flex;
  gap: 16px;
  z-index: 10;
}

.call-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), transform var(--transition);
  backdrop-filter: blur(8px);
}

.call-btn:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: scale(1.05);
}

.call-btn.active {
  background: var(--accent);
}

.call-btn.muted {
  background: var(--danger);
}

.call-btn-end {
  background: var(--danger) !important;
}

.call-btn-end:hover {
  background: #c62828 !important;
}

.call-btn-accept {
  background: #43a047 !important;
}

.call-btn-accept:hover {
  background: #2e7d32 !important;
}

/* ===== Incoming Call ===== */
.incoming-call {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  z-index: 1100;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(8px);
}

.incoming-call-content {
  text-align: center;
  animation: fadeIn 0.3s ease;
}

.incoming-call-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: var(--bg-input);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 16px;
  animation: pulse 1.5s ease-in-out infinite;
}

.incoming-call-name {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 4px;
}

.incoming-call-type {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 32px;
}

.incoming-call-actions {
  display: flex;
  gap: 40px;
  justify-content: center;
}

/* ===== Group Call ===== */
.group-call-container {
  position: relative;
}

.group-call-videos {
  display: grid;
  gap: 8px;
  padding: 20px;
  width: 100%;
  height: calc(100% - 100px);
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  grid-auto-rows: 1fr;
}

.group-call-videos .video-wrapper {
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-input);
  position: relative;
}

.group-call-videos .video-wrapper video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* ===== Emoji Picker ===== */
.emoji-picker {
  position: absolute;
  bottom: 70px;
  right: 20px;
  width: 320px;
  max-height: 300px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px var(--shadow);
  overflow-y: auto;
  z-index: 100;
}

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  padding: 8px;
  gap: 2px;
}

.emoji-grid button {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  font-size: 22px;
  cursor: pointer;
  border-radius: 6px;
  transition: background var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
}

.emoji-grid button:hover {
  background: var(--bg-hover);
}

/* ===== Hidden ===== */
.hidden {
  display: none !important;
}

/* ===== Scrollbar ===== */
.user-list::-webkit-scrollbar,
.emoji-picker::-webkit-scrollbar {
  width: 4px;
}

.user-list::-webkit-scrollbar-thumb,
.emoji-picker::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* ===== Image ===== */
.image-preview {
  max-width: 100%;
  border-radius: 8px;
  margin-top: 4px;
  cursor: pointer;
}

.image-preview:hover {
  opacity: 0.9;
}

/* ===== Mobile ===== */
.mobile-only {
  display: none;
}

@media (max-width: 768px) {
  .mobile-only {
    display: flex;
  }

  body {
    height: 100vh;
    height: 100dvh;
  }

  #chat-screen {
    height: 100vh;
    height: 100dvh;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: -100%;
    width: 280px;
    height: 100vh;
    height: 100dvh;
    z-index: 500;
    transition: left 0.3s ease;
  }

  .sidebar.open {
    left: 0;
  }

  .chat-main {
    height: 100vh;
    height: 100dvh;
  }

  .chat-header {
    padding: 8px 12px;
    flex-shrink: 0;
  }

  .chat-header-info h3 {
    font-size: 15px;
  }

  .chat-room-id-mobile {
    display: block;
    font-size: 11px;
    color: var(--text-secondary);
  }

  .messages {
    flex: 1;
    min-height: 0;
    padding: 10px 12px;
  }

  .message {
    max-width: 85%;
  }

  .message-input-area {
    padding: 6px 8px;
    flex-shrink: 0;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
  }

  .input-container {
    padding: 2px 6px;
  }

  .input-container textarea {
    padding: 8px 6px;
    font-size: 16px;
  }

  .local-video-wrapper {
    width: 120px;
    height: 90px;
    bottom: 120px;
    right: 12px;
  }

  .emoji-picker {
    width: 280px;
    right: 8px;
    bottom: 60px;
  }

  .group-call-videos {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    padding: 8px;
  }

  .login-container {
    padding: 24px 16px;
  }

  .login-logo svg {
    width: 48px;
    height: 48px;
  }

  .login-title {
    font-size: 24px;
  }

  .login-subtitle {
    margin-bottom: 20px;
  }

  .input-group {
    margin-bottom: 12px;
  }

  .input-group input {
    padding: 12px 14px;
    font-size: 16px;
  }

  .btn-primary {
    padding: 12px;
  }
}

@media (max-width: 480px) {
  .login-logo {
    margin-bottom: 16px;
  }

  .tab-switcher {
    margin-bottom: 16px;
  }
}
