:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f8f9fb;
  --text: #1b1f24;
  --muted: #69727d;
  --border: #e3e6ea;
  --accent: #2f6fb0;
  --accent-text: #ffffff;
  --accent-soft: #e7f0fa;
  --danger: #c0362c;
  --danger-soft: #fbeceb;
  --ok: #2e7d4f;
  --warn: #b26a00;
  --me: #dcebfb;
  --them: #ffffff;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 12px;
  --ch-telegram: #2a9bd8;
  --ch-avito: #29a744;
  --ch-instagram: #d6337f;
  --ch-email: #7b6fd6;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216;
    --panel: #171b21;
    --panel-2: #1d2229;
    --text: #e7e9ec;
    --muted: #97a0ab;
    --border: #2a3038;
    --accent: #6aa8e6;
    --accent-text: #0b1520;
    --accent-soft: #1f2f42;
    --danger: #f08a80;
    --danger-soft: #3a1f1d;
    --ok: #6cc28e;
    --warn: #e0a64a;
    --me: #1f3a57;
    --them: #20262e;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1216;
  --panel: #171b21;
  --panel-2: #1d2229;
  --text: #e7e9ec;
  --muted: #97a0ab;
  --border: #2a3038;
  --accent: #6aa8e6;
  --accent-text: #0b1520;
  --accent-soft: #1f2f42;
  --danger: #f08a80;
  --danger-soft: #3a1f1d;
  --ok: #6cc28e;
  --warn: #e0a64a;
  --me: #1f3a57;
  --them: #20262e;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
.muted, .hint { color: var(--muted); }
.hint { font-size: 13px; margin: 0; }
.small { font-size: 13px; }
.hidden { display: none !important; }
.grow { flex: 1; min-width: 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.between { justify-content: space-between; }
.nowrap { white-space: nowrap; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- каркас ---------- */

/* minmax(0, …): иначе длинная строка без переносов растягивает колонку шире экрана, и телефон уменьшает всю страницу */
.app { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 16px 10px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 16px; font-weight: 600; font-size: 17px; }
.brand .logo {
  width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-text); font-weight: 700;
}
.nav-link {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 9px;
  color: var(--text); text-decoration: none; position: relative;
}
.nav-link:hover { background: var(--panel-2); }
.nav-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-link .ico { width: 20px; text-align: center; }
.nav-sep { height: 1px; background: var(--border); margin: 8px 10px; }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; font-size: 12px; font-weight: 600;
  display: inline-grid; place-items: center; background: var(--danger); color: #fff; margin-left: auto;
}
.sidebar .foot { margin-top: auto; padding: 10px; display: flex; flex-direction: column; gap: 6px; }

.main { min-width: 0; display: flex; flex-direction: column; }
.view { padding: 20px 24px 32px; max-width: 1180px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: 16px; flex: 1; }
.view.full { max-width: none; padding: 0; height: 100vh; }
.view-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.view-head h1 { flex: 1; }

.bottom-nav { display: none; }

/* ---------- элементы ---------- */

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.panel > h2 { margin-bottom: 12px; }
.panel.tight { padding: 0; overflow: hidden; }

