/* Цвета берутся из темы Telegram, запасные значения нужны при открытии в обычном браузере. */
:root {
  --fb-bg: #ffffff;
  --fb-bg2: #efeff4;
  --fb-text: #000000;
  --fb-hint: #8e8e93;
  --fb-link: #2481cc;
  --fb-button: #2481cc;
  --fb-button-text: #ffffff;
  --fb-danger: #e53935;
  --fb-separator: rgba(60, 60, 67, 0.18);

  --bg: var(--tg-theme-secondary-bg-color, var(--fb-bg2));
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, var(--fb-bg)));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --subtitle: var(--tg-theme-subtitle-text-color, var(--hint));
  --section-title: var(--tg-theme-section-header-text-color, var(--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));
  --danger: var(--tg-theme-destructive-text-color, var(--fb-danger));
  --separator: var(--tg-theme-section-separator-color, var(--fb-separator));
  --green: #34c759;
  --orange: #ff9500;

  --radius: 14px;
  --safe-bottom: max(var(--tg-safe-area-inset-bottom, 0px), env(safe-area-inset-bottom, 0px));
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --fb-bg: #1c1c1e;
    --fb-bg2: #000000;
    --fb-text: #ffffff;
    --fb-hint: #8e8e93;
    --fb-link: #5ac8fa;
    --fb-button: #0a84ff;
    --fb-danger: #ff453a;
    --fb-separator: rgba(84, 84, 88, 0.6);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
button:disabled { cursor: default; opacity: 0.5; }

.icon { display: block; flex: none; }

/* ---------- каркас ---------- */

.shell {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

.view {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 12px 24px;
}

.view[hidden] { display: none; }

.tabbar {
  flex: none;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--card);
  border-top: 0.5px solid var(--separator);
  padding-bottom: var(--safe-bottom);
}

.kb-open .tabbar { display: none; }

.tab {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 7px 0 6px;
  font-size: 11px;
  color: var(--hint);
}

.tab.active { color: var(--button); }

.badge {
  position: absolute;
  top: 3px;
  left: calc(50% + 6px);
  min-width: 17px;
  height: 17px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--button);
  color: var(--button-text);
  font-size: 11px;
  line-height: 17px;
  text-align: center;
}

.boot, .center {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
}

/* ---------- карточки и списки ---------- */

.card {
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
}

.card + .card, .section-title + .card { margin-top: 8px; }

.section-title {
  margin: 18px 4px 6px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--section-title);
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 11px 12px;
  text-align: left;
  position: relative;
}

.row + .row::before {
  content: '';
  position: absolute;
  top: 0;
  left: 12px;
  right: 0;
  border-top: 0.5px solid var(--separator);
}

.row:active { background: color-mix(in srgb, var(--text) 6%, transparent); }

.row-main { flex: 1; min-width: 0; }

.row-title {
  font-weight: 500;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.row-details {
  margin-top: 2px;
  font-size: 13px;
  color: var(--subtitle);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-sub {
  margin-top: 3px;
  font-size: 13px;
  color: var(--subtitle);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 8px;
}

.row-side { flex: none; color: var(--hint); display: flex; align-items: center; }
.row-side.done { color: var(--green); }

.dim { opacity: 0.55; }

.seeds { color: var(--green); display: inline-flex; align-items: center; }
.seeds.none { color: var(--hint); }
.leech { color: var(--hint); display: inline-flex; align-items: center; }
.seeds .icon, .leech .icon { margin-right: 1px; }

.source-label {
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--hint) 16%, transparent);
  color: var(--subtitle);
}

.tags { display: inline-flex; flex-wrap: wrap; gap: 4px; }

.tag {
  font-size: 11px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--button) 14%, transparent);
  color: var(--accent);
}

.tag-year { background: color-mix(in srgb, var(--hint) 16%, transparent); color: var(--subtitle); }
.tag-bad { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.tag-hdr { background: color-mix(in srgb, var(--orange) 18%, transparent); color: var(--orange); }

/* ---------- поиск ---------- */

.search-form {
  position: sticky;
  top: -10px;
  z-index: 2;
  display: flex;
  gap: 8px;
  margin: -10px -12px 0;
  padding: 10px 12px 8px;
  background: var(--bg);
}

.search-field {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  border-radius: 12px;
  background: var(--card);
  color: var(--hint);
}

.search-field input {
  flex: 1;
  min-width: 0;
  height: 40px;
  border: 0;
  outline: 0;
  background: none;
  color: var(--text);
  font-size: 16px;
}

.search-field input::-webkit-search-cancel-button { display: none; }

.chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 4px 0 8px;
  scrollbar-width: none;
}

.chips::-webkit-scrollbar { display: none; }

.chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 30px;
  padding: 0 11px;
  border-radius: 15px;
  background: var(--card);
  font-size: 13px;
  white-space: nowrap;
}

.chip.active { background: var(--button); color: var(--button-text); }
.chip .count { opacity: 0.7; }
.chip.failed { color: var(--danger); }
.chip.active.failed { color: var(--button-text); }

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 2px 2px 8px;
  font-size: 13px;
  color: var(--hint);
}

.notes { margin: 0 4px 8px; font-size: 13px; color: var(--hint); }
.notes div + div { margin-top: 2px; }
.note-error { color: var(--danger); }

.recent { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }

/* ---------- переключатель ---------- */

.segmented {
  display: flex;
  padding: 2px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--hint) 18%, transparent);
}

.segmented button {
  flex: 1;
  padding: 5px 8px;
  border-radius: 8px;
  font-size: 13px;
  white-space: nowrap;
}

.segmented button.active {
  background: var(--card);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
  font-weight: 600;
}

.segmented .count { margin-left: 4px; color: var(--hint); font-weight: 400; }

/* ---------- прогресс ---------- */

.progress {
  height: 4px;
  margin-top: 7px;
  border-radius: 2px;
  background: color-mix(in srgb, var(--hint) 22%, transparent);
  overflow: hidden;
}

.progress-fill { height: 100%; border-radius: 2px; background: var(--button); transition: width 0.4s ease; }
.progress.paused .progress-fill { background: var(--hint); }
.progress.stalled .progress-fill { background: var(--orange); }
.progress.error .progress-fill { background: var(--danger); }
.progress.done .progress-fill { background: var(--green); }
.progress.big { height: 8px; border-radius: 4px; }

.summary { margin: 2px 4px 10px; font-size: 13px; color: var(--hint); }

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

.button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 46px;
  padding: 0 14px;
  border-radius: 12px;
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
}

.button.secondary { background: color-mix(in srgb, var(--button) 13%, transparent); color: var(--accent); }
.button.danger { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
.button.small { width: auto; min-height: 34px; padding: 0 12px; border-radius: 10px; font-size: 14px; }
.button + .button { margin-top: 8px; }

.icon-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 16px;
  color: var(--hint);
}

.icon-button.filled { background: color-mix(in srgb, var(--hint) 18%, transparent); }
.search-form .icon-button { width: 40px; height: 40px; border-radius: 12px; background: var(--card); color: var(--button); }

.categories { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

.category {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 72px;
  padding: 8px 4px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--button) 12%, transparent);
  color: var(--accent);
  font-weight: 600;
  font-size: 14px;
}

.category .emoji { font-size: 22px; line-height: 1; }
.category.suggested { background: var(--button); color: var(--button-text); }
.category.selected { background: var(--button); color: var(--button-text); }

/* ---------- карточка снизу ---------- */

.sheet-layer { position: fixed; inset: 0; z-index: 20; }

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  transition: opacity 0.22s ease;
}

.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 92%;
  display: flex;
  flex-direction: column;
  border-radius: 16px 16px 0 0;
  background: var(--bg);
  transform: translateY(100%);
  transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
  padding-bottom: var(--safe-bottom);
}

