/* GramView — Telegram Light Theme */

:root {
  --tg-blue: #3390ec;
  --tg-blue-dark: #2b7fd4;
  --tg-bg: #ffffff;
  --tg-bg-secondary: #f0f2f5;
  --tg-bg-chat: #808080;
  --tg-bg-pattern: #808080;
  --tg-sidebar: #ffffff;
  --tg-border: #e0e0e0;
  --tg-text: #000000;
  --tg-text-secondary: #707579;
  --tg-bubble-in: #ffffff;
  --tg-bubble-out: #effdde;
  --tg-hover: #f4f4f5;
  --tg-active: #e8f4fc;
  --tg-danger: #e53935;
  --sidebar-width: clamp(280px, 36vw, 380px);
  --header-height: clamp(48px, 12vw, 56px);
  --font-family: 'Cairo', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --tg-green: #4fae4e;
  --tg-reply-in: #3390ec;
  --tg-reply-out: #4fae4e;
  --tg-surface-muted: rgba(0, 0, 0, 0.06);
  --tg-surface-overlay: rgba(0, 0, 0, 0.04);
  --tg-surface-hover: rgba(0, 0, 0, 0.07);
  --tg-date-divider-bg: rgba(0, 0, 0, 0.15);
  --tg-date-divider-text: #ffffff;
  --tg-bubble-shadow: rgba(0, 0, 0, 0.06);
  --tg-login-bg: linear-gradient(135deg, #e8f4fc 0%, #f0f2f5 50%, #dfe6eb 100%);
  --tg-card-shadow: rgba(0, 0, 0, 0.08);
  --tg-loading-overlay: rgba(255, 255, 255, 0.85);
  --tg-scrollbar-track: rgba(0, 0, 0, 0.06);
  --tg-scrollbar-thumb: rgba(0, 0, 0, 0.32);
  --tg-scrollbar-thumb-hover: rgba(0, 0, 0, 0.48);
  --tg-chat-pattern-1: rgba(255, 255, 255, 0.1);
  --tg-chat-pattern-2: rgba(0, 0, 0, 0.06);
  --tg-voice-wave: rgba(0, 0, 0, 0.18);
  --tg-input-focus-ring: rgba(51, 144, 236, 0.15);
  --tg-item-border: rgba(0, 0, 0, 0.04);
  --tg-on-primary: #ffffff;

  /* Responsive scale */
  --msg-font: clamp(14px, 3.6vw, 16px);
  --msg-meta-font: clamp(10px, 2.6vw, 11px);
  --msg-sender-font: clamp(12px, 3.2vw, 13px);
  --bubble-radius: clamp(12px, 3.5vw, 16px);
  --bubble-pad-x: clamp(10px, 3vw, 12px);
  --bubble-pad-y: clamp(7px, 2.2vw, 8px);
  --bubble-max: min(78%, 520px);
  --media-max-w: min(100%, 300px);
  --media-max-h: min(55vh, 400px);
  --avatar-lg: clamp(44px, 11vw, 54px);
  --avatar-md: clamp(32px, 8.5vw, 40px);
  --avatar-sm: clamp(28px, 7.5vw, 34px);
  --voice-btn: clamp(36px, 10vw, 42px);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

[data-theme="dark"] {
  color-scheme: dark;
  --tg-blue: #6ab3f3;
  --tg-blue-dark: #5aa0db;
  --tg-bg: #17212b;
  --tg-bg-secondary: #0e1621;
  --tg-bg-chat: #0e1621;
  --tg-bg-pattern: #17212b;
  --tg-sidebar: #17212b;
  --tg-border: #242f3d;
  --tg-text: #f5f5f5;
  --tg-text-secondary: #8b959e;
  --tg-bubble-in: #182533;
  --tg-bubble-out: #2b5278;
  --tg-hover: #232e3c;
  --tg-active: #2b5278;
  --tg-danger: #ff6b6b;
  --tg-green: #8eee98;
  --tg-reply-in: #6ab3f3;
  --tg-reply-out: #8eee98;
  --tg-surface-muted: rgba(255, 255, 255, 0.06);
  --tg-surface-overlay: rgba(255, 255, 255, 0.05);
  --tg-surface-hover: rgba(255, 255, 255, 0.08);
  --tg-date-divider-bg: rgba(255, 255, 255, 0.12);
  --tg-date-divider-text: #ffffff;
  --tg-bubble-shadow: rgba(0, 0, 0, 0.2);
  --tg-login-bg: linear-gradient(135deg, #0e1621 0%, #17212b 50%, #1c2733 100%);
  --tg-card-shadow: rgba(0, 0, 0, 0.35);
  --tg-loading-overlay: rgba(14, 22, 33, 0.88);
  --tg-scrollbar-track: rgba(255, 255, 255, 0.06);
  --tg-scrollbar-thumb: rgba(255, 255, 255, 0.22);
  --tg-scrollbar-thumb-hover: rgba(255, 255, 255, 0.34);
  --tg-chat-pattern-1: rgba(255, 255, 255, 0.03);
  --tg-chat-pattern-2: rgba(255, 255, 255, 0.02);
  --tg-voice-wave: rgba(255, 255, 255, 0.22);
  --tg-input-focus-ring: rgba(106, 179, 243, 0.2);
  --tg-item-border: rgba(255, 255, 255, 0.04);
  --tg-on-primary: #ffffff;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  min-height: 100%;
  min-height: 100dvh;
  font-family: var(--font-family);
  font-weight: 600;
  background: var(--tg-bg-secondary);
  color: var(--tg-text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: lining-nums;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

/* تحديد النص مسموح فقط داخل نص الرسائل */
.messages-container .message-text,
.messages-container .message-text * {
  -webkit-user-select: text;
  user-select: text;
}

html.browser-fullscreen,
html.standalone-mode {
  overscroll-behavior: none;
}

html.browser-fullscreen body,
html.standalone-mode body {
  width: 100%;
  height: 100%;
  min-height: 100dvh;
}

button,
input,
textarea,
select {
  font-family: var(--font-family);
  font-weight: 600;
}

.hidden {
  display: none !important;
}

/* ─── Login Screen ─── */

.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--tg-login-bg);
  padding: 20px;
  padding-top: calc(20px + var(--safe-top));
  padding-bottom: calc(20px + var(--safe-bottom));
  position: relative;
}

.login-card {
  background: var(--tg-bg);
  border: 1px solid var(--tg-border);
  border-radius: 20px;
  padding: 42px 34px 34px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 36px var(--tg-card-shadow);
  text-align: center;
}

.login-logo {
  margin-bottom: 18px;
}

.login-logo svg {
  display: block;
  margin: 0 auto;
}

.login-card h1 {
  font-size: clamp(24px, 6vw, 30px);
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--tg-blue);
}

.login-subtitle {
  color: var(--tg-text-secondary);
  font-size: clamp(13px, 3.5vw, 15px);
  line-height: 1.55;
  margin-bottom: 28px;
}

.input-group {
  margin-bottom: 12px;
}

.input-group input {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid var(--tg-border);
  border-radius: 12px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  direction: ltr;
  text-align: center;
  letter-spacing: 2px;
  background: var(--tg-bg-secondary);
  color: var(--tg-text);
}

.input-group input:focus {
  border-color: var(--tg-blue);
  background: var(--tg-bg);
  box-shadow: 0 0 0 3px var(--tg-input-focus-ring);
}

.login-error {
  color: var(--tg-danger);
  font-size: 13px;
  min-height: 20px;
  margin-bottom: 8px;
}

.login-btn {
  width: 100%;
  padding: 14px;
  background: var(--tg-blue);
  color: var(--tg-on-primary);
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  touch-action: manipulation;
}

.login-btn:hover {
  background: var(--tg-blue-dark);
}

.login-btn:active {
  transform: scale(0.99);
}

.login-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.login-footer {
  margin-top: 22px;
  font-size: 12px;
  color: var(--tg-text-secondary);
}

/* ─── App Layout ─── */

.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  position: relative;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--tg-sidebar);
  border-left: 1px solid var(--tg-border);
  display: flex;
  flex-direction: column;
  z-index: 2;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: var(--header-height);
  border-bottom: 1px solid var(--tg-border);
}

