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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  background: #f0f2f5;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ── Screens ─────────────────────────────────────────────── */
.screen { width: 100%; height: 100vh; height: 100dvh; }

/* ── Auth ────────────────────────────────────────────────── */
#auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1e2235;
}

.auth-card {
  background: #fff;
  border-radius: 12px;
  padding: 40px;
  width: 360px;
  max-width: calc(100% - 32px);
  box-shadow: 0 8px 32px rgba(0,0,0,.25);
}

.auth-card h1 {
  text-align: center;
  font-size: 24px;
  margin-bottom: 24px;
  color: #1e2235;
}

.tabs {
  display: flex;
  border-bottom: 2px solid #eee;
  margin-bottom: 24px;
}

.tab {
  flex: 1;
  padding: 10px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  color: #888;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color .2s, border-color .2s;
}

.tab.active { color: #4a9dff; border-bottom-color: #4a9dff; font-weight: 600; }

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

.auth-form input {
  padding: 10px 14px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  transition: border-color .2s;
}

.auth-form input:focus { border-color: #4a9dff; }

.auth-form button {
  padding: 11px;
  background: #4a9dff;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background .2s;
}

.auth-form button:hover { background: #2e86f5; }

.form-error { color: #e74c3c; font-size: 12px; min-height: 16px; }

.password-field {
  position: relative;
  display: flex;
  align-items: center;
}

.password-field input {
  flex: 1;
  padding-right: 52px;
}

.toggle-password {
  position: absolute;
  right: 12px;
  background: none;
  border: none;
  cursor: pointer;
  color: #888;
  font-size: 12px;
  font-weight: 600;
  padding: 4px;
  transition: color .2s;
}

.toggle-password:hover { color: #4a9dff; }

/* ── App layout ──────────────────────────────────────────── */
#app-screen { display: flex; }

/* ── Sidebar ─────────────────────────────────────────────── */
.sidebar {
  width: 280px;
  min-width: 280px;
  background: #1e2235;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid #2d3554;
  color: #fff;
  font-weight: 600;
}

.sidebar-header button {
  background: none;
  border: none;
  color: #aaa;
  font-size: 18px;
  cursor: pointer;
  transition: color .2s;
}

.sidebar-header button:hover { color: #fff; }

.sidebar-actions {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid #2d3554;
}

.sidebar-actions button {
  flex: 1;
  padding: 7px;
  background: #2d3554;
  color: #ccc;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: background .2s, color .2s;
}

.sidebar-actions button:hover { background: #4a9dff; color: #fff; }

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

.chat-item {
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid #252b42;
  transition: background .15s;
}

.chat-item:hover { background: #252b42; }
.chat-item.active { background: #2d3a5e; }

.chat-item-name { color: #e0e0e0; font-weight: 500; font-size: 14px; }
.chat-item-type { color: #666; font-size: 11px; margin-top: 2px; text-transform: uppercase; letter-spacing: .5px; }

.no-chats { color: #555; font-size: 13px; padding: 20px 16px; text-align: center; }

/* ── Main area ───────────────────────────────────────────── */
.main { flex: 1; display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden; }

.empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 15px;
}

.chat-view { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

.chat-header {
  padding: 14px 20px;
  background: #fff;
  border-bottom: 1px solid #e8e8e8;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}

#chat-title { font-weight: 600; font-size: 15px; color: #1e2235; }
.chat-members-line { font-size: 12px; color: #999; margin-top: 2px; }

/* ── Messages ────────────────────────────────────────────── */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: #f0f2f5;
}

.load-more {
  align-self: center;
  margin-bottom: 8px;
  padding: 6px 16px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 16px;
  font-size: 12px;
  color: #666;
  cursor: pointer;
  transition: background .2s;
}

.load-more:hover { background: #e8e8e8; }

.message { display: flex; flex-direction: column; max-width: 65%; }
.message.mine { align-self: flex-end; align-items: flex-end; }
.message.theirs { align-self: flex-start; align-items: flex-start; }

.sender { font-size: 11px; color: #888; margin-bottom: 2px; padding-left: 4px; }

.bubble {
  padding: 9px 14px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.4;
  word-break: break-word;
}

.message.mine .bubble {
  background: #4a9dff;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.message.theirs .bubble {
  background: #fff;
  color: #1e2235;
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 2px rgba(0,0,0,.08);
}

.time { font-size: 10px; color: #aaa; margin-top: 3px; padding: 0 4px; }

.read-receipt {
  text-align: right;
  font-size: 11px;
  color: #4a9dff;
  padding: 2px 20px 6px;
  background: #f0f2f5;
}

/* ── Input area ──────────────────────────────────────────── */
.input-area {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  background: #fff;
  border-top: 1px solid #e8e8e8;
}

.input-area input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid #ddd;
  border-radius: 22px;
  font-size: 14px;
  outline: none;
  transition: border-color .2s;
}

.input-area input:focus { border-color: #4a9dff; }

.input-area button {
  padding: 10px 20px;
  background: #4a9dff;
  color: #fff;
  border: none;
  border-radius: 22px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background .2s;
}

.input-area button:hover { background: #2e86f5; }

/* ── Modals ──────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-box {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  width: 380px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 { font-size: 16px; color: #1e2235; }

.modal-close {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #999;
  line-height: 1;
}

.modal-close:hover { color: #333; }

.modal-box input {
  padding: 9px 13px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
}

.modal-box input:focus { border-color: #4a9dff; }

.modal-box button[id^="create"] {
  padding: 10px;
  background: #4a9dff;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  font-size: 14px;
}

.modal-box button[id^="create"]:hover { background: #2e86f5; }

.search-results { max-height: 200px; overflow-y: auto; }

.search-result {
  padding: 9px 12px;
  border-radius: 6px;
  cursor: pointer;
  color: #333;
  transition: background .15s;
}

.search-result:hover { background: #f0f2f5; }

.member-tags { display: flex; flex-wrap: wrap; gap: 6px; }

.member-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  background: #e8f0ff;
  color: #4a9dff;
  border-radius: 12px;
  font-size: 12px;
}

.member-tag button {
  background: none;
  border: none;
  color: #4a9dff;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0;
}

/* ── Back button (mobile only) ───────────────────────────── */
.back-btn {
  display: none;
  background: none;
  border: none;
  font-size: 22px;
  color: #1e2235;
  cursor: pointer;
  padding: 0 12px 0 0;
  line-height: 1;
  flex-shrink: 0;
}

/* ── Input area safe area (iOS home bar) ─────────────────── */
.input-area {
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
}

/* ── Mobile ──────────────────────────────────────────────── */
@media (max-width: 600px) {
  /* Auth */
  .auth-card { padding: 28px 20px; }

  /* App layout: full-screen toggle */
  #app-screen { position: relative; }

  .sidebar {
    width: 100%;
    min-width: 0;
    position: absolute;
    inset: 0;
    z-index: 1;
  }

  .main {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: none;
  }

  /* Show main only when a chat is open */
  #app-screen.chat-open .main { display: flex; }
  #app-screen.chat-open .sidebar { display: none; }

  /* Show back button */
  .back-btn { display: block; }

  /* Chat header: flex row with back button */
  .chat-header { display: flex; align-items: center; }

  /* Bigger touch targets */
  .chat-item { padding: 14px 16px; }
  .sidebar-actions button { padding: 10px; font-size: 13px; }
  .input-area button { padding: 10px 16px; }

  /* Modal full-width */
  .modal-box { width: calc(100% - 32px); }

  /* Messages max-width relaxed */
  .message { max-width: 85%; }
}
