:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f1f2f5);
  --text: var(--tg-theme-text-color, #16181c);
  --hint: var(--tg-theme-hint-color, #8a8f99);
  --link: var(--tg-theme-link-color, #2f80ed);
  --accent: var(--tg-theme-button-color, #2f80ed);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #e0455b;
  --radius: 14px;
  --gap: 10px;
}

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

/* HTML-атрибут hidden должен побеждать любые display из классов ниже */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
}

body { padding-bottom: 96px; }

/* ---------- Верхняя панель ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 8px;
  background: var(--bg);
}
.topbar__title { margin: 0; font-size: 26px; font-weight: 700; letter-spacing: -0.02em; flex: 1; }
.topbar__actions { display: flex; gap: 8px; }

.iconbtn {
  border: none;
  background: var(--secondary-bg);
  color: var(--text);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.1s ease, opacity 0.2s ease;
  flex-shrink: 0;
  margin-right: 8px;
}
.iconbtn:active { transform: scale(0.92); }
.iconbtn:disabled { opacity: 0.5; cursor: default; }

/* ---------- Контент ---------- */
#content {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: 4px 0 0;
}

/* ---------- Папки (группы) ---------- */
.folder {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin: 0 16px;
  cursor: pointer;
  transition: transform 0.1s ease;
}
.folder:active { transform: scale(0.99); }
.folder__icon { font-size: 28px; flex-shrink: 0; line-height: 1; }
.folder__info { flex: 1; min-width: 0; }
.folder__name { font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.folder__count { font-size: 13px; color: var(--hint); margin-top: 2px; }
.folder__delete {
  border: none;
  background: none;
  color: var(--hint);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 6px;
  flex-shrink: 0;
  border-radius: 50%;
  transition: background 0.15s ease;
}
.folder__delete:active { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }

.add-group-btn {
  display: block;
  width: calc(100% - 32px);
  margin: 0 16px;
  border: 1px dashed var(--hint);
  background: transparent;
  color: var(--link);
  padding: 12px;
  border-radius: var(--radius);
  font-size: 15px;
  cursor: pointer;
  text-align: center;
  transition: border-color 0.15s ease;
}
.add-group-btn:active { border-color: var(--link); }

.section-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 4px 16px 0;
}

/* ---------- Карточки заметок ---------- */
.card {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin: 0 16px;
  cursor: pointer;
  transition: transform 0.1s ease;
}
.card:active { transform: scale(0.99); }
.card__title { font-size: 17px; font-weight: 600; margin: 0 0 4px; }
.card__title--empty { color: var(--hint); font-weight: 500; }
.card__preview {
  margin: 0;
  color: var(--hint);
  font-size: 15px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card__meta {
  margin-top: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--hint);
}
.card__group {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent);
  padding: 3px 9px;
  border-radius: 999px;
  font-weight: 600;
}

/* ---------- Пустой экран ---------- */
.empty {
  text-align: center;
  color: var(--hint);
  padding: 64px 24px;
}
.empty__icon { font-size: 44px; }
.empty__title { font-size: 18px; font-weight: 600; color: var(--text); margin: 12px 0 4px; }

/* ---------- Плавающая кнопка ---------- */
.fab {
  position: fixed;
  right: 18px;
  bottom: 22px;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 30px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
  transition: transform 0.1s ease;
  z-index: 6;
}
.fab:active { transform: scale(0.9); }

/* ---------- Чипы ---------- */
.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 6px 16px 12px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chips--inline { flex-wrap: wrap; overflow: visible; padding: 0; }

.chip {
  flex: 0 0 auto;
  border: none;
  background: var(--secondary-bg);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s ease, color 0.15s ease;
}
.chip--active { background: var(--accent); color: var(--accent-text); }
.chip--add { color: var(--link); background: transparent; border: 1px dashed var(--hint); }

/* ---------- Редактор (нижний лист) ---------- */
.sheet {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 20;
  display: flex;
  flex-direction: column;
  animation: slide-up 0.22s ease;
}
@keyframes slide-up { from { transform: translateY(100%); } to { transform: translateY(0); } }

.sheet__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--secondary-bg);
  flex-shrink: 0;
}
.sheet__title { font-weight: 600; font-size: 16px; }
.sheet__body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.textbtn {
  border: none;
  background: none;
  color: var(--link);
  font-size: 16px;
  cursor: pointer;
  padding: 8px 4px;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
}
.textbtn--accent { font-weight: 600; justify-content: flex-end; }

.field-title {
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 22px;
  font-weight: 700;
  padding: 0;
  outline: none;
  width: 100%;
}
.field-content {
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 16px;
  outline: none;
  resize: none;
  min-height: 220px;
  width: 100%;
  font-family: inherit;
  line-height: 1.5;
}

.group-picker { border-top: 1px solid var(--secondary-bg); padding-top: 14px; }
.group-picker__label { display: block; font-size: 13px; color: var(--hint); margin-bottom: 10px; }

.danger {
  border: none;
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
  padding: 13px;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 4px;
}

/* ---------- Тосты ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 92px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 40;
  max-width: 84%;
  text-align: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}
.toast--err { background: var(--danger); color: #fff; }
.toast--loading::before {
  content: "";
  display: inline-block;
  width: 13px;
  height: 13px;
  margin-right: 8px;
  vertical-align: -2px;
  border: 2px solid color-mix(in srgb, var(--bg) 40%, transparent);
  border-top-color: var(--bg);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .sheet { animation: none; }
  .fab:active, .card:active, .iconbtn:active { transform: none; }
}
