:root,
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1419;
  --bg-elevated: #1a2332;
  --bg-soft: #243044;
  --border: #2d3a4f;
  --text: #e7ecf3;
  --muted: #8b9bb4;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --ok: #22c55e;
  --error: #ef4444;
  --in: #1e293b;
  --out: #1e3a5f;
  --radius: 10px;
  --font: "Segoe UI", system-ui, -apple-system, "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", "Twemoji Mozilla", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --topbar-h: 52px;
  /* Shared row height: conversation list item ↔ thread contact header */
  --conv-row-h: 4.25rem;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  --shadow-soft: 0 12px 32px rgba(0, 0, 0, 0.35);
  --auth-glow: #1e293b;
  --badge-bg: rgba(59, 130, 246, 0.2);
  --badge-fg: #93c5fd;
  --msg-tag-bg: rgba(59, 130, 246, 0.25);
  --msg-tag-fg: #93c5fd;
  --danger-bg: rgba(239, 68, 68, 0.15);
  --danger-border: rgba(239, 68, 68, 0.4);
  --danger-fg: #fecaca;
  --row-sub: rgba(0, 0, 0, 0.15);
  --theme-color: #1a2332;
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #f0f3f8;
  --bg-elevated: #ffffff;
  --bg-soft: #e4eaf2;
  --border: #cfd8e6;
  --text: #152033;
  --muted: #5b6b82;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --ok: #16a34a;
  --error: #dc2626;
  --in: #eef2f7;
  --out: #dbeafe;
  --shadow: 0 10px 28px rgba(15, 23, 42, 0.12);
  --shadow-soft: 0 12px 28px rgba(15, 23, 42, 0.1);
  --auth-glow: #c5d4f0;
  --badge-bg: rgba(37, 99, 235, 0.12);
  --badge-fg: #1d4ed8;
  --msg-tag-bg: rgba(37, 99, 235, 0.14);
  --msg-tag-fg: #1d4ed8;
  --danger-bg: rgba(220, 38, 38, 0.1);
  --danger-border: rgba(220, 38, 38, 0.35);
  --danger-fg: #b91c1c;
  --row-sub: rgba(15, 23, 42, 0.04);
  --theme-color: #f0f3f8;
}

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

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

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  min-height: 100dvh;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

