/* Тема берётся из Telegram через --tg-theme-* (выставляются в tg.js).
   Здесь — дефолты на случай отсутствия (открытие вне Telegram). */
:root {
  --bg: var(--tg-theme-bg-color, #16171a);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #1f2026);
  --text: var(--tg-theme-text-color, #ffffff);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --link: var(--tg-theme-link-color, #5b8def);
  --button: var(--tg-theme-button-color, #5b8def);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #e5484d;
  --radius: 16px;
  --gap: 12px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  overscroll-behavior-y: none;
}

#app { padding: 16px 16px 96px; max-width: 640px; margin: 0 auto; }

.screen { animation: fade .18s ease; }
.hidden { display: none !important; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ---------- Header ---------- */
.app-header { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 18px; }
.greeting { font-size: 26px; font-weight: 700; }
.subtitle { color: var(--hint); font-size: 14px; margin-top: 2px; }
.icon-btn { background: var(--secondary-bg); border: none; color: var(--text);
  width: 42px; height: 42px; border-radius: 50%; font-size: 18px; cursor: pointer; }

/* ---------- Список привычек ---------- */
.habits { display: flex; flex-direction: column; gap: var(--gap); }
.habit-card {
  display: flex; align-items: center; gap: 14px;
  background: var(--secondary-bg); border-radius: var(--radius);
  padding: 14px 16px; cursor: pointer; user-select: none;
}
.habit-info { flex: 1; min-width: 0; }
.habit-name { font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.habit-meta { color: var(--hint); font-size: 13px; margin-top: 3px; }
.habit-emoji { font-size: 24px; }

/* Кольцо прогресса (SVG) */
.ring { width: 46px; height: 46px; flex: 0 0 46px; position: relative; cursor: pointer; }
.ring svg { transform: rotate(-90deg); display: block; }
.ring .ring-bg { stroke: rgba(255,255,255,.12); }
.ring .ring-fg { stroke-linecap: round; transition: stroke-dashoffset .35s ease; }
.ring-check { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 700; }

/* ---------- FAB ---------- */
.fab {
  position: fixed; right: 20px; bottom: 24px;
  width: 58px; height: 58px; border-radius: 50%;
  background: var(--button); color: var(--button-text);
  border: none; font-size: 30px; line-height: 1; cursor: pointer;
  box-shadow: 0 6px 20px rgba(0,0,0,.35); z-index: 20;
}

/* ---------- Пусто ---------- */
.empty { text-align: center; padding: 60px 20px; color: var(--hint); }
.empty-emoji { font-size: 56px; }
.empty-title { font-size: 18px; font-weight: 600; color: var(--text); margin-top: 12px; }
.empty-text { margin-top: 6px; }

/* ---------- Детали привычки ---------- */
.habit-hero { text-align: center; padding: 12px 0 18px; }
.habit-hero-emoji { font-size: 54px; }
.habit-hero-title { font-size: 24px; font-weight: 700; margin-top: 6px; }
.habit-hero-sub { color: var(--hint); font-size: 14px; margin-top: 2px; }

.stat-row { display: flex; gap: var(--gap); margin-bottom: 20px; }
.stat-card { flex: 1; background: var(--secondary-bg); border-radius: var(--radius); padding: 14px; text-align: center; }
.stat-value { font-size: 22px; font-weight: 700; }
.stat-label { color: var(--hint); font-size: 12px; margin-top: 4px; }

.section-title { font-size: 14px; color: var(--hint); text-transform: uppercase; letter-spacing: .04em; margin: 18px 0 10px; }

/* ---------- Heatmap ---------- */
.heatmap-wrap { overflow-x: auto; background: var(--secondary-bg); border-radius: var(--radius); padding: 14px; }
.heatmap { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 1fr); gap: 4px; }
.hm-cell { width: 14px; height: 14px; border-radius: 3px; background: rgba(255,255,255,.08); cursor: pointer; }
.hint { color: var(--hint); font-size: 13px; margin-top: 8px; }

/* ---------- Календарь с подписями (дни/месяцы/год) ---------- */
.cal { display: block; }
.cal-months { display: flex; font-size: 11px; color: var(--hint); margin-bottom: 4px; height: 14px; line-height: 14px; }
.cal-corner { flex: 0 0 26px; }
.cal-month { flex: 0 0 18px; white-space: nowrap; overflow: visible; }
.cal-body { display: flex; }
.cal-weekdays { flex: 0 0 26px; display: flex; flex-direction: column; gap: 4px; }
.cal-wd { height: 14px; line-height: 14px; font-size: 10px; color: var(--hint); }
.cal-grid { display: flex; gap: 4px; }
.cal-col { display: flex; flex-direction: column; gap: 4px; }
.cal-caption { margin-top: 8px; font-size: 12px; color: var(--hint); }

.habit-actions { display: flex; gap: var(--gap); margin-top: 24px; }

/* ---------- Кнопки ---------- */
.btn { border: none; border-radius: 12px; padding: 14px 16px; font-size: 16px; font-weight: 600; cursor: pointer; width: 100%; }
.btn-primary { background: var(--button); color: var(--button-text); }
.btn-secondary { background: var(--secondary-bg); color: var(--text); }
.btn-danger { background: rgba(229,72,77,.15); color: var(--danger); }

/* ---------- Формы ---------- */
.form-title { font-size: 24px; font-weight: 700; margin-bottom: 18px; }
.field { display: block; margin-bottom: 18px; }
.field-label { display: block; color: var(--hint); font-size: 13px; margin-bottom: 8px; }
.field input[type="text"], .field input[type="number"], .field input[type="time"] {
  width: 100%; background: var(--secondary-bg); border: none; border-radius: 12px;
  padding: 14px 16px; color: var(--text); font-size: 16px;
}
.toggle-field { display: flex; align-items: center; justify-content: space-between; }
.toggle-field input { width: 22px; height: 22px; }

.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 8px; }
.emoji-grid .opt { font-size: 22px; text-align: center; padding: 8px 0; border-radius: 10px; background: var(--secondary-bg); cursor: pointer; }
.emoji-grid .opt.active { outline: 2px solid var(--button); }

.color-grid { display: flex; gap: 10px; flex-wrap: wrap; }
.color-grid .swatch { width: 32px; height: 32px; border-radius: 50%; cursor: pointer; border: 2px solid transparent; }
.color-grid .swatch.active { border-color: var(--text); }

.seg { display: flex; background: var(--secondary-bg); border-radius: 12px; padding: 4px; gap: 4px; }
.seg-btn { flex: 1; background: transparent; border: none; color: var(--hint); padding: 10px 6px; border-radius: 9px; font-size: 14px; cursor: pointer; }
.seg-btn.active { background: var(--button); color: var(--button-text); }

.weekdays { display: flex; gap: 6px; margin-top: 10px; }
.weekdays .wd { flex: 1; text-align: center; padding: 10px 0; background: var(--secondary-bg); border-radius: 9px; cursor: pointer; font-size: 14px; }
.weekdays .wd.active { background: var(--button); color: var(--button-text); }

/* ---------- Профиль ---------- */
.profile-card { background: var(--secondary-bg); border-radius: var(--radius); padding: 16px; margin-bottom: 18px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: rgba(0,0,0,.85); color: #fff; padding: 10px 18px; border-radius: 20px;
  font-size: 14px; z-index: 50; transition: opacity .2s; max-width: 90%;
  display: flex; align-items: center; gap: 12px;
}
.toast-action { background: none; border: none; color: var(--link); font-weight: 700; font-size: 14px; cursor: pointer; padding: 0; }

/* ---------- Шапка (расширение) ---------- */
.header-actions { display: flex; gap: 8px; }
.dayprogress { height: 6px; background: rgba(255,255,255,.10); border-radius: 99px; overflow: hidden; margin: 4px 0 14px; }
.dayprogress-fill { height: 100%; background: var(--button); border-radius: 99px; width: 0; transition: width .4s ease; }
.list-filter { margin-bottom: 14px; }

/* ---------- Цветовой акцент карточки + мини-неделя ---------- */
.emoji-badge { display: flex; align-items: center; justify-content: center; width: 40px; height: 40px; border-radius: 12px; font-size: 22px; flex: 0 0 40px; }
.miniweek { display: flex; gap: 5px; margin-top: 8px; }
.mw-dot { width: 9px; height: 9px; border-radius: 50%; background: transparent; }
.mw-rest { background: rgba(255,255,255,.07); }
.mw-miss { box-shadow: inset 0 0 0 1.5px rgba(255,255,255,.22); }
.mw-future { box-shadow: inset 0 0 0 1px rgba(255,255,255,.12); }
.mw-today { outline: 2px solid var(--hint); outline-offset: 1px; }

/* ---------- Статистика ---------- */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--gap); }
.top-habits { display: flex; flex-direction: column; gap: 8px; }
.top-row { display: flex; align-items: center; gap: 12px; background: var(--secondary-bg); border-radius: 12px; padding: 12px 14px; }
.top-row .t-emoji { font-size: 20px; }
.top-row .t-name { flex: 1; font-weight: 600; }
.top-row .t-streak { color: var(--hint); font-weight: 700; }

/* ---------- Шаблоны ---------- */
.templates-strip { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
.tpl-chip { flex: 0 0 auto; background: var(--secondary-bg); border-radius: 12px; padding: 8px 12px; font-size: 14px; cursor: pointer; white-space: nowrap; }
.tpl-chip:active { transform: scale(.96); }

/* ---------- Анимация выполнения ---------- */
.ring.pulse { animation: ringpulse .45s ease; }
@keyframes ringpulse { 0% { transform: scale(1); } 40% { transform: scale(1.18); } 100% { transform: scale(1); } }

/* ---------- Конфетти ---------- */
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 60; overflow: hidden; }
.confetti-piece {
  position: fixed; width: 8px; height: 8px; border-radius: 2px;
  animation: confetti-fly 1s ease-out forwards;
}
@keyframes confetti-fly {
  0% { transform: translate(0, 0) rotate(0deg); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) rotate(360deg); opacity: 0; }
}

