/* The feed looks like the feed of our Telegram iOS fork: each post is a channel message bubble on
   the chat background. Every colour comes from Telegram's theme (--tg-theme-*, set by
   telegram-web-app.js); the fallbacks only matter outside Telegram and follow the system scheme. */

:root {
  --fb-bg: #ffffff;
  --fb-secondary: #efeff4;
  --fb-text: #000000;
  --fb-hint: #8e8e93;
  --fb-link: #007aff;
  --fb-button: #007aff;
  --fb-button-text: #ffffff;
  --fb-separator: #c8c7cc;
  --fb-destructive: #ff3b30;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-tg]) {
    --fb-bg: #000000;
    --fb-secondary: #1c1c1d;
    --fb-text: #ffffff;
    --fb-hint: #98989e;
    --fb-link: #3e88f7;
    --fb-button: #3e88f7;
    --fb-separator: #3d3d40;
    --fb-destructive: #eb5545;
  }
}

:root {
  --tg-bg: var(--tg-theme-bg-color, var(--fb-bg));
  --tg-secondary: var(--tg-theme-secondary-bg-color, var(--fb-secondary));
  /* Light: grey chat background, white bubbles. Dark: black background, dark grey bubbles (Telegram iOS). */
  --page: var(--tg-secondary);
  --bubble: var(--tg-bg);
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --link: var(--tg-theme-link-color, var(--fb-link));
  --accent: var(--tg-theme-accent-text-color, var(--link));
  --button: var(--tg-theme-button-color, var(--fb-button));
  --button-text: var(--tg-theme-button-text-color, var(--fb-button-text));
  --separator: color-mix(in srgb, var(--hint) 30%, transparent);
  --destructive: var(--tg-theme-destructive-text-color, var(--fb-destructive));
  --tint: color-mix(in srgb, var(--hint) 16%, transparent);
  --link-tint: color-mix(in srgb, var(--link) 14%, transparent);

  --safe-top: calc(var(--tg-safe-area-inset-top, env(safe-area-inset-top, 0px)) + var(--tg-content-safe-area-inset-top, 0px));
  --safe-bottom: calc(var(--tg-safe-area-inset-bottom, env(safe-area-inset-bottom, 0px)) + var(--tg-content-safe-area-inset-bottom, 0px));
  --safe-left: var(--tg-safe-area-inset-left, env(safe-area-inset-left, 0px));
  --safe-right: var(--tg-safe-area-inset-right, env(safe-area-inset-right, 0px));

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, Roboto, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, "Roboto Mono", Consolas, monospace;
  --radius: 18px;
  color-scheme: light;
}

:root[data-scheme="dark"] {
  --page: var(--tg-bg);
  --bubble: var(--tg-secondary);
  --fb-bg: #000000;
  --fb-secondary: #1c1c1d;
  --fb-text: #ffffff;
  --fb-hint: #98989e;
  --fb-link: #3e88f7;
  --fb-button: #3e88f7;
  --fb-destructive: #eb5545;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--page);
  color: var(--text);
  font: 17px/1.3 var(--font);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

body { min-height: var(--tg-viewport-height, 100vh); }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
  text-align: inherit;
}

a { color: var(--link); text-decoration: none; }

[hidden] { display: none !important; }

/* MARK: the title bar «Для вас» */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  padding: calc(var(--safe-top) + 10px) 56px 14px;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
  line-height: 22px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: linear-gradient(var(--page) 62%, color-mix(in srgb, var(--page) 0%, transparent));
  pointer-events: none;
}

.screen {
  padding: 0 calc(var(--safe-right) + 8px) calc(var(--safe-bottom) + 16px) calc(var(--safe-left) + 8px);
  max-width: 640px;
  margin: 0 auto;
}

/* MARK: post bubble */

.post {
  position: relative;
  background: var(--bubble);
  border-radius: var(--radius);
  margin-bottom: 12px;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  transition: transform 0.15s ease-out;
}

.post.pressed { transform: scale(0.985); }

.post-head {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px 0;
  min-height: 52px;
  text-align: left;
}

.post-head + .body { padding-top: 6px; }
.post-head + .media { margin-top: 10px; }

.avatar {
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  letter-spacing: 0.2px;
  user-select: none;
}

.avatar { position: relative; overflow: hidden; }
.avatar img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 0.2s ease;
}
.avatar img.loaded { opacity: 1; }

