/* ============ Telegram preview — visual tokens ============ */
.tg-preview-root {
  --tg-bg-color:      #E5DDD5;
  --tg-bubble-bg:     #FFFFFF;
  --tg-bubble-shadow: 0 1px 0.5px rgba(0,0,0,.13);
  --tg-text:          #000000;
  --tg-meta:          #707579;
  --tg-meta-blue:     #3390EC;
  --tg-link:          #168ACD;
  --tg-code-bg:       #F1F3F4;
  --tg-quote-bar:     #3390EC;
  --tg-spoiler-bg:    #8E8E93;
  --tg-btn-bg:        rgba(255,255,255,0.92);
  --tg-btn-text:      #3390EC;
  --tg-doc-pdf-grad:  linear-gradient(180deg, #E15050 0%, #C13434 100%);
  --tg-font-system:   -apple-system, BlinkMacSystemFont, 'SF Pro Display',
                      'Helvetica Neue', 'Segoe UI', Roboto, sans-serif;
  --tg-font-mono:     'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace;
  font-family: var(--tg-font-system);
}

.tg-chat {
  background: var(--tg-bg-color) url(/static/tg-bg-light.svg) repeat;
  background-size: 120px 120px;
  padding: 16px 14px 18px;
  min-height: 200px;
  border-radius: 12px;
  overflow: hidden;
}

.tg-date-strip-wrap { text-align: center; }
.tg-date-strip {
  display: inline-block;
  background: rgba(0,0,0,.3);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  margin: 0 auto 14px;
}

/* Avatar aligns to top of bubble — matches Telegram's actual layout where the
   round avatar sits at the message's top-left and the bubble's "tail" cutout
   (top-left small border-radius) points to it. */
.tg-row { display: flex; gap: 8px; align-items: flex-start; }
.tg-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  display: grid; place-items: center;
  background: linear-gradient(150deg, #226654, #143F33);
  color: #EBD9A8; font-weight: 700; font-size: 12px;
  flex-shrink: 0;
}
.tg-bubble {
  background: var(--tg-bubble-bg);
  box-shadow: var(--tg-bubble-shadow);
  border-radius: 6px 12px 12px 12px;
  /* Right padding has room for absolutely-positioned time pill */
  padding: 8px 12px 10px 12px;
  max-width: 480px;
  font-size: 16px;
  line-height: 1.31;
  color: var(--tg-text);
  position: relative;
  animation: tgBubbleIn 0.25s cubic-bezier(.2,.8,.2,1) both;
}
@keyframes tgBubbleIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: none; }
}
.tg-author { font-size: 13px; font-weight: 600; margin-bottom: 4px; letter-spacing: -0.005em; }
/* Time stamp lives in the bottom-right of the bubble, like real TG. */
.tg-time {
  position: absolute;
  bottom: 6px;
  right: 10px;
  font-size: 11px;
  color: var(--tg-meta);
  white-space: nowrap;
  pointer-events: none;
}
.tg-time .check { color: var(--tg-meta-blue); margin-left: 2px; font-weight: 600; }

.tg-body { white-space: pre-wrap; word-wrap: break-word; padding-bottom: 4px; }
.tg-body p + p { margin-top: 8px; }
/* Bolder weight pops against TG's lighter system font on Windows */
.tg-body b, .tg-body strong { font-weight: 800; }
/* Last line gets right-padding so the bottom-right time pill doesn't overlap text */
.tg-body > :last-child { padding-right: 56px; }
.tg-body i, .tg-body em { font-style: italic; }
.tg-body u, .tg-body ins { text-decoration: underline; text-underline-offset: 2px; }
.tg-body s, .tg-body strike, .tg-body del { text-decoration: line-through; }
.tg-body a { color: var(--tg-link); text-decoration: none; }
.tg-body a:hover { text-decoration: underline; }
.tg-body code {
  background: var(--tg-code-bg);
  padding: 1px 4px;
  border-radius: 4px;
  font-family: var(--tg-font-mono);
  font-size: 13px;
}
.tg-body blockquote {
  border-left: 3px solid var(--tg-quote-bar);
  padding: 2px 8px;
  margin: 4px 0;
  color: #5A5C66;
}

