:root {
  --bg: #10151d;
  --panel: #18212f;
  --panel-2: #1f2b3a;
  --panel-3: #263447;
  --text: #edf4fb;
  --muted: #9eb0c2;
  --soft: #6f8398;
  --line: rgba(255, 255, 255, 0.1);
  --accent: #4e8df5;
  --accent-2: #2fb889;
  --danger: #e06b64;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.34);
  --sidebar: 320px;
}

/* Світла тема - перемикається через data-theme на <html>, дивись
   applyPrefs() у app.js. Той самий набір змінних, що й :root вище, тому
   решта CSS не потребує жодних додаткових змін - усе вже посилається на
   var(--bg)/var(--text)/... */
:root[data-theme="light"] {
  --bg: #f3f5f9;
  --panel: #ffffff;
  --panel-2: #eef1f6;
  --panel-3: #e2e7ef;
  --text: #16202c;
  --muted: #57697c;
  --soft: #7c8ea1;
  --line: rgba(15, 23, 33, 0.12);
  --accent: #3b73d6;
  --accent-2: #1f9e73;
  --danger: #c94a43;
  --shadow: 0 24px 70px rgba(20, 30, 45, 0.14);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

button,
input,
textarea {
  font: inherit;
}

button {
  color: inherit;
}

.screen {
  display: none;
  width: 100%;
  height: 100%;
}

.screen.active {
  display: flex;
}

.login-screen {
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    linear-gradient(135deg, rgba(78, 141, 245, 0.13), transparent 36%),
    radial-gradient(circle at 80% 20%, rgba(47, 184, 137, 0.16), transparent 30%),
    var(--bg);
}

.login-panel {
  width: min(420px, 100%);
  padding: 32px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(24, 33, 47, 0.92);
  box-shadow: var(--shadow);
}

.brand-mark {
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  border-radius: 16px;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  font-size: 26px;
  font-weight: 800;
}

h1 {
  margin: 18px 0 4px;
  font-size: 34px;
  line-height: 1;
  letter-spacing: 0;
}

.login-subtitle,
.quiet-note {
  color: var(--muted);
  line-height: 1.5;
}

.quiet-note {
  margin: 18px 0 0;
  font-size: 13px;
}

.field-label {
  display: block;
  margin: 22px 0 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.text-field,
.search-input,
.message-input {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  outline: none;
}

.text-field,
.search-input {
  height: 44px;
  border-radius: 10px;
  padding: 0 13px;
}

.text-field:focus,
.search-input:focus,
.message-input:focus {
  border-color: rgba(78, 141, 245, 0.75);
  box-shadow: 0 0 0 3px rgba(78, 141, 245, 0.14);
}

.handle-field {
  position: relative;
}

.field-error {
  display: block;
  margin-top: 4px;
  font-size: 12px;
  color: #e06b64;
}

.handle-field .text-field {
  padding-left: 26px;
}

.handle-prefix {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-dim, #8b98a9);
  font-weight: 700;
  pointer-events: none;
}

.password-field {
  position: relative;
}

.password-field .text-field {
  padding-right: 44px;
}

.password-toggle {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: var(--text-dim, #8b98a9);
  cursor: pointer;
  font-size: 16px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.password-toggle:hover {
  background: var(--panel-3, rgba(255, 255, 255, 0.06));
}

.avatar-picker {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.color-choice,
.avatar {
  --avatar-color: #4e8df5;
  --avatar-text: #fff;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--avatar-color);
  color: var(--avatar-text);
  font-weight: 800;
}

.color-choice {
  width: 38px;
  height: 38px;
  cursor: pointer;
  border: 2px solid transparent;
}

.color-choice.selected {
  border-color: #fff;
}

.primary-button,
.soft-button,
.icon-button,
.send-button {
  border: 0;
  cursor: pointer;
}

.primary-button {
  width: 100%;
  height: 46px;
  margin-top: 20px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
}

.primary-button:disabled {
  opacity: 0.6;
  cursor: wait;
}

.secondary-button {
  width: 100%;
  height: 46px;
  border-radius: 10px;
  background: transparent;
  border: 1px solid var(--line);
  color: #e06b64;
  font-weight: 700;
  cursor: pointer;
}

.secondary-button:hover {
  background: rgba(224, 107, 100, 0.1);
  border-color: rgba(224, 107, 100, 0.4);
}

.neutral-button {
  width: 100%;
  height: 46px;
  border-radius: 10px;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
}

.neutral-button:hover {
  background: var(--panel-3);
}

/* Дві кнопки в один ряд (замість типового одна-кнопка-на-всю-ширину) -
   .secondary-button/.neutral-button самі мають width:100%, тож тут явно
   перемикаємо на flex:1, інакше обидві намагались би зайняти всю ширину
   одночасно. */
.button-row {
  gap: 10px;
}

.button-row > button {
  width: auto;
  flex: 1;
}

.soft-button {
  height: 36px;
  padding: 0 14px;
  border-radius: 10px;
  background: rgba(47, 184, 137, 0.13);
  color: #cdf3e5;
  border: 1px solid rgba(47, 184, 137, 0.25);
  font-weight: 700;
}

.icon-button {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--panel-2);
  border: 1px solid var(--line);
}

.login-error {
  margin-top: 14px;
  padding: 10px 12px;
  border-radius: 10px;
  color: #ffe2df;
  background: rgba(224, 107, 100, 0.16);
}

.app-shell {
  background: var(--bg);
}

.sidebar {
  width: var(--sidebar);
  min-width: var(--sidebar);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: var(--panel);
}

.sidebar-header,
.chat-header {
  height: 64px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
}

.me {
  min-width: 0;
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
}

.profile-copy {
  min-width: 0;
}

.avatar {
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
}

.avatar.small {
  width: 30px;
  height: 30px;
  font-size: 12px;
}

.avatar.room {
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
}

.me-name,
.chat-header-name,
.chat-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 800;
}

.status-line,
.chat-header-status,
.chat-preview {
  color: var(--muted);
  font-size: 12px;
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent-2);
  box-shadow: 0 0 10px rgba(47, 184, 137, 0.7);
}

.status-dot.offline {
  background: var(--danger);
  box-shadow: none;
}

.sidebar-tools {
  padding: 12px;
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 8px;
}

.tool-button {
  height: 38px;
  border: 1px solid rgba(78, 141, 245, 0.28);
  border-radius: 10px;
  background: rgba(78, 141, 245, 0.12);
  color: #cfe0ff;
  cursor: pointer;
  font-weight: 800;
}

.section-label {
  padding: 12px 14px 5px;
  color: var(--soft);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.chat-list.compact {
  flex: 0 0 auto;
  max-height: 230px;
}

.chat-item {
  position: relative;
  width: 100%;
  min-height: 64px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  margin-bottom: 4px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  text-align: left;
}

.chat-item:hover,
.chat-item.active {
  background: var(--panel-2);
}

.chat-item.active {
  border-color: rgba(78, 141, 245, 0.35);
}

.chat-copy {
  min-width: 0;
  flex: 1;
  display: grid;
  gap: 3px;
}

.lock-pill {
  padding: 3px 6px;
  border-radius: 7px;
  background: rgba(78, 141, 245, 0.12);
  color: #bcd4ff;
  font-size: 10px;
  font-weight: 800;
}

.unread-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent-2);
}

.empty-state {
  padding: 12px;
  color: var(--muted);
  font-size: 13px;
}

.chat-area {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.chat-title {
  min-width: 0;
  flex: 1;
}

.messages-wrap {
  min-height: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 22px 18px;
}

.empty-chat-state {
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px;
  padding-top: 15vh;
  color: var(--text-dim, #8b98a9);
  text-align: center;
}

/* Авторський CSS переважає над стандартним [hidden]{display:none} з
   UA-стилю браузера, коли є конкуруюче правило display - без цього
   .empty-chat-state{display:flex} лишав би плейсхолдер видимим навіть
   з атрибутом hidden, і повідомлення чату та плейсхолдер показувались
   би одночасно. */
.empty-chat-state[hidden] {
  display: none;
}

.message {
  max-width: min(680px, 82%);
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin: 0 0 10px;
}

.message.mine {
  margin-left: auto;
  flex-direction: row;
}

.bubble {
  padding: 10px 12px;
  border-radius: 13px;
  background: var(--panel-2);
  border: 1px solid var(--line);
}

.message.mine .bubble {
  background: #224a7e;
  border-color: rgba(78, 141, 245, 0.3);
}

.sender {
  margin-bottom: 4px;
  color: #acd1ff;
  font-size: 12px;
  font-weight: 800;
}

.message-text {
  font-size: 15px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

body[data-font-size="large"] .message-text {
  font-size: 17px;
}

.message-meta {
  margin-top: 5px;
  color: rgba(237, 244, 251, 0.58);
  font-size: 11px;
  text-align: right;
}

.unread-badge {
  flex: 0 0 auto;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent, #4e8df5);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  line-height: 20px;
  text-align: center;
}

.load-more {
  display: block;
  margin: 8px auto 16px;
  padding: 8px 16px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.load-more:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.08);
}

.system-message {
  width: fit-content;
  max-width: 82%;
  margin: 12px auto;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
  font-size: 12px;
}

.typing-indicator {
  min-height: 24px;
  padding: 0 20px;
  color: var(--muted);
  font-size: 13px;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 12px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}

.message-input {
  min-height: 46px;
  max-height: 132px;
  resize: none;
  border-radius: 12px;
  padding: 12px 14px;
}

.attach-button {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
}

.attach-button:disabled {
  opacity: 0.5;
  cursor: default;
}

.upload-status {
  padding: 6px 14px;
  border-top: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  font-size: 13px;
}

.messages.drag-over {
  outline: 2px dashed var(--accent);
  outline-offset: -8px;
}

.file-image {
  display: block;
  max-width: min(320px, 100%);
  max-height: 360px;
  border-radius: 10px;
  cursor: zoom-in;
}

.file-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 220px;
  max-width: 100%;
  height: 140px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 8px;
}

.file-download {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.04);
  color: inherit;
  font: inherit;
  cursor: pointer;
  text-align: left;
}

.file-download small {
  color: var(--muted);
}

.send-button {
  width: 46px;
  height: 46px;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 24px;
}

.modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.55);
  z-index: 20;
}

