/* TopicChat — tema adattivo Telegram, fallback scuro, accento ciano/blu */

:root {
  --bg: var(--tg-theme-bg-color, #0e1621);
  --bg-2: var(--tg-theme-secondary-bg-color, #17212b);
  --text: var(--tg-theme-text-color, #f2f6fa);
  --hint: var(--tg-theme-hint-color, #7d8b99);
  --link: var(--tg-theme-link-color, #4cc3ff);
  --accent: #21c2a7;
  --accent-2: #2394ff;
  --grad: linear-gradient(135deg, var(--accent), var(--accent-2));
  --danger: #ff5c6c;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
}

.hidden { display: none !important; }

/* ---------- boot ---------- */
.boot {
  height: 100dvh; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 10px;
}
.boot-logo { font-size: 56px; filter: drop-shadow(0 6px 24px rgba(35,148,255,.35)); }
.boot-title {
  font-size: 24px; font-weight: 800; letter-spacing: .5px;
  background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent;
}
.boot-status { color: var(--hint); font-size: 14px; text-align: center; padding: 0 32px; }

/* ---------- viste ---------- */
.view { min-height: 100dvh; display: flex; flex-direction: column; }

/* La chat DEVE avere altezza vincolata: lo scroll vive in #messages, non nel body.
   --tg-viewport-stable-height è iniettata da telegram-web-app.js (px). */
.chat-view {
  height: var(--tg-viewport-stable-height, 100dvh);
  min-height: 0;
  overflow: hidden;
  position: relative;
}

/* ---------- home ---------- */
.home-header {
  position: sticky; top: 0; z-index: 5;
  padding: 14px 16px 10px;
  background: linear-gradient(var(--bg) 75%, transparent);
  backdrop-filter: blur(8px);
}
.home-head-row { display: flex; align-items: center; justify-content: space-between; }
.app-title {
  font-size: 22px; font-weight: 800; letter-spacing: .3px;
  background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent;
}
.app-subtitle { color: var(--hint); font-size: 12.5px; margin-top: 2px; }
.me-avatar {
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--grad); color: #fff; font-weight: 700; font-size: 15px;
  display: flex; align-items: center; justify-content: center;
  background-size: cover; background-position: center;
  box-shadow: 0 4px 14px rgba(35,148,255,.3);
}
.search-wrap { margin-top: 12px; }
#search, #msg-search {
  width: 100%; border: none; outline: none;
  background: var(--bg-2); color: var(--text);
  border-radius: 12px; padding: 10px 14px; font-size: 15px;
}
#search::placeholder, #msg-search::placeholder { color: var(--hint); }
#msg-search { background: var(--bg); }

.topic-list { padding: 4px 12px 96px; display: flex; flex-direction: column; gap: 8px; }

.topic-card {
  display: flex; gap: 12px; align-items: center;
  background: var(--bg-2); border-radius: var(--radius);
  padding: 12px 14px; cursor: pointer;
  transition: transform .08s ease;
  border: 1px solid transparent;
}
.topic-card:active { transform: scale(.985); }
.topic-tile {
  width: 48px; height: 48px; min-width: 48px; border-radius: 14px;
  display: flex; align-items: center; justify-content: center; font-size: 24px;
}
.topic-main { flex: 1; min-width: 0; }
.topic-row1 { display: flex; align-items: baseline; gap: 8px; }
.topic-title {
  font-weight: 650; font-size: 15.5px; flex: 1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topic-time { color: var(--hint); font-size: 12px; white-space: nowrap; }
.topic-row2 { display: flex; align-items: center; gap: 8px; margin-top: 3px; }
.topic-preview {
  color: var(--hint); font-size: 13.5px; flex: 1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topic-preview b { color: var(--text); font-weight: 550; }
.badge {
  min-width: 20px; height: 20px; border-radius: 10px; padding: 0 6px;
  background: var(--grad); color: #fff; font-size: 12px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.topic-flags { color: var(--hint); font-size: 12px; }

.empty { text-align: center; margin-top: 12vh; padding: 0 40px; }
.empty-emoji { font-size: 52px; }
.empty-title { font-weight: 700; font-size: 17px; margin-top: 12px; }
.empty-sub { color: var(--hint); font-size: 14px; margin-top: 6px; }

.fab {
  position: fixed; right: 18px; bottom: calc(22px + var(--safe-bottom)); z-index: 6;
  width: 56px; height: 56px; border-radius: 18px; border: none;
  background: var(--grad); color: #fff; font-size: 28px; line-height: 1;
  box-shadow: 0 8px 24px rgba(35,148,255,.4); cursor: pointer;
  transition: transform .1s ease;
}
.fab:active { transform: scale(.92); }

/* ---------- chat ---------- */
.chat-header {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 8px 10px 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(125,139,153,.15);
  flex: none; z-index: 5;
}
.icon-btn {
  border: none; background: transparent; color: var(--link);
  font-size: 20px; cursor: pointer; padding: 4px 8px; border-radius: 8px;
}
.back-btn { font-size: 30px; line-height: 1; padding: 0 6px 4px; }
.chat-emoji {
  width: 38px; height: 38px; min-width: 38px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center; font-size: 20px;
}
.chat-head-main { flex: 1; min-width: 0; cursor: pointer; margin-left: 4px; }
.chat-title {
  font-weight: 700; font-size: 16px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-sub { color: var(--hint); font-size: 12.5px; margin-top: 1px; }
.chat-sub.typing { color: var(--accent); }
.chat-sub.typing::after {
  content: ''; display: inline-block; width: 1.1em; text-align: left;
  animation: dots 1.2s steps(4) infinite;
}
@keyframes dots {
  0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; }
}

/* barra messaggio fissato */
.pinned-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 14px;
  background: color-mix(in srgb, var(--bg-2) 80%, transparent);
  border-bottom: 1px solid rgba(125,139,153,.15);
  cursor: pointer; flex: none;
}
.pinned-icon { font-size: 15px; }
.pinned-body { flex: 1; min-width: 0; border-left: 2px solid var(--accent-2); padding-left: 8px; }
.pinned-label { color: var(--link); font-size: 11.5px; font-weight: 700; }
.pinned-text {
  color: var(--hint); font-size: 12.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.messages {
  flex: 1 1 0; min-height: 0; overflow-y: auto;
  padding: 14px 12px 8px;
  display: flex; flex-direction: column; gap: 3px;
  -webkit-overflow-scrolling: touch;
}
.msg-day {
  align-self: center; margin: 10px 0 6px;
  background: rgba(125,139,153,.18); color: var(--hint);
  font-size: 12px; padding: 3px 12px; border-radius: 12px;
}
.msg-row { display: flex; gap: 8px; align-items: flex-end; max-width: 88%; }
.msg-row.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg-row.theirs { align-self: flex-start; }
.msg-avatar {
  width: 30px; height: 30px; min-width: 30px; border-radius: 50%;
  color: #fff; font-size: 12px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  background-size: cover; background-position: center;
  margin-bottom: 2px;
}
.msg-row.same .msg-avatar { visibility: hidden; }
.bubble {
  border-radius: 16px; padding: 7px 12px 6px;
  font-size: 15px; line-height: 1.35;
  word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap;
  position: relative; cursor: pointer;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.msg-row.theirs .bubble { background: var(--bg-2); border-bottom-left-radius: 6px; }
.msg-row.mine .bubble {
  background: linear-gradient(135deg, rgba(33,194,167,.28), rgba(35,148,255,.30));
  border: 1px solid rgba(35,148,255,.25);
  border-bottom-right-radius: 6px;
}
.bubble-author { font-size: 12.5px; font-weight: 700; margin-bottom: 2px; }
.bubble-fwd { color: var(--link); font-size: 12.5px; font-weight: 600; margin-bottom: 2px; }
.bubble-reply {
  border-left: 3px solid var(--accent-2);
  background: rgba(35,148,255,.10);
  border-radius: 6px; padding: 4px 8px; margin: 2px 0 5px;
  font-size: 12.5px; cursor: pointer;
}
.bubble-reply-author { font-weight: 700; color: var(--link); }
.bubble-reply-text { color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
.bubble-meta {
  float: right; margin: 6px -4px -2px 8px;
  color: var(--hint); font-size: 11px; white-space: nowrap;
}
.msg-row.mine .bubble-meta { color: rgba(242,246,250,.65); }
.bubble .mention { color: var(--link); font-weight: 600; }
.bubble a { color: var(--link); }

.msg-photo {
  display: block; max-width: min(62vw, 320px); max-height: 340px;
  border-radius: 10px; margin: 2px 0 4px; cursor: zoom-in;
}
.msg-row.same .bubble { border-top-left-radius: 16px; }

/* reazioni */
.reaction-pills { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 5px; }
.reaction-pill {
  display: inline-flex; align-items: center; gap: 4px;
  border: 1px solid rgba(125,139,153,.25);
  background: rgba(125,139,153,.12);
  color: var(--text); border-radius: 999px;
  padding: 2px 8px; font-size: 13px; cursor: pointer;
}
.reaction-pill.mine {
  background: rgba(35,148,255,.22); border-color: var(--accent-2);
}
.reaction-pill b { font-size: 12px; font-weight: 700; }

/* sondaggi */
.poll { min-width: min(58vw, 280px); }
.poll-q { font-weight: 700; margin-bottom: 2px; }
.poll-sub { color: var(--hint); font-size: 12px; margin-bottom: 8px; }
.poll-opt {
  position: relative; border: 1px solid rgba(125,139,153,.3);
  border-radius: 10px; padding: 8px 10px; margin-top: 6px;
  cursor: pointer; overflow: hidden; font-size: 14px;
}
.poll-opt .poll-fill {
  position: absolute; inset: 0; width: 0%;
  background: rgba(35,148,255,.18); transition: width .25s ease;
}
.poll-opt.voted { border-color: var(--accent-2); }
.poll-opt-row { position: relative; display: flex; justify-content: space-between; gap: 8px; }
.poll-pct { color: var(--hint); font-size: 12.5px; font-weight: 700; }
.poll-footer { color: var(--hint); font-size: 12px; margin-top: 8px; display: flex; justify-content: space-between; }
.poll-retract { color: var(--link); cursor: pointer; font-weight: 600; }

/* highlight jump-to-message */
@keyframes flashbg {
  0%, 40% { background: rgba(35,148,255,.28); }
  100% { background: transparent; }
}
.msg-row.flash { border-radius: 16px; animation: flashbg 1.6s ease forwards; }

.join-banner {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  margin: 0 12px 8px; padding: 10px 14px;
  background: rgba(35,148,255,.12); border: 1px solid rgba(35,148,255,.25);
  border-radius: 12px; font-size: 13.5px; flex: none;
}
.pill-btn {
  border: none; border-radius: 999px; padding: 7px 16px;
  background: var(--grad); color: #fff; font-weight: 700; font-size: 13.5px; cursor: pointer;
}
.closed-banner {
  margin: 0 12px 8px; padding: 10px 14px; text-align: center;
  background: rgba(125,139,153,.12); color: var(--hint);
  border-radius: 12px; font-size: 13.5px; flex: none;
}

.reply-bar {
  display: flex; align-items: center; gap: 8px;
  margin: 0 12px; padding: 8px 12px;
  background: var(--bg-2); border-radius: 12px 12px 0 0;
  border-left: 3px solid var(--accent-2); flex: none;
}
.edit-bar { border-left-color: var(--accent); }
.reply-bar-body { flex: 1; min-width: 0; }
.reply-bar-author { color: var(--link); font-size: 12.5px; font-weight: 700; }
.reply-bar-text {
  color: var(--hint); font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* autocomplete menzioni */
.mention-panel {
  margin: 0 12px; background: var(--bg-2);
  border: 1px solid rgba(125,139,153,.2);
  border-radius: 12px 12px 0 0; overflow: hidden; flex: none;
  max-height: 190px; overflow-y: auto;
}
.mention-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; cursor: pointer;
  border-bottom: 1px solid rgba(125,139,153,.1);
}
.mention-item:active { background: rgba(35,148,255,.12); }
.mention-item .msg-avatar { margin: 0; }
.mention-name { font-size: 14.5px; font-weight: 600; }
.mention-username { color: var(--hint); font-size: 12.5px; }

.composer {
  display: flex; align-items: flex-end; gap: 6px;
  padding: 8px 10px calc(10px + var(--safe-bottom));
  background: var(--bg);
  border-top: 1px solid rgba(125,139,153,.12);
  flex: none;
}
.attach-btn { font-size: 21px; padding: 8px 6px; }
#input {
  flex: 1; resize: none; border: none; outline: none;
  background: var(--bg-2); color: var(--text);
  border-radius: 18px; padding: 10px 14px; font-size: 15px;
  font-family: inherit; line-height: 1.35; max-height: 120px;
}
#input::placeholder { color: var(--hint); }
.send-btn {
  width: 42px; height: 42px; min-width: 42px; border-radius: 50%; border: none;
  background: var(--grad); color: #fff; font-size: 17px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: transform .1s ease, opacity .15s;
}
.send-btn:disabled { opacity: .4; }
.send-btn:active { transform: scale(.9); }

/* bottone torna in fondo */
.scroll-down {
  position: absolute; right: 14px; bottom: 86px; z-index: 6;
  width: 42px; height: 42px; border-radius: 50%; border: none;
  background: var(--bg-2); color: var(--link); font-size: 20px;
  box-shadow: 0 4px 16px rgba(0,0,0,.35); cursor: pointer;
}
.sd-badge {
  position: absolute; top: -6px; right: -4px;
  min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px;
  background: var(--grad); color: #fff; font-size: 11px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}

/* ---------- modali / sheet ---------- */
.modal {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(0,0,0,.5);
  display: flex; align-items: flex-end;
  animation: fadeIn .15s ease;
}
@keyframes fadeIn { from { opacity: 0; } }
.sheet {
  width: 100%; background: var(--bg-2);
  border-radius: 20px 20px 0 0;
  padding: 10px 18px calc(24px + var(--safe-bottom));
  animation: slideUp .2s ease;
  max-height: 88dvh; overflow-y: auto;
}
.sheet-tall { min-height: 55dvh; }
@keyframes slideUp { from { transform: translateY(40px); opacity: .6; } }
.sheet-handle {
  width: 40px; height: 4px; border-radius: 2px;
  background: rgba(125,139,153,.4); margin: 4px auto 14px;
}
.sheet-title { font-size: 18px; font-weight: 800; margin-bottom: 14px; }
.sheet-subtitle { font-size: 14px; font-weight: 700; color: var(--hint); margin: 16px 0 6px; }

/* riga quick reactions in cima al context menu */
.react-row {
  display: flex; justify-content: space-between; gap: 4px;
  padding: 2px 2px 12px; border-bottom: 1px solid rgba(125,139,153,.12);
  margin-bottom: 4px;
}
.react-opt {
  width: 44px; height: 44px; border-radius: 50%; border: none;
  background: var(--bg); font-size: 22px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: transform .1s ease;
}
.react-opt:active { transform: scale(1.2); }
.react-opt.mine { background: rgba(35,148,255,.22); box-shadow: 0 0 0 2px var(--accent-2); }

.emoji-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.emoji-opt {
  width: 44px; height: 44px; border-radius: 12px; border: 2px solid transparent;
  background: var(--bg); font-size: 22px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.emoji-opt.sel { border-color: var(--accent-2); background: rgba(35,148,255,.12); }

.field { display: block; margin-bottom: 12px; }
.field span { display: block; color: var(--hint); font-size: 12.5px; margin-bottom: 5px; }
.field input {
  width: 100%; border: none; outline: none;
  background: var(--bg); color: var(--text);
  border-radius: 10px; padding: 11px 13px; font-size: 15px;
}
.primary-btn {
  width: 100%; border: none; border-radius: 12px;
  background: var(--grad); color: #fff;
  font-size: 16px; font-weight: 700; padding: 13px; margin-top: 6px; cursor: pointer;
}
.primary-btn:disabled { opacity: .5; }
.ghost-btn {
  width: 100%; border: 1px dashed rgba(125,139,153,.4); border-radius: 12px;
  background: transparent; color: var(--link);
  font-size: 14px; font-weight: 600; padding: 10px; margin-top: 10px; cursor: pointer;
}

.sheet-item {
  display: block; width: 100%; text-align: left;
  background: transparent; border: none; color: var(--text);
  font-size: 15.5px; padding: 13px 6px; cursor: pointer;
  border-bottom: 1px solid rgba(125,139,153,.12);
}
.sheet-item:last-child { border-bottom: none; }
.sheet-item.danger { color: var(--danger); }
.sheet-topic-head {
  display: flex; gap: 12px; align-items: center;
  padding: 4px 6px 14px; border-bottom: 1px solid rgba(125,139,153,.12);
  margin-bottom: 4px;
}
.sheet-topic-head > span { font-size: 32px; }
.sheet-topic-title { font-weight: 700; font-size: 16px; }
.sheet-topic-sub { color: var(--hint); font-size: 12.5px; margin-top: 2px; }

/* membri */
.members-list { display: flex; flex-direction: column; }
.member-row {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 4px; border-bottom: 1px solid rgba(125,139,153,.1);
  cursor: pointer;
}
.member-row .msg-avatar { width: 38px; height: 38px; min-width: 38px; font-size: 14px; }
.member-main { flex: 1; min-width: 0; }
.member-name { font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.member-sub { color: var(--hint); font-size: 12.5px; }
.member-sub.online { color: var(--accent); }
.role-chip {
  font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px;
  color: var(--link); border: 1px solid rgba(76,195,255,.4);
  padding: 1px 6px; border-radius: 6px;
}
.online-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--accent);
  display: inline-block;
}

/* inoltra */
.forward-list { display: flex; flex-direction: column; }
.forward-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 4px; border-bottom: 1px solid rgba(125,139,153,.1); cursor: pointer;
}
.forward-row .topic-tile { width: 40px; height: 40px; min-width: 40px; font-size: 20px; border-radius: 12px; }
.forward-title { font-size: 15px; font-weight: 600; }
.forward-sub { color: var(--hint); font-size: 12.5px; }

/* ricerca */
.search-results { margin-top: 12px; display: flex; flex-direction: column; }
.search-hit {
  padding: 10px 4px; border-bottom: 1px solid rgba(125,139,153,.1); cursor: pointer;
}
.search-hit-head { display: flex; justify-content: space-between; gap: 8px; }
.search-hit-author { font-weight: 700; font-size: 13.5px; }
.search-hit-time { color: var(--hint); font-size: 12px; }
.search-hit-text {
  color: var(--hint); font-size: 13.5px; margin-top: 2px;
  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}
.search-hit-text mark { background: rgba(35,148,255,.35); color: var(--text); border-radius: 3px; }
.search-empty { color: var(--hint); text-align: center; padding: 24px 0; font-size: 14px; }

/* anteprima foto */
.photo-preview {
  display: block; max-width: 100%; max-height: 42dvh;
  border-radius: 12px; margin: 0 auto 14px;
}

/* opzioni sondaggio in creazione */
.poll-opt-field { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.poll-opt-field input {
  flex: 1; border: none; outline: none; background: var(--bg); color: var(--text);
  border-radius: 10px; padding: 10px 13px; font-size: 15px;
}
.poll-opt-del {
  border: none; background: transparent; color: var(--hint); font-size: 17px; cursor: pointer;
}

/* lightbox */
.lightbox {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0,0,0,.92);
  display: flex; align-items: center; justify-content: center;
  animation: fadeIn .15s ease;
}
.lightbox img { max-width: 100vw; max-height: 100dvh; object-fit: contain; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%);
  background: rgba(20,28,38,.95); color: #fff; font-size: 14px;
  padding: 10px 18px; border-radius: 12px; z-index: 50;
  box-shadow: 0 8px 24px rgba(0,0,0,.4);
  animation: fadeIn .15s ease;
  max-width: 86vw; text-align: center;
}