/* ---------- Празднование вехи ---------- */
.celebrate {
  position: fixed; inset: 0; z-index: 70; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.55); opacity: 0; transition: opacity .25s;
}
.celebrate.show { opacity: 1; }
.celebrate-box {
  background: var(--secondary-bg); border-radius: 24px; padding: 32px 40px; text-align: center;
  transform: scale(.8); transition: transform .25s cubic-bezier(.2,.8,.3,1.4);
}
.celebrate.show .celebrate-box { transform: scale(1); }
.celebrate-emoji { font-size: 64px; }
.celebrate-streak { font-size: 24px; font-weight: 800; margin-top: 8px; }
.celebrate-text { color: var(--hint); margin-top: 6px; }

/* ---------- Достижения (бейджи) ---------- */
.ach-count { color: var(--hint); font-size: 13px; font-weight: 400; letter-spacing: 0; text-transform: none; }
.badges { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.badge { background: var(--secondary-bg); border-radius: 14px; padding: 12px 6px; text-align: center; }
.badge.locked { opacity: 0.4; }
.badge-emoji { font-size: 26px; }
.badge-title { font-size: 11px; margin-top: 4px; line-height: 1.2; }
.badge-prog { font-size: 10px; color: var(--hint); margin-top: 2px; }

/* ---------- «Плохие привычки»: счётчик дней без срыва ---------- */
.quit-count { flex: 0 0 auto; text-align: center; min-width: 52px; }
.quit-num { font-size: 22px; font-weight: 800; line-height: 1; }
.quit-lbl { font-size: 9px; color: var(--hint); margin-top: 2px; }
.mw-slip { background: var(--danger) !important; opacity: 1 !important; }

/* ---------- Перетаскивание + категории ---------- */
.drag-handle { flex: 0 0 auto; color: var(--hint); font-size: 20px; cursor: grab; touch-action: none; padding: 0 2px; user-select: none; }
.habit-card.dragging { opacity: 0.9; box-shadow: 0 10px 26px rgba(0,0,0,.45); position: relative; z-index: 5; }
.cat-group { margin-top: 16px; }
.cat-group:first-child { margin-top: 0; }
.cat-title { font-size: 13px; color: var(--hint); margin: 0 0 8px 4px; }

/* ---------- Настроение дня ---------- */
.mood-bar { background: var(--secondary-bg); border-radius: 14px; padding: 10px 12px; margin-bottom: 14px; }
.mood-row { display: flex; justify-content: space-between; gap: 6px; }
.mood-opt { font-size: 24px; flex: 1; text-align: center; padding: 6px 0; border-radius: 10px; cursor: pointer; opacity: 0.45; transition: opacity .15s, transform .15s; }
.mood-opt.active { opacity: 1; background: rgba(255,255,255,.08); transform: scale(1.12); }
.mood-note { width: 100%; margin-top: 8px; background: transparent; border: none; border-top: 1px solid rgba(255,255,255,.08); padding: 8px 2px 0; color: var(--text); font-size: 14px; }
.mood-note::placeholder { color: var(--hint); }

/* ---------- Бар-чарты ---------- */
.barchart { display: flex; align-items: flex-end; gap: 6px; height: 130px; background: var(--secondary-bg); border-radius: 14px; padding: 12px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; }
.bar { width: 100%; max-width: 28px; background: var(--button); border-radius: 6px 6px 0 0; min-height: 2px; transition: height .4s ease; }
.bar-val { font-size: 9px; color: var(--hint); }
.bar-label { font-size: 10px; color: var(--hint); }

/* ---------- Лента ---------- */
.feed { display: flex; flex-direction: column; gap: 8px; }
.feed-item { background: var(--secondary-bg); border-radius: 12px; padding: 10px 14px; font-size: 14px; }
.feed-who { font-weight: 600; }
.feed-when { color: var(--hint); font-size: 12px; margin-top: 2px; }
.friend-sub { color: var(--hint); font-size: 13px; margin-top: 3px; }

/* ---------- Нижняя навигация (вкладки) ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; background: var(--secondary-bg);
  border-top: 1px solid rgba(255,255,255,.08);
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; padding: 9px 0 10px; background: none; border: none; color: var(--hint); cursor: pointer;
}
.tab-ico { font-size: 20px; line-height: 1; filter: grayscale(1) opacity(.7); transition: filter .15s; }
.tab-lbl { font-size: 11px; }
.tab.active { color: var(--button); }
.tab.active .tab-ico { filter: none; }

/* Сдвигаем контент/FAB/тост, чтобы не перекрывались панелью вкладок */
body.has-tabbar #app { padding-bottom: 150px; }
body.has-tabbar .fab { bottom: 84px; }
body.has-tabbar .toast { bottom: 140px; }

/* ---------- Цели ---------- */
.goals { display: flex; flex-direction: column; gap: var(--gap); }
.goal-card { align-items: center; }
.goal-sub { color: var(--text); opacity: .85; font-size: 13px; margin-top: 4px; }
.goal-done { opacity: .62; }
.hz-chip {
  display: inline-block; background: rgba(255,255,255,.08); color: var(--hint);
  border-radius: 6px; padding: 1px 7px; font-size: 11px; vertical-align: middle;
}

/* Большое кольцо на экране цели */
.goal-ring-wrap { display: flex; justify-content: center; padding: 6px 0 4px; }
.goal-ring-wrap .ring-check { font-weight: 800; }
.goal-progress-label { text-align: center; color: var(--hint); font-size: 14px; margin-bottom: 4px; }

/* Подцели-чеклист */
.goal-steps { display: flex; flex-direction: column; gap: 8px; }
.goal-step {
  display: flex; align-items: center; gap: 12px; background: var(--secondary-bg);
  border-radius: 12px; padding: 12px 14px; user-select: none;
}
.goal-step .step-check {
  flex: 0 0 22px; width: 22px; height: 22px; border-radius: 50%;
  border: 2px solid var(--hint); display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 800; cursor: pointer;
}
.goal-step.done .step-check { background: var(--button); border-color: var(--button); color: var(--button-text); }
.goal-step .step-title { flex: 1; font-size: 15px; cursor: pointer; }
.goal-step.done .step-title { text-decoration: line-through; color: var(--hint); }
.goal-step .step-del { background: none; border: none; color: var(--hint); font-size: 15px; cursor: pointer; padding: 0 2px; }
.goal-step.dragging { opacity: .9; box-shadow: 0 10px 26px rgba(0,0,0,.45); position: relative; z-index: 5; }

.step-add { display: flex; gap: 8px; margin-top: 10px; }
.step-add input {
  flex: 1; background: var(--secondary-bg); border: none; border-radius: 12px;
  padding: 12px 14px; color: var(--text); font-size: 15px;
}
.step-add-btn {
  flex: 0 0 46px; background: var(--button); color: var(--button-text);
  border: none; border-radius: 12px; font-size: 22px; cursor: pointer;
}

/* Числовой прогресс */
.numeric-box { display: flex; align-items: center; gap: 12px; }
.num-val { flex: 1; text-align: center; font-size: 22px; font-weight: 800; }
.num-btn {
  flex: 0 0 auto; min-width: 64px; background: var(--secondary-bg); color: var(--text);
  border: none; border-radius: 12px; padding: 14px 16px; font-size: 18px; font-weight: 700; cursor: pointer;
}
.num-btn.primary { background: var(--button); color: var(--button-text); }

/* Сферы жизни (чипы) */
.sphere-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.sphere-chip { background: var(--secondary-bg); border-radius: 12px; padding: 8px 14px; font-size: 14px; cursor: pointer; }
.sphere-chip.active { background: var(--button); color: var(--button-text); }

/* Рефлексия / select */
.reflection {
  width: 100%; min-height: 80px; resize: vertical; background: var(--secondary-bg);
  border: none; border-radius: 12px; padding: 14px 16px; color: var(--text); font-size: 15px;
  font-family: inherit;
}
.reflection::placeholder { color: var(--hint); }
.select {
  width: 100%; background: var(--secondary-bg); border: none; border-radius: 12px;
  padding: 14px 16px; color: var(--text); font-size: 16px;
}

/* Сегмент-контрол с горизонтальной прокруткой (много вариантов: горизонты) */
.seg-wrap { overflow-x: auto; }
.seg-wrap .seg-btn { flex: 0 0 auto; padding: 10px 14px; }

/* ---------- Этап 5: кольцо дня + уровень в шапке ---------- */
.day-summary { display: flex; align-items: center; gap: 14px; margin: 4px 0 14px; }
.dayring { flex: 0 0 auto; }
.day-summary-meta { flex: 1; min-width: 0; }
.level-chip-mini { font-size: 13px; font-weight: 700; color: var(--button); }
.day-summary-hint { font-size: 12px; color: var(--hint); margin-top: 2px; }

/* ---------- Баннер заморозки стрика ---------- */
.freeze-banner {
  background: rgba(90,200,250,.12); border: 1px solid rgba(90,200,250,.35);
  border-radius: 14px; padding: 12px 14px; margin-bottom: 14px;
}
.freeze-text { font-size: 14px; margin-bottom: 10px; }
.btn-freeze {
  background: #5ac8fa; color: #04212e; border: none; border-radius: 10px;
  padding: 9px 14px; font-size: 14px; font-weight: 700; cursor: pointer;
}

/* ---------- Геймификация: панель уровня/XP ---------- */
.xp-panel { background: var(--secondary-bg); border-radius: 14px; padding: 14px; margin-bottom: 16px; }
.xp-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.level-chip { font-size: 15px; font-weight: 700; }
.season-text { font-size: 12px; color: var(--hint); }
.xp-bar { height: 9px; background: rgba(255,255,255,.10); border-radius: 99px; overflow: hidden; }
.xp-fill { height: 100%; background: var(--button); border-radius: 99px; width: 0; transition: width .5s ease; }
.xp-meta { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--hint); margin-top: 8px; }

