:root {
  --bg: var(--tg-theme-bg-color, #17212b);
  --card: var(--tg-theme-secondary-bg-color, #232e3c);
  --text: var(--tg-theme-text-color, #f5f5f5);
  --hint: var(--tg-theme-hint-color, #708499);
  --link: var(--tg-theme-link-color, #6ab3f3);
  --button: var(--tg-theme-button-color, #5288c1);
  --button-text: var(--tg-theme-button-text-color, #fff);
  --sep: rgba(255,255,255,0.08);
  --s-open: #e74c3c;
  --s-assigned: #f39c12;
  --s-in_progress: #3498db;
  --s-waiting_user: #9b59b6;
  --s-closed: #2ecc71;
}
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.4;
  padding-bottom: 16px;
}
.loading, .empty, .error {
  text-align: center; padding: 56px 20px; color: var(--hint);
}
.error { color: #ff6b5e; }
.spinner {
  width: 30px; height: 30px; margin: 0 auto 12px;
  border: 3px solid var(--sep); border-top-color: var(--link);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* tabs */
.tabs {
  display: flex; position: sticky; top: 0; z-index: 10;
  background: var(--bg); border-bottom: 1px solid var(--sep);
}
.tab {
  flex: 1; padding: 13px 4px; text-align: center; font-size: 12px;
  font-weight: 600; color: var(--hint); cursor: pointer;
  border-bottom: 2px solid transparent;
}
.tab.active { color: var(--link); border-bottom-color: var(--link); }
.tab .badge { font-size: 11px; opacity: .8; }

/* ticket cards */
.list { padding: 10px 12px; }
.card {
  background: var(--card); border-radius: 12px; padding: 13px;
  margin-bottom: 10px; cursor: pointer; border-left: 3px solid var(--sep);
  transition: transform .1s;
}
.card:active { transform: scale(.99); }
.card.s-open { border-left-color: var(--s-open); }
.card.s-assigned { border-left-color: var(--s-assigned); }
.card.s-in_progress { border-left-color: var(--s-in_progress); }
.card.s-waiting_user { border-left-color: var(--s-waiting_user); }
.card.s-closed { border-left-color: var(--s-closed); }
.card-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.card-id { font-weight: 700; font-size: 14px; }
.badge-status {
  font-size: 11px; padding: 3px 9px; border-radius: 10px; font-weight: 600; white-space: nowrap;
}
.b-open { background: rgba(231,76,60,.18); color: #ff6b5e; }
.b-assigned { background: rgba(243,156,18,.18); color: #f5a623; }
.b-in_progress { background: rgba(52,152,219,.18); color: #4aa3e8; }
.b-waiting_user { background: rgba(155,89,182,.18); color: #b06fd4; }
.b-closed { background: rgba(46,204,113,.18); color: #3dd882; }
.card-subject {
  font-size: 14px; margin-bottom: 6px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.card-meta { display: flex; gap: 12px; font-size: 12px; color: var(--hint); flex-wrap: wrap; }

/* chat */
.chat { display: flex; flex-direction: column; height: 100vh; position: relative; }
.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 12px;
  border-bottom: 1px solid var(--sep); position: sticky; top: 0; background: var(--bg); z-index: 10;
}
.back { font-size: 22px; background: none; border: none; color: var(--link); cursor: pointer; }
.chat-title-main { font-weight: 600; font-size: 15px; }
.chat-title-sub { font-size: 12px; color: var(--hint); }
.chat-body { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.bubble {
  max-width: 80%;
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 14px;
  word-break: break-word;
  position: relative;
  animation: bubble-in .18s ease;
}
@keyframes bubble-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.b-user {
  align-self: flex-start;
  background: var(--card);
  border-bottom-left-radius: 5px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.18);
}
.b-operator {
  align-self: flex-end;
  color: #fff;
  border-bottom-right-radius: 5px;
  background: linear-gradient(135deg, #3a8bdd 0%, #5b6ee8 100%);
  box-shadow: 0 2px 10px rgba(58,139,221,0.40);
}
.b-system { align-self: center; background: transparent; color: var(--hint); font-size: 12px; }
.bubble .media { font-size: 12px; opacity: .9; }
.bubble .time { font-size: 10px; opacity: .65; margin-top: 3px; }
.b-operator .time { opacity: .8; }

.actions { display: flex; gap: 8px; padding: 8px 12px; flex-wrap: wrap; border-top: 1px solid var(--sep); }
.btn {
  flex: 1; min-width: 100px; padding: 11px; border-radius: 10px; border: none;
  font-size: 14px; font-weight: 600; cursor: pointer; background: var(--button); color: var(--button-text);
}
.btn.neutral { background: var(--card); color: var(--text); }
.btn.danger { background: rgba(231,76,60,.2); color: #ff6b5e; }
.btn:active { opacity: .85; }

.input-row { display: flex; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--sep); }
.input-row textarea {
  flex: 1; resize: none; border-radius: 18px; border: 1px solid var(--sep);
  background: var(--card); color: var(--text); padding: 9px 14px; font-size: 14px; font-family: inherit;
  max-height: 110px;
}
.send {
  width: 42px; border: none; border-radius: 50%;
  background: linear-gradient(135deg, #3a8bdd 0%, #5b6ee8 100%);
  color: #fff; font-size: 17px; cursor: pointer; flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(58,139,221,0.35);
}
.send:active { transform: scale(0.94); }
.attach {
  width: 38px; border: none; border-radius: 50%; background: var(--card);
  color: var(--text); font-size: 18px; cursor: pointer; flex-shrink: 0;
}
.attach:active { opacity: .7; }

/* stats */
.stats-head { display: flex; align-items: center; gap: 10px; padding: 12px; border-bottom: 1px solid var(--sep); }
.periods { display: flex; gap: 8px; padding: 12px; }
.period { flex: 1; padding: 8px; border: none; border-radius: 8px; background: var(--card); color: var(--hint); font-size: 13px; cursor: pointer; }
.period.active { background: var(--button); color: var(--button-text); }
.summary { display: flex; gap: 8px; padding: 0 12px 12px; }
.scard { flex: 1; background: var(--card); border-radius: 12px; padding: 14px; text-align: center; }
.snum { font-size: 22px; font-weight: 700; }
.slbl { font-size: 11px; color: var(--hint); margin-top: 2px; }
.opstat { background: var(--card); border-radius: 12px; padding: 13px; margin: 0 12px 10px; }
.opstat-name { font-weight: 600; margin-bottom: 6px; }
.opstat-row { display: flex; justify-content: space-between; font-size: 13px; color: var(--hint); padding: 2px 0; }
.opstat-row b { color: var(--text); }

.chat-media {
  display: block;
  margin-top: 4px;
  border-radius: 10px;
}
img.chat-media {
  max-width: 100%;
  width: 240px;
  max-height: 320px;
  cursor: pointer;
  object-fit: cover;
  background: var(--sep);
  min-width: 80px;
  min-height: 60px;
}
.chat-audio { width: 240px; max-width: 100%; height: 40px; }
.chat-video { width: 240px; max-width: 100%; max-height: 320px; background: #000; }

.filecard {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  padding: 9px 11px;
  background: rgba(255,255,255,0.06);
  border-radius: 10px;
  cursor: pointer;
  max-width: 240px;
}
.filecard:active { opacity: .8; }
.file-ic { font-size: 20px; flex-shrink: 0; }
.file-name {
  flex: 1; font-size: 13px; word-break: break-word;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.file-dl { font-size: 16px; opacity: .8; flex-shrink: 0; }

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
  animation: fadein .15s ease;
}
.lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 8px;
}
.lightbox-close {
  position: fixed;
  top: 16px;
  right: 20px;
  color: #fff;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.85;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }

/* прикріплення-прев'ю та drag&drop */
.attach-preview {
  display: none;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--sep);
  background: var(--card);
  overflow-x: auto;
}
.pp-item {
  display: flex; align-items: center; gap: 6px;
  background: var(--bg); border-radius: 8px;
  padding: 4px 6px 4px 4px; flex-shrink: 0; max-width: 170px;
}
.pp-thumb { width: 36px; height: 36px; border-radius: 5px; object-fit: cover; flex-shrink: 0; }
.pp-ic { font-size: 20px; width: 36px; text-align: center; flex-shrink: 0; }
.pp-name {
  font-size: 12px; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; max-width: 90px;
}
.pp-x {
  border: none; background: var(--sep); color: var(--text);
  width: 22px; height: 22px; border-radius: 50%; cursor: pointer;
  font-size: 12px; flex-shrink: 0;
}
.drop-zone {
  position: absolute; inset: 0; z-index: 60;
  display: none; align-items: center; justify-content: center;
  background: rgba(82,136,193,0.20);
  border: 2px dashed var(--button);
  border-radius: 8px;
  color: var(--text); font-size: 15px; font-weight: 600;
  pointer-events: none;
}
.drop-zone.show { display: flex; pointer-events: auto; }

/* файл/медіа всередині градієнтної бульбашки оператора — трохи помітніший фон */
.b-operator .filecard { background: rgba(255,255,255,0.16); }
.b-operator .file-dl, .b-operator .file-ic { opacity: .95; }

.rating-note {
  font-size: 11px;
  color: var(--hint);
  line-height: 1.5;
  padding: 0 12px 12px;
}

/* пошук у списку */
.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--sep);
  background: var(--bg);
  position: sticky;
  top: 41px;
  z-index: 9;
}
.search-ic { font-size: 14px; opacity: .6; flex-shrink: 0; font-style: normal; }
.search-bar input {
  flex: 1;
  border: none;
  background: var(--card);
  color: var(--text);
  border-radius: 18px;
  padding: 8px 14px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
.search-bar input::placeholder { color: var(--hint); }
.search-clear {
  border: none;
  background: var(--card);
  color: var(--text);
  width: 30px;
  height: 30px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 13px;
  flex-shrink: 0;
}

/* SLA-таймер у картці */
.sla { color: var(--hint); font-size: 12px; }
.sla-over { color: #ff6b5e; font-weight: 600; }

/* форматований текст повідомлення */
.msg-text {
  display: block;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-text a { color: inherit; text-decoration: underline; }
.b-operator .msg-text a { color: #fff; }
.msg-text code {
  background: rgba(127,127,127,0.18);
  padding: 1px 4px;
  border-radius: 4px;
  font-size: 0.92em;
}
.msg-text pre {
  background: rgba(127,127,127,0.18);
  padding: 8px 10px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 4px 0;
  white-space: pre;
}
.msg-text blockquote {
  border-left: 3px solid currentColor;
  margin: 4px 0;
  padding-left: 8px;
  opacity: 0.85;
}
tg-spoiler { background: currentColor; border-radius: 3px; }

.appver { font-size: 10px; color: var(--hint); opacity: .6; flex-shrink: 0; align-self: center; }

.take-hint {
  padding: 10px 14px;
  margin: 0;
  font-size: 13px;
  color: var(--hint);
  text-align: center;
  border-top: 1px solid var(--sep);
  line-height: 1.5;
}

.card-op { font-size: 12px; color: var(--text); opacity: .85; margin-top: 2px; }

.rate-progress { font-size: 11px; color: var(--hint); font-weight: 400; }
