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

html, body {
  height: 100%;
  overflow: hidden; /* nothing here should ever scroll except #app's own
  panes — otherwise mobile browsers scroll the whole page to keep a focused
  input visible when the keyboard opens, dragging the header off-screen */
  overscroll-behavior: none; /* kill the rubber-band bounce that drags the
  whole page (revealing the body background) past the composer on mobile */
}

body {
  font-family: 'Figtree', ui-sans-serif, system-ui, sans-serif;
  background: var(--bg-page);
  color: var(--text);
}

input { font-family: inherit; }
input::placeholder { color: var(--text-muted-2); }

a { color: var(--accent); }
a:hover { color: var(--accent); }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: oklch(65% 0.01 75 / 0.5); border-radius: 8px; }

#app {
  height: 100vh;
  height: 100dvh; /* accounts for mobile browser chrome (address bar) */
  width: 100%;
  display: flex;
  overflow: hidden;
  /* fixed, not relative — pins #app to the actual viewport regardless of
  any scroll position the browser might give the document itself (e.g. to
  keep a focused input visible when the keyboard opens) */
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}

#app > [data-zone='auth'] {
  flex: 1;
  height: 100%;
}

/* ---------- Theme toggle switch ---------- */
.theme-toggle {
  width: 36px;
  height: 20px;
  border-radius: 999px;
  background: var(--border);
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
  border: none;
  padding: 0;
}
.theme-toggle[data-on='true'] { background: var(--accent); }
.theme-toggle .knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: oklch(99% 0 0);
  transition: left 0.15s;
}
.theme-toggle[data-on='true'] .knob { left: 18px; }

/* ---------- Auth screen ---------- */
.auth-screen {
  flex: 1;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow-y: auto;
}
.auth-card {
  width: 400px;
  max-width: 100%;
  background: var(--bg-white);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 1px 2px var(--shadow), 0 16px 32px var(--shadow);
}
.auth-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--accent); flex-shrink: 0;
}
.brand-mark--sm { width: 22px; height: 22px; }
.brand-name { font-size: 18px; font-weight: 600; letter-spacing: -0.01em; }
.brand-name--sm { font-size: 15px; }

.auth-title { font-size: 22px; font-weight: 600; margin-bottom: 6px; }
.auth-subtitle { font-size: 14px; color: var(--text-muted-3); margin-bottom: 28px; }

.field-list { display: flex; flex-direction: column; gap: 16px; }
.field label {
  font-size: 12px; font-weight: 500; color: var(--text-muted-3);
  margin-bottom: 6px; display: block;
}
.field input, .text-input {
  width: 100%;
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--border-soft);
  font-size: 14px;
  outline: none;
  color: var(--text);
  background: var(--bg-card);
}
.field--tag { position: relative; }
.field--tag .at-prefix {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  font-size: 14px; color: var(--text-muted-2); pointer-events: none;
}
.field--tag input { padding-left: 28px; }

.settings-avatar-row { display: flex; align-items: center; gap: 14px; }
.settings-avatar-actions { display: flex; align-items: center; gap: 10px; }
.settings-avatar-upload { cursor: pointer; width: auto; display: inline-block; text-align: center; }
.settings-avatar-status { font-size: 13px; color: var(--text-muted-2); }

