:root {
  --bg: var(--tg-theme-bg-color, #0f0f12);
  --text: var(--tg-theme-text-color, #f2f2f4);
  --hint: var(--tg-theme-hint-color, #8b8b96);
  --link: var(--tg-theme-link-color, #6ab7ff);
  --btn: var(--tg-theme-button-color, #3390ec);
  --btn-t: var(--tg-theme-button-text-color, #fff);
  --card: var(--tg-theme-secondary-bg-color, #1a1a22);
  --danger: #e85d5d;
  --warn: #e6a23c;
  --ok: #58d68d;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); min-height: 100vh; padding-bottom: 2rem; }
.hidden { display: none !important; }

.top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; padding: 12px 16px 8px; position: sticky; top: 0; background: var(--bg); z-index: 2; }
.top h1 { margin: 0; font-size: 1.25rem; font-weight: 700; }
.sub { margin: 4px 0 0; font-size: 0.85rem; color: var(--hint); }

.btn { border: none; border-radius: 10px; padding: 10px 14px; font-size: 0.95rem; font-weight: 650; background: var(--card); color: var(--text); cursor: pointer; }
.btn.primary { background: var(--btn); color: var(--btn-t); }
.btn.ghost { background: transparent; color: var(--link); }
.btn.warn { background: rgba(230, 162, 60, 0.2); color: var(--warn); }
.btn.danger { background: rgba(232, 93, 93, 0.2); color: var(--danger); }

.view { padding-bottom: 24px; }
.hero-card { margin: 10px 16px 14px; padding: 18px; border-radius: 18px; background: linear-gradient(135deg, rgba(51,144,236,.22), rgba(88,214,141,.08)), var(--card); border: 1px solid rgba(255,255,255,.08); }
.hero-card h2 { margin: 4px 0 8px; font-size: 1.55rem; }
.hero-card p { margin: 0; color: var(--hint); line-height: 1.35; }
.eyebrow { color: var(--link) !important; text-transform: uppercase; font-size: .72rem; letter-spacing: .08em; font-weight: 800; }

.menu-grid { display: grid; gap: 10px; padding: 0 16px; }
.menu-grid.compact { padding-top: 6px; }
.menu-card { width: 100%; display: flex; gap: 12px; align-items: center; text-align: left; border: 1px solid rgba(255,255,255,.08); background: var(--card); color: var(--text); border-radius: 16px; padding: 15px; cursor: pointer; }
.menu-card:active { opacity: .92; }
.menu-card.disabled { opacity: .55; cursor: default; }
.menu-icon { width: 38px; height: 38px; display: inline-flex; align-items: center; justify-content: center; border-radius: 12px; background: rgba(51,144,236,.16); color: var(--link); font-weight: 800; flex: 0 0 38px; }
.menu-card b { display: block; font-size: 1rem; margin-bottom: 3px; }
.menu-card small { display: block; color: var(--hint); line-height: 1.3; }

.tabs { display: flex; gap: 8px; padding: 0 16px 12px; }
.tab { flex: 1; padding: 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.08); background: var(--card); color: var(--hint); font-weight: 650; }
.tab.active { color: var(--text); border-color: var(--btn); background: rgba(51, 144, 236, 0.15); }

.task-list { list-style: none; margin: 0; padding: 0 12px; }
.task-list li, .info-card { background: var(--card); border-radius: 14px; padding: 14px; margin-bottom: 10px; cursor: pointer; border: 1px solid rgba(255,255,255,0.06); }
.task-list li:active { opacity: 0.92; }
.task-list .t-title { font-weight: 650; margin: 0 0 6px; font-size: 1rem; }
.task-list .t-row { font-size: 0.8rem; color: var(--hint); display: flex; gap: 8px; flex-wrap: wrap; }
.pill { display: inline-block; font-size: 0.72rem; padding: 2px 8px; border-radius: 999px; background: rgba(255,255,255,0.08); color: var(--hint); }
.pill.done { background: rgba(80, 180, 120, 0.25); color: #9dffb0; }
.empty { text-align: center; color: var(--hint); padding: 2rem 1rem; }
.banner.err { margin: 0 16px 12px; padding: 12px; border-radius: 12px; background: rgba(232, 93, 93, 0.15); color: #ffb4b4; font-size: 0.9rem; }

.chat-layout { padding: 0 16px; display: flex; flex-direction: column; gap: 10px; }
.label { display: block; color: var(--hint); font-size: .82rem; margin: 0 0 6px; }
.select, .send-box textarea, .modal-box textarea { width: 100%; border-radius: 12px; border: 1px solid rgba(255,255,255,.12); background: var(--card); color: var(--text); padding: 11px; font-size: 1rem; }

.chats-rows { display: flex; flex-direction: column; padding: 0 0 12px; }
.chat-row { display: flex; align-items: center; gap: 12px; padding: 12px 16px; cursor: pointer; border-bottom: 1px solid rgba(255,255,255,.05); background: transparent; border-left: 0; border-right: 0; border-top: 0; width: 100%; text-align: left; color: var(--text); }
.chat-row:active { background: rgba(255,255,255,.04); }
.chat-avatar { width: 46px; height: 46px; flex: 0 0 46px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 1rem; color: #fff; text-transform: uppercase; letter-spacing: .02em; }
.chat-row-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.chat-row-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.chat-row-title { font-weight: 650; font-size: 0.98rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row-snippet { color: var(--text); font-size: 0.85rem; line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row-snippet.muted { color: var(--hint); font-style: italic; }
.chat-row-badge { font-size: 0.65rem; color: var(--hint); text-transform: uppercase; letter-spacing: .06em; flex: 0 0 auto; }
.chat-row-time { font-size: 0.72rem; color: var(--hint); flex: 0 0 auto; }
.chat-row-unread {
  min-width: 22px; height: 22px; padding: 0 7px; border-radius: 999px;
  background: var(--btn); color: var(--btn-t); font-weight: 700; font-size: 0.72rem;
  display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto; margin-left: 4px;
}

.thread-layout { display: flex; flex-direction: column; gap: 10px; padding: 0 16px; min-height: calc(100vh - 90px); }
.thread-layout .chat-list { flex: 1; }

.chat-list { display: flex; flex-direction: column; gap: 8px; max-height: 60vh; overflow: auto; padding: 4px 0; }
.msg { max-width: 88%; border-radius: 14px; padding: 9px 11px; border: 1px solid rgba(255,255,255,.06); background: var(--card); }
.msg.out { align-self: flex-end; background: rgba(51,144,236,.2); }
.msg.in { align-self: flex-start; }
.msg-meta { color: var(--hint); font-size: .72rem; margin-bottom: 4px; }
.msg-body { white-space: pre-wrap; line-height: 1.35; }
.send-box { display: flex; flex-direction: column; gap: 8px; }

.week-nav { display: flex; align-items: center; gap: 8px; padding: 4px 16px 10px; position: sticky; top: 56px; background: var(--bg); z-index: 1; }
.week-nav .week-label { flex: 1; display: flex; flex-direction: column; align-items: center; min-width: 0; }
.week-nav .week-label b { font-size: 0.95rem; }
.week-nav .week-label small { font-size: 0.78rem; }
.week-nav button { padding: 8px 12px; }

.reports-toolbar { padding: 0 16px 8px; display: flex; flex-direction: column; gap: 8px; }
.search-input {
  width: 100%; padding: 9px 12px; border-radius: 10px;
  border: 1px solid rgba(255,255,255,.10); background: var(--card); color: var(--text);
  font-size: 0.92rem;
}
.search-input:focus { outline: none; border-color: var(--btn); }
.filter-chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; }
.filter-chips .chip {
  flex: 0 0 auto; padding: 6px 12px; border-radius: 999px;
  background: var(--card); color: var(--hint);
  border: 1px solid rgba(255,255,255,.06); font-size: 0.82rem; font-weight: 600; cursor: pointer;
}
.filter-chips .chip.active { background: rgba(51,144,236,.18); color: var(--text); border-color: var(--btn); }
.filter-chips .chip:active { opacity: .85; }

.lead-card .lead-actions-inline { display: flex; gap: 6px; margin-left: 6px; flex: 0 0 auto; }
.icon-btn {
  width: 30px; height: 30px; border-radius: 8px; border: 1px solid rgba(255,255,255,.08);
  background: var(--card); color: var(--text); font-size: 0.95rem; cursor: pointer; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:active { opacity: .85; }

.lead-head-actions { display: flex; gap: 6px; align-items: center; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: 26px; z-index: 50;
  background: rgba(20,20,25,.95); color: var(--text);
  padding: 10px 16px; border-radius: 12px; border: 1px solid rgba(255,255,255,.08);
  font-size: 0.9rem; box-shadow: 0 6px 24px rgba(0,0,0,.35);
  max-width: 86vw; text-align: center;
  opacity: 1; transition: opacity .2s ease;
}
.toast.fade { opacity: 0; }

.counts-row { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px 8px; }
.count-pill { font-size: 0.74rem; padding: 4px 10px; border-radius: 999px; background: rgba(255,255,255,.06); color: var(--hint); }
.count-pill b { color: var(--text); margin-right: 4px; }

.day-sections { padding: 0; }
.day-section { padding: 0 12px 6px; }
.day-section h3 { margin: 14px 4px 8px; font-size: 0.78rem; color: var(--hint); text-transform: uppercase; letter-spacing: 0.08em; font-weight: 700; }
.lead-card { display: flex; align-items: center; gap: 12px; background: var(--card); border-radius: 14px; padding: 12px 14px; margin-bottom: 8px; border: 1px solid rgba(255,255,255,0.06); cursor: pointer; }
.lead-card:active { opacity: 0.92; }
.lead-card-body { flex: 1; min-width: 0; }
.lead-phone-line { display: flex; align-items: center; gap: 8px; }
.lead-phone { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 1.02rem; font-weight: 650; letter-spacing: 0.01em; }
.lead-time { color: var(--hint); font-size: 0.78rem; flex: 0 0 auto; }
.lead-snippet { color: var(--hint); font-size: 0.85rem; line-height: 1.3; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lead-meta-row { display: flex; gap: 8px; align-items: center; margin-top: 4px; font-size: 0.74rem; color: var(--hint); }
.lead-repeat { color: var(--link); }

.status-pill { font-size: 0.7rem; font-weight: 700; padding: 4px 9px; border-radius: 999px; white-space: nowrap; }
.status-pill.s-novaya     { background: rgba(255,255,255,.10); color: #d8d8e0; }
.status-pill.s-dozvon     { background: rgba(230,162,60,.22); color: #f3c272; }
.status-pill.s-dogovor    { background: rgba(51,144,236,.22); color: #8ec5ff; }
.status-pill.s-proshla    { background: rgba(88,214,141,.22); color: #9dffb0; }
.status-pill.s-ne-proshla { background: rgba(232,93,93,.22); color: #ffb4b4; }
.status-pill.s-otmena, .status-pill.s-otkaz { background: rgba(255,255,255,.05); color: #8b8b96; }

.sheet-status-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 8px 0 12px; }
.sheet-status-grid button { min-height: 52px; border-radius: 12px; font-size: 0.95rem; font-weight: 650; border: 1px solid rgba(255,255,255,.10); background: var(--card); color: var(--text); cursor: pointer; padding: 8px 10px; }
.sheet-status-grid button.current { border-color: var(--btn); background: rgba(51,144,236,.18); }
.sheet-status-grid button:active { opacity: 0.92; }
.lead-sheet-actions { display: flex; justify-content: center; margin-top: 12px; }
#lead-sheet-comment { width: 100%; border-radius: 12px; border: 1px solid rgba(255,255,255,.12); background: var(--card); color: var(--text); padding: 10px; font-size: 0.95rem; resize: vertical; }

.lead-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 0 16px; }
.lead-head h2 { margin: 0; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 1.25rem; }
.lead-actions { display: flex; gap: 8px; padding: 8px 16px 4px; }
.lead-events-title { margin: 14px 16px 8px; font-size: 0.82rem; color: var(--hint); text-transform: uppercase; letter-spacing: 0.08em; }
.lead-events { padding: 0 16px; display: flex; flex-direction: column; gap: 6px; }
.ev-row { display: flex; gap: 10px; padding: 10px 12px; background: var(--card); border-radius: 12px; border: 1px solid rgba(255,255,255,0.05); }
.ev-icon { flex: 0 0 24px; font-size: 1rem; line-height: 1.2; }
.ev-body { flex: 1; min-width: 0; }
.ev-top { display: flex; justify-content: space-between; gap: 8px; font-size: 0.78rem; color: var(--hint); margin-bottom: 3px; }
.ev-text { font-size: 0.9rem; line-height: 1.35; white-space: pre-wrap; }
#lead-notes { margin: 0 16px; width: calc(100% - 32px); }
.lead-events-title { padding: 0; }

.cards-list { padding: 0 16px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 6px; }
.info-card p { margin: 6px 0; color: var(--text); white-space: pre-wrap; line-height: 1.35; }
.info-card small { color: var(--hint); }
.settings-card { margin: 0 16px; padding: 12px 14px; border-radius: 16px; background: var(--card); border: 1px solid rgba(255,255,255,.08); }
.setting-row { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid rgba(255,255,255,.06); }
.setting-row:last-child { border-bottom: 0; }
.setting-row b { min-width: 38%; }
.setting-row span { color: var(--hint); text-align: right; }
.switch input { display: none; }
.switch span { width: 44px; height: 26px; border-radius: 999px; background: rgba(255,255,255,.14); display: inline-block; position: relative; }
.switch span:before { content: ""; position: absolute; width: 20px; height: 20px; border-radius: 50%; background: #fff; left: 3px; top: 3px; transition: .15s; }
.switch input:checked + span { background: var(--btn); }
.switch input:checked + span:before { transform: translateX(18px); }

.sheet { position: fixed; inset: 0; z-index: 10; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.45); }
.sheet-panel { position: absolute; left: 0; right: 0; bottom: 0; max-height: 92vh; overflow: auto; background: var(--bg); border-radius: 18px 18px 0 0; padding: 12px 16px 28px; box-shadow: 0 -8px 32px rgba(0,0,0,0.4); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet-panel h2 { margin: 8px 0; font-size: 1.1rem; line-height: 1.35; }
.meta { color: var(--hint); font-size: 0.85rem; margin: 0 0 12px; }
.desc { white-space: pre-wrap; font-size: 0.9rem; line-height: 1.45; color: var(--text); opacity: 0.95; margin-bottom: 16px; max-height: 40vh; overflow: auto; }
.actions { display: flex; flex-direction: column; gap: 10px; }

.modal { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,0.5); display: flex; align-items: flex-end; justify-content: center; padding: 16px; }
.modal-box { width: 100%; max-width: 420px; background: var(--card); border-radius: 16px; padding: 16px; border: 1px solid rgba(255,255,255,0.08); }
.modal-box h3 { margin: 0 0 12px; font-size: 1rem; }
.modal-box textarea { background: var(--bg); resize: vertical; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 12px; }
