:root {
  --bg: #0f0f0f;
  --panel: #1a1a1a;
  --text: #fff;
  --accent: #2ea6ff;
  --border: #333;
  --hover: #2a2a2a;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvb;
  overflow: hidden;
  position: fixed;
  width: 100%;
  touch-action: pan-y pinch-zoom;
}

/* ===== ВХОД/РЕГИСТРАЦИЯ ===== */
.auth-body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  height: 100dvb;
  background: linear-gradient(135deg, #1a1a1a 0%, #000 100%);
  padding: 20px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.auth-card {
  background: var(--panel);
  padding: 40px 30px;
  border-radius: 20px;
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: 0 20px 40px rgba(0,0,0,0.5);
  border: 1px solid var(--border);
  margin: auto;
}

.auth-card h1 {
  text-align: center;
  color: var(--accent);
  font-size: clamp(28px, 8vw, 32px);
  margin-bottom: 10px;
}

.auth-card input {
  width: 100%;
  padding: 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #111;
  color: white;
  font-size: 16px;
  outline: none;
}

.auth-card input:focus {
  border-color: var(--accent);
}

.auth-card button {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.switch {
  text-align: center;
  color: #888;
  font-size: 14px;
}

.switch span {
  color: var(--accent);
  cursor: pointer;
  font-weight: 500;
}

#msg {
  color: #f44;
  text-align: center;
  font-size: 14px;
  min-height: 20px;
}

/* ===== ЧАТ ===== */
.app {
  display: flex;
  height: 100vh;
  height: 100dvb;
  width: 100vw;
  overflow: hidden;
  position: relative;
}

.sidebar {
  width: 320px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
  height: 100%;
  overflow: hidden;
  flex-shrink: 0;
  z-index: 10;
}

.user-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 20px;
  color: white;
  flex-shrink: 0;
}

.username-display {
  font-size: 18px;
  font-weight: 600;
  color: white;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.actions-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#search {
  flex: 1;
  min-width: 0;
  padding: 10px 16px;
  border-radius: 25px;
  border: 1px solid var(--border);
  background: #111;
  color: white;
  font-size: 14px;
  outline: none;
  height: 40px;
}

#search:focus {
  border-color: var(--accent);
}

.logout-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 20px;
  background: #f44;
  color: white;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

#chatList {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  border-left: 3px solid transparent;
  min-height: 60px;
}

.chat-item:hover {
  background: var(--hover);
}

.chat-item.active {
  background: rgba(46, 166, 255, 0.15);
  border-left: 3px solid var(--accent);
}

.chat-avatar {
  width: 45px;
  height: 45px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 16px;
  color: white;
  flex-shrink: 0;
}

.chat-info {
  flex: 1;
  overflow: hidden;
}

.chat-name {
  font-weight: 600;
  font-size: 15px;
  margin-bottom: 4px;
  color: white;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-preview {
  font-size: 13px;
  color: #888;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-time {
  font-size: 11px;
  color: #666;
  margin-left: auto;
  white-space: nowrap;
  flex-shrink: 0;
}

.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #0a0a0a;
  height: 100%;
  overflow: hidden;
  min-width: 0;
  position: relative;
}

.chat-header {
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 18px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  min-height: 60px;
  z-index: 5;
}

.mobile-menu-btn {
  display: none;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  width: 40px;
  height: 40px;
  color: white;
  font-size: 20px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
}

.message {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 15px;
  word-wrap: break-word;
}

.message.me {
  background: var(--accent);
  align-self: flex-end;
}

.message.other {
  background: #2a2a2a;
  align-self: flex-start;
}

.message-time {
  font-size: 10px;
  opacity: 0.7;
  text-align: right;
  margin-top: 4px;
}

.message-date {
  text-align: center;
  color: #888;
  font-size: 12px;
  margin: 10px 0;
  width: 100%;
}

.message-input {
  display: flex;
  padding: 10px 12px;
  gap: 8px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 65px;
  z-index: 5;
  transition: transform 0.2s ease;
}

.message-input input {
  flex: 1;
  min-width: 0;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 30px;
  background: #111;
  color: white;
  font-size: 16px;
  outline: none;
}

.message-input input:focus {
  border-color: var(--accent);
}

.message-input button {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.7);
  z-index: 2000;
}

.modal-content {
  background: var(--panel);
  margin: 15% auto;
  padding: 20px;
  width: 90%;
  max-width: 400px;
  border-radius: 16px;
  border: 1px solid var(--border);
  max-height: 70vh;
  overflow-y: auto;
}

.modal-content h3 {
  margin-bottom: 15px;
  color: var(--accent);
}

.close {
  float: right;
  font-size: 24px;
  cursor: pointer;
  color: #888;
}

.close:hover {
  color: white;
}

.search-result-item {
  padding: 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}

.search-result-item:hover {
  background: var(--hover);
}

/* ===== АДАПТАЦИЯ ===== */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: -320px;
    top: 0;
    height: 100%;
    z-index: 1000;
    width: 280px;
    box-shadow: 2px 0 10px rgba(0,0,0,0.5);
  }

  .sidebar.active {
    left: 0;
  }

  .mobile-menu-btn {
    display: flex;
  }

  .message {
    max-width: 85%;
  }

  .auth-card {
    padding: 30px 20px;
    margin: 20px auto;
  }

  .chat-header {
    padding: 10px 12px;
    min-height: 55px;
  }

  .message-input {
    padding: 8px 10px;
    min-height: 60px;
  }

  .message-input input {
    padding: 10px 14px;
    font-size: 16px;
  }

  .message-input button {
    width: 44px;
    height: 44px;
  }

  .chat-item {
    padding: 10px 15px;
    min-height: 55px;
  }

  .chat-avatar {
    width: 40px;
    height: 40px;
    font-size: 15px;
  }

  .avatar {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }

  .actions-row {
    padding: 10px 15px;
  }

  #search {
    padding: 8px 12px;
    height: 38px;
    font-size: 14px;
  }

  .logout-btn {
    padding: 6px 12px;
    height: 38px;
    font-size: 13px;
  }

  .username-display {
    font-size: 16px;
  }

  .user-row {
    padding: 12px 15px;
  }
}

@media (max-width: 480px) {
  .sidebar {
    width: 260px;
  }
  
  .message {
    max-width: 90%;
  }
  
  .logout-btn {
    padding: 5px 10px;
    font-size: 12px;
  }
  
  .message-input input {
    padding: 8px 12px;
  }
  
  .message-input button {
    width: 40px;
    height: 40px;
  }
}

/* Исправление для iOS */
@supports (-webkit-touch-callout: none) {
  .app, .auth-body {
    height: -webkit-fill-available;
  }
}

::-webkit-scrollbar {
  width: 4px;
}

::-webkit-scrollbar-thumb {
  background: #444;
    border-radius: 4px;
}