:root {
  --bg: #f0ede7;
  --card: #ffffff;
  --border: #e8e4dd;
  --muted: #7a7269;
  --text: #22201d;
  --text-soft: #5f5a53;
  --accent: #e8883c;
  --accent-soft: #fff3e8;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --brand-gradient: linear-gradient(180deg, #1c2b3d 0%, #0f4c75 100%);
  --pin-bg: #fff7cc;
  --pin-border: #f9d94a;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
  --radius: 12px;
  --radius-sm: 10px;
  --header-h: 56px;
  --sidebar-w: 240px;
  --sidebar-collapsed: 72px;
  --conv-pane-w: 330px;
  --conv-pane-tablet-w: 240px;
  --mobile-tab-h: 66px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

body[data-theme="dark"] {
  --bg: #0f172a;
  --card: #1e293b;
  --border: #334155;
  --muted: #94a3b8;
  --text: #e2e8f0;
  --text-soft: #cbd5e1;
  --accent: #f59e0b;
  --accent-soft: rgba(245, 158, 11, 0.16);
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.16);
  --pin-bg: rgba(245, 208, 74, 0.18);
  --pin-border: rgba(245, 208, 74, 0.65);
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.35);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

a {
  color: inherit;
  text-decoration: none;
}

button,
input,
select,
textarea {
  font: inherit;
}

.hidden {
  display: none !important;
}

/* Shell */
.side-rail {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--sidebar-w);
  background: var(--brand-gradient);
  border-right: 1px solid rgba(255, 255, 255, 0.2);
  z-index: 1300;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
  color: #f8fafc;
  transition: width 0.2s ease;
}

body.sidebar-collapsed .side-rail {
  width: var(--sidebar-collapsed);
}

.rail-head {
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
  padding: 16px 12px 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
}

.rail-toggle {
  width: 36px;
  height: 36px;
  min-height: 36px;
  border-radius: 8px;
  border: none;
  background: rgba(255, 255, 255, 0.08);
  color: rgba(255, 255, 255, 0.6);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  justify-self: end;
  align-self: start;
  grid-column: 2;
  grid-row: 1;
  cursor: pointer;
}

.brand,
.brand-rail {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-rail {
  grid-column: 1;
  grid-row: 1;
  justify-self: start;
  width: 100%;
}

.brand-mark {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: 9px;
  box-shadow: 0 6px 16px rgba(15, 76, 117, 0.28);
}

.brand-copy {
  min-width: 0;
  display: grid;
  gap: 1px;
}

.brand-rail .brand-copy {
  align-items: start;
  justify-items: start;
  text-align: left;
  padding-top: 1px;
}

.brand-title {
  font-size: 15px;
  font-weight: 800;
  line-height: 1.1;
}

.brand-subtitle {
  font-size: 11px;
  color: rgba(219, 234, 254, 0.86);
  line-height: 1.2;
}

.rail-nav,
.rail-foot {
  display: grid;
  gap: 6px;
}

body.sidebar-collapsed .brand-copy,
body.sidebar-collapsed .rail-link span,
body.sidebar-collapsed .nav-user-text {
  display: none;
}

body.sidebar-collapsed .rail-link .nav-badge {
  display: none !important;
}

body.sidebar-collapsed .rail-link,
body.sidebar-collapsed .nav-user-badge,
body.sidebar-collapsed .nav-logout {
  justify-content: center;
}

.rail-nav {
  padding: 10px 10px 4px;
  align-content: start;
  overflow-y: auto;
}

.rail-foot {
  padding: 10px;
  border-top: 1px solid rgba(255, 255, 255, 0.15);
}

.rail-foot .nav-user-badge {
  width: 100%;
  justify-content: flex-start;
}

.rail-foot .nav-logout {
  width: 100%;
}

body.sidebar-collapsed .rail-foot .nav-logout span {
  display: none;
}

body.sidebar-collapsed .side-rail .brand-mark {
  margin: 0 auto;
}

body:not(.sidebar-collapsed) .rail-toggle .rail-toggle-icon {
  transform: rotate(180deg);
}

.rail-toggle:hover {
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
}

.rail-toggle:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.35);
  outline-offset: 2px;
}

