/* =========================================================
   Messenger — пятицветная палитра.
   Все цвета приложения строго из набора:
     #D7755A   терракота — primary accent (CTA, активные табы, исходящий бабл)
     #7B7A72   тёплый серый — шапки, текст, аватары
     #C3C2A9   сэйдж — фон чата, вторичные поверхности
     #B4B3A9   тёплый бордюр — разделители, неактивное состояние
     #FFFFFF   белый — основной фон
   ========================================================= */

* { box-sizing: border-box; }

:root {
  --c-accent:  #D7755A;
  --c-text:    #7B7A72;
  --c-sage:    #2B2B2B;
  --c-line:    #3A3A3A;
  --c-bg:      #1F1F1F;

  --bg:         var(--c-bg);
  --bg-2:       #252525;
  --panel:      #242424;
  --panel-2:    #2E2E2E;
  --panel-3:    #3A3A3A;
  --line:       var(--c-line);

  --text:       #E7E7E7;
  --text-strong:#FFFFFF;
  --text-dim:   #B5B5B5;

  --accent:     var(--c-accent);
  --accent-2:   #C46449;
  --accent-3:   var(--c-accent);

  --bubble-in:  #303030;
  --bubble-out: #3F3F3F;
  --bubble-out-text: #E7E7E7;

  --bubble-bg:  #2A2A2A;

  --danger:     #D7755A;
  --danger-2:   #C46449;

  --header-bg:  #232323;
  --header-fg:  #FFFFFF;

  --tabbar-bg:  #1C1C1C;
  --tabbar-active:   var(--c-accent);
  --tabbar-inactive: #9A9A9A;

  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);

  --ease: cubic-bezier(0.4, 0, 0.2, 1);
}

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text-strong);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: calc(15px + var(--ui-fs-bump, 0px));
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

input, textarea, select, button {
  font-size: calc(16px + var(--ui-fs-bump, 0px));
  font-family: inherit;
}

/* SVG icon helper */
.ic {
  width: 22px;
  height: 22px;
  display: inline-block;
  vertical-align: middle;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}
.ic-lg { width: 26px; height: 26px; }
.ic-sm { width: 18px; height: 18px; }
.ic-xs { width: 14px; height: 14px; }

/* ==== centered card pages (gate/auth) ==== */
.centered {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: calc(20px + var(--sat)) calc(20px + var(--sar)) calc(20px + var(--sab)) calc(20px + var(--sal));
  background:
    linear-gradient(180deg, var(--c-accent) 0%, var(--c-accent) 40%, var(--c-sage) 40%, var(--c-sage) 100%);
}

.card {
  background: var(--panel);
  border-radius: 16px;
  padding: 28px;
  box-shadow: 0 12px 36px rgba(123,122,114,0.22);
  width: 100%;
}
.narrow { max-width: 380px; }
.card h1 { margin: 0 0 8px; font-size: calc(22px + var(--ui-fs-bump, 0px)); font-weight: 600; color: var(--text-strong); }
.muted { color: var(--text-dim); }
.small { font-size: calc(12px + var(--ui-fs-bump, 0px)); }

input {
  width: 100%;
  padding: 12px 14px;
  background: var(--c-sage);
  color: var(--text-strong);
  border-radius: 12px;
  border: 1.5px solid transparent;
  outline: none;
  margin: 8px 0;
  transition: border-color 0.15s var(--ease), background 0.15s var(--ease);
}
input::placeholder { color: var(--text-dim); }
input:focus { border-color: var(--c-accent); background: var(--c-bg); }

button {
  cursor: pointer;
  border: none;
  outline: none;
  border-radius: 24px;
  background: var(--c-accent);
  color: var(--c-bg);
  padding: 12px 18px;
  width: 100%;
  font-weight: 600;
  min-height: 44px;
  transition: background 0.15s var(--ease), transform 0.1s var(--ease);
  box-shadow: 0 1px 2px rgba(123,122,114,0.18);
}
button:hover:not(:disabled) { background: var(--accent-2); }
button:active:not(:disabled) { transform: scale(0.97); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.error { color: var(--c-accent); margin-top: 10px; min-height: 18px; }

.tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.tab {
  background: transparent;
  color: var(--text-dim);
  border: 1.5px solid var(--line);
  padding: 10px;
  border-radius: 10px;
  box-shadow: none;
}
.tab.active { background: var(--c-accent); color: var(--c-bg); border-color: var(--c-accent); }
.tab-pane { display: none; }
.tab-pane.active { display: block; }

/* === main app layout === */
.app {
  position: relative;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--c-bg);
}

/* === sidebar === */
.sidebar {
  background: #202020;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-left: var(--sal);
  border-right: 1px solid var(--line);
  z-index: 1;
}