.sheet-layer.shown .sheet-backdrop { opacity: 1; }
.sheet-layer.shown .sheet { transform: translateY(0); }

.sheet-grip {
  width: 36px;
  height: 5px;
  margin: 7px auto 0;
  border-radius: 3px;
  background: color-mix(in srgb, var(--hint) 45%, transparent);
}

.sheet-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px 4px 16px;
}

.sheet-head h2 { flex: 1; margin: 0; font-size: 17px; font-weight: 600; }

.sheet-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 12px 16px;
}

.sheet-open .view { overflow: hidden; }

.detail-title {
  margin: 0 0 4px;
  font-size: 19px;
  font-weight: 700;
  line-height: 1.25;
  overflow-wrap: anywhere;
  user-select: text;
  -webkit-user-select: text;
}

.detail-text {
  font-size: 14px;
  color: var(--subtitle);
  overflow-wrap: anywhere;
  user-select: text;
  -webkit-user-select: text;
}

.detail-block { margin-top: 14px; }
.detail-block .tags { margin-top: 8px; }

.path {
  margin-top: 10px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--card);
  font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--subtitle);
  overflow-wrap: anywhere;
  user-select: text;
  -webkit-user-select: text;
}

.stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1px;
  margin: 14px 0 0;
  border-radius: 12px;
  overflow: hidden;
  background: var(--separator);
}

.stats div { padding: 8px 10px; background: var(--card); }
.stats dt { font-size: 12px; color: var(--hint); }
.stats dd { margin: 1px 0 0; font-weight: 600; }

.link-button {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 12px;
  color: var(--link);
  font-size: 15px;
}

/* ---------- добавление ---------- */

.textarea {
  width: 100%;
  min-height: 96px;
  padding: 10px 12px;
  border: 0;
  border-radius: 12px;
  outline: 0;
  background: var(--card);
  resize: vertical;
  font-size: 14px;
  overflow-wrap: anywhere;
}

.file-pick {
  position: relative;
  overflow: hidden;
}

.file-pick input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.picked { margin-top: 8px; }
.picked .row { padding: 8px 10px; }

.hint { margin: 6px 4px 0; font-size: 13px; color: var(--hint); }
.label { margin: 16px 4px 6px; font-size: 13px; color: var(--section-title); text-transform: uppercase; }

/* ---------- пустые состояния и подсказки ---------- */

.empty { padding: 48px 20px; text-align: center; }
.empty-title { font-size: 17px; font-weight: 600; }
.empty-text { margin-top: 6px; color: var(--hint); }
.empty .button { width: auto; margin: 16px auto 0; display: inline-flex; }

.error-box {
  margin: 8px 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  color: var(--danger);
  font-size: 14px;
}

.toasts {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(64px + var(--safe-bottom));
  z-index: 40;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}

.toast {
  max-width: 100%;
  padding: 9px 14px;
  border-radius: 12px;
  background: rgba(30, 30, 30, 0.92);
  color: #fff;
  font-size: 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  animation: toast-in 0.2s ease;
}

.toast-success { background: rgba(28, 120, 58, 0.95); }
.toast-error { background: rgba(190, 40, 40, 0.95); }

@keyframes toast-in {
  from { transform: translateY(8px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.spinner {
  display: inline-block;
  width: 28px;
  height: 28px;
  border: 3px solid color-mix(in srgb, var(--hint) 30%, transparent);
  border-top-color: var(--button);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.spinner-small { width: 14px; height: 14px; border-width: 2px; }

@keyframes spin { to { transform: rotate(360deg); } }

.skeleton {
  height: 58px;
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--hint) 12%, transparent), transparent);
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite;
}

@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

@media (min-width: 640px) {
  .view, .sheet-body { padding-left: max(12px, calc((100% - 620px) / 2)); padding-right: max(12px, calc((100% - 620px) / 2)); }
  .sheet { left: 50%; right: auto; width: 640px; transform: translate(-50%, 100%); }
  .sheet-layer.shown .sheet { transform: translate(-50%, 0); }
}
