/* ---------------------------------------------------------------------
   Design tokens
--------------------------------------------------------------------- */
:root {
  --text-xs: clamp(0.75rem, 0.72rem + 0.15vw, 0.8125rem);
  --text-sm: clamp(0.875rem, 0.84rem + 0.2vw, 0.9375rem);
  --text-base: clamp(1rem, 0.96rem + 0.2vw, 1.0625rem);
  --text-lg: clamp(1.125rem, 1.05rem + 0.4vw, 1.3125rem);
  --text-xl: clamp(1.375rem, 1.2rem + 0.8vw, 1.75rem);

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --space-10: 2.5rem;

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-full: 999px;

  --color-bg: #f5f7fa;
  --color-surface: #ffffff;
  --color-surface-2: #f0f2f5;
  --color-border: #e2e6eb;
  --color-text: #1a1f29;
  --color-text-muted: #667085;
  --color-text-faint: #98a2b3;

  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-primary-highlight: #dbeafe;

  --color-error: #dc2626;
  --color-error-highlight: #fee2e2;

  --bubble-own: #2563eb;
  --bubble-own-text: #ffffff;
  --bubble-other: #ffffff;
  --bubble-other-text: #1a1f29;

  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 16px rgba(16, 24, 40, 0.10);
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #0f1115;
    --color-surface: #171a21;
    --color-surface-2: #1f2330;
    --color-border: #2a2f3a;
    --color-text: #eef1f5;
    --color-text-muted: #9aa3b2;
    --color-text-faint: #6b7280;

    --color-primary: #3b82f6;
    --color-primary-hover: #60a5fa;
    --color-primary-highlight: #1e3a5f;

    --bubble-own: #2563eb;
    --bubble-own-text: #ffffff;
    --bubble-other: #232733;
    --bubble-other-text: #eef1f5;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.45);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

.app {
  max-width: 560px;
  margin: 0 auto;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  box-shadow: var(--shadow-md);
}

/* ---------------------------------------------------------------------
   Identity picker
--------------------------------------------------------------------- */
.view-identity {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: var(--space-6);
  padding-top: min(18vh, var(--space-10));
  height: 100%;
  overflow-y: auto;
}
.card {
  width: 100%;
  max-width: 380px;
}
.brand-mark {
  color: var(--color-primary);
  margin-bottom: var(--space-5);
}
.title {
  font-size: var(--text-xl);
  font-weight: 700;
  margin: 0 0 var(--space-2);
}
.subtitle {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  margin: 0 0 var(--space-6);
}
.identity-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin-bottom: var(--space-6);
}
.identity-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  cursor: pointer;
  font-size: var(--text-base);
  color: var(--color-text);
  text-align: left;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.identity-item:hover { border-color: var(--color-primary); background: var(--color-primary-highlight); }

.add-user-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.add-user-form input {
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: var(--text-base);
  background: var(--color-surface);
  color: var(--color-text);
}

.password-form-label { margin-bottom: var(--space-1); }
.password-form-actions { display: flex; gap: var(--space-2); }
.password-form-actions .btn-primary { flex: 1; }
.form-error {
  font-size: var(--text-sm);
  color: var(--color-error);
  margin: 0;
}
.form-success {
  font-size: var(--text-sm);
  color: #059669;
  margin: 0;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 900;
  padding: var(--space-6);
}
.modal-card {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: var(--space-6);
  width: 100%;
  max-width: 360px;
  box-shadow: var(--shadow-md);
}
.modal-card .section-title { margin: 0 0 var(--space-4); }

.topbar-actions { display: flex; align-items: center; gap: var(--space-1); }

.avatar {
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 600;
  font-size: var(--text-sm);
}