.me {
  display: flex;
  align-items: center;
  padding: 14px 16px;
  padding-top: calc(14px + var(--sat));
  background: var(--header-bg);
  color: var(--header-fg);
  gap: 12px;
  min-height: calc(56px + var(--sat));
}
.me-info { flex: 1; min-width: 0; }
.me-info .name { font-weight: 600; font-size: calc(19px + var(--ui-fs-bump, 0px)); color: var(--c-bg); }
.me-info .muted.small { color: rgba(255,255,255,0.72); }

.icon-btn {
  background: transparent;
  color: var(--header-fg);
  width: 40px;
  height: 40px;
  padding: 0;
  font-size: 0;
  border-radius: 50%;
  min-height: 40px;
  box-shadow: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover:not(:disabled) { background: rgba(255,255,255,0.14); }
.icon-btn:active:not(:disabled) { background: rgba(255,255,255,0.22); }

.add-contact {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--c-bg);
  border-bottom: 1px solid var(--line);
}
.add-contact input {
  margin: 0;
  flex: 1;
  min-width: 0;
  height: 48px;
  background: var(--c-sage);
  border-radius: 24px;
  padding: 0 18px;
  font-size: calc(15px + var(--ui-fs-bump, 0px));
}
.add-contact input:focus { background: var(--c-bg); }
.add-contact button {
  width: 48px; height: 48px;
  border-radius: 50%;
  font-size: 0;
  padding: 0;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#addErr { padding: 4px 16px; }

.contacts {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  -webkit-overflow-scrolling: touch;
}
/* Контейнер-обёртка для свайп-удаления */
.contact-row {
  position: relative;
  list-style: none;
  overflow: hidden;
  background: var(--c-accent);
}
.contact-row .swipe-action {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 24px;
  gap: 8px;
  background: var(--c-accent);
  color: #fff;
  cursor: pointer;
  font-weight: 600;
  font-size: calc(14px + var(--ui-fs-bump, 0px));
  border: none;
  width: 100%;
  border-radius: 0;
  box-shadow: none;
  z-index: 0;
}
.contact-row .swipe-action .ic { width: 18px; height: 18px; }

.contact {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
  position: relative;
  background: #202020;
  transition: background 0.15s var(--ease), transform 0.22s var(--ease);
  touch-action: pan-y;
  z-index: 1;
  user-select: none;
  -webkit-user-select: none;
}
.contact.swiping { transition: none; }
.contact.swiped { transform: translateX(-96px); }
.contact:hover {
  background: #2D2D2D;
}

.contact:active,
.contact.active {
  background: #343434;
}
.contact .avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--c-text);
  color: var(--c-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: calc(19px + var(--ui-fs-bump, 0px));
  flex-shrink: 0;
}
.contact .info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.contact .info .name {
  font-weight: 500;
  font-size: calc(16px + var(--ui-fs-bump, 0px));
  color: var(--text-strong);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 4px;
}
.contact .info .login {
  color: var(--text-dim);
  font-size: calc(13px + var(--ui-fs-bump, 0px));
}
.contact .lock { color: var(--text-dim); opacity: 0.55; }

.contact .online-dot { display: none; }
.contact .info .status-text {
  font-size: calc(12px + var(--ui-fs-bump, 0px));
  color: var(--text-dim);
  margin-top: 2px;
  letter-spacing: 0.2px;
}
.contact .info .status-text.online { color: var(--c-accent); font-weight: 500; }

.chat-header .peer-status {
  font-size: calc(12px + var(--ui-fs-bump, 0px));
  font-weight: 400;
  color: rgba(255,255,255,0.72);
  margin-top: 1px;
}
.chat-header .peer-status.online { color: var(--c-accent); }
.contact .unread {
  background: var(--c-accent);
  color: var(--c-bg);
  border-radius: 12px;
  padding: 2px 8px;
  font-size: calc(12px + var(--ui-fs-bump, 0px));
  font-weight: 600;
  min-width: 22px;
  text-align: center;
}
.contact .unread.hidden { display: none; }
.contact .remove-btn { display: none; }

/* === FAB === */
.fab {
  position: absolute;
  right: 16px;
  bottom: calc(82px + var(--sab));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--c-accent);
  color: var(--c-bg);
  font-size: 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  cursor: pointer;
  box-shadow: 0 8px 18px rgba(215,117,90,0.45);
  z-index: 5;
  transition: transform 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.fab:hover { box-shadow: 0 8px 22px rgba(215,117,90,0.55); }
.fab:active { transform: scale(0.94); }

