:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --text: var(--tg-theme-text-color, #111);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --link: var(--tg-theme-link-color, #2a7ae2);
  --btn: var(--tg-theme-button-color, #2a7ae2);
  --btn-text: var(--tg-theme-button-text-color, #fff);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --sep: color-mix(in srgb, var(--hint) 25%, transparent);
  --done: #30a46c;
  --progress: #f5a524;
  --todo: #9ba1a6;
  --overdue: #e5484d;
  --current: #2a7ae2;
  --radius: 12px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg2); color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
button { font: inherit; color: inherit; }
input, textarea, select { font: inherit; color: var(--text); background: var(--bg2); border: 1px solid transparent;
  border-radius: 10px; padding: 10px 12px; width: 100%; outline: none; }
input:focus, textarea:focus { border-color: var(--btn); }
textarea { resize: vertical; min-height: 70px; }
.hidden { display: none !important; }
.muted { color: var(--hint); }
.center { text-align: center; padding: 40px 16px; }
.small { font-size: 13px; }

/* ---- layout ---- */
.wrap { max-width: 760px; margin: 0 auto; padding: 8px 12px 90px; }
.wrap.wide { max-width: 1200px; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 2px 6px; }
.topbar h1 { font-size: 20px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 4px 0 10px; scrollbar-width: none; }
.chip { border: 0; background: var(--bg); padding: 6px 12px; border-radius: 16px; white-space: nowrap; cursor: pointer; }
.chip.on { background: var(--btn); color: var(--btn-text); }

.nav { position: fixed; left: 0; right: 0; bottom: 0; display: flex; background: var(--bg);
  border-top: 1px solid var(--sep); padding-bottom: env(safe-area-inset-bottom); z-index: 5; }
.nav button { flex: 1; border: 0; background: none; padding: 8px 0 10px; color: var(--hint); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 12px; }
.nav button .ic { font-size: 20px; line-height: 1; }
.nav button.on { color: var(--btn); }
@media (min-width: 820px) {
  .nav { position: sticky; top: 0; bottom: auto; border: 0; border-bottom: 1px solid var(--sep);
    justify-content: center; gap: 8px; }
  .nav button { flex: 0 0 auto; flex-direction: row; padding: 12px 18px; font-size: 15px; gap: 6px; }
  .nav button .ic { font-size: 16px; }
  .wrap { padding-bottom: 40px; }
  .fab { bottom: 24px !important; }
}

/* ---- список задач ---- */
.card { background: var(--bg); border-radius: var(--radius); overflow: hidden; }
.card + .card { margin-top: 10px; }
.group-title { font-size: 13px; color: var(--hint); text-transform: uppercase; letter-spacing: .03em;
  margin: 14px 4px 6px; }
.task > .row { display: flex; align-items: center; gap: 10px; padding: 11px 12px; cursor: pointer; min-height: 48px; }
.task + .task > .row, .task > .kids > .task > .row { border-top: 1px solid var(--sep); }
.task .body { flex: 1; min-width: 0; }
.task .title { word-break: break-word; }
.task.done > .row .title { text-decoration: line-through; color: var(--hint); }
.task .meta { font-size: 12px; color: var(--hint); margin-top: 2px; display: flex; gap: 8px; flex-wrap: wrap; }
.task .date { font-size: 13px; color: var(--hint); white-space: nowrap; }
.date.today { color: var(--btn); font-weight: 600; }
.date.overdue { color: var(--overdue); font-weight: 600; }
.task > .kids { padding-left: 26px; background: color-mix(in srgb, var(--bg2) 40%, var(--bg)); }
.task > .kids .row { padding-top: 9px; padding-bottom: 9px; min-height: 42px; }
.caret { color: var(--hint); font-size: 11px; transition: transform .15s; width: 10px; }
.task.open > .row .caret { transform: rotate(90deg); }
.actions { display: flex; gap: 4px; padding: 6px 8px 8px; border-top: 1px solid var(--sep); }
.actions button { border: 0; background: none; color: var(--link); padding: 6px 8px; cursor: pointer; font-size: 14px; }

.chk { flex: 0 0 auto; width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--hint);
  display: grid; place-items: center; cursor: pointer; background: none; padding: 0; }
.chk.on { background: var(--done); border-color: var(--done); }
.chk.on::after { content: ""; width: 6px; height: 11px; border: solid #fff; border-width: 0 2px 2px 0;
  transform: rotate(45deg) translate(-1px, -1px); }
.foreign-tag { background: var(--bg2); border-radius: 6px; padding: 0 6px; }

.fab { position: fixed; right: 18px; bottom: 76px; width: 54px; height: 54px; border-radius: 50%;
  border: 0; background: var(--btn); color: var(--btn-text); font-size: 28px; line-height: 1;
  box-shadow: 0 4px 14px rgba(0,0,0,.2); cursor: pointer; z-index: 6; }

/* ---- кнопки и формы ---- */
.btn { display: block; width: 100%; border: 0; border-radius: 10px; padding: 12px; background: var(--btn);
  color: var(--btn-text); font-weight: 600; cursor: pointer; }
.btn.sec { background: var(--bg2); color: var(--link); }
.btn.danger { background: none; color: var(--danger); }
.btn + .btn { margin-top: 8px; }
.field { margin-bottom: 10px; }
.field label { display: block; font-size: 13px; color: var(--hint); margin: 0 0 4px 4px; }
.row2 { display: flex; gap: 8px; }
.row2 > * { flex: 1; }
.linkbtn { border: 0; background: none; color: var(--link); cursor: pointer; padding: 4px; text-align: left; }

/* ---- шторка ---- */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; max-height: 92vh; overflow-y: auto; z-index: 21;
  background: var(--bg); border-radius: 16px 16px 0 0; padding: 14px 16px calc(16px + env(safe-area-inset-bottom));
  animation: up .18s ease-out; }
