:root {
  --bg-primary: #313338;
  --bg-secondary: #2b2d31;
  --bg-tertiary: #1e1f22;
  --bg-modifier: #35373c;
  --bg-hover: #3f4147;
  --bg-active: #404249;
  --accent: #5865f2;
  --accent-hover: #4752c4;
  --text-primary: #f2f3f5;
  --text-secondary: #b5bac1;
  --text-muted: #949ba4;
  --danger: #da373c;
  --success: #23a559;
  --radius: 8px;
  --radius-sm: 4px;
  --shadow: 0 8px 16px rgba(0, 0, 0, 0.24);
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --header-h: 56px;
  --input-h: 60px;
  --userbar-h: 56px;
}

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

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  height: 100%;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}

.hidden { display: none !important; }

/* Auth */
.auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  background: linear-gradient(135deg, #5865f2 0%, #7289da 50%, #99aab5 100%);
  padding: 20px;
  padding-top: calc(20px + var(--safe-top));
  padding-bottom: calc(20px + var(--safe-bottom));
}

.auth-card {
  background: var(--bg-primary);
  border-radius: var(--radius);
  padding: 28px 24px;
  width: 100%;
  max-width: 480px;
  box-shadow: var(--shadow);
}

.auth-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: center;
  margin-bottom: 8px;
}

.auth-logo h1 { font-size: 24px; font-weight: 700; }

.auth-subtitle {
  text-align: center;
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 24px;
}

.auth-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
}

.auth-tab {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font-family: var(--font);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}

.auth-tab.active {
  background: var(--bg-modifier);
  color: var(--text-primary);
}

.form-group { margin-bottom: 16px; }

.form-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.form-group input {
  width: 100%;
  padding: 14px 12px;
  background: var(--bg-tertiary);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 16px;
  outline: none;
}

.form-group input:focus { border-color: var(--accent); }

.form-error {
  color: var(--danger);
  font-size: 13px;
  min-height: 20px;
  margin-bottom: 8px;
}

.btn {
  padding: 12px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}

.btn-primary { background: var(--accent); color: white; }
.btn-secondary { background: var(--bg-modifier); color: var(--text-primary); }
.btn-full { width: 100%; padding: 14px; font-size: 16px; }
.btn-sm { padding: 8px 14px; font-size: 13px; }
.btn-success { background: var(--success); color: white; }
.btn-danger { background: transparent; color: var(--text-muted); }

/* App layout */
.app {
  display: flex;
  height: 100dvh;
  height: -webkit-fill-available;
  min-height: 0;
}

.sidebar {
  width: 100%;
  max-width: 320px;
  background: var(--bg-secondary);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--bg-tertiary);
  min-height: 0;
  padding-top: var(--safe-top);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  min-height: var(--header-h);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
  flex-shrink: 0;
}

.sidebar-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
}

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.sidebar-section { padding: 8px 12px 0; flex-shrink: 0; }
.sidebar-section-grow { flex: 1; min-height: 0; overflow-y: auto; }

.section-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  padding: 8px 4px 6px;
}

.friend-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.friend-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius);
  cursor: pointer;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--font);
  font-size: 16px;
  width: 100%;
  text-align: left;
  min-height: 52px;
  -webkit-tap-highlight-color: transparent;
}

.friend-item:active { background: var(--bg-modifier); }
.friend-item.active { background: var(--bg-active); color: var(--text-primary); }
.friend-item.pending-item { cursor: default; flex-wrap: wrap; }

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

.unread-badge {
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  background: var(--danger);
  color: white;
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.pending-actions {
  display: flex;
  gap: 8px;
  width: 100%;
  padding-left: 44px;
  margin-top: 6px;
}

.pending-actions .btn { min-height: 36px; }

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
  color: white;
  flex-shrink: 0;
  text-transform: uppercase;
}

.user-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-bottom));
  background: var(--bg-tertiary);
  flex-shrink: 0;
  min-height: var(--userbar-h);
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  color: white;
  flex-shrink: 0;
  text-transform: uppercase;
}

.user-name {
  flex: 1;
  font-size: 15px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:active { background: var(--bg-modifier); color: var(--text-primary); }

/* Chat area */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  min-width: 0;
  min-height: 0;
}

.empty-state {
  flex: 1;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  padding: 40px;
  text-align: center;
}

.empty-state h2 {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding-top: var(--safe-top);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  min-height: var(--header-h);
  border-bottom: 1px solid var(--bg-tertiary);
  flex-shrink: 0;
  background: var(--bg-primary);
}

.back-btn { display: none; }

.chat-header-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  color: white;
  flex-shrink: 0;
}

.chat-header-name {
  flex: 1;
  font-size: 17px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
}

.message-group {
  display: flex;
  gap: 10px;
  padding: 4px 0;
  margin-top: 10px;
}

.message-group:first-child { margin-top: 0; }

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  color: white;
  flex-shrink: 0;
}

.message-author { font-size: 15px; font-weight: 600; }
.message-time { font-size: 11px; color: var(--text-muted); }
.message-header { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; }
.message-text { font-size: 16px; line-height: 1.4; word-wrap: break-word; }

.message-input-area {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-bottom));
  background: var(--bg-primary);
  border-top: 1px solid var(--bg-tertiary);
  flex-shrink: 0;
}

.message-input-area input {
  flex: 1;
  min-width: 0;
  padding: 12px 16px;
  background: var(--bg-modifier);
  border: none;
  border-radius: 22px;
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 16px;
  outline: none;
  line-height: 1.3;
}

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

.send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  cursor: pointer;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.send-btn:active { background: var(--accent-hover); }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  padding: 0;
}

.modal {
  background: var(--bg-primary);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 24px;
  width: 100%;
  max-width: 480px;
  padding-bottom: calc(24px + var(--safe-bottom));
}

.modal h2 { font-size: 20px; font-weight: 700; margin-bottom: 8px; }
.modal-desc { color: var(--text-secondary); font-size: 14px; margin-bottom: 20px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.modal-actions .btn { min-height: 44px; }

/* Install banner */
.install-banner {
  position: fixed;
  bottom: calc(12px + var(--safe-bottom));
  left: 12px;
  right: 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--bg-modifier);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 200;
  box-shadow: var(--shadow);
  font-size: 14px;
}

.install-banner span { flex: 1; }

/* Mobile: single-panel navigation */
@media (max-width: 768px) {
  .app { flex-direction: column; }

  .sidebar {
    max-width: none;
    width: 100%;
    flex: 1;
    min-height: 0;
  }

  .chat-area {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 50;
    width: 100%;
    height: 100dvh;
    height: -webkit-fill-available;
  }

  .app.show-chat .sidebar { display: none; }
  .app.show-chat .chat-area { display: flex; }

  .back-btn { display: flex; }

  .empty-state { display: none !important; }
}

/* Desktop: side-by-side */
@media (min-width: 769px) {
  .sidebar { width: 280px; min-width: 280px; }
  .back-btn { display: none !important; }

  .empty-state {
    display: flex;
  }

  .app.show-chat .empty-state { display: none; }

  .modal-overlay {
    align-items: center;
    padding: 20px;
  }

  .modal {
    border-radius: var(--radius);
    padding-bottom: 24px;
  }

  .install-banner {
    max-width: 420px;
    left: auto;
    right: 20px;
  }
}

@media (min-width: 769px) {
  .friend-item:hover { background: var(--bg-modifier); color: var(--text-primary); }
  .icon-btn:hover { color: var(--text-primary); background: var(--bg-modifier); }
}
