:root {
  --bg: #0f1416;
  --bg2: #131618;
  --panel: #1a1e21;
  --panel2: #20262a;
  --border: #2a3034;
  --text: #e9ece9;
  --muted: #9aa39c;
  --accent: #63e2b7;
  --accent-ink: #08231d;
  --danger: #e88080;
  --header-h: 60px;
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
}

/* Светлая тема: применяется либо по системной настройке (если пользователь не
   выбрал вручную), либо принудительно через data-theme="light". */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #eef2f0;
    --bg2: #f6f8f7;
    --panel: #ffffff;
    --panel2: #f0f3f1;
    --border: #d9e0db;
    --text: #14201b;
    --muted: #5d6b63;
    --accent: #12a37a;
    --accent-ink: #ffffff;
    --danger: #c0392b;
  }
}

:root[data-theme="light"] {
  --bg: #eef2f0;
  --bg2: #f6f8f7;
  --panel: #ffffff;
  --panel2: #f0f3f1;
  --border: #d9e0db;
  --text: #14201b;
  --muted: #5d6b63;
  --accent: #12a37a;
  --accent-ink: #ffffff;
  --danger: #c0392b;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg2);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

/* Жёстко фиксируем страницу: iOS не должен прокручивать layout-viewport, когда
   появляется клавиатура (именно из-за этого поле ввода «улетало» вверх). Видимую
   высоту целиком отдаём экрану чата через --app-height/--vv-top. */
body {
  position: fixed;
  inset: 0;
  width: 100%;
  overflow: hidden;
}

.screen { height: 100%; min-height: 100dvh; }

/* Экран чата фиксируем по реальному видимому вьюпорту. И высота (--app-height),
   и сдвиг сверху (--vv-top) задаются из JS по visualViewport: на iPhone при показе
   клавиатуры WebKit прокручивает layout-viewport, поэтому без компенсации offsetTop
   поле ввода «улетает» вверх. С --vv-top экран всегда совпадает с видимой областью. */
.screen.chat {
  position: fixed;
  top: var(--vv-top, 0px);
  left: 0;
  right: 0;
  margin: 0 auto;
  height: var(--app-height, 100dvh);
  min-height: 0;
  overflow: hidden;
}

button {
  font: inherit;
  cursor: pointer;
  border: 0;
  border-radius: 10px;
}
button:disabled { opacity: 0.55; cursor: default; }

/* ============ ВХОД ============ */

.login {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px calc(20px + env(safe-area-inset-right)) calc(24px + var(--safe-bottom)) calc(20px + env(safe-area-inset-left));
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(99, 226, 183, 0.10), transparent 60%),
    var(--bg2);
}

.card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.28);
}

.brand { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 6px; margin-bottom: 6px; }
.brand-logo { border-radius: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3); }
.card h1 { margin: 6px 0 0; font-size: 21px; }
.sub { margin: 0; color: var(--muted); font-size: 13.5px; }

label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }

input, textarea {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 11px;
  color: var(--text);
  padding: 12px 13px;
  font: inherit;
  outline: none;
  transition: border-color 0.15s;
}
input:focus, textarea:focus { border-color: var(--accent); }

.card button {
  background: var(--accent);
  color: var(--accent-ink);
  padding: 13px 16px;
  font-weight: 700;
  border-radius: 12px;
}

.error { margin: 0; color: var(--danger); font-size: 13px; }
.muted { color: var(--muted); }

/* ============ ЧАТ ============ */

.chat {
  display: flex;
  flex-direction: column;
  max-width: 760px;
  margin: 0 auto;
  background: var(--bg2);
  animation: fade-in 0.25s ease;
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

.chat-header {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  z-index: 5;
}

.ch-id { display: flex; align-items: center; gap: 11px; min-width: 0; }
.ch-avatar { border-radius: 50%; flex: none; }
.ch-meta { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.ch-meta strong { font-size: 15.5px; }
.ch-status { font-size: 12px; color: var(--accent); }
.ch-status.off { color: var(--danger); }

.ch-actions { display: flex; align-items: center; gap: 4px; flex: none; }

.icon-btn {
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: var(--muted);
  border-radius: 11px;
  font-size: 17px;
}
.icon-btn:hover { background: var(--panel2); }
.icon-btn.on { color: var(--accent); }
.icon-btn .ic { line-height: 1; }

/* баннер-подсказка */
.banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: linear-gradient(180deg, rgba(99, 226, 183, 0.12), rgba(99, 226, 183, 0.05));
  border-bottom: 1px solid var(--border);
}
.banner-text { flex: 1; font-size: 13px; color: var(--text); }
.banner-actions { display: flex; align-items: center; gap: 6px; flex: none; }
.banner-btn {
  background: var(--accent);
  color: var(--accent-ink);
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 700;
}
.banner-x {
  background: transparent;
  color: var(--muted);
  width: 30px;
  height: 30px;
  font-size: 14px;
}

/* лента сообщений */
.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scrollbar-width: none;
}
.messages::-webkit-scrollbar { display: none; }

.empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  padding: 24px;
}
.empty-emoji { font-size: 40px; margin-bottom: 8px; }
.empty p { margin: 0; font-size: 14px; }

