/* Цвета берутся из темы Telegram (переменные --tg-theme-*), запасные — для браузера. */
:root {
  --fb-bg: #efeff4;
  --fb-section: #ffffff;
  --fb-text: #000000;
  --fb-hint: #8e8e93;
  --fb-link: #2481cc;
  --fb-button: #2481cc;
  --fb-button-text: #ffffff;
  --fb-header: #6d6d72;
  --fb-danger: #e53935;
  --fb-separator: rgba(60, 60, 67, 0.12);
}

@media (prefers-color-scheme: dark) {
  :root {
    --fb-bg: #0f0f0f;
    --fb-section: #1c1c1e;
    --fb-text: #ffffff;
    --fb-hint: #8d8e93;
    --fb-link: #62bcf9;
    --fb-button: #3e88f7;
    --fb-header: #8d8e93;
    --fb-danger: #ff5a52;
    --fb-separator: rgba(255, 255, 255, 0.1);
  }
}

:root {
  --bg: var(--tg-theme-secondary-bg-color, var(--fb-bg));
  --section: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, var(--fb-section)));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --link: var(--tg-theme-link-color, var(--fb-link));
  --accent: var(--tg-theme-accent-text-color, var(--link));
  --button: var(--tg-theme-button-color, var(--fb-button));
  --button-text: var(--tg-theme-button-text-color, var(--fb-button-text));
  --header: var(--tg-theme-section-header-text-color, var(--fb-header));
  --danger: var(--tg-theme-destructive-text-color, var(--fb-danger));
  --separator: var(--tg-theme-section-separator-color, var(--fb-separator));
  --ok: #2fa84f;
  --warn: #e0900b;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 12px 16px calc(32px + env(safe-area-inset-bottom));
  transition: opacity .15s;
}
main.busy { opacity: .55; pointer-events: none; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

/* --- Заголовки --- */
.page-title { font-size: 26px; font-weight: 700; margin: 8px 4px 2px; letter-spacing: -0.3px; }
.page-sub { color: var(--hint); margin: 0 4px 4px; font-size: 15px; }

/* --- Секции в стиле настроек Telegram --- */
.section { margin: 18px 0 0; }
.section-title {
  color: var(--header);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .3px;
  margin: 0 16px 6px;
}
.section-note { color: var(--hint); font-size: 13px; margin: 6px 16px 0; }
.card { background: var(--section); border-radius: var(--radius); overflow: hidden; }
.card.padded { padding: 14px 16px; }

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 46px;
  padding: 10px 16px;
  background: none;
  border: 0;
  text-align: left;
  position: relative;
}
.row + .row::before {
  content: "";
  position: absolute;
  top: 0; right: 0; left: 16px;
  border-top: 1px solid var(--separator);
}
.row.has-icon + .row::before, .row + .row.has-icon::before { left: 60px; }
button.row:active { background: var(--separator); }
.row .main { flex: 1; min-width: 0; }
.row .title { overflow-wrap: anywhere; }
.row .sub { color: var(--hint); font-size: 14px; margin-top: 1px; overflow-wrap: anywhere; }
.row .right { color: var(--hint); font-size: 15px; white-space: nowrap; }
.row .chevron { color: var(--hint); opacity: .6; font-size: 22px; line-height: 1; margin-left: -4px; }
.row.accent .title { color: var(--accent); }
.row.danger .title { color: var(--danger); }

.icon {
  width: 32px; height: 32px;
  border-radius: 9px;
  display: grid; place-items: center;
  color: #fff; font-size: 16px; font-weight: 600;
  flex: none;
}
.avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  display: grid; place-items: center;
  color: #fff; font-weight: 600; font-size: 16px;
  flex: none;
}
.row.has-avatar + .row::before, .row + .row.has-avatar::before { left: 68px; }