input, select, textarea, button { font: inherit; color: var(--text); }
input, select, textarea {
  background: var(--panel); border: 1px solid var(--border); border-radius: 9px; padding: 8px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; }
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; cursor: pointer;
  border: 1px solid transparent; border-radius: 9px; padding: 8px 14px; background: var(--accent); color: var(--accent-text);
  font-weight: 500; text-decoration: none; white-space: nowrap;
}
.btn:hover { filter: brightness(1.05); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.secondary { background: var(--accent-soft); color: var(--accent); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.danger { background: var(--danger-soft); color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.icon { padding: 6px 9px; }
.link-btn { background: none; border: none; color: var(--accent); cursor: pointer; padding: 0; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow);
  text-decoration: none; color: inherit; display: flex; flex-direction: column; gap: 4px;
}
a.card:hover { border-color: var(--accent); }
.card .label { color: var(--muted); font-size: 13px; }
.card .value { font-size: 24px; font-weight: 600; }
.card .sub { color: var(--muted); font-size: 13px; }
.card.alert .value { color: var(--danger); }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; align-items: start; }

.list { list-style: none; margin: 0; padding: 0; }
.list > li { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-top: 1px solid var(--border); }
.list > li:first-child { border-top: none; }
.list .title { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.list .meta { color: var(--muted); font-size: 13px; }
.overdue { color: var(--danger) !important; }
.empty { color: var(--muted); text-align: center; padding: 28px 12px; }

.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tab { padding: 6px 12px; border-radius: 20px; border: 1px solid var(--border); background: var(--panel); cursor: pointer; font-size: 14px; color: var(--text); }
.tab.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 10px; font-size: 12px; font-weight: 600;
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--border);
}
.ch { color: #fff; border: none; }
.ch-telegram { background: var(--ch-telegram); }
.ch-avito { background: var(--ch-avito); }
.ch-instagram { background: var(--ch-instagram); }
.ch-email { background: var(--ch-email); }

.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none; display: grid; place-items: center;
  font-weight: 600; color: #fff; font-size: 16px;
}

.skeleton { height: 14px; border-radius: 6px; background: linear-gradient(90deg, var(--panel-2), var(--border), var(--panel-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- чат с ассистентом ---------- */

.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head { padding: 14px 20px; border-bottom: 1px solid var(--border); background: var(--panel); display: flex; align-items: center; gap: 10px; }
.messages { flex: 1; overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.bubble {
  max-width: min(720px, 85%); padding: 8px 12px; border-radius: 14px; overflow-wrap: anywhere; position: relative;
  background: var(--them); border: 1px solid var(--border);
}
.bubble.mine { align-self: flex-end; background: var(--me); border-color: transparent; }
.bubble .when { font-size: 11px; color: var(--muted); margin-top: 2px; text-align: right; }
.bubble .tag { font-size: 11px; margin-bottom: 3px; display: flex; gap: 6px; align-items: center; }
.bubble.bot-reply { border-style: dashed; }
.bubble p { margin: 0 0 6px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre { background: var(--panel-2); padding: 8px; border-radius: 8px; overflow-x: auto; margin: 6px 0; font-size: 13px; }
.bubble code { background: var(--panel-2); padding: 0 4px; border-radius: 4px; font-size: 13px; }
.bubble ul { margin: 4px 0; padding-left: 20px; }
.bubble .files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel-2); padding: 2px 10px; border-radius: 10px; margin: 6px 0; }
.typing { align-self: flex-start; color: var(--muted); font-size: 13px; padding: 4px 8px; }
.typing::after { content: "…"; animation: dots 1.2s steps(3) infinite; display: inline-block; width: 1em; overflow: hidden; vertical-align: bottom; }
@keyframes dots { from { width: 0; } to { width: 1em; } }

.composer { border-top: 1px solid var(--border); background: var(--panel); padding: 10px 16px; display: flex; flex-direction: column; gap: 8px; }
.composer textarea { width: 100%; min-height: 44px; max-height: 220px; resize: none; }
.composer .attach { display: flex; gap: 6px; align-items: center; font-size: 13px; }
.notice { padding: 8px 12px; border-radius: 9px; background: var(--accent-soft); color: var(--accent); font-size: 13px; }
.notice.warn { background: var(--danger-soft); color: var(--danger); }

/* ---------- люди ---------- */

.people { display: grid; grid-template-columns: 340px minmax(0, 1fr); height: 100vh; min-height: 0; }
.people-list { border-right: 1px solid var(--border); background: var(--panel); display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.people-list .top { padding: 14px; display: flex; flex-direction: column; gap: 10px; border-bottom: 1px solid var(--border); }
.people-list .scroll { overflow-y: auto; flex: 1; }
.person-row { display: flex; gap: 10px; padding: 10px 14px; text-decoration: none; color: inherit; border-bottom: 1px solid var(--border); align-items: center; }
.person-row:hover { background: var(--panel-2); }
.person-row.active { background: var(--accent-soft); }
.person-row .name { font-weight: 600; }
.person-row .preview { color: var(--muted); font-size: 13px; }
.person-row .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); flex: none; }
.group-title { padding: 10px 14px 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

.pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; position: relative; }
.pane-head { padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--panel); display: flex; align-items: center; gap: 10px; }
.pane-empty { flex: 1; display: grid; place-items: center; color: var(--muted); padding: 24px; text-align: center; }

.drawer {
  position: absolute; top: 0; right: 0; bottom: 0; width: min(420px, 100%); background: var(--panel); border-left: 1px solid var(--border);
  box-shadow: -8px 0 24px rgba(0, 0, 0, .12); overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; z-index: 5;
}
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 13px; color: var(--muted); }
.field textarea, .field input, .field select { width: 100%; }
.link-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--border); }
.link-row:first-child { border-top: none; }

