/* ============ Telegram-style message viewer ============ */
:root {
  --tg-blue: #3390ec;
  --tg-blue-dark: #2b7fd4;
  --green: #4fae4e;
  --green-bubble: #e3f2fd;
  --white-bubble: #ffffff;
  --text: #1f2430;
  --muted: #707579;
  --border: #e4e8ea;
  --error: #d64545;
  --sidebar-bg: #ffffff;
  --sidebar-hover: #f4f5f6;
  --sidebar-active: #3390ec;
  /* chat area: subtle blue paper with a fine dot pattern (data URI = offline-safe) */
  --chat-bg: #d6e4f0;
  --chat-pattern: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='28' height='28'%3E%3Ccircle cx='2' cy='2' r='1.1' fill='%23a9c4dc' opacity='0.55'/%3E%3C/svg%3E");
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background: var(--chat-bg);
}

a { color: var(--tg-blue); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ============ App frame (two panels) ============ */
.app {
  display: flex;
  height: 100vh;
  /* Proportional width: fill up to 92% of the viewport, capped at a sane max.
     On very wide screens the app stays centred with breathing room. */
  width: 100%;
  max-width: clamp(360px, 92vw, 1600px);
  margin: 0 auto;
  background: var(--sidebar-bg);
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.12);
}

/* ============ Sidebar (chat list) ============ */
.sidebar {
  /* Proportional to the app width; clamped to a readable range so it neither
     collapses on narrow desktops nor dominates wide ones. */
  width: clamp(260px, 26%, 400px);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
}

.sidebar-head {
  padding: 10px 12px;
}

.search-summary {
  margin: 8px 4px 0;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.4;
}