/* Prevent iOS rubber-band from fighting fixed inbox layout */
.app-body {
  overflow: hidden;
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}
.app-body > .inbox-layout,
.app-body > .settings-layout,
.app-body > .page {
  flex: 1;
  min-height: 0;
}
.app-body > .page,
.app-body > .settings-layout {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  display: flex;
  align-items: center;
  gap: 0.75rem 1.25rem;
  padding: 0.55rem 1rem;
  padding-top: max(0.55rem, var(--safe-top));
  padding-left: max(1rem, var(--safe-left));
  padding-right: max(1rem, var(--safe-right));
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-shrink: 0;
  min-height: var(--topbar-h);
}
.topbar-brand a {
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  letter-spacing: -0.02em;
  white-space: nowrap;
}
.topbar-nav { display: flex; gap: 0.35rem; flex: 1; min-width: 0; }
.topbar-nav a {
  color: var(--muted);
  padding: 0.45rem 0.7rem;
  border-radius: 8px;
  text-decoration: none;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
}
.topbar-nav a:hover,
.topbar-nav a.active {
  color: var(--text);
  background: var(--bg-soft);
}
.topbar-user {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  min-width: 0;
}
.user-chip {
  font-size: 0.9rem;
  color: var(--muted);
  max-width: 10rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Theme switcher */
.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  min-width: 40px;
  min-height: 40px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font-size: 1.15rem;
  line-height: 1;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.theme-toggle:hover {
  background: var(--bg-soft);
  border-color: var(--accent);
}
.theme-toggle .theme-icon-sun,
.theme-toggle .theme-icon-moon {
  display: none;
}
/* In dark mode, show sun (switch to light). In light mode, show moon. */
html[data-theme="dark"] .theme-toggle .theme-icon-sun,
html:not([data-theme]) .theme-toggle .theme-icon-sun {
  display: inline;
}
html[data-theme="light"] .theme-toggle .theme-icon-moon {
  display: inline;
}
/* Auth pages (no topbar): floating control */
.auth-body .theme-toggle-float {
  position: fixed;
  top: max(0.75rem, var(--safe-top));
  right: max(0.75rem, var(--safe-right));
  z-index: 50;
}

/* Transient header toasts (auto-dismiss) */
.toast-host {
  position: fixed;
  top: calc(0.65rem + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  width: min(420px, calc(100vw - 1.5rem));
  pointer-events: none;
}
.app-body .toast-host {
  top: calc(var(--topbar-h) + 0.5rem + var(--safe-top));
}
.toast {
  pointer-events: auto;
  display: flex;
  align-items: flex-start;
  gap: 0.65rem;
  width: 100%;
  padding: 0.7rem 0.75rem 0.7rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
  font-size: 0.92rem;
  line-height: 1.4;
  animation: toast-in 0.28s ease;
}
.toast-ok {
  border-color: rgba(34, 197, 94, 0.45);
  background: linear-gradient(180deg, rgba(34, 197, 94, 0.18), var(--bg-elevated) 70%);
}
.toast-error {
  border-color: rgba(239, 68, 68, 0.5);
  background: linear-gradient(180deg, rgba(239, 68, 68, 0.18), var(--bg-elevated) 70%);
}
.toast-msg {
  flex: 1;
  min-width: 0;
  word-break: break-word;
}
.toast-close {
  flex-shrink: 0;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.25rem;
  line-height: 1;
  padding: 0.1rem 0.35rem;
  margin: -0.15rem -0.15rem 0 0;
  cursor: pointer;
  border-radius: 6px;
  min-width: 32px;
  min-height: 32px;
}
.toast-close:hover {
  color: var(--text);
  background: var(--bg-soft);
}
.toast.toast-out {
  animation: toast-out 0.3s ease forwards;
}
@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
@keyframes toast-out {
  from {
    opacity: 1;
    transform: translateY(0);
  }
  to {
    opacity: 0;
    transform: translateY(-8px);
  }
}
/* Legacy class kept for any page-level inline errors (login validation, etc.) */
.flash {
  margin: 0.75rem 1.25rem;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.flash-ok { background: rgba(34, 197, 94, 0.12); border-color: rgba(34, 197, 94, 0.35); }
.flash-error { background: rgba(239, 68, 68, 0.12); border-color: rgba(239, 68, 68, 0.35); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text);
  padding: 0.55rem 0.95rem;
  border-radius: 8px;
  font: inherit;
  cursor: pointer;
  text-decoration: none;
  min-height: 44px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { filter: brightness(1.08); text-decoration: none; }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; }
.btn-danger {
  background: var(--danger-bg);
  border-color: var(--danger-border);
  color: var(--danger-fg);
}
.btn-sm { padding: 0.4rem 0.65rem; font-size: 0.85rem; min-height: 36px; }
.btn-block { width: 100%; }

.auth-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1.5rem;
  background:
    radial-gradient(ellipse at top, var(--auth-glow) 0%, transparent 55%),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.75rem;
  box-shadow: var(--shadow-soft);
}
.auth-card.wide { max-width: 480px; }
.auth-card h1 { margin: 0 0 0.25rem; font-size: 1.5rem; }

.form-stack { display: flex; flex-direction: column; gap: 0.9rem; }
.form-stack label,
.form-row label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.9rem;
  color: var(--muted);
}
.form-stack input,
.form-stack select,
.form-stack textarea,
.form-row input,
.composer textarea {
  font: inherit;
  font-size: 16px; /* avoids iOS focus zoom */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  max-width: 100%;
}
.form-stack input,
.form-stack select,
.form-stack textarea,
.form-row input {
  width: 100%;
}
.form-stack textarea { resize: vertical; }
.form-stack input:focus,
.form-stack select:focus,
.form-stack textarea:focus,
.composer textarea:focus {
  outline: 2px solid rgba(59, 130, 246, 0.45);
  border-color: var(--accent);
}
.hint { font-size: 0.8rem; color: var(--muted); }
.hint-box {
  background: var(--bg-elevated);
  border: 1px dashed var(--border);
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
}
.muted { color: var(--muted); }
.form-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.75rem;
}
.checkbox {
  flex-direction: row !important;
  align-items: center;
  gap: 0.5rem !important;
  color: var(--text) !important;
}
.checkbox input { width: auto; }