.avatar-wrap {
  position: relative;
  display: inline-flex;
  flex-shrink: 0;
}
.presence-dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--color-text-faint);
  border: 2px solid var(--color-surface);
  box-sizing: border-box;
}
.presence-dot.online { background: #22c55e; }

/* ---------------------------------------------------------------------
   Buttons
--------------------------------------------------------------------- */
.btn {
  font-family: inherit;
  font-size: var(--text-sm);
  font-weight: 600;
  border-radius: var(--radius-md);
  border: none;
  cursor: pointer;
  padding: var(--space-3) var(--space-5);
  transition: background 0.15s ease, opacity 0.15s ease;
}
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary { background: var(--color-surface-2); color: var(--color-text); }
.btn-secondary:hover { background: var(--color-border); }
.btn-ghost { background: transparent; color: var(--color-text-muted); padding: var(--space-2) var(--space-3); }
.btn-ghost:hover { color: var(--color-text); }
.btn-icon {
  background: transparent; border: none; color: var(--color-text);
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: var(--radius-full); cursor: pointer;
  /* Overrides .btn's horizontal padding (1.25rem each side) explicitly — combined
     with box-sizing: border-box and this fixed 36px width, that padding used to
     leave zero/negative space for the icon, silently shrinking it to invisible
     while the button itself stayed clickable. Affected #emoji-btn and #chat-back-btn. */
  padding: 0;
}
.btn-icon:hover { background: var(--color-surface-2); }
.btn-icon svg { flex-shrink: 0; }

/* ---------------------------------------------------------------------
   Contacts view
--------------------------------------------------------------------- */
.view-contacts { display: flex; flex-direction: column; height: 100%; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--color-border);
}
.topbar-caption { font-size: var(--text-xs); color: var(--color-text-muted); }
.topbar-title { font-size: var(--text-base); font-weight: 700; }
.section-title {
  font-size: var(--text-sm);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
  margin: var(--space-5) var(--space-5) var(--space-2);
}
.contacts-list { display: flex; flex-direction: column; overflow-y: auto; }
.contact-item {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border: none;
  border-bottom: 1px solid var(--color-border);
  background: transparent;
  cursor: pointer;
  text-align: left;
  width: 100%;
  font-family: inherit;
  color: var(--color-text);
}
.contact-item:hover { background: var(--color-surface-2); }
.contact-info { flex: 1; min-width: 0; }
.contact-name { font-size: var(--text-base); font-weight: 600; }
.contact-preview {
  font-size: var(--text-sm); color: var(--color-text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.contact-time { font-size: var(--text-xs); color: var(--color-text-faint); white-space: nowrap; }
.empty-state { color: var(--color-text-muted); font-size: var(--text-sm); padding: 0 var(--space-5); }

/* ---------------------------------------------------------------------
   Chat view
--------------------------------------------------------------------- */
.view-chat { display: flex; flex-direction: column; height: 100%; }
.chat-header {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
  flex-shrink: 0;
}
.chat-header-info { display: flex; align-items: center; gap: var(--space-3); flex: 1; min-width: 0; }
.chat-with-name { font-weight: 700; font-size: var(--text-base); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.connection-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: #22c55e;
  flex-shrink: 0;
}
.connection-dot.offline { background: var(--color-text-faint); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  background: var(--color-bg);
}
.date-sep {
  align-self: center;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  background: var(--color-surface-2);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-full);
  margin: var(--space-3) 0;
}
.msg-row { display: flex; }
.msg-row.own { justify-content: flex-end; }
.msg-row.other { justify-content: flex-start; }
.bubble {
  max-width: 78%;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-lg);
  font-size: var(--text-base);
  line-height: 1.4;
  box-shadow: var(--shadow-sm);
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: pre-wrap;
}
.msg-row.own .bubble { background: var(--bubble-own); color: var(--bubble-own-text); border-bottom-right-radius: var(--radius-sm); }
.msg-row.other .bubble { background: var(--bubble-other); color: var(--bubble-other-text); border-bottom-left-radius: var(--radius-sm); }
.bubble-time {
  display: block;
  font-size: var(--text-xs);
  margin-top: var(--space-1);
  opacity: 0.7;
  text-align: right;
}
.bubble-img {
  display: block;
  max-width: 220px;
  max-height: 220px;
  border-radius: var(--radius-sm);
  cursor: zoom-in;
  margin-bottom: var(--space-1);
}
.msg-row.system {
  justify-content: center;
}
.system-note {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  background: var(--color-surface-2);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-full);
}

