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

:root {
  --bg: #0e1621;
  --panel: #17212b;
  --panel-2: #242f3d;
  --accent: #5288c1;
  --accent-hover: #6ba3d8;
  --text: #f5f5f5;
  --muted: #8b9bab;
  --danger: #e74c3c;
  --mine: #2b5278;
  --other: #182533;
  --border: #0d1117;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

.hidden { display: none !important; }

.screen {
  height: 100vh;
}

#auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}

.auth-card h1 {
  font-size: 28px;
  margin-bottom: 4px;
}

.subtitle {
  color: var(--muted);
  margin-bottom: 24px;
}

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

.tab {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--muted);
  cursor: pointer;
}

.tab.active {
  background: var(--accent);
  color: white;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

input {
  padding: 12px 14px;
  border: 1px solid var(--panel-2);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

input:focus {
  outline: 2px solid var(--accent);
  border-color: transparent;
}

button {
  padding: 12px 16px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

button:hover {
  background: var(--accent-hover);
}

.error {
  color: var(--danger);
  margin-top: 12px;
  font-size: 14px;
}

.btn-secondary {
  width: 100%;
  padding: 10px;
  margin-top: 4px;
  border: 1px solid var(--panel-2);
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-weight: 500;
}

.btn-secondary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-link {
  width: 100%;
  margin-top: 4px;
  padding: 8px;
  border: none;
  background: transparent;
  color: var(--accent);
  font-weight: 500;
}

.btn-link:hover {
  background: transparent;
  text-decoration: underline;
}

.code-hint {
  color: var(--muted);
  font-size: 14px;
}

.dev-hint {
  margin-top: 12px;
  padding: 10px;
  border-radius: 8px;
  background: rgba(82, 136, 193, 0.15);
  color: var(--accent);
  font-size: 14px;
  text-align: center;
}

#code-input {
  letter-spacing: 0.3em;
  text-align: center;
  font-size: 24px;
}

#chat-screen {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100vh;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

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

.sidebar-user-btn {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0;
  background: transparent;
  color: inherit;
  text-align: left;
}

.sidebar-user-text {
  min-width: 0;
}

.sidebar-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--panel-2);
}

.profile-card {
  width: min(480px, 100%);
  max-height: 90vh;
}

.profile-body {
  overflow-y: auto;
  max-height: calc(90vh - 64px);
  padding: 16px;
}

.profile-avatar-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.profile-avatar-preview,
.profile-avatar-placeholder {
  width: 96px;
  height: 96px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--panel-2);
}

.profile-avatar-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
}

.profile-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.profile-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
}