.sidebar-header h1 {
  font-size: 20px;
  font-weight: 600;
  color: var(--tg-blue);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.theme-toggle-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 6px;
  flex-shrink: 0;
}

.theme-toggle-btn .theme-emoji {
  width: 28px;
  height: 28px;
  object-fit: contain;
  display: block;
  pointer-events: none;
  user-select: none;
}

.theme-toggle-btn .theme-icon-light.hidden,
.theme-toggle-btn .theme-icon-dark.hidden {
  display: none;
}

.theme-toggle-floating {
  position: absolute;
  top: max(16px, var(--safe-top));
  left: max(16px, var(--safe-left));
  z-index: 2;
  background: var(--tg-bg);
  box-shadow: 0 2px 12px var(--tg-card-shadow);
  min-width: 48px;
  min-height: 48px;
}

.theme-toggle-floating .theme-emoji {
  width: 32px;
  height: 32px;
}

.login-screen .theme-toggle-floating,
.identity-screen .theme-toggle-floating {
  background: var(--tg-bg);
}

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  color: var(--tg-text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.icon-btn:hover {
  background: var(--tg-hover);
  color: var(--tg-danger);
}

.chat-list {
  flex: 1;
  overflow-y: scroll;
  overflow-x: hidden;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: var(--tg-scrollbar-thumb) var(--tg-scrollbar-track);
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 16px;
  border: none;
  background: none;
  cursor: pointer;
  text-align: right;
  transition: background 0.15s;
  border-bottom: 1px solid var(--tg-item-border);
}

.chat-item:hover {
  background: var(--tg-hover);
}

.chat-item.active {
  background: var(--tg-active);
}

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

.chat-item-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 4px;
}

