/* === Telegram Light Theme === */
:root {
  --tg-blue: #0088cc;
  --tg-bg: #ffffff;
  --tg-text: #000000;
  --tg-secondary: #707579;
  --tg-bubble-out: #e3ffc4;
  --tg-bubble-in: #ffffff;
  --tg-header: #ffffff;
  --tg-border: #e0e0e0;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

/* === Cross-browser base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html {
  height: 100%;
  height: -webkit-fill-available;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, sans-serif;
  background: #dfe4e8;
  color: var(--tg-text);
  display: flex;
  justify-content: center;
  min-height: 100vh;
  min-height: -webkit-fill-available;
  overscroll-behavior: none;
}

/* === Page Layout: 3 columns === */
.page-layout {
  display: flex;
  align-items: stretch;
  width: 100%;
  max-width: 1200px;
  height: 100vh;
  height: 100dvh;
  padding: 1rem;
  gap: 1rem;
}

/* === Left Sidebar: Social Links === */
.sidebar-left {
  width: 168px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: .6rem;
  padding-top: 4.5rem;
}

.social-btn {
  display: flex;
  align-items: center;
  gap: .55rem;
  padding: .65rem .7rem;
  background: #fff;
  border: 1px solid var(--tg-border);
  border-radius: 10px;
  text-decoration: none;
  color: var(--tg-text);
  font-size: .76rem;
  font-weight: 600;
  line-height: 1.3;
  box-shadow: 0 1px 3px rgba(0,0,0,.07);
  -webkit-transition: background .15s, color .15s, border-color .15s;
  transition: background .15s, color .15s, border-color .15s;
  min-height: 44px;
}
.social-btn svg { width: 22px; height: 22px; flex-shrink: 0; fill: var(--tg-blue); }
.social-logo { width: 22px; height: 22px; flex-shrink: 0; object-fit: contain; border-radius: 4px; }
.social-btn:hover { background: var(--tg-blue); color: #fff; border-color: var(--tg-blue); }
.social-btn:hover svg { fill: #fff; }
.social-btn:active { opacity: .8; }

/* === Center: Telegram Window === */
.tg-window {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--tg-bg);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 24px rgba(0,0,0,.13);
  min-width: 0;
}

.tg-window-header {
  height: 56px;
  background: var(--tg-header);
  border-bottom: 1px solid var(--tg-border);
  display: flex;
  align-items: center;
  padding: 0 1rem;
  flex-shrink: 0;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
.tg-channel-info { display: flex; align-items: center; gap: .75rem; flex: 1; min-width: 0; }
.tg-avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--tg-blue);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: 1.1rem;
  flex-shrink: 0;
}
.tg-channel-name {
  font-weight: 600; font-size: 1rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tg-channel-subs { font-size: .8rem; color: var(--tg-secondary); }

/* === Feed === */
.feed-wrapper {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 1rem 1rem 1rem 1.5rem;
  background-image: var(--chat-bg-url, none);
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  background-repeat: no-repeat;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
/* iOS Safari: fixed background не поддерживается */
@supports (-webkit-touch-callout: none) {
  .feed-wrapper { background-attachment: scroll; }
}

/* === Post Card (Telegram bubble style) === */
.post-card {
  background: #fff;
  border-radius: 14px 14px 14px 3px;
  box-shadow: 0 1px 3px rgba(0,0,0,.13);
  width: 100%;
  max-width: 580px;
  margin-bottom: .75rem;
  position: relative;
}

/* Bubble tail at bottom-left */
.post-card::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: -10px;
  width: 12px;
  height: 18px;
  background: #fff;
  -webkit-clip-path: polygon(100% 0%, 100% 100%, 0% 100%);
  clip-path: polygon(100% 0%, 100% 100%, 0% 100%);
}

.post-body { padding: .6rem .9rem .5rem; }
.post-text {
  font-size: .97rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  margin-bottom: .25rem;
}
.post-meta { text-align: right; margin-top: .1rem; }
.post-time { font-size: .73rem; color: var(--tg-secondary); }
.post-edited { font-size: .78rem; color: var(--tg-secondary); }

/* === Post Media === */
.post-media {
  width: 100%;
  overflow: hidden;
  border-radius: 14px 14px 0 0;
}
.post-media img { width: 100%; display: block; max-height: 420px; object-fit: cover; }
.post-media video { width: 100%; display: block; max-height: 420px; background: #000; }
.album-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; }
.album-grid img, .album-grid video { width: 100%; height: 200px; object-fit: cover; display: block; }

/* === Global Input Bar (bottom of center) === */
.global-input-bar {
  display: flex;
  gap: .5rem;
  align-items: center;
  padding: .75rem 1rem;
  padding-bottom: calc(.75rem + var(--safe-bottom));
  border-top: 1px solid var(--tg-border);
  background: #fff;
  flex-shrink: 0;
}
.global-input {
  flex: 1;
  border: 1px solid var(--tg-border);
  border-radius: 22px;
  padding: .6rem 1.1rem;
  font-size: .95rem;
  font-family: inherit;
  outline: none;
  -webkit-transition: border .2s;
  transition: border .2s;
  background: #f5f7f9;
  -webkit-appearance: none;
  appearance: none;
}
.global-input:focus { border-color: var(--tg-blue); background: #fff; }
.global-send-btn {
  background: var(--tg-blue);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 44px; height: 44px;
  cursor: pointer;
  font-size: 1.2rem;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  -webkit-transition: background .15s;
  transition: background .15s;
  -webkit-appearance: none;
  touch-action: manipulation;
}
.global-send-btn:hover { background: #006fa8; }
.global-send-btn:active { background: #005c8a; }

/* === Right Sidebar: Menu === */
.sidebar-right {
  width: 180px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  padding-top: 1rem;
  align-items: flex-end;
}

/* На десктопе меню фиксировано у правого края экрана */
@media (min-width: 641px) {
  .sidebar-right {
    position: fixed;
    top: 1rem;
    right: 1rem;
    width: auto;
    padding-top: 0;
    z-index: 300;
  }
  /* Резервируем место для чат-виджета справа (320px + 1.5rem отступ + запас) */
  .page-layout {
    padding-right: 155px;
  }
}

/* === Menu Container === */
.menu-container { position: relative; }

.menu-btn {
  background: none;
  border: 2px solid var(--tg-blue);
  color: var(--tg-blue);
  font-weight: 700;
  font-size: .9rem;
  padding: .8rem;
  border-radius: 10px;
  cursor: pointer;
  letter-spacing: .06em;
  white-space: nowrap;
  width: auto;
  -webkit-transition: background .15s, color .15s;
  transition: background .15s, color .15s;
  font-family: inherit;
  touch-action: manipulation;
  min-height: 44px;
}
.menu-btn:hover { background: var(--tg-blue); color: #fff; }
.menu-btn:active { opacity: .85; }

/* === Menu Dropdown === */
.menu-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 180px;
  background: #fff;
  border: 1px solid var(--tg-border);
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0,0,0,.15);
  z-index: 300;
  overflow: hidden;
  opacity: 0;
  -webkit-transform: translateY(-6px);
  transform: translateY(-6px);
  pointer-events: none;
  -webkit-transition: opacity .2s, -webkit-transform .2s;
  transition: opacity .2s, transform .2s;
}
.menu-dropdown.open {
  opacity: 1;
  -webkit-transform: translateY(0);
  transform: translateY(0);
  pointer-events: auto;
}
.menu-dropdown ul { list-style: none; }
.menu-dropdown li a {
  display: flex;
  align-items: center;
  padding: .9rem 1.2rem;
  min-height: 44px;
  color: var(--tg-text);
  text-decoration: none;
  font-weight: 600;
  font-size: .9rem;
  border-bottom: 1px solid var(--tg-border);
  -webkit-transition: background .15s, color .15s;
  transition: background .15s, color .15s;
}
.menu-dropdown li:last-child a { border-bottom: none; }
.menu-dropdown li a:hover { background: #f5f5f5; color: var(--tg-blue); }
.menu-dropdown li a:active { background: #eeeeee; }

/* === Quick Chat FAB === */
.quick-chat-fab {
  position: fixed;
  bottom: calc(1.5rem + var(--safe-bottom));
  right: calc(1.5rem + var(--safe-right));
  width: 56px; height: 56px;
  background: var(--tg-blue);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,136,204,.4);
  z-index: 200;
  border: none;
  -webkit-transition: -webkit-transform .2s;
  transition: transform .2s;
  touch-action: manipulation;
  -webkit-appearance: none;
}
.quick-chat-fab:hover { -webkit-transform: scale(1.1); transform: scale(1.1); }
.quick-chat-fab:active { -webkit-transform: scale(.93); transform: scale(.93); }
.quick-chat-fab svg { width: 28px; height: 28px; fill: #fff; }

/* === Quick Chat Window === */
.quick-chat-window {
  position: fixed;
  bottom: calc(6rem + var(--safe-bottom));
  right: calc(1.5rem + var(--safe-right));
  width: 320px;
  max-height: 460px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 8px 30px rgba(0,0,0,.15);
  z-index: 200;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-transform: scale(0);
  transform: scale(0);
  -webkit-transform-origin: calc(100% - 28px) calc(100% + 44px);
  transform-origin: calc(100% - 28px) calc(100% + 44px);
  -webkit-transition: -webkit-transform .35s cubic-bezier(.34,1.56,.64,1), border-radius .3s ease;
  transition: transform .35s cubic-bezier(.34,1.56,.64,1), border-radius .3s ease;
}
.quick-chat-window.open {
  -webkit-transform: scale(1);
  transform: scale(1);
  border-radius: 16px;
}

.qc-header {
  background: var(--tg-blue);
  color: #fff;
  padding: .9rem 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  flex-shrink: 0;
}
.qc-close {
  background: none; border: none; color: #fff;
  font-size: 1.3rem; cursor: pointer; line-height: 1;
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  touch-action: manipulation;
}
.qc-messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: .75rem;
  display: flex;
  flex-direction: column;
  gap: .4rem;
  background: #f0f2f5;
}
.qc-input-row {
  display: flex; gap: .4rem; padding: .6rem;
  border-top: 1px solid var(--tg-border);
  background: #fff;
  flex-shrink: 0;
}
.qc-input {
  flex: 1; border: 1px solid var(--tg-border);
  border-radius: 20px; padding: .45rem .9rem;
  font-size: .9rem; font-family: inherit;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
.qc-input:focus { border-color: var(--tg-blue); }
.qc-send {
  background: var(--tg-blue); color: #fff; border: none;
  border-radius: 50%; width: 36px; height: 36px;
  cursor: pointer; font-size: 1rem;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  touch-action: manipulation;
  -webkit-appearance: none;
}
.qc-send:active { opacity: .8; }

/* === Chat Bubbles === */
.chat-bubble {
  padding: .5rem .85rem;
  border-radius: 14px;
  max-width: 80%;
  font-size: .9rem;
  line-height: 1.4;
  word-break: break-word;
}
.bubble-visitor { background: var(--tg-bubble-out); align-self: flex-end; }
.bubble-admin {
  background: var(--tg-bubble-in);
  border: 1px solid var(--tg-border);
  align-self: flex-start;
}
.bubble-time { font-size: .72rem; color: var(--tg-secondary); margin-top: 2px; }

/* === Contact Popup === */
.contact-popup {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,.45);
  z-index: 400;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  opacity: 0;
  pointer-events: none;
  -webkit-transition: opacity .2s;
  transition: opacity .2s;
}
.contact-popup.open { opacity: 1; pointer-events: auto; }
.contact-popup-card {
  background: #fff;
  border-radius: 18px;
  padding: 2rem 1.75rem 1.75rem;
  width: 320px;
  max-width: 100%;
  text-align: center;
  position: relative;
  box-shadow: 0 10px 40px rgba(0,0,0,.2);
}
.contact-popup-close {
  position: absolute;
  top: .8rem; right: .9rem;
  background: none; border: none;
  font-size: 1.6rem; cursor: pointer;
  color: var(--tg-secondary);
  line-height: 1;
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  -webkit-transition: color .15s;
  transition: color .15s;
  touch-action: manipulation;
}
.contact-popup-close:hover { color: var(--tg-text); }
.contact-popup-icon {
  width: 60px; height: 60px;
  background: var(--tg-blue);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto 1rem;
}
.contact-popup-icon svg { width: 30px; height: 30px; fill: #fff; }
.contact-popup-title { font-size: 1.1rem; font-weight: 700; margin-bottom: .5rem; line-height: 1.4; }
.contact-popup-sub { font-size: .85rem; color: var(--tg-secondary); margin-bottom: 1.25rem; }
.contact-popup-btns {
  display: -webkit-box;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  flex-direction: column;
  gap: .55rem;
}
.contact-popup-btn {
  display: -webkit-box;
  display: flex;
  -webkit-box-align: center;
  align-items: center;
  gap: .65rem;
  color: #fff;
  text-decoration: none;
  padding: .75rem 1.1rem;
  border-radius: 12px;
  font-weight: 600;
  font-size: .95rem;
  min-height: 48px;
  -webkit-transition: opacity .15s;
  transition: opacity .15s;
  touch-action: manipulation;
}
.contact-popup-btn:hover { opacity: .85; }
.contact-popup-btn:active { opacity: .7; }
.contact-popup-btn--tg  { background: #229ED9; }
.contact-popup-btn--vk  { background: #4C75A3; }
.contact-popup-btn--max { background: #0088cc; }
.contact-popup-logo { width: 22px; height: 22px; object-fit: contain; border-radius: 4px; flex-shrink: 0; }

/* === Menu separator + social links === */
.menu-sep { height: 1px; background: rgba(255,255,255,.15); margin: .3rem 0; list-style: none; }
.menu-social {
  display: -webkit-box;
  display: flex;
  -webkit-box-align: center;
  align-items: center;
  gap: .55rem;
  font-size: .8rem !important;
  padding: .45rem .7rem !important;
  opacity: .9;
}
.menu-social:hover { opacity: 1 !important; }
.menu-social-logo { width: 18px; height: 18px; object-fit: contain; border-radius: 3px; flex-shrink: 0; }

/* === Lightbox === */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.88);
  z-index: 999;
  display: -webkit-box;
  display: flex;
  -webkit-box-align: center;
  align-items: center;
  -webkit-box-pack: center;
  justify-content: center;
  cursor: zoom-out;
  touch-action: manipulation;
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
}
.lightbox.hidden { display: none; }
.lightbox img {
  max-width: 95vw;
  max-height: 92vh;
  object-fit: contain;
  border-radius: 6px;
  pointer-events: none;
  box-shadow: 0 4px 40px rgba(0,0,0,.5);
}

/* post images clickable */
.post-media img,
.album-grid img { cursor: zoom-in; touch-action: manipulation; }

/* === TG link inside post === */
.post-tg-link {
  color: var(--tg-blue);
  text-decoration: none;
  font-weight: 600;
}
.post-tg-link:hover { text-decoration: underline; }

/* === Loading === */
.loading { text-align: center; padding: 2rem; color: #fff; width: 100%; }
#scroll-sentinel { height: 1px; width: 100%; }

/* === Webkit scrollbar (Chrome, Safari, Edge) === */
.feed-wrapper::-webkit-scrollbar,
.qc-messages::-webkit-scrollbar { width: 4px; }
.feed-wrapper::-webkit-scrollbar-track,
.qc-messages::-webkit-scrollbar-track { background: transparent; }
.feed-wrapper::-webkit-scrollbar-thumb,
.qc-messages::-webkit-scrollbar-thumb { background: rgba(0,0,0,.2); border-radius: 2px; }

/* === Responsive: Tablet (≤ 960px) === */
@media (max-width: 960px) {
  .sidebar-left { display: none; }
  .sidebar-right { width: 150px; }
}

/* === Responsive: Mobile (≤ 640px) === */
@media (max-width: 640px) {
  .page-layout {
    padding-top: calc(10vh + var(--safe-top));
    padding-bottom: calc(10vh + var(--safe-bottom));
    padding-left: calc(7vw + var(--safe-left));
    padding-right: calc(7vw + var(--safe-right));
    gap: 0;
  }
  .sidebar-left { display: none; }
  .tg-window { border-radius: 12px; }

  /* Menu button — плавает поверх окна, в правом верхнем углу */
  .sidebar-right {
    display: block;
    position: fixed;
    top: calc(var(--safe-top) + 10px);
    right: calc(var(--safe-right) + 10px);
    width: auto;
    padding: 0;
    z-index: 500;
  }
  .menu-btn {
    padding: .4rem .75rem;
    font-size: .78rem;
    border-radius: 8px;
  }
  .menu-dropdown {
    right: 0;
    width: calc(100vw - 2rem);
    max-width: 300px;
    max-height: calc(100vh - 80px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Header: место под кнопку меню */
  .tg-window-header { padding-right: 75px; }

  /* Input bar — компактнее на узком окне */
  .global-input-bar {
    padding: .55rem .65rem;
    padding-bottom: calc(.55rem + var(--safe-bottom));
    gap: .4rem;
  }
  .global-send-btn { width: 36px; height: 36px; font-size: 1rem; }

  /* Prevent iOS zoom on input focus */
  .global-input,
  .qc-input { font-size: 16px; }

  /* Feed padding */
  .feed-wrapper { padding: .75rem .75rem .75rem 1rem; }

  /* Post cards full-width */
  .post-card { max-width: 100%; }

  /* Album smaller cells */
  .album-grid img,
  .album-grid video { height: 140px; }

  /* FAB — левый верхний угол */
  .quick-chat-fab {
    top: calc(var(--safe-top) + 10px);
    left: calc(var(--safe-left) + 10px);
    bottom: auto;
    right: auto;
    width: 44px; height: 44px;
  }
  .quick-chat-fab svg { width: 20px; height: 20px; }

  /* Чат-виджет — открывается из левого верхнего угла */
  .quick-chat-window {
    top: calc(var(--safe-top) + 62px);
    left: calc(var(--safe-left) + 7vw);
    bottom: auto;
    right: auto;
    width: 86vw;
    max-height: 55vh;
    max-height: 55dvh;
    -webkit-transform-origin: top left;
    transform-origin: top left;
  }
}

/* === Very narrow screens (≤ 380px) === */
@media (max-width: 380px) {
  .tg-channel-name { font-size: .85rem; }
  .post-body { padding: .4rem .6rem .35rem; }
  .post-text { font-size: .8rem; }
}

@media (max-width: 640px) {
  .post-text { font-size: .88rem; line-height: 1.5; }
  .post-time { font-size: .68rem; }
  .post-body { padding: .55rem .8rem .45rem; }
  .tg-channel-name { font-size: .93rem; }
  .tg-channel-subs { font-size: .76rem; }
}
