:root {
  --bg: #f4f5f7; --surface: #ffffff; --text: #1b1f27; --muted: #667085; --line: #e3e6ec;
  --accent: #1f5eff; --accent-ink: #ffffff; --danger: #c62828; --warn: #b26a00; --ok: #1b7f4b;
  --warn-bg: #fff4e0; --danger-bg: #fdecec; --ok-bg: #e6f6ee; --chip: #eef0f4;
  --shadow: 0 1px 2px rgba(16,24,40,.08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c; --surface: #1d2128; --text: #e8eaee; --muted: #98a0ae; --line: #2c323c;
    --accent: #6c93ff; --accent-ink: #0b1020; --danger: #ff8a80; --warn: #ffc15e;
    --warn-bg: #3a2e15; --danger-bg: #3a1b1b; --ok-bg: #16301f; --ok: #6fd69b; --chip: #2a2f38;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); text-decoration: none; }
@media (hover: hover) { a:hover { text-decoration: underline; } }
h1 { font-size: 22px; margin: 0 0 14px; }
h2 { font-size: 16px; margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.top { background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.top-in { display: flex; align-items: center; gap: 12px; padding: 8px 16px; max-width: 1500px; margin: 0 auto; }
.brand { font-weight: 700; color: var(--text); }
.nav { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.nav a { color: var(--muted); padding: 8px 10px; border-radius: 8px; white-space: nowrap; }
.nav a.active { color: var(--text); background: var(--chip); font-weight: 600; }
.nav a:hover { text-decoration: none; background: var(--chip); }
main { max-width: 1500px; margin: 0 auto; padding: 16px; }

.btn, button { font: inherit; cursor: pointer; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 8px; line-height: 1.2; }
.btn:hover, button:hover { background: var(--chip); text-decoration: none; }
.btn.primary, button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover, button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.danger, .btn.danger { color: var(--danger); }
button.link { border: 0; background: none; padding: 4px 6px; color: var(--muted); }
.inline { display: inline; }

input, select, textarea { font: inherit; font-size: 16px; color: var(--text); background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; width: 100%; }
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.cols { display: grid; gap: 16px; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); align-items: start; }
@media (max-width: 860px) { .cols { grid-template-columns: 1fr; } }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; margin-bottom: 14px; }
.filters > div { min-width: 140px; }
.filters .grow { flex: 1; min-width: 200px; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--chip); color: var(--text); white-space: nowrap; }
.src-profi { background: #e6efff; color: #1546b8; } .src-avito { background: #e4f7ea; color: #14703a; }
.src-yandex { background: #fff0d6; color: #8a5200; } .src-site { background: #f1e8ff; color: #5b2bb0; }
.src-call { background: #e5f4f7; color: #0d6678; } .src-ref { background: #ffe8ef; color: #a3224f; } .src-other { background: var(--chip); }
@media (prefers-color-scheme: dark) {
  .src-profi { background: #1d2c52; color: #a9c1ff; } .src-avito { background: #16301f; color: #7fdba2; }
  .src-yandex { background: #3a2e15; color: #ffcf7a; } .src-site { background: #2d2147; color: #cdb3ff; }
  .src-call { background: #15323a; color: #84d4e4; } .src-ref { background: #3d1c29; color: #ff9fbd; }
}
.flag { font-size: 12px; font-weight: 600; padding: 1px 7px; border-radius: 6px; }
.flag.warn { background: var(--warn-bg); color: var(--warn); } .flag.bad { background: var(--danger-bg); color: var(--danger); }
.flag.ok { background: var(--ok-bg); color: var(--ok); }

/* доска */
.board { display: flex; gap: 12px; overflow-x: auto; padding-bottom: 12px; scroll-snap-type: x proximity; align-items: flex-start; }
.col { flex: 1 0 200px; max-width: 340px; background: var(--chip); border-radius: 12px; padding: 8px; scroll-snap-align: start; min-height: 120px; }
.col.over { outline: 2px dashed var(--accent); }
.col h3 { margin: 4px 6px 8px; font-size: 14px; display: flex; justify-content: space-between; }
.col h3 .n { color: var(--muted); font-weight: 500; }
.card { display: block; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 8px;
  color: var(--text); box-shadow: var(--shadow); border-left: 4px solid var(--line); }
@media (hover: hover) { .card:hover { border-color: var(--accent); } }
.card.dragging { opacity: .4; }
.card .t { font-weight: 600; margin: 4px 0 2px; overflow-wrap: anywhere; }
.card .m { font-size: 13px; color: var(--muted); }
.card.late { border-left-color: var(--danger); }
.card.s-Профи { border-left-color: #1f5eff; }
.empty { color: var(--muted); font-size: 13px; padding: 8px 6px; }

/* таблицы */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
@media (hover: hover) { tr:hover td { background: color-mix(in srgb, var(--chip) 50%, transparent); } }
td.num, th.num { text-align: right; white-space: nowrap; }
.bar { height: 8px; border-radius: 4px; background: var(--chip); overflow: hidden; min-width: 80px; }
.bar > i { display: block; height: 100%; background: var(--accent); }

.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.stat .v { font-size: 24px; font-weight: 700; }
.stat .l { color: var(--muted); font-size: 13px; }

.status-row { display: flex; gap: 6px; flex-wrap: wrap; margin: 10px 0 4px; }
.status-row button.cur { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 8px 0; border-bottom: 1px solid var(--line); }
.timeline .when { font-size: 12px; color: var(--muted); }
.timeline .note { white-space: pre-wrap; overflow-wrap: anywhere; }
.task { display: flex; gap: 8px; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--line); }
.task.done .tt { text-decoration: line-through; color: var(--muted); }
.task .tt { flex: 1; overflow-wrap: anywhere; }
.alert { padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; background: var(--warn-bg); color: var(--warn); }
.alert.ok { background: var(--ok-bg); color: var(--ok); } .alert.bad { background: var(--danger-bg); color: var(--danger); }
pre { background: var(--chip); padding: 12px; border-radius: 8px; overflow-x: auto; font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
.pager { display: flex; gap: 8px; align-items: center; justify-content: center; margin-top: 12px; }
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--text); padding: 16px; width: min(92vw, 360px); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.login { max-width: 360px; margin: 14vh auto 0; }
.brand:hover { text-decoration: none; }
html { -webkit-tap-highlight-color: transparent; }
body { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
.top { padding-top: env(safe-area-inset-top); }
input[type="search"] { -webkit-appearance: none; appearance: none; }
input[type="date"], input[type="datetime-local"] { min-height: 40px; -webkit-appearance: none; appearance: none; }
.lead-head { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.lead-head h1 { margin: 0; }
.lost-row { margin-top: 8px; }
.lost-row select { width: auto; }
.task-form { margin-top: 10px; }
.save-bar { display: flex; gap: 8px; margin-top: 12px; }

/* карточка на доске: вся кликабельна, кнопки поверх растянутой ссылки */
.card { position: relative; }
.card .card-link { color: inherit; }
.card .card-link::after { content: ""; position: absolute; inset: 0; border-radius: inherit; }
.card .card-link:focus-visible { outline: none; }
.card:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }
.card-actions { position: relative; z-index: 1; display: flex; gap: 6px; margin-top: 8px; }
.chip-btn { white-space: nowrap; display: inline-flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 600; padding: 6px 10px;
  border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--text); line-height: 1.2; }
.chip-btn.call { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 35%, var(--line)); }
.chip-btn.js-move { margin-left: auto; }
.c-client .nm { display: block; }
.c-status .lr { display: block; }
.col:not(.is-empty) .empty { display: none; }

/* контакты в карточке заявки */
.contact { display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.contact-main { min-width: 0; flex: 1 1 240px; display: grid; gap: 2px; }
.contact-name { font-size: 18px; font-weight: 700; overflow-wrap: anywhere; }
.contact-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* вкладки этапов и нижнее меню на компьютере скрыты */
.stage-tabs, .tabbar, .mob-only { display: none !important; }
.stage-tabs[hidden] { display: none !important; }
.ico { width: 22px; height: 22px; display: block; }

/* шторка снизу (выбор этапа, причина отказа) */
.sheet h2 { margin-bottom: 12px; }
.sheet-list { display: grid; gap: 6px; }
.sheet-list button { text-align: left; padding: 12px 14px; }
.sheet-list button.cur { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.sheet-cancel { width: 100%; margin-top: 10px; }
.sheet-row > button { flex: 1; }

/* всплывающее сообщение */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none; transition: .2s; z-index: 60;
  max-width: calc(100vw - 32px); text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- планшет и узкие окна ---------- */
@media (max-width: 1000px) {
  .hint { display: none; }
}

@media (max-width: 900px) {
  /* таблицы -> карточки */
  .rtable { padding: 0; overflow: visible; }
  .rtable h2 { padding: 14px 14px 0; }
  .rtable table, .rtable tbody, .rtable tr, .rtable td { display: block; }
  .rtable thead { display: none; }
  .rtable td { border: 0; padding: 0; }

  .lead-row { position: relative; display: flex !important; flex-wrap: wrap; align-items: center; gap: 4px 8px; padding: 12px 14px;
    border-bottom: 1px solid var(--line); }
  .lead-row:last-child { border-bottom: 0; }
  .lead-row .row-link::after { content: ""; position: absolute; inset: 0; }
  .lead-row .c-client { order: 1; flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
  .lead-row .c-client .nm { font-size: 16px; font-weight: 700; overflow-wrap: anywhere; }
  .lead-row .c-sum { order: 2; font-weight: 600; }
  .lead-row .c-what { order: 3; flex-basis: 100%; font-size: 14px; overflow-wrap: anywhere; }
  .lead-row .c-src { order: 4; }
  .lead-row .c-city { order: 5; }
  .lead-row .c-city .city { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--chip); }
  .lead-row .c-city:empty { display: none; }
  .lead-row .c-status { order: 6; font-size: 13px; }
  .lead-row .c-status .st { font-weight: 600; }
  .lead-row .c-status .lr { display: inline; margin-left: 4px; }
  .lead-row .c-id { order: 7; margin-left: auto; font-size: 13px; }
  .lead-row .c-date { order: 8; font-size: 13px; }
  .rtable td[colspan] { padding: 14px; }

}

/* ---------- телефоны (iPhone SE … Pro Max, Android 360–430 px) ---------- */
@media (max-width: 760px), (pointer: coarse) and (max-height: 500px) {
  body { font-size: 15px; }
  h1 { font-size: 20px; margin-bottom: 12px; }
  main { padding: 12px 12px calc(84px + env(safe-area-inset-bottom)); }
  .panel { padding: 14px; border-radius: 12px; margin-bottom: 12px; }
  .mob-only { display: revert !important; }
  .desk-only { display: none !important; }

  /* шапка: логотип + настройки + выход, разделы внизу */
  .top-in { padding: 6px 8px 6px 14px; gap: 4px; min-height: 52px; }
  .brand { font-size: 18px; margin-right: auto; }
  .top .nav, .top .new-btn { display: none; }
  .icon-btn, .logout { display: inline-flex !important; align-items: center; justify-content: center; width: 44px; height: 44px;
    border-radius: 10px; color: var(--muted); padding: 0; }
  .icon-btn.active { color: var(--accent); background: var(--chip); }

  .tabbar { display: grid !important; grid-template-columns: repeat(5, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    background: color-mix(in srgb, var(--surface) 94%, transparent); -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
    border-top: 1px solid var(--line);
    padding: 4px max(4px, env(safe-area-inset-right)) calc(4px + env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-left)); }
  .tabbar a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: 52px;
    color: var(--muted); font-size: 11px; font-weight: 500; border-radius: 10px; text-decoration: none; }
  .tabbar a span { white-space: nowrap; }
  .tabbar a.active { color: var(--accent); font-weight: 600; }
  .tabbar .tab-new b { display: flex; align-items: center; justify-content: center; width: 40px; height: 30px; border-radius: 10px;
    background: var(--accent); color: var(--accent-ink); }
  .tabbar .tab-new.active { color: var(--accent); }

  /* кнопки и касания: не меньше 44 px */
  .btn, button { min-height: 44px; padding: 10px 14px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
  button.link { min-width: 44px; min-height: 44px; }
  .chip-btn { min-height: 38px; padding: 8px 12px; }
  input, select, textarea { padding: 10px 12px; min-height: 44px; }
  label { font-size: 13px; }

  /* фильтры */
  .filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
  .filters > div { min-width: 0; }
  .filters .grow { grid-column: 1 / -1; min-width: 0; }
  .filters .overdue { grid-column: 1 / -1; justify-self: start; padding: 6px 10px; }
  .list-filters { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .list-filters > button { grid-column: 1 / -1; }

  /* доска: вкладки этапов, одна колонка на экран */
  .stage-tabs:not([hidden]) { display: flex !important; gap: 6px; overflow-x: auto; margin: 0 -12px 10px; padding: 2px 12px 4px;
    scrollbar-width: none; scroll-snap-type: x proximity; position: sticky; top: calc(52px + env(safe-area-inset-top)); z-index: 10;
    background: var(--bg); }
  .stage-tabs::-webkit-scrollbar { display: none; }
  .stage-tabs button { flex: none; min-height: 38px; padding: 6px 12px; border-radius: 999px; white-space: nowrap; scroll-snap-align: start; font-size: 14px; }
  .stage-tabs button .n { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 999px; background: var(--chip); font-size: 12px; font-weight: 600; }
  .stage-tabs button.is-empty { color: var(--muted); }
  .stage-tabs button[aria-selected="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
  .stage-tabs button[aria-selected="true"] .n { background: rgba(255,255,255,.25); color: inherit; }

  .board { display: block; overflow: visible; padding: 0; }
  .col { width: 100%; max-width: none; padding: 0; background: none; min-height: 0; margin-bottom: 14px; }
  .col h3 { margin: 4px 2px 8px; }
  .col.is-empty { display: none; }
  .board.tabbed .col { display: none; }
  .board.tabbed .col.show { display: block; }
  .board.tabbed .col h3 { display: none; }
  .board.tabbed .col .empty { padding: 32px 0; text-align: center; font-size: 14px; }
  .card { padding: 12px; margin-bottom: 10px; }
  .card .t { font-size: 16px; }

  .stats-table tr { display: grid !important; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px 4px; padding: 12px 14px;
    border-bottom: 1px solid var(--line); }
  .stats-table tr:last-child { border-bottom: 0; }
  .stats-table .c-name { grid-column: 1 / -1; font-weight: 600; }
  .stats-table td.num { text-align: left; font-weight: 600; font-size: 14px; }
  .stats-table td.num::before { content: attr(data-label); display: block; font-size: 11px; font-weight: 500; color: var(--muted); }
  .stats-table .c-bar { grid-column: 1 / -1; }

  .period { flex-wrap: nowrap; display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .period .btn { padding: 8px 2px; font-size: 14px; white-space: nowrap; }
  .stat-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat { padding: 10px 12px; }
  .stat .v { font-size: 20px; }
  .stat .l { font-size: 12px; }

  /* карточка заявки */
  .lead-head { gap: 6px; }
  .lead-head .back { display: inline-flex !important; align-items: center; justify-content: center; width: 36px; height: 36px; margin-left: -6px;
    font-size: 22px; color: var(--text); border-radius: 10px; }
  .lead-head > .muted.small { flex-basis: 100%; }
  .contact { align-items: stretch; }
  .contact-actions { flex: 1 1 100%; display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); }
  .status-row { flex-wrap: nowrap; overflow-x: auto; margin: 8px -14px 4px; padding: 0 14px 4px; scrollbar-width: none; }
  .status-row::-webkit-scrollbar { display: none; }
  .status-row button { white-space: nowrap; }
  .lost-row { display: grid; grid-template-columns: 1fr auto; }
  .lost-row > span { grid-column: 1 / -1; }
  .lost-row select { width: 100%; }

  .lead-cols { display: flex; flex-direction: column; align-items: stretch; gap: 0; }
  .lead-cols > div { display: contents; }
  .o-notes { order: 1; } .o-tasks { order: 2; } .o-data { order: 3; } .o-raw { order: 4; } .o-others { order: 5; } .o-del { order: 6; margin-bottom: 12px; }
  .o-del button { width: 100%; }
  .grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .grid > div { min-width: 0; }

  /* кнопка «Сохранить» прилипает над нижним меню */
  .save-bar { position: sticky; bottom: calc(68px + env(safe-area-inset-bottom)); z-index: 5; margin: 12px -14px -14px; padding: 10px 14px;
    background: color-mix(in srgb, var(--surface) 94%, transparent); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
    border-top: 1px solid var(--line); border-radius: 0 0 12px 12px; }
  .save-bar > * { flex: 1; }

  /* задачи */
  .task { gap: 4px; padding: 4px 0; }
  .task .check { font-size: 20px; }
  .task-new { display: grid; grid-template-columns: 1fr auto; }
  .task-new .grow { grid-column: 1 / -1; }
  .task-new > div:not(.grow) { min-width: 0; }
  .task-new > button { align-self: end; }

  pre { font-size: 12px; }
  .login { margin-top: 8vh; }

  /* шторка снизу */
  dialog.sheet { width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 16px 16px 0 0; border-bottom: 0;
    padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); max-height: 85vh; overflow-y: auto; }
  dialog.sheet[open] { animation: sheet-up .2s ease-out; }
  .toast { bottom: calc(80px + env(safe-area-inset-bottom)); }
}
@keyframes sheet-up { from { transform: translateY(40px); opacity: .5; } to { transform: none; opacity: 1; } }

/* самые узкие (Galaxy S 360 px, iPhone SE/mini 375 px) */
@media (max-width: 380px) {
  .period .btn { font-size: 13px; }
  .tabbar a { font-size: 10px; }
  .list-filters { grid-template-columns: 1fr 1fr; }
  .list-filters > button { grid-column: auto; align-self: end; }
  .stats-table tr { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid { grid-template-columns: 1fr; }
}

/* телефон боком: низкий экран, меню компактнее */
@media (pointer: coarse) and (max-height: 500px) {
  .tabbar a { min-height: 40px; flex-direction: row; gap: 6px; }
  .tabbar .tab-new b { height: 26px; }
}

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