:root {
  color-scheme: light dark;
  --tg-theme-bg-color: #ffffff;
  --tg-theme-secondary-bg-color: #efeff4;
  --tg-theme-text-color: #000000;
  --tg-theme-hint-color: #6d6d72;
  --tg-theme-link-color: #2481cc;
  --tg-theme-button-color: #2481cc;
  --tg-theme-button-text-color: #ffffff;
  --tg-theme-section-separator-color: #dcdce0;
  --tg-theme-destructive-text-color: #d63b30;
  --skel-color: #e1e1e6;
  --tg-tint: color-mix(in srgb, var(--tg-theme-link-color) 14%, transparent);
  --tg-dtint: color-mix(in srgb, var(--tg-theme-destructive-text-color) 14%, transparent);
  --safe-bottom: var(--tg-safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
  --safe-top: var(--tg-content-safe-area-inset-top, env(safe-area-inset-top, 0px));
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme-applied]) {
    --tg-theme-bg-color: #17212b;
    --tg-theme-secondary-bg-color: #0e1621;
    --tg-theme-text-color: #f5f5f5;
    --tg-theme-hint-color: #8595a6;
    --tg-theme-link-color: #6ab3f3;
    --tg-theme-button-color: #5288c1;
    --tg-theme-button-text-color: #ffffff;
    --tg-theme-section-separator-color: #0a1118;
    --tg-theme-destructive-text-color: #ef6b6b;
    --skel-color: #1f2c3a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, sans-serif;
  -webkit-font-smoothing: antialiased;
  background: var(--tg-theme-secondary-bg-color);
  color: var(--tg-theme-text-color);
  overflow: hidden;
}

.hidden { display: none !important; }

.screen {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: var(--app-height, 100dvh);
  background: var(--tg-theme-secondary-bg-color);
  overflow: hidden;
  padding-top: var(--safe-top);
}

/* ---------- skeletons ---------- */
@keyframes skel { 0%, 100% { opacity: 1 } 50% { opacity: .55 } }
.sk { animation: skel 1.2s ease-in-out infinite; }
.sk-block { background: var(--skel-color); border-radius: 6px; flex-shrink: 0; }

/* ---------- generic centered state (empty / error) ---------- */
.state-center {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 0 32px;
  text-align: center;
}
.state-icon {
  width: 88px; height: 88px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.state-icon.info { background: var(--tg-tint); color: var(--tg-theme-link-color); }
.state-icon.error { width: 80px; height: 80px; background: var(--tg-dtint); color: var(--tg-theme-destructive-text-color); }
.state-title { margin: 4px 0 0; font-size: 21px; font-weight: 600; }
.state-text { margin: 0; font-size: 15px; line-height: 21px; color: var(--tg-theme-hint-color); max-width: 300px; }
.chip-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 10px; }
.chip {
  height: 36px; padding: 0 14px; border: none; border-radius: 18px;
  background: var(--tg-theme-bg-color); color: var(--tg-theme-link-color);
  font-family: inherit; font-size: 14px; font-weight: 500; cursor: pointer;
}
.chip:active { opacity: 0.7; }
.retry-btn {
  margin-top: 12px; height: 44px; padding: 0 28px; border: none; border-radius: 10px;
  background: var(--tg-theme-button-color); color: var(--tg-theme-button-text-color);
  font-family: inherit; font-size: 16px; font-weight: 600; cursor: pointer;
}
.error-meta { font-size: 12px; color: var(--tg-theme-hint-color); }

