:root {
  color-scheme: light;
  --bg: #eef3f8;
  --panel: #ffffff;
  --panel-strong: #f7fafc;
  --border: #d4dde6;
  --border-strong: #c0ccd8;
  --text: #162330;
  --muted: #647487;
  --accent: #2777c7;
  --accent-strong: #145caa;
  --accent-soft: #dcecff;
  --danger: #b53d3d;
  --incoming: #ffffff;
  --outgoing: #dff0ff;
  --shadow: 0 18px 48px rgba(17, 31, 48, 0.08);
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(180deg, #f8fbfe 0%, var(--bg) 100%);
  color: var(--text);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app-shell {
  min-height: 100vh;
  padding: 18px;
}

.topbar,
.auth-card,
.sidebar,
.thread-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px;
  border-radius: 22px;
}

.topbar h1,
.sidebar h2,
.thread-heading h2,
.auth-card h2,
.composer-header h3 {
  margin: 0;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.12em;
}

.topbar-actions {
  display: grid;
  justify-items: end;
  gap: 10px;
}

.topbar-buttons { display: flex; align-items: center; gap: 8px; }
.icon-button { width: 44px; height: 44px; padding: 0; display: grid; place-items: center; font-size: 22px; }

.session-label,
.muted {
  margin: 0;
  color: var(--muted);
}

.global-status {
  margin: 14px 2px 0;
  color: var(--muted);
  min-height: 20px;
}

main {
  margin-top: 14px;
}

.auth-panel {
  display: grid;
  place-items: center;
  min-height: calc(100vh - 170px);
}

.auth-card {
  width: min(100%, 460px);
  border-radius: 24px;
  padding: 28px;
}

.auth-form,
.search-field,
.composer {
  display: grid;
  gap: 10px;
}

.auth-form {
  margin-top: 18px;
}

label {
  font-weight: 650;
}

input[type="number"],
input[type="text"],
input[type="search"],
textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  background: #fff;
  color: var(--text);
  padding: 12px 14px;
}

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

button,
.file-button {
  border: 0;
  border-radius: 14px;
  padding: 11px 16px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  text-decoration: none;
}

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

.secondary-button {
  background: var(--panel-strong);
  color: var(--accent-strong);
  border: 1px solid var(--border);
}

.secondary-button:hover {
  background: #ecf4fc;
}

select {
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  padding: 9px 11px;
  font: inherit;
}

.message-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.sort-control { display: flex; align-items: center; gap: 8px; margin-left: auto; font-size: 14px; }
.photo-preview { display: block; width: min(100%, 520px); max-height: 480px; object-fit: contain; border-radius: 8px; background: #eef3f8; }
.media-attachment audio { width: min(100%, 420px); }
.media-attachment video { display: block; width: min(100%, 520px); max-height: 520px; border-radius: 8px; background: #101820; }

.settings-dialog { width: min(92vw, 620px); max-height: 86vh; border: 1px solid var(--border); border-radius: 12px; padding: 0; color: var(--text); }
.settings-dialog::backdrop { background: rgba(22, 35, 48, 0.45); }
.settings-header { position: sticky; top: 0; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px; border-bottom: 1px solid var(--border); background: #fff; }
.settings-header h2, .settings-section h3 { margin: 0; }
.settings-section { display: grid; gap: 10px; padding: 16px; }
.settings-section + .settings-section { border-top: 1px solid var(--border); }
.settings-peer-list { list-style: none; display: grid; gap: 6px; margin: 0; padding: 0; }
.settings-peer-list li { display: flex; justify-content: space-between; gap: 12px; padding: 10px; border: 1px solid var(--border); border-radius: 8px; }
.settings-peer-list small { color: var(--muted); }
.settings-peer-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.settings-peer-actions label { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); }
.settings-peer-actions input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); }
.settings-peer-actions input[type="number"] { width: 112px; min-width: 0; border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; color: var(--text); background: #fff; }
.settings-dialog-results { display: grid; gap: 4px; min-height: 120px; max-height: 240px; overflow-y: auto; padding: 4px; border: 1px solid var(--border); border-radius: 8px; }
.settings-dialog-result { width: 100%; display: grid; gap: 2px; padding: 8px 10px; text-align: left; background: transparent; color: var(--text); }
.settings-dialog-result small { color: var(--muted); }
.settings-dialog-result.selected { background: var(--accent); color: #fff; }
.settings-dialog-result.selected small { color: inherit; }

.back-button {
  display: none;
  flex: 0 0 auto;
}

.inbox-panel {
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  gap: 16px;
  align-items: stretch;
}

.sidebar,
.thread-panel {
  border-radius: 24px;
  min-height: calc(100vh - 170px);
}

.sidebar {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  padding: 18px;
}

.sidebar-header,
.thread-header,
.composer-header,
.composer-footer,
.thread-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.search-field {
  margin-top: 16px;
}

.search-field span {
  font-size: 14px;
}

.conversation-list {
  list-style: none;
  margin: 16px 0 0;
  padding: 0;
  display: grid;
  gap: 8px;
  align-content: start;
  max-height: 100%;
  overflow-y: auto;
}

.conversation-item {
  border: 1px solid var(--border);
  border-radius: 18px;
  background: #fff;
}

.conversation-button {
  width: 100%;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  padding: 14px;
  display: grid;
  gap: 6px;
}

.conversation-button:hover {
  background: #f8fbff;
}

.conversation-item.is-active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.conversation-topline,
.conversation-bottomline {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.conversation-title {
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-preview {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

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

.empty-list,
.thread-empty {
  display: grid;
  place-items: center;
  min-height: 240px;
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

.thread-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  padding: 18px;
}

.thread-actions {
  display: grid;
  justify-items: end;
  gap: 6px;
}

.export-status {
  min-height: 18px;
  text-align: right;
}

.backfill-control {
  display: flex;
  align-items: center;
  gap: 6px;
}

.backfill-control select {
  padding: 5px 7px;
  font-size: 12px;
}

.thread-content {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
  min-height: 0;
}

.message-toolbar {
  display: flex;
  justify-content: center;
}

.message-list {
  list-style: none;
  margin: 0;
  padding: 4px;
  overflow-y: auto;
  display: grid;
  gap: 10px;
  align-content: start;
}

.message-row {
  display: flex;
}

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

.message-bubble {
  width: min(100%, 720px);
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--incoming);
  padding: 12px 14px;
}

.message-row.outgoing .message-bubble {
  background: var(--outgoing);
}

.message-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 12px;
}

.message-body {
  margin: 8px 0 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message-attachments,
.attachment-list {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.message-attachment,
.composer-attachment {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.72);
}

.message-attachment a {
  color: var(--accent-strong);
  font-weight: 700;
  text-decoration: none;
}

.message-attachment a:hover {
  text-decoration: underline;
}

.attachment-line {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.remove-attachment {
  border: 0;
  background: transparent;
  color: var(--danger);
  padding: 0;
  font-weight: 700;
}

.composer {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--panel-strong);
}

.composer-footer {
  align-items: end;
}

.composer-footer .muted {
  flex: 1;
}

.file-button {
  display: inline-block;
}

#attachment-input {
  position: absolute;
  left: -9999px;
}

body.mobile-thread .sidebar {
  display: none;
}

body.mobile-list .thread-panel {
  display: none;
}

@media (max-width: 900px) {
  .app-shell {
    padding: 12px;
  }

  .inbox-panel {
    grid-template-columns: 1fr;
  }

  .sidebar,
  .thread-panel {
    min-height: calc(100vh - 156px);
  }

  .back-button {
    display: inline-flex !important;
  }
}

@media (max-width: 640px) {
  .topbar,
  .thread-header,
  .sidebar-header,
  .thread-header-row,
  .composer-header,
  .composer-footer {
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar-actions,
  .thread-actions {
    justify-items: start;
  }

  .sidebar,
  .thread-panel,
  .auth-card {
    border-radius: 18px;
  }

  .message-bubble {
    width: 100%;
  }
}

/* Compact archive workspace: page stays fixed while lists scroll independently. */
@media (min-width: 901px) {
  .app-shell { height: 100dvh; max-width: 1440px; margin: 0 auto; padding: 8px; overflow: hidden; }
  .topbar { padding: 10px 14px; border-radius: 10px; }
  .topbar h1 { font-size: 24px; }
  .global-status { margin: 6px 2px; }
  main { height: calc(100dvh - 104px); min-height: 0; margin-top: 0; }
  .inbox-panel { height: 100%; min-width: 0; gap: 8px; }
  .sidebar, .thread-panel { height: 100%; min-width: 0; min-height: 0; overflow: hidden; border-radius: 10px; padding: 12px; }
}

.conversation-button { padding: 9px 10px; }
.message-bubble { width: min(86%, 680px); border-radius: 9px; padding: 8px 10px; }
.message-body { margin-top: 4px; }
.composer { padding: 10px; border-radius: 10px; }
.settings-dialog { overflow-x: hidden; }
.settings-dialog input, .settings-dialog select, .settings-dialog button { max-width: 100%; min-width: 0; }
.message-flags { min-height: 14px; color: var(--muted); font-size: 11px; font-style: italic; }
.message-flags:empty { display: none; }
.message-actions { display: flex; justify-content: flex-end; margin-top: 4px; }
.message-action { padding: 3px 6px; border-radius: 6px; background: transparent; color: var(--accent-strong); font-size: 12px; }
.message-action:hover { background: rgba(39, 119, 199, 0.1); }
.article-body { display: block; line-height: 1.48; }
.article-body a { color: var(--accent-strong); font-weight: 650; }
.article-body blockquote { margin: 8px 0; padding: 6px 10px; border-left: 3px solid var(--accent); color: #33485d; background: #f4f8fc; }
.article-body code, .article-body pre { border: 1px solid var(--border); border-radius: 6px; background: #f6f8fa; font-family: Consolas, "Courier New", monospace; }
.article-body code { padding: 1px 4px; }
.article-body pre { margin: 8px 0; padding: 8px 10px; overflow-x: auto; white-space: pre; }
.tg-spoiler { border-radius: 4px; background: #9aa8b7; color: transparent; }
.tg-spoiler:hover, .tg-spoiler:focus { color: inherit; background: #e6edf4; }
.message-context { margin-top: 5px; color: var(--muted); font-size: 11px; font-weight: 650; }
.article-toggle { margin-top: 5px; }
.message-attachments:has(.photo-preview) { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.message-attachments:has(.photo-preview) .photo-preview { width: 100%; aspect-ratio: 4 / 3; max-height: 280px; object-fit: cover; }

/* Dense archive workspace inspired by Telegram Web's information layout. */
html,
body {
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.app-shell {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  width: min(100%, 1440px);
  height: 100dvh;
  min-height: 0;
  margin: 0 auto;
  padding: 6px;
}

.topbar {
  min-height: 54px;
  padding: 7px 12px;
  border-radius: 8px;
  box-shadow: none;
}

.topbar h1 {
  font-size: 22px;
  line-height: 1.1;
}

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

.topbar-buttons {
  gap: 6px;
}

.icon-button {
  width: 36px;
  height: 36px;
  font-size: 18px;
}

.topbar button {
  padding: 8px 11px;
  border-radius: 8px;
}

.global-status {
  min-height: 22px;
  margin: 3px 4px;
  font-size: 13px;
  line-height: 16px;
}

main {
  min-height: 0;
  margin-top: 0;
  overflow: hidden;
}

.inbox-panel {
  grid-template-columns: minmax(240px, 320px) minmax(0, 1fr);
  height: 100%;
  min-width: 0;
  min-height: 0;
  gap: 6px;
}

.sidebar,
.thread-panel {
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 8px;
  overflow: hidden;
  border-radius: 8px;
  box-shadow: none;
}

.sidebar {
  grid-template-rows: auto auto minmax(0, 1fr);
}

.sidebar-header h2,
.thread-heading h2 {
  font-size: 18px;
  line-height: 1.2;
}

.sidebar-header .muted,
.thread-heading .muted {
  font-size: 12px;
}

.sidebar-header button,
.thread-header button,
.message-toolbar button {
  padding: 7px 10px;
  border-radius: 8px;
  font-size: 13px;
}

.search-field {
  display: block;
  margin-top: 7px;
}

.search-field span {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

input[type="search"] {
  height: 36px;
  padding: 7px 10px;
  border-radius: 8px;
  font-size: 14px;
}

.sidebar nav {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.conversation-list {
  min-width: 0;
  height: 100%;
  margin-top: 6px;
  gap: 2px;
  scrollbar-width: thin;
}

.conversation-item {
  min-width: 0;
  border: 0;
  border-radius: 7px;
}

.conversation-button {
  min-width: 0;
  min-height: 58px;
  padding: 7px 9px;
  gap: 3px;
  border-radius: 7px;
}

.conversation-topline,
.conversation-bottomline {
  width: 100%;
  min-width: 0;
  gap: 7px;
  overflow: hidden;
}

.conversation-title,
.conversation-preview {
  min-width: 0;
  flex: 1 1 auto;
  display: block;
}

.conversation-time,
.unread-badge {
  flex: 0 0 auto;
}

.conversation-preview {
  font-size: 13px;
  line-height: 18px;
}

.conversation-time {
  font-size: 11px;
}

.unread-badge {
  min-width: 19px;
  height: 19px;
  padding: 0 5px;
}

.thread-panel {
  grid-template-rows: auto minmax(0, 1fr);
}

.thread-header {
  min-width: 0;
  padding: 1px 2px 7px;
}

.thread-header-row,
.thread-heading {
  min-width: 0;
}

.thread-heading {
  overflow: hidden;
}

.thread-heading h2,
.thread-heading p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-actions {
  flex: 0 0 auto;
}

.thread-content {
  gap: 5px;
}

.message-toolbar {
  min-height: 32px;
}

.sort-control {
  gap: 5px;
  font-size: 12px;
}

.sort-control select {
  padding: 5px 7px;
  font-size: 12px;
}

.message-list {
  gap: 5px;
  padding: 2px 5px;
  scrollbar-width: thin;
}

.message-bubble {
  width: min(82%, 680px);
  padding: 7px 9px;
}

.composer {
  display: grid;
  gap: 4px;
  padding: 5px;
  border-radius: 8px;
}

.composer-row {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) auto;
  align-items: end;
  gap: 5px;
  min-width: 0;
}

.composer textarea {
  min-height: 38px;
  max-height: 112px;
  padding: 8px 10px;
  resize: none;
  border-radius: 8px;
}

.composer .file-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
}

.composer .file-button:hover {
  background: #ecf4fc;
  color: var(--accent-strong);
}

.composer #send-button {
  min-height: 38px;
  padding: 7px 13px;
  border-radius: 8px;
}

.composer-status:empty,
.attachment-list:empty {
  display: none;
}

.composer-status {
  padding: 0 43px;
  font-size: 12px;
}

.attachment-list {
  max-height: 88px;
  margin: 0 43px;
  overflow-y: auto;
}

@media (max-width: 900px) {
  .app-shell {
    width: 100%;
    padding: 0;
  }

  .topbar {
    min-height: 50px;
    border-width: 0 0 1px;
    border-radius: 0;
  }

  .topbar .eyebrow,
  .session-label {
    display: none;
  }

  .global-status {
    margin: 2px 8px;
  }

  .inbox-panel {
    display: block;
  }

  .sidebar,
  .thread-panel {
    width: 100%;
    min-height: 0;
    border: 0;
    border-radius: 0;
  }

  .back-button {
    display: inline-flex !important;
  }

  .thread-actions {
    display: none;
  }

  .message-bubble {
    width: min(92%, 680px);
  }
}

@media (max-width: 640px) {
  .topbar,
  .sidebar-header,
  .thread-header,
  .thread-header-row {
    flex-direction: row;
    align-items: center;
  }

  .topbar-actions {
    justify-items: end;
  }

  .sidebar,
  .thread-panel {
    padding: 6px;
  }

  .sidebar-header .muted,
  .thread-heading .muted {
    display: none;
  }

  .message-bubble {
    width: 94%;
  }

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

  .composer .file-button,
  .composer textarea,
  .composer #send-button {
    min-height: 36px;
    height: 36px;
  }
}