.rail-toggle .rail-toggle-icon {
  width: 16px;
  height: 16px;
  transition: transform 200ms ease;
}

.rail-link {
  min-height: 46px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: #dbeafe;
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  justify-content: flex-start;
}

.rail-link i {
  width: 18px;
  height: 18px;
}

.rail-link:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.rail-link.active {
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.2);
}

.nav-badge {
  margin-left: auto;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: #ef4444;
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.nav-user-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  padding: 8px 10px;
  min-height: 44px;
  background: rgba(255, 255, 255, 0.06);
}

.nav-user-initials {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 800;
}

.nav-user-text {
  font-size: 12px;
  color: rgba(248, 250, 252, 0.92);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nav-logout {
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid rgba(248, 113, 113, 0.85);
  background: rgba(239, 68, 68, 0.88);
  color: #fff;
  cursor: pointer;
  font-weight: 700;
}

.global-nav {
  position: fixed;
  top: 0;
  left: var(--sidebar-w);
  right: 0;
  height: var(--header-h);
  border-bottom: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(26, 35, 50, 0.9);
  backdrop-filter: blur(10px);
  z-index: 1200;
}

body.sidebar-collapsed .global-nav {
  left: var(--sidebar-collapsed);
}

.global-nav-inner {
  max-width: none;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.help-btn {
  height: 36px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
}

.page-wrap {
  padding: calc(var(--header-h) + 12px) 12px 12px calc(var(--sidebar-w) + 12px);
  min-height: 100vh;
}

body.sidebar-collapsed .page-wrap {
  padding-left: calc(var(--sidebar-collapsed) + 12px);
}

/* Messaging layout */
.messaging-shell {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow);
  min-height: calc(100vh - var(--header-h) - 24px);
  display: grid;
  grid-template-columns: var(--conv-pane-w) minmax(0, 1fr);
  overflow: hidden;
}

.conversation-pane {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--card);
}

.chat-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
  background: linear-gradient(to bottom, color-mix(in srgb, var(--card) 78%, var(--bg)) 0%, var(--card) 100%);
}

.pane-head {
  min-height: 64px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}

.pane-head-title {
  font-size: 18px;
  font-weight: 800;
}

.pane-head-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn,
.plus-btn,
.send-btn,
.attach-btn,
.fab,
.topbar-back,
.topbar-menu {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text-soft);
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 160ms ease, background 160ms ease, color 160ms ease;
}

.icon-btn:hover,
.plus-btn:hover,
.send-btn:hover,
.attach-btn:hover,
.fab:hover,
.topbar-back:hover,
.topbar-menu:hover {
  transform: translateY(-1px);
  background: var(--bg);
  color: var(--text);
}

.plus-btn,
.send-btn,
.fab {
  background: var(--accent);
  color: #fff;
  border-color: transparent;
}

.search-wrap {
  position: relative;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.search-icon {
  position: absolute;
  top: 50%;
  left: 24px;
  width: 15px;
  height: 15px;
  color: var(--muted);
  transform: translateY(-50%);
  pointer-events: none;
}

.search-input {
  width: 100%;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0 12px 0 38px;
  background: var(--card);
  color: var(--text);
}

.search-input:focus-visible {
  outline: none;
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 12%, transparent);
}

.filter-row {
  display: flex;
  gap: 8px;
  padding: 8px 12px 10px;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.filter-chip {
  height: 34px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  padding: 0 12px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
}

.filter-chip.active {
  border-color: transparent;
  background: var(--accent);
  color: #fff;
}

.conversation-list {
  overflow-y: auto;
  flex: 1;
  padding: 10px;
}

.conv-group + .conv-group {
  margin-top: 14px;
}

.group-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 800;
  border-top: 1px solid var(--border);
  padding: 16px 4px 8px;
}