/* ---------- Инсайты: дельты + связь привычка↔настроение ---------- */
.delta-up { color: #34c759; font-weight: 700; }
.delta-down { color: var(--danger); font-weight: 700; }
.delta-eq { color: var(--hint); }
.insights { display: flex; flex-direction: column; gap: 8px; }
.insight-row { display: flex; align-items: center; gap: 12px; background: var(--secondary-bg); border-radius: 12px; padding: 10px 12px; }
.insight-info { flex: 1; min-width: 0; }
.insight-name { font-weight: 600; font-size: 15px; }
.insight-sub { color: var(--hint); font-size: 12px; margin-top: 2px; }
.insight-mini { flex: 0 0 64px; width: 64px; height: 46px; padding: 6px 4px; gap: 4px; }
.bar.worst { background: var(--danger) !important; }

/* ---------- Цели: прогноз + вехи с датами + привязанные привычки ---------- */
.goal-forecast { text-align: center; font-size: 13px; color: #34c759; margin-bottom: 8px; }
.goal-forecast.behind { color: var(--danger); }
.step-date { flex: 0 0 auto; width: 130px; background: var(--secondary-bg); border: none; border-radius: 12px;
  padding: 12px 10px; color: var(--text); font-size: 13px; }
.goal-step .due { font-size: 11px; color: var(--hint); background: rgba(255,255,255,.08);
  border-radius: 6px; padding: 1px 6px; margin-left: 8px; white-space: nowrap; }
.goal-step .due.soon { color: #ff9500; background: rgba(255,149,0,.15); }
.goal-step .due.overdue { color: var(--danger); background: rgba(229,72,77,.15); }
.mini-habit-list { display: flex; flex-direction: column; gap: 8px; }
.mini-habit { display: flex; align-items: center; gap: 12px; background: var(--secondary-bg);
  border-radius: 12px; padding: 10px 12px; cursor: pointer; }