.chat-item-name {
  font-family: var(--font-family);
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item-time {
  font-size: 12px;
  color: var(--tg-text-secondary);
  flex-shrink: 0;
  margin-right: 8px;
  font-variant-numeric: tabular-nums;
  direction: ltr;
  unicode-bidi: isolate;
}

.chat-item-preview {
  font-family: var(--font-family);
  font-weight: 600;
  font-size: 14px;
  color: var(--tg-text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.avatar {
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--tg-on-primary);
  font-weight: 600;
  font-size: clamp(12px, 3.2vw, 18px);
  flex-shrink: 0;
  user-select: none;
}

.avatar.size-lg {
  width: var(--avatar-lg);
  height: var(--avatar-lg);
  font-size: clamp(14px, 3.8vw, 18px);
}

.avatar.size-md {
  width: var(--avatar-md);
  height: var(--avatar-md);
  font-size: clamp(12px, 3.2vw, 15px);
}

.avatar.size-sm {
  width: var(--avatar-sm);
  height: var(--avatar-sm);
  font-size: clamp(11px, 3vw, 13px);
}

.avatar-spacer {
  width: var(--avatar-sm);
  flex-shrink: 0;
}

/* ─── Mobile ─── */

@media (max-width: 768px) {
  :root {
    --bubble-max: min(88%, 100%);
    --media-max-w: min(100%, calc(100vw - 88px));
    --media-max-h: min(50vh, 360px);
  }

  .login-screen {
    padding: 16px;
    padding-top: calc(16px + var(--safe-top));
    padding-bottom: calc(16px + var(--safe-bottom));
  }

  .login-card {
    padding: 28px 22px;
    border-radius: 14px;
  }

  .login-card h1 {
    font-size: clamp(22px, 6vw, 28px);
  }

  .sidebar {
    width: 100%;
    min-width: 100%;
    position: absolute;
    inset: 0;
    transition: transform 0.25s ease;
    z-index: 3;
  }

  .sidebar.hidden-mobile {
    transform: translateX(100%);
    pointer-events: none;
  }

  .chat-panel {
    position: absolute;
    inset: 0;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    z-index: 4;
  }

  .chat-panel.visible-mobile {
    transform: translateX(0);
  }

  .chat-header-actions {
    display: flex;
    align-items: center;
    flex-shrink: 0;
  }

  .theme-toggle-btn .theme-emoji {
    width: 32px;
    height: 32px;
  }

  .sidebar-header .header-actions .theme-toggle-btn .theme-emoji {
    width: 30px;
    height: 30px;
  }

  .chat-header {
    padding: 0 clamp(10px, 3vw, 16px);
    gap: clamp(8px, 2.5vw, 12px);
    position: relative;
    z-index: 5;
  }

  .chat-header-info h2 {
    font-size: clamp(14px, 4vw, 16px);
  }

  .chat-header-info span {
    font-size: clamp(11px, 3vw, 13px);
  }

  .chat-item {
    padding: clamp(8px, 2.5vw, 10px) clamp(12px, 3.5vw, 16px);
    gap: clamp(10px, 3vw, 12px);
    touch-action: manipulation;
  }

  .chat-item-name {
    font-size: clamp(14px, 3.8vw, 15px);
  }

  .chat-item-preview {
    font-size: clamp(13px, 3.5vw, 14px);
  }

  .message-row.incoming .avatar,
  .message-row.incoming .avatar-spacer {
    width: var(--avatar-sm);
    height: var(--avatar-sm);
    font-size: clamp(11px, 3vw, 13px);
  }

  .date-divider span {
    font-size: clamp(11px, 3vw, 12px);
    padding: 4px 12px;
  }

  .window-range {
    font-size: clamp(11px, 3vw, 12px);
    max-width: 95%;
  }

  .icon-btn {
    min-width: 40px;
    min-height: 40px;
    touch-action: manipulation;
  }
}

@media (max-width: 380px) {
  :root {
    --msg-font: 14px;
    --bubble-max: min(92%, 100%);
    --media-max-w: calc(100vw - 72px);
  }

  .messages-container {
    padding-left: 8px;
    padding-right: 8px;
  }

  .voice-wave span {
    width: 2px;
  }
}

@media (min-width: 769px) and (max-width: 1024px) {
  :root {
    --bubble-max: min(75%, 460px);
    --media-max-w: min(100%, 280px);
  }
}

.chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--tg-bg-chat);
  background-image:
    radial-gradient(circle at 20% 30%, var(--tg-chat-pattern-1) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, var(--tg-chat-pattern-2) 0%, transparent 50%);
  min-width: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  height: var(--header-height);
  background: var(--tg-bg);
  border-bottom: 1px solid var(--tg-border);
  flex-shrink: 0;
}

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

