:root {
  --bg-primary: #f5f5f5;
  --bg-secondary: white;
  --bg-tertiary: #f8f8f8;
  --bg-accent: #E5E5EA;
  --text-primary: #333;
  --text-secondary: #666;
  --text-tertiary: #999;
  --text-accent: white;
  --border-color: #e0e0e0;
  --border-secondary: #d0d0d0;
  --blue-primary: #007AFF;
  --blue-secondary: #0051D5;
  --green: #34C759;
  --red: #FF3B30;
  --orange: #FF9500;
}

.dark-mode {
  --bg-primary: #1a1a1a;
  --bg-secondary: #2a2a2a;
  --bg-tertiary: #3a3a3a;
  --bg-accent: #404040;
  --text-primary: #e0e0e0;
  --text-secondary: #b0b0b0;
  --text-tertiary: #888;
  --text-accent: #e0e0e0;
  --border-color: #444;
  --border-secondary: #555;
  --blue-primary: #0A84FF;
  --blue-secondary: #0066CC;
  --green: #30D158;
  --red: #FF453A;
  --orange: #FF9F0A;
}

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

html,
body {
  height: 100%;
  width: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
  background: var(--bg-primary);
  overflow: hidden;
  position: fixed;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  background: var(--bg-secondary);
  position: relative;
  overflow: hidden;
}

/* Fixed Header */
.header {
  position: relative;
  z-index: 100;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
  gap: 12px;
}

.soft-back-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-primary);
  opacity: 0.6;
  transition: opacity 0.2s;
  padding: 8px;
  border-radius: 50%;
  margin-right: -8px;
}

.soft-back-btn:hover {
  opacity: 1;
  background-color: var(--bg-tertiary);
}

.dark-mode .soft-back-btn {
  opacity: 0.9;
}

.dark-mode .soft-back-btn:hover {
  opacity: 1;
  background-color: var(--bg-accent);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.input {
  -webkit-appearance: none;
  appearance: none;
  -moz-appearance: none;
}

.header-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-primary);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-logo {
  width: 28px;
  height: 28px;
  display: block;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.theme-toggle {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 6px;
  border-radius: 50%;
  transition: background-color 0.2s;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.theme-toggle:hover {
  background: rgba(0, 0, 0, 0.1);
}

.dark-mode .theme-toggle:hover {
  background: rgba(255, 255, 255, 0.1);
}

.online-count {
  font-size: 14px;
  font-weight: normal;
  color: var(--text-secondary);
  background: var(--bg-tertiary);
  padding: 4px 12px;
  border-radius: 12px;
  white-space: nowrap;
}

/* Chat Content Area */
.chat-screen {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
}

.status {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 20px;
  font-size: 16px;
  color: var(--text-secondary);
  gap: 10px;
}

/* Scrollable Messages */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  min-height: 0;
}

/* Inner wrapper to align messages to bottom */
.chat-messages::before {
  content: '';
  flex: 1;
  min-height: 0;
}

.chat-messages {
  display: flex;
  flex-direction: column;
}

/* Messages wrapper - keeps typing indicator at bottom */
.chat-messages>.message,
.chat-messages>.system-message,
.chat-messages>.typing-indicator {
  flex-shrink: 0;
}

.message {
  margin-bottom: 12px;
  padding: 10px 14px;
  border-radius: 16px;
  max-width: 75%;
  word-wrap: break-word;
  word-break: break-word;
  line-height: 1.4;
  font-size: 15px;
  animation: messageSlideIn 0.2s ease-out;
}

@keyframes messageSlideIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message-sender {
  font-weight: bold;
  margin-bottom: 4px;
  font-size: 13px;
}

.message-time {
  font-size: 11px;
  opacity: 0.6;
  margin-top: 4px;
}

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

.message-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  font-size: 11px;
  opacity: 0.7;
}

.message-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.message-status.pending::before {
  content: '🕒';
  font-size: 11px;
}

