:root {
  color-scheme: light dark;
  --bg: var(--tg-theme-bg-color, #fff);
  --text: var(--tg-theme-text-color, #111);
  --hint: var(--tg-theme-hint-color, #888);
  --card: var(--tg-theme-secondary-bg-color, #f3f3f3);
  --accent: var(--tg-theme-button-color, #3390ec);
  /* Иконка-глиф «график» (мини-столбики) для кликабельных тайлов. */
  --chart-glyph: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Crect x='3' y='12' width='4' height='9' rx='1'/%3E%3Crect x='10' y='7' width='4' height='14' rx='1'/%3E%3Crect x='17' y='3' width='4' height='18' rx='1'/%3E%3C/svg%3E");
  /* Иконка-глиф «персона» — помечает персональные плитки-метрики. */
  --person-glyph: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='8' r='4'/%3E%3Cpath d='M4 20c0-4.4 3.6-7 8-7s8 2.6 8 7v1H4z'/%3E%3C/svg%3E");
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.wrap { max-width: 640px; margin: 0 auto; padding: 16px; }
h1 { font-size: 22px; margin: 8px 0 16px; }
h2 { font-size: 17px; margin: 24px 0 12px; }

.feed-title { display: flex; align-items: center; gap: 8px; }
.live {
  font-size: 12px; font-weight: 800; letter-spacing: 0.08em;
  color: #ff3b30; border: 1.5px solid #ff3b30; border-radius: 6px;
  padding: 2px 6px; line-height: 1;
  animation: live-pulse 1.6s ease-in-out infinite;
}
@keyframes live-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.45; } }

.stats { display: flex; gap: 8px; }
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stat {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  background: var(--card); border-radius: 14px; padding: 14px 8px;
}
.stat .num { font-size: 20px; font-weight: 700; }
.stat .lbl { font-size: 12px; color: var(--hint); margin-top: 4px; text-align: center; }

/* Тайл-кнопка, открывающая график. Выглядит как .stat, но кликабелен. */
.stat-chart {
  position: relative; border: 0; font: inherit; color: inherit; cursor: pointer;
  -webkit-tap-highlight-color: transparent; transition: transform 0.12s ease, background 0.12s ease;
}
.stat-chart::after {
  content: ""; position: absolute; top: 9px; right: 9px;
  width: 14px; height: 14px; opacity: 0.5;
  background: var(--hint);
  -webkit-mask: var(--chart-glyph) center / contain no-repeat;
  mask: var(--chart-glyph) center / contain no-repeat;
}
.stat-chart:active { transform: scale(0.97); background: color-mix(in srgb, var(--accent) 14%, var(--card)); }

/* Персональные плитки-метрики: тот же вид, что .stat, кликабельны (тап = правка),
   помечены глифом-персоной в углу. */
.metrics-grid { margin-top: 8px; }
.metrics-grid:empty { display: none; }
.stat-metric {
  position: relative; border: 0; font: inherit; color: inherit; cursor: pointer;
  -webkit-tap-highlight-color: transparent; transition: transform 0.12s ease, background 0.12s ease;
}
.stat-metric::after {
  content: ""; position: absolute; top: 9px; right: 9px;
  width: 14px; height: 14px; opacity: 0.5;
  background: var(--accent);
  -webkit-mask: var(--person-glyph) center / contain no-repeat;
  mask: var(--person-glyph) center / contain no-repeat;
}
.stat-metric:active { transform: scale(0.97); background: color-mix(in srgb, var(--accent) 14%, var(--card)); }
.add-metric { width: 100%; margin-top: 8px; }

.feed { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.card {
  /* Фон карточки = тир грейда. По умолчанию стандартный (COMMON/COMMON_PLUS).
     Цвет тира — подмешиваем к фону темы (color-mix), чтобы тинт оставался
     читаемым и в светлой, и в тёмной теме Telegram. */
  --grade-bg: var(--card);
  position: relative;
  display: flex; align-items: center; gap: 12px;
  background: var(--grade-bg); border-radius: 14px; padding: 10px 12px;
}
.card.g-rare    { --grade-bg: color-mix(in srgb, #2f6fd0 34%, var(--card)); }
.card.g-epic    { --grade-bg: color-mix(in srgb, #d63bd6 32%, var(--card)); }
.card.g-legend  { --grade-bg: color-mix(in srgb, #f0b429 38%, var(--card)); }
.card.g-diamond { --grade-bg: color-mix(in srgb, #45c8f0 36%, var(--card)); }

/* «+» для грейдов с постфиксом _PLUS — оттенок чуть темнее фона карточки. */
.card.is-plus { padding-right: 76px; }
.card.is-plus::after {
  content: "";
  position: absolute; top: 50%; right: 14px; transform: translateY(-50%);
  width: 60px; height: 60px; pointer-events: none;
  --plus-color: color-mix(in srgb, black 24%, var(--grade-bg));
  background:
    linear-gradient(var(--plus-color) 0 0) center / 100% 16px no-repeat,
    linear-gradient(var(--plus-color) 0 0) center / 16px 100% no-repeat;
}

.card img { width: 48px; height: 48px; border-radius: 10px; object-fit: cover; }
.card .meta { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.card .name { font-size: 14px; }
.card .price { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; color: var(--hint); }
.card .price .gram { width: 14px; height: 14px; flex: none; }
.card.new { animation: flash 1.2s ease; }
.empty { color: var(--hint); font-size: 14px; }

@keyframes flash {
  from { background: var(--tg-theme-button-color, #3390ec); }
  to { background: var(--grade-bg); }
}

/* --- Кнопки --- */
.btn {
  display: inline-block; border: 0; cursor: pointer;
  background: var(--tg-theme-button-color, #3390ec);
  color: var(--tg-theme-button-text-color, #fff);
  border-radius: 10px; padding: 9px 14px; font-size: 14px; text-decoration: none;
}
.btn.ghost {
  background: var(--card); color: var(--text);
}
.icon-btn {
  border: 0; background: transparent; cursor: pointer; font-size: 16px;
  padding: 4px 6px; color: var(--text); line-height: 1;
}

/* --- Секция «Мои фильтры» на дашборде --- */
.filters-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.filters-card {
  background: var(--card); border-radius: 14px; padding: 12px 14px; margin-top: 12px;
}
.filters-card h2 { margin: 4px 0; }
.filters-counts { display: flex; gap: 8px; margin-top: 8px; }
.filters-counts .stat { background: var(--bg); }

/* --- Список фильтров --- */
.card.filter { align-items: flex-start; }
.card.filter.off { opacity: 0.55; }
.row-actions { display: flex; gap: 2px; align-items: center; }

/* --- Шторка (bottom sheet) --- */
body.sheet-lock { overflow: hidden; }
.sheet {
  position: fixed; inset: 0; z-index: 50;
  visibility: hidden; pointer-events: none;
}
.sheet.open { visibility: visible; pointer-events: auto; }
.sheet-backdrop {
  position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45);
  opacity: 0; transition: opacity 0.25s ease;
}
.sheet.open .sheet-backdrop { opacity: 1; }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-height: 90vh; overflow-y: auto;
  background: var(--bg); color: var(--text);
  border-radius: 18px 18px 0 0; padding: 8px 16px calc(24px + env(safe-area-inset-bottom));
  transform: translateY(100%); transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.25);
}
.sheet.open .sheet-panel { transform: translateY(0); }
.sheet-grip {
  width: 40px; height: 4px; border-radius: 4px; margin: 6px auto 10px;
  background: var(--hint); opacity: 0.5; cursor: grab;
}

/* --- Форма фильтра --- */
.filter-form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; border: 0; padding: 0; margin: 0; }
.field > span, .legend { font-size: 13px; color: var(--hint); }
.field input[type="text"], .field input[type="number"], .filter-form select {
  background: var(--card); color: var(--text); border: 0; border-radius: 10px;
  padding: 10px 12px; font-size: 15px; width: 100%;
}
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; margin-right: 12px; }
.sep { border: 0; border-top: 1px solid var(--card); margin: 4px 0; }

/* --- Селектор коллекции с поиском --- */
.combo { position: relative; }
.suggest {
  list-style: none; margin: 4px 0 0; padding: 4px;
  position: absolute; left: 0; right: 0; top: 100%; z-index: 5;
  background: var(--bg); border-radius: 10px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
  max-height: 200px; overflow-y: auto;
}
.suggest li { padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.suggest li:hover, .suggest li:active { background: var(--card); }
.grades { display: flex; flex-wrap: wrap; gap: 6px 4px; }

/* Чипы выбранных коллекций в модалке метрики. */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips:empty { display: none; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--card); border-radius: 999px; padding: 5px 6px 5px 12px; font-size: 13px;
}
.chip .chip-x {
  border: 0; background: transparent; color: var(--hint); cursor: pointer;
  font-size: 15px; line-height: 1; padding: 0 4px;
}
.period-row { display: flex; gap: 8px; }
.period-row select { flex: 1; }
.number-row { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; }
.number-row select, .number-row input { width: auto; flex: 1; min-width: 70px; }
.form-actions { display: flex; gap: 8px; }
.err { color: #e3493a; font-size: 13px; }
.hint { color: var(--hint); font-size: 14px; }

/* --- Универсальный paywall (оверлай Pro) --- */
.paywall {
  position: fixed; inset: 0; z-index: 50; display: none;
  align-items: center; justify-content: center; padding: 16px;
}
.paywall.open { display: flex; }
body.paywall-lock { overflow: hidden; }
.paywall-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.5); }
.paywall-card {
  position: relative; z-index: 1; width: 100%; max-width: 360px;
  background: var(--bg); color: var(--text); border-radius: 16px;
  padding: 22px 20px; box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  text-align: center;
}
.paywall-x {
  position: absolute; top: 10px; right: 10px; border: 0; background: transparent;
  color: var(--hint); font-size: 16px; cursor: pointer; line-height: 1; padding: 4px;
}
.paywall-title { margin: 0 0 8px; font-size: 19px; }
.paywall-reason { color: var(--hint); font-size: 14px; margin: 0 0 16px; }
.paywall-price { font-size: 28px; font-weight: 700; margin-bottom: 18px; }
.paywall-price small { font-size: 14px; font-weight: 400; color: var(--hint); }
.paywall-pay { width: 100%; }
.paywall-status { font-size: 13px; margin: 12px 0 0; color: var(--hint); }
.paywall-status.err { color: #e3493a; }
.paywall-status.ok { color: #1aab55; }

/* --- Поп-ап с графиком статистики --- */
.chart-panel { padding-left: 12px; padding-right: 12px; }
.chart-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.chart-title { margin: 2px 0; font-size: 18px; }
.chart-x {
  border: 0; background: var(--card); color: var(--text); cursor: pointer;
  width: 30px; height: 30px; border-radius: 50%; font-size: 14px; line-height: 1; flex: none;
}
.chart-utc { font-size: 12px; color: var(--hint); margin: 0 0 12px; }

/* Сегментированный переключатель точности. */
.chart-seg {
  display: flex; gap: 2px; padding: 3px; margin-bottom: 14px;
  background: var(--card); border-radius: 12px;
}
.chart-seg button {
  flex: 1; border: 0; background: transparent; color: var(--hint); cursor: pointer;
  padding: 8px 4px; font-size: 13px; font-weight: 600; border-radius: 9px;
  transition: background 0.15s ease, color 0.15s ease;
}
.chart-seg button.on {
  background: var(--bg); color: var(--text);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.18);
}

/* Тело графика: закреплённая ось Y слева + скроллируемое полотно. */
.chart-body { display: flex; align-items: flex-start; gap: 4px; }
.chart-yaxis { position: relative; width: 38px; flex: none; }
.chart-ytick {
  position: absolute; right: 0; transform: translateY(-50%);
  font-size: 10px; color: var(--hint); white-space: nowrap;
}
.chart-scroll {
  flex: 1; overflow-x: auto; overflow-y: hidden;
  -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain;
}
.chart-canvas { position: relative; }
.chart-svg { display: block; }

.chart-grid { stroke: var(--hint); stroke-width: 1; opacity: 0.16; }
.chart-area { stroke: none; }
.chart-line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart-guide { stroke: var(--accent); stroke-width: 1; stroke-dasharray: 3 3; opacity: 0.5; }
.chart-dot { fill: var(--bg); stroke: var(--accent); stroke-width: 2; transition: r 0.1s ease; }
.chart-dot.on { r: 5; fill: var(--accent); }
.chart-hit { fill: transparent; cursor: pointer; }

.chart-xtick {
  position: absolute; bottom: 4px; transform: translateX(-50%);
  font-size: 10px; color: var(--hint); white-space: nowrap; pointer-events: none;
}

/* Подсказка над выбранной вершиной. */
.chart-tip {
  position: absolute; transform: translate(-50%, -100%); pointer-events: none;
  background: var(--text); color: var(--bg); border-radius: 10px;
  padding: 6px 10px; white-space: nowrap; text-align: center;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.28); z-index: 2;
}
.chart-tip::after {
  content: ""; position: absolute; left: 50%; bottom: -5px; transform: translateX(-50%);
  border: 5px solid transparent; border-top-color: var(--text); border-bottom: 0;
}
/* Хинт под точкой (когда сверху мало места) — стрелка смотрит вверх. */
.chart-tip.below { transform: translate(-50%, 0); }
.chart-tip.below::after {
  top: -5px; bottom: auto;
  border-top: 0; border-bottom: 5px solid var(--text);
}
.chart-tip-val { display: block; font-size: 15px; font-weight: 700; }
.chart-tip-val small { font-size: 11px; font-weight: 500; opacity: 0.7; }
.chart-tip-date { display: block; font-size: 11px; opacity: 0.7; margin-top: 1px; }

.chart-msg {
  display: flex; align-items: center; justify-content: center;
  height: 240px; color: var(--hint); font-size: 14px;
}