.banner {
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
  text-align: center;
  background: var(--color-error-highlight);
  color: var(--color-error);
}

/* ---------------------------------------------------------------------
   Composer
--------------------------------------------------------------------- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--color-border);
  background: var(--color-surface);
  flex-wrap: wrap;
  flex-shrink: 0;
}
.attach-btn {
  display: flex; align-items: center; justify-content: center;
  width: 40px; height: 40px;
  border-radius: var(--radius-full);
  color: var(--color-text-muted);
  cursor: pointer;
  flex-shrink: 0;
}
.attach-btn:hover { background: var(--color-surface-2); color: var(--color-text); }

.emoji-wrap { position: relative; flex-shrink: 0; }
.emoji-toggle-btn { color: var(--color-text-muted); }
.emoji-panel {
  position: absolute;
  bottom: calc(100% + var(--space-2));
  left: 0;
  width: 270px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: var(--space-2);
  z-index: 50;
}
.emoji-search {
  width: 100%;
  box-sizing: border-box;
  margin-bottom: var(--space-2);
  padding: var(--space-2) var(--space-3);
  font-family: inherit;
  font-size: var(--text-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface-2);
  color: var(--color-text);
}
.emoji-search:focus { outline: 2px solid var(--color-primary); outline-offset: 1px; }
.emoji-grid {
  max-height: 180px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: var(--space-1);
}
.emoji-item {
  border: none;
  background: transparent;
  font-size: 1.25rem;
  line-height: 1;
  padding: var(--space-2);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.emoji-item:hover { background: var(--color-surface-2); }
.emoji-empty {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  text-align: center;
  margin: var(--space-2) 0 0;
}
#message-input {
  flex: 1;
  min-width: 120px;
  min-height: 46px;
  resize: none;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-3) var(--space-4);
  font-family: inherit;
  font-size: clamp(1.0625rem, 1rem + 0.3vw, 1.1875rem);
  color: var(--color-text);
  background: var(--color-surface-2);
  max-height: 120px;
  line-height: 1.4;
  box-sizing: border-box;
}
#message-input:focus { outline: 2px solid var(--color-primary); outline-offset: 1px; }
#message-input::placeholder {
  font-style: italic;
  color: var(--color-text-faint);
}
.char-counter {
  font-size: var(--text-xs);
  color: var(--color-text-faint);
  flex-shrink: 0;
  align-self: center;
}
.btn-send { flex-shrink: 0; }

.attach-preview {
  order: -1;
  width: 100%;
  margin-bottom: var(--space-1);
}
.attach-thumb {
  position: relative;
  display: inline-block;
}
.attach-thumb img {
  max-height: 96px;
  max-width: 160px;
  border-radius: var(--radius-sm);
  display: block;
  cursor: zoom-in;
}
.attach-preview-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-top: var(--space-1);
}
.attach-remove {
  position: absolute;
  top: -8px; right: -8px;
  width: 22px; height: 22px;
  border-radius: 50%;
  border: 2px solid var(--color-surface);
  background: var(--color-error);
  color: #fff;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}

.btn-end-chat {
  width: calc(100% - 2 * var(--space-4));
  margin: 0 var(--space-4) var(--space-4);
  background: var(--color-surface-2);
  color: var(--color-error);
  border: 1px solid var(--color-border);
  flex-shrink: 0;
}
.btn-end-chat:hover { background: var(--color-error-highlight); }

/* ---------------------------------------------------------------------
   Lightbox
--------------------------------------------------------------------- */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: var(--space-6);
}
.lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
}
.lightbox-close {
  position: absolute;
  top: var(--space-5);
  right: var(--space-5);
  width: 44px; height: 44px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}
.lightbox-close:hover { background: rgba(255, 255, 255, 0.3); }

@media (max-width: 480px) {
  .app { max-width: 100%; }
}

@media (max-width: 380px) {
  .char-counter { display: none; }
  .btn-send { padding: var(--space-3) var(--space-3); }
  .attach-btn { width: 36px; height: 36px; }
}