.conv-group:first-child .group-title {
  border-top: 0;
  padding-top: 6px;
}

.conversation-item {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 12px;
  min-height: 66px;
  padding: 10px 11px;
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  text-align: left;
  cursor: pointer;
  margin-bottom: 6px;
  box-shadow: var(--shadow);
}

.conversation-item:hover {
  border-color: color-mix(in srgb, var(--accent) 24%, var(--border));
  background: color-mix(in srgb, var(--card) 86%, var(--accent-soft));
}

.conversation-item.active {
  background: color-mix(in srgb, var(--accent) 14%, var(--card));
  border-color: color-mix(in srgb, var(--accent) 34%, var(--border));
}

.conv-avatar {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-soft);
  font-size: 12px;
  font-weight: 800;
  border: 1px solid var(--border);
  background: var(--bg);
}

.conv-avatar.square {
  border-radius: 8px;
}

.conv-avatar.circle {
  border-radius: 50%;
}

.conv-avatar.board {
  background: color-mix(in srgb, #dbeafe 52%, var(--card));
  color: #5b6d8f;
}

.conv-avatar.board i {
  width: 16px;
  height: 16px;
}

.conv-avatar.dm {
  background: color-mix(in srgb, #dbeafe 36%, var(--card));
  color: #334155;
}

.conv-avatar.dm i {
  width: 16px;
  height: 16px;
}

.chat-topbar .conv-avatar {
  width: 34px;
  height: 34px;
}

.conv-body {
  min-width: 0;
}

.conv-topic-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px;
  margin-top: 2px;
}

.conv-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.25;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-type-chip {
  font-size: 9px;
  border: 1px solid var(--border);
  color: var(--muted);
  background: var(--bg);
  border-radius: 999px;
  padding: 2px 6px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.conv-topic {
  margin-top: 2px;
  font-size: 11px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-preview {
  margin-top: 4px;
  font-size: 12px;
  color: var(--text-soft);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-preview.is-empty {
  color: var(--muted);
  font-style: italic;
}

.conv-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 5px;
  min-width: 48px;
  align-self: flex-start;
}

.conv-time {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

.unread-badge {
  min-width: 18px;
  height: 18px;
  border-radius: 999px;
  padding: 0 6px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.unread-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--accent);
}

.chat-topbar {
  min-height: 68px;
  border-bottom: 1px solid var(--border);
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
}

.chat-topbar-main {
  min-width: 0;
}

.chat-title {
  font-size: 16px;
  font-weight: 600;
  line-height: 1.2;
}

.chat-topic {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
}

.chat-banner {
  margin: 8px 12px 0;
  border: 1px solid var(--pin-border);
  background: var(--pin-bg);
  border-radius: 10px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #8a5a00;
}

body[data-theme="dark"] .chat-banner {
  color: #fde68a;
}

.locked-banner {
  margin: 8px 12px 0;
  border: 1px solid color-mix(in srgb, var(--danger) 35%, var(--border));
  background: var(--danger-soft);
  border-radius: 10px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--danger);
}

.messages-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px 120px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.empty-chat {
  margin: auto;
  text-align: center;
  color: var(--muted);
  display: grid;
  gap: 8px;
  justify-items: center;
  max-width: 340px;
  padding: 0 16px;
}

.empty-chat-icon {
  width: 58px;
  height: 58px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--bg);
  display: grid;
  place-items: center;
}

.empty-chat-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-soft);
}

.empty-chat-subtitle {
  font-size: 12px;
  color: var(--muted);
}

.date-divider {
  align-self: stretch;
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: center;
  font-size: 11px;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 0;
  padding: 2px 0;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin: 6px 0 2px;
}

.date-divider::before,
.date-divider::after {
  content: "";
  height: 1px;
  background: var(--border);
  flex: 1;
}

.message-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
  max-width: min(74%, 640px);
}

.message-row.received {
  align-self: flex-start;
}

.message-row.sent {
  align-self: flex-end;
  align-items: flex-end;
}

.message-sender {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  margin-left: 4px;
}

.message-bubble {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 18px;
  padding: 10px 12px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  box-shadow: var(--shadow);
  word-break: break-word;
  transition: transform 180ms ease, opacity 180ms ease;
}

.message-row.sent .message-bubble {
  background: var(--accent);
  color: #fff;
  border-color: transparent;
  box-shadow: 0 2px 10px rgba(232, 136, 60, 0.24);
}

.message-row.deleted .message-bubble {
  background: var(--bg);
  color: var(--muted);
  font-style: italic;
}

.message-row.failed .message-bubble {
  border-color: color-mix(in srgb, var(--danger) 45%, var(--border));
}

.message-meta {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.2;
}

.message-row.sent .message-meta {
  align-self: flex-end;
  text-align: right;
}

.message-row.received .message-meta {
  align-self: flex-start;
  text-align: left;
}

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

.message-bubble.has-image-only {
  padding: 6px;
}

.message-bubble.has-image-with-text {
  padding-bottom: 8px;
}

.message-image-trigger {
  margin-top: 6px;
  display: block;
  width: 100%;
  border: 0;
  padding: 0;
  background: transparent;
  cursor: zoom-in;
}

.message-bubble .message-image-trigger:first-child,
.message-bubble .message-file-card:first-child {
  margin-top: 0;
}

.message-image-wrap {
  position: relative;
  display: block;
  width: min(280px, 100%);
  max-width: 100%;
  max-height: 300px;
  overflow: hidden;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #f0ede7;
}

.message-image-wrap.sent {
  border: 2px solid rgba(232, 136, 60, 0.75);
}

.message-image-wrap.received {
  border: 1px solid var(--border);
}

.message-image {
  display: block;
  width: 100%;
  height: auto;
  max-height: 300px;
  object-fit: cover;
  transition: opacity 180ms ease;
}

.message-image-wrap.loading .message-image {
  opacity: 0.12;
}

.message-image-loading {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, #f0ede7 90%, transparent);
}

.message-image-wrap.loading .message-image-loading {
  display: flex;
}

.message-image-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(15, 23, 42, 0.14);
  border-top-color: rgba(15, 23, 42, 0.46);
  border-radius: 999px;
  animation: messageSpin 900ms linear infinite;
}

.message-image-error {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 6px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  background: color-mix(in srgb, var(--card) 70%, var(--bg));
  padding: 10px;
}

.message-image-error i {
  width: 16px;
  height: 16px;
}

.message-image-wrap.is-error .message-image {
  display: none;
}

.message-image-wrap.is-error .message-image-error {
  display: flex;
}

.message-file-card {
  margin-top: 6px;
  border: 1px solid color-mix(in srgb, var(--border) 84%, transparent);
  border-radius: 8px;
  background: color-mix(in srgb, var(--card) 82%, var(--bg));
  padding: 8px 9px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
}

.message-file-icon {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  border: 1px solid color-mix(in srgb, var(--border) 80%, transparent);
  background: var(--card);
  color: var(--text-soft);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.message-file-icon i {
  width: 14px;
  height: 14px;
}

.message-file-meta {
  min-width: 0;
  display: grid;
  gap: 1px;
}

.message-file-name {
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-file-size {
  font-size: 11px;
  color: var(--muted);
}

.message-file-download {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  text-decoration: underline;
}

.message-row.sent .message-file-card {
  border-color: rgba(255, 255, 255, 0.45);
  background: rgba(255, 255, 255, 0.17);
}

.message-row.sent .message-file-icon {
  border-color: rgba(255, 255, 255, 0.45);
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
}

.message-row.sent .message-file-name,
.message-row.sent .message-file-size,
.message-row.sent .message-file-download {
  color: #fff;
}

@keyframes messageSpin {
  to { transform: rotate(360deg); }
}

.typing-indicator {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 7px 10px;
  background: var(--card);
  color: var(--muted);
  font-size: 12px;
}

.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--muted);
  animation: typingPulse 1s infinite ease-in-out;
}