/* ---------- list screen ---------- */
.list-content {
  flex-grow: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 0 16px; max-width: 640px; margin: 0 auto; width: 100%;
}
.section-label {
  padding: 20px 14px 8px;
  font-size: 13px; letter-spacing: 0.02em; text-transform: uppercase;
  color: var(--tg-theme-hint-color);
}
.ticket-group {
  display: flex; flex-direction: column;
  background: var(--tg-theme-bg-color); border-radius: 12px; overflow: hidden;
}
.ticket-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; min-height: 72px;
  text-decoration: none; color: var(--tg-theme-text-color);
  border-top: 1px solid var(--tg-theme-section-separator-color);
  background: none; border-left: none; border-right: none; border-bottom: none;
  width: 100%; text-align: left; cursor: pointer; font: inherit;
}
.ticket-row:first-child { border-top: none; }
.ticket-row:active { opacity: 0.7; }
.ticket-avatar {
  width: 44px; height: 44px; flex-shrink: 0; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.ticket-avatar.open { background: var(--tg-tint); color: var(--tg-theme-link-color); }
.ticket-avatar.closed { background: var(--tg-theme-secondary-bg-color); color: var(--tg-theme-hint-color); }
.ticket-main { flex-grow: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.ticket-title-row { display: flex; align-items: center; gap: 8px; }
.ticket-title-row .title { font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { flex-shrink: 0; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 10px; }
.badge.open { background: var(--tg-tint); color: var(--tg-theme-link-color); }
.badge.closed { background: var(--tg-theme-secondary-bg-color); color: var(--tg-theme-hint-color); }
.ticket-sub { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--tg-theme-hint-color); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ticket-sub .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; background: transparent; }
.ticket-sub.answered { color: var(--tg-theme-link-color); font-weight: 500; }
.ticket-sub.answered .dot { background: var(--tg-theme-link-color); }
.ticket-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; flex-shrink: 0; color: var(--tg-theme-hint-color); }
.ticket-meta .date { font-size: 13px; }

/* ---------- composer (shared) ---------- */
.composer-wrap {
  flex-shrink: 0; background: var(--tg-theme-bg-color);
  border-top: 1px solid var(--tg-theme-section-separator-color);
  padding-bottom: max(8px, var(--safe-bottom));
}
.composer-hint { max-width: 640px; margin: 0 auto; font-size: 12px; color: var(--tg-theme-hint-color); padding: 8px 18px 4px; }
.composer { max-width: 640px; margin: 0 auto; display: flex; align-items: flex-end; gap: 6px; padding: 8px 8px 2px; }
.composer textarea {
  flex-grow: 1; max-height: 120px; min-height: 44px; box-sizing: border-box;
  resize: none; border: none; outline: none; border-radius: 22px;
  padding: 11px 16px; background: var(--tg-theme-secondary-bg-color); color: var(--tg-theme-text-color);
  font-family: inherit; font-size: 16px; line-height: 22px;
}
.icon-btn {
  width: 44px; height: 44px; flex-shrink: 0; border: none; padding: 0; background: transparent;
  color: var(--tg-theme-hint-color); display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.icon-btn:active { opacity: 0.6; }
.send-btn {
  width: 44px; height: 44px; flex-shrink: 0; border: none; padding: 0; border-radius: 50%;
  background: var(--tg-theme-secondary-bg-color); color: var(--tg-theme-hint-color);
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.send-btn.active { background: var(--tg-theme-button-color); color: var(--tg-theme-button-text-color); }
.send-btn:active { opacity: 0.85; }

/* ---------- ticket (chat) subheader ---------- */
.ticket-subheader {
  height: 64px; flex-shrink: 0; box-sizing: border-box;
  display: flex; align-items: center; gap: 12px; padding: 0 12px 0 16px;
  background: var(--tg-theme-bg-color); border-bottom: 1px solid var(--tg-theme-section-separator-color);
}
.ticket-subheader-info { flex-grow: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.ticket-subheader h1 { margin: 0; font-size: 17px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ticket-status-line { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--tg-theme-hint-color); }
.ticket-status-line .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.ticket-status-line.open .status-text { color: var(--tg-theme-link-color); font-weight: 600; }
.ticket-status-line.open .dot { background: var(--tg-theme-link-color); }
.ticket-status-line.closed .status-text { font-weight: 600; }
.ticket-status-line svg { flex-shrink: 0; }
.close-chip {
  height: 36px; display: flex; align-items: center; padding: 0 14px; border: none; flex-shrink: 0;
  border-radius: 18px; background: var(--tg-tint); color: var(--tg-theme-link-color);
  font-family: inherit; font-size: 14px; font-weight: 600; cursor: pointer;
}
.close-chip:active { opacity: 0.7; }

/* ---------- messages ---------- */
.messages {
  flex-grow: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; gap: 6px;
  padding: 12px 10px; max-width: 640px; margin: 0 auto; width: 100%;
}
.day-divider {
  align-self: center; margin: 6px 0; padding: 3px 10px; border-radius: 12px;
  background: var(--tg-theme-bg-color); color: var(--tg-theme-hint-color);
  font-size: 13px; font-weight: 500;
}
.bubble { max-width: min(78%, 480px); box-sizing: border-box; font-size: 16px; line-height: 21px; }
.bubble.text { padding: 7px 12px 5px; }
.bubble.mine { align-self: flex-end; border-radius: 18px 18px 6px 18px; background: var(--tg-theme-button-color); color: var(--tg-theme-button-text-color); }
.bubble.theirs { align-self: flex-start; border-radius: 18px 18px 18px 6px; background: var(--tg-theme-bg-color); color: var(--tg-theme-text-color); }
.bubble .sender-label { font-size: 13px; font-weight: 600; color: var(--tg-theme-link-color); margin-bottom: 2px; }
.bubble-time-row { display: flex; justify-content: flex-end; align-items: center; gap: 4px; margin-top: 2px; font-size: 11px; }
.bubble.mine .bubble-time-row { opacity: 0.8; }
.bubble.theirs .bubble-time-row { color: var(--tg-theme-hint-color); }
.bubble.sending { opacity: 0.75; }

.error-row { align-self: flex-end; display: flex; align-items: flex-end; gap: 6px; }
.error-row .bubble { opacity: 0.6; }
.retry-circle {
  width: 32px; height: 32px; flex-shrink: 0; border: none; padding: 0; border-radius: 50%;
  background: var(--tg-theme-destructive-text-color); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.error-stack { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.error-caption { font-size: 12px; font-weight: 500; color: var(--tg-theme-destructive-text-color); }

/* media bubbles */
.bubble.media { padding: 4px; }
.bubble.media.mine { align-self: flex-end; border-radius: 18px 18px 6px 18px; background: var(--tg-theme-button-color); color: var(--tg-theme-button-text-color); }
.bubble.media.theirs { align-self: flex-start; border-radius: 18px 18px 18px 6px; background: var(--tg-theme-bg-color); color: var(--tg-theme-text-color); }
.media-photo { display: block; width: 244px; max-width: 100%; border-radius: 14px; object-fit: cover; cursor: zoom-in; background: var(--skel-color); min-height: 120px; }
.media-caption { padding: 6px 8px 2px; font-size: 16px; line-height: 21px; }
.media-time { display: flex; justify-content: flex-end; padding: 0 8px 2px; font-size: 11px; }
.bubble.media.mine .media-time { opacity: 0.8; }
.bubble.media.theirs .media-time { color: var(--tg-theme-hint-color); }

.doc-row { display: flex; align-items: center; gap: 12px; padding: 10px 12px 0; width: 240px; max-width: 100%; }
.doc-icon { width: 44px; height: 44px; flex-shrink: 0; border-radius: 50%; display: flex; align-items: center; justify-content: center; }
.bubble.media.mine .doc-icon { background: var(--tg-theme-button-text-color); color: var(--tg-theme-button-color); }
.bubble.media.theirs .doc-icon { background: var(--tg-theme-button-color); color: var(--tg-theme-button-text-color); }
.doc-info { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.doc-info .name { font-size: 15px; font-weight: 600; }
.doc-info .sub { font-size: 14px; opacity: 0.85; }
.doc-footer { display: flex; align-items: center; justify-content: space-between; padding: 6px 12px 5px; }
.doc-download {
  display: flex; align-items: center; gap: 6px; min-height: 32px;
  font-size: 14px; font-weight: 600; text-decoration: none;
  background: none; border: none; font-family: inherit; cursor: pointer; padding: 0;
}
.bubble.media.theirs .doc-download { color: var(--tg-theme-link-color); }
.bubble.media.mine .doc-download { color: var(--tg-theme-button-text-color); }

.video-wrap { position: relative; width: 244px; max-width: 100%; height: 142px; border-radius: 14px; overflow: hidden; background: #2a2f36; }
.video-wrap video { display: block; width: 100%; height: 100%; object-fit: cover; }
.video-play {
  position: absolute; inset: 0; margin: auto; width: 52px; height: 52px; border: none; padding: 0;
  border-radius: 50%; background: rgba(0,0,0,0.5); color: #fff;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.video-caption { padding: 6px 8px 2px; font-size: 16px; line-height: 21px; }

.voice-row { display: flex; align-items: center; gap: 10px; padding: 8px 12px 5px 8px; width: 236px; max-width: 100%; }
.voice-play {
  width: 40px; height: 40px; flex-shrink: 0; border: none; padding: 0; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.bubble.media.mine .voice-play { background: var(--tg-theme-button-text-color); color: var(--tg-theme-button-color); }
.bubble.media.theirs .voice-play { background: var(--tg-theme-button-color); color: var(--tg-theme-button-text-color); }
.voice-info { flex-grow: 1; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.voice-bar { height: 4px; border-radius: 2px; background: rgba(127,127,127,0.35); overflow: hidden; }
.voice-bar-fill { height: 100%; width: 0%; background: currentColor; }
.voice-meta { display: flex; justify-content: space-between; font-size: 12px; opacity: 0.85; }

.sticker-wrap { align-self: flex-end; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.sticker-wrap.theirs { align-self: flex-start; align-items: flex-start; }
.sticker-img { width: 120px; height: 120px; object-fit: contain; }
.sticker-fallback {
  width: 120px; height: 120px; border-radius: 16px; background: var(--tg-theme-bg-color); color: var(--tg-theme-hint-color);
  display: flex; align-items: center; justify-content: center; font-size: 13px; text-align: center; padding: 8px;
}
.sticker-time { padding: 1px 8px; border-radius: 10px; background: var(--tg-theme-bg-color); color: var(--tg-theme-hint-color); font-size: 11px; }

/* upload-in-progress bubble */
.upload-progress-wrap { position: relative; width: 244px; max-width: 100%; height: 160px; border-radius: 14px; overflow: hidden; background: #1f2630; }
.upload-progress-overlay { position: absolute; inset: 0; background: rgba(0,0,0,0.35); display: flex; align-items: center; justify-content: center; }
.upload-cancel {
  position: relative; width: 52px; height: 52px; border: none; padding: 0; border-radius: 50%;
  background: rgba(0,0,0,0.45); color: #fff; display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.upload-ring { position: absolute; inset: 0; transform: rotate(-90deg); }
.upload-meta { display: flex; justify-content: space-between; padding: 5px 8px 2px; font-size: 12px; opacity: 0.9; }

/* ---------- hints / system line ---------- */
.hint-line {
  flex-shrink: 0; display: flex; align-items: center; gap: 8px; max-width: 640px; margin: 0 auto; width: 100%;
  padding: 10px 18px 4px; background: var(--tg-theme-bg-color);
  font-size: 13px; line-height: 18px; color: var(--tg-theme-hint-color);
}
.hint-line svg { flex-shrink: 0; }

.system-line {
  align-self: center; display: flex; align-items: center; gap: 6px; max-width: 320px;
  margin: 10px 0 4px; padding: 5px 12px; border-radius: 12px;
  background: var(--tg-theme-bg-color); color: var(--tg-theme-hint-color);
  font-size: 13px; line-height: 18px; text-align: center;
}
.system-line svg { flex-shrink: 0; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-bottom)); transform: translateX(-50%);
  display: flex; align-items: center; gap: 8px; padding: 9px 16px; border-radius: 20px;
  background: var(--tg-theme-text-color); color: var(--tg-theme-bg-color);
  font-size: 14px; font-weight: 500; white-space: nowrap;
  box-shadow: 0 4px 16px rgba(0,0,0,0.18); z-index: 50;
}

/* ---------- lightbox ---------- */
.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,0.92); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }
