:root {
  --bg: #f1f2f6;
  --card: #ffffff;
  --text: #16171b;
  --hint: #858994;
  --accent: #2f7bf5;
  --accent-text: #ffffff;
  --danger: #e5484d;
  --warn: #c76a00;
  --ok: #1f9d63;
  --shade: rgba(120, 124, 136, 0.11);
  --shade-strong: rgba(120, 124, 136, 0.2);
  --line: rgba(20, 22, 30, 0.08);
  --shadow: 0 1px 2px rgba(20, 22, 30, 0.06), 0 1px 6px rgba(20, 22, 30, 0.04);
  --shadow-lift: 0 10px 28px rgba(20, 22, 30, 0.18);
  --c-inbox: #8e949e;
  --c-today: #f59e0b;
  --c-week: #3b82f6;
  --c-later: #8b5cf6;
  --c-done: #22a06b;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111214;
    --card: #222327;
    --text: #ececef;
    --hint: #8e919a;
    --accent: #4c8dff;
    --warn: #f0a33a;
    --ok: #3ccf8e;
    --shade: rgba(160, 162, 172, 0.12);
    --shade-strong: rgba(160, 162, 172, 0.22);
    --line: rgba(255, 255, 255, 0.07);
    --shadow: none;
    --shadow-lift: 0 10px 28px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #111214;
  --card: #222327;
  --text: #ececef;
  --hint: #8e919a;
  --accent: #4c8dff;
  --warn: #f0a33a;
  --ok: #3ccf8e;
  --shade: rgba(160, 162, 172, 0.12);
  --shade-strong: rgba(160, 162, 172, 0.22);
  --line: rgba(255, 255, 255, 0.07);
  --shadow: none;
  --shadow-lift: 0 10px 28px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }

body {
  margin: 0;
  height: 100vh;
  height: var(--tg-viewport-stable-height, 100dvh);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  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, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.i {
  width: 16px;
  height: 16px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- шапка ---------- */

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px 6px;
  padding-top: max(10px, env(safe-area-inset-top));
}

.today { flex: 1; min-width: 0; }
.today-date { font-size: 17px; font-weight: 650; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.today-stats { margin-top: 1px; color: var(--hint); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.danger-text { color: var(--danger); }

.search {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 260px;
  height: 36px;
  padding: 0 10px;
  border-radius: 10px;
  background: var(--shade);
  color: var(--hint);
}
.search input { flex: 1; min-width: 0; border: 0; background: transparent; outline: none; color: var(--text); }
.search:focus-within { box-shadow: 0 0 0 2px var(--accent) inset; }

.filter {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  max-width: 190px;
  padding: 0 10px;
  border-radius: 10px;
  background: var(--shade);
  color: var(--hint);
  font-size: 14px;
}
.filter-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.filter select { position: absolute; inset: 0; width: 100%; opacity: 0; cursor: pointer; }
.filter:focus-within { box-shadow: 0 0 0 2px var(--accent) inset; }
.filter.active { background: rgba(47, 123, 245, 0.12); color: var(--accent); }
.filter.active .filter-label { color: var(--accent); font-weight: 600; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 10px;
  background: var(--shade);
  white-space: nowrap;
  font-weight: 500;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger { background: transparent; color: var(--danger); padding: 0 6px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  min-height: 36px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--hint);
}
.icon-btn:hover { background: var(--shade); color: var(--text); }
.icon-btn .i { width: 18px; height: 18px; }

.narrow-only { display: none; }

@media (max-width: 760px) {
  .wide-only { display: none; }
  .narrow-only { display: inline-flex; }
  #btn-new { width: 36px; padding: 0; }
  .search { display: none; }
  .filter { padding: 0; width: 36px; justify-content: center; }
  .filter .i { width: 18px; height: 18px; }
  .filter-label { display: none; }
  .filter:not(.active) { background: transparent; }
  .topbar.searching .today, .topbar.searching .filter, .topbar.searching #btn-search { display: none; }
  .topbar.searching .search { display: flex; flex: 1; width: auto; }
}

/* ---------- вкладки колонок (узкий экран) ---------- */

.tabs {
  flex: none;
  display: none;
  gap: 6px;
  padding: 4px 12px 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
@media (max-width: 899px) { .tabs { display: flex; } }

.tab {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 11px;
  border: 0;
  border-radius: 15px;
  background: var(--shade);
  color: var(--hint);
  font-size: 13px;
  font-weight: 550;
}
.tab .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--dot); }
.tab-count { color: var(--hint); font-weight: 500; }
.tab.active { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* ---------- доска ---------- */

.board {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  gap: 10px;
  padding: 2px 12px calc(12px + env(safe-area-inset-bottom));
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scroll-padding: 0 12px;
  overscroll-behavior-x: contain;
}

.column {
  flex: 0 0 min(88vw, 340px);
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-radius: 16px;
  background: var(--shade);
  scroll-snap-align: start;
}

@media (min-width: 900px) {
  .board { scroll-snap-type: none; padding-top: 6px; }
  .column { flex-basis: 296px; }
}

.column-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 11px 14px 6px;
}
.column-title { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 650; }
.column-title .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dot); }
.count { color: var(--hint); font-size: 13px; font-weight: 500; }