.typing-dot:nth-child(2) { animation-delay: 120ms; }
.typing-dot:nth-child(3) { animation-delay: 240ms; }

@keyframes typingPulse {
  0%, 100% { opacity: 0.35; transform: translateY(0); }
  50% { opacity: 1; transform: translateY(-2px); }
}

.message-row.new .message-bubble {
  animation: messageSlideIn 220ms ease;
}

@keyframes messageSlideIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

.chat-composer-dock {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--card) 95%, transparent);
  backdrop-filter: blur(6px);
  padding: 10px 12px calc(10px + var(--safe-bottom));
}

.reply-chip,
.attachment-chip {
  display: none;
  margin-bottom: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  font-size: 12px;
  color: var(--text-soft);
  padding: 6px 8px;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.reply-chip.show,
.attachment-chip.show {
  display: flex;
}

.attachment-chip {
  display: none;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
}

.attachment-chip-thumb {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #f0ede7;
  flex-shrink: 0;
}

.attachment-chip #attachmentLabel {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-size {
  display: none;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

.attachment-chip .icon-btn {
  width: 32px;
  height: 32px;
  min-width: 32px;
  min-height: 32px;
}

.composer-row {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  align-items: end;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  box-shadow: var(--shadow);
  padding: 6px;
}

.composer-input {
  min-height: 44px;
  max-height: 180px;
  resize: none;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  padding: 10px 8px;
  line-height: 1.45;
  overflow-y: auto;
}

.composer-input::placeholder {
  color: color-mix(in srgb, var(--muted) 88%, transparent);
}

.composer-input:focus-visible {
  outline: none;
}

.composer-row:focus-within {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 14%, transparent);
}

.attach-btn {
  background: var(--bg);
  color: var(--text-soft);
}

.send-btn {
  width: 40px;
  height: 40px;
  border-radius: 999px;
  box-shadow: 0 4px 12px rgba(232, 136, 60, 0.28);
}

