:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #0f1114);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --link: var(--tg-theme-link-color, #2f81f7);
  --button: var(--tg-theme-button-color, #2f81f7);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f1f3f5);
  --card: var(--tg-theme-bg-color, #ffffff);
  --accent: #34c759;
  --danger: #ff3b30;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--secondary-bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

.topbar {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: 8px 12px calc(8px + env(safe-area-inset-top)) 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid color-mix(in srgb, var(--hint) 22%, transparent);
}
.tabs { display: flex; gap: 6px; background: var(--secondary-bg); border-radius: 12px; padding: 4px; }
.tab {
  flex: 1; border: 0; background: transparent; color: var(--hint);
  font-size: 15px; font-weight: 600; padding: 9px 6px; border-radius: 9px; cursor: pointer;
}
.tab.active { background: var(--bg); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }

main { padding: 14px 12px 28px; max-width: 640px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

.date-row { display: flex; align-items: center; justify-content: space-between; margin: 2px 0 14px; }
.date-row h2 { font-size: 17px; margin: 0; font-weight: 700; }

.habit-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.habit {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border-radius: var(--radius); padding: 13px 14px;
  box-shadow: 0 1px 2px rgba(0,0,0,.06);
}
.habit .emoji { font-size: 22px; width: 28px; text-align: center; flex: 0 0 auto; }
.habit .body { flex: 1; min-width: 0; }
.habit .name { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.habit .meta { font-size: 12px; color: var(--hint); margin-top: 3px; display: flex; gap: 10px; flex-wrap: wrap; }
.habit .meta .streak.live { color: var(--accent); font-weight: 600; }

.progress { height: 5px; background: var(--secondary-bg); border-radius: 99px; margin-top: 7px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 99px; }

.check {
  flex: 0 0 auto; width: 30px; height: 30px; border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--hint) 55%, transparent);
  background: transparent; cursor: pointer; color: #fff; font-size: 16px; line-height: 1;
  display: flex; align-items: center; justify-content: center; transition: all .12s ease;
}
.check.done { background: var(--accent); border-color: var(--accent); }

button.primary { background: var(--button); color: var(--button-text); border: 0; border-radius: 11px; padding: 11px 16px; font-size: 15px; font-weight: 600; cursor: pointer; }
button.ghost { background: transparent; border: 0; color: var(--link); font-size: 20px; cursor: pointer; padding: 4px 10px; }
button.danger { background: transparent; border: 0; color: var(--danger); font-size: 15px; font-weight: 600; cursor: pointer; }
button.wide { width: 100%; margin-top: 14px; }

.manage-item { justify-content: space-between; }
.manage-item .edit { color: var(--link); background: transparent; border: 0; font-size: 14px; font-weight: 600; cursor: pointer; }

.summary { display: flex; gap: 10px; margin-bottom: 14px; }
.summary .stat { flex: 1; background: var(--card); border-radius: var(--radius); padding: 12px; text-align: center; box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.summary .stat .num { font-size: 22px; font-weight: 800; }
.summary .stat .lbl { font-size: 12px; color: var(--hint); margin-top: 2px; }

.table-wrap { overflow-x: auto; background: var(--card); border-radius: var(--radius); box-shadow: 0 1px 2px rgba(0,0,0,.06); }
table { border-collapse: collapse; width: 100%; font-size: 12px; }
th, td { padding: 7px 8px; text-align: center; white-space: nowrap; }
th.habit-col, td.habit-col { text-align: left; position: sticky; left: 0; background: var(--card); z-index: 1; }
thead th { color: var(--hint); font-weight: 600; border-bottom: 1px solid color-mix(in srgb, var(--hint) 20%, transparent); }
tbody tr + tr td { border-top: 1px solid color-mix(in srgb, var(--hint) 12%, transparent); }
td.zero { color: var(--hint); }
td.total { font-weight: 700; }
.pct-cell { font-weight: 700; color: var(--accent); }

.empty { color: var(--hint); text-align: center; margin-top: 40px; }
.hidden { display: none !important; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: flex-end; justify-content: center; z-index: 20; }
.modal-card { background: var(--bg); width: 100%; max-width: 640px; border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
.modal-card h3 { margin: 0 0 14px; font-size: 17px; }
.modal-card label { display: block; font-size: 13px; color: var(--hint); margin-bottom: 12px; }
.modal-card input { display: block; width: 100%; margin-top: 5px; padding: 11px 12px; font-size: 16px; border-radius: 10px; border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent); background: var(--secondary-bg); color: var(--text); }
.modal-actions { display: flex; align-items: center; gap: 10px; margin-top: 4px; }
.modal-actions .spacer { flex: 1; }

.seg { display: flex; gap: 6px; background: var(--secondary-bg); border-radius: 10px; padding: 4px; margin-bottom: 12px; }
.seg-btn { flex: 1; border: 0; background: transparent; color: var(--hint); font-size: 14px; font-weight: 600; padding: 8px; border-radius: 7px; cursor: pointer; }
.seg-btn.active { background: var(--bg); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.hint { font-size: 12px; color: var(--hint); margin: 0 0 10px; line-height: 1.4; }

/* степпер ввода числа за день (pyramid) */
.stepper { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.stepper button { width: 30px; height: 30px; border-radius: 8px; border: 0; background: var(--secondary-bg); color: var(--text); font-size: 18px; cursor: pointer; line-height: 1; }
.stepper input { width: 46px; text-align: center; font-size: 16px; font-weight: 700; padding: 5px 2px; border-radius: 8px; border: 1px solid color-mix(in srgb, var(--hint) 30%, transparent); background: var(--bg); color: var(--text); }
.habit.pyramid-done { outline: 1.5px solid color-mix(in srgb, var(--accent) 55%, transparent); }
.week-tag { font-weight: 600; }
.week-tag.met { color: var(--accent); }

.modal-card select { display: block; width: 100%; margin-top: 5px; padding: 11px 12px; font-size: 16px; border-radius: 10px; border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent); background: var(--secondary-bg); color: var(--text); }

/* группы */
.group-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px 8px; }
.group-head .emoji { font-size: 20px; }
.group-head .gname { flex: 1; font-size: 15px; font-weight: 700; }
.group-head .gcount { font-size: 13px; color: var(--hint); font-weight: 600; }
.group-head .gcount.met { color: var(--accent); }
.group-block { background: var(--card); border-radius: var(--radius); box-shadow: 0 1px 2px rgba(0,0,0,.06); margin-bottom: 10px; overflow: hidden; }
.group-block .habit-list { padding: 0 10px 10px; gap: 8px; }
.group-block .habit { box-shadow: none; background: var(--secondary-bg); }
.group-progress { height: 4px; background: var(--secondary-bg); margin: 0 14px 10px; border-radius: 99px; overflow: hidden; }
.group-progress > span { display: block; height: 100%; background: var(--accent); }
.group-empty { font-size: 12px; color: var(--hint); padding: 0 14px 12px; }

/* ---- графики (инлайн SVG) ---- */
:root { --chart-ink: var(--accent); }
.chart-card { background: var(--card); border-radius: var(--radius); box-shadow: 0 1px 2px rgba(0,0,0,.06); padding: 14px 14px 10px; margin-bottom: 14px; }
.chart-card .chart-title { font-size: 14px; font-weight: 700; margin: 0 0 2px; }
.chart-card .chart-sub { font-size: 12px; color: var(--hint); margin: 0 0 12px; }
.chart-svg { width: 100%; height: auto; display: block; overflow: visible; }
.bar-rect { fill: var(--chart-ink); transition: opacity .12s; }
.bar-rect.dim { opacity: .35; }
.bar-base { fill: var(--secondary-bg); }
.axis-tick { fill: var(--hint); font-size: 10px; }
.grid-line { stroke: color-mix(in srgb, var(--hint) 22%, transparent); stroke-width: 1; }
.chart-tip { position: absolute; pointer-events: none; background: var(--text); color: var(--bg); font-size: 12px; font-weight: 600; padding: 4px 8px; border-radius: 7px; transform: translate(-50%, -115%); white-space: nowrap; opacity: 0; transition: opacity .1s; z-index: 10; }
.chart-wrap { position: relative; }

/* ---- таблица статистики: явные группы ---- */
.table-wrap { padding-bottom: 4px; }
.stats-group-row td {
  background: color-mix(in srgb, var(--accent) 14%, var(--card));
  font-weight: 700; text-align: left; font-size: 13px;
  border-top: 2px solid color-mix(in srgb, var(--accent) 45%, transparent);
}
.stats-group-row .gsub { color: var(--hint); font-weight: 600; }
tr.child td.habit-col { padding-left: 20px; }
tr.child td.habit-col::before { content: ""; display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: color-mix(in srgb, var(--accent) 55%, transparent); margin-right: 7px; vertical-align: middle; }
.rowbar { display: inline-block; width: 42px; height: 5px; border-radius: 99px; background: var(--secondary-bg); overflow: hidden; vertical-align: middle; margin-right: 6px; }
.rowbar > span { display: block; height: 100%; background: var(--accent); }
.pct-wrap { display: inline-flex; align-items: center; white-space: nowrap; }

/* ---- чипы дней недели ---- */
.chips { display: flex; gap: 6px; margin: 4px 0 12px; }
.chips .chip { flex: 1; border: 0; background: var(--secondary-bg); color: var(--hint); font-size: 13px; font-weight: 600; padding: 9px 0; border-radius: 9px; cursor: pointer; }
.chips .chip.on { background: var(--accent); color: #fff; }

/* ---- «не сегодня» (вне расписания) ---- */
.habit.off { opacity: .55; }
.habit .skip { flex: 0 0 auto; font-size: 11px; color: var(--hint); border: 1px dashed color-mix(in srgb, var(--hint) 45%, transparent); border-radius: 8px; padding: 4px 8px; }

/* ---- архив / инструменты ---- */
.manage-tools { margin-top: 14px; text-align: center; }
.linkbtn { background: transparent; border: 0; color: var(--link); font-size: 14px; font-weight: 600; cursor: pointer; }
.arch-title { font-size: 15px; margin: 16px 0 8px; }
.manage-item .arch-actions { display: flex; gap: 8px; }
.manage-item .arch-actions button { background: transparent; border: 0; font-size: 13px; font-weight: 600; cursor: pointer; }
.arch-actions .restore { color: var(--link); }
.arch-actions .purge { color: var(--danger); }
.manage-item .reorder { display: flex; flex-direction: column; gap: 2px; margin-right: 4px; }
.manage-item .reorder button { width: 26px; height: 20px; border: 0; border-radius: 6px; background: var(--secondary-bg); color: var(--text); cursor: pointer; font-size: 12px; line-height: 1; }
.manage-item .hist { background: transparent; border: 0; font-size: 17px; cursor: pointer; margin-right: 2px; }

/* ---- календарь истории ---- */
.hist-head { display: flex; align-items: center; justify-content: space-between; }
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; }
.cal .cal-h { text-align: center; font-size: 11px; color: var(--hint); font-weight: 600; padding-bottom: 2px; }
.cal .cell { aspect-ratio: 1; border-radius: 8px; border: 0; background: var(--secondary-bg); color: var(--text); font-size: 13px; cursor: pointer; display: flex; align-items: center; justify-content: center; position: relative; }
.cal .cell.empty { background: transparent; cursor: default; }
.cal .cell.off { opacity: .4; }
.cal .cell.done { background: var(--accent); color: #fff; font-weight: 700; }
.cal .cell.part { background: color-mix(in srgb, var(--accent) 40%, var(--secondary-bg)); }
.cal .cell.today { outline: 2px solid var(--link); }

/* ---- годовой хитмэп ---- */
.heat-wrap { overflow-x: auto; padding-bottom: 4px; }
.heat { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 10px); grid-auto-columns: 10px; gap: 2px; }
.heat i { width: 10px; height: 10px; border-radius: 2px; background: var(--secondary-bg); }
.heat i.l1 { background: color-mix(in srgb, var(--accent) 35%, var(--secondary-bg)); }
.heat i.l2 { background: color-mix(in srgb, var(--accent) 65%, var(--secondary-bg)); }
.heat i.l3 { background: var(--accent); }
