/* ==========================================================================
   Мини-апп «Задачи» — тема, layout, компоненты.
   Цвета берём из Telegram (--tg-theme-*), с фолбэками для обычного браузера.
   ========================================================================== */

:root {
  --bg:        var(--tg-theme-bg-color, #ffffff);
  --bg-soft:   var(--tg-theme-secondary-bg-color, #f2f3f5);
  --surface:   var(--tg-theme-section-bg-color, #ffffff);
  --text:      var(--tg-theme-text-color, #1c1c1e);
  --hint:      var(--tg-theme-hint-color, #7d8b99);
  --link:      var(--tg-theme-link-color, #2481cc);
  --accent:    var(--tg-theme-button-color, #2481cc);
  --accent-fg: var(--tg-theme-button-text-color, #ffffff);
  --danger:    var(--tg-theme-destructive-text-color, #e5484d);

  --line: color-mix(in srgb, var(--hint) 26%, transparent);
  --line-soft: color-mix(in srgb, var(--hint) 14%, transparent);
  --hover: color-mix(in srgb, var(--hint) 12%, transparent);

  --high: #f0483e;
  --medium: #f5a623;
  --low: #3cba54;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 6px 20px rgba(0,0,0,.08);
  --shadow-lg: 0 12px 44px rgba(0,0,0,.28);
  --topbar-h: 56px;
  --sidebar-w: 244px;
  --drawer-w: 440px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg-soft);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4, p { margin: 0; }
[hidden] { display: none !important; }

/* ------------------------------- загрузка ------------------------------- */
.boot { position: fixed; inset: 0; display: grid; place-content: center; justify-items: center; gap: 14px; background: var(--bg-soft); z-index: 100; }
.boot__spinner { width: 30px; height: 30px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
.boot__text { color: var(--hint); font-size: 14px; text-align: center; max-width: 300px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------- каркас --------------------------------- */
.app { display: flex; flex-direction: column; height: 100dvh; }

.topbar {
  display: flex; align-items: center; gap: 10px;
  height: var(--topbar-h); flex: 0 0 var(--topbar-h);
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line-soft);
  position: relative; z-index: 30;
}
.topbar__burger { display: none; }
.topbar__title { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.topbar__view { font-weight: 650; font-size: 16px; white-space: nowrap; }
.topbar__count { color: var(--hint); font-size: 13px; white-space: nowrap; }
.topbar__actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }

.search { position: relative; flex: 1 1 260px; max-width: 420px; margin-left: 8px; }
.search__icon { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); font-size: 13px; opacity: .6; pointer-events: none; }
.search input {
  width: 100%; height: 36px; padding: 0 12px 0 32px;
  background: var(--bg-soft); border: 1px solid transparent; border-radius: 999px; outline: none;
}
.search input:focus { border-color: var(--accent); background: var(--bg); }
.search input::-webkit-search-cancel-button { -webkit-appearance: none; }

.layout { display: flex; flex: 1; min-height: 0; }

.sidebar {
  width: var(--sidebar-w); flex: 0 0 var(--sidebar-w);
  background: var(--surface); border-right: 1px solid var(--line-soft);
  overflow-y: auto; padding: 12px 10px calc(16px + var(--safe-b));
  display: flex; flex-direction: column; gap: 18px;
}
.main { flex: 1; min-width: 0; overflow: hidden; display: flex; flex-direction: column; }

/* ------------------------------ навигация ------------------------------- */
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav__item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border: 0; border-radius: var(--radius-sm);
  background: transparent; cursor: pointer; text-align: left; font-size: 14.5px;
}
.nav__item span { width: 18px; text-align: center; opacity: .8; }
.nav__item:hover { background: var(--hover); }
.nav__item.is-active { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); font-weight: 600; }
.badge {
  margin-left: auto; min-width: 20px; height: 20px; padding: 0 6px;
  display: grid; place-items: center; border-radius: 999px;
  background: var(--danger); color: #fff; font-size: 11px; font-style: normal; font-weight: 700;
}

/* ------------------------------- фильтры -------------------------------- */
.filters { display: flex; flex-direction: column; gap: 14px; }
.filters__head { display: flex; align-items: center; justify-content: space-between; }
.filters__head h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--hint); }
.link-btn { border: 0; background: none; color: var(--link); cursor: pointer; font-size: 12.5px; padding: 2px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field__label { font-size: 12px; color: var(--hint); }
.field select, .field input[type="text"], .field input[type="datetime-local"], .field textarea {
  width: 100%; padding: 8px 10px; background: var(--bg-soft);
  border: 1px solid var(--line-soft); border-radius: var(--radius-sm); outline: none;
}
.field select:focus, .field input:focus, .field textarea:focus { border-color: var(--accent); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 5px 10px; border: 1px solid var(--line); border-radius: 999px;
  background: transparent; cursor: pointer; font-size: 12.5px; white-space: nowrap;
}
.chip:hover { background: var(--hover); }
.chip.is-on { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }

.switch { position: relative; display: inline-block; width: 42px; height: 24px; flex: 0 0 auto; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch i { position: absolute; inset: 0; border-radius: 999px; background: var(--line); transition: background .18s; pointer-events: none; }
.switch i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .18s; }
.switch input:checked + i { background: var(--accent); }
.switch input:checked + i::after { transform: translateX(18px); }

/* ------------------------------- кнопки --------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border: 1px solid var(--line);
  border-radius: var(--radius-sm); background: var(--surface); cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.btn--primary:hover { filter: brightness(1.06); background: var(--accent); }
.btn--danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn--wide { width: 100%; }
.btn[disabled] { opacity: .5; pointer-events: none; }

.icon-btn {
  width: 36px; height: 36px; flex: 0 0 36px; display: grid; place-items: center;
  border: 0; border-radius: var(--radius-sm); background: transparent; cursor: pointer; font-size: 17px;
}
.icon-btn:hover { background: var(--hover); }

.seg { display: inline-flex; background: var(--bg-soft); border-radius: var(--radius-sm); padding: 2px; }
.seg__btn { border: 0; background: transparent; padding: 6px 11px; border-radius: 6px; cursor: pointer; font-size: 13px; color: var(--hint); }
.seg__btn.is-active { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.12); }

.avatar {
  width: 32px; height: 32px; flex: 0 0 32px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-fg); font-size: 12px; font-weight: 700;
  display: grid; place-items: center; background-size: cover; background-position: center;
}

/* -------------------------------- доска --------------------------------- */
.board { flex: 1; display: flex; gap: 12px; padding: 12px; overflow-x: auto; overflow-y: hidden; align-items: stretch; }
.column {
  flex: 1 1 0; min-width: 268px; max-width: 420px;
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--line-soft);
  border-radius: var(--radius); overflow: hidden;
}
.column.is-dropzone { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 22%, transparent) inset; }
.column__head {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  border-bottom: 1px solid var(--line-soft); font-weight: 600; font-size: 13.5px;
}
.column__dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 8px; background: var(--hint); }
.column__n { margin-left: auto; color: var(--hint); font-size: 12px; font-weight: 600; }
.column__body { flex: 1; overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 8px; }
.column__empty { color: var(--hint); font-size: 12.5px; text-align: center; padding: 22px 8px; border: 1px dashed var(--line); border-radius: var(--radius-sm); }

.card {
  position: relative; padding: 10px 12px 10px 14px;
  background: var(--bg); border: 1px solid var(--line-soft); border-radius: var(--radius-sm);
  cursor: pointer; user-select: none; touch-action: pan-y;
}
.card::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; border-radius: 3px 0 0 3px; background: var(--medium); }
.card[data-priority="high"]::before { background: var(--high); }
.card[data-priority="low"]::before { background: var(--low); }
.card:hover { border-color: var(--line); box-shadow: var(--shadow); }
.card.is-ghost { opacity: .35; }
.card.is-dragging {
  position: fixed; z-index: 90; pointer-events: none; width: var(--drag-w);
  box-shadow: var(--shadow-lg); transform: rotate(1.4deg); cursor: grabbing;
}
.card__top { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.card__id { color: var(--hint); font-size: 11.5px; font-weight: 600; }
.card__title { font-size: 14px; line-height: 1.35; word-break: break-word; }
.card__meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 8px; }
.card__who { display: flex; align-items: center; gap: 5px; margin-left: auto; color: var(--hint); font-size: 11.5px; }
.mini-avatar { width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; font-size: 9.5px; font-weight: 700; background: var(--bg-soft); color: var(--text); }

.tag { padding: 2px 7px; border-radius: 5px; background: var(--bg-soft); color: var(--hint); font-size: 11px; }
.pill { display: inline-flex; align-items: center; gap: 4px; padding: 2px 7px; border-radius: 5px; font-size: 11px; background: var(--bg-soft); color: var(--hint); }
.pill--due { background: color-mix(in srgb, var(--medium) 18%, transparent); color: color-mix(in srgb, var(--medium) 92%, var(--text)); }
.pill--overdue { background: color-mix(in srgb, var(--high) 16%, transparent); color: var(--high); font-weight: 600; }
.placeholder { border: 2px dashed var(--accent); border-radius: var(--radius-sm); background: color-mix(in srgb, var(--accent) 8%, transparent); }

/* -------------------------------- список -------------------------------- */
.list-wrap { flex: 1; overflow: auto; padding: 12px; }
.table { width: 100%; border-collapse: separate; border-spacing: 0; background: var(--surface); border: 1px solid var(--line-soft); border-radius: var(--radius); overflow: hidden; min-width: 720px; }
.table th, .table td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
.table th { background: var(--bg-soft); font-size: 12px; color: var(--hint); font-weight: 600; position: sticky; top: 0; z-index: 2; cursor: pointer; white-space: nowrap; }
.table th.is-sorted { color: var(--accent); }
.table tbody tr { cursor: pointer; }
.table tbody tr:hover { background: var(--hover); }
.table tr:last-child td { border-bottom: 0; }
.table td.cell--title { font-weight: 500; }
.table select { padding: 4px 6px; border: 1px solid var(--line-soft); border-radius: 6px; background: var(--bg-soft); }

/* ------------------------------- аналитика ------------------------------ */
.dash { flex: 1; overflow-y: auto; padding: 14px; display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); align-content: start; }
.tiles { grid-column: 1 / -1; display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
.tile { background: var(--surface); border: 1px solid var(--line-soft); border-radius: var(--radius); padding: 14px; }
.tile__n { font-size: 26px; font-weight: 700; line-height: 1.1; }
.tile__label { color: var(--hint); font-size: 12.5px; margin-top: 4px; }
.tile--alert .tile__n { color: var(--high); }
.panel { background: var(--surface); border: 1px solid var(--line-soft); border-radius: var(--radius); padding: 14px; }
.panel h3 { font-size: 13px; margin-bottom: 12px; }
.bar-row { display: grid; grid-template-columns: 108px 1fr 42px; align-items: center; gap: 10px; margin-bottom: 9px; font-size: 12.5px; }
.bar { height: 8px; border-radius: 999px; background: var(--bg-soft); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 999px; background: var(--accent); }
.bar-row__n { text-align: right; color: var(--hint); font-variant-numeric: tabular-nums; }

/* ------------------------------ уведомления ----------------------------- */
.feed { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.feed__item { background: var(--surface); border: 1px solid var(--line-soft); border-left: 3px solid transparent; border-radius: var(--radius-sm); padding: 10px 12px; }
.feed__item.is-unread { border-left-color: var(--accent); }
.feed__time { color: var(--hint); font-size: 11.5px; margin-top: 4px; }

/* -------------------------------- drawer -------------------------------- */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.42); z-index: 40; animation: fade .16s; }
@keyframes fade { from { opacity: 0; } }

.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: var(--drawer-w); max-width: 100vw;
  background: var(--bg); border-left: 1px solid var(--line-soft);
  z-index: 50; display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg); animation: slide-in .2s ease;
}
@keyframes slide-in { from { transform: translateX(24px); opacity: .4; } }
.drawer__inner { flex: 1; overflow-y: auto; padding: 14px 16px calc(24px + var(--safe-b)); }

