/* Цвета берутся из темы Telegram (--tg-theme-*); запасные значения — для открытия вне Telegram и старых клиентов. */
:root {
  color-scheme: light dark;
  --fb-bg: #f0f0f0;
  --fb-surface: #ffffff;
  --fb-text: #000000;
  --fb-hint: #6b7077;
  --fb-link: #2481cc;
  --fb-button: #3390ec;
  --fb-danger: #d13b3b;

  --bg: var(--tg-theme-secondary-bg-color, var(--fb-bg));
  --surface: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, var(--fb-surface)));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --accent: var(--tg-theme-link-color, var(--fb-link));
  --button: var(--tg-theme-button-color, var(--fb-button));
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, var(--fb-danger));
  --section-title: var(--tg-theme-section-header-text-color, var(--hint));
  --line: var(--tg-theme-section-separator-color, color-mix(in srgb, var(--text) 14%, transparent));
  --radius: 12px;
  --gap: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --fb-bg: #0f1720;
    --fb-surface: #17212b;
    --fb-text: #f5f5f5;
    --fb-hint: #8a9aab;
    --fb-link: #6ab3f3;
    --fb-button: #5288c1;
    --fb-danger: #ec6a6a;
  }
}

/* Полосы прокрутки: тонкие, цвета подсказок темы, без подложки — как на портале (static/app.css).
   Старый Chromium/WebKit без scrollbar-color получает то же через ::-webkit-scrollbar. */
:root {
  --scrollbar-thumb: color-mix(in srgb, var(--hint) 55%, transparent);
  --scrollbar-track: transparent;
}
@media (prefers-contrast: more) {
  :root { --scrollbar-thumb: var(--text); }
}
html { scrollbar-gutter: stable; }
* {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}
@supports not (scrollbar-color: auto) {
  ::-webkit-scrollbar { width: 8px; height: 8px; }
  ::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 9999px; }
  ::-webkit-scrollbar-track { background: var(--scrollbar-track); }
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-text-size-adjust: 100%;
}

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