@media (min-width: 820px) {
  .sheet { left: 50%; right: auto; bottom: auto; top: 50%; transform: translate(-50%, -50%); width: 560px;
    border-radius: 16px; animation: none; max-height: 85vh; }
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet h2 { font-size: 17px; margin: 0 0 12px; display: flex; align-items: center; gap: 8px; }
.sheet h2 .x { margin-left: auto; border: 0; background: var(--bg2); width: 30px; height: 30px; border-radius: 50%; cursor: pointer; }
.title-input { font-size: 17px; font-weight: 600; }
.crumbs { font-size: 13px; color: var(--hint); margin-bottom: 8px; }
.crumbs a { color: var(--link); cursor: pointer; }

.comments { margin-top: 16px; }
.comment { background: var(--bg2); border-radius: 12px; padding: 8px 10px; margin-bottom: 6px; max-width: 90%; }
.comment.mine { margin-left: auto; background: color-mix(in srgb, var(--btn) 15%, var(--bg)); }
.comment .who { font-size: 12px; color: var(--hint); margin-bottom: 2px; }
.comment-form { display: flex; gap: 6px; margin-top: 8px; }
.comment-form textarea { min-height: 42px; height: 42px; }
.comment-form button { flex: 0 0 auto; width: auto; padding: 0 14px; }

/* ---- настройки ---- */
.set-item { display: flex; align-items: center; gap: 10px; padding: 12px; border-top: 1px solid var(--sep); }
.set-item:first-child { border-top: 0; }
.set-item .grow { flex: 1; min-width: 0; }
.switch { position: relative; width: 46px; height: 28px; flex: 0 0 auto; }
.switch input { display: none; }
.switch span { position: absolute; inset: 0; border-radius: 14px; background: var(--sep); transition: .2s; cursor: pointer; }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 22px; height: 22px; border-radius: 50%;
  background: #fff; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.switch input:checked + span { background: var(--done); }
.switch input:checked + span::after { transform: translateX(18px); }
.code { font-family: ui-monospace, monospace; background: var(--bg2); padding: 2px 8px; border-radius: 6px; }
.radio { width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--hint); flex: 0 0 auto; }
.radio.on { border: 6px solid var(--btn); }

/* ---- дерево ---- */
.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: 12px; color: var(--hint); margin: 0 4px 10px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.tree-card { background: var(--bg); border-radius: var(--radius); padding: 14px 10px; margin-bottom: 10px; overflow-x: auto; }
.tnode { display: flex; align-items: center; }
.tbox { flex: 0 0 auto; width: 170px; border-radius: 10px; padding: 8px 10px; cursor: pointer;
  background: var(--bg); border: 2px solid var(--todo); border-left-width: 6px; position: relative; }