/* --- Значки --- */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  background: var(--separator);
  color: var(--hint);
}
.badge.debt { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.badge.ok { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.badge.warn { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); }

/* --- Кнопки --- */
.btn {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 10px;
  padding: 13px 16px;
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
  font-size: 16px;
  text-align: center;
}
.btn:active { filter: brightness(.92); }
.btn:disabled { opacity: .5; }
.btn.secondary { background: var(--section); color: var(--link); }
.btn.danger { background: var(--section); color: var(--danger); }
.btn.small { padding: 8px 10px; font-size: 14px; border-radius: 9px; }
.btn.tonal { background: color-mix(in srgb, var(--button) 13%, transparent); color: var(--link); }
.buttons { display: flex; gap: 8px; margin-top: 14px; }
.buttons > * { flex: 1; }
.stack { display: grid; gap: 8px; margin-top: 14px; }

/* --- Главная карточка экрана --- */
.hero { background: var(--section); border-radius: var(--radius); padding: 16px; margin-top: 8px; }
.hero .label { color: var(--hint); font-size: 14px; }
.hero .big { font-size: 32px; font-weight: 700; letter-spacing: -0.5px; margin: 2px 0; }
.hero .big.debt { color: var(--danger); }
.hero .big.ok { color: var(--ok); }
.hero .title { font-size: 20px; font-weight: 700; }
.hero .meta { color: var(--hint); font-size: 14px; margin-top: 4px; }

/* --- Плитки «ребёнок» и «родитель» --- */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.tile {
  background: var(--section);
  border: 0;
  border-radius: var(--radius);
  padding: 14px;
  text-align: left;
  min-height: 118px;
  display: flex; flex-direction: column; gap: 4px;
}
.tile:active { filter: brightness(.95); }
.tile .icon { margin-bottom: 6px; }
.tile .name { font-weight: 600; font-size: 17px; }
.tile .who { color: var(--hint); font-size: 13px; }
.tile .what { color: var(--hint); font-size: 13px; margin-top: auto; }

/* --- Урок с действиями --- */
.lesson { padding: 12px 16px; position: relative; }
.lesson + .lesson::before { content: ""; position: absolute; top: 0; left: 16px; right: 0; border-top: 1px solid var(--separator); }
.lesson .when { font-weight: 600; }
.lesson .state { color: var(--hint); font-size: 14px; margin-top: 2px; }
.lesson .state.ok { color: var(--ok); }
.lesson .state.warn { color: var(--warn); }
.lesson .buttons { margin-top: 10px; }

/* --- Выбор дня и времени --- */
.days { display: flex; gap: 6px; overflow-x: auto; padding: 10px 12px; scrollbar-width: none; scroll-snap-type: x proximity; }
.days::-webkit-scrollbar { display: none; }
.day {
  flex: none;
  width: 48px;
  padding: 7px 0 8px;
  border: 0;
  border-radius: 10px;
  background: var(--bg);
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  scroll-snap-align: start;
}
.day .wd { font-size: 12px; color: var(--hint); }
.day .num { font-size: 18px; font-weight: 600; }
.day .dot { width: 5px; height: 5px; border-radius: 50%; background: var(--hint); opacity: .7; }
.day .dot.none { visibility: hidden; }
.day.active { background: var(--button); color: var(--button-text); }
.day.active .wd { color: var(--button-text); opacity: .8; }
.day.active .dot { background: var(--button-text); }

.chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; padding: 12px; }
.chip {
  border: 0;
  border-radius: 10px;
  background: var(--bg);
  padding: 9px 4px;
  text-align: center;
  font-weight: 500;
  display: flex; flex-direction: column; align-items: center;
}
.chip small { font-size: 11px; color: var(--warn); font-weight: 500; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.active { background: var(--button); color: var(--button-text); }
.chip.active small { color: var(--button-text); }
.chip:disabled { opacity: .35; }
.chip.wide { grid-column: span 2; }

.summary { text-align: center; color: var(--hint); font-size: 14px; margin-top: 14px; }
.summary b { color: var(--text); font-weight: 600; }

/* --- Формы --- */
.field { padding: 10px 16px; position: relative; display: block; }
.field + .field::before { content: ""; position: absolute; top: 0; left: 16px; right: 0; border-top: 1px solid var(--separator); }
.field .label { display: block; color: var(--hint); font-size: 13px; margin-bottom: 3px; }
.input {
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  padding: 2px 0;
  font-size: 17px;
  border-radius: 0;
  -webkit-appearance: none;
  appearance: none;
}
textarea.input { resize: vertical; min-height: 84px; line-height: 1.4; }
select.input { background: transparent; }
.input::placeholder { color: var(--hint); opacity: .7; }
.inline-inputs { display: flex; align-items: center; gap: 10px; }
.inline-inputs select { width: auto; flex: 1; }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; }
.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; border-radius: 31px; background: var(--separator); transition: background .2s; }
.switch span::after { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.2); transition: transform .2s; }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { transform: translateX(20px); }

/* --- Прочее --- */
.text-block { padding: 12px 16px; white-space: pre-wrap; overflow-wrap: anywhere; }
.link-box {
  font: 14px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--bg);
  border-radius: 8px;
  padding: 9px 10px;
  margin-top: 8px;
  overflow-wrap: anywhere;
  user-select: all;
  -webkit-user-select: all;
}
.amount-plus { color: var(--ok); font-weight: 600; }
.amount-minus { color: var(--text); }
.empty { text-align: center; color: var(--hint); padding: 22px 16px; font-size: 15px; }
.welcome { text-align: center; padding: 28px 8px 6px; }
.welcome .logo {
  width: 84px; height: 84px; margin: 0 auto 14px;
  border-radius: 24px;
  background: linear-gradient(135deg, #5aa7f0, #7b6cf0);
  color: #fff; display: grid; place-items: center;
  font-size: 40px; font-weight: 800;
}
.welcome h1 { margin: 0 0 6px; font-size: 26px; }
.welcome p { margin: 0 auto; color: var(--hint); max-width: 340px; }

.loader {
  width: 28px; height: 28px;
  margin: 30vh auto 0;
  border-radius: 50%;
  border: 3px solid var(--separator);
  border-top-color: var(--button);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  background: rgba(30, 30, 30, .92);
  color: #fff;
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 15px;
  text-align: center;
  z-index: 10;
  box-shadow: 0 4px 16px rgba(0,0,0,.2);
}
.error-box { text-align: center; padding: 30vh 16px 0; color: var(--hint); }
.error-box .btn { margin-top: 16px; }
