:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f0f2f5;
  --bg-chat: #e8ecf0;
  --bg-bubble: #ffffff;
  --bg-bubble-own: #effdde;
  --bg-header: #517da2;
  --bg-input: #ffffff;
  --text-primary: #000000;
  --text-secondary: #707579;
  --text-header: #ffffff;
  --text-link: #3390ec;
  --text-time: #8c9fa6;
  --text-name: #3390ec;
  --border: #e0e0e0;
  --shadow: 0 1px 2px rgba(0,0,0,.08);
  --radius-bubble: 12px;
  --radius-card: 10px;
  --radius-btn: 8px;
  --font: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", "Segoe UI", sans-serif;
  --max-width: 520px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

[data-theme="dark"] {
  --bg-primary: #212121;
  --bg-secondary: #181818;
  --bg-chat: #0e1621;
  --bg-bubble: #182533;
  --bg-bubble-own: #2b5278;
  --bg-header: #242f3d;
  --bg-input: #242f3d;
  --text-primary: #f5f5f5;
  --text-secondary: #8b9bab;
  --text-header: #f5f5f5;
  --text-link: #6ab2f2;
  --text-time: #6b8594;
  --text-name: #6ab2f2;
  --border: #2b3641;
  --shadow: 0 1px 2px rgba(0,0,0,.3);
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.4;
  color: var(--text-primary);
  background: var(--bg-chat);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* ========== App Shell ========== */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: var(--max-width);
  margin: 0 auto;
  background: var(--bg-chat);
}

/* ========== Header ========== */
.tg-header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--bg-header);
  color: var(--text-header);
  min-height: 56px;
}

.tg-header .back-btn {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
  cursor: pointer;
  fill: var(--text-header);
}

.tg-header .channel-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #5ba0d0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 600;
  color: #fff;
  flex-shrink: 0;
}

.tg-header .channel-info {
  flex: 1;
  min-width: 0;
}

.tg-header .channel-info h1 {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tg-header .channel-info .subtitle {
  font-size: 13px;
  opacity: .75;
}

.tg-header .theme-toggle {
  width: 24px;
  height: 24px;
  cursor: pointer;
  fill: var(--text-header);
  flex-shrink: 0;
}

/* ========== Chat Area ========== */
.chat-area {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px 8px calc(8px + var(--safe-bottom));
  -webkit-overflow-scrolling: touch;
}

/* ========== Date Separator ========== */
.date-sep {
  text-align: center;
  padding: 8px 0;
}

.date-sep span {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 16px;
  background: rgba(0,0,0,.15);
  color: #fff;
  font-size: 13px;
  font-weight: 500;
}

[data-theme="dark"] .date-sep span {
  background: rgba(255,255,255,.1);
}

/* ========== Message Bubble ========== */
.msg {
  display: flex;
  gap: 6px;
  margin-bottom: 4px;
  max-width: 85%;
  align-items: flex-end;
}

.msg .avatar-sm {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #5ba0d0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  flex-shrink: 0;
  align-self: flex-end;
}

.msg .bubble {
  background: var(--bg-bubble);
  border-radius: var(--radius-bubble);
  padding: 6px 10px 4px;
  box-shadow: var(--shadow);
  min-width: 60px;
  position: relative;
  word-break: break-word;
}

.msg .bubble .sender {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-name);
  margin-bottom: 2px;
}

.msg .bubble .text {
  font-size: 15px;
  line-height: 1.4;
  white-space: pre-wrap;
}

.msg .bubble .time {
  display: flex;
  justify-content: flex-end;
  font-size: 11px;
  color: var(--text-time);
  margin-top: 2px;
}

/* ========== Media in Bubble ========== */
.msg .bubble .media-wrap {
  margin: 4px -10px 4px -10px;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
}

.msg .bubble .media-wrap:first-child {
  margin-top: -6px;
  border-radius: var(--radius-bubble) var(--radius-bubble) 0 0;
}

.msg .bubble .media-wrap img,
.msg .bubble .media-wrap video {
  display: block;
  width: 100%;
  max-height: 320px;
  object-fit: cover;
  background: var(--bg-secondary);
}

