  :root {
    --teal: #008069;
    --teal-dark: #075e54;
    --panel: #ffffff;
    --bg: #eae6df;
    --bubble-out: #d9fdd3;
    --bubble-in: #ffffff;
    --ink: #111b21;
    --muted: #667781;
    --line: #e9edef;
    --tick-read: #53bdeb;
    --shadow: 0 1px 2px rgba(11, 20, 26, .12);
    --side: 380px;
    --body-bg: #d1d7db;
    --auth-bg-bottom: #d1d7db;
    --head-bg: #f0f2f5;
    --search-input-bg: #f0f2f5;
    --chat-row-hover: #f5f6f6;
    --chat-row-active: #f0f2f5;
    --composer-bg: #f0f2f5;
    --composer-input-bg: #ffffff;
    --empty-bg: #f0f2f5;
    --empty-title: #41525d;
    --profile-sheet-bg: #f3f4f6;
    --profile-back-bg: #ffffff;
    --profile-back-color: #1f2937;
    --profile-card-bg: #ffffff;
    --profile-row-border: #eceff3;
    --profile-label-color: #111827;
    --profile-value-color: #6b7280;
    --profile-inline-bg: #ffffff;
    --profile-inline-border: #cfd6dd;
    --icon-color: #54656f;
    --icon-hover-bg: #e9edef;
    --chat-wallpaper:
      radial-gradient(1200px 700px at 15% -10%, rgba(149, 191, 255, 0.20) 0%, rgba(149, 191, 255, 0) 60%),
      radial-gradient(1000px 620px at 88% 8%, rgba(169, 228, 205, 0.20) 0%, rgba(169, 228, 205, 0) 58%),
      radial-gradient(840px 520px at 52% 118%, rgba(216, 194, 255, 0.16) 0%, rgba(216, 194, 255, 0) 60%),
      linear-gradient(160deg, #f7f9fc 0%, #eef3f9 48%, #ecf2f7 100%);
    font-synthesis: none;
  }
  body[data-theme="dark"] {
    --panel: #111b21;
    --bg: #0b141a;
    --bubble-out: #005c4b;
    --bubble-in: #202c33;
    --ink: #e9edef;
    --muted: #94a3b8;
    --line: #2a3942;
    --body-bg: #0f1720;
    --auth-bg-bottom: #0f1720;
    --head-bg: #202c33;
    --search-input-bg: #202c33;
    --chat-row-hover: #1f2c34;
    --chat-row-active: #2a3942;
    --composer-bg: #202c33;
    --composer-input-bg: #2a3942;
    --empty-bg: #0f1720;
    --empty-title: #dbe4ea;
    --profile-sheet-bg: #0f1720;
    --profile-back-bg: #202c33;
    --profile-back-color: #e2e8f0;
    --profile-card-bg: #111b21;
    --profile-row-border: #24333b;
    --profile-label-color: #e2e8f0;
    --profile-value-color: #9fb2c1;
    --profile-inline-bg: #1c2730;
    --profile-inline-border: #3a4b57;
    --icon-color: #aebac1;
    --icon-hover-bg: #2a3942;
    --shadow: 0 1px 2px rgba(0, 0, 0, .35);
    --chat-wallpaper:
      radial-gradient(900px 560px at 10% -10%, rgba(45, 78, 112, 0.42) 0%, rgba(45, 78, 112, 0) 58%),
      radial-gradient(760px 500px at 92% 10%, rgba(26, 94, 76, 0.34) 0%, rgba(26, 94, 76, 0) 60%),
      radial-gradient(700px 460px at 55% 115%, rgba(63, 49, 101, 0.34) 0%, rgba(63, 49, 101, 0) 58%),
      linear-gradient(160deg, #0d171e 0%, #101d26 50%, #0f1a22 100%);
  }
  * { box-sizing: border-box; }
  html, body { height: 100%; margin: 0; }
  body {
    font-family: "Segoe UI", system-ui, -apple-system, Helvetica, Arial, sans-serif;
    color: var(--ink);
    background: var(--body-bg);
  }
  .hidden { display: none !important; }

  /* ---------- Auth ---------- */
  #auth {
    position: fixed; inset: 0;
    display: grid; place-items: center;
    background:
      linear-gradient(180deg, var(--teal) 0 222px, var(--auth-bg-bottom) 222px 100%);
  }
  .auth-card {
    width: 420px; max-width: calc(100vw - 32px);
    background: var(--panel); border-radius: 4px;
    box-shadow: var(--shadow); padding: 34px 40px 40px;
  }
  .brand {
    display: flex; align-items: center; gap: 12px;
    margin-bottom: 26px; color: var(--muted);
    font-size: 13px; letter-spacing: .14em; text-transform: uppercase;
  }
  .brand .logo {
    width: 38px; height: 38px; border-radius: 50%;
    background: var(--teal); display: grid; place-items: center;
  }
  .brand .logo svg { width: 22px; height: 22px; fill: #fff; }
  .brand .brand-name,
  .empty .big .brand-name {
    display: inline-flex;
    align-items: center;
    gap: 8px;
  }
  .pulse-mark {
    width: 20px;
    height: 20px;
    color: var(--teal);
    flex: 0 0 auto;
    transform: translateY(1px);
  }
  .pulse-mark path {
    stroke: currentColor;
    fill: none;
    stroke-width: 2.15;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
  .empty .big .pulse-mark {
    width: 22px;
    height: 22px;
    transform: translateY(2px);
  }
  h1 { font-size: 22px; font-weight: 600; margin: 0 0 22px; }
  label { display: block; font-size: 12px; color: var(--muted); margin: 14px 0 6px; }
  input[type=text], input[type=email], input[type=password] {
    width: 100%; padding: 11px 12px; border: 1px solid var(--line);
    border-radius: 8px; font-size: 15px; outline: none; transition: border-color .15s;
  }
  input:focus { border-color: var(--teal); }
  .btn {
    margin-top: 22px; width: 100%; padding: 12px; border: 0; border-radius: 24px;
    background: var(--teal); color: #fff; font-size: 15px; font-weight: 600;
    cursor: pointer; transition: filter .15s;
  }
  .btn:hover { filter: brightness(1.06); }
  .btn.btn-secondary {
    background: transparent;
    color: var(--teal);
    border: 1px solid var(--teal);
  }
  .btn.btn-secondary:hover { background: color-mix(in srgb, var(--teal) 8%, transparent); }
  .switch { margin-top: 16px; text-align: center; font-size: 14px; color: var(--muted); }
  .switch a { color: var(--teal); cursor: pointer; font-weight: 600; }
.auth-link-row {
  margin-top: 10px;
}
.password-input-wrap {
  position: relative;
}
.password-input-wrap input[type=password],
.password-input-wrap input[type=text] {
  padding-right: 44px;
}
.password-toggle-btn {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}
.password-toggle-btn:hover {
  background: color-mix(in srgb, var(--teal) 10%, transparent);
  color: var(--teal);
}
.password-toggle-btn.is-visible {
  color: var(--teal);
}
  .err { margin-top: 14px; color: #c0392b; font-size: 13px; min-height: 16px; }
  .auth-notice-text {
    margin-top: 8px;
    color: var(--muted);
    font-size: 14px;
    line-height: 1.5;
  }
  .auth-inline-action {
    margin-top: 10px;
    width: auto;
    padding: 8px 14px;
    border-radius: 12px;
    font-size: 14px;
  }

  /* ---------- Layout ---------- */
  #app { display: grid; grid-template-columns: var(--side) 1fr; height: 100vh; }
  #app.profile-mode { grid-template-columns: minmax(360px, 520px) 1fr; }
  #app.profile-mode .conversation {
    opacity: .22;
    pointer-events: none;
    filter: saturate(.85);
  }
  .sidebar {
    background: var(--panel);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
  }
  .side-head {
    min-height: 60px;
    background: var(--head-bg);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    gap: 12px;
  }
  .me { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
  .avatar {
    width: 40px; height: 40px; border-radius: 50%; flex: 0 0 40px;
    display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 16px;
    overflow: hidden;
    position: relative;
  }
  .avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    display: block;
  }
  .me .name { flex: 1; min-width: 0; font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .icon-btn {
    width: 40px; height: 40px; border: 0; background: transparent; border-radius: 50%;
    cursor: pointer; color: var(--icon-color); display: grid; place-items: center;
  }
  .icon-btn:hover { background: var(--icon-hover-bg); }
  .icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
  .side-actions {
    display: inline-flex;
    align-items: center;
    gap: 2px;
  }
  .side-actions .icon-btn {
    width: 38px;
    min-width: 38px;
    height: 38px;
    padding: 0;
    border-radius: 50%;
  }
  .side-actions .icon-btn svg {
    width: 20px;
    height: 20px;
  }
  .chat-actions {
    background: var(--panel);
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    padding: 8px 10px;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }
  .chat-action-btn {
    border: 1px solid var(--line);
    background: transparent;
    color: var(--ink);
    border-radius: 10px;
    padding: 8px 10px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
  }
  .chat-action-btn:hover {
    background: var(--icon-hover-bg);
  }

  .search { padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
  .search input { background: var(--search-input-bg); border: 0; border-radius: 18px; padding: 9px 14px; color: var(--ink); }

  .chats { overflow-y: auto; flex: 1; }
  .chat-row {
    display: flex; gap: 13px; padding: 11px 16px; cursor: pointer; align-items: center;
    border-bottom: 1px solid var(--line);
  }
  .chat-row:hover { background: var(--chat-row-hover); }
  .chat-row.active { background: var(--chat-row-active); }
  .chat-main { flex: 1; min-width: 0; }
  .chat-top { display: flex; justify-content: space-between; gap: 8px; }
  .chat-title-wrap { display: inline-flex; align-items: center; gap: 8px; min-width: 0; }
  .chat-title { font-size: 16px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .chat-type-pill {
    font-size: 11px;
    line-height: 1;
    padding: 4px 7px;
    border-radius: 999px;
    color: #0f766e;
    background: rgba(15, 118, 110, 0.14);
    flex: 0 0 auto;
  }
  body[data-theme="dark"] .chat-type-pill {
    color: #7ee0d7;
    background: rgba(126, 224, 215, 0.16);
  }
  .group-avatar {
    outline: 2px solid rgba(15, 118, 110, 0.35);
    outline-offset: 1px;
  }
  .group-badge {
    position: absolute;
    right: -2px;
    bottom: -2px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #0f766e;
    color: #fff;
    font-size: 10px;
    border: 2px solid var(--panel);
    display: grid;
    place-items: center;
    line-height: 1;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.22);
  }
  body[data-theme="dark"] .group-avatar { outline-color: rgba(126, 224, 215, 0.45); }
  body[data-theme="dark"] .group-badge {
    background: #0d9488;
    color: #06201d;
  }
  .chat-time { font-size: 12px; color: var(--muted); flex: 0 0 auto; }
  .chat-sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* ---------- Conversation ---------- */
  .conversation { display: flex; flex-direction: column; min-width: 0; }
  .conv-head {
    height: 60px; background: var(--head-bg); display: flex; align-items: center; gap: 13px;
    padding: 0 16px; border-bottom: 1px solid var(--line);
  }
  .conv-back {
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--icon-color);
    cursor: pointer;
    display: none;
    place-items: center;
    flex: 0 0 36px;
  }
  .conv-back:hover { background: var(--icon-hover-bg); }
  .conv-back svg { width: 20px; height: 20px; fill: currentColor; }
  .conv-head .meta { line-height: 1.25; min-width: 0; }
  .conv-head .title { font-weight: 600; font-size: 16px; }
  .conv-head .title.group {
    display: inline-flex;
    align-items: center;
    gap: 7px;
  }
  .conv-head .title.group::before {
    content: "👥";
    font-size: 14px;
    transform: translateY(-1px);
  }
  .conv-head .members { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .thread {
    flex: 1; overflow-y: auto; padding: 18px 8% 12px;
    background-color: var(--bg);
    background-image:
      var(--chat-wallpaper),
      url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160' viewBox='0 0 160 160'%3E%3Cg fill='none' stroke='%23ffffff' stroke-opacity='0.18' stroke-width='1'%3E%3Ccircle cx='24' cy='24' r='2'/%3E%3Ccircle cx='104' cy='46' r='2'/%3E%3Ccircle cx='68' cy='118' r='2'/%3E%3Cpath d='M18 92c12-8 24-8 36 0s24 8 36 0 24-8 36 0'/%3E%3C/g%3E%3C/svg%3E");
    background-blend-mode: normal, soft-light;
  }
  .day { text-align: center; margin: 8px 0 16px; }
  .day span {
    background: #ffffffcc; color: var(--muted); font-size: 12px;
    padding: 5px 12px; border-radius: 8px; box-shadow: var(--shadow);
  }
  .msg { max-width: 65%; margin: 2px 0; padding: 6px 9px 8px; border-radius: 8px;
    box-shadow: var(--shadow); position: relative; font-size: 14.2px; line-height: 1.32; word-wrap: break-word; }
  .msg.in { background: var(--bubble-in); align-self: flex-start; border-top-left-radius: 0; }
  .msg.out { background: var(--bubble-out); align-self: flex-end; border-top-right-radius: 0; }
  .msg .sender { font-size: 12.5px; font-weight: 600; color: var(--teal-dark); margin-bottom: 2px; }
  .msg .meta-line { float: right; margin: 6px 0 -4px 10px; font-size: 11px; color: var(--muted); display: inline-flex; align-items: center; gap: 3px; }
  .tick { width: 16px; height: 11px; }
  .tick.read { color: var(--tick-read); }
  .msg .att { margin: 4px 0; }
  .msg .att img { max-width: 240px; border-radius: 6px; display: block; }
  .msg .att a { color: var(--teal-dark); font-size: 13px; text-decoration: none; display: inline-flex; gap: 6px; align-items: center;
    background: #f5f6f6; padding: 8px 10px; border-radius: 6px; }
  .rows { display: flex; flex-direction: column; }

  .composer {
    background: var(--composer-bg); padding: 10px 16px; display: flex; align-items: center; gap: 10px;
  }
  .composer input[type=text] {
    flex: 1; border: 0; border-radius: 22px; padding: 11px 16px; font-size: 15px; background: var(--composer-input-bg); color: var(--ink);
  }
  .composer .send {
    width: 46px; height: 46px; border: 0; border-radius: 50%; background: var(--teal);
    color: #fff; cursor: pointer; display: grid; place-items: center;
  }
  .composer .send svg { width: 22px; height: 22px; fill: #fff; }
  .attach-name { font-size: 12px; color: var(--muted); max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .empty { flex: 1; display: grid; place-items: center; background: var(--empty-bg); color: var(--muted); text-align: center; padding: 40px; }
  .empty .big { font-size: 26px; color: var(--empty-title); font-weight: 300; margin-bottom: 8px; }

  /* ---------- Profile panel ---------- */
  .profile-sheet {
    position: absolute;
    inset: 0;
    background: var(--profile-sheet-bg);
    z-index: 10;
    display: flex;
    flex-direction: column;
  }
  .profile-head {
    height: 60px;
    background: var(--profile-sheet-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    font-size: 32px;
    font-weight: 600;
    padding-top: 8px;
  }
  .profile-back {
    position: absolute;
    left: 14px;
    top: 10px;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    border: 0;
    background: var(--profile-back-bg);
    color: var(--profile-back-color);
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
  }
  .profile-main {
    padding: 10px 18px 20px;
    overflow-y: auto;
  }
  .profile-avatar-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    margin: 6px 0 18px;
  }
  .profile-avatar {
    width: 120px;
    height: 120px;
    min-width: 120px;
    min-height: 120px;
    max-width: 120px;
    max-height: 120px;
    aspect-ratio: 1 / 1;
    flex: none;
    border-radius: 9999px;
    overflow: hidden;
    border: 3px solid #ffffff;
    box-shadow: 0 6px 18px rgba(17, 24, 39, 0.16);
    font-size: 42px;
    background: #ffffff;
  }
  .profile-avatar img {
    width: 100%;
    height: 100%;
    border-radius: inherit;
    object-fit: cover;
    object-position: center;
    display: block;
  }
  .profile-edit-btn {
    border: 0;
    background: transparent;
    color: var(--teal);
    font-size: 22px;
    font-weight: 600;
    cursor: pointer;
  }
  .profile-card {
    background: var(--profile-card-bg);
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 1px 2px rgba(11, 20, 26, .08);
  }
  .profile-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--profile-row-border);
    cursor: default;
  }
  .profile-row:last-child { border-bottom: 0; }
  .profile-row.clickable { cursor: pointer; }
  .profile-label {
    font-size: 15px;
    color: var(--profile-label-color);
    font-weight: 500;
  }
  .profile-value-wrap {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
  }
  .profile-value-wrap-column {
    display: flex;
    align-items: flex-end;
    flex-direction: column;
    gap: 4px;
  }
  .profile-inline-line {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
  }
  .profile-inline-hint {
    max-width: 320px;
    text-align: right;
    font-size: 12px;
    color: var(--muted);
    line-height: 1.35;
  }
  .profile-inline-error {
    max-width: 320px;
    text-align: right;
    font-size: 12px;
    color: #c0392b;
    line-height: 1.35;
  }
  .profile-value {
    font-size: 14px;
    color: var(--profile-value-color);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 175px;
  }
  .profile-email-value {
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
    max-width: 320px;
    word-break: break-word;
    text-align: right;
    line-height: 1.35;
  }
  .profile-value.accent { color: var(--teal); }
  .profile-chevron {
    color: #9ca3af;
    font-size: 18px;
    transform: translateY(-1px);
  }
  .profile-edit-controls {
    display: inline-flex;
    align-items: center;
    gap: 6px;
  }
  .profile-edit-btn-icon,
  .profile-mini-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 0;
    background: transparent;
    color: #6b7280;
    cursor: pointer;
    display: grid;
    place-items: center;
    font-size: 16px;
    line-height: 1;
  }
  .profile-edit-btn-icon:hover,
  .profile-mini-btn:hover { background: #f3f4f6; }
  .profile-mini-btn.save {
    color: var(--teal);
    font-weight: 700;
  }
  .profile-mini-btn.cancel { font-weight: 700; }
  .profile-inline-edit {
    width: 170px;
    padding: 6px 9px;
    border: 1px solid var(--profile-inline-border);
    border-radius: 8px;
    outline: none;
    font-size: 14px;
    color: var(--ink);
    background: var(--profile-inline-bg);
    text-align: right;
  }
  .profile-inline-edit:focus { border-color: var(--teal); }
  .profile-err {
    margin-top: 12px;
    font-size: 13px;
    color: #c0392b;
    min-height: 16px;
    text-align: center;
  }
  .profile-password-box {
    margin-top: 14px;
    background: var(--profile-card-bg);
    border-radius: 14px;
    border: 1px solid var(--profile-row-border);
    padding: 12px;
  }
  .profile-password-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 8px;
  }
  .profile-password-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--profile-label-color);
  }
  .profile-password-form {
    display: grid;
    gap: 8px;
  }
  .profile-password-form .btn {
    margin-top: 6px;
    width: auto;
    justify-self: end;
    padding: 10px 14px;
    border-radius: 10px;
  }
  .profile-password-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 4px;
  }
  .profile-password-actions .btn {
    margin-top: 0;
    width: auto;
    padding: 8px 12px;
    border-radius: 10px;
  }
  .contacts-list {
    margin-top: 12px;
    background: var(--profile-card-bg);
    border-radius: 16px;
    border: 1px solid var(--profile-row-border);
    overflow: hidden;
  }
  .contacts-empty {
    padding: 16px;
    text-align: center;
    color: var(--muted);
    font-size: 14px;
  }
  .contact-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--profile-row-border);
    cursor: pointer;
    transition: background-color .15s ease;
  }
  .contact-row:hover {
    background: color-mix(in srgb, var(--profile-card-bg) 85%, var(--ink) 15%);
  }
  .contact-row:last-child {
    border-bottom: 0;
  }
  .contact-row .avatar {
    width: 38px;
    height: 38px;
    min-width: 38px;
    min-height: 38px;
    font-size: 14px;
  }
  .contact-main {
    flex: 1;
    min-width: 0;
  }
  .contact-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .contact-username {
    font-size: 12px;
    color: var(--muted);
    margin-top: 1px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .contact-actions {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    flex: 0 0 auto;
  }
  .contact-write-hint {
    font-size: 12px;
    font-weight: 600;
    color: var(--teal);
    line-height: 1;
    user-select: none;
    letter-spacing: .01em;
  }
  .contact-actions-divider {
    width: 1px;
    height: 18px;
    background: var(--profile-row-border);
    flex: 0 0 auto;
  }
  .contact-remove-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid var(--profile-row-border);
    background: var(--profile-card-bg);
    color: #7b8694;
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    flex: 0 0 auto;
    transition: background-color .15s ease, color .15s ease, border-color .15s ease;
  }
  .contact-remove-btn:hover {
    background: color-mix(in srgb, #ef4444 12%, var(--profile-card-bg));
    border-color: color-mix(in srgb, #ef4444 30%, var(--profile-row-border));
    color: #c0392b;
  }
  body[data-theme="dark"] .contact-remove-btn:hover {
    background: color-mix(in srgb, #ef4444 20%, #1f2c34);
    border-color: color-mix(in srgb, #ef4444 38%, #33434d);
    color: #f87171;
  }
  .modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    display: grid;
    place-items: center;
    z-index: 30;
    padding: 16px;
  }
  .modal-card {
    width: 420px;
    max-width: 100%;
    background: var(--panel);
    border-radius: 16px;
    box-shadow: var(--shadow);
    padding: 22px 22px 18px;
  }
  .modal-title {
    margin: 0;
    font-size: 20px;
    font-weight: 600;
    color: var(--ink);
  }
  .modal-subtitle {
    margin: 6px 0 10px;
    font-size: 14px;
    font-weight: 600;
    color: var(--muted);
  }
  .modal-contact-search-wrap {
    position: relative;
    margin-bottom: 10px;
  }
  .modal-contact-search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--muted);
    font-size: 15px;
    pointer-events: none;
  }
  .modal-contact-search {
    width: 100%;
    margin: 0;
    padding: 10px 12px 10px 36px;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: var(--search-input-bg);
    color: var(--ink);
    font-size: 14px;
    outline: none;
    transition: border-color .15s ease, background-color .15s ease;
  }
  .modal-contact-search:focus {
    border-color: var(--teal);
    background: var(--panel);
  }
  .modal-contacts-list {
    margin-bottom: 12px;
    max-height: min(42vh, 360px);
    min-height: 160px;
    overflow-y: auto;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--profile-card-bg);
  }
  .modal-contact-row {
    width: 100%;
    border: 0;
    border-bottom: 1px solid var(--line);
    background: transparent;
    color: inherit;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    text-align: left;
    cursor: pointer;
    transition: background-color .15s ease;
  }
  .modal-contact-row:last-child {
    border-bottom: 0;
  }
  .modal-contact-row:hover {
    background: var(--chat-row-hover);
  }
  .modal-contact-row.selected {
    background: color-mix(in srgb, var(--teal) 10%, var(--profile-card-bg));
  }
  .modal-contact-avatar {
    width: 40px;
    height: 40px;
    min-width: 40px;
    min-height: 40px;
    font-size: 14px;
  }
  .modal-contact-text {
    min-width: 0;
  }
  .modal-contact-name {
    font-size: 15px;
    font-weight: 600;
    color: var(--ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .modal-contact-username {
    margin-top: 2px;
    font-size: 13px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .modal-contacts-empty {
    padding: 12px;
    font-size: 13px;
    color: var(--muted);
    text-align: center;
  }
  .modal-contact-check {
    margin-left: auto;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 1px solid var(--line);
    display: grid;
    place-items: center;
    color: #fff;
    font-size: 13px;
    font-weight: 700;
    background: transparent;
    flex: 0 0 22px;
  }
  .modal-contact-row.selected .modal-contact-check {
    border-color: var(--teal);
    background: var(--teal);
  }
  .modal-selected-info {
    margin-top: 8px;
    margin-bottom: 4px;
    font-size: 12px;
    color: var(--muted);
  }
  .modal-group-contacts-list {
    min-height: 180px;
  }
  .modal-manual-label {
    display: block;
    margin-top: 8px;
    margin-bottom: 6px;
    color: var(--muted);
    font-size: 13px;
  }
  .modal-actions {
    margin-top: 14px;
    display: flex;
    justify-content: flex-end;
    gap: 10px;
  }
  .modal-actions .btn {
    width: auto;
    min-width: 116px;
    margin-top: 0;
    padding: 10px 14px;
    border-radius: 12px;
  }
  .modal-actions .btn.btn-secondary {
    border-color: var(--line);
    color: var(--ink);
  }
  .modal-err {
    margin-top: 10px;
    min-height: 16px;
    font-size: 13px;
    color: #c0392b;
  }

  @media (max-width: 900px) {
    #app {
      display: block;
      height: 100dvh;
    }
    #app.profile-mode {
      grid-template-columns: unset;
    }
    #app.profile-mode .conversation {
      opacity: 1;
      pointer-events: auto;
      filter: none;
    }
    .sidebar,
    .conversation {
      height: 100dvh;
    }
    .sidebar {
      border-right: 0;
    }
    #app .conversation {
      display: none;
    }
    #app.mobile-chat-open .sidebar {
      display: none;
    }
    #app.mobile-chat-open .conversation {
      display: flex;
    }
    .side-head {
      padding: 10px 12px;
      gap: 8px;
    }
    .side-actions {
      gap: 4px;
    }
    .chat-actions {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    #archiveBtn {
      grid-column: 1 / -1;
    }
    .search {
      padding: 8px 10px;
    }
    .chat-row {
      padding: 10px 12px;
    }
    .conv-head {
      height: 56px;
      padding: 0 10px;
      gap: 10px;
    }
    .conv-back {
      display: grid;
    }
    .thread {
      padding: 12px 12px 8px;
    }
    .msg {
      max-width: 84%;
    }
    .composer {
      padding: 8px 10px;
      gap: 8px;
    }
    .composer .send {
      width: 42px;
      height: 42px;
    }
    .composer input[type=text] {
      padding: 10px 12px;
    }
    .empty {
      padding: 18px;
    }
    .profile-sheet {
      position: fixed;
      z-index: 40;
    }
  }

  @media (max-width: 600px) {
    #auth {
      background: var(--panel);
      place-items: stretch;
    }
    .auth-card {
      width: 100%;
      max-width: none;
      min-height: 100dvh;
      border-radius: 0;
      box-shadow: none;
      padding: 24px 18px 24px;
    }
    .modal-overlay {
      padding: 12px;
    }
    .modal-card {
      border-radius: 14px;
      padding: 18px 16px 14px;
    }
    .chat-actions {
      grid-template-columns: 1fr;
    }
    #archiveBtn {
      grid-column: auto;
    }
  }
