/* Цвета берутся из темы Telegram (telegram-web-app.js выставляет --tg-theme-*).
   Значения после запятой — запасные для открытия в обычном браузере. */
:root {
  --bg: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --text: var(--tg-theme-text-color, #000000);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --sub: var(--tg-theme-subtitle-text-color, var(--hint));
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-accent-text-color, var(--tg-theme-link-color, #2481cc));
  --danger: var(--tg-theme-destructive-text-color, #e53935);
  --section-title: var(--tg-theme-section-header-text-color, var(--hint));
  --sep: var(--tg-theme-section-separator-color, rgba(128, 128, 128, 0.2));
  --radius: 14px;
  --radius-sm: 10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not(.tg) {
    --bg: #000000;
    --card: #1c1c1e;
    --text: #ffffff;
    --hint: #8e8e93;
    --accent: #3e88f7;
    --link: #3e88f7;
    --danger: #ff453a;
  }
}

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

html { font-size: var(--fs, 16px); }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }

#app { max-width: 560px; margin: 0 auto; padding: 8px 16px calc(96px + var(--safe-bottom)); }

/* ---------- шапка ---------- */
.top { display: flex; align-items: center; gap: 8px; min-height: 44px; margin: 4px 0 12px; }
.top h1 { font-size: 1.5rem; font-weight: 700; margin: 0; flex: 1; letter-spacing: -0.3px; }
.chip {
  font-size: 0.8125rem; color: var(--sub); background: var(--card);
  padding: 6px 10px; border-radius: 999px; white-space: nowrap;
  max-width: 50%; overflow: hidden; text-overflow: ellipsis;
}
.back-link { color: var(--link); font-size: 1.0625rem; padding: 6px 4px 6px 0; }
/* в Telegram назад ведёт системная кнопка */
.tg .back-link { display: none; }

/* ---------- переключатель Список / Календарь ---------- */
.segmented {
  position: relative; display: grid; grid-template-columns: 1fr 1fr;
  background: var(--card); border-radius: var(--radius-sm); padding: 3px; margin-bottom: 16px;
}
.seg { position: relative; z-index: 1; padding: 7px 0; font-size: 0.875rem; font-weight: 600; color: var(--hint); transition: color .2s; }
.seg.active { color: var(--accent-text); }
.seg-thumb {
  position: absolute; top: 3px; bottom: 3px; left: 3px; width: calc(50% - 3px);
  background: var(--accent); border-radius: calc(var(--radius-sm) - 2px); transition: transform .25s cubic-bezier(.3, .7, .4, 1);
}
.segmented.right .seg-thumb { transform: translateX(100%); }

/* ---------- карточки и списки ---------- */
.card { background: var(--card); border-radius: var(--radius); overflow: hidden; }
.section-title {
  font-size: 0.8125rem; text-transform: uppercase; letter-spacing: .3px; color: var(--section-title);
  margin: 20px 4px 8px; font-weight: 500;
}
.section-title:first-child { margin-top: 4px; }

.item {
  display: flex; align-items: center; gap: 14px; width: 100%; text-align: left;
  padding: 12px 14px; position: relative;
}
.item + .item::before {
  content: ""; position: absolute; top: 0; left: 78px; right: 0; height: 1px; background: var(--sep);
}
.item:active { background: rgba(128, 128, 128, .12); }
.item-time { width: 50px; flex: none; font-size: 1.0625rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.item-time small { display: block; font-size: 0.6875rem; font-weight: 500; color: var(--hint); }
.item-body { flex: 1; min-width: 0; }
.item-text { overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.item-meta { font-size: 0.8125rem; color: var(--link); margin-top: 2px; }
.item-chevron { color: var(--hint); opacity: .6; font-size: 1.25rem; }

.empty { text-align: center; padding: 48px 24px; color: var(--hint); }
.empty .emoji { font-size: 3.5rem; margin-bottom: 12px; }
.empty b { display: block; color: var(--text); font-size: 1.0625rem; margin-bottom: 6px; }
.empty-small { padding: 20px; text-align: center; color: var(--hint); font-size: 0.9375rem; }
.link-btn { color: var(--link); font-weight: 600; margin-top: 14px; }

/* ---------- календарь ---------- */
.calendar { padding: 8px 8px 10px; }
.cal-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 4px 8px; }
.cal-title { font-size: 1.0625rem; font-weight: 600; text-transform: capitalize; padding: 6px 10px; border-radius: 8px; }
.icon-btn { width: 36px; height: 36px; border-radius: 50%; font-size: 1.625rem; line-height: 1; color: var(--link); }
.icon-btn:active, .cal-title:active { background: rgba(128, 128, 128, .15); }
.cal-weekdays, .cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); text-align: center; }
.cal-weekdays span { font-size: 0.75rem; color: var(--hint); padding-bottom: 6px; font-weight: 500; }
.cal-weekdays span.we { color: var(--danger); opacity: .75; }
.day {
  position: relative; aspect-ratio: 1; width: 100%; max-width: 44px; margin: 1px auto;
  display: flex; align-items: center; justify-content: center; border-radius: 50%;
  font-size: 1rem; font-variant-numeric: tabular-nums; transition: background .15s;
}
.day.other { color: var(--hint); opacity: .45; }
.day.past:not(.today):not(.selected) { color: var(--hint); }
.day.today { color: var(--link); font-weight: 700; }
.day.selected { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.day .dots { position: absolute; bottom: 12%; display: flex; gap: 2px; }
.day .dots i { width: 4px; height: 4px; border-radius: 50%; background: var(--accent); }
.day.selected .dots i { background: var(--accent-text); }

/* ---------- редактор ---------- */
textarea {
  display: block; width: 100%; border: 0; outline: none; resize: none; background: transparent;
  color: var(--text); font: inherit; font-size: 1.0625rem; padding: 14px;
}
textarea::placeholder { color: var(--hint); }

.quick { display: flex; gap: 8px; overflow-x: auto; margin: 12px -16px 0; padding: 0 16px; scrollbar-width: none; }
.quick::-webkit-scrollbar { display: none; }
.quick button {
  flex: none; background: var(--card); color: var(--link); font-size: 0.875rem; font-weight: 500;
  padding: 8px 12px; border-radius: 999px;
}
.quick button:active { opacity: .6; }

.rows .row {
  position: relative; display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 6px 14px;
}
.rows .row + .row::before {
  content: ""; position: absolute; top: 0; left: 56px; right: 0; height: 1px; background: var(--sep);
}
.row-icon {
  width: 30px; height: 30px; border-radius: 8px; flex: none; display: grid; place-items: center;
  font-size: 1rem; background: color-mix(in srgb, var(--c) 18%, transparent);
}
.row-label { flex: 1; }
.row-value { color: var(--hint); text-align: right; }
.row:not(.custom) .row-value::after { content: " ›"; opacity: .6; }
/* нативный пикер растянут поверх строки и невидим — тап открывает системный выбор даты/времени */
.overlay-input {
  position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer;
  -webkit-appearance: none; appearance: none; border: 0; font-size: 1rem;
}
.row.custom { gap: 10px; }
.row.custom input, .row.custom select {
  font: inherit; color: var(--text); background: rgba(128, 128, 128, .12);
  border: 0; border-radius: 8px; padding: 6px 10px; outline: none;
}
.row.custom input { width: 72px; text-align: center; }

.hint { color: var(--hint); font-size: 0.875rem; margin: 8px 4px 0; }
.hint.warn { color: var(--danger); }
.danger-btn {
  display: block; width: 100%; margin-top: 28px; padding: 14px; border-radius: var(--radius);
  background: var(--card); color: var(--danger); font-size: 1.0625rem;
}
.danger-btn:active { opacity: .6; }

/* ---------- вне Telegram: своя «главная кнопка» ---------- */
.fallback-main {
  position: fixed; left: 16px; right: 16px; bottom: calc(16px + var(--safe-bottom)); max-width: 528px; margin: 0 auto;
  padding: 15px; border-radius: var(--radius-sm); background: var(--accent); color: var(--accent-text);
  font-weight: 600; font-size: 1rem; box-shadow: 0 6px 20px rgba(0, 0, 0, .15);
}
.fallback-main:disabled { opacity: .5; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%);
  background: rgba(30, 30, 30, .92); color: #fff; padding: 10px 16px; border-radius: 12px;
  font-size: 0.875rem; max-width: calc(100% - 32px); text-align: center; z-index: 10;
  animation: toast-in .2s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }

.view { animation: view-in .22s ease-out; }
@keyframes view-in { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) { .view, .toast, .seg-thumb { animation: none; transition: none; } }

/* ---------- компактный список ---------- */
.compact .item { padding-top: 7px; padding-bottom: 7px; }
.compact .item-text { -webkit-line-clamp: 1; }
.compact .item-meta { font-size: 0.75rem; margin-top: 0; }
.compact .section-title { margin-top: 14px; }

/* ---------- настройки ---------- */
.chip { display: inline-flex; align-items: center; gap: 6px; }
.chip svg { flex: none; }

.preview .preview-actions { display: flex; align-items: center; gap: 10px; padding: 10px 14px 14px; flex-wrap: wrap; }
.preview-chip { color: var(--link); background: var(--bg); padding: 6px 12px; border-radius: 999px; font-size: 0.875rem; font-weight: 500; }
.preview-button {
  flex: 1; text-align: center; background: var(--accent); color: var(--accent-text); font-weight: 600;
  padding: 10px; border-radius: var(--radius-sm); font-size: 0.9375rem; min-width: 160px;
}

.themes { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.theme {
  position: relative; display: flex; flex-direction: column; align-items: stretch; gap: 6px;
  padding: 8px; border-radius: var(--radius); background: var(--card); text-align: center;
  font-size: 0.8125rem; font-weight: 500; outline: 2px solid transparent; outline-offset: -2px;
}
.theme[aria-checked="true"] { outline-color: var(--accent); }
.theme-swatch {
  height: 54px; border-radius: calc(var(--radius) - 6px); background: var(--sw-bg); padding: 8px;
  display: flex; flex-direction: column; gap: 5px; justify-content: center; border: 1px solid var(--sep);
}
.theme-swatch i { display: block; height: 8px; border-radius: 4px; background: var(--sw-card); }
.theme-swatch i:first-child { width: 70%; }
.theme-swatch b { display: block; height: 12px; width: 50%; border-radius: 6px; background: var(--sw-accent); align-self: flex-end; }

.accents { display: flex; flex-wrap: wrap; gap: 12px; padding: 14px; }
.accent {
  width: 36px; height: 36px; border-radius: 50%; background: var(--sw); position: relative;
  display: grid; place-items: center; color: #fff; font-size: 0.875rem; font-weight: 700;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .08);
}
.accent[aria-checked="true"]::after {
  content: ""; position: absolute; inset: -4px; border-radius: 50%; border: 2px solid var(--sw);
}
.accent.auto { background: conic-gradient(#ff3b30, #ff9500, #ffcc00, #34c759, #007aff, #af52de, #ff3b30); }
.accent.custom { overflow: hidden; background: var(--sw, var(--bg)); color: var(--hint); }
.accent.custom input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }

.mini-seg { display: inline-flex; background: rgba(128, 128, 128, .14); border-radius: 9px; padding: 2px; flex: none; }
.mini-seg button { padding: 5px 11px; border-radius: 7px; font-size: 0.875rem; font-weight: 500; color: var(--text); min-width: 38px; }
.mini-seg button[aria-pressed="true"] { background: var(--card); color: var(--link); box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }

.switch {
  -webkit-appearance: none; appearance: none; width: 51px; height: 31px; border-radius: 999px; flex: none;
  background: rgba(128, 128, 128, .3); position: relative; cursor: pointer; transition: background .2s; margin: 0;
}
.switch::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:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(20px); }
.reset-btn { display: block; margin: 12px auto 0; font-weight: 500; font-size: 0.9375rem; }

/* ---------- круговой выбор времени ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .4);
  display: flex; align-items: flex-end; justify-content: center; animation: fade-in .2s ease-out;
}
.sheet {
  width: 100%; max-width: 560px; background: var(--card); color: var(--text);
  border-radius: calc(var(--radius) + 6px) calc(var(--radius) + 6px) 0 0;
  padding: 20px 20px calc(16px + var(--safe-bottom)); animation: sheet-up .28s cubic-bezier(.2, .8, .3, 1);
}
@keyframes fade-in { from { opacity: 0; } }
@keyframes sheet-up { from { transform: translateY(100%); } }
.dial-display { display: flex; justify-content: center; align-items: center; gap: 4px; }
.dial-part {
  font-size: 3.25rem; font-weight: 300; font-variant-numeric: tabular-nums; line-height: 1.1;
  padding: 2px 12px; border-radius: var(--radius-sm); color: var(--text); background: rgba(128, 128, 128, .12);
}
.dial-part.active { color: var(--accent-text); background: var(--accent); }
.dial-colon { font-size: 3rem; font-weight: 300; }
.dial-hint { text-align: center; color: var(--hint); font-size: 0.875rem; margin: 8px 0 4px; }
.dial { display: block; width: min(100%, 300px); margin: 0 auto; touch-action: none; user-select: none; outline: none; cursor: pointer; }
.dial .face { fill: rgba(128, 128, 128, .12); }
.dial .hand { stroke: var(--accent); stroke-width: 2; }
.dial .knob { fill: var(--accent); }
.dial .knob-dot { fill: var(--accent-text); }
.dial .center { fill: var(--accent); }
.dial text { fill: var(--text); font-size: 15px; text-anchor: middle; dominant-baseline: central; font-variant-numeric: tabular-nums; }
.dial text.inner { font-size: 12px; fill: var(--hint); }
.dial text.on { fill: var(--accent-text); }
.dial:focus-visible .face { stroke: var(--accent); stroke-width: 2; }
.dial-quick { display: flex; gap: 8px; justify-content: center; margin: 10px 0 6px; flex-wrap: wrap; }
.dial-quick button { color: var(--link); background: rgba(128, 128, 128, .12); padding: 7px 12px; border-radius: 999px; font-size: 0.875rem; font-weight: 500; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.sheet-btn { padding: 13px; border-radius: var(--radius-sm); font-weight: 600; background: rgba(128, 128, 128, .14); color: var(--link); }
.sheet-btn.primary { background: var(--accent); color: var(--accent-text); }

/* ---------- напомнить заранее ---------- */
.before-card { padding: 12px; }
.before-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.bchip {
  padding: 7px 12px; border-radius: 999px; font-size: 0.875rem; font-weight: 500;
  background: rgba(128, 128, 128, .12); color: var(--text);
}
.bchip[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }
.bchip.extra { color: var(--link); background: transparent; box-shadow: inset 0 0 0 1px var(--sep); }
.bchip.past { text-decoration: line-through; opacity: .55; }
.bchip .x { margin-left: 6px; opacity: .8; }
.before-extra { display: flex; align-items: center; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.before-extra input, .before-extra select {
  font: inherit; color: var(--text); background: rgba(128, 128, 128, .12);
  border: 0; border-radius: 8px; padding: 6px 10px; outline: none;
}
.before-extra input[type=number] { width: 68px; text-align: center; }
.mini-btn { color: var(--accent-text); background: var(--accent); padding: 7px 12px; border-radius: 8px; font-weight: 600; font-size: 0.875rem; }
.hint.warn, .hint .warn { color: var(--danger); }

/* ---------- люди ---------- */
.row-btn { width: 100%; text-align: left; }
.row-btn .row-value::after { content: " ›"; opacity: .6; }
.person-item { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 14px; text-align: left; position: relative; }
.person-item + .person-item::before { content: ""; position: absolute; top: 0; left: 62px; right: 0; height: 1px; background: var(--sep); }
.person-item:active { background: rgba(128, 128, 128, .12); }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; flex: none; display: grid; place-items: center;
  font-weight: 600; color: #fff; background: var(--av, var(--accent)); font-size: 0.9375rem;
}
.person-main { flex: 1; min-width: 0; }
.person-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-sub { font-size: 0.8125rem; color: var(--hint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-check { color: var(--link); font-size: 1.125rem; }
.person-edit { color: var(--link); padding: 6px 8px; border-radius: 8px; font-size: 0.875rem; }
.person-edit:active { background: rgba(128, 128, 128, .15); }
.fields .field { display: flex; align-items: center; gap: 12px; padding: 10px 14px; position: relative; }
.fields .field + .field::before { content: ""; position: absolute; top: 0; left: 14px; right: 0; height: 1px; background: var(--sep); }
.fields .field > span { width: 92px; flex: none; color: var(--hint); font-size: 0.9375rem; }
.fields input, .fields textarea {
  flex: 1; min-width: 0; border: 0; outline: none; background: transparent; color: var(--text);
  font: inherit; padding: 4px 0;
}
.fields textarea { resize: none; padding: 4px 0; }
.field-note { align-items: flex-start; }
.item-meta .sep { opacity: .5; margin: 0 4px; }
