/* Цвета берутся из темы Телеграма; значения после запятой - запасные для светлой темы. */
:root {
  --bg: var(--tg-theme-secondary-bg-color, #f1f1f4);
  --card: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1c1c1e);
  --muted: var(--tg-theme-hint-color, #6d6d72);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --line: color-mix(in srgb, var(--muted) 28%, transparent);
  --danger: var(--tg-theme-destructive-text-color, #d93025);
  --ok: #2e9e5b;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  padding: 0 0 calc(64px + env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--text);
  font: 14px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

main { max-width: 640px; margin: 0 auto; padding: 8px 8px 0; }

h2 { margin: 0 0 6px; font-size: 14px; font-weight: 600; }
p { margin: 4px 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hint { margin: 6px 0 0; color: var(--muted); font-size: 12px; line-height: 1.3; }
.hint.lead { margin: 2px 4px 8px; }

/* --- вкладки: одна строка, листается в сторону, как папки в Телеграме --- */
.tabs {
  position: sticky;
  top: 0;
  z-index: 3;
  display: flex;
  gap: 2px;
  padding: 0 4px;
  overflow-x: auto;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  flex: none;
  min-height: 38px;
  padding: 0 9px;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}
.tabs button[aria-current] { color: var(--accent); border-bottom-color: var(--accent); }
@media (min-width: 460px) { .tabs { justify-content: center; } }

.card {
  margin: 0 0 8px;
  padding: 10px 12px;
  background: var(--card);
  border-radius: 10px;
}

.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.row h2 { margin: 0; }

.badge {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--line);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge.running { background: color-mix(in srgb, var(--ok) 20%, transparent); color: var(--ok); }
.badge.backfill { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent); }

/* --- строки «название - число» --- */
.rows { margin: 0; }
.rows div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
  border-top: 1px solid var(--line);
}
.rows div:first-child { border-top: 0; }
.rows dt { color: var(--muted); }
.rows dd { margin: 0; font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.rows .total dt { color: var(--text); font-weight: 600; }

/* --- кнопки и поля --- */
button {
  min-height: 36px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
button.primary { border-color: var(--accent); background: var(--accent); color: var(--accent-text); }
button.wide { width: 100%; margin-top: 8px; }
button:disabled { opacity: 0.45; cursor: default; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

label { display: block; margin: 6px 0 0; color: var(--muted); font-size: 12px; }
input, select, textarea {
  display: block;
  width: 100%;
  min-height: 34px;
  margin-top: 2px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
textarea { resize: vertical; }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 8px; }
.grid label:first-child, .grid label:nth-child(2) { margin-top: 0; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

.switch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 8px;
  color: var(--text);
  font-size: 14px;
}
.switch input {
  flex: none;
  width: 42px;
  min-height: 24px;
  height: 24px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: var(--line);
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
  position: relative;
}
.switch input::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
}
.switch input:checked { background: var(--ok); }
.switch input:checked::after { left: 20px; }
.switch input:disabled { opacity: 0.45; cursor: default; }

/* --- источники --- */
.list { margin: 0; padding: 0; list-style: none; }
.list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}
.list .name { font-weight: 600; overflow-wrap: anywhere; }
.list .bad { color: var(--danger); }
.list input[type="checkbox"] { width: 20px; min-height: 20px; height: 20px; margin: 0; flex: none; accent-color: var(--accent); }

.add-row { display: flex; gap: 6px; margin-top: 8px; }
.add-row input { margin-top: 0; }
.add-row button { flex: none; }

/* --- пожелания --- */
.wish { padding: 8px 0; border-top: 1px solid var(--line); }
#builtin .wish:first-child, #keywords .wish:first-child { border-top: 0; padding-top: 2px; }
.wish-head { display: flex; align-items: center; gap: 6px; }
.wish-head input, .wish-head select { margin-top: 0; }
.wish-title { flex: 1; min-width: 0; font-weight: 500; }
.wish-head select { flex: none; width: 138px; padding-left: 6px; padding-right: 2px; font-size: 12px; }
.keyword .wish-head input { flex: 1; min-width: 0; }
.kw-remove { flex: none; min-height: 34px; width: 30px; padding: 0; border: 0; color: var(--muted); }
.word-row { display: flex; align-items: center; gap: 6px; margin-top: 4px; }
.word-row span { flex: none; width: 26px; }
.word-row input { margin-top: 0; }
.wish label textarea { font-size: 13px; }

details > summary { cursor: pointer; list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details > summary h2 { display: inline; margin: 0; }
details > summary::after { content: "  ▾"; color: var(--muted); }
details[open] > summary::after { content: "  ▴"; }
details[open] > summary { margin-bottom: 6px; }

.prose h2:not(:first-child) { margin-top: 12px; }
.prose ol, .prose ul { margin: 4px 0; padding-left: 20px; }
.prose li { margin: 2px 0; }

.save-bar {
  position: fixed;
  z-index: 3;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: var(--card);
  border-top: 1px solid var(--line);
}

#loading { margin: 12px; }

.notice {
  position: sticky;
  top: 40px;
  z-index: 2;
  max-width: 624px;
  margin: 8px auto 0;
  width: calc(100% - 16px);
  padding: 8px 12px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--danger) 14%, var(--card));
  font-size: 13px;
}
.notice.info { background: color-mix(in srgb, var(--ok) 16%, var(--card)); }

@media (prefers-reduced-motion: no-preference) {
  button { transition: opacity 0.15s; }
}