.tg-pre {
  background: var(--tg-code-bg);
  border-radius: 6px;
  margin: 6px 0;
  overflow: hidden;
}
.tg-pre-head {
  padding: 4px 8px;
  font-size: 12px;
  color: var(--tg-meta);
  cursor: pointer;
  border-bottom: 1px solid rgba(0,0,0,.04);
  user-select: none;
}
.tg-pre-head:hover { background: rgba(0,0,0,.03); }
.tg-pre-head.copied { color: #047857; }
.tg-pre-body {
  padding: 8px;
  font-family: var(--tg-font-mono);
  font-size: 13px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}

.tg-spoiler {
  background: var(--tg-spoiler-bg);
  color: transparent;
  text-shadow: 0 0 8px rgba(0,0,0,.5);
  border-radius: 2px;
  cursor: pointer;
  transition: text-shadow 0.4s, background 0.4s, color 0.4s;
}
.tg-spoiler.revealed {
  background: transparent;
  color: inherit;
  text-shadow: none;
}

/* Substituted placeholder in preview — subtle filled pill matching the editor's
   visual language. Kept readable without screaming. */
.tg-ph-pill {
  background: rgba(51, 144, 236, 0.10);
  color: var(--tg-meta-blue);
  border-radius: 3px;
  padding: 0 4px;
  font-weight: 500;
}

.tg-attach {
  display: flex;
  gap: 10px;
  padding: 6px 0 8px;
  align-items: center;
  border-bottom: 1px solid rgba(0,0,0,.04);
  margin-bottom: 6px;
}
.tg-doc-icon {
  width: 40px; height: 48px;
  background: var(--tg-doc-pdf-grad);
  border-radius: 6px;
  color: #fff; font-weight: 700; font-size: 11px;
  display: grid; place-items: center;
  flex-shrink: 0;
}
.tg-doc-meta { flex: 1; min-width: 0; }
.tg-doc-name {
  font-size: 14px; font-weight: 500;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tg-doc-sub { font-size: 12px; color: var(--tg-meta); margin-top: 2px; }

.tg-video {
  position: relative;
  aspect-ratio: 16/9;
  background: linear-gradient(135deg, #1a1a1a, #3a3a3a);
  border-radius: 8px;
  margin-bottom: 6px;
  overflow: hidden;
}
.tg-video-play {
  position: absolute; inset: 0;
  display: grid; place-items: center;
}
.tg-video-play-glyph {
  width: 44px; height: 44px;
  background: rgba(255,255,255,.95);
  color: #000; font-size: 18px;
  border-radius: 50%;
  display: grid; place-items: center;
  padding-left: 4px;
}
.tg-video-duration {
  position: absolute;
  bottom: 8px; right: 8px;
  background: rgba(0,0,0,.5);
  color: #fff;
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 4px;
}

/* Inline keyboard sits tight under the bubble (TG default ~2-4px gap) and is
   left-offset by avatar (28) + gap (8) = 36px so it aligns with the bubble. */
.tg-kb {
  margin-top: 3px;
  max-width: 480px;
  margin-left: 36px;
}
.tg-kb-btn {
  display: block;
  background: var(--tg-btn-bg);
  color: var(--tg-btn-text);
  font-weight: 500;
  padding: 9px 12px;
  border-radius: 8px;
  text-align: center;
  font-size: 14px;
  box-shadow: 0 1px 0.5px rgba(0,0,0,.13);
}
.tg-kb-btn:hover { background: #fff; }

/* ============ Editor + preview layout ============ */
.me-root {
  display: grid;
  gap: 18px;
  grid-template-columns: 1fr 380px;
  align-items: start;
}
.me-preview-wrap { position: sticky; top: 16px; }
.me-preview-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  font-size: 12.5px;
  color: var(--ink-mute);
  list-style: none;
}
.me-preview-head::-webkit-details-marker { display: none; }
.me-preview-head strong { color: var(--ink); font-weight: 600; letter-spacing: -0.005em; }
.me-preview-head .muted { color: var(--ink-fade); }
.me-preview-head label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-tint);
  padding: 3px 8px 3px 10px;
  border-radius: var(--r-pill);
  border: 1px solid var(--line-soft);
}
.me-preview-head select {
  font-size: 12px;
  padding: 2px 6px;
  width: auto;
  border: 0;
  background: transparent;
  font-weight: 600;
  color: var(--ink);
  cursor: pointer;
}

.me-toolbar {
  display: flex;
  gap: 3px;
  flex-wrap: wrap;
  align-items: center;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: var(--r-2) var(--r-2) 0 0;
  background: var(--bg-tint);
}
/* Toolbar buttons read as buttons even at rest — subtle bg + border so users
   don't mistake them for inline labels. */
.me-toolbar .tb {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  padding: 5px 10px;
  min-width: 30px;
  border-radius: var(--r-1);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-soft);
  transition: background .12s, color .12s, border-color .12s;
}
.me-toolbar .tb:hover {
  background: var(--bg-hover);
  border-color: var(--line-strong);
  color: var(--ink);
}
.me-toolbar .tb.is-active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
/* Group separator — visible but not loud */
.me-toolbar .sep {
  width: 1px;
  height: 18px;
  background: var(--line-strong);
  margin: 0 6px;
}

.me-edit {
  border: 1px solid var(--line);
  border-radius: 0 0 var(--r-2) var(--r-2);
  padding: 12px 14px;
  min-height: 180px;
  background: var(--bg-elev);
  font-size: 14px;
  line-height: 1.5;
  outline: none;
}
.me-edit:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.me-edit p { margin: 0 0 8px; }
.me-edit p:last-child { margin-bottom: 0; }
/* Placeholder atoms in the editor share the preview's visual language so the
   owner sees the same affordance in both panes. */
.me-edit .ph-token {
  background: rgba(51, 144, 236, 0.12);
  color: var(--tg-meta-blue, #3390EC);
  padding: 1px 6px;
  border-radius: 3px;
  font-weight: 500;
  font-size: 13px;
  white-space: nowrap;
  cursor: default;
}

/* Fallback when TipTap fails to load */
.me-fallback .me-toolbar { display: none; }
.me-fallback .me-edit { display: none; }
.me-fallback textarea[name] { display: block !important; }

@media (max-width: 959px) {
  .me-root { grid-template-columns: 1fr; }
  .me-preview-wrap { position: static; }
}
@media (max-width: 599px) {
  .me-preview-details > summary {
    cursor: pointer;
    padding: 10px 0;
    font-weight: 600;
    color: var(--ink);
    list-style: none;
  }
  .me-preview-details > summary::before { content: '▾ '; }
  .me-preview-details:not([open]) > summary::before { content: '▸ '; }
}