.chat-header-actions {
  margin-right: auto;
  flex-shrink: 0;
}

@media (max-width: 768px) {
  .chat-header-actions {
    display: flex;
    align-items: center;
  }
}

@media (min-width: 769px) {
  .chat-header-actions {
    display: none;
  }
}

.back-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  color: var(--tg-text-secondary);
  border-radius: 50%;
  flex-shrink: 0;
}

@media (min-width: 769px) {
  .back-btn {
    display: none;
  }
}

@media (max-width: 768px) {
  .back-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    touch-action: manipulation;
  }
}

.back-btn:hover {
  background: var(--tg-hover);
}

.chat-header-info h2 {
  font-family: var(--font-family);
  font-size: 16px;
  font-weight: 600;
}

.chat-header-info span {
  font-family: var(--font-family);
  font-weight: 600;
  font-size: 13px;
  color: var(--tg-text-secondary);
}

.chat-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  background: var(--tg-bg);
  border-bottom: 1px solid var(--tg-border);
  flex-shrink: 0;
  z-index: 1;
}

.chat-toolbar.hidden {
  display: none;
}

.chat-toolbar-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--tg-border);
  border-radius: 20px;
  background: var(--tg-bg-secondary);
  color: var(--tg-blue);
  font-family: var(--font-family);
  font-size: clamp(12px, 3.2vw, 13px);
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  touch-action: manipulation;
  white-space: nowrap;
}

.chat-toolbar-btn:hover:not(:disabled) {
  background: var(--tg-active);
}

.chat-toolbar-btn:disabled {
  opacity: 0.45;
  cursor: default;
  color: var(--tg-text-secondary);
}

.messages-container {
  flex: 1;
  overflow-y: scroll;
  overflow-x: hidden;
  padding: clamp(8px, 2.5vw, 12px) clamp(10px, 3vw, 16px);
  padding-bottom: calc(clamp(8px, 2.5vw, 12px) + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 2px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: var(--tg-scrollbar-thumb) var(--tg-scrollbar-track);
}

.window-range {
  text-align: center;
  font-size: clamp(11px, 3vw, 12px);
  color: var(--tg-text-secondary);
  padding: 8px 12px 4px;
  font-variant-numeric: tabular-nums;
  direction: ltr;
  unicode-bidi: isolate;
  flex-shrink: 0;
}

.pagination-sentinel {
  width: 100%;
  height: 1px;
  flex-shrink: 0;
  pointer-events: none;
  visibility: hidden;
}

.window-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 16px;
  color: var(--tg-text-secondary);
  font-size: clamp(12px, 3.2vw, 13px);
}