/* === bottom tab bar === */
.tab-bar {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  display: flex;
  background: var(--tabbar-bg);
  border-top: 1px solid var(--line);
  padding-bottom: var(--sab);
  z-index: 4;
  box-shadow: 0 -2px 12px rgba(123,122,114,0.08);
}
.tab-bar button {
  flex: 1;
  background: transparent;
  color: var(--tabbar-inactive);
  border-radius: 0;
  padding: 8px 4px;
  font-size: calc(11px + var(--ui-fs-bump, 0px));
  font-weight: 500;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  width: auto;
  min-height: 56px;
  box-shadow: none;
}
.tab-bar button:hover:not(:disabled) { background: rgba(195,194,169,0.30); }
.tab-bar button.active { color: var(--tabbar-active); }
.tab-bar .ic { width: 22px; height: 22px; }

/* === chat (main area) === */
.chat {
  display: flex;
  flex-direction: column;
  background: #252525;
  overflow: hidden;
  position: relative;
  padding-right: var(--sar);
}

.chat-header {
  background: var(--header-bg);
  color: var(--header-fg);
  padding: 8px 14px;
  padding-top: calc(8px + var(--sat));
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: calc(56px + var(--sat));
  flex-shrink: 0;
}
.chat-header.empty { justify-content: center; color: rgba(255,255,255,0.85); }
.chat-header.empty .actions,
.chat-header.empty .back-btn,
.chat-header.empty .chat-avatar { display: none; }
.chat-header .chat-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--c-accent);
  color: var(--c-bg);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600;
  flex-shrink: 0;
  font-size: calc(15px + var(--ui-fs-bump, 0px));
}
.chat-header .name {
  flex: 1; font-weight: 600; font-size: calc(16px + var(--ui-fs-bump, 0px)); min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-header .name .muted { font-weight: 400; font-size: calc(12px + var(--ui-fs-bump, 0px)); color: rgba(255,255,255,0.7); }
.chat-header .actions { display: flex; gap: 2px; flex-shrink: 0; }

.chat-header .fingerprint {
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
  font-size: calc(9px + var(--ui-fs-bump, 0px));
  color: rgba(255,255,255,0.65);
  font-weight: 400;
  margin-top: 2px;
  letter-spacing: 0.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.back-btn {
  display: none;
  margin-left: -4px;
  flex-shrink: 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.msg {
  max-width: 80%;
  padding: 7px 10px 9px;
  border-radius: 12px;
  word-wrap: break-word;
  white-space: pre-wrap;
  position: relative;
  font-size: calc(14.5px + var(--ui-fs-bump, 0px));
  animation: msgIn 0.18s var(--ease);
  box-shadow: 0 1px 1.5px rgba(123,122,114,0.18);
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}
.msg.in {
  align-self: flex-start;
  background: #343434;
  color: var(--text-strong);
  border-top-left-radius: 3px;
}
.msg.out {
  align-self: flex-end;
  background: var(--bubble-out);
  color: var(--bubble-out-text);
  border-top-right-radius: 3px;
}
.msg .ts {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: calc(10px + var(--ui-fs-bump, 0px));
  color: var(--text-dim);
  margin-left: 8px;
  float: right;
  margin-top: 4px;
  user-select: none;
}
.msg.out .ts { color: rgba(231,231,231,0.7); }

/* reply (quote) preview above input */
.reply-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: #1F1F1F;
  border-top: 1px solid var(--line);
}
.reply-preview.hidden { display: none; }
.reply-preview-stripe {
  width: 3px;
  align-self: stretch;
  background: var(--c-accent);
  border-radius: 2px;
}
.reply-preview-thumb {
  width: 40px; height: 40px;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
  background: #1a1a1a;
}
.reply-preview-thumb.hidden { display: none; }
.reply-preview-text {
  flex: 1;
  min-width: 0;
}
.reply-preview-author {
  color: var(--c-accent);
  font-size: calc(13px + var(--ui-fs-bump, 0px));
  font-weight: 600;
  line-height: 1.2;
}
.reply-preview-snippet {
  color: var(--text-dim);
  font-size: calc(13px + var(--ui-fs-bump, 0px));
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.reply-preview .icon-btn { color: var(--text-dim); flex-shrink: 0; }

/* quoted block inside a message bubble */
.msg-reply {
  display: flex;
  align-items: stretch;
  gap: 8px;
  padding: 6px 8px;
  margin-bottom: 4px;
  background: rgba(255,255,255,0.06);
  border-radius: 6px;
  border-left: 3px solid var(--c-accent);
  font-size: calc(13px + var(--ui-fs-bump, 0px));
  min-width: 140px;
}
.msg.out .msg-reply { background: rgba(0,0,0,0.18); }
.msg-reply-thumb {
  width: 38px; height: 38px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
  align-self: center;
  background: #1a1a1a;
}
.msg-reply-text { min-width: 0; flex: 1; }
.msg-reply-author { color: var(--c-accent); font-weight: 600; line-height: 1.2; }
.msg-reply-snippet {
  color: var(--text-dim);
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 240px;
}

/* message selection mode */
.selection-bar.hidden { display: none; }
.chat.selecting #chatHeader { display: none; }
.selection-bar .name { font-weight: 600; color: var(--text-strong); }
.selection-bar .actions .icon-btn:disabled { opacity: 0.35; pointer-events: none; }

.msg .sel-mark {
  position: absolute;
  top: 6px;
  left: -28px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--c-bg);
  border: 2px solid var(--text-dim);
  display: none;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 14px;
  z-index: 2;
}
.msg.out .sel-mark { left: auto; right: -28px; }
.chat.selecting .msg .sel-mark { display: inline-flex; }
.chat.selecting .msg { cursor: pointer; }
.chat.selecting .msg.selected .sel-mark {
  background: var(--c-accent);
  border-color: var(--c-accent);
}
.chat.selecting .msg.selected {
  outline: 2px solid rgba(215,117,90,0.55);
  outline-offset: 2px;
  border-radius: 12px;
}
.chat.selecting .msg.msg-att .att-img { cursor: pointer; }
.chat.selecting .msg.msg-att.selected .att-img { filter: brightness(0.7); }
/* На мобильных, где сообщения шире, ставим маркер внутри */
@media (max-width: 640px) {
  .msg .sel-mark { left: 4px; top: 4px; background: rgba(0,0,0,0.55); border-color: rgba(255,255,255,0.6); }
  .msg.out .sel-mark { left: 4px; right: auto; }
}

.forward-list {
  max-height: 320px;
  overflow-y: auto;
  margin: 6px 0 4px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.forward-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 8px;
  border-radius: 8px;
  cursor: pointer;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  color: var(--text-strong);
  font: inherit;
  min-height: 56px;
}
.forward-item:hover { background: var(--panel-2); }
.forward-item .avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--c-text);
  color: var(--c-bg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: calc(15px + var(--ui-fs-bump, 0px));
  flex-shrink: 0;
}
.forward-item .meta { min-width: 0; flex: 1; }
.forward-item .meta .name { font-weight: 500; font-size: calc(14.5px + var(--ui-fs-bump, 0px)); }
.forward-item .meta .login { color: var(--text-dim); font-size: calc(12px + var(--ui-fs-bump, 0px)); }

/* image lightbox viewer */
.img-viewer {
  background: rgba(0,0,0,0.92);
  padding: 0;
  z-index: 300;
  flex-direction: column;
  align-items: stretch;
  justify-content: stretch;
}
.img-viewer-header {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + var(--sat)) 14px 10px;
  z-index: 2;
  background: linear-gradient(to bottom, rgba(0,0,0,0.55), transparent);
  color: #fff;
}
.img-viewer-counter {
  font-size: calc(14px + var(--ui-fs-bump, 0px));
  color: rgba(255,255,255,0.85);
  font-variant-numeric: tabular-nums;
}
.img-viewer-actions { display: flex; gap: 6px; }
.img-viewer-header .icon-btn { color: #fff; background: transparent; }
.img-viewer-header .icon-btn:hover:not(:disabled) { background: rgba(255,255,255,0.12); }
.img-viewer-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}
.img-viewer-stage img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: default;
  cursor: zoom-out;
}
.img-viewer-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.12);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 3;
  font-size: 0;
}
.img-viewer-nav:hover { background: rgba(255,255,255,0.22); }
.img-viewer-nav:disabled { opacity: 0; pointer-events: none; }
.img-viewer-nav.prev { left: 12px; }
.img-viewer-nav.next { right: 12px; }
@media (max-width: 640px) {
  .img-viewer-nav { width: 40px; height: 40px; }
  .img-viewer-nav.prev { left: 6px; }
  .img-viewer-nav.next { right: 6px; }
}