/* ---------- настройки ---------- */

.settings-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 16px; align-items: start; }
.setting { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 8px 0; border-top: 1px solid var(--border); }
.setting:first-of-type { border-top: none; }
.setting label { flex: 1; }
.setting.wide { flex-direction: column; align-items: stretch; }
.setting.wide textarea { width: 100%; }

.keyrow { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; padding: 10px 0; border-top: 1px solid var(--border); }
.keyrow:first-child { border-top: none; }
.keyrow .title { min-width: 200px; flex: 1; }
.keyrow input { flex: 1; min-width: 160px; }

.event { display: flex; gap: 12px; padding: 8px 0; border-top: 1px solid var(--border); }
.event:first-child { border-top: none; }
.event .time { width: 96px; flex: none; color: var(--muted); font-size: 14px; }

.routine { display: flex; flex-direction: column; gap: 6px; padding: 12px 0; border-top: 1px solid var(--border); }
.routine:first-child { border-top: none; }
.routine details { font-size: 13px; color: var(--muted); }
.routine pre { white-space: pre-wrap; font: inherit; margin: 6px 0 0; }

.more-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.more-grid a { display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.more-grid .ico { font-size: 22px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; max-width: calc(100% - 32px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, .2);
}
#toast.error { background: var(--danger); color: #fff; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login .panel { max-width: 420px; text-align: center; display: flex; flex-direction: column; gap: 10px; }

/* ---------- телефон ---------- */

@media (max-width: 900px) {
  html, body { overflow-x: hidden; }
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { display: none; }
  .main { padding-bottom: 64px; }
  .view { padding: 14px 16px 24px; }
  .view.full { height: calc(100vh - 64px); height: calc(100dvh - 64px); }
  .bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; height: 64px; z-index: 20;
    background: var(--panel); border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom);
  }
  .bottom-nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    font-size: 11px; color: var(--muted); text-decoration: none; position: relative;
  }
  .bottom-nav a .ico { font-size: 20px; }
  .bottom-nav a.active { color: var(--accent); font-weight: 600; }
  .bottom-nav .badge { position: absolute; top: 6px; left: 55%; margin: 0; min-width: 16px; height: 16px; font-size: 10px; }
  .people { grid-template-columns: minmax(0, 1fr); height: 100%; }
  .people-list { border-right: none; }
  .people.has-pane .people-list { display: none; }
  .people:not(.has-pane) .pane { display: none; }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .bubble { max-width: 92%; }
  .settings-grid { grid-template-columns: minmax(0, 1fr); }
  .messages { padding: 12px; }
  .hide-mobile { display: none !important; }
  .task-form input.grow { flex-basis: 100%; }
  .list input[type="date"] { width: 132px !important; }
}

@media (min-width: 901px) {
  .show-mobile { display: none !important; }
}

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

.icon { flex: none; vertical-align: -3px; }
.nav-link .ico, .bottom-nav .ico { display: inline-grid; place-items: center; }
.prio { color: var(--muted); display: inline-grid; }
.prio.high { color: var(--danger); }
.name .icon { color: var(--warn); vertical-align: -2px; }
.title .icon { color: var(--ok); }

mark { background: var(--accent-soft); color: var(--accent); border-radius: 3px; padding: 0 2px; }