.quick-add { padding: 2px 8px 6px; }
.quick-add-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  border-radius: 10px;
  color: var(--hint);
}
.quick-add-row:focus-within { background: var(--card); box-shadow: var(--shadow); color: var(--accent); }
.quick-add input { flex: 1; min-width: 0; height: 38px; border: 0; background: transparent; outline: none; color: var(--text); }
.quick-add input::placeholder { color: var(--hint); }

.preview { display: flex; flex-wrap: wrap; gap: 4px; padding: 6px 10px 2px; }
.preview[hidden] { display: none; }

.cards {
  flex: 1;
  min-height: 60px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 2px 8px 10px;
}
.cards:empty::before {
  content: attr(data-empty);
  display: block;
  margin: 6px 4px;
  padding: 18px 12px;
  border: 1.5px dashed var(--line);
  border-radius: 12px;
  color: var(--hint);
  font-size: 13px;
  text-align: center;
}

.card {
  position: relative;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 11px;
  border-radius: 12px;
  background: var(--card);
  box-shadow: var(--shadow);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  transition: transform 0.12s ease, opacity 0.25s ease, box-shadow 0.12s ease;
}
.card[hidden] { display: none; }
.card::before {
  content: "";
  position: absolute;
  left: 0;
  top: 10px;
  bottom: 10px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: transparent;
}
.card.prio-1::before { background: #5b9cf3; }
.card.prio-2::before { background: #f0a020; }
.card.prio-3::before { background: var(--danger); }
@media (hover: hover) {
  .card:hover { box-shadow: var(--shadow), 0 0 0 1px var(--line); }
}

.check {
  position: relative;
  flex: none;
  width: 20px;
  height: 20px;
  margin-top: 1px;
  padding: 0;
  border: 1.6px solid var(--hint);
  border-radius: 50%;
  background: transparent center / 12px no-repeat;
  transition: background-color 0.18s ease, border-color 0.18s ease;
}
.check::after { content: ""; position: absolute; inset: -10px; } /* шире зона нажатия */
.card.prio-3 .check { border-color: var(--danger); }
.check:hover { border-color: var(--accent); }
.card.is-done .check,
.card.completing .check {
  border-color: var(--ok);
  background-color: var(--ok);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 6.2l2.3 2.3 4.7-5' fill='none' stroke='white' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}
.card.completing { opacity: 0.45; transform: scale(0.98); }
.card.completing .card-title, .card.is-done .card-title { color: var(--hint); text-decoration: line-through; }

.card-body { flex: 1; min-width: 0; }
.card-title { overflow-wrap: anywhere; line-height: 1.32; }

.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  height: 20px;
  padding: 0 6px;
  border-radius: 6px;
  background: var(--shade);
  color: var(--hint);
  font-size: 12px;
  white-space: nowrap;
}
.chip .i { width: 12px; height: 12px; }
.chip.today { color: var(--warn); background: rgba(245, 158, 11, 0.14); }
.chip.soon { color: var(--accent); background: rgba(47, 123, 245, 0.1); }
.chip.overdue { color: var(--danger); background: rgba(229, 72, 77, 0.12); }
.chip.ok { color: var(--ok); }
.chip.tag { padding: 0 2px; background: transparent; color: var(--accent); }
.chip.route { background: transparent; padding: 0 2px; }
.pdot { width: 7px; height: 7px; border-radius: 50%; background: hsl(var(--h) 62% 52%); }

.sortable-ghost { opacity: 0.35; }
.sortable-chosen { box-shadow: var(--shadow-lift); }
.sortable-drag { transform: rotate(1.2deg); }

.notice { margin: auto; padding: 24px; max-width: 440px; text-align: center; color: var(--hint); line-height: 1.5; }

/* ---------- окно задачи ---------- */

.sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.4); animation: fade 0.18s ease-out; }

