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

:root {
  --primary-color: #6366f1;
  --primary-dark: #4f46e5;
  --secondary-color: #8b5cf6;
  --bg-color: #0f172a;
  --bg-light: #1e293b;
  --bg-lighter: #334155;
  --text-primary: #f1f5f9;
  --text-secondary: #cbd5e1;
  --border-color: #334155;
  --success-color: #10b981;
  --system-color: #64748b;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background: var(--bg-color);
  color: var(--text-primary);
  height: 100vh;
  height: 100dvh; /* Dynamic viewport height for mobile */
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  /* Safe area insets for iOS */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.chat-container {
  display: flex;
  height: 100vh;
  max-width: 1400px;
  margin: 0 auto;
}

/* Sidebar */
.sidebar {
  width: 280px;
  background: var(--bg-light);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-header h2 {
  font-size: 1.25rem;
  font-weight: 600;
}

.room-section {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

.current-room {
  background: var(--bg-lighter);
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.room-label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-bottom: 5px;
}

.room-name {
  display: block;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--primary-color);
  margin-bottom: 5px;
}

.user-count {
  display: block;
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.user-names-list {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 150px;
  overflow-y: auto;
}

.user-name-item {
  font-size: 0.85rem;
  color: var(--text-secondary);
  padding: 4px 8px;
  background: var(--bg-color);
  border-radius: 4px;
}

.room-input-section {
  margin-bottom: 20px;
}

.room-input-section input {
  width: 100%;
  padding: 10px;
  background: var(--bg-lighter);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 0.9rem;
  margin-bottom: 10px;
}

.room-input-section input:focus {
  outline: none;
  border-color: var(--primary-color);
}

.active-rooms h3 {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.rooms-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.room-item {
  padding: 10px 12px;
  background: var(--bg-lighter);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: manipulation;
}

.room-item:hover {
  background: var(--primary-color);
  transform: translateX(3px);
}

.room-item.active {
  background: var(--primary-color);
  border: 1px solid var(--primary-dark);
}

.room-item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.room-leave-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  flex-shrink: 0;
}

.room-leave-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  color: var(--text-primary);
  transform: scale(1.1);
}

.room-item:hover .room-leave-btn {
  color: var(--text-primary);
}

.no-rooms {
  padding: 10px;
  color: var(--text-secondary);
  font-size: 0.85rem;
  font-style: italic;
}

.user-info {
  padding: 15px 20px;
  border-top: 1px solid var(--border-color);
  background: var(--bg-lighter);
  font-size: 0.9rem;
  color: var(--text-secondary);
}

/* Main Chat */
.main-chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-color);
}

.chat-header {
  padding: 20px 30px;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-light);
}

.chat-header h1 {
  font-size: 1.5rem;
  margin-bottom: 5px;
}

.room-info {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.room-info span {
  color: var(--primary-color);
  font-weight: 600;
}

.message-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px 30px;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.message {
  display: flex;
  animation: slideIn 0.3s ease-out;
}

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

.message.self {
  justify-content: flex-end;
}

.message.other {
  justify-content: flex-start;
}

.message.system {
  justify-content: center;
}

.message-content {
  max-width: 70%;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--bg-light);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.message.self .message-content {
  background: var(--primary-color);
  border-bottom-right-radius: 4px;
}

.message.other .message-content {
  background: var(--bg-lighter);
  border-bottom-left-radius: 4px;
}

.system-message {
  background: transparent !important;
  color: var(--system-color);
  font-size: 0.85rem;
  font-style: italic;
  text-align: center;
  padding: 8px 0;
}

.message-name {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 2px;
}

.message.self .message-name {
  color: rgba(255, 255, 255, 0.8);
}

.message-text {
  font-size: 0.95rem;
  line-height: 1.4;
  word-wrap: break-word;
}

.message-time {
  font-size: 0.7rem;
  color: var(--text-secondary);
  margin-top: 4px;
  align-self: flex-end;
}

.message.self .message-time {
  color: rgba(255, 255, 255, 0.7);
}

.input-container {
  padding: 20px 30px;
  border-top: 1px solid var(--border-color);
  background: var(--bg-light);
}

#send-container {
  display: flex;
  gap: 10px;
  align-items: center;
}

#message-input {
  flex: 1;
  padding: 12px 16px;
  background: var(--bg-lighter);
  border: 1px solid var(--border-color);
  border-radius: 24px;
  color: var(--text-primary);
  font-size: 0.95rem;
  transition: all 0.2s;
  -webkit-appearance: none; /* Remove iOS input styling */
  appearance: none;
  touch-action: manipulation;
}

#message-input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.btn {
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  -webkit-tap-highlight-color: transparent; /* Remove tap highlight on mobile */
  user-select: none; /* Prevent text selection on buttons */
  touch-action: manipulation; /* Improve touch response */
}

.btn-primary {
  background: var(--primary-color);
  color: white;
}

.btn-primary:hover {
  background: var(--primary-dark);
  transform: translateY(-1px);
}

.btn-send {
  background: var(--primary-color);
  color: white;
  padding: 12px;
  border-radius: 50%;
  width: 44px;
  height: 44px;
}

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