main {
  display: grid;
  gap: var(--gap);
  align-content: start;
  max-width: 40rem;
  min-height: 100vh;
  margin: 0 auto;
  padding: 8px max(var(--gap), env(safe-area-inset-right)) calc(32px + env(safe-area-inset-bottom)) max(var(--gap), env(safe-area-inset-left));
  outline: none;
}
main > * { min-width: 0; }
main.working { cursor: progress; }
main.working .group, main.working .add { opacity: 0.7; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1:focus { outline: none; }

/* --- заголовки ------------------------------------------------------------ */

.head { display: grid; gap: 2px; padding-top: 8px; }
h1 { margin: 0; font-size: 1.75rem; line-height: 1.15; font-weight: 700; letter-spacing: -0.01em; overflow-wrap: anywhere; }
h2 { margin: 0; font-size: 1rem; font-weight: 600; }
p { margin: 0; }
.hint { color: var(--hint); font-size: 0.9375rem; }
.head .hint::first-letter { text-transform: uppercase; }

.back {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  min-height: 44px;
  margin-left: -6px;
  color: var(--accent);
  text-decoration: none;
}
.back .icon { transform: scaleX(-1); }

.section-title { margin: 0 0 6px 4px; color: var(--section-title); font-size: 0.875rem; font-weight: 500; }
.section-title a { display: flex; align-items: center; justify-content: space-between; min-height: 44px; margin-top: -10px; margin-bottom: -10px; color: inherit; text-decoration: none; }

/* --- группы и строки ------------------------------------------------------- */

.group, .panel { background: var(--surface); border-radius: var(--radius); overflow: hidden; }
.panel { display: grid; gap: 14px; padding: 14px 16px; overflow: visible; }
.panel + .panel, .panel + .hint { margin-top: 10px; }
.panel > .section-title { margin: 0; }

.row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 10px 16px;
  color: inherit;
  text-decoration: none;
}
.row + .row { border-top: 1px solid var(--line); }
.row.stack, .stack { flex-direction: column; align-items: flex-start; gap: 2px; }
.grow { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.label { min-width: 0; overflow-wrap: anywhere; }
.memory-body { white-space: pre-wrap; }
.when { color: var(--accent); font-size: 0.875rem; font-variant-numeric: tabular-nums; }
.badge { font-size: 0.8125rem; color: var(--danger); }
.link .icon, .section-title .icon { color: var(--hint); }
.link { cursor: pointer; }

/* --- иконки и кнопки -------------------------------------------------------- */

.icon { flex: none; width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

button, .btn, .field { font: inherit; }
button { color: inherit; background: none; border: 0; padding: 0; cursor: pointer; text-align: left; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 18px;
  border: 0;
  border-radius: 10px;
  background: var(--surface);
  color: var(--accent);
  font-weight: 500;
  text-align: center;
  text-decoration: none;
}
.btn.primary { background: var(--button); color: var(--button-text); }
.btn.danger { color: var(--danger); }
.btn.block { display: flex; width: 100%; }
section > .btn.block { margin-top: 8px; }

.icon-btn {
  display: grid;
  place-items: center;
  flex: none;
  width: 44px;
  height: 44px;
  margin-block: -6px;
  border-radius: 50%;
  color: var(--hint);
}
.row > .icon-btn:last-child { margin-right: -10px; }
.icon-btn:hover { color: var(--danger); }

.check-row {
  display: flex;
  flex: 1;
  align-items: center;
  gap: 12px;
  min-width: 0;
  min-height: 44px;
  margin: -6px 0;
}
.tick {
  display: grid;
  place-items: center;
  flex: none;
  width: 24px;
  height: 24px;
  border: 2px solid var(--hint);
  border-radius: 50%;
  color: transparent;
}
.tick .icon { width: 16px; height: 16px; stroke-width: 3; }
.check-row[aria-checked="true"] .tick { background: var(--button); border-color: var(--button); color: var(--button-text); }
.check-row[aria-checked="true"] .label { color: var(--hint); text-decoration: line-through; }

.switch-row { width: 100%; padding-block: 12px; }
.track { flex: none; position: relative; width: 51px; height: 31px; border-radius: 16px; background: color-mix(in srgb, var(--hint) 45%, transparent); }
.track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgb(0 0 0 / 0.3); }
.switch-row[aria-checked="true"] .track { background: var(--button); }
.switch-row[aria-checked="true"] .track::after { transform: translateX(20px); }

/* --- формы ------------------------------------------------------------------ */

.add, .form { display: grid; gap: 6px; }
.form { gap: 16px; }
.add > label, .form-row:not(.segmented) > label, .form legend, .segmented legend { color: var(--section-title); font-size: 0.875rem; padding: 0; }
.add-line { display: flex; gap: 8px; }
.add-line .field { flex: 1; min-width: 0; }
.form-row { display: grid; gap: 6px; margin: 0; padding: 0; border: 0; min-width: 0; }

.field {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
}
textarea.field { resize: vertical; }
.field::placeholder { color: var(--hint); opacity: 1; }
.form-error { min-height: 1.4em; color: var(--danger); font-size: 0.9375rem; }
.form-error:empty { display: none; }

.segmented { display: flex; flex-wrap: wrap; gap: 6px; }
.segmented legend { flex-basis: 100%; }
.segmented label, .chip { position: relative; flex: 1 1 auto; min-width: 0; }
.segmented label { min-width: min-content; }
.segmented input, .chip input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.segmented span, .chip span {
  display: grid;
  place-items: center;
  min-height: 44px;
  padding: 0 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.9375rem;
  text-align: center;
}
.segmented input:checked + span, .chip input:checked + span { background: var(--button); border-color: var(--button); color: var(--button-text); }
.segmented input:focus-visible + span, .chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { flex: 1 1 36px; }
.chip span { padding: 0; }

.note { white-space: pre-line; }
.fold summary { min-height: 44px; display: flex; align-items: center; gap: 10px; padding-left: 4px; color: var(--section-title); cursor: pointer; list-style: none; }
.fold summary::-webkit-details-marker { display: none; }
.fold summary::before { content: ""; width: 7px; height: 7px; border: solid currentColor; border-width: 0 2px 2px 0; transform: rotate(-45deg); }
.fold[open] summary::before { transform: rotate(45deg); }
.fold[open] summary { margin-bottom: 6px; }

/* --- состояния --------------------------------------------------------------- */

.state { display: grid; justify-items: center; gap: 8px; padding: 32px 16px; text-align: center; }
.state-title { font-weight: 600; }
.state .btn { margin-top: 8px; }
.gate { min-height: 60vh; align-content: center; }

.skeleton { display: grid; gap: 1px; background: var(--surface); border-radius: var(--radius); overflow: hidden; }
.skeleton span { display: block; height: 48px; background: color-mix(in srgb, var(--hint) 14%, var(--surface)); margin-bottom: 1px; }

.toast {
  position: fixed;
  inset: auto 16px calc(16px + env(safe-area-inset-bottom)) 16px;
  max-width: 28rem;
  margin: 0 auto;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.9375rem;
  opacity: 0;
  pointer-events: none;
}
.toast.show { opacity: 1; }
.toast.error { background: var(--danger); color: #fff; }
.toast:empty { display: none; }

/* --- диалоги ----------------------------------------------------------------- */

dialog {
  width: min(24rem, calc(100vw - 32px));
  padding: 20px;
  border: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 0.5); }
dialog form { display: grid; gap: 12px; }
dialog p { overflow-wrap: anywhere; white-space: pre-wrap; max-height: 40vh; overflow-y: auto; color: var(--hint); }
.dialog-actions { display: flex; gap: 8px; }
.dialog-actions .btn { flex: 1; background: color-mix(in srgb, var(--hint) 16%, var(--surface)); }
.dialog-actions .btn.primary { background: var(--button); }

/* --- движение: только если не отключено в системе ----------------------------- */

@media (prefers-reduced-motion: no-preference) {
  .tick, .track, .track::after, .btn, .icon-btn { transition: background-color 0.15s, border-color 0.15s, color 0.15s, transform 0.15s; }
  main.working .group, main.working .add { transition: opacity 0.2s 0.2s; }
  .toast { transition: opacity 0.2s; }
  dialog[open] { transition: opacity 0.16s, transform 0.16s; }
  @starting-style { dialog[open] { opacity: 0; transform: translateY(8px); } }
  dialog[open]::backdrop { transition: opacity 0.16s; }
  @starting-style { dialog[open]::backdrop { opacity: 0; } }
  .skeleton span { animation: pulse 1.2s ease-in-out infinite alternate; }
  .skeleton span:nth-child(2) { animation-delay: 0.15s; }
  .skeleton span:nth-child(3) { animation-delay: 0.3s; }
}
@keyframes pulse { to { opacity: 0.55; } }