.dhead { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 12px; }
.dhead__id { color: var(--hint); font-size: 12.5px; font-weight: 600; }
.dhead h2 { font-size: 17px; line-height: 1.3; word-break: break-word; margin-top: 2px; }
.dsection { margin-top: 16px; }
.dsection > h4 { font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--hint); margin-bottom: 8px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.editable {
  padding: 9px 10px; background: var(--surface); border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm); min-height: 38px; white-space: pre-wrap; word-break: break-word; cursor: text;
}
.editable:hover { border-color: var(--line); }
.editable--empty { color: var(--hint); }
.hist { display: flex; flex-direction: column; gap: 8px; }
.hist__row { display: flex; gap: 8px; font-size: 12.5px; }
.hist__icon { flex: 0 0 18px; text-align: center; }
.hist__time { color: var(--hint); font-size: 11px; }
.drawer .row-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }

/* -------------------------------- модалка ------------------------------- */
.modal { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 16px; background: rgba(0,0,0,.45); }
.modal__card {
  width: min(560px, 100%); max-height: 88dvh; overflow-y: auto;
  background: var(--bg); border-radius: 16px; padding: 18px;
  box-shadow: var(--shadow-lg); animation: pop .18s ease;
}
@keyframes pop { from { transform: scale(.97); opacity: .5; } }
.modal__head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal__head h2 { font-size: 17px; }
.form { display: flex; flex-direction: column; gap: 12px; }
.form textarea { min-height: 92px; resize: vertical; }
.form__actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; }
.radio-row { display: flex; gap: 6px; }
.radio-row .chip { flex: 1; text-align: center; }