.btn:active {
  transform: translateY(0) scale(0.98);
}

/* Modal */
.modal {
  display: flex;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(15, 23, 42, 0.9);
  backdrop-filter: blur(4px);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.modal-content {
  background: var(--bg-light);
  padding: 40px;
  border-radius: 16px;
  border: 1px solid var(--border-color);
  max-width: 400px;
  width: 90%;
  text-align: center;
}

.modal-content h2 {
  margin-bottom: 10px;
  font-size: 1.5rem;
}

.modal-content p {
  color: var(--text-secondary);
  margin-bottom: 20px;
}

.modal-content input {
  width: 100%;
  padding: 12px 16px;
  background: var(--bg-lighter);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 1rem;
  margin-bottom: 20px;
}

.modal-content input:focus {
  outline: none;
  border-color: var(--primary-color);
}

.modal-content .btn {
  width: 100%;
  padding: 12px;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: var(--bg-color);
}

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

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

/* Responsive */
/* Mobile Optimizations */
@media (max-width: 768px) {
  body {
    padding: 0;
  }

  .chat-container {
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-width: 100%;
  }

  .sidebar {
    width: 100%;
    height: auto;
    max-height: 50vh;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
    position: relative;
  }

  .sidebar-header {
    padding: 15px 20px;
  }

  .sidebar-header h2 {
    font-size: 1.1rem;
  }

  .room-section {
    padding: 15px;
  }

  .current-room {
    padding: 12px;
    margin-bottom: 15px;
  }

  .room-name {
    font-size: 1rem;
  }

  .user-count {
    font-size: 0.8rem;
  }

  .room-input-section input {
    padding: 12px;
    font-size: 16px; /* Prevents zoom on iOS */
  }

  .btn {
    padding: 12px 16px;
    min-height: 44px; /* Minimum touch target size */
    font-size: 0.95rem;
  }

  .btn-primary {
    width: 100%;
  }

  .main-chat {
    flex: 1;
    min-height: 0; /* Allows flex shrinking */
  }

  .chat-header {
    padding: 15px 20px;
  }

  .chat-header h1 {
    font-size: 1.25rem;
  }

  .message-container {
    padding: 15px 20px;
    gap: 12px;
    /* Better scrolling on mobile */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .message-content {
    max-width: 85%;
    padding: 10px 14px;
  }

  .message-text {
    font-size: 0.9rem;
    line-height: 1.5;
  }

  .message-name {
    font-size: 0.7rem;
  }

  .message-time {
    font-size: 0.65rem;
  }

  .input-container {
    padding: 15px 20px;
    /* Safe area for iOS home indicator */
    padding-bottom: calc(15px + env(safe-area-inset-bottom));
  }

  #message-input {
    padding: 14px 16px;
    font-size: 16px; /* Prevents zoom on iOS */
    min-height: 44px;
    border-radius: 22px;
  }

  .btn-send {
    width: 44px;
    height: 44px;
    padding: 0;
  }

  .user-names-list {
    max-height: 100px;
  }

  .user-name-item {
    font-size: 0.8rem;
    padding: 6px 10px;
  }

  .rooms-list {
    gap: 8px;
  }

  .room-item {
    padding: 12px;
    min-height: 44px;
  }

  .room-leave-btn {
    width: 28px;
    height: 28px;
    font-size: 1.3rem;
  }

  /* Modal improvements for mobile */
  .modal-content {
    padding: 30px 25px;
    max-width: 90%;
    margin: 20px;
  }

  .modal-content h2 {
    font-size: 1.3rem;
  }

  .modal-content input {
    padding: 14px 16px;
    font-size: 16px; /* Prevents zoom on iOS */
    min-height: 44px;
  }

  .modal-content .btn {
    width: 100%;
    padding: 14px;
  }
}

/* Small mobile devices */
@media (max-width: 480px) {
  .sidebar-header {
    padding: 12px 15px;
  }

  .sidebar-header h2 {
    font-size: 1rem;
  }

  .room-section {
    padding: 12px 15px;
  }

  .current-room {
    padding: 10px;
  }

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

  .chat-header h1 {
    font-size: 1.1rem;
  }

  .message-container {
    padding: 12px 15px;
  }

  .message-content {
    max-width: 90%;
    padding: 8px 12px;
  }

  .input-container {
    padding: 12px 15px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
  }

  .modal-content {
    padding: 25px 20px;
  }
}

/* Landscape mobile */
@media (max-width: 768px) and (orientation: landscape) {
  .sidebar {
    max-height: 100vh;
    max-height: 100dvh;
    overflow-y: auto;
  }

  .room-section {
    max-height: calc(100vh - 200px);
    max-height: calc(100dvh - 200px);
    overflow-y: auto;
  }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
  /* Better touch targets */
  .btn:active {
    transform: scale(0.95);
    opacity: 0.8;
  }

  .room-item:active {
    transform: scale(0.98);
  }

  /* Remove hover effects on touch devices */
  .room-item:hover {
    transform: none;
  }

  .btn:hover {
    transform: none;
  }
}