.page { padding: 1.25rem; }
.page.narrow { max-width: 640px; margin: 0 auto; }

.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
  margin-bottom: 1rem;
}
.card h2 { margin-top: 0; font-size: 1.1rem; }
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
}
.link-card {
  display: block;
  color: inherit;
  text-decoration: none;
  transition: border-color 0.15s, transform 0.15s;
}
.link-card:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
  text-decoration: none;
}
.link-card p { margin: 0.35rem 0 0; color: var(--muted); }

.settings-layout {
  display: grid;
  grid-template-columns: 200px 1fr;
  gap: 0;
  min-height: 0;
  overflow: auto;
}
.settings-nav {
  border-right: 1px solid var(--border);
  background: var(--bg-elevated);
  padding: 1rem 0;
}
.settings-nav ul { list-style: none; margin: 0; padding: 0; }
.settings-nav a {
  display: block;
  padding: 0.7rem 1.1rem;
  color: var(--muted);
  text-decoration: none;
  min-height: 44px;
}
.settings-nav li.active a,
.settings-nav a:hover {
  color: var(--text);
  background: var(--bg-soft);
}
.settings-content {
  padding: 1.25rem 1.5rem;
  padding-bottom: max(1.25rem, var(--safe-bottom));
  max-width: 900px;
  overflow-x: hidden;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.table th, .table td {
  text-align: left;
  padding: 0.55rem 0.4rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.table th { color: var(--muted); font-weight: 600; }
.table-actions { white-space: nowrap; }
.table-actions form.inline { display: inline; }
.row-sub td { background: var(--row-sub); }
.inline-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: flex-end;
  margin-bottom: 0.5rem;
}
.inline-label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.85rem; color: var(--muted); }