.avatar.small { width: 34px; height: 34px; font-size: 13px; }
.avatar.tiny { width: 30px; height: 30px; font-size: 12px; }
.avatar.anon { background: color-mix(in srgb, var(--hint) 45%, var(--bubble)); color: #fff; }
.avatar.anon svg { width: 18px; height: 18px; }

.who { min-width: 0; display: flex; flex-direction: column; }

.name {
  font-weight: 600;
  font-size: 16px;
  line-height: 20px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.name .verified { display: inline-block; vertical-align: -2px; width: 16px; height: 16px; margin-left: 3px; color: var(--button); }
.name .verified svg { width: 16px; height: 16px; display: block; }

.handle {
  color: var(--hint);
  font-size: 15px;
  line-height: 19px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.body {
  position: relative;
  padding: 8px 12px 6px;
}

.text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  line-height: 22px;
}

.text b { font-weight: 600; }

.more { color: var(--link); }

/* Time and views: bottom right inside the bubble; an invisible copy at the end of the last line keeps
   the room, so they share the line when it fits and drop below when it does not (as in Telegram). */
.meta {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  color: var(--hint);
  font-size: 13px;
  line-height: 16px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.meta svg { width: 16px; height: 16px; }
.meta.ghost { visibility: hidden; margin-left: 8px; vertical-align: -2px; }
.meta.live { position: absolute; right: 12px; bottom: 7px; }

.media .meta.live {
  right: 8px;
  bottom: 8px;
  padding: 1px 6px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
}

.mono { font-family: var(--mono); font-size: 15px; }
code.mono { background: var(--tint); border-radius: 4px; padding: 0 3px; }

pre.mono {
  margin: 4px 0;
  padding: 8px 10px;
  background: var(--tint);
  border-left: 3px solid var(--link);
  border-radius: 6px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

blockquote {
  margin: 4px 0;
  padding: 2px 8px 3px 10px;
  border-left: 3px solid var(--link);
  border-radius: 4px;
  background: var(--link-tint);
}

.tag { color: var(--link); }

.spoiler {
  border-radius: 4px;
  color: transparent;
  background-color: var(--tint);
  background-image: radial-gradient(circle, color-mix(in srgb, var(--text) 50%, transparent) 0.6px, transparent 0.9px);
  background-size: 4px 4px;
  cursor: pointer;
}

.spoiler * { color: transparent !important; }
.spoiler.shown { color: inherit; background: none; cursor: auto; }
.spoiler.shown * { color: inherit !important; }
.spoiler.shown a { color: var(--link) !important; }

/* MARK: media */

.media { position: relative; }

.pic {
  display: block;
  width: 100%;
  position: relative;
  overflow: hidden;
  background: var(--tint);
}

.pic img, .pic video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  opacity: 0;
  transition: opacity 0.2s;
}

.pic img.loaded, .pic video { opacity: 1; }

.album { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; }
.album .pic { aspect-ratio: 1; }
.album .pic.wide { grid-column: 1 / -1; aspect-ratio: 2; }

.album-more {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.4);
  color: #fff;
  font-size: 24px;
  font-weight: 600;
}

.badge {
  position: absolute;
  left: 8px;
  top: 8px;
  padding: 1px 6px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-size: 12px;
  line-height: 18px;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.play {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 48px;
  height: 48px;
  margin: -24px 0 0 -24px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.45);
  display: grid;
  place-items: center;
  color: #fff;
  pointer-events: none;
}

.play svg { width: 22px; height: 22px; margin-left: 3px; }

.link-preview {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 5px 10px 7px;
  border-left: 3px solid var(--link);
  border-radius: 6px;
  background: var(--link-tint);
  color: var(--text);
  text-align: left;
}

.link-preview .site { color: var(--link); font-weight: 600; font-size: 15px; line-height: 19px; }
.link-preview .title { font-weight: 600; font-size: 15px; line-height: 19px; }
.link-preview .desc { font-size: 15px; line-height: 19px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.link-preview .pic { margin-top: 6px; border-radius: 6px; }

.poll-q { font-weight: 600; }
.poll-kind { color: var(--hint); font-size: 14px; margin: 1px 0 4px; }
.poll-opt { position: relative; margin: 6px 0; padding-bottom: 7px; font-size: 16px; }
.poll-opt .row { display: flex; gap: 10px; }
.poll-opt .pct { font-weight: 600; width: 38px; flex: none; text-align: right; font-size: 14px; }
.poll-opt .bar { position: absolute; left: 48px; bottom: 0; height: 3px; border-radius: 2px; background: var(--link); min-width: 3px; }
.poll-total { color: var(--hint); font-size: 14px; }

/* MARK: reactions */

.reactions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
}

/* Telegram's reaction button (ReactionButtonListComponent): a 30pt capsule, 11pt sides, a 20pt
   emoji, the counter in medium weight. Unchosen: the accent at 10 % (light) or white at 7 % (dark)
   with the accent / white counter; chosen: filled with the accent, a white counter. */
.reaction {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 30px;
  padding: 0 11px 0 8px;
  border-radius: 15px;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.reaction .emoji { width: 22px; font-size: 18px; line-height: 20px; text-align: center; }
span.reaction { cursor: default; }
:root[data-scheme="dark"] .reaction { background: rgba(255, 255, 255, 0.07); color: var(--text); }
.reaction.chosen, :root[data-scheme="dark"] .reaction.chosen { background: var(--button); color: var(--button-text); }
.reaction.pop .emoji { animation: pop 0.3s ease-out; }

@keyframes pop { 50% { transform: scale(1.35); } }

.burst {
  position: absolute;
  left: 50%;
  top: 50%;
  font-size: 64px;
  margin: -40px 0 0 -32px;
  pointer-events: none;
  animation: burst 0.7s ease-out forwards;
}

@keyframes burst {
  0% { transform: scale(0.3); opacity: 0; }
  30% { transform: scale(1.1); opacity: 1; }
  100% { transform: scale(1.4) translateY(-20px); opacity: 0; }
}

/* MARK: the comments bar */

.comments-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 44px;
  padding: 6px 10px 6px 12px;
  border-top: 0.5px solid var(--separator);
  color: var(--link);
  font-size: 17px;
  text-align: left;
}

.comments-bar .icon { width: 30px; height: 30px; display: grid; place-items: center; }
/* the latest commenters' faces, overlapping, each ringed with the bubble colour */
.comments-bar .faces { display: flex; flex: none; padding-left: 2px; }
.comments-bar .faces .avatar.tiny {
  width: 26px;
  height: 26px;
  font-size: 10px;
  box-shadow: 0 0 0 2px var(--bubble);
}
.comments-bar .faces .avatar + .avatar { margin-left: -8px; }
.comments-bar .icon svg { width: 26px; height: 26px; }
.comments-bar .label { flex: 1; }
.comments-bar .chev { width: 20px; height: 20px; }
.comments-bar:active { background: var(--tint); }

.hidden-post {
  background: var(--bubble);
  border-radius: var(--radius);
  margin-bottom: 12px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--hint);
  font-size: 15px;
}

.hidden-post .undo { margin-left: auto; color: var(--link); font-weight: 500; }

/* MARK: states */

.state {
  min-height: calc(var(--tg-viewport-stable-height, 100vh) - var(--safe-top) - var(--safe-bottom) - 110px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px 20px;
}

.state-card {
  background: var(--bubble);
  border-radius: var(--radius);
  padding: 22px 20px 18px;
  max-width: 320px;
}

.state-icon { color: var(--button); margin-bottom: 10px; }
.state-icon svg { width: 52px; height: 52px; }
.state-title { font-size: 17px; line-height: 22px; font-weight: 600; margin: 0 0 6px; }
.state-text { font-size: 15px; line-height: 20px; color: var(--hint); margin: 0; }
.state-text b { color: var(--text); font-weight: 500; }

.button {
  margin-top: 16px;
  height: 42px;
  padding: 0 22px;
  border-radius: 10px;
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
  font-size: 16px;
  text-align: center;
}

.button.plain { background: none; color: var(--link); font-weight: 500; }

/* Telegram's service message: a small pill on the chat background. */
.service {
  display: table;
  margin: 4px auto 12px;
  padding: 3px 10px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--hint) 22%, transparent);
  color: var(--text);
  font-size: 14px;
  line-height: 18px;
  font-weight: 500;
}

