* { box-sizing: border-box; }
html { height: 100%; overflow: hidden; }
body { font-family: monospace; margin: 0; background: #0e1621; color: #d4d4d4; display: flex; height: 100vh; height: 100dvh; overflow: hidden; }
h2   { color: #9cdcfe; margin-top: 0; }
label { display: block; margin: 10px 0 4px; color: #9cdcfe; }
input, textarea {
  width: 100%;
  background: #2d2d2d; border: 1px solid #444; color: #d4d4d4;
  padding: 6px 8px; border-radius: 4px; font-family: monospace; font-size: 14px;
}
textarea { height: 80px; resize: vertical; }
button {
  margin-top: 16px; padding: 8px 24px;
  background: #0e639c; border: none; color: #fff;
  border-radius: 4px; font-size: 14px; cursor: pointer;
}
button:hover { background: #1177bb; }
.ok  { border-color: #4ec9b0; }
.err { border-color: #f44747; }

#identity {
  margin-bottom: 24px; padding: 10px 12px; border-radius: 4px;
  background: #2d2d2d; border: 1px solid #555; font-size: 13px;
  line-height: 1.7;
}
#identity span { color: #ce9178; }
.badge {
  display: inline-block; font-size: 11px; padding: 1px 6px;
  border-radius: 3px; margin-left: 6px; vertical-align: middle;
}
.badge-new  { background: #264f78; color: #9cdcfe; }
.badge-ok   { background: #1e3a1e; color: #4ec9b0; }

/* ── Сообщения ── */
.msg {
  margin-bottom: 10px; padding: 8px 12px; border-radius: 10px;
  font-size: 13px; line-height: 1.6;
  width: 75%; align-self: center;
  background: #1e1e2d; border: 1px solid #3a3a6a;
  color: #888; text-align: center;
}
#chat-header { flex-shrink: 0; }
#chat-header-name { font-size: 14px; font-weight: bold; color: #d4d4d4; }
#chat-header-status { font-size: 11px; color: #555; }

.msg-meta {
  color: #aaa; font-size: 11px; margin-top: 4px;
  display: flex; justify-content: flex-end;
}
.msg-payload { word-break: break-word; }

.msg-left {
  background: #182533; border-color: #081523;
  border-radius: 10px 10px 10px 0;
  align-self: flex-start; text-align: left; color: #d4d4d4;
}
.msg-right {
  background: #2b5278; border-color: #1b4268;
  border-radius: 10px 10px 0 10px;
  align-self: flex-end; text-align: left; color: #fff;
}
.msg-warning {
  background: #3a1a1a; border-color: #f44747; color: #f44747;
  font-weight: bold; max-width: 100%; align-self: stretch; text-align: center;
}


/* ── Layout ── */
#sidebar {
  width: 220px; min-width: 180px; background: #252526; border-right: 1px solid #333;
  display: flex; flex-direction: column; padding: 16px 12px; overflow-y: auto;
}
#sidebar h3 { color: #9cdcfe; margin: 0 0 12px; font-size: 14px; }
#contacts-title { display: flex; align-items: center; gap: 8px; margin: 7px 0 12px; }
#contacts-title h3 { margin: 0; }
#contacts-badge {
  display: none; background: #b8860b; color: #fff;
  font-size: 10px; border-radius: 10px; padding: 1px 6px; font-weight: bold;
}

#contact-list { flex: 1; overflow-y: auto; }
.contact-item {
  position: relative;
  display: flex; align-items: center; gap: 8px;
  padding: 7px 8px; border-radius: 4px; cursor: pointer; font-size: 13px;
  color: #d4d4d4; border: 1px solid transparent; margin-bottom: 4px;
}
.contact-item:hover { background: #2d2d2d; border-color: #444; }
.contact-item.active { background: #264f78; border-color: #0e639c; color: #fff; }
.contact-addr { font-size: 10px; color: #666; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.contact-unread {
  display: inline-block; background: #0e639c; color: #fff;
  font-size: 14px; font-weight: bold; border-radius: 10px;
  padding: 1px 6px; margin-left: 4px; vertical-align: middle;
}
.contact-item.active .contact-unread { background: #fff; color: #264f78; }
.contact-item.unknown { border-color: #5a4a00; background: #2a2200; }
.contact-item.unknown:hover { background: #332a00; }
.contact-item.unknown.active { background: #4a3800; border-color: #b8860b; }
.contact-badge-new { font-size: 9px; background: #b8860b; color: #fff; border-radius: 3px; padding: 1px 4px; margin-left: 4px; vertical-align: middle; }
.contact-actions {
  display: none; position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  gap: 4px; align-items: center;
}
.contact-item:hover .contact-actions { display: flex; }
.contact-actions button {
  margin: 0; padding: 2px 6px; font-size: 11px; border-radius: 3px; line-height: 1.4;
}
.btn-action-add   { background: #1e3a1e; border: 1px solid #4ec9b0; color: #4ec9b0; }
.btn-action-add:hover   { background: #264f26; }
.btn-action-block { background: #3a1e1e; border: 1px solid #f44747; color: #f44747; }
.btn-action-block:hover { background: #4f2626; }
.btn-action-forget { background: #2a2a1e; border: 1px solid #888; color: #888; }
.btn-action-forget:hover { background: #3a3a28; color: #aaa; }
.btn-action-edit  { background: #1e2a3a; border: 1px solid #9cdcfe; color: #9cdcfe; }
.btn-action-edit:hover  { background: #263a4f; }
.btn-action-retry { background: #1e2a3a; border: 1px solid #ce9178; color: #ce9178; }
.btn-action-retry:hover { background: #2a2010; }

#send-area.hidden { display: none; }
#no-key-notice {
  padding: 10px 28px; font-size: 13px; color: #b8860b;
  border-top: 1px solid #333; background: #1e1e1e; flex-shrink: 0;
}

/* ── Layout правой части ── */
#main {
  flex: 1; padding: 0; overflow: hidden;
  display: flex; flex-direction: column;
}
#chat-area {
  flex: 1; overflow: hidden;
  padding: 0;
  display: flex; flex-direction: column;
}
#inbox {
  display: flex; flex-direction: column;
  flex: 1; align-items: stretch;
  padding: 16px 28px;
  overflow-y: auto;
  min-height: 0;
}
/* spacer прижимает сообщения к низу когда их мало */
#inbox-spacer { flex: 1; }
#send-area {
  padding: 16px 28px 20px;
  border-top: 1px solid #333;
  background: #1e1e1e; flex-shrink: 0;
}

/* ── Адаптив: мобильный вид ── */
@media (max-width: 600px) {
  body { position: relative; overflow: hidden; }

  #sidebar {
    position: absolute; top: 0; left: 0; right: 0; bottom: 0;
    width: 100%; z-index: 10;
    border-right: none; padding: 12px 10px;
    transform: translateX(0); transition: transform .25s ease;
    overflow-y: auto;
  }
  /* Когда выбран чат — сайдбар уходит влево */
  body.mobile-chat #sidebar {
    transform: translateX(-100%);
  }
  #main {
    position: absolute; top: 0; left: 0; right: 0; bottom: 0;
    z-index: 5;
    transform: translateX(100%); transition: transform .25s ease;
    display: flex; flex-direction: column; overflow: hidden;
  }
  body.mobile-chat #main {
    transform: translateX(0);
  }

  /* chat-area — flex-колонка без скролла, inbox сам скроллится */
  #chat-area {
    flex: 1; min-height: 0; overflow: hidden;
  }
  #inbox {
    overflow-y: auto; min-height: 0;
  }

  /* send-area всегда внизу, не выходит за экран */
  #send-area {
    flex-shrink: 0;
    padding: 8px 12px 12px;
    /* запас под системную навигацию на телефоне */
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  /* Кнопка «назад» в заголовке чата */
  #btn-back {
    display: flex; align-items: center; justify-content: center;
    background: none; border: none; color: #9cdcfe;
    font-size: 20px; padding: 0 8px 0 0; margin: 0;
    cursor: pointer; flex-shrink: 0; line-height: 1;
  }
  #btn-back:hover { color: #4ec9b0; background: none; }

  #inbox { padding: 10px 12px; }
  #chat-header { padding: 10px 12px !important; }

  .msg { width: 85%; }

  .modal { width: 95vw !important; padding: 16px; }
}

/* На широких экранах кнопка «назад» скрыта */
@media (min-width: 601px) {
  #btn-back { display: none; }
}

/* ── Модальное окно ── */
.modal-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.6);
  align-items: center; justify-content: center; z-index: 100;
}
.modal-overlay.show { display: flex; }
.modal {
  background: #252526; border: 1px solid #555; border-radius: 6px;
  padding: 24px; width: 480px; max-width: 95vw;
}
.modal h3 { color: #9cdcfe; margin-top: 0; }
.modal label { color: #9cdcfe; display: block; margin: 10px 0 4px; font-size: 13px; }
.modal input { width: 100%; background: #2d2d2d; border: 1px solid #444; color: #d4d4d4;
  padding: 6px 8px; border-radius: 4px; font-family: monospace; font-size: 13px; }
.modal textarea { width: 100%; height: 80px; resize: vertical;
  background: #2d2d2d; border: 1px solid #444; color: #d4d4d4;
  padding: 6px 8px; border-radius: 4px; font-family: monospace; font-size: 12px; }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; justify-content: flex-end; }
.modal-actions button { margin-top: 0; }
.btn-cancel { background: #3a3a3a; }
.btn-cancel:hover { background: #444; }
.modal input[readonly] { color: #888; border-color: #333; cursor: default; }
#modal-addr-preview { font-size: 11px; color: #666; margin-top: 4px; min-height: 16px; }

#btn-manage-contacts {
  background: none; border: none; color: #555; font-size: 14px;
  cursor: pointer; padding: 0; margin: 0; line-height: 1;
}
#btn-manage-contacts:hover { color: #9cdcfe; background: none; }
#modal-contacts-list { max-height: 400px; overflow-y: auto; margin-top: 12px; }
.mc-section-title { font-size: 11px; color: #666; margin: 12px 0 6px; text-transform: uppercase; letter-spacing: 1px; }
.mc-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 8px; border-radius: 4px; margin-bottom: 3px;
  background: #2d2d2d; font-size: 13px;
}
.mc-item.blocked { background: #2a1a1a; color: #888; }
.mc-item-info { flex: 1; }
.mc-item-addr { font-size: 10px; color: #555; margin-top: 2px; }
.mc-item-actions { display: flex; gap: 6px; flex-shrink: 0; }
.mc-item-actions button { margin: 0; padding: 2px 8px; font-size: 11px; border-radius: 3px; line-height: 1.5; }