.composer-row.disabled {
  opacity: 0.5;
  pointer-events: none;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(2px);
  display: none;
  z-index: 400;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal.show {
  display: flex;
  animation: fadeIn 160ms ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-card {
  width: min(520px, 100%);
  max-height: min(86vh, 760px);
  overflow-y: auto;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--card);
  box-shadow: var(--shadow);
  padding: 16px;
  animation: popIn 160ms ease;
}

@keyframes popIn {
  from { transform: scale(0.96); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.modal-head h2 {
  margin: 0;
  font-size: 18px;
}

.modal-head .icon-btn {
  margin-left: auto;
}

.form-grid {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.form-grid label {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-soft);
}

.form-grid input,
.form-grid select,
.form-grid textarea {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  background: var(--bg);
  color: var(--text);
}

.form-grid textarea {
  min-height: 92px;
  resize: vertical;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 14px;
}

.action-btn {
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text-soft);
  padding: 0 14px;
  cursor: pointer;
  font-weight: 700;
}

.action-btn.primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
}

.action-btn.danger {
  color: var(--danger);
}

.image-lightbox {
  position: fixed;
  inset: 0;
  z-index: 700;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(2, 6, 23, 0.74);
  padding: 18px;
}

.image-lightbox.show {
  display: flex;
}

.image-lightbox-close {
  position: absolute;
  top: max(14px, env(safe-area-inset-top, 0px) + 6px);
  right: 14px;
  width: 40px;
  height: 40px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.44);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.image-lightbox-close i {
  width: 18px;
  height: 18px;
}

.image-lightbox-img {
  max-width: 90vw;
  max-height: 90vh;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  box-shadow: 0 10px 28px rgba(2, 6, 23, 0.38);
  touch-action: pinch-zoom;
}

.mobile-topbar {
  display: none;
}

.mobile-tabbar {
  display: none;
}

.fab {
  display: none;
  position: fixed;
  right: 16px;
  bottom: calc(var(--mobile-tab-h) + 14px + var(--safe-bottom));
  z-index: 250;
  border-radius: 999px;
  width: 48px;
  height: 48px;
  box-shadow: 0 4px 14px rgba(232, 136, 60, 0.3);
  animation: fabScaleIn 220ms ease;
}

@keyframes fabScaleIn {
  from { transform: scale(0.84); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.message-sheet {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 500;
  background: var(--card);
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  border: 1px solid var(--border);
  border-bottom: 0;
  box-shadow: var(--shadow);
  transform: translateY(100%);
  transition: transform 180ms ease;
  padding: 10px;
}

.message-sheet.show {
  transform: translateY(0);
}

.message-sheet button {
  width: 100%;
  min-height: 46px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  margin-bottom: 8px;
  text-align: left;
  padding: 0 12px;
  font-weight: 600;
}

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

.pull-refresh-indicator {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px;
  font-size: 12px;
  color: var(--muted);
}

.pull-refresh-indicator i {
  width: 14px;
  height: 14px;
}

.pull-refresh-indicator.show {
  display: flex;
}

.pull-refresh-indicator.show i {
  animation: refreshSpin 900ms linear infinite;
}

@keyframes refreshSpin {
  to {
    transform: rotate(360deg);
  }
}

.toast-wrap {
  position: fixed;
  right: 16px;
  top: max(16px, env(safe-area-inset-top, 0px) + 8px);
  z-index: 1000;
  max-width: min(360px, calc(100vw - 24px));
}

#status {
  position: fixed;
  left: 50%;
  bottom: calc(var(--mobile-tab-h) + 12px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 1000;
  font-size: 12px;
  color: var(--muted);
}

/* Tablet: keep two panes, compact list */
@media (min-width: 768px) and (max-width: 1024px) {
  .side-rail {
    width: var(--sidebar-collapsed);
    padding-inline: 8px;
  }

  .side-rail .brand-copy,
  .side-rail .rail-link span,
  .side-rail .nav-user-text,
  .side-rail .nav-logout {
    display: none;
  }

  .rail-link {
    justify-content: center;
    padding: 0;
  }

  .global-nav {
    left: var(--sidebar-collapsed);
  }

  .page-wrap {
    padding-left: calc(var(--sidebar-collapsed) + 10px);
  }

  .messaging-shell {
    grid-template-columns: var(--conv-pane-tablet-w) minmax(0, 1fr);
  }
}

/* Mobile stack mode */
@media (max-width: 767px) {
  .side-rail,
  .global-nav {
    display: none;
  }

  .page-wrap {
    padding: 0;
    min-height: 100vh;
  }

  .mobile-topbar {
    display: flex;
    height: calc(var(--header-h) + var(--safe-top));
    padding: var(--safe-top) 12px 0;
    border-bottom: 1px solid var(--border);
    background: var(--card);
    align-items: center;
    gap: 8px;
    position: sticky;
    top: 0;
    z-index: 210;
  }

  .mobile-topbar-title {
    font-size: 18px;
    font-weight: 800;
    margin-right: auto;
  }

  .messaging-shell {
    min-height: calc(100vh - var(--header-h) - var(--safe-top) - var(--mobile-tab-h) - var(--safe-bottom));
    border-radius: 0;
    border: 0;
    box-shadow: none;
    grid-template-columns: 1fr;
    background: var(--bg);
  }

  .conversation-pane,
  .chat-pane {
    border: 0;
    min-height: calc(100vh - var(--header-h) - var(--safe-top) - var(--mobile-tab-h) - var(--safe-bottom));
  }

  .chat-pane {
    display: none;
    background: var(--bg);
  }

  body.mobile-chat-open .conversation-pane {
    display: none;
  }

  body.mobile-chat-open .chat-pane {
    display: flex;
  }

  .chat-topbar {
    position: sticky;
    top: 0;
    z-index: 200;
    background: var(--card);
    min-height: calc(var(--header-h) + var(--safe-top));
    padding-top: calc(8px + var(--safe-top));
  }

  .chat-actions .desktop-admin-only,
  .chat-actions .desktop-only {
    display: none;
  }

  .chat-title {
    font-size: 15px;
  }

  .messages-scroll {
    padding: 10px 10px 12px;
    gap: 8px;
  }

  .message-row {
    max-width: 85%;
  }

  .message-image-wrap {
    width: min(220px, 100%);
  }

  .attachment-chip {
    align-items: flex-start;
  }

  .composer-row {
    grid-template-columns: 44px minmax(0, 1fr) 44px;
  }

  .composer-input {
    min-height: 44px;
  }

  .mobile-tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--mobile-tab-h) + var(--safe-bottom));
    padding: 6px 6px calc(6px + var(--safe-bottom));
    border-top: 1px solid var(--border);
    background: color-mix(in srgb, var(--card) 92%, transparent);
    backdrop-filter: blur(8px);
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 4px;
    z-index: 220;
  }

  body.mobile-chat-open .mobile-tabbar {
    display: none;
  }

  .conversation-item {
    min-height: 72px;
    padding: 10px 12px;
  }

  .conv-title {
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .conv-preview {
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .chat-composer-dock {
    position: sticky;
    bottom: 0;
    z-index: 4;
  }

  .fab {
    display: inline-flex;
    bottom: calc(var(--mobile-tab-h) + 14px + var(--safe-bottom));
    right: 14px;
  }

  .mobile-tab {
    min-height: 44px;
    border-radius: 10px;
    border: 0;
    background: transparent;
    color: var(--text-soft);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    font-size: 11px;
    font-weight: 700;
    position: relative;
  }

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

  .mobile-tab-badge {
    position: absolute;
    top: 3px;
    right: 12px;
    min-width: 16px;
    height: 16px;
    border-radius: 999px;
    background: var(--accent);
    color: #fff;
    font-size: 10px;
    font-weight: 800;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
  }

  .mobile-tab-badge.show {
    display: inline-flex;
  }

  .toast-wrap {
    left: 12px;
    right: 12px;
    max-width: none;
    top: max(12px, env(safe-area-inset-top, 0px) + 8px);
  }

  #status {
    bottom: calc(var(--mobile-tab-h) + 8px + var(--safe-bottom));
    max-width: calc(100vw - 24px);
    text-align: center;
  }

  .pane-head {
    min-height: calc(var(--header-h) + var(--safe-top));
    padding-top: calc(8px + var(--safe-top));
  }

  .pane-head-title {
    font-size: 17px;
  }

  .icon-btn,
  .plus-btn,
  .send-btn,
  .attach-btn,
  .fab,
  .topbar-back,
  .topbar-menu,
  .mobile-tab,
  .action-btn,
  .filter-chip {
    min-width: 44px;
    min-height: 44px;
  }
}

body[data-theme="dark"] .conv-avatar.board {
  background: color-mix(in srgb, var(--card) 82%, #1d4ed8);
  color: #bfdbfe;
}

body[data-theme="dark"] .conv-avatar.dm {
  background: color-mix(in srgb, var(--card) 82%, #334155);
  color: #e2e8f0;
}

body[data-theme="dark"] .conversation-item:hover {
  background: color-mix(in srgb, var(--card) 86%, #334155);
}

body[data-theme="dark"] .message-image-wrap {
  background: #0f172a;
}

body[data-theme="dark"] .message-image-loading {
  background: color-mix(in srgb, #0f172a 85%, transparent);
}

body[data-theme="dark"] .message-image-spinner {
  border-color: rgba(226, 232, 240, 0.22);
  border-top-color: rgba(226, 232, 240, 0.72);
}

body[data-theme="dark"] .message-image-error {
  background: color-mix(in srgb, var(--card) 76%, #0f172a);
}

body[data-theme="dark"] .message-file-card {
  background: color-mix(in srgb, var(--card) 86%, #0f172a);
}

body[data-theme="dark"] .message-file-icon {
  background: color-mix(in srgb, var(--card) 90%, #0f172a);
}
