:root {
  --bg: #faf9f6;
  --surface: #ffffff;
  --grid: #e5e7f0;
  --grid-half: #f1f2f7;
  --text: #1e2034;
  --muted: #767a91;
  --accent: #4f46e5;
  --accent2: #14b8a6;
  --today-bg: #f0eeff;
  --now: #ef4444;
  --weekend: #be5a78;
  --danger: #dc2626;
  --radius: 14px;
  --shadow: 0 8px 28px rgba(30, 32, 52, 0.12);
  --topbar-h: 132px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --tg-bg: var(--tg-theme-bg-color, var(--bg));
  --tg-text: var(--tg-theme-text-color, var(--text));
  --tg-hint: var(--tg-theme-hint-color, var(--muted));
  --tg-btn: var(--tg-theme-button-color, var(--accent));
  --tg-btn-text: var(--tg-theme-button-text-color, #fff);
  --tg-secondary: var(--tg-theme-secondary-bg-color, var(--surface));
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--tg-bg);
  color: var(--tg-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
#app { display: flex; flex-direction: column; height: 100%; height: 100dvh; }

.topbar {
  flex: 0 0 auto;
  background: var(--tg-secondary);
  border-bottom: 1px solid var(--grid);
  padding: 10px 12px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  position: sticky; top: 0; z-index: 20;
}
.nav-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  background: var(--bg); color: var(--accent); font-size: 26px; line-height: 1;
  display: grid; place-items: center;
}
.icon-btn:active { transform: scale(0.96); }
.today-btn {
  padding: 8px 14px; border-radius: 999px;
  background: var(--accent2); color: #fff; font-weight: 600; font-size: 13px;
}
.title {
  margin: 8px 0 10px; font-size: 20px; font-weight: 700; letter-spacing: -0.02em;
  text-align: center; color: var(--tg-text);
}
.segment {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px;
  background: var(--bg); padding: 4px; border-radius: 12px;
}
.seg {
  padding: 8px 0; border-radius: 10px; color: var(--muted); font-weight: 600; font-size: 13px;
}
.seg.active {
  background: var(--surface); color: var(--accent);
  box-shadow: 0 1px 4px rgba(30,32,52,0.08);
}

.main { flex: 1; min-height: 0; position: relative; overflow: hidden; }
.view-root {
  height: 100%; overflow: auto; -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  padding-bottom: calc(88px + var(--safe-b));
}
.status {
  position: absolute; left: 50%; top: 40%; transform: translate(-50%, -50%);
  background: rgba(30,32,52,0.85); color: #fff; padding: 10px 16px;
  border-radius: 12px; font-size: 13px; z-index: 5;
}

/* Month */
.month-grid {
  display: grid; grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 1px; background: var(--grid); border-top: 1px solid var(--grid);
  min-height: 100%;
}
.wd {
  background: var(--tg-secondary); text-align: center; padding: 8px 0;
  font-size: 11px; font-weight: 700; color: var(--muted); position: sticky; top: 0; z-index: 2;
}
.wd.weekend { color: var(--weekend); }
.day-cell {
  background: var(--tg-bg); min-height: 92px; padding: 6px 4px 4px;
  display: flex; flex-direction: column; gap: 2px;
}
.day-cell.out { opacity: 0.35; }
.day-cell.today { background: var(--today-bg); }
.day-num {
  width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center; font-size: 13px; font-weight: 600; margin-bottom: 2px;
}
.day-cell.today .day-num { background: var(--accent); color: #fff; }
.day-cell.weekend .day-num { color: var(--weekend); }
.chip {
  font-size: 10px; line-height: 1.2; padding: 2px 4px; border-radius: 5px;
  border-left: 3px solid transparent; min-width: 0;
  overflow: hidden; overflow-wrap: anywhere; word-break: break-word;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical;
}
.chip b { display: block; font-weight: 700; opacity: 0.75; }
.day-cell { min-width: 0; padding: 4px 2px 3px; }
@media (max-width: 480px) { .chip { font-size: 9px; padding: 2px 2px; border-left-width: 2px; } }
.chip-more { font-size: 10px; color: var(--muted); padding: 1px 4px; }

/* Week / Day timeline */
.timeline { display: flex; flex-direction: column; min-height: 100%; }
.tl-head {
  display: grid; position: sticky; top: 0; z-index: 3;
  background: var(--tg-secondary); border-bottom: 1px solid var(--grid);
}
.tl-head.week { grid-template-columns: 52px repeat(7, minmax(0, 1fr)); }
.tl-head.day { grid-template-columns: 52px minmax(0, 1fr); }
.tl-gutter { }
.tl-day-h {
  text-align: center; padding: 8px 2px 10px; border-left: 1px solid var(--grid);
}
.tl-day-h .wd-l { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; }
.tl-day-h.weekend .wd-l { color: var(--weekend); }
.tl-day-h .dn {
  display: inline-grid; place-items: center; width: 32px; height: 32px;
  border-radius: 50%; font-size: 16px; font-weight: 700; margin-top: 2px;
}
.tl-day-h.today .dn { background: var(--accent); color: #fff; }
.tl-day-h.today .wd-l { color: var(--accent); }

.allday {
  display: grid; border-bottom: 1px solid var(--grid); background: var(--surface);
}
.allday.week { grid-template-columns: 52px repeat(7, minmax(0, 1fr)); }
.allday.day { grid-template-columns: 52px minmax(0, 1fr); }
.allday-label {
  font-size: 10px; color: var(--muted); padding: 8px 4px; text-align: right;
}
.allday-col { padding: 4px; border-left: 1px solid var(--grid); min-height: 28px; display: flex; flex-direction: column; gap: 3px; }
.allday-chip {
  font-size: 11px; padding: 4px 6px; border-radius: 7px;
  border-left: 3px solid; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.tl-body { position: relative; }
.tl-body.week { display: grid; grid-template-columns: 52px repeat(7, minmax(0, 1fr)); }
.tl-body.day { display: grid; grid-template-columns: 52px minmax(0, 1fr); }
.hours { position: relative; }
.hour-row {
  height: 52px; border-bottom: 1px solid var(--grid); position: relative;
}
.hour-row::after {
  content: ""; position: absolute; left: 0; right: 0; top: 50%;
  border-top: 1px dashed var(--grid-half);
}
.hour-label {
  height: 52px; font-size: 11px; color: var(--muted);
  text-align: right; padding-right: 6px; transform: translateY(-7px);
}
.col {
  position: relative; border-left: 1px solid var(--grid);
  background: var(--tg-bg);
}
.col.today { background: var(--today-bg); }
.ev-block {
  position: absolute; border-radius: 8px; padding: 3px 5px 3px 7px;
  overflow: hidden; border-left: 3px solid; font-size: 11px; line-height: 1.25;
  box-shadow: 0 1px 2px rgba(30,32,52,0.06); z-index: 1;
  cursor: pointer;
}
.ev-block .t { font-weight: 700; font-size: 10px; opacity: 0.75; }
.ev-block .n { font-weight: 600; }
.ev-block.done { opacity: 0.55; text-decoration: line-through; }
.now-line {
  position: absolute; left: 0; right: 0; height: 2px; background: var(--now);
  z-index: 4; pointer-events: none;
}
.now-line::before {
  content: ""; position: absolute; left: -5px; top: -4px;
  width: 10px; height: 10px; border-radius: 50%; background: var(--now);
}

.empty-hint {
  position: absolute; left: 50%; top: 30%; transform: translateX(-50%);
  color: var(--muted); font-size: 14px; pointer-events: none;
}

.fab {
  position: fixed; right: 18px; bottom: calc(18px + var(--safe-b));
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--tg-btn); color: var(--tg-btn-text);
  font-size: 32px; line-height: 1; font-weight: 300;
  box-shadow: var(--shadow); z-index: 30;
  display: grid; place-items: center;
}
.fab:active { transform: scale(0.95); }

.sheet[hidden], .sheet-backdrop[hidden] { display: none !important; }
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(20, 22, 40, 0.45);
  z-index: 40; backdrop-filter: blur(2px);
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  background: var(--tg-secondary); border-radius: 20px 20px 0 0;
  box-shadow: var(--shadow); max-height: 88vh;
  display: flex; flex-direction: column;
  padding-bottom: var(--safe-b);
  animation: slideUp 0.22s ease-out;
}
@keyframes slideUp {
  from { transform: translateY(40%); opacity: 0.6; }
  to { transform: translateY(0); opacity: 1; }
}
.sheet-handle {
  width: 40px; height: 4px; border-radius: 2px; background: var(--grid);
  margin: 10px auto 4px;
}
.sheet-body { padding: 8px 16px 12px; overflow: auto; }
.sheet-title { margin: 0 0 12px; font-size: 18px; }
.sheet-actions {
  display: flex; gap: 8px; padding: 8px 16px 16px; border-top: 1px solid var(--grid);
}
.btn {
  flex: 1; padding: 12px 14px; border-radius: 12px; font-weight: 600;
}
.btn.primary { background: var(--tg-btn); color: var(--tg-btn-text); }
.btn.ghost { background: var(--bg); color: var(--tg-text); }
.btn.danger { background: #fee2e2; color: var(--danger); }
.btn:disabled { opacity: 0.5; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.field > span { font-size: 12px; font-weight: 600; color: var(--muted); }
.field input, .field select, .field textarea {
  border: 1px solid var(--grid); border-radius: 12px; padding: 11px 12px;
  background: var(--surface); outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79,70,229,0.15);
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.smart-row { display: flex; gap: 8px; }
.smart-row input { flex: 1; }
.smart-row .btn { flex: 0 0 auto; padding: 10px 12px; }
.form-error { color: var(--danger); font-size: 13px; margin: 0 0 8px; }

.detail-title { font-size: 20px; font-weight: 700; margin: 4px 0 8px; }
.detail-meta { color: var(--muted); font-size: 14px; margin-bottom: 8px; }
.detail-notes { background: var(--bg); border-radius: 12px; padding: 12px; margin-top: 8px; white-space: pre-wrap; }
.badge {
  display: inline-block; font-size: 11px; font-weight: 700; padding: 3px 8px;
  border-radius: 999px; background: var(--today-bg); color: var(--accent); margin-bottom: 6px;
}
.badge.task { background: #d1fae5; color: #0f766e; }

/* ---------- Заметки (дела без даты) ---------- */
.notes-mode .nav-row { visibility: hidden; }
.notes-mode .fab { display: none; }
.notes-view { padding: 12px 12px 0; max-width: 680px; margin: 0 auto; }
.note-add { display: flex; gap: 8px; margin-bottom: 12px; }
.note-add input {
  flex: 1; min-width: 0; border: 1px solid var(--grid); border-radius: 12px;
  padding: 11px 12px; background: var(--surface); color: var(--tg-text); outline: none;
  font-size: 16px; /* без автозума на iOS */
}
.note-add input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79,70,229,0.15); }
.note-add .btn { flex: 0 0 auto; padding: 10px 14px; }
.notes-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.note-item {
  display: flex; align-items: flex-start; gap: 6px; background: var(--surface);
  border: 1px solid var(--grid); border-radius: 12px; padding: 10px 6px 10px 12px;
}
.note-label { flex: 1; min-width: 0; display: flex; align-items: flex-start; gap: 10px; cursor: pointer; }
.note-label input[type="checkbox"] {
  flex: 0 0 auto; width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--accent);
}
.note-text { flex: 1; min-width: 0; font-size: 15px; line-height: 1.35; overflow-wrap: anywhere; white-space: pre-wrap; }
.note-item.done .note-text { text-decoration: line-through; color: var(--muted); }
.note-del {
  flex: 0 0 auto; width: 32px; height: 32px; border-radius: 8px; color: var(--muted);
  font-size: 16px; line-height: 1; background: transparent;
}
.note-del:active { background: #fee2e2; color: var(--danger); }
.notes-empty { color: var(--muted); text-align: center; padding: 32px 8px; }
.notes-clear { display: block; width: 100%; margin-top: 12px; }
@media (max-width: 380px) { .seg { font-size: 12px; } }