.search-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f1f3f5;
  border-radius: 18px;
  padding: 7px 14px;
  transition: background 0.15s;
}
.search-pill:focus-within { background: #e9eef2; }
.search-pill input {
  border: none;
  background: transparent;
  outline: none;
  font-size: 0.92rem;
  width: 100%;
  font-family: var(--font);
  color: var(--text);
}
.pill-icon { color: var(--muted); display: flex; align-items: center; }

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 12px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 10px;
  border-radius: 12px;
  color: var(--text);
  transition: background 0.12s;
}
.chat-item:hover { background: var(--sidebar-hover); text-decoration: none; }
.chat-item.active { background: var(--sidebar-active); }
.chat-item.active .chat-name,
.chat-item.active .chat-time,
.chat-item.active .chat-sub { color: #fff; }
.chat-item.active .chat-count-sep { opacity: 0.7; }

.chat-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.chat-top { display: flex; align-items: baseline; gap: 8px; }
.chat-name {
  font-weight: 600;
  font-size: 0.95rem;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-time { font-size: 0.75rem; color: var(--muted); flex-shrink: 0; }
.chat-sub { display: flex; align-items: center; gap: 6px; font-size: 0.8rem; color: var(--muted); }
.chat-count-sep { opacity: 0.6; }
.chat-last {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* ============ Avatars ============ */
.avatar {
  width: 46px;
  height: 46px;
  flex-shrink: 0;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 1.15rem;
  font-weight: 600;
  background: linear-gradient(135deg, #ff885e, #ff516a);
  user-select: none;
}
.avatar.alt { background: linear-gradient(135deg, #37aee2, #1e96c8); }
.chat-item .avatar { width: 46px; height: 46px; }
.msg-header .avatar { width: 42px; height: 42px; font-size: 1.05rem; }

/* ============ Chat pane (right) ============ */
.chat-pane {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background-color: var(--chat-bg);
  background-image: var(--chat-pattern);
}

.msg-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 2;
}
.back-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  color: var(--tg-blue);
  background: transparent;
  border: none;
  cursor: pointer;
}
.back-btn:hover { background: var(--sidebar-hover); }
.msg-title { min-width: 0; }
.msg-name {
  font-weight: 600;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg-sub { font-size: 0.8rem; color: var(--muted); }

/* ============ In-chat search ============ */
.msg-search {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px auto 12px;
  background: #fff;
  border-radius: 18px;
  padding: 7px 14px;
  box-shadow: var(--shadow);
  max-width: min(calc(100% - 32px), 640px);
  position: relative;
}
.msg-search input[type="text"] {
  border: none;
  outline: none;
  font-size: 0.9rem;
  flex: 1;
  background: transparent;
  font-family: var(--font);
  color: var(--text);
  min-width: 0;
}
.search-clear {
  color: var(--muted);
  font-size: 1.2rem;
  line-height: 1;
  width: 22px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}
.search-clear:hover { background: var(--sidebar-hover); color: var(--text); text-decoration: none; }

/* ============ Message list ============ */
.msg-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px 16px;
}

.msg-inner {
  /* Fill the chat pane (no absolute cap) so the message area scales with the
     window; bubbles are clamped by .bubble below. */
  position: relative;   /* anchor for the floating jump-latest button */
  width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  min-height: 100%;
  padding: 0 6px;
}

/* Infinite-scroll loader at the top (older messages) */
.load-more {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 0;
  min-height: 34px;
}
.load-label {
  font-size: 0.8rem;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.55);
  padding: 5px 14px;
  border-radius: 13px;
}
.load-label.oldest { color: var(--muted); }
.load-label.err { color: var(--error); background: #fdecec; }

/* Floating "jump to latest" button — sits inside the message column
   (.msg-inner) and stays pinned near the bottom of the scroller viewport. */
.jump-latest {
  position: sticky;
  bottom: 16px;
  /* push to the right edge of the message column */
  align-self: flex-end;
  margin: 8px 6px 8px 0;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--tg-blue);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
  z-index: 5;
  flex-shrink: 0;
}
.jump-latest.show {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}
.jump-latest:hover { box-shadow: 0 6px 18px rgba(0, 0, 0, 0.24); }
.jump-latest:active { transform: translateY(0) scale(0.94); }
.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(0, 0, 0, 0.15);
  border-top-color: var(--tg-blue);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: none;
}
.load-more.loading .spinner { display: inline-block; }
.load-more.loading .load-label { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

.msg-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.day-chip {
  align-self: center;
  background: rgba(0, 0, 0, 0.18);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 500;
  padding: 3px 12px;
  border-radius: 12px;
  margin: 10px 0 6px;
  backdrop-filter: blur(2px);
}

.bubble-wrap { display: flex; }
.bubble-wrap.own { justify-content: flex-end; }
.bubble-wrap.other { justify-content: flex-start; }

.bubble {
  /* Scale with the chat pane; min keeps short bubbles readable on phones. */
  max-width: 82%;
  padding: 7px 12px 6px;
  border-radius: 14px;
  box-shadow: var(--shadow);
  position: relative;
  animation: bubble-in 0.18s ease-out both;
}
@keyframes bubble-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.bubble-wrap.own .bubble {
  background: var(--green-bubble);
  border-bottom-right-radius: 5px;
}
.bubble-wrap.other .bubble {
  background: var(--white-bubble);
  border-bottom-left-radius: 5px;
}

.sender {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--tg-blue);
  margin-bottom: 2px;
}
.bubble-wrap.own .sender { color: var(--green); }

.bubble .text {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--font);
  font-size: 0.95rem;
  line-height: 1.4;
}

/* Rendered markdown inside a message */
.bubble .text b,
.bubble .text strong { font-weight: 700; }
.bubble .text a {
  color: var(--tg-blue);
  text-decoration: none;
  word-break: break-all;
}
.bubble .text a:hover { text-decoration: underline; }
.bubble .text code {
  font-family: var(--mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
  background: rgba(0, 0, 0, 0.06);
  border-radius: 4px;
  padding: 1px 4px;
  font-size: 0.85em;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble-wrap.own .text code { background: rgba(255, 255, 255, 0.18); }
.bubble .text pre {
  margin: 4px 0;
  padding: 8px 10px;
  background: rgba(0, 0, 0, 0.06);
  border-radius: 8px;
  overflow-x: auto;
}
.bubble-wrap.own .text pre { background: rgba(0, 0, 0, 0.18); }
.bubble .text pre code {
  background: none;
  padding: 0;
  font-size: 0.82em;
  white-space: pre;
}

.meta-inline {
  float: right;
  margin: 8px 0 0 10px;
  font-size: 0.7rem;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 5px;
  user-select: none;
}
.bubble-wrap.own .meta-inline { color: #6a9e6e; }
.edited { font-style: italic; }

.msg-empty {
  align-self: center;
  text-align: center;
  color: var(--muted);
  padding: 40px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.msg-empty svg { opacity: 0.5; }
.empty { color: var(--muted); padding: 20px; text-align: center; }

/* ============ Search highlight ============ */
mark {
  background: #fff3a3;
  color: inherit;
  padding: 0 2px;
  border-radius: 3px;
}

/* ============ Login ============ */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--chat-bg);
  background-image: var(--chat-pattern);
  padding: 20px;
}

.login-card {
  background: #fff;
  border-radius: 16px;
  padding: 34px 30px;
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 13px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.16);
  animation: card-in 0.3s ease-out both;
}
@keyframes card-in {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.login-logo { display: flex; justify-content: center; }
.login-card h1 { margin: 4px 0 0; font-size: 1.15rem; text-align: center; }
.login-sub { margin: 0; text-align: center; color: var(--muted); font-size: 0.85rem; }

.login-card label { display: flex; flex-direction: column; gap: 5px; font-size: 0.82rem; color: var(--muted); font-weight: 500; }

.login-card input[type="text"],
.login-card input[type="password"] {
  padding: 10px 13px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 0.95rem;
  font-family: var(--font);
  color: var(--text);
  transition: border-color 0.15s;
}
.login-card input:focus { outline: none; border-color: var(--tg-blue); }

.login-card button {
  margin-top: 6px;
  padding: 11px;
  background: var(--tg-blue);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 0.98rem;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s;
}
.login-card button:hover { background: var(--tg-blue-dark); }

.error {
  color: var(--error);
  font-size: 0.85rem;
  margin: 0;
  background: #fdecec;
  border-radius: 8px;
  padding: 8px 10px;
  text-align: center;
}

/* ============ Scrollbars (WebKit) ============ */
.chat-list::-webkit-scrollbar,
.msg-scroll::-webkit-scrollbar { width: 6px; }
.chat-list::-webkit-scrollbar-thumb,
.msg-scroll::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.18);
  border-radius: 3px;
}

/* ============ Responsive ============ */

/* Tablet / smaller desktop: tighten the sidebar a bit */
@media (max-width: 1024px) {
  .sidebar {
    width: clamp(220px, 30%, 320px);
  }
  .bubble {
    max-width: 84%;
  }
}

/* Mobile: single panel (sidebar <-> chat) */
@media (max-width: 780px) {
  .app { height: 100dvh; width: 100%; max-width: 100%; box-shadow: none; }
  .sidebar { width: 100%; border-right: none; }
  .chat-pane { display: none; }
  .app.show-chat .sidebar { display: none; }
  .app.show-chat .chat-pane { display: flex; }
  .back-btn { display: inline-flex; }
  .bubble { max-width: 88%; }
  .msg-header { padding: 8px 10px; gap: 10px; }
  .msg-header .avatar { width: 38px; height: 38px; font-size: 1rem; }
  .msg-search { margin: 8px 10px 0; max-width: calc(100% - 20px); }
  .msg-scroll { padding: 8px 8px 14px; }
  .bubble .text { font-size: 0.92rem; }
  .day-chip { font-size: 0.72rem; padding: 2px 10px; margin: 8px 0 5px; }
  .chat-item .avatar { width: 42px; height: 42px; font-size: 1.05rem; }
  .jump-latest { right: 12px; bottom: 12px; width: 40px; height: 40px; }
}