.footer-note { min-height: 40px; padding: 4px 0; }

.spinner {
  width: 22px;
  height: 22px;
  margin: 10px auto;
  border-radius: 50%;
  border: 2px solid var(--tint);
  border-top-color: var(--hint);
  animation: spin 0.8s linear infinite;
}

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

.skeleton .avatar, .skeleton .bone {
  background: color-mix(in srgb, var(--hint) 22%, transparent);
  animation: pulse 1.2s ease-in-out infinite alternate;
}

.skeleton .bone { height: 12px; border-radius: 6px; }
.skeleton .body { display: flex; flex-direction: column; gap: 8px; padding-bottom: 16px; }

@keyframes pulse { to { opacity: 0.45; } }

/* MARK: comments (a thread of message bubbles, replies on a branch line) */

.thread-list { padding: 4px 0 8px; }

.sort-row { display: flex; justify-content: space-between; align-items: center; margin: 0 0 10px; }
.chip {
  padding: 4px 12px;
  border-radius: 14px;
  background: color-mix(in srgb, var(--hint) 22%, transparent);
  font-size: 15px;
  line-height: 20px;
  font-weight: 500;
}
.chip-link { color: var(--text); }
.chip-link:active, .discuss:active, .more-replies:active { opacity: 0.6; }

/* A comment as an incoming message of Telegram iOS in a thread: the avatar (34pt, 26pt for a
   reply) at the bubble's bottom, the bubble 17pt round, the author's name in their colour. */