.kv { display: grid; grid-template-columns: 160px 1fr; gap: 0.5rem 1rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.kv code, .secret-mono code, .hint-box code {
  font-family: var(--mono);
  font-size: 0.85em;
  background: var(--bg);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
}

.totp-qr { margin: 1rem 0; }
.totp-qr img {
  background: #fff;
  padding: 10px;
  border-radius: 8px;
}
.secret-mono { word-break: break-all; }

/* Inbox */
.inbox-layout {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  height: 100%;
}
.phone-tabs {
  display: flex;
  gap: 0.35rem;
  padding: 0.5rem 0.65rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
  overflow-x: auto;
  flex-shrink: 0;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}
.phone-tab {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0.5rem 0.85rem;
  border-radius: 8px;
  border: 1px solid transparent;
  color: var(--muted);
  text-decoration: none;
  min-width: 6.5rem;
  min-height: 44px;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.phone-tab:hover { background: var(--bg-soft); color: var(--text); text-decoration: none; }
.phone-tab.active {
  background: var(--bg-soft);
  border-color: var(--border);
  color: var(--text);
}
.phone-tab-label { font-weight: 600; font-size: 0.9rem; }
.phone-tab-num { font-size: 0.75rem; opacity: 0.8; }
.compose-tab {
  margin-left: auto;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  gap: 0.35rem;
  font-weight: 600;
  color: var(--accent);
  min-width: auto;
  flex-shrink: 0;
  cursor: pointer;
  position: relative;
  z-index: 2;
}
.compose-tab:hover {
  color: var(--accent-hover);
  text-decoration: none;
}
.compose-plus { font-size: 1.25rem; line-height: 1; }
.empty-inline { padding: 0.4rem 0.5rem; color: var(--muted); font-size: 0.9rem; }

.inbox-body {
  display: grid;
  grid-template-columns: minmax(240px, 320px) 1fr;
  flex: 1;
  min-height: 0;
}
.conv-list {
  border-right: 1px solid var(--border);
  overflow-y: auto;
  background: var(--bg-elevated);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.conv-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-sizing: border-box;
  height: var(--conv-row-h);
  min-height: var(--conv-row-h);
  max-height: var(--conv-row-h);
  padding: 0 1rem;
  border-bottom: 1px solid var(--border);
  color: inherit;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.conv-item:hover { background: var(--bg-soft); text-decoration: none; }
.conv-item:active { background: var(--bg-soft); }
.conv-item.active { background: var(--bg-soft); box-shadow: inset 3px 0 0 var(--accent); }
.conv-item-top {
  display: flex;
  align-items: baseline;
  gap: 0.4rem;
  margin-bottom: 0.15rem;
  line-height: 1.25;
}
.conv-item-top strong {
  flex: 1;
  font-size: 0.95rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv-time { font-size: 0.75rem; color: var(--muted); flex-shrink: 0; }
.conv-preview {
  font-size: 0.85rem;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badge {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: var(--badge-bg);
  color: var(--badge-fg);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  flex-shrink: 0;
}
.pad { padding: 0.75rem 1rem; }

.thread {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}
.thread-empty {
  margin: auto;
  text-align: center;
  max-width: 360px;
  padding: 2rem 1.25rem;
}
.thread-header {
  display: flex;
  align-items: center; /* vertically center Contacts / Delete with title block */
  justify-content: space-between;
  gap: 0.65rem;
  box-sizing: border-box;
  height: var(--conv-row-h);
  min-height: var(--conv-row-h);
  max-height: var(--conv-row-h);
  padding: 0 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
  flex-shrink: 0;
}
.thread-header-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  line-height: 1.25;
}
.thread-header h2 {
  margin: 0 0 0.15rem;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.25;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.back-to-list {
  display: none;
  align-items: center;
  gap: 0.25rem;
  flex-shrink: 0;
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.95rem;
  padding: 0.35rem 0.4rem 0.35rem 0;
  min-height: 36px;
  align-self: center;
}
.back-to-list:hover { text-decoration: none; color: var(--accent-hover); }
.participant-chips {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.35rem;
  overflow: hidden;
  min-height: 0;
}
.chip {
  font-size: 0.85rem;
  line-height: 1.25;
  background: transparent;
  border: 0;
  border-radius: 0;
  padding: 0;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
}
.chip-sub { color: var(--muted); margin-left: 0.25rem; opacity: 0.85; }
.thread-header-actions {
  display: flex;
  align-items: center;
  align-self: center;
  gap: 0.4rem;
  flex-shrink: 0;
  height: 100%;
}
.delete-conversation-form {
  margin: 0;
  display: flex;
  align-items: center;
}
.delete-conversation-form .btn {
  min-height: 36px;
}
.thread-manage {
  flex-shrink: 0;
  position: relative;
  display: flex;
  align-items: center;
}
.thread-manage summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 0.85rem;
  list-style: none;
  padding: 0.4rem 0.55rem;
  border-radius: 8px;
  min-height: 36px;
  user-select: none;
  display: inline-flex;
  align-items: center;
}
.thread-manage summary::-webkit-details-marker { display: none; }
.thread-manage[open] summary {
  background: var(--bg-soft);
  color: var(--text);
}
.thread-manage-body {
  margin-top: 0.5rem;
  position: absolute;
  right: 0;
  top: 100%;
  z-index: 15;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
  min-width: min(300px, 85vw);
  box-shadow: var(--shadow-soft);
}

.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.msg {
  max-width: min(560px, 92%);
  padding: 0.65rem 0.85rem;
  border-radius: 12px;
  border: 1px solid var(--border);
}
.msg-in {
  align-self: flex-start;
  background: var(--in);
}
.msg-out {
  align-self: flex-end;
  background: var(--out);
}
.msg-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: baseline;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
}
.msg-author { font-weight: 600; color: var(--text); }
.msg-tag {
  font-size: 0.65rem;
  text-transform: uppercase;
  background: var(--msg-tag-bg);
  color: var(--msg-tag-fg);
  padding: 0.05rem 0.35rem;
  border-radius: 4px;
}
.msg-body {
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--font);
  line-height: 1.5;
}
.msg-media {
  margin-top: 0.4rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.msg-media img {
  max-width: min(280px, 75vw);
  max-height: 280px;
  border-radius: 8px;
  display: block;
}
.media-player {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  max-width: min(360px, 85vw);
}
.msg-video {
  width: 100%;
  max-height: 320px;
  border-radius: 10px;
  background: #0a0a0a;
  display: block;
}
.msg-audio {
  width: 100%;
  min-height: 40px;
}
.media-fallback {
  font-size: 0.8rem;
}
.media-note,
.media-error {
  font-size: 0.8rem;
  margin: 0;
  line-height: 1.35;
}
.media-error {
  color: var(--error);
}
.msg-status { font-size: 0.75rem; margin-top: 0.35rem; }
.msg-status.status-updated {
  color: var(--text);
  transition: color 0.6s ease;
}

/* Composer: [ message | 😀 | Attach | Send ] */
.composer {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.65rem 0.75rem;
  padding-bottom: max(0.65rem, var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
  flex-shrink: 0;
  align-items: center;
}

/* Emoji picker */
.emoji-wrap {
  position: relative;
  flex: 0 0 auto;
  align-self: center;
}
.emoji-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  padding: 0 0.55rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}
.emoji-toggle:hover,
.emoji-toggle[aria-expanded="true"] {
  border-color: var(--accent);
  background: var(--bg-soft);
}
.emoji-picker {
  position: absolute;
  bottom: calc(100% + 6px);
  right: 0;
  z-index: 40;
  width: min(320px, calc(100vw - 1.5rem));
  max-height: 240px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  padding: 0.5rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-soft);
}
.emoji-picker[hidden] {
  display: none !important;
}
.emoji-picker-compose {
  bottom: auto;
  top: calc(100% + 6px);
  left: 0;
  right: auto;
}
.emoji-picker button {
  appearance: none;
  border: 0;
  background: transparent;
  border-radius: 8px;
  font-size: 1.35rem;
  line-height: 1;
  padding: 0.35rem;
  min-height: 40px;
  cursor: pointer;
  font-family: var(--font);
}
.emoji-picker button:hover,
.emoji-picker button:focus-visible {
  background: var(--bg-soft);
  outline: none;
}
.compose-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin: -0.25rem 0 0.5rem;
}
.composer-input,
.composer textarea.composer-input {
  flex: 1 1 10rem;
  min-width: 0;
  width: auto;
  min-height: 44px;
  max-height: 120px;
  resize: vertical;
  margin: 0;
  align-self: center;
}
.composer-send {
  flex: 0 0 auto;
  min-width: 4.5rem;
  min-height: 44px;
  align-self: center;
}
.attach-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  font-size: 0.85rem;
  color: var(--accent);
  cursor: pointer;
  margin: 0;
  position: relative;
  align-self: center;
  min-height: 44px;
}
.attach-btn input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  overflow: hidden;
}
.attach-btn span {
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 0 0.75rem;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  box-sizing: border-box;
}
.attach-btn:hover span {
  border-color: var(--accent);
  background: rgba(59, 130, 246, 0.08);
}
.attach-names {
  flex: 1 0 100%;
  font-size: 0.8rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
  order: 10; /* full-width row under the controls when visible */
}
.attach-names:not([hidden]) {
  display: block;
}
.msg-new {
  animation: msgIn 0.35s ease;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

/* ── Mobile / narrow screens ─────────────────────────── */
@media (max-width: 800px) {
  .topbar {
    gap: 0.4rem;
    padding: 0.4rem 0.65rem;
    padding-top: max(0.4rem, var(--safe-top));
    padding-left: max(0.65rem, var(--safe-left));
    padding-right: max(0.65rem, var(--safe-right));
  }
  .topbar-brand a { font-size: 0.95rem; max-width: 6.5rem; overflow: hidden; text-overflow: ellipsis; }
  .topbar-nav a { padding: 0.4rem 0.55rem; font-size: 0.9rem; }
  .user-chip { display: none; }
  .topbar-logout {
    padding: 0.4rem 0.55rem;
    font-size: 0.8rem;
    min-height: 36px;
  }
  .theme-toggle {
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
    font-size: 1.05rem;
  }

  .toast-host {
    width: min(100% - 1rem, 420px);
  }
  .flash {
    margin: 0.5rem 0.65rem;
    padding: 0.65rem 0.85rem;
    font-size: 0.9rem;
  }

  .page { padding: 0.85rem; padding-bottom: max(0.85rem, var(--safe-bottom)); }
  .auth-body { padding: 1rem; align-items: flex-start; padding-top: max(1.5rem, var(--safe-top)); }
  .auth-card { padding: 1.25rem; }

  .settings-layout {
    grid-template-columns: 1fr;
    overflow: auto;
  }
  .settings-nav {
    border-right: 0;
    border-bottom: 1px solid var(--border);
    padding: 0;
    position: sticky;
    top: 0;
    z-index: 5;
  }
  .settings-nav ul {
    display: flex;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 0;
  }
  .settings-nav li { flex-shrink: 0; }
  .settings-nav a {
    padding: 0.75rem 0.9rem;
    white-space: nowrap;
  }
  .settings-content {
    padding: 1rem 0.85rem;
    padding-bottom: max(1.25rem, var(--safe-bottom));
  }

  .kv { grid-template-columns: 1fr; }
  .form-actions .btn { flex: 1 1 auto; }

  /* Master–detail: list OR thread, not both stacked */
  .inbox-body {
    grid-template-columns: 1fr;
  }
  .inbox-layout.is-list .thread {
    display: none;
  }
  .inbox-layout.is-thread-open .conv-list {
    display: none;
  }
  .inbox-layout.is-thread-open .phone-tabs {
    display: none; /* more room for the conversation */
  }
  .conv-list {
    max-height: none;
    border-right: 0;
    border-bottom: 0;
    height: 100%;
  }
  .back-to-list {
    display: inline-flex;
  }
  .thread-header {
    flex-wrap: nowrap;
    align-items: center;
    /* Keep same row height as desktop when possible; allow grow if needed */
    height: auto;
    min-height: var(--conv-row-h);
    max-height: none;
    padding: 0 0.75rem;
  }
  .thread-header-main {
    order: 0;
    flex: 1 1 auto;
    min-width: 0;
  }
  .back-to-list { order: -1; }
  .thread-header-actions { order: 1; }
  .thread-header h2 {
    white-space: nowrap;
    display: block;
    -webkit-line-clamp: unset;
  }
  .chip-sub { display: none; } /* reduce clutter on small screens */
  .msg {
    max-width: 88%;
  }
  .msg-media img {
    max-width: min(280px, 75vw);
  }
  .composer {
    gap: 0.4rem;
  }
  .composer-send {
    min-height: 44px;
    padding-left: 0.85rem;
    padding-right: 0.85rem;
  }
  .thread-header-actions {
    margin-left: auto;
  }

  /* Users table → stacked cards on very small widths handled below */
  .table-wrap { margin: 0 -0.15rem; }
}

@media (max-width: 520px) {
  .table thead { display: none; }
  .table,
  .table tbody,
  .table tr,
  .table td {
    display: block;
    width: 100%;
  }
  .table tr {
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-bottom: 0.65rem;
    padding: 0.5rem 0.65rem;
    background: var(--bg);
  }
  .table td {
    border: 0;
    padding: 0.35rem 0;
    display: flex;
    justify-content: space-between;
    gap: 0.75rem;
  }
  .table td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 0.8rem;
    flex-shrink: 0;
  }
  .table td.table-actions {
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 0.35rem;
  }
  .table td.table-actions::before { width: 100%; }
  .table .row-sub {
    border-style: dashed;
    margin-top: -0.4rem;
  }
  .table .row-sub td {
    display: block;
  }
  .table .row-sub td::before { display: none; }
  .inline-form { flex-direction: column; align-items: stretch; }
  .inline-form .btn { width: 100%; }
  .phone-tab-num { display: none; }
  .phone-tab { min-width: 4.5rem; padding: 0.45rem 0.65rem; }
  .compose-label { /* keep visible */ }
}

/* Desktop: hide mobile-only back control */
@media (min-width: 801px) {
  .back-to-list { display: none !important; }
}