/* attachment messages */
.msg.msg-att { padding: 4px 4px 6px; max-width: min(360px, 70%); }
.msg.msg-att .att-img-link { display: block; line-height: 0; }
.msg.msg-att .att-img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 360px;
  object-fit: contain;
  border-radius: 8px;
  background: #1a1a1a;
  cursor: zoom-in;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.msg.msg-att .ts { float: none; display: inline-flex; margin-left: 6px; margin-top: 4px; }
.msg.msg-att .msg-att-caption { padding: 4px 6px 0; line-height: 1.35; }
.msg .ts .status { line-height: 0; display: inline-flex; }
.msg .ts .status .ic { width: 14px; height: 14px; }
.msg .ts .status.delivered { color: var(--c-accent); }
.msg .ts .status.pending   { color: rgba(231,231,231,0.4); }

.send-form {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  padding-bottom: calc(8px + var(--sab));
  background: #1F1F1F;
  border-top: 1px solid var(--line);
  flex-shrink: 0;
  align-items: center;
}
.send-form input {
  margin: 0;
  flex: 1;
  min-width: 0;
  height: 48px;
  border-radius: 24px;
  padding: 0 18px;
  background: #2B2B2B;
  color: #fff;
  border: 1.5px solid transparent;
  font-size: calc(15px + var(--ui-fs-bump, 0px));
}
.send-form input:focus { border-color: var(--c-accent); background: var(--c-bg); }
.send-form button {
  width: 48px; height: 48px;
  border-radius: 50%;
  font-size: 0;
  padding: 0;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.send-form .attach-btn { background: var(--c-sage); color: var(--text-strong); }
.send-form .attach-btn:hover:not(:disabled) { background: var(--c-line); }
.send-form .attach-btn .ic { stroke: currentColor; }

/* === profile screen === */
.profile-pane {
  position: absolute;
  inset: 0;
  bottom: calc(64px + var(--sab));
  background: #1F1F1F;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  z-index: 3;
  transform: translateY(100%);
  transition: transform 0.25s var(--ease);
}
.app.tab-profile .profile-pane { transform: translateY(0); }
.profile-pane.hidden { display: none; }
.profile-header {
  background: var(--header-bg);
  color: var(--header-fg);
  padding: 14px 16px;
  padding-top: calc(14px + var(--sat));
  font-weight: 600;
  font-size: calc(19px + var(--ui-fs-bump, 0px));
  min-height: calc(56px + var(--sat));
  display: flex;
  align-items: center;
  gap: 12px;
}
.profile-card {
  background: var(--c-bg);
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--line);
}
.profile-avatar {
  position: relative;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  background: var(--c-text);
  color: var(--c-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: calc(38px + var(--ui-fs-bump, 0px));
  margin-bottom: 8px;
  border: none;
  padding: 0;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.profile-avatar:hover { transform: scale(1.03); box-shadow: 0 4px 14px rgba(0,0,0,0.18); }
.profile-avatar:focus-visible { outline: 2px solid var(--c-accent, #25d366); outline-offset: 3px; }
.profile-avatar-edit {
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--c-bg);
  color: var(--c-text);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.25);
}
.profile-avatar-edit .ic { width: 14px; height: 14px; }

.fs-options { display: flex; flex-direction: column; gap: 6px; }
.fs-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-2);
  color: var(--text-strong);
  cursor: pointer;
  text-align: left;
  width: 100%;
}
.fs-option:hover { background: var(--panel-2); }
.fs-option.active { border-color: var(--c-accent); background: var(--panel-2); }
.fs-option .fs-label { font-weight: 500; }
.fs-option .fs-sample { color: var(--text-dim); }