.day { text-align: center; margin: 12px 0 8px; }
.day span {
  display: inline-block;
  padding: 3px 12px;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.msg {
  position: relative;
  max-width: 82%;
  border-radius: 16px;
  padding: 9px 13px 7px;
  font-size: 14.5px;
  margin-top: 4px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
}
.msg-text { white-space: pre-wrap; word-break: break-word; }
.msg-text a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
  font-weight: 600;
  word-break: break-all;
}
.msg.admin .msg-text a, .msg.system .msg-text a { color: var(--accent); }

.msg.client {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-ink);
  border-bottom-right-radius: 5px;
}
.msg.admin, .msg.system {
  align-self: flex-start;
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
}
.msg.system {
  align-self: center;
  text-align: center;
  background: var(--panel2);
  font-size: 13px;
  color: var(--muted);
}

.msg .time {
  display: block;
  margin-top: 3px;
  font-size: 10.5px;
  opacity: 0.7;
  text-align: right;
}
.msg.client .time { color: var(--accent-ink); }
.msg.admin .time, .msg.system .time { color: var(--muted); }

/* кнопка «вниз» */
.scroll-down {
  position: fixed;
  right: calc(14px + env(safe-area-inset-right));
  bottom: calc(82px + max(8px, var(--safe-bottom)));
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 22px;
  line-height: 1;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);
  z-index: 4;
}

/* поле ввода — «парящее», как в Telegram: текстовое поле отдельно от кнопки.
   flex: none — низ закреплён внизу фиксированного экрана, над клавиатурой. */
.compose {
  flex: none;
  display: flex;
  gap: 9px;
  align-items: flex-end;
  padding: 7px calc(10px + env(safe-area-inset-right)) max(6px, calc(var(--safe-bottom) - 16px)) calc(10px + env(safe-area-inset-left));
  background: var(--bg2);
}
/* при открытой клавиатуре системного safe-area нет — прижимаем поле вплотную */
.kbd .compose {
  padding-top: 4px;
  padding-bottom: 4px;
}
.compose textarea {
  flex: 1;
  resize: none;
  max-height: 120px;
  border-radius: 22px;
  padding: 11px 16px;
  line-height: 1.35;
  background: var(--panel);
  border: 1px solid var(--border);
}
.compose .send {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  flex: none;
  background: var(--accent);
  color: var(--accent-ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s ease;
}
.compose .send:active { transform: scale(0.92); }

/* ============ ВЛОЖЕНИЕ-КЛЮЧ ============ */

.att {
  margin-top: 8px;
  padding: 10px 11px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 12px;
  font-size: 13.5px;
  background: rgba(0, 0, 0, 0.08);
}
.msg.admin .att { border-color: var(--border); background: var(--panel2); }
.att.expired { color: var(--muted); }
.att-title { margin-bottom: 8px; font-weight: 600; }
.att-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.att-btn {
  background: var(--panel);
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 9px;
  padding: 7px 13px;
  font-size: 13px;
  font-weight: 600;
}
.att-btn.primary { background: var(--accent); color: var(--accent-ink); }

/* ============ КАБИНЕТ «МОЙ VPN» ============ */

.vpn-card { max-width: min(420px, 92vw); gap: 16px; }
.vpn-head { display: flex; align-items: center; justify-content: space-between; }
.vpn-head strong { font-size: 17px; }
.vpn-body { display: flex; flex-direction: column; gap: 14px; }
.vpn-loading { text-align: center; padding: 16px; }

.vpn-rows { display: flex; flex-direction: column; gap: 10px; }
.vpn-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.vpn-label { color: var(--muted); font-size: 13px; flex: none; }
.vpn-val { text-align: right; font-size: 14px; font-weight: 600; }

.vpn-bar {
  height: 8px;
  border-radius: 999px;
  background: var(--panel2);
  overflow: hidden;
  margin-top: -4px;
}
.vpn-bar span { display: block; height: 100%; background: var(--accent); border-radius: 999px; }

.vpn-tariff {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}
.vpn-tariff-row { display: flex; align-items: center; justify-content: space-between; }
.vpn-tariff-row span { color: var(--muted); font-size: 13px; }
.vpn-tariff-row strong { font-size: 15px; }

.vpn-pay-btn {
  background: var(--accent);
  color: var(--accent-ink);
  padding: 13px 16px;
  font-weight: 700;
  font-size: 15px;
  border-radius: 12px;
}
.vpn-pay-btn:disabled { opacity: 0.6; }
.vpn-note { margin: 0; font-size: 12.5px; text-align: center; }

/* ============ OVERLAY (QR / текст / iOS) ============ */

.qr-overlay, .overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 20;
  animation: fade-in 0.18s ease;
}

.qr-card, .overlay-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 22px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  max-width: 92vw;
}
.overlay-card { align-items: stretch; max-width: min(420px, 92vw); }
.overlay-card strong { font-size: 16px; }

.qr-card img {
  width: min(320px, 78vw);
  height: auto;
  border-radius: 8px;
  background: #fff;
  padding: 6px;
}
.qr-tabs { justify-content: center; }
.qr-hint { font-size: 12.5px; color: var(--muted); text-align: center; }

.text-card { max-width: min(560px, 92vw); align-items: stretch; }
.conf-text {
  margin: 0;
  max-height: 50vh;
  overflow: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 12px;
  font: 12.5px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap;
  word-break: break-all;
  -webkit-user-select: all;
  user-select: all;
}
.text-card .att-actions { justify-content: flex-end; }

.ios-steps { margin: 0; padding-left: 20px; font-size: 14px; line-height: 1.7; }
.ios-steps b { color: var(--text); }
.ios-share { display: inline-block; transform: translateY(2px); }
