/* Цвета берутся из темы Telegram (--tg-theme-*), запасные значения — для браузера. */
:root {
  --fb-bg: #f1f2f6;
  --fb-card: #ffffff;
  --fb-text: #17181c;
  --fb-hint: #8b909a;
  --fb-accent: #2779a7;
  --fb-accent-text: #ffffff;
  --fb-danger: #e5484d;

  --bg: var(--tg-theme-secondary-bg-color, var(--fb-bg));
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, var(--fb-card)));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --accent: var(--tg-theme-button-color, var(--fb-accent));
  --accent-text: var(--tg-theme-button-text-color, var(--fb-accent-text));
  --link: var(--tg-theme-link-color, var(--accent));
  --danger: var(--tg-theme-destructive-text-color, var(--fb-danger));
  --soft: rgba(128, 132, 145, .14);
  --sep: rgba(128, 132, 145, .2);
  --accent-soft: rgba(39, 121, 167, .12);
  --safe-bottom: max(env(safe-area-inset-bottom, 0px), var(--tg-safe-area-inset-bottom, 0px));
  --radius: 16px;
  color-scheme: light dark;
}
@supports (color: color-mix(in srgb, red, blue)) {
  :root { --accent-soft: color-mix(in srgb, var(--accent) 13%, transparent); }
}
@media (prefers-color-scheme: dark) {
  :root {
    --fb-bg: #0e0f13;
    --fb-card: #1b1c22;
    --fb-text: #f1f2f5;
    --fb-hint: #8c8f99;
    --fb-accent: #4aa3dc;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  overscroll-behavior-y: none;
}
body.locked { overflow: hidden; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; margin: 0; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }
svg { display: block; flex: none; }
[hidden] { display: none !important; }

.app { max-width: 560px; margin: 0 auto; padding-bottom: calc(110px + var(--safe-bottom)); }

/* ---------- шапка ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px 6px;
  background: var(--bg);
}
.icon-btn {
  width: 38px; height: 38px; border-radius: 12px;
  display: grid; place-items: center;
  background: var(--card); color: var(--hint);
}
.icon-btn svg { width: 20px; height: 20px; }
.month-title {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 21px; font-weight: 700; letter-spacing: -.01em;
  margin-right: auto;
}
.month-title svg { width: 18px; height: 18px; color: var(--hint); }
.pill {
  height: 32px; padding: 0 14px; border-radius: 16px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 14px; font-weight: 600;
}

/* ---------- неделя ---------- */
.week {
  display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px;
  padding: 6px 12px 10px;
  position: sticky; top: 54px; z-index: 4; background: var(--bg);
  touch-action: pan-y;
}
.day {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 7px 0 6px; border-radius: 14px;
  transition: background .15s, color .15s;
}
.day .dow { font-size: 12px; color: var(--hint); font-weight: 500; }
.day .num { font-size: 18px; font-weight: 600; line-height: 1.15; }
.day .dot { width: 5px; height: 5px; border-radius: 50%; background: transparent; }
.day .dot.todo { background: var(--accent); }
.day .dot.done { background: #22c55e; }
.day.today .num { color: var(--accent); }
.day.sel { background: var(--accent); color: var(--accent-text); }
.day.sel .dow { color: var(--accent-text); opacity: .8; }
.day.sel .num { color: var(--accent-text); }
.day.sel .dot.todo, .day.sel .dot.done { background: var(--accent-text); }
.week.slide-l { animation: slideL .22s ease; }
.week.slide-r { animation: slideR .22s ease; }
@keyframes slideL { from { transform: translateX(24px); opacity: .3; } }
@keyframes slideR { from { transform: translateX(-24px); opacity: .3; } }

/* ---------- списки ---------- */
.section { margin: 0 16px; }
.section-head {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 16px 4px 8px;
  font-size: 13px; font-weight: 600; color: var(--hint);
  text-transform: uppercase; letter-spacing: .04em;
}
.section-head .count { text-transform: none; letter-spacing: 0; font-weight: 500; }
.section-head.danger { color: var(--danger); }
.card { background: var(--card); border-radius: var(--radius); overflow: hidden; }

.row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; position: relative;
  min-height: 54px;
}
.row + .row::before {
  content: ""; position: absolute; top: 0; left: 52px; right: 0;
  height: 1px; background: var(--sep); transform: scaleY(.6);
}
.row-main { flex: 1; min-width: 0; }
.row-title { font-size: 16px; word-break: break-word; }
.row-meta {
  display: flex; flex-wrap: wrap; gap: 3px 10px; margin-top: 3px;
  font-size: 13px; color: var(--hint);
}
.meta { display: inline-flex; align-items: center; gap: 4px; }
.meta svg { width: 13px; height: 13px; }
.meta.late { color: var(--danger); }
.row.done .row-title { color: var(--hint); text-decoration: line-through; }
.row.done .row-meta { opacity: .7; }

.check {
  width: 26px; height: 26px; border-radius: 50%; flex: none;
  border: 2px solid var(--c, var(--accent));
  display: grid; place-items: center; color: #fff;
  transition: background .15s, transform .15s;
}
.check svg { width: 15px; height: 15px; opacity: 0; transform: scale(.5); transition: .15s; }
.check.on { background: var(--c, var(--accent)); }
.check.on svg { opacity: 1; transform: none; }
.check:active { transform: scale(.88); }

.small-btn {
  height: 30px; padding: 0 12px; border-radius: 15px; flex: none;
  background: var(--accent-soft); color: var(--accent);
  font-size: 13px; font-weight: 600;
}

.done-toggle {
  display: flex; align-items: center; gap: 6px;
  padding: 14px 4px 8px; font-size: 13px; font-weight: 600; color: var(--hint);
  text-transform: uppercase; letter-spacing: .04em;
}
.done-toggle svg { width: 14px; height: 14px; transition: transform .2s; }
.done-toggle.open svg { transform: rotate(180deg); }

/* ---------- привычки ---------- */
.habits {
  display: flex; gap: 10px; overflow-x: auto;
  padding: 0 16px 2px; margin: 0 -16px;
  scroll-snap-type: x proximity; scroll-padding: 0 16px; scrollbar-width: none;
}
.habits::-webkit-scrollbar { display: none; }
.habit {
  flex: 0 0 108px; scroll-snap-align: start;
  background: var(--card); border-radius: var(--radius);
  padding: 12px 8px 10px;
  display: flex; flex-direction: column; align-items: center; gap: 7px;
  text-align: center;
}
.habit-ring {
  width: 54px; height: 54px; border-radius: 50%;
  border: 3px solid var(--c); display: grid; place-items: center;
  font-size: 22px; font-weight: 700; color: var(--c);
  transition: background .18s, transform .15s;
  position: relative;
}
.habit-ring svg { width: 26px; height: 26px; color: #fff; }
.habit.done .habit-ring { background: var(--c); }
.habit-ring:active { transform: scale(.9); }
.habit-title {
  font-size: 13px; font-weight: 500; line-height: 1.25;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  min-height: 2.5em; word-break: break-word;
}
.habit-streak { font-size: 12px; color: var(--hint); min-height: 15px; }

/* ---------- баннер / пусто / загрузка ---------- */
.banner {
  margin: 10px 16px 0; padding: 12px 14px;
  border-radius: var(--radius); background: var(--accent-soft);
  display: flex; align-items: center; gap: 12px;
  font-size: 14px;
}
.banner b { display: block; font-size: 15px; margin-bottom: 2px; }
.banner .small-btn { background: var(--accent); color: var(--accent-text); }
.empty { padding: 56px 32px 24px; text-align: center; color: var(--hint); }
.empty svg { width: 84px; height: 84px; margin: 0 auto 14px; color: var(--accent); opacity: .85; }
.empty b { display: block; color: var(--text); font-size: 17px; margin-bottom: 4px; }
.loading { display: grid; place-items: center; padding: 80px 0; }
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--soft); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.fatal { padding: 80px 32px; text-align: center; color: var(--hint); }
.fatal b { display: block; font-size: 18px; color: var(--text); margin-bottom: 6px; }

/* ---------- кнопка добавления ---------- */
.fab {
  position: fixed; z-index: 10;
  right: max(18px, calc(50% - 280px + 18px));
  bottom: calc(22px + var(--safe-bottom));
  width: 58px; height: 58px; border-radius: 29px;
  background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
  transition: transform .15s;
}
.fab svg { width: 28px; height: 28px; }
.fab:active { transform: scale(.92); }

/* ---------- шторки ---------- */
.backdrop {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0, 0, 0, .42);
  opacity: 0; pointer-events: none; transition: opacity .25s;
}
.backdrop.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  max-width: 560px; margin: 0 auto;
  background: var(--bg); border-radius: 20px 20px 0 0;
  max-height: 92vh; display: flex; flex-direction: column;
  transform: translateY(105%); transition: transform .3s cubic-bezier(.2, .8, .2, 1);
  visibility: hidden;
}
.sheet.open { transform: none; visibility: visible; }
.sheet-full { height: 94vh; }
.sheet-head {
  display: grid; grid-template-columns: 84px 1fr 84px; align-items: center;
  padding: 14px 12px 8px; flex: none;
}
.sheet-head h2 { margin: 0; font-size: 17px; font-weight: 600; text-align: center; }
.link-btn { color: var(--link); font-size: 16px; justify-self: start; padding: 4px; }
.link-btn.end { justify-self: end; font-weight: 600; }
.sheet-body { overflow-y: auto; padding: 4px 16px 20px; flex: 1; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.sheet-foot { padding: 10px 16px calc(12px + var(--safe-bottom)); flex: none; }

.btn-primary {
  width: 100%; height: 50px; border-radius: 14px;
  background: var(--accent); color: var(--accent-text);
  font-size: 16px; font-weight: 600;
}
.btn-primary:disabled { opacity: .55; }
.btn-plain {
  width: 100%; padding: 14px; border-radius: 14px;
  background: var(--card); font-weight: 500;
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
.btn-plain.accent { color: var(--link); font-weight: 600; }
.btn-plain.danger { color: var(--danger); }
.btn-plain svg { width: 18px; height: 18px; }

/* ---------- форма ---------- */
.seg {
  display: grid; grid-template-columns: 1fr 1fr; gap: 2px;
  background: var(--soft); border-radius: 12px; padding: 3px;
  margin-bottom: 12px;
}
.seg button { height: 34px; border-radius: 9px; font-size: 14px; font-weight: 600; color: var(--hint); }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .1); }
.input-card { background: var(--card); border-radius: var(--radius); }
.title-input, .note-input {
  width: 100%; border: 0; outline: none; background: transparent; display: block;
  padding: 14px 14px 6px; font-size: 18px; font-weight: 600;
}
.note-input { font-size: 15px; font-weight: 400; padding: 6px 14px 14px; resize: none; min-height: 44px; color: var(--text); }
.title-input::placeholder, .note-input::placeholder { color: var(--hint); font-weight: 400; }
.shake { animation: shake .35s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

.colors { display: flex; gap: 12px; padding: 14px 4px 4px; flex-wrap: wrap; }
.color { width: 30px; height: 30px; border-radius: 50%; background: var(--c); transition: box-shadow .15s; }
.color.on { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--c); }

