/* Тема приходит из Telegram (--tg-theme-*), значения в fallback — тёмные. */
:root {
  --bg: var(--tg-theme-bg-color, #17212b);
  --bg-secondary: var(--tg-theme-secondary-bg-color, #232e3c);
  --text: var(--tg-theme-text-color, #f2f5f7);
  --hint: var(--tg-theme-hint-color, #8b9bab);
  --link: var(--tg-theme-link-color, #6ab2f2);
  --accent: var(--tg-theme-button-color, #2f8ee0);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --stroke: rgba(127, 127, 127, 0.18);
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

/* Светлая палитра — для превью в браузере и как запасной вариант. */
:root[data-theme="light"] {
  --bg: #ffffff;
  --bg-secondary: #f2f4f7;
  --text: #121417;
  --hint: #6b7280;
  --link: #1f6feb;
  --accent: #2f8ee0;
  --accent-text: #ffffff;
  --stroke: rgba(15, 23, 42, 0.10);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

body {
  overscroll-behavior-y: none;
  min-height: 100dvh;
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(12px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
}

.top h1 {
  margin: 4px 0 2px;
  font-size: 28px;
  line-height: 1.15;
  letter-spacing: -0.02em;
}

.sub {
  margin: 0 0 16px;
  color: var(--hint);
  font-size: 14px;
}

.create {
  display: flex;
  gap: 8px;
  margin: 0 0 16px;
}

.create input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  padding: 10px 14px;
  border: 1px solid var(--stroke);
  border-radius: 12px;
  background: var(--bg-secondary);
  color: var(--text);
  font-size: 16px; /* 16px — иначе iOS зумит поле при фокусе */
}

.create input::placeholder { color: var(--hint); }

.create input:focus {
  outline: none;
  border-color: var(--accent);
}

.create button {
  flex: 0 0 auto;
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 15px;
  font-weight: 600;
}

.create button:disabled { opacity: 0.6; }

.list {
  display: grid;
  gap: 10px;
}

.card {
  padding: 14px;
  border: 1px solid var(--stroke);
  border-radius: var(--radius);
  background: var(--bg-secondary);
}

.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.metka {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 15px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.spark {
  flex: 0 0 auto;
  color: var(--accent);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 1px;
}

.target {
  margin: 4px 0 10px;
  color: var(--hint);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stats {
  display: flex;
  gap: 16px;
  margin-bottom: 12px;
  color: var(--hint);
  font-size: 13px;
}

.stats b {
  display: block;
  color: var(--text);
  font-size: 18px;
  font-weight: 650;
  line-height: 1.2;
}

.actions {
  display: flex;
  gap: 8px;
}

.actions button,
.actions a {
  flex: 1 1 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--stroke);
  border-radius: 12px;
  background: transparent;
  color: var(--link);
  font-size: 14px;
  text-decoration: none;
  text-align: center;
}

.actions button:active,
.actions a:active { background: rgba(127, 127, 127, 0.12); }

.empty,
.error {
  margin: 24px 0;
  color: var(--hint);
  text-align: center;
  font-size: 15px;
}

.error { color: #e0625b; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + var(--safe-bottom));
  transform: translate(-50%, 20px);
  max-width: 80vw;
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.78);
  color: #fff;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (prefers-reduced-motion: reduce) {
  .toast { transition: none; }
}

/* --- нижние вкладки, кабинет, диалоги ---------------------------------- */

.app { padding-bottom: calc(96px + var(--safe-bottom)); }

.tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  gap: 8px;
  padding: 8px 16px calc(8px + var(--safe-bottom));
  background: var(--bg);
  border-top: 1px solid var(--stroke);
}

.tab {
  flex: 1;
  min-height: 48px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--hint);
  font-size: 14px;
  font-weight: 600;
}

.tab.active {
  color: var(--accent);
  background: rgba(127, 127, 127, 0.12);
}

.balance-card { display: grid; gap: 8px; margin-bottom: 12px; }

.label {
  margin: 0;
  color: var(--hint);
  font-size: 12px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.balance {
  margin: 0;
  font-size: 34px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.primary {
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 15px;
  font-weight: 600;
}

.ghost {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--stroke);
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-size: 15px;
}

.hint-line {
  margin: 6px 0 0;
  color: var(--hint);
  font-size: 13px;
}

.tariff-card { margin-bottom: 4px; }
.tariff-card p { margin: 0 0 6px; font-size: 14px; }

.section-title { margin: 20px 0 8px; font-size: 17px; }

.badge {
  display: inline-block;
  margin-top: 8px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.badge-free { background: rgba(47, 142, 224, 0.16); color: var(--accent); }
.badge-paid { background: rgba(46, 160, 67, 0.16); color: #3fb950; }
.badge-warn { background: rgba(224, 98, 91, 0.18); color: #e0625b; }

.tx-list { display: grid; gap: 8px; }

.tx {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border: 1px solid var(--stroke);
  border-radius: 12px;
}

.tx-left { display: grid; gap: 2px; }
.tx-note { font-size: 14px; }
.tx-date { color: var(--hint); font-size: 12px; }
.tx-amount { font-weight: 650; white-space: nowrap; }
.tx-amount.pos { color: #3fb950; }

.docs { display: grid; gap: 12px; }
.docs a { color: var(--link); font-size: 14px; text-decoration: none; }

dialog.sheet {
  width: min(92vw, 420px);
  padding: 18px;
  border: 1px solid var(--stroke);
  border-radius: 18px;
  background: var(--bg);
  color: var(--text);
}

dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.5); }

dialog.sheet h2 { margin: 0 0 6px; font-size: 19px; }

dialog.sheet input {
  width: 100%;
  min-height: 44px;
  margin: 12px 0;
  padding: 10px 12px;
  border: 1px solid var(--stroke);
  border-radius: 12px;
  background: var(--bg-secondary);
  color: var(--text);
  font-size: 16px;
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }

.chip {
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--stroke);
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  font-size: 14px;
}

.chip.active { border-color: var(--accent); color: var(--accent); }

.dialog-actions { display: flex; gap: 8px; margin-top: 16px; }
.dialog-actions button { flex: 1; }

.renew-btn {
  margin-top: 10px;
  min-height: 44px;
  width: 100%;
  border: 1px solid var(--accent);
  border-radius: 12px;
  background: transparent;
  color: var(--accent);
  font-size: 14px;
  font-weight: 600;
}
.link-danger {
  margin-top: 8px;
  min-height: 40px;
  width: 100%;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: #e0625b;
  font-size: 13px;
}

.danger {
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  background: #e0625b;
  color: #ffffff;
  font-size: 15px;
  font-weight: 600;
}

.tx-pending { opacity: 0.6; }

.head-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.refresh-btn {
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--accent);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.2s ease;
}

.refresh-btn:hover {
  background: var(--accent-soft, rgba(47, 142, 224, 0.12));
}

.refresh-btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.refresh-btn.spin {
  animation: refresh-spin 0.8s linear infinite;
}

@keyframes refresh-spin {
  to {
    transform: rotate(360deg);
  }
}

.card-foot {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.card-foot > button {
  flex: 1 1 0;
  min-height: 40px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  font-size: 13px;
}

.card-foot .link-danger {
  margin-top: 0;
  width: auto;
}

.link-muted {
  color: var(--hint);
}

.link-muted:hover {
  color: var(--text);
}

/* Явная тёмная тема — когда пользователь выбрал её вручную. */
:root[data-theme="dark"] {
  --bg: #17212b;
  --bg-secondary: #232e3c;
  --text: #f2f5f7;
  --hint: #8b9bab;
  --link: #6ab2f2;
  --accent: #2f8ee0;
  --accent-text: #ffffff;
}

.chip.active {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-text, #ffffff);
  font-weight: 600;
}

.admin-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 8px;
}

.admin-stat {
  background: var(--bg-secondary);
  border: 1px solid var(--stroke);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.admin-stat b {
  display: block;
  font-size: 20px;
  margin-top: 2px;
}

.admin-stat span {
  color: var(--hint);
  font-size: 12px;
}

.admin-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.admin-row .btn-mini {
  min-height: 30px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid var(--stroke);
  background: transparent;
  color: var(--accent);
  font-size: 12px;
}

.admin-user {
  font-size: 13px;
}

.admin-user code {
  font-size: 13px;
}

.admin-tools {
  margin-bottom: 8px;
}

.admin-tools input {
  flex: 1 1 160px;
  min-height: 38px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--stroke);
  background: var(--bg-secondary);
  color: var(--text);
  font-size: 14px;
}

.admin-user.blocked {
  opacity: 0.55;
}