.window-loading-top {
  margin-bottom: 4px;
}

.window-loading-bottom {
  margin-top: 4px;
}

.window-start,
.window-end {
  text-align: center;
  font-size: clamp(11px, 3vw, 12px);
  color: var(--tg-text-secondary);
  padding: 12px;
}

.window-start {
  margin-bottom: 4px;
}

.window-end {
  margin-top: 8px;
}

.spinner.small {
  width: 22px;
  height: 22px;
  border-width: 2px;
}

/* ─── Messages ─── */

.date-divider {
  display: flex;
  justify-content: center;
  margin: 16px 0 8px;
}

.date-divider span {
  background: var(--tg-date-divider-bg);
  color: var(--tg-date-divider-text);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 12px;
  font-weight: 600;
}

.message-service {
  text-align: center;
  font-size: 13px;
  color: var(--tg-text-secondary);
  padding: 6px 16px;
  margin: 8px 0;
  background: var(--tg-surface-muted);
  border-radius: 12px;
  align-self: center;
  max-width: 80%;
}

.message-row {
  display: flex;
  align-items: flex-end;
  gap: clamp(6px, 2vw, 8px);
  margin-bottom: 3px;
  max-width: var(--bubble-max);
  clear: both;
  width: fit-content;
}

.message-row.incoming {
  align-self: flex-start;
  margin-left: 0;
  margin-right: auto;
}

.message-row.outgoing {
  align-self: flex-end;
  flex-direction: row-reverse;
  margin-right: 0;
  margin-left: auto;
}

.message-row.joined {
  margin-top: -2px;
}

.message-row.outgoing.joined .message-bubble {
  border-top-right-radius: 6px;
}

.message-row.incoming.joined .message-bubble {
  border-top-left-radius: 6px;
}

.message-row.outgoing .message-bubble {
  background: var(--tg-bubble-out);
  border-bottom-right-radius: 6px;
}

.message-row.incoming .message-bubble {
  background: var(--tg-bubble-in);
  border-bottom-left-radius: 6px;
  box-shadow: 0 1px 1px var(--tg-bubble-shadow);
}