.group-label {
  padding: 18px 4px 8px; font-size: 13px; font-weight: 600; color: var(--hint);
  text-transform: uppercase; letter-spacing: .04em;
}
.field {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 14px; min-height: 50px; position: relative;
}
.field + .field::before, .card > * + .chips::before {
  content: ""; position: absolute; top: 0; left: 14px; right: 0; height: 1px; background: var(--sep); transform: scaleY(.6);
}
.field-label { font-size: 16px; }
.field-label small { display: block; font-size: 12px; color: var(--hint); }
.field-control { display: flex; align-items: center; gap: 6px; }
.ctl {
  -webkit-appearance: none; appearance: none;
  border: 0; outline: none; border-radius: 9px;
  background: var(--soft); padding: 7px 10px; font-size: 15px; min-height: 34px;
  min-width: 0; text-align: center;
}
select.ctl { padding-right: 26px; background-image: linear-gradient(45deg, transparent 50%, var(--hint) 50%), linear-gradient(135deg, var(--hint) 50%, transparent 50%);
  background-position: calc(100% - 14px) 50%, calc(100% - 9px) 50%; background-size: 5px 5px; background-repeat: no-repeat; text-align: left; }
.clear-btn { width: 26px; height: 26px; border-radius: 13px; background: var(--soft); color: var(--hint); display: grid; place-items: center; }
.clear-btn svg { width: 12px; height: 12px; }
.ghost { color: var(--link); font-size: 15px; font-weight: 500; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 14px; position: relative; }
.chip {
  height: 34px; padding: 0 14px; border-radius: 17px;
  background: var(--soft); font-size: 14px; font-weight: 500;
  transition: background .15s, color .15s;
}
.chip.on { background: var(--accent); color: var(--accent-text); }
.weekdays { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; padding: 0 14px 12px; }
.wd { height: 38px; border-radius: 11px; background: var(--soft); font-size: 14px; font-weight: 600; }
.wd.on { background: var(--accent); color: var(--accent-text); }