.sheet-panel {
  position: relative;
  width: 100%;
  max-width: 580px;
  max-height: 94%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom));
  border-radius: 18px 18px 0 0;
  background: var(--card);
  animation: rise 0.2s cubic-bezier(0.2, 0.9, 0.3, 1);
}
.sheet-panel > * { flex-shrink: 0; } /* иначе при прокрутке поля сплющиваются */
@media (min-width: 700px) {
  .sheet { align-items: center; }
  .sheet-panel { border-radius: 18px; max-height: 88%; }
}
@keyframes rise { from { transform: translateY(28px); } }
@keyframes fade { from { opacity: 0; } }

.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: -6px; }
.hint { color: var(--hint); font-size: 13px; }

.sheet-panel textarea {
  width: 100%;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
}
.title-input { padding: 2px 0; font-size: 19px; font-weight: 650; line-height: 1.3; }
.notes-input { padding: 8px 10px; border-radius: 10px; background: var(--shade) !important; font-size: 14px; }
.sheet-panel textarea:focus { box-shadow: none; }
.notes-input:focus { box-shadow: 0 0 0 2px var(--accent) inset !important; }

.group { display: flex; flex-direction: column; gap: 8px; }
.group-title { color: var(--hint); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }

.chips-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chips-row button {
  height: 30px;
  padding: 0 12px;
  border: 0;
  border-radius: 15px;
  background: var(--shade);
  font-size: 13px;
}
.chips-row button.active { background: var(--accent); color: var(--accent-text); }

.pair { display: grid; grid-template-columns: 1.4fr 1fr; gap: 8px; }
.pair input, .field input, .field select, .checklist-new {
  width: 100%;
  min-height: 38px;
  padding: 0 10px;
  border: 0;
  border-radius: 10px;
  background: var(--shade);
  outline: none;
}
.pair input:focus, .field input:focus, .field select:focus, .checklist-new:focus { box-shadow: 0 0 0 2px var(--accent) inset; }

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 360px) { .row { grid-template-columns: 1fr; } }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { color: var(--hint); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }

.segmented { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 3px; border-radius: 11px; background: var(--shade); }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  height: 32px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--hint);
  cursor: pointer;
}
.segmented span i { width: 7px; height: 7px; border-radius: 50%; }
.segmented .p1 i { background: #5b9cf3; }
.segmented .p2 i { background: #f0a020; }
.segmented .p3 i { background: var(--danger); }
.segmented input:checked + span { background: var(--card); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }
@media (max-width: 380px) { .segmented span { font-size: 12px; } }

.checklist { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.checklist li { display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--line); }
.checklist input[type="checkbox"] { width: 18px; height: 18px; flex: none; margin: 0; accent-color: var(--ok); }
.checklist input[type="text"] { flex: 1; min-width: 0; padding: 9px 0; border: 0; background: transparent; outline: none; }
.checklist li.done input[type="text"] { color: var(--hint); text-decoration: line-through; }
.checklist .icon-btn { min-width: 30px; min-height: 30px; }

.sheet-actions { display: flex; align-items: center; gap: 8px; padding-top: 4px; }
.spacer { flex: 1; }
.in-tg #btn-save { display: none; } /* в Telegram сохраняет нативная кнопка внизу */

/* ---------- уведомление снизу ---------- */

.toast {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(16px + env(safe-area-inset-bottom));
  width: max-content;
  margin: 0 auto;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 16px;
  max-width: calc(100% - 32px);
  padding: 10px 14px;
  border-radius: 12px;
  background: rgba(28, 29, 33, 0.96);
  color: #fff;
  font-size: 14px;
  box-shadow: var(--shadow-lift);
  animation: rise 0.18s ease-out;
}
.toast[hidden] { display: none; }
.toast button { border: 0; background: none; color: #8ab4ff; font-weight: 600; padding: 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before { animation: none !important; transition: none !important; }
}