.msg .bubble .media-wrap .play-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,.25);
  cursor: pointer;
}

.msg .bubble .media-wrap .play-icon {
  width: 48px;
  height: 48px;
  fill: #fff;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,.3));
}

/* ========== Reply ========== */
.msg .bubble .reply-bar {
  border-left: 2px solid var(--text-link);
  padding: 2px 8px;
  margin-bottom: 4px;
  font-size: 13px;
  color: var(--text-secondary);
  background: rgba(0,0,0,.03);
  border-radius: 4px;
}

[data-theme="dark"] .msg .bubble .reply-bar {
  background: rgba(255,255,255,.05);
}

/* ========== Landing / Auth Pages ========== */
.page-landing,
.page-auth,
.page-pricing {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 24px;
  background: var(--bg-primary);
  text-align: center;
}

.page-landing .logo {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: var(--bg-header);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

.page-landing .logo svg { fill: #fff; width: 40px; height: 40px; }

.page-landing h1 {
  font-size: 24px;
  margin-bottom: 8px;
}

.page-landing .desc {
  font-size: 15px;
  color: var(--text-secondary);
  margin-bottom: 32px;
  max-width: 300px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 24px;
  border-radius: var(--radius-btn);
  border: none;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: opacity .15s;
}

.btn:active { opacity: .7; }

.btn-primary {
  background: #3390ec;
  color: #fff;
}

.btn-secondary {
  background: transparent;
  color: var(--text-link);
}

.btn-block {
  width: 100%;
}

/* ========== Auth Form ========== */
.auth-form {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.auth-form h1 {
  font-size: 22px;
  text-align: center;
  margin-bottom: 8px;
}

.input-field {
  width: 100%;
  min-height: 44px;
  padding: 0 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-btn);
  font-size: 16px;
  background: var(--bg-input);
  color: var(--text-primary);
  outline: none;
  transition: border-color .2s;
}

.input-field:focus {
  border-color: #3390ec;
}

.auth-form .error-msg {
  font-size: 13px;
  color: #e53935;
  text-align: center;
  min-height: 18px;
}

/* ========== Pricing ========== */
.pricing-card {
  background: var(--bg-primary);
  border-radius: var(--radius-card);
  padding: 32px 24px;
  box-shadow: var(--shadow);
  text-align: center;
  max-width: 320px;
  width: 100%;
}

.pricing-card .price {
  font-size: 36px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 16px 0 8px;
}

.pricing-card .period {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 24px;
}

.pricing-card .features {
  list-style: none;
  text-align: left;
  margin-bottom: 24px;
}

.pricing-card .features li {
  padding: 6px 0;
  font-size: 14px;
  color: var(--text-secondary);
}

.pricing-card .features li::before {
  content: "\2713 ";
  color: #4caf50;
  font-weight: 700;
}

/* ========== Member Area ========== */
.member-nav {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  overflow-x: auto;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
}

.member-nav .day-chip {
  flex-shrink: 0;
  padding: 6px 14px;
  border-radius: 16px;
  font-size: 13px;
  background: var(--bg-secondary);
  color: var(--text-secondary);
  text-decoration: none;
  white-space: nowrap;
}

.member-nav .day-chip.active {
  background: #3390ec;
  color: #fff;
}

/* ========== Loading / Empty ========== */
.loading-spinner {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 0;
}

.loading-spinner::after {
  content: "";
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: #3390ec;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

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

.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-secondary);
  font-size: 14px;
}

/* ========== Video Player Overlay ========== */
.video-player-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-player-overlay video {
  width: 100%;
  max-height: 100%;
}

.video-player-overlay .close-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,.2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  fill: #fff;
}

/* ========== Image Viewer ========== */
.image-viewer {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0,0,0,.92);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}

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

/* ========== Scroll to Bottom ========== */
.scroll-bottom {
  position: fixed;
  bottom: calc(16px + var(--safe-bottom));
  right: 16px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg-primary);
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  fill: var(--text-secondary);
  z-index: 50;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}

.scroll-bottom.visible {
  opacity: 1;
  pointer-events: auto;
}

/* ========== Utilities ========== */
.hidden { display: none !important; }