.message-bubble {
  padding: var(--bubble-pad-y) var(--bubble-pad-x) 4px;
  border-radius: var(--bubble-radius);
  max-width: 100%;
  min-width: 0;
  position: relative;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.message-sender {
  font-size: var(--msg-sender-font);
  font-weight: 600;
  color: var(--tg-blue);
  margin-bottom: 4px;
}

.message-text {
  font-family: var(--font-family);
  font-weight: 700;
  font-size: var(--msg-font);
  line-height: 1.45;
  white-space: pre-wrap;
}

.message-emoji {
  display: inline-block;
  width: 1.35em;
  height: 1.35em;
  margin: 0 1px;
  vertical-align: -0.25em;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}

.reply-text .message-emoji {
  width: 1.15em;
  height: 1.15em;
  vertical-align: -0.2em;
}

.chat-item-name .message-emoji,
.chat-item-preview .message-emoji,
.chat-header-info h2 .message-emoji,
.reply-author .message-emoji,
.date-divider .message-emoji {
  width: 1.15em;
  height: 1.15em;
  vertical-align: -0.18em;
}

.chat-item-name .message-emoji,
.chat-header-info h2 .message-emoji {
  width: 1.2em;
  height: 1.2em;
}

.message-text a {
  color: var(--tg-blue);
  text-decoration: none;
}

.message-text a:hover {
  text-decoration: underline;
}

.message-text code {
  background: var(--tg-surface-muted);
  padding: 1px 4px;
  border-radius: 4px;
  font-size: 13px;
}

.message-text pre {
  background: var(--tg-surface-muted);
  padding: 8px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
  margin: 4px 0;
}

.message-meta {
  display: flex;
  justify-content: flex-end;
  margin-top: 2px;
}

.message-time {
  font-size: var(--msg-meta-font);
  color: var(--tg-text-secondary);
  font-variant-numeric: tabular-nums;
  direction: ltr;
  unicode-bidi: isolate;
}

.date-divider span {
  background: var(--tg-voice-wave);
  color: var(--tg-on-primary);
  font-size: 12px;
  padding: 5px 14px;
  border-radius: 14px;
  font-weight: 600;
  font-variant-numeric: lining-nums;
}

/* ─── Reply ─── */

.message-reply {
  display: flex;
  gap: 8px;
  margin-bottom: 6px;
  padding: 6px 8px;
  border-radius: 8px;
  background: var(--tg-surface-overlay);
  overflow: hidden;
}

.message-reply[data-reply-to] {
  cursor: pointer;
  transition: background 0.15s;
}

.message-reply[data-reply-to]:hover {
  background: var(--tg-surface-hover);
}

.message-reply .reply-bar {
  width: 2px;
  border-radius: 2px;
  flex-shrink: 0;
  align-self: stretch;
}

.message-reply.in-incoming .reply-bar {
  background: var(--tg-reply-in);
}

.message-reply.in-outgoing .reply-bar {
  background: var(--tg-reply-out);
}

.reply-author {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 2px;
}

.reply-author-in {
  color: var(--tg-reply-in);
}

.reply-author-out {
  color: var(--tg-reply-out);
}

.reply-text {
  font-size: clamp(12px, 3.2vw, 13px);
  color: var(--tg-text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: min(240px, 55vw);
}

.reply-photo-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.reply-photo-thumb {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
}

/* ─── Media ─── */

.media-photo img,
.media-sticker img {
  width: auto;
  height: auto;
  max-width: var(--media-max-w);
  max-height: var(--media-max-h);
  border-radius: clamp(8px, 2.5vw, 10px);
  display: block;
  margin-bottom: 4px;
  cursor: pointer;
  object-fit: contain;
}

.media-video video {
  width: 100%;
  max-width: var(--media-max-w);
  max-height: var(--media-max-h);
  border-radius: clamp(8px, 2.5vw, 10px);
  display: block;
  object-fit: contain;
}

.media-voice {
  display: flex;
  align-items: center;
  gap: clamp(6px, 2vw, 10px);
  width: 100%;
  min-width: min(180px, 70vw);
  max-width: min(280px, 100%);
  padding: 4px 0;
}

.voice-play-btn {
  width: var(--voice-btn);
  height: var(--voice-btn);
  min-width: var(--voice-btn);
  min-height: var(--voice-btn);
  border-radius: 50%;
  border: none;
  background: var(--tg-blue);
  color: var(--tg-on-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
  touch-action: manipulation;
}

.message-row.outgoing .voice-play-btn {
  background: var(--tg-green);
}

.voice-play-btn:hover {
  filter: brightness(1.08);
}

.voice-wave {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 1;
  height: 28px;
}

.voice-wave span {
  display: block;
  width: 3px;
  border-radius: 2px;
  background: var(--tg-voice-wave);
  height: 8px;
  transition: height 0.15s;
}

.voice-wave span:nth-child(odd) { height: 14px; }
.voice-wave span:nth-child(3n) { height: 20px; }
.voice-wave span:nth-child(4n) { height: 11px; }

.media-voice.playing .voice-wave span {
  animation: voiceBar 0.8s ease-in-out infinite alternate;
  background: var(--tg-blue);
}

.message-row.outgoing .media-voice.playing .voice-wave span {
  background: var(--tg-green);
}

.media-voice.playing .voice-wave span:nth-child(2) { animation-delay: 0.1s; }
.media-voice.playing .voice-wave span:nth-child(3) { animation-delay: 0.2s; }
.media-voice.playing .voice-wave span:nth-child(4) { animation-delay: 0.15s; }
.media-voice.playing .voice-wave span:nth-child(5) { animation-delay: 0.25s; }

@keyframes voiceBar {
  from { height: 6px; }
  to { height: 22px; }
}

.voice-duration,
.voice-timer {
  font-size: 12px;
  color: var(--tg-text-secondary);
  font-variant-numeric: tabular-nums;
  direction: ltr;
  flex-shrink: 0;
  min-width: 2.5em;
  text-align: left;
}

.media-file .file-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--tg-surface-overlay);
  border-radius: 8px;
  text-decoration: none;
  color: var(--tg-blue);
  font-size: 14px;
}

.media-file .file-link:hover {
  background: var(--tg-surface-hover);
}

.media-missing {
  font-size: 13px;
  color: var(--tg-text-secondary);
  font-style: italic;
  padding: 8px;
  background: var(--tg-surface-overlay);
  border-radius: 8px;
  margin-bottom: 4px;
}

/* ─── Empty / Welcome ─── */

.empty-state,
.empty-chat,
.welcome-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px 20px;
  color: var(--tg-text-secondary);
  height: 100%;
}