/* 2FA / forms inside modals: left-align text, dedicated section spacing */
#twofaModal .modal-card,
#changePwdModal .modal-card,
#editNameModal .modal-card { text-align: left; }
#twofaModal #twofaSetup > * + *,
#twofaModal #twofaEnabled > * + *,
#changePwdModal .modal-card > * + * { margin-top: 10px; }
#twofaModal .call-actions,
#changePwdModal .call-actions { margin-top: 18px; }
#twofaModal input,
#changePwdModal input { margin: 0; }

/* fingerprint verify */
.verify-btn { position: relative; }
.verify-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #b5b5b5;
  border: 2px solid var(--header-bg);
}
.verify-btn.v-verified .verify-badge { background: #5cb85c; }
.verify-btn.v-changed .verify-badge { background: #e25555; box-shadow: 0 0 0 0 rgba(226,85,85,0.7); animation: vpulse 1.4s infinite; }
@keyframes vpulse {
  0%   { box-shadow: 0 0 0 0 rgba(226,85,85,0.65); }
  70%  { box-shadow: 0 0 0 8px rgba(226,85,85,0); }
  100% { box-shadow: 0 0 0 0 rgba(226,85,85,0); }
}
.verify-status {
  padding: 10px 12px;
  border-radius: 8px;
  margin-bottom: 12px;
  font-size: calc(13px + var(--ui-fs-bump, 0px));
  border: 1px solid var(--line);
}
.verify-status.v-unknown { background: rgba(181,181,181,0.12); color: var(--text-dim); }
.verify-status.v-verified { background: rgba(92,184,92,0.14); color: #b6e0b6; border-color: #2e6b2e; }
.verify-status.v-changed { background: rgba(226,85,85,0.16); color: #f3b8b8; border-color: #7a2a2a; }
.verify-section { margin: 12px 0; }
.verify-label { color: var(--text-dim); font-size: calc(12px + var(--ui-fs-bump, 0px)); margin-bottom: 6px; }
.verify-fp {
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
  font-size: calc(13px + var(--ui-fs-bump, 0px));
  background: var(--panel-2);
  border-radius: 8px;
  padding: 10px 12px;
  letter-spacing: 0.5px;
  word-break: break-all;
}
.verify-qr-wrap {
  display: flex;
  justify-content: center;
  background: #fff;
  border-radius: 12px;
  padding: 10px;
}
.verify-qr-wrap img { width: 200px; height: 200px; display: block; }

.scan-video-wrap {
  position: relative;
  width: 100%;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  aspect-ratio: 1;
  margin: 12px 0;
}
.scan-video-wrap video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.scan-overlay {
  position: absolute;
  inset: 14%;
  border: 2px solid rgba(92,184,92,0.9);
  border-radius: 12px;
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.35);
  pointer-events: none;
}
.scan-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
}
.scan-select {
  flex: 1;
  min-width: 0;
  background: var(--c-sage);
  color: var(--text-strong);
  border: 1.5px solid transparent;
  border-radius: 10px;
  padding: 8px 10px;
  font-size: calc(13px + var(--ui-fs-bump, 0px));
}

#verifyModal .modal-card { text-align: left; }
#verifyModal > .modal-card > * + * { margin-top: 10px; }
#verifyModal .call-actions { margin-top: 18px; }

.verify-warning {
  background: rgba(226,85,85,0.14);
  border: 1px solid #7a2a2a;
  color: #f3b8b8;
  padding: 10px 12px;
  border-radius: 8px;
  margin-top: 8px;
  font-size: calc(13px + var(--ui-fs-bump, 0px));
  line-height: 1.45;
}

.contact .name .lock.v-verified { color: #5cb85c; }
.contact .name .lock.v-changed { color: #e25555; }

.profile-name { font-size: calc(19px + var(--ui-fs-bump, 0px)); font-weight: 600; color: var(--text-strong); }
.profile-login { color: var(--text-dim); font-size: calc(14px + var(--ui-fs-bump, 0px)); }
.profile-fingerprint {
  margin-top: 12px;
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
  font-size: calc(11px + var(--ui-fs-bump, 0px));
  color: var(--text-dim);
  text-align: center;
  letter-spacing: 0.5px;
  background: var(--c-sage);
  border-radius: 8px;
  padding: 6px 10px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.profile-section { background: var(--c-bg); margin-top: 8px; }
.profile-row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
  color: var(--text-strong);
  background: var(--c-bg);
  transition: background 0.15s var(--ease);
}
.profile-row:hover { background: var(--c-sage); }
.profile-row:active { background: var(--c-line); }
.profile-row .ic-wrap {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--c-accent);
  color: var(--c-bg);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.profile-row .ic-wrap.alt { background: var(--c-text); }
.profile-row .info { flex: 1; min-width: 0; }
.profile-row .info .title { font-weight: 500; color: var(--text-strong); }
.profile-row .info .sub { font-size: calc(13px + var(--ui-fs-bump, 0px)); color: var(--text-dim); margin-top: 2px; }
.profile-row .chev { color: var(--text-dim); }

/* === modals === */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(61,60,55,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
  padding-top: calc(20px + var(--sat));
  padding-bottom: calc(20px + var(--sab));
  animation: fadeIn 0.2s var(--ease);
}
@keyframes fadeIn { from {opacity:0} to {opacity:1} }
.modal.hidden { display: none; }
.modal-card {
  background: var(--c-bg);
  border-radius: 18px;
  padding: 28px;
  text-align: center;
  min-width: 280px;
  max-width: 380px;
  width: 100%;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  box-shadow: 0 16px 48px rgba(61,60,55,0.30);
  animation: scaleIn 0.22s var(--ease);
  color: var(--text-strong);
}
@keyframes scaleIn {
  from { opacity: 0; transform: scale(0.94); }
  to   { opacity: 1; transform: scale(1); }
}
.call-title { font-size: calc(14px + var(--ui-fs-bump, 0px)); color: var(--text-dim); margin-bottom: 6px; }
.call-from { font-size: calc(22px + var(--ui-fs-bump, 0px)); font-weight: 600; margin-bottom: 24px; color: var(--text-strong); }
.call-actions { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 16px; }
.call-actions button { flex: 1 1 120px; min-width: 0; }

.btn-green { background: var(--c-accent); }
.btn-red { background: var(--c-text); }
.btn-red:hover:not(:disabled) { background: #5C5B54; }
.btn-grey { background: var(--c-sage); color: var(--text-strong); box-shadow: none; }
.btn-grey:hover:not(:disabled) { background: var(--c-line); }

.link-btn {
  background: transparent;
  color: var(--text-dim);
  font-weight: 400;
  font-size: calc(13px + var(--ui-fs-bump, 0px));
  min-height: 0;
  padding: 12px 0 0;
  margin: 0;
  width: auto;
  box-shadow: none;
}
.link-btn:hover:not(:disabled) { background: transparent; color: var(--text-strong); }

.install-card { max-width: 380px; }
.install-icon {
  display: inline-flex;
  width: 60px; height: 60px;
  border-radius: 50%;
  background: var(--c-sage);
  color: var(--c-accent);
  align-items: center; justify-content: center;
  margin-bottom: 12px;
}
.install-card .ios-step {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  text-align: left;
}
.install-card .ios-step .badge {
  background: var(--c-sage);
  color: var(--text-strong);
  border-radius: 6px;
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: calc(13px + var(--ui-fs-bump, 0px));
}
.install-card .ios-step b { color: var(--text-strong); font-weight: 600; }

/* incoming call modal */
#callModal .modal-card {
  background: linear-gradient(180deg, #232323 0%, #111111 100%);
  color: var(--c-bg);
  max-width: 360px;
}
#callModal .call-title { color: rgba(255,255,255,0.75); }
#callModal .call-from { color: var(--c-bg); }
#callModal .call-actions button { border-radius: 28px; height: 50px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }

/* active call panel */
.call-panel {
  position: fixed;
  inset: 0;
  background: linear-gradient(180deg, #1E1E1E 0%, #101010 100%);
  color: var(--c-bg);
  z-index: 150;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  padding-top: calc(40px + var(--sat));
  padding-bottom: calc(20px + var(--sab));
  animation: fadeIn 0.2s var(--ease);
}
.call-panel.hidden { display: none; }
.call-status {
  color: rgba(255,255,255,0.85);
  margin-bottom: 12px;
  font-size: calc(14px + var(--ui-fs-bump, 0px));
  text-align: center;
}
.call-media {
  position: relative;
  width: min(800px, 100%);
  flex: 1;
  max-height: 70vh;
  background: #1a1a17;
  border-radius: 16px;
  overflow: hidden;
  margin-bottom: 20px;
}
#remoteVideo { width: 100%; height: 100%; object-fit: cover; background: #1a1a17; }
#localVideo {
  position: absolute;
  bottom: 12px;
  right: 12px;
  width: 110px;
  height: 80px;
  object-fit: cover;
  border-radius: 10px;
  border: 2px solid var(--c-bg);
  background: #1a1a17;
}
#hangupBtn {
  width: 200px;
  max-width: 80%;
  border-radius: 28px;
  height: 52px;
  background: var(--c-accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

/* === gate page === */
.gate-card { padding: 32px 28px; }
.gate-card input { font-size: calc(17px + var(--ui-fs-bump, 0px)); padding: 14px 16px; }

/* === invite UI === */
.invite-banner {
  background: rgba(215,117,90,0.10);
  border: 1px solid rgba(215,117,90,0.40);
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 16px;
  color: var(--text-strong);
  display: flex;
  align-items: center;
  gap: 14px;
}
.invite-banner.hidden { display: none; }
.invite-avatar {
  width: 52px;
  height: 52px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--c-accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: calc(22px + var(--ui-fs-bump, 0px));
}
.invite-banner-info { flex: 1; min-width: 0; }
.invite-banner-title {
  font-size: calc(11px + var(--ui-fs-bump, 0px));
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--c-accent);
  margin-bottom: 2px;
}
.invite-banner-from {
  font-size: calc(17px + var(--ui-fs-bump, 0px)); font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.invite-banner #inviteFromLogin {
  margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.invites-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
  max-height: 40vh;
  overflow-y: auto;
}
.invite-row {
  background: var(--c-sage);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: calc(13px + var(--ui-fs-bump, 0px));
  display: flex;
  flex-direction: column;
  gap: 4px;
  color: var(--text-strong);
}
.invite-row.used { opacity: 0.55; }
.invite-url {
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
  font-size: calc(11px + var(--ui-fs-bump, 0px));
  color: var(--text-dim);
  word-break: break-all;
  background: var(--c-bg);
  padding: 6px 8px;
  border-radius: 6px;
  user-select: all;
  border: 1px solid var(--line);
}
.invite-row .row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.invite-row .row-actions button {
  width: auto;
  padding: 6px 12px;
  font-size: calc(12px + var(--ui-fs-bump, 0px));
  min-height: 32px;
  font-weight: 500;
  border-radius: 20px;
  box-shadow: none;
}
.invite-row .row-actions .secondary { background: var(--c-bg); color: var(--text-strong); border: 1px solid var(--line); }
.invite-row .row-actions .secondary:hover:not(:disabled) { background: var(--c-line); }
.invite-row .row-actions .danger { background: var(--c-text); }
.invite-row .row-actions .danger:hover:not(:disabled) { background: #5C5B54; }
.invite-row .badge-used {
  background: var(--c-bg);
  color: var(--text-dim);
  font-size: calc(10px + var(--ui-fs-bump, 0px));
  padding: 2px 6px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  display: inline-block;
  align-self: flex-start;
  border: 1px solid var(--line);
}
.invites-modal-card { max-width: 460px; width: 100%; }

/* =====================================
   MOBILE (< 768px)
   ===================================== */
@media (max-width: 767.98px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    grid-row: 1; grid-column: 1;
    width: 100%;
    padding-bottom: calc(64px + var(--sab));
  }
  .chat {
    grid-row: 1; grid-column: 1;
    width: 100%;
    transform: translateX(100%);
    transition: transform 0.26s var(--ease);
    will-change: transform;
    padding-left: var(--sal);
    z-index: 6;
  }
  .app.show-chat .chat {
    transform: translateX(0);
    box-shadow: -8px 0 24px rgba(61,60,55,0.20);
  }
  .app.show-chat .sidebar {
    transform: translateX(-26%);
    transition: transform 0.26s var(--ease);
  }
  .app.show-chat .tab-bar,
  .app.show-chat .fab { display: none; }
  .back-btn { display: inline-flex; align-items: center; justify-content: center; }
  .msg { max-width: 86%; }
  #localVideo { width: 90px; height: 130px; }
  .contact .remove-btn { opacity: 0.55; }
}

@media (max-width: 380px) {
  .msg { font-size: calc(14px + var(--ui-fs-bump, 0px)); }
  .contact { padding: 10px 12px; gap: 10px; }
  .contact .avatar { width: 44px; height: 44px; font-size: calc(17px + var(--ui-fs-bump, 0px)); }
  .me { padding: 12px; padding-top: calc(12px + var(--sat)); }
  .add-contact { padding: 8px 10px; }
  .send-form { padding: 6px 10px; padding-bottom: calc(6px + var(--sab)); }
  .chat-header { padding: 6px 10px; padding-top: calc(6px + var(--sat)); }
}

/* =====================================
   DESKTOP (≥ 768px)
   ===================================== */
@media (min-width: 768px) {
  .app { grid-template-columns: 360px 1fr; background: var(--c-bg); }
  .sidebar { grid-column: 1; border-right: 1px solid var(--line); padding-bottom: 0; }
  .chat { grid-column: 2; transform: none !important; }
  .back-btn { display: none !important; }
  .app.show-chat .sidebar { transform: none; }
  .tab-bar, .fab { display: none; }
  .profile-pane { bottom: 0; transform: translateY(100%); }
  .app.tab-profile .profile-pane { transform: translateY(0); }
}

@media (min-width: 1100px) {
  .app { grid-template-columns: 400px 1fr; }
  .msg { max-width: 65%; }
}

@media (max-width: 932px) and (orientation: landscape) and (max-height: 500px) {
  .me { min-height: 50px; padding: 6px 12px; padding-top: calc(6px + var(--sat)); }
  .chat-header { min-height: 50px; padding: 4px 12px; padding-top: calc(4px + var(--sat)); }
  .send-form { padding: 6px 10px; padding-bottom: calc(6px + var(--sab)); }
  .call-media { max-height: 55vh; }
}

@media (hover: hover) and (pointer: fine) {
  .contacts::-webkit-scrollbar,
  .messages::-webkit-scrollbar,
  .invites-list::-webkit-scrollbar { width: 8px; }
  .contacts::-webkit-scrollbar-thumb,
  .messages::-webkit-scrollbar-thumb,
  .invites-list::-webkit-scrollbar-thumb {
    background: var(--c-line);
    border-radius: 4px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}

.lock-note { margin-top: 14px; line-height: 1.4; color: var(--text-dim); }

.pw-strength { margin: -4px 0 10px; }
.pw-bar { height: 4px; background: var(--panel-2); border-radius: 2px; overflow: hidden; }
.pw-bar-fill {
  height: 100%;
  width: 0%;
  background: #e25555;
  transition: width 0.2s, background 0.2s;
}
.pw-bar-fill[data-score="1"] { background: #e25555; }
.pw-bar-fill[data-score="2"] { background: #e2a655; }
.pw-bar-fill[data-score="3"] { background: #5cb85c; }
.pw-checks { list-style: none; padding: 0; margin: 6px 0 0; font-size: calc(12px + var(--ui-fs-bump, 0px)); color: var(--text-dim); }
.pw-checks li { padding-left: 18px; position: relative; line-height: 1.6; }
.pw-checks li::before { content: '○'; position: absolute; left: 0; color: var(--text-dim); }
.pw-checks li.ok { color: #b6e0b6; }
.pw-checks li.ok::before { content: '✓'; color: #5cb85c; }

/* password input border state — overrides default focus and inline styles */
input.pw-bad,  input.pw-bad:focus  { border-color: #e25555 !important; }
input.pw-weak, input.pw-weak:focus { border-color: #e2a655 !important; }
input.pw-ok,   input.pw-ok:focus   { border-color: #5cb85c !important; }