.modal.active {
  display: flex;
}

.modal-card {
  width: min(480px, 100%);
  max-height: calc(100vh - 36px);
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.settings-card {
  width: min(560px, 100%);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 16px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

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

.modal-body {
  padding: 16px;
  color: var(--muted);
  line-height: 1.55;
  overflow-y: auto;
  min-height: 0;
}

.modal-body strong {
  color: var(--text);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
}

.settings-grid {
  display: grid;
  gap: 12px;
}

.settings-grid label span {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.recovery-phrase-box {
  margin-top: 12px;
  padding: 16px;
  border-radius: 10px;
  background: var(--panel-3, rgba(255, 255, 255, 0.06));
  border: 1px solid var(--line);
  font-family: monospace;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.5px;
  text-align: center;
  color: var(--accent);
  word-break: break-word;
}

.push-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.push-row .neutral-button {
  flex: 0 0 auto;
  width: auto;
  margin: 0;
  padding: 0 14px;
}

.invite-banner {
  margin: 16px 0 0;
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(78, 141, 245, 0.12);
  border: 1px solid rgba(78, 141, 245, 0.35);
  font-size: 14px;
  line-height: 1.4;
}

.invite-banner.invalid {
  background: rgba(232, 92, 92, 0.12);
  border-color: rgba(232, 92, 92, 0.4);
}

.invite-link-box {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.invite-link-box .neutral-button {
  flex: 0 0 auto;
  width: auto;
  margin: 0;
}

.invite-link-box .text-field {
  flex: 1;
  min-width: 0;
  font-family: monospace;
  font-size: 12px;
}

.invite-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.invite-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.04);
  font-size: 13px;
  color: var(--muted);
}

.invite-list li.active {
  color: inherit;
}

.invite-list button {
  background: none;
  border: none;
  color: var(--danger, #e85c5c);
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  padding: 0;
}

.settings-section-title {
  margin: 20px 0 10px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  font-size: 13px;
  font-weight: 800;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.settings-checkbox {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  color: var(--text);
  cursor: pointer;
}

.settings-checkbox input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}

.modal-note {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.fingerprint {
  margin-top: 12px;
  padding: 12px;
  border-radius: 10px;
  background: var(--panel-2);
}

.fingerprint span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.fingerprint strong {
  display: block;
  margin-top: 5px;
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--panel-3);
  color: var(--text);
  box-shadow: var(--shadow);
  z-index: 30;
}

.mobile-only {
  display: none;
}

@media (max-width: 760px) {
  :root {
    --sidebar: 100%;
  }

  .mobile-only {
    display: grid;
  }

  .app-shell {
    position: relative;
  }

  .sidebar {
    position: absolute;
    inset: 0 auto 0 0;
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 180ms ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .message {
    max-width: 92%;
  }

  .soft-button {
    width: 38px;
    padding: 0;
    overflow: hidden;
  }

  .login-panel {
    padding: 24px;
  }
}