.tbox .tt { font-size: 13px; font-weight: 600; word-break: break-word;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.tbox .td { font-size: 11px; color: var(--hint); margin-top: 3px; display: flex; justify-content: space-between; gap: 6px; }
.tbox .bar { height: 4px; border-radius: 2px; background: var(--sep); margin-top: 6px; overflow: hidden; }
.tbox .bar > div { height: 100%; background: var(--done); }
.tbox.s-done { border-color: var(--done); background: color-mix(in srgb, var(--done) 10%, var(--bg)); }
.tbox.s-done .tt { text-decoration: line-through; color: var(--hint); }
.tbox.s-progress { border-color: var(--progress); background: color-mix(in srgb, var(--progress) 10%, var(--bg)); }
.tbox.s-overdue { border-color: var(--overdue); background: color-mix(in srgb, var(--overdue) 10%, var(--bg)); }
.tbox.current { box-shadow: 0 0 0 3px var(--current); }
.tbox.current::before { content: "▶ здесь"; position: absolute; top: -10px; right: 8px; font-size: 10px; font-weight: 700;
  background: var(--current); color: #fff; padding: 1px 6px; border-radius: 6px; }
.tkids { display: flex; flex-direction: column; padding-left: 22px; position: relative; }
.tkids::before { content: ""; position: absolute; left: 0; top: 50%; width: 22px; border-top: 2px solid var(--sep); }
.tkids > .tnode { position: relative; padding: 6px 0 6px 16px; }
.tkids > .tnode::before { content: ""; position: absolute; left: 0; top: 50%; width: 16px; border-top: 2px solid var(--sep); }
.tkids > .tnode::after { content: ""; position: absolute; left: 0; top: 0; bottom: 0; border-left: 2px solid var(--sep); }
.tkids > .tnode:first-child::after { top: 50%; }
.tkids > .tnode:last-child::after { bottom: 50%; }
.tkids > .tnode:only-child::after { display: none; }
@media (max-width: 600px) {
  /* на телефоне — вертикальное дерево с отступами */
  .tree-card { padding: 12px; }
  .tnode { flex-direction: column; align-items: stretch; }
  .tbox { width: auto; }
  .tkids { padding-left: 16px; }
  .tkids::before { display: none; }
  .tkids > .tnode { padding: 10px 0 0 16px; }
  .tkids > .tnode::before { top: 32px; width: 16px; }
  .tkids > .tnode::after, .tkids > .tnode:first-child::after { top: 0; bottom: 0; }
  .tkids > .tnode:last-child::after { bottom: auto; height: 32px; }
  .tkids > .tnode:only-child::after { display: block; }
}

.toast { position: fixed; left: 50%; bottom: 140px; transform: translateX(-50%); background: rgba(0,0,0,.8); color: #fff;
  padding: 8px 14px; border-radius: 10px; z-index: 30; font-size: 14px; max-width: 90%; }
.empty { text-align: center; color: var(--hint); padding: 36px 16px; }
.onb { max-width: 420px; margin: 0 auto; padding: 32px 16px; }
.onb h1 { font-size: 22px; margin: 0 0 8px; }
.onb p { color: var(--hint); margin: 0 0 20px; }


/* Перетаскивание категорий */
.category-sortable {
  cursor: grab;
  user-select: none;
}
.category-drag-handle {
  display: inline-block;
  margin-right: 5px;
  cursor: grab;
  touch-action: none;
  user-select: none;
  opacity: .65;
}
.category-sortable.dragging {
  opacity: .45;
  outline: 2px dashed var(--btn);
}

/* Второстепенные фильтры задач */
.chips:has(.secondary-filter) {
  gap: 14px;
  padding: 2px 0 8px;
}

.secondary-filter.chip {
  background: transparent;
  color: var(--hint);
  font-size: 12px;
  font-weight: 400;
  padding: 5px 2px;
  border-radius: 0;
  opacity: .75;
}

.secondary-filter.chip.on {
  background: transparent;
  color: var(--text);
  font-weight: 500;
  opacity: 1;
  box-shadow: inset 0 -2px 0 var(--btn);
}

.secondary-filter.chip:hover {
  opacity: 1;
}

/* Ненавязчивая кнопка управления категориями */
.category-management-actions {
  padding-top: 0;
  padding-bottom: 6px;
}

.category-management-actions .subtle-action {
  background: transparent;
  color: var(--hint);
  font-size: 12px;
  font-weight: 400;
  padding: 4px 2px;
  border-radius: 0;
  opacity: .7;
}

.category-management-actions .subtle-action:hover {
  color: var(--btn);
  opacity: 1;
}
