:root {
  --bg: #f4f4f5;
  --chat-bg: #c8d7b4;
  --chat-pattern: linear-gradient(135deg, #d6e2bd 0%, #a8c69b 50%, #8fb4a3 100%);
  --surface: #ffffff;
  --bubble-in: #ffffff;
  --bubble-out: #e3fee0;
  --text: #0f1419;
  --muted: #707579;
  --meta-out: #4fae4e;
  --accent: #3390ec;
  --line: #e4e4e7;
  --service-bg: rgba(0, 0, 0, .22);
  --reply-bg: rgba(51, 144, 236, .08);
  --shadow: 0 1px 2px rgba(16, 35, 47, .15);
  --radius: 15px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #181818;
    --chat-bg: #0f0f0f;
    --chat-pattern: linear-gradient(135deg, #1b2a24 0%, #14201d 50%, #10181f 100%);
    --surface: #212121;
    --bubble-in: #212121;
    --bubble-out: #2b5278;
    --text: #f5f5f5;
    --muted: #aaaaaa;
    --meta-out: #8ab4dd;
    --accent: #8774e1;
    --line: #2f2f2f;
    --service-bg: rgba(0, 0, 0, .45);
    --reply-bg: rgba(255, 255, 255, .06);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}
body.chat-bg { background: var(--chat-pattern) fixed, var(--chat-bg); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 28px 0 10px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  height: 56px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.topbar .back { font-size: 22px; color: var(--muted); padding: 4px 8px; border-radius: 50%; }
.topbar .back:hover { background: var(--bg); text-decoration: none; }
.topbar-title { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.topbar-title b, .topbar-title small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-title small { color: var(--muted); }
.topbar-nav { display: flex; gap: 4px; }
.topbar-nav a { font-size: 19px; padding: 6px 9px; border-radius: 50%; }
.topbar-nav a:hover { background: var(--bg); text-decoration: none; }
.search-input {
  flex: 1; height: 40px; padding: 0 16px; font: inherit; color: var(--text);
  border: 1px solid var(--line); border-radius: 20px; background: var(--bg); outline: none;
}
.search-input:focus { border-color: var(--accent); }

/* ---------- pages ---------- */
.page { max-width: 760px; margin: 0 auto; padding: 16px; }
.page.wide { max-width: 1100px; }
.avatar {
  flex: none; display: inline-flex; align-items: center; justify-content: center;
  width: 42px; height: 42px; border-radius: 50%; object-fit: cover;
  color: #fff; font-weight: 600; font-size: 15px;
}
.avatar.big { width: 96px; height: 96px; font-size: 36px; }

.dialogs { background: var(--surface); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
.dialog { display: flex; gap: 12px; align-items: center; padding: 10px 14px; color: var(--text); }
.dialog + .dialog { border-top: 1px solid var(--line); }
.dialog:hover { background: var(--bg); text-decoration: none; }
.dialog-body { display: flex; flex-direction: column; min-width: 0; }
.dialog-body small { color: var(--muted); }

.chat-card { display: flex; gap: 20px; align-items: center; padding: 20px; background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); }
.chat-card p { margin: 4px 0; }
.stats { display: flex; flex-wrap: wrap; gap: 14px; }
.warn { color: #d97706; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px !important; }
.btn { display: inline-block; padding: 8px 16px; border-radius: 10px; background: var(--accent); color: #fff; font-weight: 500; }
.btn.ghost { background: transparent; color: var(--accent); border: 1px solid var(--line); }
.btn:hover { text-decoration: none; opacity: .9; }

.months { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; }
.month { display: flex; flex-direction: column; padding: 12px 14px; background: var(--surface); border-radius: 10px; color: var(--text); box-shadow: var(--shadow); }
.month:hover { text-decoration: none; outline: 2px solid var(--accent); }
.month small { color: var(--muted); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 3px; }
.tile { position: relative; aspect-ratio: 1; overflow: hidden; background: var(--line); }
.tile img, .tile video { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform .2s; }
.tile:hover img { transform: scale(1.04); }

/* ---------- history ---------- */
.history { max-width: 760px; margin: 0 auto; padding: 8px 12px 40px; display: flex; flex-direction: column; }
.month-nav { align-self: center; margin: 14px 0; padding: 6px 14px; border-radius: 16px; background: var(--service-bg); color: #fff; font-weight: 500; }
.day { position: sticky; top: 64px; z-index: 5; align-self: center; margin: 10px 0; }
.day span, .service > span { display: inline-block; padding: 3px 10px; border-radius: 14px; background: var(--service-bg); color: #fff; font-size: 14px; font-weight: 500; }
.service { align-self: center; text-align: center; margin: 6px 0; max-width: 80%; }
.service a { color: #fff; text-decoration: underline; }
.service-photo { display: block; margin: 6px auto 0; width: 120px; height: 120px; border-radius: 50%; object-fit: cover; }

.row { display: flex; align-items: flex-end; gap: 8px; margin-top: 2px; }
.row.first { margin-top: 8px; }
.row.out { justify-content: flex-end; }
.row-avatar { width: 42px; flex: none; align-self: flex-start; }
.row:target .bubble, .row.flash .bubble { animation: flash 1.6s ease; }
@keyframes flash { 0%, 40% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: var(--shadow); } }

.bubble {
  position: relative; max-width: min(480px, 85%); min-width: 60px;
  padding: 6px 9px 6px; border-radius: var(--radius);
  background: var(--bubble-in); box-shadow: var(--shadow); overflow-wrap: anywhere;
}
.row.in.first .bubble { border-top-left-radius: 6px; }
.row.out .bubble { background: var(--bubble-out); }
.row.out.first .bubble { border-top-right-radius: 6px; }
.row .bubble.bare { background: none; box-shadow: none; padding: 0; }
.bubble.has-media { padding: 0; overflow: hidden; }
.bubble.has-media .text { padding: 6px 9px 0; }
.bubble.has-media .meta { padding: 0 9px 5px; }
.bubble.has-media .sender { padding: 6px 9px 4px; }

.sender { font-weight: 600; font-size: 14px; margin-bottom: 2px; }
.fwd { font-size: 14px; color: var(--accent); margin-bottom: 4px; }
.reply {
  display: flex; flex-direction: column; margin: 2px 0 5px; padding: 3px 8px;
  border-left: 3px solid; border-radius: 4px; background: var(--reply-bg); color: var(--text); font-size: 14px;
}
.reply span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
.reply:hover { text-decoration: none; filter: brightness(.97); }
.text { white-space: pre-wrap; }
.text pre, .text code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13.5px; }
.text pre { margin: 4px 0; padding: 6px 8px; border-radius: 6px; background: var(--reply-bg); overflow-x: auto; }
.text blockquote { margin: 4px 0; padding: 2px 8px; border-left: 3px solid var(--accent); background: var(--reply-bg); border-radius: 4px; }
tg-spoiler, .text spoiler { background: var(--muted); color: transparent; border-radius: 4px; cursor: pointer; }
tg-spoiler.open { background: none; color: inherit; }

.meta { display: flex; justify-content: flex-end; align-items: center; flex-wrap: wrap; gap: 6px; margin-top: 2px; font-size: 12px; color: var(--muted); }
.row.out .meta, .row.out .meta a { color: var(--meta-out); }
.meta .time { color: inherit; }
.bubble.bare .meta { display: inline-flex; padding: 1px 8px; border-radius: 10px; background: var(--service-bg); color: #fff; float: right; }
.bubble.bare .meta .time { color: #fff; }
.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-right: auto; }
.reaction { padding: 1px 7px; border-radius: 12px; background: var(--reply-bg); color: var(--accent); font-size: 13px; }

/* ---------- media ---------- */
.media { display: block; position: relative; }
.media img, .media video { display: block; width: 100%; height: auto; max-height: 560px; object-fit: cover; background: #000; }
.bubble:not(.has-media) .media img, .bubble:not(.has-media) .media video { border-radius: 10px; margin: 2px 0; }
.media.round { width: 240px; height: 240px; border-radius: 50%; object-fit: cover; background: #000; }
.media.gif { border-radius: 10px; max-width: 100%; }
.badge { position: absolute; left: 8px; top: 8px; padding: 1px 6px; border-radius: 8px; background: rgba(0, 0, 0, .5); color: #fff; font-size: 12px; }
.sticker { width: 200px; height: 200px; object-fit: contain; display: block; }
.sticker-tgs { font-size: 96px; line-height: 1; text-decoration: none !important; }
.album { display: grid; gap: 2px; grid-template-columns: 1fr 1fr; }
.album-3 > :first-child { grid-column: 1 / -1; }
.album-item .media img, .album-item .media video { aspect-ratio: 1; height: 100%; max-height: none; }
.album-item .file { margin: 6px; }

.file { display: flex; align-items: center; gap: 10px; padding: 4px 0; color: var(--text); }
.file:hover { text-decoration: none; }
.file-icon { flex: none; display: inline-flex; align-items: center; justify-content: center; width: 46px; height: 46px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 20px; }
.file.missing .file-icon { background: #9ca3af; }
.file-info { display: flex; flex-direction: column; min-width: 0; }
.file-info b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-info small { color: var(--muted); }
.audio { display: flex; flex-direction: column; gap: 2px; padding: 2px 0; }
.audio audio { width: 280px; max-width: 100%; height: 40px; }
.audio small { color: var(--muted); }
.audio-title { font-weight: 500; }
.webpage { display: flex; flex-direction: column; margin-top: 4px; padding: 4px 8px; border-left: 3px solid var(--accent); border-radius: 4px; background: var(--reply-bg); color: var(--text); font-size: 14px; }
.webpage b { color: var(--accent); }
.webpage .wp-title { font-weight: 600; }
.webpage .wp-desc { color: var(--muted); }
.webpage:hover { text-decoration: none; }
.poll { display: flex; flex-direction: column; gap: 6px; min-width: 240px; padding: 2px 0; }
.poll-row { position: relative; display: flex; justify-content: space-between; gap: 8px; padding: 4px 8px; border-radius: 6px; overflow: hidden; }
.poll-bar { position: absolute; inset: 0 auto 0 0; background: var(--reply-bg); z-index: 0; }
.poll-row > :not(.poll-bar) { position: relative; }
.poll > small { color: var(--muted); }

/* ---------- search ---------- */
.result { display: block; padding: 10px 14px; margin-bottom: 6px; background: var(--surface); border-radius: 10px; color: var(--text); box-shadow: var(--shadow); }
.result:hover { text-decoration: none; outline: 2px solid var(--accent); }
.result-head { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--muted); }
.result-head b { color: var(--text); }
.result-text { white-space: pre-wrap; overflow-wrap: anywhere; }
mark { background: #fde68a; color: inherit; border-radius: 2px; }

@media (max-width: 560px) {
  .history { padding: 8px 6px 40px; }
  .row-avatar { width: 34px; }
  .row-avatar .avatar { width: 34px; height: 34px; font-size: 13px; }
  .bubble { max-width: 88%; }
  .media.round { width: 200px; height: 200px; }
  .chat-card { flex-direction: column; text-align: center; }
  .stats, .actions { justify-content: center; }
  .grid { grid-template-columns: repeat(3, 1fr); }
}

/* ---------- video thumbnails ---------- */
.media.video img, .media.video video { cursor: pointer; }
.play {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  display: flex; align-items: center; justify-content: center;
  width: 54px; height: 54px; padding-left: 4px; border-radius: 50%;
  background: rgba(0, 0, 0, .5); color: #fff; font-size: 22px; pointer-events: none;
}
.album-item .play { width: 40px; height: 40px; font-size: 16px; }

/* ---------- lightbox ---------- */
html.lb-lock, html.lb-lock body { overflow: hidden; }
.lb { position: fixed; inset: 0; z-index: 100; display: none; background: rgba(0, 0, 0, .95); color: #fff; user-select: none; }
.lb.open { display: block; }
.lb-top {
  position: absolute; inset: 0 0 auto 0; z-index: 3; display: flex; align-items: center; gap: 4px;
  padding: 8px 8px 8px 16px; background: linear-gradient(rgba(0, 0, 0, .6), transparent);
}
.lb-info { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.lb-info b { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-info small { color: #bbb; }
.lb-btn {
  display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px;
  border: 0; border-radius: 50%; background: none; color: #fff; font-size: 20px; cursor: pointer;
}
.lb-btn:hover { background: rgba(255, 255, 255, .12); text-decoration: none; }
.lb-btn[hidden] { display: none; }
.lb-stage { position: absolute; inset: 0; overflow: hidden; touch-action: none; }
.lb-media {
  position: absolute; inset: 0; margin: auto; max-width: 100%; max-height: 100%;
  width: auto; height: auto; object-fit: contain;
}
img.lb-media { cursor: zoom-in; }
video.lb-media { width: 100%; height: 100%; background: transparent; }
.lb-media.anim { transition: transform .25s ease; }
.lb-stage.zoomed { overflow: auto; touch-action: auto; }
.lb-stage.zoomed .lb-media { position: static; max-width: none; max-height: none; margin: 0; cursor: zoom-out; }
.lb-nav {
  position: absolute; top: 50%; z-index: 3; transform: translateY(-50%);
  width: 56px; height: 96px; border: 0; background: none; color: #fff; font-size: 44px; line-height: 1;
  opacity: .6; cursor: pointer;
}
.lb-nav:hover { opacity: 1; }
.lb-nav[hidden] { display: none; }
.lb-prev { left: 0; }
.lb-next { right: 0; }
.lb-text {
  position: absolute; inset: auto 0 0 0; z-index: 3; max-height: 30%; overflow: auto;
  padding: 28px 16px 16px; background: linear-gradient(transparent, rgba(0, 0, 0, .7));
  text-align: center; white-space: pre-wrap; pointer-events: none;
}
.lb-text[hidden] { display: none; }
@media (hover: none) { .lb-nav { display: none; } }

/* ---------- login ---------- */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card {
  width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px;
  padding: 28px 24px; border-radius: 16px; background: var(--surface); box-shadow: 0 8px 30px rgba(0, 0, 0, .15); text-align: center;
}
.auth-card h1 { margin: 0; }
.auth-card p { margin: 0; }
.auth-logo {
  align-self: center; display: flex; align-items: center; justify-content: center;
  width: 72px; height: 72px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 34px;
}
.auth-card label { display: flex; flex-direction: column; gap: 4px; text-align: left; font-size: 14px; color: var(--muted); }
.auth-card input {
  height: 44px; padding: 0 12px; font: inherit; font-size: 16px; color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); outline: none;
}
.auth-card input:focus { border-color: var(--accent); }
.auth-card .btn { height: 44px; border: 0; font: inherit; font-weight: 600; cursor: pointer; }
.auth-code { text-align: center; letter-spacing: .4em; font-size: 22px !important; }
.auth-error { padding: 8px 10px; border-radius: 8px; background: rgba(229, 57, 53, .12); color: #e53935; font-size: 14px; }
.auth-notice { padding: 8px 10px; border-radius: 8px; background: var(--reply-bg); font-size: 14px; }
.auth-alt { font-size: 14px; }
.auth-or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.auth-or::before, .auth-or::after { content: ""; flex: 1; border-top: 1px solid var(--line); }
.auth-card .btn.ghost { background: transparent; border: 1px solid var(--line); }
