:root {
  color-scheme: light;
  --bg: #f7f8fb;
  --surface: #ffffff;
  --surface-2: #eef3f6;
  --line: #dfe6ec;
  --text: #162027;
  --muted: #6f7f8d;
  --accent: #0b8f7a;
  --accent-strong: #087464;
  --accent-soft: #dff5ef;
  --danger: #b84a52;
  --warning: #fff3d7;
  --warning-line: #f0d59c;
  --shadow: 0 18px 50px rgba(21, 32, 39, 0.12);
  --small-shadow: 0 8px 28px rgba(21, 32, 39, 0.08);
  --radius: 8px;
  --app-height: 100vh;
  --keyboard-offset: 0px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@supports (height: 100svh) {
  :root {
    --app-height: 100svh;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #101416;
    --surface: #171d20;
    --surface-2: #20282c;
    --line: #2c373d;
    --text: #eff5f3;
    --muted: #98a8ad;
    --accent: #28c2a3;
    --accent-strong: #36d3b2;
    --accent-soft: #133930;
    --warning: #2b2618;
    --warning-line: #6d5a2e;
    --shadow: 0 18px 50px rgba(0, 0, 0, 0.3);
    --small-shadow: 0 8px 28px rgba(0, 0, 0, 0.22);
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  min-width: 320px;
  -webkit-text-size-adjust: 100%;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
  touch-action: manipulation;
}

.shell {
  height: var(--app-height);
  min-height: var(--app-height);
}

.auth-view {
  height: var(--app-height);
  min-height: var(--app-height);
  display: grid;
  place-items: center;
  padding: 24px;
  overflow-y: auto;
}

.auth-panel {
  width: min(100%, 420px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 22px;
}

.brand-icon,
.empty-icon {
  width: 54px;
  height: 54px;
  border-radius: 14px;
  display: block;
  object-fit: cover;
  background: var(--accent-soft);
}

.brand h1,
.empty-state h2 {
  margin: 0;
  font-size: 26px;
  line-height: 1.1;
}

.brand p,
.empty-state p,
.key-notice p,
.me-meta span,
.peer-title span {
  margin: 4px 0 0;
  color: var(--muted);
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: var(--surface-2);
  border-radius: var(--radius);
  padding: 4px;
  margin-bottom: 18px;
}

.auth-tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  padding: 10px 12px;
  transition: background 160ms ease, color 160ms ease, box-shadow 160ms ease;
}

.auth-tab.is-active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--small-shadow);
}

.auth-form {
  display: grid;
  gap: 14px;
}

.auth-form label,
.search-box {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

input {
  min-height: 44px;
  padding: 0 12px;
}

textarea {
  min-height: 44px;
  max-height: 140px;
  resize: none;
  padding: 11px 12px;
}

input:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

.primary-button,
.send-button,
.ghost-button,
.icon-button {
  min-height: 40px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  transition: transform 140ms ease, background 160ms ease, border 160ms ease, opacity 160ms ease;
}

.primary-button,
.send-button {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
  font-weight: 700;
  padding: 0 16px;
}

.primary-button:hover,
.send-button:hover {
  background: var(--accent-strong);
}

.ghost-button,
.icon-button {
  background: var(--surface);
  color: var(--text);
  border-color: var(--line);
  padding: 0 12px;
}

.ghost-button:hover,
.icon-button:hover {
  background: var(--surface-2);
}

button:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.form-message {
  min-height: 22px;
  margin: 14px 0 0;
  color: var(--danger);
}

.messenger {
  height: var(--app-height);
  min-height: 0;
  background: var(--bg);
  overflow: hidden;
}

.sidebar {
  display: flex;
  height: 100%;
  min-height: 0;
  flex-direction: column;
  gap: 14px;
  padding: 14px;
  background: var(--surface);
  border-right: 1px solid var(--line);
}

.sidebar-top {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
}

.me-card,
.chat-item,
.result-item {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
}

.me-card {
  min-width: 0;
  padding: 6px;
}

.me-card:hover,
.chat-item:hover,
.result-item:hover,
.chat-item.is-active {
  background: var(--surface-2);
}

.me-meta,
.chat-meta,
.result-meta,
.peer-title {
  min-width: 0;
  display: grid;
}

.me-meta strong,
.chat-name,
.result-meta strong,
.peer-title strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.avatar-slot,
.avatar {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border-radius: 50%;
  overflow: hidden;
}

.avatar {
  display: grid;
  place-items: center;
  color: #ffffff;
  background: var(--accent);
  font-weight: 800;
  user-select: none;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.toolbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  color: var(--muted);
}

.switch input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch-track {
  position: relative;
  width: 42px;
  height: 24px;
  border-radius: 999px;
  background: var(--line);
  transition: background 160ms ease;
}

.switch-track::after {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  top: 3px;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.16);
  transition: transform 160ms ease;
}