.message-status.sent::before {
  content: '✓';
  font-size: 11px;
}

.message.own {
  background: var(--blue-primary);
  color: var(--text-accent);
  margin-left: auto;
  border-bottom-right-radius: 4px;
}

.message.other {
  background: var(--bg-accent);
  color: var(--text-primary);
  margin-right: auto;
  border-bottom-left-radius: 4px;
}

.system-message {
  text-align: center;
  color: var(--text-tertiary);
  font-size: 13px;
  padding: 8px 16px;
  margin: 12px auto;
  max-width: 80%;
  line-height: 1.4;
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

.typing-indicator {
  padding: 10px 14px;
  border-radius: 16px;
  max-width: 75%;
  background: var(--bg-accent);
  color: var(--text-primary);
  margin-right: auto;
  border-bottom-left-radius: 4px;
  font-size: 15px;
  animation: fadeIn 0.2s ease-out;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.typing-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.typing-dots {
  display: flex;
  gap: 4px;
}

.typing-dots span {
  width: 8px;
  height: 8px;
  background: var(--text-secondary);
  border-radius: 50%;
  animation: typing 1.4s infinite ease-in-out;
}

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

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

.typing-text {
  color: var(--text-secondary);
  font-size: 14px;
}

@keyframes typing {

  0%,
  80%,
  100% {
    transform: scale(0.8);
    opacity: 0.5;
  }

  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* Unread Messages Notification */
.unread-notification {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--blue-primary);
  color: var(--text-accent);
  padding: 10px 20px;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(0, 122, 255, 0.4);
  z-index: 50;
  cursor: pointer;
  transition: all 0.3s ease;
  animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

.unread-notification:hover {
  background: var(--blue-secondary);
  box-shadow: 0 6px 16px rgba(0, 122, 255, 0.5);
}

.unread-notification:active {
  transform: translateX(-50%) scale(0.95);
}

.unread-notification button {
  background: none;
  border: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

#unreadCount {
  font-weight: 600;
}

/* Bottom Section - Fixed positioning for keyboard stability */
.bottom-section {
  position: relative;
  z-index: 100;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
  padding-bottom: env(safe-area-inset-bottom);
}

/* Input Area */
.input-area {
  padding: 6px 8px;
  display: flex;
  gap: 10px;
  align-items: center;
  background: var(--bg-secondary);
}

#messageInput {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border-secondary);
  border-radius: 20px;
  font-size: 15px;
  outline: none;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  resize: none;
  max-height: 100px;
}

#messageInput:focus {
  background: var(--bg-secondary);
  border-color: var(--blue-primary);
}

.input-area button {
  padding: 10px 20px;
  background: var(--blue-primary);
  color: var(--text-accent);
  border: none;
  border-radius: 20px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  transition: opacity 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  display: flex;
  justify-content: center;
  align-items: center;
}

.input-area button:active,
.input-area button.pressed {
  opacity: 0.8;
  transform: scale(0.97);
}

/* Action Buttons */
.action-buttons {
  padding: 0px 16px 8px 16px;
  display: flex;
  gap: 10px;
  justify-content: center;
  background: var(--bg-secondary);
}

.action-buttons button {
  flex: 1;
  padding: 8px 20px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  display: flex;
  justify-content: center;
  align-items: center;
}

.action-buttons button:active,
.action-buttons button.pressed {
  opacity: 0.7;
  transform: scale(0.97);
}

.action-buttons button.confirming {
  animation: pulse 0.5s ease-in-out;
  box-shadow: 0 0 0 3px rgba(255, 59, 48, 0.3);
}

@keyframes pulse {

  0%,
  100% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.05);
  }
}


.rematch,
.input-area .rematch {
  background: var(--blue-primary);
  color: var(--text-accent);
  text-align: center;
}

.stop,
.input-area .stop {
  background: var(--red);
  color: var(--text-accent);
  text-align: center;
}

