/* Цвета берутся из темы Telegram (--tg-theme-*), запасные — для обычного браузера. */
:root {
  --bg: var(--tg-theme-secondary-bg-color, #efeff4);
  --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);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, var(--accent));
  --danger: var(--tg-theme-destructive-text-color, #e53935);
  --sep: var(--tg-theme-section-separator-color, rgba(0, 0, 0, 0.08));
  --section-head: var(--tg-theme-section-header-text-color, var(--hint));
  --soft: color-mix(in srgb, var(--accent) 12%, transparent);
  --warn: #e8710a;
  --radius: 14px;
  --safe-top: var(--tg-content-safe-area-inset-top, 0px);
  --safe-bottom: max(var(--tg-safe-area-inset-bottom, 0px), env(safe-area-inset-bottom, 0px));
}

@media (prefers-color-scheme: dark) {
  :root:not(.tg) {
    --bg: #000000;
    --card: #1c1c1e;
    --text: #ffffff;
    --hint: #8e8e93;
    --accent: #3e88f7;
    --sep: rgba(255, 255, 255, 0.1);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  min-height: 100vh;
  padding-bottom: calc(76px + var(--safe-bottom));
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:disabled { cursor: default; }
input, textarea { font: inherit; color: inherit; }

/* ---------------------------------------------------------------- шапка */

.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + var(--safe-top)) 12px 8px 16px;
  background: var(--bg);
}
.tabs { display: flex; gap: 4px; flex: 1; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  padding: 7px 12px; border-radius: 999px; white-space: nowrap;
  color: var(--hint); font-weight: 600; font-size: 15px;
}
.tab.is-active { background: var(--card); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06); }
.badge {
  display: inline-block; min-width: 18px; margin-left: 5px; padding: 0 5px;
  border-radius: 9px; background: var(--accent); color: var(--accent-text);
  font-size: 12px; line-height: 18px; text-align: center; vertical-align: 1px;
}
.badge:empty { display: none; }
.icon-btn { width: 38px; height: 38px; display: grid; place-items: center; border-radius: 50%; color: var(--accent); }
.icon-btn:active { background: var(--soft); }

.searchbar {
  position: sticky; top: calc(54px + var(--safe-top)); z-index: 9;
  display: flex; gap: 10px; align-items: center; padding: 0 16px 8px; background: var(--bg);
}
.searchbar input {
  flex: 1; min-width: 0; height: 38px; padding: 0 12px; border: 0; border-radius: 10px;
  background: var(--card); outline: none;
}
.link-btn { color: var(--link); }

/* ---------------------------------------------------------------- списки */

main { padding: 4px 16px 16px; }
.center { display: grid; place-items: center; min-height: 50vh; text-align: center; }
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--sep); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.page-head { display: flex; align-items: center; gap: 8px; margin: 6px 4px 10px; }
.page-head h1 { flex: 1; min-width: 0; margin: 0; font-size: 22px; line-height: 1.2; overflow-wrap: anywhere; }
.page-head .count { color: var(--hint); font-weight: 400; }
.section-title {
  margin: 18px 4px 6px; color: var(--section-head);
  font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.02em;
}
.section-title.is-danger { color: var(--danger); }

.group { background: var(--card); border-radius: var(--radius); overflow: hidden; }
.group + .group { margin-top: 12px; }

.row {
  display: flex; align-items: flex-start; gap: 12px; width: 100%;
  padding: 11px 14px; text-align: left; position: relative;
}
.row + .row::before {
  content: ""; position: absolute; top: 0; left: 50px; right: 0; border-top: 1px solid var(--sep);
}
.row.plain + .row.plain::before { left: 14px; }
.row:active { background: var(--soft); }
.row-body { flex: 1; min-width: 0; }
.row-text {
  overflow-wrap: anywhere; white-space: pre-line;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.is-done .row-text { color: var(--hint); text-decoration: line-through; }
.row-meta { display: flex; flex-wrap: wrap; gap: 4px 10px; margin-top: 3px; color: var(--hint); font-size: 13px; }
.row-meta .late { color: var(--danger); }
.row-side { color: var(--hint); font-size: 15px; align-self: center; white-space: nowrap; }
.chevron { color: var(--hint); opacity: 0.6; align-self: center; }

/* кружок задачи / значок заметки слева */
/* кружок крупнее, чем видно: по нему легко попасть пальцем */
.mark {
  flex: none; width: 24px; height: 24px; display: grid; place-items: center;
  box-sizing: content-box; padding: 10px; margin: -10px;
}
.check {
  width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--hint); opacity: 0.7;
  display: grid; place-items: center; transition: background 0.15s, border-color 0.15s;
}
.check svg { opacity: 0; }
.is-done .check { background: var(--accent); border-color: var(--accent); opacity: 1; }
.is-done .check svg { opacity: 1; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--hint); opacity: 0.5; }
.project-icon {
  flex: none; width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center;
  background: var(--soft); color: var(--accent); font-weight: 700; font-size: 14px;
}