.switch input:checked + .switch-track {
  background: var(--accent);
}

.switch input:checked + .switch-track::after {
  transform: translateX(18px);
}

.search-results,
.chat-list {
  display: grid;
  gap: 4px;
}

.search-results {
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
  max-height: min(36vh, 260px);
  overflow-y: auto;
}

.result-item,
.chat-item {
  padding: 8px;
}

.list-heading {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.chat-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding-bottom: 16px;
  align-content: start;
  -webkit-overflow-scrolling: touch;
}

.chat-preview,
.chat-time,
.result-meta span {
  color: var(--muted);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-row-top {
  display: flex;
  gap: 8px;
  justify-content: space-between;
}

.unread-badge {
  min-width: 21px;
  height: 21px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: var(--accent);
  color: #ffffff;
  font-size: 12px;
  font-weight: 800;
}

.chat-pane {
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.key-notice {
  flex: 0 0 auto;
  margin: 12px;
  padding: 12px;
  background: var(--warning);
  border: 1px solid var(--warning-line);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.key-notice-content {
  flex: 1 1 auto;
  min-width: 0;
}

.key-recovery-form {
  display: grid;
  grid-template-columns: minmax(0, 220px) auto;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.key-recovery-form input,
.key-recovery-form .primary-button {
  min-height: 38px;
}

.key-recovery-form .primary-button {
  white-space: nowrap;
}

.empty-state {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 10px;
  padding: 24px;
  text-align: center;
}

.conversation {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}

.conversation-header {
  min-height: 66px;
  padding: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(14px);
}

.back-button {
  width: 40px;
  padding: 0;
  font-size: 28px;
  line-height: 1;
}

.messages {
  min-height: 0;
  overflow-y: auto;
  padding: 18px 12px 22px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

.message-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.message-row.outgoing {
  justify-content: flex-end;
}

.message-row.outgoing .avatar {
  order: 2;
}

.bubble {
  width: fit-content;
  max-width: min(74vw, 560px);
  padding: 9px 11px 7px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: 0 5px 18px rgba(21, 32, 39, 0.06);
}

.message-row.outgoing .bubble {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}

.message-text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.42;
}

.message-time {
  display: block;
  margin-top: 4px;
  font-size: 11px;
  color: var(--muted);
}

.message-row.outgoing .message-time {
  color: rgba(255, 255, 255, 0.78);
}

.bubble.is-error {
  border-color: color-mix(in srgb, var(--danger) 42%, var(--line));
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 10px;
  padding: 12px;
  background: var(--surface);
  border-top: 1px solid var(--line);
  position: relative;
  z-index: 2;
}

.send-button {
  min-width: 76px;
}

.thread-empty {
  margin: auto;
  color: var(--muted);
  text-align: center;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 20;
  width: min(calc(100vw - 32px), 420px);
  padding: 11px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  text-align: center;
}

@media (max-width: 780px) {
  body {
    overflow: hidden;
    font-size: 14px;
  }

  input,
  textarea {
    font-size: 16px;
  }

  .auth-view {
    align-items: start;
    padding: max(12px, var(--safe-top)) 12px max(12px, var(--safe-bottom));
  }

  .keyboard-open .auth-view {
    padding-bottom: calc(12px + var(--keyboard-offset) + var(--safe-bottom));
  }

  .auth-panel {
    padding: 18px;
    box-shadow: var(--small-shadow);
  }

  .brand {
    gap: 12px;
    margin-bottom: 18px;
  }

  .brand-icon,
  .empty-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
  }

  .brand h1,
  .empty-state h2 {
    font-size: 24px;
  }

  .auth-form {
    gap: 12px;
  }

  .primary-button,
  .send-button,
  .ghost-button,
  .icon-button {
    min-height: 44px;
  }

  .messenger:not(.chat-open) .chat-pane {
    display: none;
  }

  .messenger.chat-open .sidebar {
    display: none;
  }

  .sidebar {
    gap: 12px;
    padding: calc(10px + var(--safe-top)) 10px calc(10px + var(--safe-bottom));
    border-right: 0;
  }

  .sidebar-top {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
  }

  .ghost-button {
    padding: 0 11px;
  }

  .me-card {
    min-height: 52px;
    gap: 9px;
  }

  .toolbar-row {
    min-height: 36px;
  }

  .switch {
    font-size: 14px;
  }

  .search-box {
    gap: 6px;
    font-size: 12px;
  }

  .result-item,
  .chat-item {
    min-height: 56px;
    padding: 8px 6px;
  }

  .chat-list {
    padding-bottom: 0;
  }

  .chat-preview,
  .chat-time,
  .result-meta span {
    font-size: 12px;
  }

  .chat-row-top {
    align-items: center;
  }

  .unread-badge {
    min-width: 20px;
    height: 20px;
    font-size: 11px;
  }

  .key-notice {
    align-items: flex-start;
    margin: 10px;
    padding: 10px;
  }

  .key-notice .ghost-button {
    flex: 0 0 auto;
    min-height: 36px;
  }

  .key-recovery-form {
    grid-template-columns: 1fr;
  }

  .empty-state {
    padding: 16px;
  }

  .conversation-header {
    min-height: calc(56px + var(--safe-top));
    padding: max(8px, var(--safe-top)) 10px 8px;
    gap: 9px;
  }

  .back-button {
    width: 44px;
    flex: 0 0 44px;
    font-size: 30px;
  }

  .conversation-header .avatar-slot,
  .conversation-header .avatar {
    width: 38px;
    height: 38px;
    flex-basis: 38px;
  }

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

  .keyboard-open .messages {
    padding-bottom: calc(12px + var(--keyboard-offset));
  }

  .message-row {
    gap: 6px;
  }

  .message-row .avatar {
    width: 30px;
    height: 30px;
    flex-basis: 30px;
    font-size: 12px;
  }

  .bubble {
    max-width: calc(100vw - 82px);
    padding: 8px 10px 6px;
    border-radius: 15px;
  }

  .message-text {
    font-size: 14px;
    line-height: 1.38;
  }

  .message-time {
    font-size: 10px;
  }

  .composer {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    padding: 8px 10px calc(8px + var(--safe-bottom));
    transform: translate3d(0, calc(0px - var(--keyboard-offset)), 0);
    will-change: transform;
  }

  .composer textarea {
    min-height: 42px;
    max-height: 112px;
    padding: 10px 11px;
  }

  .send-button {
    min-width: 64px;
    padding: 0 14px;
  }

  .toast {
    bottom: max(10px, var(--safe-bottom));
    width: min(calc(100vw - 20px), 420px);
  }

  .keyboard-open .toast {
    bottom: max(calc(10px + var(--keyboard-offset)), calc(var(--safe-bottom) + var(--keyboard-offset)));
  }
}

@media (max-width: 360px) {
  .sidebar {
    padding-left: 8px;
    padding-right: 8px;
  }

  .avatar-slot,
  .avatar {
    width: 36px;
    height: 36px;
    flex-basis: 36px;
  }

  .me-card,
  .chat-item,
  .result-item {
    gap: 8px;
  }

  .send-button {
    min-width: 58px;
    padding: 0 10px;
  }

  .bubble {
    max-width: calc(100vw - 74px);
  }

  .conversation-header {
    gap: 7px;
  }

  .me-meta span,
  .peer-title span {
    font-size: 12px;
  }
}

@media (min-width: 781px) {
  .messenger {
    display: grid;
    grid-template-columns: minmax(300px, 370px) 1fr;
  }

  .back-button {
    display: none;
  }

  .bubble {
    max-width: min(58vw, 620px);
  }
}
