:root {
  color-scheme: dark;
  --bg: #07110f;
  --rail: #091512;
  --surface: #0d1a18;
  --surface-strong: #12231f;
  --surface-hover: #152b26;
  --line: #243a35;
  --line-soft: #1a2c28;
  --ink: #eef8f5;
  --muted: #8fa39e;
  --muted-strong: #b7c8c3;
  --accent: #28c99a;
  --accent-strong: #119c79;
  --accent-soft: #153d33;
  --danger: #ff8e98;
  --radius: 14px;
  font-family: "Avenir Next", Avenir, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body { background: var(--bg); color: var(--ink); margin: 0; overflow: hidden; }
button, input, select { color: inherit; font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: wait; opacity: .55; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app-shell {
  display: grid;
  grid-template-columns: 224px 390px minmax(420px, 1fr);
  height: 100dvh;
  min-height: 640px;
}

.app-rail {
  background: var(--rail);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 22px 16px 18px;
}

.brand { align-items: center; color: var(--ink); display: flex; gap: 11px; padding: 0 6px; text-decoration: none; }
.brand img { height: 40px; width: 40px; }
.brand span, .rail-account > span:not(.rail-avatar) { display: flex; flex-direction: column; min-width: 0; }
.brand strong { font-size: 14px; letter-spacing: -.02em; }
.brand small, .rail-account small { color: var(--muted); font-size: 10px; margin-top: 2px; }
.app-rail nav { margin-top: 34px; }
.rail-link { align-items: center; border: 1px solid transparent; border-radius: 11px; color: var(--muted-strong); display: flex; font-size: 12px; gap: 11px; padding: 11px 12px; text-decoration: none; }
.rail-link span { color: var(--accent); font-size: 17px; }
.rail-link.is-active { background: var(--accent-soft); border-color: #205b4a; color: var(--ink); }
.rail-account { align-items: center; border-top: 1px solid var(--line); display: grid; gap: 9px; grid-template-columns: auto minmax(0, 1fr); margin-top: auto; padding: 17px 6px 0; }
.rail-account strong { font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rail-avatar, .workspace-avatar, .contact-avatar { align-items: center; background: var(--accent-soft); border: 1px solid #286653; color: var(--accent); display: inline-flex; font-weight: 800; justify-content: center; text-transform: uppercase; }
.rail-avatar { border-radius: 10px; font-size: 10px; height: 34px; width: 34px; }
.rail-account button { background: transparent; border: 0; color: var(--muted); font-size: 10px; grid-column: 2; justify-self: start; padding: 0; }
.rail-account button:hover { color: var(--ink); }

.conversation-pane {
  background: #0a1614;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}
.pane-header { align-items: center; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; min-height: 84px; padding: 18px 20px; }
.pane-header p, .auth-kicker { color: var(--accent); font-size: 9px; font-weight: 850; letter-spacing: .14em; margin: 0 0 5px; }
.pane-header h1 { font-size: 25px; letter-spacing: -.045em; line-height: 1; margin: 0; }
.header-actions { align-items: center; display: flex; gap: 7px; }
.icon-button, .secondary-button, .filter-button, .load-more, .history-action button, .mobile-back {
  background: var(--surface-strong);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--muted-strong);
  font-size: 10px;
  font-weight: 750;
  min-height: 34px;
  padding: 7px 10px;
}
.icon-button { font-size: 18px; padding: 3px; width: 34px; }
.icon-button:hover, .secondary-button:hover, .filter-button:hover, .load-more:hover, .history-action button:hover { background: var(--surface-hover); color: var(--ink); }
.workspace-summary { align-items: center; display: flex; justify-content: space-between; padding: 16px 20px 10px; }
.workspace-summary > div { align-items: center; display: flex; gap: 10px; min-width: 0; }
.workspace-summary > div > span:last-child { display: flex; flex-direction: column; min-width: 0; }
.workspace-summary strong { font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.workspace-summary small { color: var(--muted); font-size: 9px; margin-top: 3px; }
.workspace-avatar { border-radius: 10px; font-size: 10px; height: 36px; width: 36px; }
.count-badge { background: var(--surface-strong); border-radius: 999px; color: var(--muted-strong); font-size: 9px; font-weight: 850; padding: 5px 8px; }

.filters { padding: 6px 20px 12px; }
.search-field { display: flex; flex-direction: column; gap: 5px; }
.search-field > label, .filter-row label > span { color: var(--muted); font-size: 9px; font-weight: 750; }
.search-control { display: grid; grid-template-columns: minmax(0, 1fr) auto; }
.search-field input, .filter-row select, .date-jump-form input {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--ink);
  min-height: 39px;
  outline: 0;
  padding: 9px 11px;
  width: 100%;
}
.search-control input { border-radius: 10px 0 0 10px; }
.search-control button, .date-jump-form button {
  background: var(--surface-strong);
  border: 1px solid var(--line);
  border-left: 0;
  border-radius: 0 10px 10px 0;
  color: var(--muted-strong);
  font-size: 9px;
  font-weight: 800;
  padding: 0 11px;
}
.search-control button:hover, .date-jump-form button:hover { background: var(--surface-hover); color: var(--ink); }
.search-field input::placeholder { color: #70847e; }
.search-field input:focus, .filter-row select:focus, .date-jump-form input:focus { border-color: #3b8872; box-shadow: 0 0 0 3px rgba(40, 201, 154, .1); position: relative; z-index: 1; }
.search-scope { background: #081310; border: 1px solid var(--line-soft); border-radius: 9px; display: grid; gap: 3px; grid-template-columns: repeat(2, 1fr); margin-top: 7px; padding: 3px; }
.search-scope button { background: transparent; border: 0; border-radius: 6px; color: var(--muted); font-size: 8px; font-weight: 800; min-height: 26px; }
.search-scope button[aria-pressed="true"] { background: var(--surface-strong); color: var(--accent); }
.filter-row { align-items: end; display: grid; gap: 8px; grid-template-columns: minmax(0, 1fr) auto; margin-top: 9px; }
.filter-row label { display: flex; flex-direction: column; gap: 5px; }
.filter-button { min-height: 39px; }
.filter-button[aria-pressed="true"] { background: var(--accent-soft); border-color: #286653; color: var(--accent); }
.conversation-kind-filter { background: #081310; border: 1px solid var(--line-soft); border-radius: 10px; display: grid; gap: 3px; grid-template-columns: repeat(3, 1fr); margin-top: 9px; padding: 3px; }
.conversation-kind-filter button { background: transparent; border: 0; border-radius: 7px; color: var(--muted); font-size: 9px; font-weight: 800; min-height: 30px; padding: 6px 8px; }
.conversation-kind-filter button:hover { background: var(--surface); color: var(--muted-strong); }
.conversation-kind-filter button[aria-pressed="true"] { background: var(--surface-strong); box-shadow: inset 0 0 0 1px var(--line); color: var(--accent); }

.conversation-list { flex: 1; min-height: 0; overflow-y: auto; padding: 0 10px 10px; scrollbar-color: #2e4c44 transparent; scrollbar-width: thin; }
.conversation-item { background: transparent; border: 1px solid transparent; border-radius: var(--radius); color: inherit; display: grid; gap: 10px; grid-template-columns: auto minmax(0, 1fr) auto; margin: 2px 0; padding: 12px 10px; text-align: left; width: 100%; }
.conversation-item:hover { background: var(--surface); }
.conversation-item.is-active { background: var(--surface-strong); border-color: var(--line); }
.conversation-item.is-search-result { border-color: var(--line-soft); margin-bottom: 5px; }
.conversation-item.is-search-result .conversation-copy > p { -webkit-box-orient: vertical; -webkit-line-clamp: 2; display: -webkit-box; line-height: 1.45; white-space: normal; }
.search-result-context { color: var(--accent); font-size: 8px; font-weight: 800; margin-top: 6px; }
.deleted-result-badge { background: rgba(222, 151, 55, .13); border: 1px solid rgba(222, 151, 55, .35); border-radius: 999px; color: #e4b56e; display: inline-flex; font-size: 7px; letter-spacing: .03em; margin-right: 5px; padding: 2px 5px; text-transform: uppercase; }
.conversation-avatar { align-items: center; background: #17332d; border-radius: 13px; color: #76d8bb; display: flex; font-size: 11px; font-weight: 850; height: 42px; justify-content: center; text-transform: uppercase; width: 42px; }
.conversation-copy { min-width: 0; }
.conversation-title { align-items: center; display: flex; gap: 7px; }
.conversation-title strong { font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-copy > p { color: var(--muted); font-size: 10px; line-height: 1.35; margin: 5px 0 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-tags { display: flex; gap: 5px; margin-top: 7px; overflow: hidden; }
.conversation-tags span, .metadata-chip { background: #172723; border: 1px solid #29423b; border-radius: 999px; color: #9fb4ae; display: inline-flex; font-size: 8px; font-weight: 750; max-width: 130px; overflow: hidden; padding: 3px 6px; text-overflow: ellipsis; white-space: nowrap; }
.conversation-meta { align-items: flex-end; display: flex; flex-direction: column; gap: 7px; }
.conversation-meta time { color: #71867f; font-size: 8px; white-space: nowrap; }
.unread-badge { align-items: center; background: var(--accent); border-radius: 999px; color: #05271d; display: flex; font-size: 8px; font-weight: 900; height: 18px; justify-content: center; min-width: 18px; padding: 0 5px; }
.load-more { margin: 0 20px 14px; }

.thread-pane { background: #0b1412; display: flex; flex-direction: column; min-width: 0; overflow: hidden; position: relative; }
.thread-pane::before { background-image: radial-gradient(circle at 1px 1px, rgba(117, 158, 147, .06) 1px, transparent 0); background-size: 24px 24px; content: ""; inset: 0; pointer-events: none; position: absolute; }
.thread-header { align-items: center; background: rgba(13, 26, 24, .96); border-bottom: 1px solid var(--line); display: grid; gap: 11px; grid-template-columns: auto minmax(0, 1fr) auto; min-height: 84px; padding: 14px 22px; position: relative; }
.contact-avatar { border-radius: 13px; font-size: 12px; height: 44px; width: 44px; }
.thread-identity { display: flex; flex-direction: column; min-width: 0; }
.thread-identity strong { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-identity small { color: var(--muted); font-size: 9px; margin-top: 3px; }
.thread-metadata { align-items: center; display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; max-width: 390px; }
.thread-tools { align-items: end; display: flex; gap: 7px; grid-column: 2 / -1; justify-content: flex-end; }
.date-jump-form { align-items: end; display: grid; grid-template-columns: minmax(120px, 155px) auto; }
.date-jump-form label { color: var(--muted); font-size: 8px; font-weight: 750; grid-column: 1 / -1; margin-bottom: 4px; }
.date-jump-form input { border-radius: 9px 0 0 9px; color-scheme: dark; min-height: 32px; padding: 5px 8px; }
.date-jump-form button { min-height: 32px; }
.thread-tools .secondary-button { min-height: 32px; }
.metadata-chip.stage { border-color: #36544c; color: #d7e5e1; }
.mobile-back { display: none; }
.thread-empty { align-items: center; display: flex; flex: 1; flex-direction: column; justify-content: center; padding: 30px; position: relative; text-align: center; }
.thread-empty img { filter: saturate(.75); height: 68px; opacity: .55; width: 68px; }
.thread-empty h2 { font-size: 20px; letter-spacing: -.035em; margin: 18px 0 0; }
.thread-empty p { color: var(--muted); font-size: 11px; line-height: 1.6; margin: 8px 0 0; max-width: 420px; }
.thread-content { display: flex; flex: 1; flex-direction: column; min-height: 0; position: relative; }
.history-action { display: flex; justify-content: center; min-height: 48px; padding: 10px; }
.history-action button { min-height: 30px; }
.history-newer { min-height: 42px; padding-block: 6px; }
.message-list { display: flex; flex: 1; flex-direction: column; gap: 7px; min-height: 0; overflow-y: auto; padding: 8px clamp(22px, 5vw, 72px) 26px; scrollbar-color: #2e4c44 transparent; scrollbar-width: thin; }
.message { align-self: flex-start; background: #16241f; border: 1px solid #263b34; border-radius: var(--radius); max-width: min(680px, 78%); padding: 9px 11px 7px; }
.message.is-outbound { align-self: flex-end; background: #174236; border-color: #255f4e; }
.message.is-system { align-self: center; background: #17201e; color: var(--muted); max-width: 86%; text-align: center; }
.message.is-preview { border-style: dashed; margin-top: auto; opacity: .88; }
.message.is-highlighted { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(40, 201, 154, .12); }
.message.is-deleted { border-color: rgba(222, 151, 55, .42); }
.message-deleted-state { color: #e4b56e; display: block; font-size: 8px; font-weight: 800; letter-spacing: .01em; margin-bottom: 6px; }
.message-sync-state { color: var(--accent); display: block; font-size: 8px; font-weight: 800; margin-bottom: 6px; }
.message-refresh-hint { align-self: center; color: var(--muted); font-size: 8px; margin: 1px 0 auto; }
.message-sender { color: var(--accent); display: block; font-size: 8px; font-weight: 800; margin-bottom: 4px; }
.message p { font-size: 11px; line-height: 1.48; margin: 0; overflow-wrap: anywhere; white-space: pre-wrap; }
.message-media { align-items: center; background: rgba(5, 18, 14, .32); border-radius: 9px; color: var(--muted-strong); display: flex; font-size: 9px; gap: 7px; margin-bottom: 6px; padding: 8px; }
.message footer { align-items: center; color: #8fa59f; display: flex; font-size: 8px; gap: 5px; justify-content: flex-end; margin-top: 5px; }
.read-only-note { align-items: center; background: var(--surface); border-top: 1px solid var(--line); display: flex; gap: 11px; min-height: 64px; padding: 12px 22px; }
.read-only-note span { background: var(--accent-soft); border: 1px solid #286653; border-radius: 999px; color: var(--accent); font-size: 8px; font-weight: 850; padding: 5px 8px; white-space: nowrap; }
.read-only-note p { color: var(--muted); font-size: 9px; margin: 0; }

.empty-list, .error-list { align-items: center; color: var(--muted); display: flex; flex-direction: column; font-size: 10px; justify-content: center; min-height: 220px; padding: 28px; text-align: center; }
.empty-list strong, .error-list strong { color: var(--ink); font-size: 12px; margin-bottom: 6px; }
.error-list strong { color: var(--danger); }
.skeleton-row { align-items: center; display: grid; gap: 10px; grid-template-columns: 42px minmax(0, 1fr); padding: 13px 10px; }
.skeleton-row i, .skeleton-row span { background: #162723; border-radius: 8px; display: block; }
.skeleton-row i { height: 42px; }
.skeleton-row span { height: 9px; margin: 5px 0; width: 80%; }
.skeleton-row span:last-child { opacity: .65; width: 58%; }
.message-list .skeleton-row { align-self: flex-start; width: 55%; }
.message-list .skeleton-row.is-wide { width: 68%; }
.message-list .skeleton-row.is-right { align-self: flex-end; }

.auth-screen { align-items: center; background: var(--bg); display: flex; inset: 0; justify-content: center; min-height: 100dvh; padding: 22px; position: fixed; }
.auth-card { background: #f3f7f5; border-radius: 20px; box-shadow: 0 34px 100px rgba(0, 0, 0, .45); color: #15231f; overflow: hidden; width: min(440px, 100%); }
.auth-brand { align-items: center; background: #102a26; color: #eef8f5; display: flex; gap: 11px; padding: 18px 22px; }
.auth-brand img { height: 42px; width: 42px; }
.auth-brand span { display: flex; flex-direction: column; }
.auth-brand strong { font-size: 13px; }
.auth-brand small { color: #98b7ae; font-size: 9px; margin-top: 2px; }
.auth-body { padding: 28px; }
.auth-body form { display: flex; flex-direction: column; }
.auth-body h1 { font-size: 27px; letter-spacing: -.05em; line-height: 1.05; margin: 0; }
.auth-body form > p:not(.auth-kicker) { color: #60726c; font-size: 11px; line-height: 1.6; margin: 9px 0 20px; }
.auth-body label { color: #42534e; font-size: 9px; font-weight: 800; margin: 10px 0 5px; }
.auth-body input { background: white; border: 1px solid #c9d7d2; border-radius: 10px; color: #15231f; min-height: 43px; outline: 0; padding: 10px 11px; }
.auth-body input::placeholder { color: #788984; }
.auth-body input:focus { border-color: #258a6d; box-shadow: 0 0 0 3px rgba(17, 156, 121, .12); }
.primary-button { background: #087c61; border: 1px solid #087c61; border-radius: 10px; color: white; font-size: 11px; font-weight: 850; margin-top: 18px; min-height: 43px; padding: 10px 14px; }
.primary-button:hover { background: #066e56; }
.text-button { align-self: center; background: transparent; border: 0; color: #176f5b; font-size: 9px; font-weight: 800; margin-top: 13px; }
.auth-error { background: #ffebed; border: 1px solid #efc2c7; border-radius: 9px; color: #983b46; font-size: 9px; line-height: 1.4; margin-top: 10px; padding: 8px 9px; }
.toast { background: #eaf7f2; border: 1px solid #b7d9cd; border-radius: 10px; bottom: 18px; box-shadow: 0 14px 40px rgba(0,0,0,.3); color: #12372d; font-size: 10px; font-weight: 750; left: 50%; max-width: min(460px, calc(100vw - 36px)); padding: 10px 13px; position: fixed; transform: translateX(-50%); }

@media (max-width: 1120px) {
  .app-shell { grid-template-columns: 72px 350px minmax(360px, 1fr); }
  .app-rail { align-items: center; padding-inline: 10px; }
  .brand { padding: 0; }
  .brand span, .rail-link b, .rail-account > span:not(.rail-avatar), .rail-account button { display: none; }
  .rail-link { justify-content: center; padding: 11px; }
  .rail-account { display: flex; padding-inline: 0; }
}

@media (max-width: 760px) {
  body { overflow: auto; }
  .app-shell { grid-template-columns: 1fr; height: 100dvh; min-height: 540px; }
  .app-rail { display: none; }
  .conversation-pane { border: 0; grid-column: 1; grid-row: 1; }
  .thread-pane { display: none; grid-column: 1; grid-row: 1; }
  body.thread-open .conversation-pane { display: none; }
  body.thread-open .thread-pane { display: flex; }
  .thread-header { grid-template-columns: auto auto minmax(0, 1fr); min-height: 76px; padding: 12px 14px; }
  .mobile-back { display: inline-flex; grid-row: 1; }
  .contact-avatar { grid-column: 2; }
  .thread-identity { grid-column: 3; }
  .thread-metadata { grid-column: 1 / -1; justify-content: flex-start; max-width: none; }
  .thread-tools { grid-column: 1 / -1; justify-content: flex-start; width: 100%; }
  .date-jump-form { flex: 1; grid-template-columns: minmax(0, 1fr) auto; }
  .message-list { padding: 8px 14px 22px; }
  .message { max-width: 88%; }
  .read-only-note { padding-inline: 14px; }
}

@media (prefers-reduced-motion: no-preference) {
  button, .conversation-item, .rail-link { transition: background-color .16s ease, border-color .16s ease, color .16s ease, transform .12s ease; }
  button:active { transform: translateY(1px); }
}