.c-row { position: relative; display: flex; align-items: flex-end; gap: 8px; margin-bottom: 8px; padding-left: 2px; }
.c-row .avatar.small { width: 34px; height: 34px; font-size: 13px; }
.c-row.reply { padding-left: 44px; }
.c-row.reply .avatar.small { width: 26px; height: 26px; font-size: 10px; }
.c-row.reply .avatar.anon svg { width: 14px; height: 14px; }

.c-bubble {
  min-width: 120px;
  /* Telegram's widest incoming message in a thread: ~306pt of the fork plus our wider web text, the rest stays chat background. */
  max-width: min(320px, calc(100% - 44px));
  background: var(--bubble);
  border-radius: 17px;
  padding: 6px 11px 6px 11px;
  position: relative;
}
.c-bubble.flash { animation: flash 1.6s ease-out; }
@keyframes flash {
  0%, 35% { background: color-mix(in srgb, var(--accent) 28%, var(--bubble)); }
  100% { background: var(--bubble); }
}

.c-name { font-size: 15px; font-weight: 600; line-height: 20px; display: flex; gap: 6px; align-items: baseline; }
.c-name .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-name .role { color: var(--hint); font-weight: 400; font-size: 14px; }
.c-text { font-size: 17px; line-height: 22px; white-space: pre-wrap; overflow-wrap: anywhere; }
.c-row.folded .c-text { color: var(--hint); }

/* The last line: the reactions, the time pushed to the right. Without reactions the time sits at
   the end of the text's last line (floated, as Telegram leaves room for it). */
.c-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 4px; min-height: 20px; }
.c-foot .reaction { margin: 1px 0; }
.c-total { color: var(--hint); font-size: 13px; }
.c-time { margin-left: auto; padding-left: 10px; color: var(--hint); font-size: 12px; line-height: 20px; font-variant-numeric: tabular-nums; }
.c-time.inline { float: right; position: relative; top: 5px; margin-left: 0; }
.c-text::after { content: ""; display: table; clear: both; }

/* Telegram's reply header: a bar and a tint in the quoted author's colour (currentColor), their
   name, one line of the text. A tap scrolls to the comment (or opens the post) and flashes it. */
.c-quote {
  position: relative;
  display: block;
  margin: 3px 0 5px;
  padding: 3px 9px 4px 11px;
  border-radius: 6px;
  background: color-mix(in srgb, currentColor 13%, transparent);
  overflow: hidden;
  cursor: pointer;
  transition: background 0.15s;
}
.c-quote::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: currentColor; }
.c-quote:active { background: color-mix(in srgb, currentColor 26%, transparent); }
.c-quote .q-name { display: block; font-size: 15px; font-weight: 600; line-height: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.c-quote .q-link { display: inline-block; max-width: 100%; vertical-align: top; }
.c-quote .q-link:active { opacity: 0.6; }
.c-quote .q-text { display: block; color: var(--text); font-size: 15px; line-height: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* the branch: a line down from the parent's avatar, an elbow into each reply's avatar */
.branch { position: relative; --branch: color-mix(in srgb, var(--hint) 38%, var(--page)); }
.branch::before {
  content: "";
  position: absolute;
  left: 18px;
  top: -8px;
  bottom: 34px;
  border-left: 2px solid var(--branch);
}
.branch .c-row.reply::before {
  content: "";
  position: absolute;
  left: 18px;
  bottom: 12px;
  width: 24px;
  height: 22px;
  border-left: 2px solid var(--branch);
  border-bottom: 2px solid var(--branch);
  border-bottom-left-radius: 12px;
}

/* «Ещё N ответов», «Все комментарии в Telegram»: Telegram's service pills on the chat background. */
.more-replies, .discuss, .folded-toggle {
  display: table;
  padding: 4px 12px;
  border-radius: 14px;
  background: color-mix(in srgb, var(--hint) 22%, transparent);
  color: var(--text);
  font-size: 15px;
  line-height: 20px;
  font-weight: 500;
}
.more-replies { margin: -2px 0 10px 44px; }
.discuss { margin: 6px auto 12px; }
.folded-toggle { margin: 4px auto 12px; font-size: 14px; font-weight: 400; }

/* MARK: viewer, toast */

.viewer {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: #000;
  display: grid;
  place-items: center;
}

.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 16px);
  transform: translateX(-50%);
  z-index: 20;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  font-size: 15px;
  line-height: 20px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