/* --------------------------------- тосты -------------------------------- */
.toasts { position: fixed; left: 50%; bottom: calc(18px + var(--safe-b)); transform: translateX(-50%); z-index: 80; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { padding: 9px 16px; border-radius: 999px; background: #202b36; color: #fff; font-size: 13px; box-shadow: var(--shadow-lg); animation: pop .18s ease; max-width: 92vw; }
.toast--error { background: var(--danger); }
.toast--ok { background: #2b8a3e; }

.empty { display: grid; place-content: center; justify-items: center; gap: 8px; height: 100%; color: var(--hint); text-align: center; padding: 30px; }
.empty__icon { font-size: 34px; opacity: .5; }

/* ------------------------------- адаптив -------------------------------- */
@media (max-width: 1080px) {
  :root { --sidebar-w: 210px; --drawer-w: 400px; }
}

@media (max-width: 860px) {
  :root { --topbar-h: 52px; }
  .topbar__burger { display: grid; }
  .topbar__title { display: none; }
  .search { margin-left: 0; }
  .btn__label { display: none; }
  .btn--primary { width: 36px; padding: 0; }
  .seg { display: none; }

  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; width: 282px; z-index: 45;
    transform: translateX(-100%); transition: transform .2s ease; padding-top: 14px;
  }
  .sidebar.is-open { transform: none; }

  .board { padding: 10px; gap: 10px; scroll-snap-type: x mandatory; }
  .column { min-width: min(84vw, 340px); scroll-snap-align: start; }
  .drawer { width: 100vw; border-left: 0; animation: slide-up .22s ease; }
  @keyframes slide-up { from { transform: translateY(30px); opacity: .4; } }
  .list-wrap { padding: 8px; }
  .dash { grid-template-columns: 1fr; padding: 10px; }
  .grid-2 { grid-template-columns: 1fr; }
}

@media (max-width: 480px) {
  .topbar { gap: 6px; padding: 0 8px; }
  .topbar__actions { gap: 4px; }
  #refresh { display: none; }
}