.more {
  display: block; width: 100%; margin-top: 12px; padding: 10px; border-radius: var(--radius);
  color: var(--link); font-size: 15px;
}
.empty { margin: 32px 12px; color: var(--hint); text-align: center; }
.empty b { color: var(--text); display: block; font-size: 17px; margin-bottom: 6px; }
.empty code {
  font-family: inherit; color: var(--text); background: var(--card); padding: 1px 6px; border-radius: 6px;
}
.add-row { color: var(--link); }
.add-row .project-icon { font-size: 20px; font-weight: 400; }

/* ---------------------------------------------------------------- поле ввода */

.composer {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 12px calc(8px + var(--safe-bottom));
  background: var(--card); border-top: 1px solid var(--sep);
}
.composer textarea {
  flex: 1; min-width: 0; max-height: 40vh; resize: none;
  padding: 9px 14px; border: 0; border-radius: 20px; outline: none;
  background: var(--bg); line-height: 1.35;
}
.send {
  flex: none; width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-text); transition: opacity 0.15s, transform 0.1s;
}
.send:disabled { opacity: 0.35; }
.send:not(:disabled):active { transform: scale(0.92); }

/* ---------------------------------------------------------------- карточка записи */

.backdrop {
  position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, 0.35);
  animation: fade 0.2s ease-out;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 31;
  max-height: calc(100vh - 40px - var(--safe-top)); overflow-y: auto; overscroll-behavior: contain;
  padding: 8px 16px calc(16px + var(--safe-bottom));
  background: var(--bg); border-radius: 16px 16px 0 0;
  animation: rise 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
}
@media (min-width: 640px) {
  .sheet { left: 50%; right: auto; width: 560px; transform: translateX(-50%); }
}
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
@media (min-width: 640px) {
  @keyframes rise { from { transform: translate(-50%, 40px); opacity: 0; } }
}
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
  .sheet, .backdrop { animation: none; }
}
/* пока открыта карточка, поле ввода внизу только мешает (и всплывает с клавиатурой) */
body.has-sheet { overflow: hidden; }
body.has-sheet .composer { display: none; }
body.has-sheet .toast { bottom: calc(16px + var(--safe-bottom)); }
.grabber { width: 36px; height: 5px; margin: 0 auto 10px; border-radius: 3px; background: var(--hint); opacity: 0.4; }

.editor {
  display: block; width: 100%; min-height: 96px; max-height: 45vh; resize: none;
  padding: 12px 14px; border: 0; border-radius: var(--radius); outline: none; background: var(--card);
}
.photo { display: block; max-width: 100%; max-height: 40vh; margin: 0 auto 10px; border-radius: var(--radius); }
.links { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.links a { color: var(--link); overflow-wrap: anywhere; font-size: 14px; text-decoration: none; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  padding: 7px 12px; border-radius: 999px; background: var(--card); font-size: 15px;
}
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.is-on { background: var(--soft); color: var(--accent); font-weight: 600; }
.chip.is-accent { background: var(--accent); color: var(--accent-text); }
.chip.is-late { color: var(--danger); }
.chip:active { opacity: 0.7; }

.panel { margin-top: 12px; padding: 12px; border-radius: var(--radius); background: var(--card); }
.panel .chips { margin-top: 0; }
.panel .chip { background: var(--bg); }
.panel .chip.is-on { background: var(--soft); }
.panel-title { margin-bottom: 10px; color: var(--hint); font-size: 13px; }
.when-row { display: flex; gap: 8px; margin-top: 10px; }
.when-row input, .panel input {
  flex: 1; min-width: 0; height: 38px; padding: 0 12px; border: 0; border-radius: 10px;
  background: var(--bg); outline: none;
}
.btn {
  height: 38px; padding: 0 16px; border-radius: 10px;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
}
.btn:disabled { opacity: 0.4; }
.when-hint { min-height: 18px; margin-top: 6px; color: var(--hint); font-size: 13px; }
.when-hint.ok { color: var(--accent); }
.when-hint.bad { color: var(--danger); }

.sheet-foot {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-top: 16px; color: var(--hint); font-size: 13px;
}
.danger-btn { color: var(--danger); font-size: 15px; padding: 6px 0; }

/* ---------------------------------------------------------------- настройки и тост */

.settings { margin-top: 24px; }
.settings .chips { margin-top: 8px; }
.settings .chip { background: var(--card); }

.toast {
  position: fixed; left: 12px; right: 12px; z-index: 40;
  bottom: calc(68px + var(--safe-bottom));
  display: flex; align-items: center; gap: 12px; max-width: 520px; margin: 0 auto;
  padding: 11px 14px; border-radius: 12px;
  background: rgba(30, 30, 32, 0.94); color: #fff; font-size: 15px;
  animation: pop 0.2s ease-out;
}
.toast span { flex: 1; min-width: 0; }
.toast button { color: #6ab3f3; font-weight: 600; white-space: nowrap; }