.empty-state.error {
  color: var(--tg-danger);
}

.welcome-panel h2 {
  font-size: 24px;
  color: var(--tg-blue);
  margin: 16px 0 8px;
}

.welcome-panel p {
  font-size: 15px;
}

.empty-state small {
  margin-top: 8px;
  font-size: 12px;
}

/* ─── Loading ─── */

.app-loading {
  position: absolute;
  inset: 0;
  background: var(--tg-loading-overlay);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
  gap: 16px;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--tg-border);
  border-top-color: var(--tg-blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ─── Scrollbar ─── */

.chat-list::-webkit-scrollbar,
.messages-container::-webkit-scrollbar {
  width: 10px;
}

.chat-list::-webkit-scrollbar-track,
.messages-container::-webkit-scrollbar-track {
  background: var(--tg-surface-muted);
  border-radius: 5px;
}

.chat-list::-webkit-scrollbar-thumb,
.messages-container::-webkit-scrollbar-thumb {
  background: var(--tg-scrollbar-thumb);
  border-radius: 5px;
  border: 2px solid transparent;
  background-clip: padding-box;
}

.chat-list::-webkit-scrollbar-thumb:hover,
.messages-container::-webkit-scrollbar-thumb:hover {
  background: var(--tg-scrollbar-thumb-hover);
  background-clip: padding-box;
}

@media (max-width: 768px) {
  .chat-list::-webkit-scrollbar,
  .messages-container::-webkit-scrollbar {
    width: 6px;
  }
}

/* ─── Identity Picker ─── */

.identity-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--tg-login-bg);
  padding: 20px;
  position: relative;
}

.identity-card {
  background: var(--tg-bg);
  border-radius: 16px;
  padding: 36px 32px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 8px 32px var(--tg-card-shadow);
  text-align: center;
}

.identity-card h2 {
  font-size: 24px;
  margin-bottom: 8px;
}

.identity-subtitle {
  color: var(--tg-text-secondary);
  font-size: 14px;
  margin-bottom: 24px;
}

.identity-options {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.identity-btn {
  width: 100%;
  padding: 14px 20px;
  border: 2px solid var(--tg-border);
  border-radius: 12px;
  background: var(--tg-bg-secondary);
  color: var(--tg-text);
  font-family: var(--font-family);
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  touch-action: manipulation;
}

.identity-btn:hover {
  background: var(--tg-active);
  border-color: var(--tg-blue);
}

.identity-btn:active {
  transform: scale(0.98);
}

/* ─── Rahma welcome fullscreen (MR.RA) ─── */

body.rahma-welcome-open {
  overflow: hidden;
}

.rahma-welcome {
  position: fixed;
  inset: 0;
  z-index: 250;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(20px, var(--safe-top))
    max(16px, var(--safe-right))
    max(20px, var(--safe-bottom))
    max(16px, var(--safe-left));
  opacity: 0;
  pointer-events: auto;
  transition: opacity 0.55s ease;
}

.rahma-welcome-in {
  opacity: 1;
}

.rahma-welcome-out {
  opacity: 0;
}

.rahma-welcome-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(8, 12, 18, 0.82);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.rahma-welcome-content {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: min(88vw, 760px);
  text-align: center;
}

.rahma-welcome-text {
  margin: 0;
  font-family: var(--font-family);
  font-size: clamp(28px, 5.5vw, 52px);
  font-weight: 700;
  line-height: 1.35;
  color: #ffffff;
  text-shadow: 0 2px 20px rgba(0, 0, 0, 0.35);
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: auto;
}

@media (max-width: 768px) {
  .rahma-welcome-content {
    max-width: min(72vw, 280px);
  }

  .rahma-welcome-text {
    font-size: clamp(24px, 9vw, 38px);
    line-height: 1.5;
    letter-spacing: 0.01em;
  }
}

@media (max-width: 380px) {
  .rahma-welcome-content {
    max-width: min(68vw, 220px);
  }

  .rahma-welcome-text {
    font-size: clamp(22px, 10vw, 32px);
    line-height: 1.55;
  }
}

/* ─── Photo Lightbox ─── */

.photo-lightbox {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.photo-lightbox .lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
}

.photo-lightbox .lightbox-stage {
  position: relative;
  z-index: 1;
  max-width: min(96vw, 1200px);
  max-height: 92vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.photo-lightbox .lightbox-img {
  max-width: 100%;
  max-height: 92vh;
  object-fit: contain;
  border-radius: 4px;
  user-select: none;
}

.photo-lightbox .lightbox-close {
  position: absolute;
  top: max(12px, var(--safe-top));
  left: max(12px, var(--safe-left));
  z-index: 2;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  color: var(--tg-on-primary);
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.photo-lightbox .lightbox-close:hover {
  background: rgba(255, 255, 255, 0.25);
}

/* ─── Reply highlight ─── */

.message-row.message-highlight .message-bubble {
  animation: msgHighlight 1.8s ease;
}

@keyframes msgHighlight {
  0%, 100% { box-shadow: none; }
  25% { box-shadow: 0 0 0 2px var(--tg-blue); }
  50% { box-shadow: 0 0 0 2px var(--tg-input-focus-ring); }
}

/* ─── Chat Search ─── */

.chat-search-wrap {
  background: var(--tg-bg);
  border-bottom: 1px solid var(--tg-border);
  flex-shrink: 0;
  z-index: 2;
}

.chat-search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
}

.search-bar-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 28px;
  height: 28px;
}

.search-bar-icon .message-emoji {
  width: 22px;
  height: 22px;
}

.chat-search-bar input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border: 1px solid var(--tg-border);
  border-radius: 20px;
  background: var(--tg-bg-secondary);
  color: var(--tg-text);
  font-family: var(--font-family);
  font-weight: 600;
  font-size: 15px;
  outline: none;
}