.stepper { display: inline-flex; align-items: center; background: var(--soft); border-radius: 10px; }
.stepper button { width: 38px; height: 34px; font-size: 21px; color: var(--accent); font-weight: 500; }
.stepper button:disabled { color: var(--hint); opacity: .5; }
.stepper span { min-width: 26px; text-align: center; font-weight: 600; font-variant-numeric: tabular-nums; }

.rem-card { background: var(--card); border-radius: var(--radius); margin-bottom: 10px; overflow: hidden; }
.rem-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px 2px; font-size: 13px; color: var(--hint); font-weight: 600;
}
.rem-head .clear-btn { width: 24px; height: 24px; }
.rem-summary {
  margin: 2px 14px 12px; padding: 9px 12px; border-radius: 11px;
  background: var(--accent-soft); color: var(--text); font-size: 13px; line-height: 1.4;
}
.rem-summary b { font-weight: 600; }
.hint-text { padding: 6px 4px 0; font-size: 13px; color: var(--hint); }
.warn {
  padding: 10px 12px; border-radius: 12px; margin-bottom: 10px;
  background: rgba(245, 158, 11, .14); font-size: 13px;
}

/* ---------- календарь ---------- */
.cal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 6px; }
.cal-head h2 { margin: 0; font-size: 17px; font-weight: 600; }
.cal-nav { width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; color: var(--accent); }
.cal-nav svg { width: 20px; height: 20px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; padding: 4px 12px 8px; }
.cal-dow { text-align: center; font-size: 12px; color: var(--hint); padding: 4px 0; font-weight: 500; }
.cal-day {
  aspect-ratio: 1 / 1; border-radius: 12px; font-size: 16px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
}
.cal-day.out { opacity: .35; }
.cal-day.today { color: var(--accent); font-weight: 700; }
.cal-day.sel { background: var(--accent); color: var(--accent-text); }
.cal-day .dot { width: 5px; height: 5px; border-radius: 50%; }
.cal-day .dot.todo { background: var(--accent); }
.cal-day .dot.done { background: #22c55e; }
.cal-day.sel .dot { background: var(--accent-text); }

/* ---------- меню добавления ---------- */
.add-options { display: grid; gap: 10px; padding: 4px 16px 8px; }
.add-opt {
  display: flex; align-items: center; gap: 14px; text-align: left;
  background: var(--card); border-radius: var(--radius); padding: 14px;
}
.add-opt .ico { width: 44px; height: 44px; border-radius: 13px; display: grid; place-items: center; color: #fff; flex: none; }
.add-opt .ico svg { width: 22px; height: 22px; }
.add-opt b { display: block; font-size: 16px; font-weight: 600; color: var(--text); }
.add-opt .sub { font-size: 13px; color: var(--hint); }

/* ---------- уведомление ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-bottom)); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); /* инверсия — читается в любой теме */
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
  padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 500; max-width: 86vw; text-align: center;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