.profile-form select {
  padding: 12px 14px;
  border: 1px solid var(--panel-2);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

.profile-section {
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.profile-section h3 {
  margin-bottom: 12px;
  font-size: 16px;
}

.success {
  color: #2ecc71;
  font-size: 14px;
}

.sidebar-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

#logout-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  background: var(--panel-2);
  font-size: 18px;
}

.icon-btn-small {
  width: 36px;
  height: 36px;
  padding: 0;
  flex-shrink: 0;
  background: var(--panel-2);
  font-size: 18px;
  line-height: 1;
}

.icon-btn-small:hover {
  background: var(--accent);
}

.icon-btn-small.muted-btn {
  opacity: 0.5;
}

.message-toast {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  width: min(420px, calc(100vw - 32px));
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
  cursor: pointer;
  animation: toast-in 0.25s ease;
}

.message-toast-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.message-toast strong {
  font-size: 15px;
}

.message-toast span {
  color: var(--muted);
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(-12px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

.muted {
  color: var(--muted);
  font-size: 13px;
}

.search-box {
  padding: 12px;
  position: relative;
}

.search-results {
  position: absolute;
  left: 12px;
  right: 12px;
  top: 56px;
  background: var(--panel-2);
  border-radius: 10px;
  max-height: 200px;
  overflow-y: auto;
  z-index: 10;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
}

.search-item,
.chat-item {
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}

.search-item:hover,
.chat-item:hover,
.chat-item.active {
  background: var(--panel-2);
}

.chat-item .preview {
  color: var(--muted);
  font-size: 13px;
  margin-top: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
}

.chat-main {
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

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

.active-chat {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.chat-header {
  padding: 16px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.back-btn {
  display: none;
  width: 40px;
  height: 40px;
  padding: 0;
  flex-shrink: 0;
  background: var(--panel-2);
  font-size: 20px;
  line-height: 1;
}

.typing {
  color: var(--accent);
  font-size: 13px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message {
  max-width: 70%;
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.4;
  word-wrap: break-word;
  cursor: pointer;
  transition: outline 0.15s ease, box-shadow 0.15s ease;
  user-select: none;
  -webkit-user-select: none;
}

.message.selected {
  outline: 2px solid var(--accent);
  box-shadow: 0 0 0 2px rgba(82, 136, 193, 0.25);
}

.message.deleted {
  opacity: 0.75;
  font-style: italic;
}

.message-forwarded {
  display: block;
  font-size: 12px;
  color: var(--accent);
  margin-bottom: 6px;
}

.message-menu {
  position: fixed;
  z-index: 1100;
  min-width: 200px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
}

.message-menu button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  border-radius: 8px;
}

.message-menu button:hover {
  background: var(--panel);
}

.message-menu button.danger {
  color: var(--danger);
}

.menu-divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

.reaction-picker {
  position: fixed;
  z-index: 1101;
  display: flex;
  gap: 4px;
  padding: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
}

.reaction-picker button {
  width: 40px;
  height: 40px;
  padding: 0;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  border-radius: 50%;
}

.reaction-picker button:hover {
  background: var(--panel);
}

.message-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}

.reaction-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(82, 136, 193, 0.2);
  font-size: 14px;
}

.reaction-chip.mine {
  outline: 1px solid var(--accent);
}

.message-edited {
  font-size: 11px;
  color: var(--muted);
  margin-left: 6px;
}

.pinned-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.pinned-jump {
  flex: 1;
  text-align: left;
  background: transparent;
  color: var(--text);
  padding: 0;
  font-weight: 500;
}

.pinned-label {
  display: block;
  color: var(--accent);
  font-size: 12px;
  margin-bottom: 2px;
}

.pinned-preview {
  display: block;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pinned-unpin {
  width: 32px;
  height: 32px;
  padding: 0;
  background: var(--panel-2);
  font-size: 18px;
  line-height: 1;
  flex-shrink: 0;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 1200;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-card {
  width: min(420px, 100%);
  max-height: 70vh;
  background: var(--panel);
  border-radius: 16px;
  border: 1px solid var(--border);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

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

.modal-close {
  width: 36px;
  height: 36px;
  padding: 0;
  background: var(--panel-2);
  font-size: 22px;
  line-height: 1;
}

.forward-chat-list {
  overflow-y: auto;
  max-height: 50vh;
}

.forward-chat-item {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.forward-chat-item:hover {
  background: var(--panel-2);
}

.message.mine {
  align-self: flex-end;
  background: var(--mine);
  border-bottom-right-radius: 4px;
}

.message.other {
  align-self: flex-start;
  background: var(--other);
  border-bottom-left-radius: 4px;
}

.message .time {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
  text-align: right;
}

.message-form {
  display: flex;
  gap: 8px;
  padding: 16px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  align-items: flex-end;
}

.message-input-wrap {
  flex: 1;
  position: relative;
  display: flex;
  gap: 8px;
  align-items: center;
}

.message-input-wrap input {
  flex: 1;
  min-width: 0;
}

.icon-btn {
  width: 44px;
  height: 44px;
  padding: 0;
  flex-shrink: 0;
  background: var(--panel-2);
  font-size: 22px;
  line-height: 1;
}

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

.message-form .send-btn {
  width: 48px;
  height: 44px;
  padding: 0;
  font-size: 20px;
  flex-shrink: 0;
}

.emoji-picker {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(100% + 8px);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
  z-index: 20;
  max-height: 220px;
  overflow-y: auto;
}

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
}

.emoji-item {
  width: 100%;
  aspect-ratio: 1;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: transparent;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}

.emoji-item:hover {
  background: var(--panel);
}

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

.message-text.emoji-only {
  font-size: 32px;
  line-height: 1.2;
}

.message-image img {
  display: block;
  max-width: min(280px, 100%);
  max-height: 320px;
  border-radius: 10px;
  cursor: pointer;
}

.message.has-image {
  padding: 8px;
}

.message.has-image .message-text {
  margin-top: 8px;
}

.photo-btn.uploading {
  opacity: 0.5;
  pointer-events: none;
}

@media (max-width: 768px) {
  body {
    height: 100dvh;
  }

  .screen {
    height: 100dvh;
  }

  #chat-screen {
    grid-template-columns: 1fr;
    position: relative;
    height: 100dvh;
  }

  .sidebar {
    position: absolute;
    inset: 0;
    z-index: 2;
    width: 100%;
    border-right: none;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .chat-main {
    position: absolute;
    inset: 0;
    z-index: 1;
    height: 100dvh;
  }

  #chat-screen.show-chat .sidebar {
    display: none;
  }

  #chat-screen.show-chat .chat-main {
    z-index: 3;
  }

  .back-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  #empty-state {
    display: none;
  }

  .message {
    max-width: 85%;
  }

  .message-form {
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }

  .emoji-picker {
    max-height: 180px;
  }

  .emoji-grid {
    grid-template-columns: repeat(7, 1fr);
  }

  .sidebar-header {
    padding-top: calc(16px + env(safe-area-inset-top));
  }

  .message-toast {
    top: calc(16px + env(safe-area-inset-top));
  }
}