.chat-search-bar input:focus {
  border-color: var(--tg-blue);
  box-shadow: 0 0 0 3px var(--tg-input-focus-ring);
}

.search-clear-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: var(--tg-surface-muted);
  color: var(--tg-text-secondary);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.chat-search-results {
  max-height: min(42vh, 360px);
  overflow-y: auto;
  background: var(--tg-bg);
  border-bottom: 1px solid var(--tg-border);
  scrollbar-width: thin;
}

.search-result-item {
  display: block;
  width: 100%;
  padding: 12px 16px;
  border: none;
  border-bottom: 1px solid var(--tg-item-border);
  background: none;
  text-align: right;
  cursor: pointer;
  font-family: var(--font-family);
  touch-action: manipulation;
}

.search-result-item:hover,
.search-result-item:focus-visible {
  background: var(--tg-hover);
  outline: none;
}

.search-result-text {
  font-weight: 700;
  font-size: 14px;
  color: var(--tg-text);
  margin-bottom: 4px;
  line-height: 1.45;
}

.search-result-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--tg-text-secondary);
}

.search-result-item.outgoing .search-result-side {
  color: var(--tg-reply-out);
}

.search-result-item.incoming .search-result-side {
  color: var(--tg-reply-in);
}

.search-result-date {
  direction: ltr;
  unicode-bidi: isolate;
}

.search-hit {
  background: rgba(51, 144, 236, 0.28);
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
}

[data-theme="dark"] .search-hit {
  background: rgba(106, 179, 243, 0.28);
}

.search-status,
.search-empty {
  padding: 16px;
  text-align: center;
  color: var(--tg-text-secondary);
  font-size: 14px;
}

.search-toggle-btn {
  display: none;
  min-width: 44px;
  min-height: 44px;
  align-items: center;
  justify-content: center;
  padding: 6px;
}

.search-toggle-btn .message-emoji,
.search-toggle-btn .search-emoji {
  width: 30px;
  height: 30px;
}

.search-emoji-fallback {
  font-size: 24px;
  line-height: 1;
}

@media (max-width: 768px) {
  .chat-search-wrap:not(.search-open) {
    display: none !important;
  }

  .chat-search-wrap.search-open {
    display: block;
  }

  .search-toggle-btn {
    display: inline-flex;
  }

  .search-toggle-btn .message-emoji,
  .search-toggle-btn .search-emoji {
    width: 32px;
    height: 32px;
  }
}