.field--password { position: relative; }
.field--password input { padding-right: 40px; }
.password-toggle {
  position: absolute; right: 6px; bottom: 6px;
  width: 28px; height: 28px; border: none; background: transparent;
  color: var(--text-muted-2); cursor: pointer; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.password-toggle:hover { color: var(--text); background: rgba(0, 0, 0, 0.06); }

.tag-availability { font-size: 12.5px; margin-top: -8px; min-height: 0; }
.tag-availability--ok { color: oklch(55% 0.13 145); }
.tag-availability--taken { color: oklch(55% 0.18 25); }
.tag-availability .action { color: var(--accent); cursor: pointer; font-weight: 600; }

.btn-primary {
  width: 100%; padding: 12px; border-radius: 12px; border: none;
  background: var(--accent); color: var(--accent-text);
  font-size: 14px; font-weight: 600; cursor: pointer; margin-top: 4px;
}
.btn-primary:disabled { opacity: 0.6; cursor: default; }
.btn-secondary {
  width: 100%; padding: 12px; border-radius: 12px;
  border: 1px solid var(--border-soft); background: transparent;
  color: var(--text); font-size: 14px; font-weight: 500; cursor: pointer;
}

.divider { display: flex; align-items: center; gap: 10px; margin: 24px 0; }
.divider .line { flex: 1; height: 1px; background: var(--border); }
.divider span { font-size: 12px; color: var(--text-muted-2); }

.auth-toggle { margin-top: 24px; text-align: center; font-size: 14px; color: var(--text-muted-3); }
.auth-toggle .action { color: var(--accent); cursor: pointer; font-weight: 600; }

.auth-forgot-password { text-align: right; margin-top: -8px; }
.auth-forgot-password .action { color: var(--accent); cursor: pointer; font-size: 13px; font-weight: 600; }

.form-error {
  font-size: 13px; color: oklch(55% 0.18 25); margin-top: -4px;
  min-height: 0;
}
.form-success {
  font-size: 13px; color: oklch(55% 0.15 145); margin-top: -4px;
  min-height: 0;
}
.form-warning {
  font-size: 13px; line-height: 1.5; color: oklch(55% 0.16 70);
  background: oklch(55% 0.16 70 / 0.1); border-radius: 10px;
  padding: 10px 12px; margin-top: 4px;
}

/* ---------- App shell ---------- */
.app-shell { width: 100%; height: 100%; display: flex; }

.sidebar {
  width: 320px; min-width: 320px; height: 100%;
  border-right: 1px solid var(--border);
  background: var(--bg-sidebar);
  display: flex; flex-direction: column;
}
.sidebar-top { padding: 16px 16px 12px 16px; }
.sidebar-top-row {
  display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px;
}
.sidebar-top-actions { display: flex; align-items: center; gap: 10px; }
.logout-btn {
  padding: 6px 12px; border-radius: 999px;
  border: 1px solid var(--border-soft); background: transparent;
  color: var(--text-muted-3); font-size: 12px; font-weight: 500; cursor: pointer;
}
.logout-btn:hover { color: var(--text); border-color: var(--border); }
.settings-btn {
  width: 28px; height: 28px; border-radius: 50%;
  border: 1px solid var(--border-soft); background: transparent;
  color: var(--text-muted-3); font-size: 14px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.settings-btn:hover { color: var(--text); border-color: var(--border); }
.search-wrap { position: relative; }
.search-icon-circle {
  position: absolute; left: 12px; top: 11px; width: 13px; height: 13px;
  border: 1.5px solid var(--text-muted-2); border-radius: 50%; pointer-events: none;
}
.search-icon-line {
  position: absolute; left: 21px; top: 20px; width: 6px; height: 1.5px;
  background: var(--text-muted-2); transform: rotate(45deg); pointer-events: none;
}
.search-input {
  width: 100%; padding: 10px 14px 10px 34px; border-radius: 999px;
  border: 1px solid var(--border-soft); background: var(--bg-card);
  font-size: 14px; outline: none; color: var(--text);
}

.chat-list {
  flex: 1; overflow-y: auto; padding: 0 8px 8px 8px;
  display: flex; flex-direction: column; gap: 2px;
  overscroll-behavior: contain;
}
.chat-row {
  display: flex; gap: 12px; padding: 13px 12px; border-radius: 14px;
  cursor: pointer; align-items: flex-start; background: transparent;
  border: none; width: 100%; text-align: left; font: inherit; color: inherit;
}
.chat-row[data-selected='true'] { background: var(--row-selected-bg); }

.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600; flex-shrink: 0; position: relative;
}
.avatar--md { width: 36px; height: 36px; font-size: 12px; }
.avatar--sm { width: 36px; height: 36px; font-size: 12px; }
.avatar-dot {
  position: absolute; bottom: -1px; right: -1px; width: 10px; height: 10px;
  border-radius: 50%; border: 2px solid var(--bg-sidebar);
}
.avatar-fallback { position: relative; z-index: 0; }
.avatar-img {
  position: absolute; inset: 0; width: 100%; height: 100%;
  border-radius: 50%; object-fit: cover; z-index: 1;
}
.avatar--clickable { cursor: pointer; }

.chat-row-body { flex: 1; min-width: 0; }
.chat-row-line1 { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.chat-row-name {
  font-size: 14px; font-weight: 600; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.chat-row-time { font-size: 11px; color: var(--text-muted-2); flex-shrink: 0; }
.chat-row-tag { font-size: 12px; color: var(--text-muted-2); margin-bottom: 2px; }
.chat-row-preview {
  font-size: 13px; color: var(--text-muted); overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.chat-row-unread-badge {
  flex-shrink: 0; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--accent); color: #fff;
  font-size: 11px; font-weight: 600; display: flex;
  align-items: center; justify-content: center; align-self: center;
}

/* ---------- Conversation pane ---------- */
.conversation-pane { flex: 1; min-width: 0; height: 100%; position: relative; }
.conversation {
  height: 100%; display: flex; flex-direction: column;
}
.conversation-header {
  height: 64px; display: flex; align-items: center; gap: 12px; padding: 0 24px;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
  position: sticky; top: 0; z-index: 5; background: var(--bg-page);
}
.conversation-header-inner {
  width: 100%; max-width: 1040px; margin: 0 auto;
  display: flex; align-items: center; gap: 12px;
}
.conversation-back-btn {
  display: none; flex-shrink: 0; width: 32px; height: 32px; border-radius: 50%;
  border: none; background: transparent; color: var(--text); font-size: 24px;
  cursor: pointer; align-items: center; justify-content: center; line-height: 1;
}
.conversation-back-btn:hover { background: rgba(0, 0, 0, 0.06); }
.conversation-header-name { font-size: 15px; font-weight: 600; }
.conversation-header-tag { margin-left: 6px; font-size: 12px; font-weight: 400; color: var(--text-muted-2); }
.conversation-header-status { font-size: 12px; color: var(--text-muted); }
.conversation-header-status[data-typing='true'] { color: var(--accent); }

.message-list {
  flex: 1; overflow-y: auto; padding: 24px;
  display: flex; flex-direction: column; gap: 14px;
  overscroll-behavior: contain; /* don't let scrolling past the end of the
  list bubble up into a page-level bounce */
}
.message-list-inner {
  width: 100%; max-width: 1040px; margin: 0 auto;
  display: flex; flex-direction: column; gap: 14px;
}
.history-loading {
  text-align: center; font-size: 12px; color: var(--text-muted); padding: 8px 0;
}
.date-divider {
  display: flex; justify-content: center; align-items: center;
  margin: 6px 0;
}
.date-divider span {
  font-size: 12px; color: var(--text-muted);
  background: var(--bg-card); padding: 4px 12px; border-radius: 12px;
}
.date-sticky {
  position: sticky; top: 0; z-index: 2;
  display: flex; justify-content: center;
  pointer-events: none;
  margin-bottom: -34px;
}
.date-sticky[hidden] { display: none; }
.date-sticky span {
  font-size: 12px; color: var(--text-muted);
  background: color-mix(in oklch, var(--bg-card) 85%, transparent);
  backdrop-filter: blur(4px);
  padding: 4px 12px; border-radius: 12px;
  margin-top: 8px;
}
.message-row {
  display: flex; flex-direction: column; width: 100%; padding: 2px 0;
  transition: background-color 0.1s;
}
.message-row[data-menu-open='true'] { background: var(--message-row-hover-bg); }
.message-row[data-mine='true'] { align-items: flex-end; }
.message-row[data-mine='false'] { align-items: flex-start; }

.message-row-inner {
  display: flex; align-items: flex-end; gap: 4px; max-width: 60%;
  padding: 2px 8px; border-radius: 12px; transition: background-color 0.1s;
}
.message-row-inner:hover { background: var(--message-row-hover-bg); }
.message-row[data-mine='true'] .message-row-inner { flex-direction: row-reverse; align-self: flex-end; }
.message-row[data-mine='false'] .message-row-inner { align-self: flex-start; }

.bubble {
  position: relative; padding: 10px 14px; border-radius: 16px;
  font-size: 14px; line-height: 1.5; min-width: 0;
}
.bubble-text { white-space: pre-wrap; }
.message-row[data-mine='true'] .bubble { background: var(--accent); color: var(--accent-text); }
.message-row[data-mine='false'] .bubble { background: var(--bg-white); color: var(--text); }
.message-time { font-size: 11px; color: var(--text-muted-2); margin-top: 4px; padding: 0 4px; }
.message-edited-tag { margin-left: 6px; font-style: italic; }
.read-ticks { margin-left: 4px; }
.read-ticks[data-read='true'] { color: var(--accent); letter-spacing: -2px; }

.bubble--deleted {
  max-width: 60%; font-style: italic; color: var(--text-muted-2);
  background: transparent !important; border: 1px dashed var(--border-soft);
}

.message-menu-btn {
  flex-shrink: 0; width: 24px; height: 24px; margin-bottom: 2px;
  border: none; background: transparent; cursor: pointer; border-radius: 50%;
  font-size: 14px; line-height: 1; color: var(--text-muted-3); opacity: 0;
  display: flex; align-items: center; justify-content: center;
  transition: opacity 0.1s;
}
.message-row:hover .message-menu-btn { opacity: 1; }
.message-menu-btn:hover { background: rgba(0, 0, 0, 0.08); color: var(--text); }

.message-menu {
  position: fixed; z-index: 20;
  background: var(--bg-white); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 20px var(--shadow); overflow: hidden; min-width: 150px;
}
.message-menu[hidden] { display: none; }
.message-menu-item {
  padding: 7px 12px; font-size: 13px; color: var(--text); cursor: pointer; white-space: nowrap;
}
.message-menu-item:hover { background: var(--row-selected-bg); }
.message-menu-item--danger { color: oklch(55% 0.18 25); }

.bubble--editing { max-width: 70%; padding: 10px 14px; }
.edit-input {
  width: 100%; padding: 8px 10px; border-radius: 10px;
  border: 1px solid var(--border-soft); background: var(--bg-card);
  font-size: 14px; outline: none; color: var(--text); font-family: inherit;
}
.edit-actions { display: flex; gap: 12px; margin-top: 6px; font-size: 12px; }
.edit-actions .action { cursor: pointer; color: var(--text-muted-3); }
.edit-actions .action:hover { color: var(--accent); }

.composer {
  padding: 16px 24px; border-top: 1px solid var(--border);
  display: flex; justify-content: center; flex-shrink: 0;
}
.composer-inner {
  width: 100%; max-width: 1040px;
  display: flex; gap: 10px; align-items: center;
}
.composer-input {
  flex: 1; padding: 12px 16px; border-radius: 999px;
  border: 1px solid var(--border-soft); background: var(--bg-card);
  font-size: 14px; outline: none; color: var(--text);
}
.send-btn {
  width: 40px; height: 40px; border-radius: 50%; border: none;
  background: var(--border); color: var(--accent-text); font-size: 17px;
  cursor: pointer; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
}
.send-btn[data-enabled='true'] { background: var(--accent); cursor: pointer; }

.scroll-to-bottom-btn {
  position: absolute; right: 24px; bottom: 96px; z-index: 4;
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--border-soft); background: var(--bg-white);
  color: var(--text); font-size: 18px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 12px var(--shadow);
}
.scroll-to-bottom-btn[hidden] { display: none; }
.scroll-to-bottom-btn:hover { background: var(--bg-card); }

/* ---------- Empty state ---------- */
.empty-state {
  height: 100%; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 16px; padding: 40px;
}
.empty-graphic { position: relative; width: 88px; height: 60px; }
.empty-graphic .rect-a {
  position: absolute; left: 0; top: 12px; width: 60px; height: 40px;
  border-radius: 14px; background: var(--bg-white); border: 1px solid var(--border);
}
.empty-graphic .rect-b {
  position: absolute; right: 0; top: 0; width: 46px; height: 32px;
  border-radius: 11px; background: var(--empty-accent-tint);
}
.empty-title { font-size: 17px; font-weight: 600; }
.empty-subtitle {
  font-size: 14px; color: var(--text-muted); text-align: center; max-width: 280px;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; top: 24px; right: 24px; width: 340px;
  background: var(--bg-white); border: 1px solid var(--border); border-radius: 16px;
  box-shadow: 0 10px 28px var(--shadow); padding: 16px;
  display: flex; gap: 12px; align-items: flex-start; z-index: 50; cursor: pointer;
}
.toast-body { flex: 1; min-width: 0; }
.toast-line1 { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.toast-name { font-size: 13px; font-weight: 600; }
.toast-dismiss { font-size: 16px; color: var(--text-muted-2); cursor: pointer; line-height: 1; }
.toast-text { font-size: 13px; color: var(--text-muted); margin-top: 2px; }

/* ---------- Settings modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: oklch(0% 0 0 / 0.4);
  display: flex; align-items: center; justify-content: center; z-index: 60;
}
.modal {
  width: 380px; max-width: calc(100vw - 48px);
  background: var(--bg-white); border: 1px solid var(--border); border-radius: 20px;
  box-shadow: 0 16px 40px var(--shadow); padding: 24px;
  display: flex; flex-direction: column; gap: 14px;
}
.modal-header { display: flex; align-items: center; justify-content: space-between; }
.modal-title { font-size: 17px; font-weight: 600; }
.modal-close {
  width: 28px; height: 28px; border-radius: 50%; border: none; background: transparent;
  color: var(--text-muted-3); font-size: 18px; cursor: pointer; line-height: 1;
}
.modal-close:hover { color: var(--text); }

.avatar-preview {
  position: relative; max-width: min(90vw, 480px); max-height: 90vh;
}
.avatar-preview-img {
  display: block; width: 100%; height: 100%; max-height: 90vh;
  object-fit: contain; border-radius: 12px;
}
.avatar-preview-close {
  position: absolute; top: -12px; right: -12px;
  background: var(--bg-white); border: 1px solid var(--border);
  box-shadow: 0 4px 12px var(--shadow);
}

/* ---------- Mobile layout ----------
   Sidebar and conversation become two full-screen views instead of a
   side-by-side split — #app[data-mobile-view] (set in main.js whenever
   selectedChatId changes) decides which one is showing. */
@media (max-width: 768px) {
  .auth-card { padding: 28px 24px; }

  .sidebar { width: 100%; min-width: 0; }

  #app[data-mobile-view='sidebar'] .conversation-pane { display: none; }
  #app[data-mobile-view='conversation'] .sidebar { display: none; }

  .conversation-back-btn { display: flex; }

  .conversation-header {
    padding: 0 12px; height: 56px;
    padding-top: env(safe-area-inset-top);
    height: calc(56px + env(safe-area-inset-top));
  }
  .sidebar-top { padding-top: calc(16px + env(safe-area-inset-top)); }
  .message-list { padding: 16px; }
  .composer { padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); }

  .message-row-inner { max-width: 82%; }
  .bubble--deleted { max-width: 82%; }

  .scroll-to-bottom-btn { right: 16px; bottom: calc(84px + env(safe-area-inset-bottom)); }

  .composer-input, .field input, .text-input, .search-input {
    font-size: 16px;
  }

  .toast {
    left: 12px; right: 12px; top: 12px; width: auto;
  }

  .modal { width: 100%; max-width: calc(100vw - 32px); padding: 20px; }

  .settings-avatar-row { flex-wrap: wrap; }
}