.start,
.input-area .start {
  background: var(--green);
  color: var(--text-accent);
  text-align: center;
}

/* Connection Status */
.connection-status {
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 500;
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.connection-status.connecting {
  background: var(--orange);
  color: var(--text-accent);
}

.connection-status.connected {
  background: var(--green);
  color: var(--text-accent);
}

.connection-status.disconnected {
  background: var(--red);
  color: var(--text-accent);
}

.spinner {
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid var(--text-accent);
  border-radius: 50%;
  width: 14px;
  height: 14px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

/* Desktop Optimizations - Show more content */
@media (min-width: 768px) {
  .message {
    font-size: 14px;
    padding: 9px 12px;
  }

  .chat-messages {
    padding: 16px 20px;
  }

  .message-sender {
    font-size: 12px;
  }

  .system-message {
    font-size: 12px;
  }

  .status {
    padding: 30px 20px;
    font-size: 15px;
  }
}

/* Mobile Optimizations */
@media (max-width: 480px) {
  .header {
    padding: 12px 12px;
    gap: 8px;
  }

  .header-left {
    gap: 2px;
    min-width: 0;
  }

  .header-title {
    font-size: 16px;
  }

  .header-logo {
    width: 24px;
    height: 24px;
  }

  .soft-back-btn {
    padding: 6px;
    margin-right: 0;
  }

  .header-right {
    gap: 6px;
    flex-shrink: 0;
  }

  .online-count {
    font-size: 13px;
    padding: 3px 10px;
  }

  .connection-status {
    font-size: 11px;
    padding: 4px 8px;
  }

  /* Hide connection text on mobile to save space */
  #connectionText {
    display: none;
  }

  .chat-messages {
    padding: 16px;
  }

  .message {
    font-size: 14px;
    max-width: 80%;
  }

  .unread-notification {
    bottom: 12px;
    font-size: 13px;
    padding: 8px 16px;
  }
}

/* Landscape mode on mobile */
@media (max-height: 500px) and (orientation: landscape) {
  .header {
    padding: 8px 16px;
  }

  .input-area,
  .action-buttons {
    padding: 8px 16px;
  }

  .action-buttons button {
    padding: 8px 16px;
  }
}

/* iOS Keyboard handling */
@supports (-webkit-touch-callout: none) {
  .container {
    height: -webkit-fill-available;
  }
}

/* Settings Button */
.settings-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: 50%;
  transition: background-color 0.2s, color 0.2s;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

.settings-btn:hover {
  background: rgba(0, 0, 0, 0.05);
  color: var(--text-primary);
}

.dark-mode .settings-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* Settings Modal */
.settings-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: fadeIn 0.2s ease-out;
}

.settings-content {
  background: var(--bg-secondary);
  border-radius: 20px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  animation: scaleIn 0.2s ease-out;
}

@keyframes scaleIn {
  from {
    transform: scale(0.95);
    opacity: 0;
  }

  to {
    transform: scale(1);
    opacity: 1;
  }
}

.settings-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.settings-header h3 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--text-secondary);
  cursor: pointer;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  line-height: 1;
}

.close-btn:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.settings-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.setting-item.column {
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}

.setting-item label {
  font-size: 15px;
  color: var(--text-primary);
}

.settings-input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border-secondary);
  border-radius: 12px;
  font-size: 14px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  outline: none;
}

.settings-input:focus {
  border-color: var(--blue-primary);
  background: var(--bg-secondary);
}

/* Switch Toggle */
.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 28px;
  flex-shrink: 0;
}

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

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--bg-accent);
  transition: .4s;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 22px;
  width: 22px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

input:checked+.slider {
  background-color: var(--green);
}

input:checked+.slider:before {
  transform: translateX(22px);
}

.dark-mode .slider {
  background-color: #444;
}

.dark-mode .slider:before {
  background-color: #e0e0e0;
}

.dark-mode input:checked+.slider:before {
  background-color: white;
}