:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #6b7686;
  --line: #dfe4ea;
  --accent: #1f4e79;
  --accent-2: #2f6fa8;
  --accent-soft: #e6eef7;
  --ok: #1e7a46;
  --warn: #b25b00;
  --danger: #b3261e;
  --input: #ffffff;
  --radius: 10px;
  --brand: #1f4e79;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161c; --card: #1b2129; --text: #e6eaef; --muted: #9aa5b3; --line: #2c3540;
    --accent: #6fa8dc; --accent-2: #8bbbe6; --accent-soft: #1f2d3c; --input: #141a21;
    --ok: #5cc28a; --warn: #f0a24a; --danger: #ff8a80;
  }
}
:root[data-theme="dark"] {
  --bg: #12161c; --card: #1b2129; --text: #e6eaef; --muted: #9aa5b3; --line: #2c3540;
  --accent: #6fa8dc; --accent-2: #8bbbe6; --accent-soft: #1f2d3c; --input: #141a21;
  --ok: #5cc28a; --warn: #f0a24a; --danger: #ff8a80;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 18px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 16px 0 8px; }
.boot { padding: 40px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.wrap { max-width: 1100px; margin: 0 auto; padding: 16px; }
.narrow { max-width: 720px; }

/* Верхняя панель кабинета */
.top {
  position: sticky; top: 0; z-index: 10;
  background: var(--brand); color: #fff;
}
.top .wrap { display: flex; align-items: center; gap: 12px; padding-top: 10px; padding-bottom: 10px; }
.top .brand { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .spacer { flex: 1; }
.top select { max-width: 180px; }
.top button { background: rgba(255,255,255,.15); color: #fff; border-color: transparent; }
.nav { background: var(--card); border-bottom: 1px solid var(--line); }
.nav .wrap { display: flex; gap: 4px; overflow-x: auto; padding-top: 6px; padding-bottom: 6px; scrollbar-width: none; }
.nav a {
  text-decoration: none; color: var(--text); padding: 6px 12px; border-radius: 8px; white-space: nowrap;
}
.nav a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-bottom: 16px;
}
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }

label.f { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label.f > span { font-weight: 500; }
label.chk { display: flex; gap: 8px; align-items: center; cursor: pointer; }
input, select, textarea, button {
  font: inherit; color: var(--text); background: var(--input);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-height: 38px;
}
input[type="checkbox"] { min-height: 0; width: 18px; height: 18px; accent-color: var(--accent); }
textarea { min-height: 70px; resize: vertical; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-2); outline-offset: -1px; }
button { cursor: pointer; background: var(--card); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.danger { color: var(--danger); }
button.link { border: 0; background: none; color: var(--accent); padding: 4px 6px; min-height: 0; }
button:disabled { opacity: .55; cursor: default; }

/* Таблицы */
.tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; text-align: left; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .02em; }
td.num, th.num { text-align: right; white-space: nowrap; }
tr.total td { font-weight: 700; border-bottom: 0; font-size: 16px; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--accent-soft); }
table.edit input, table.edit select { width: 100%; min-width: 70px; padding: 6px 8px; min-height: 34px; }
table.edit td { padding: 4px; }
table.edit input.w-s { min-width: 60px; }
table.edit input.w-l { min-width: 260px; }

.total-big { font-size: 26px; font-weight: 700; color: var(--accent); }
.pill { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; background: var(--accent-soft); color: var(--accent); }
.pill.ok { color: var(--ok); }
.pill.warn { color: var(--warn); }
.msg { padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; background: var(--accent-soft); }
.msg.err { color: var(--danger); }
.stub {
  border: 2px dashed var(--line); border-radius: var(--radius); padding: 24px; text-align: center; color: var(--muted);
}

/* Открытая часть */
.hero { background: var(--brand); color: #fff; }
.hero .wrap { padding-top: 20px; padding-bottom: 20px; }
.hero h1 { color: #fff; }
.hero a { color: #fff; }

.login { max-width: 380px; margin: 60px auto; }

@media (max-width: 600px) {
  .wrap { padding: 12px 16px; }
  .card { padding: 12px; }
  .total-big { font-size: 22px; }
  .hide-sm { display: none; }
}

/* Не даём длинным спискам и fieldset раздвигать страницу на телефоне */
select { max-width: 100%; }
fieldset { min-width: 0; }
.tbl-wrap { max-width: 100%; }

/* Несколько типов свай в расчёте */
.pile-group { padding-bottom: 12px; margin-bottom: 12px; border-bottom: 1px dashed var(--line); }
.pile-group:last-of-type { border-bottom: 0; margin-bottom: 0; }

/* Встраивание на сайт: без фона и шапки */
body.embed { background: transparent; }
body.embed .wrap { padding: 0; }
/* Поле-ловушка для ботов */
.hp { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }

/* Кнопка-выбор файла (камера / галерея) */
label.btnlike {
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; min-height: 38px; background: var(--card);
}
label.btnlike:active { background: var(--accent-soft); }

/* Плитки анализа */
.grid.tiles { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); margin-bottom: 16px; }
.tile { margin-bottom: 0; }
.tile .tv { font-size: 22px; font-weight: 700; margin: 4px 0; }

/* Панель действий вверху страницы: «Сохранить» и др. — закреплена под шапкой при прокрутке */
.actionbar {
  position: sticky; top: var(--top-h, 60px); z-index: 6;
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  background: var(--bg); padding: 8px 0 10px; margin-bottom: 8px; border-bottom: 1px solid var(--line);
}

/* ---------- Телефоны (iPhone, Android) ---------- */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
button, a, label.btnlike, summary { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
/* Вырез экрана и полоска «домой» у iPhone: шапка под строкой состояния, отступы по краям */
.top { padding-top: env(safe-area-inset-top); }
body { padding-bottom: env(safe-area-inset-bottom); }
@supports (padding: max(0px)) {
  .wrap { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
}
/* Сенсорный экран: шрифт полей 16px — iPhone не увеличивает страницу при вводе; кнопки под палец */
@media (pointer: coarse) {
  input, select, textarea { font-size: 16px; }
  input, select, button, label.btnlike { min-height: 44px; }
  button.link { min-height: 36px; padding: 6px 8px; }
  input[type="checkbox"] { min-height: 0; width: 22px; height: 22px; }
  table.edit input, table.edit select { font-size: 16px; min-height: 40px; }
  .nav a { padding: 10px 14px; }
}
@media (max-width: 600px) {
  h2 { font-size: 17px; }
  th, td { padding: 6px; }
  /* панель действий — одной строкой, листается вбок: не занимает пол-экрана */
  .actionbar { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; padding: 6px 0; }
  .actionbar button { flex: 0 0 auto; min-height: 40px; padding: 6px 12px; }
}

/* План свайного поля у прораба */
.plan-img { display: block; max-width: 100%; height: auto; background: #fff; border: 1px solid var(--line); border-radius: 6px; }
/* Аванс и остаток на руках */
table.settle td { vertical-align: top; }
table.settle td.num { width: 1%; padding-left: 12px; }
a.btnlike {
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; min-height: 38px; background: var(--card);
}

/* Метки шаблона договора рядом с полями: {number}, {client_name}… */
code.tag { font: 12px/1.3 ui-monospace, SFMono-Regular, Consolas, monospace; color: var(--accent); background: var(--accent-soft);
  padding: 1px 5px; border-radius: 4px; font-weight: 400; white-space: nowrap; }
details.card > summary { cursor: pointer; }
table.tags-tbl td { vertical-align: top; }
.unsaved { color: var(--warn); font-size: 13px; }

/* Автосохранение: надпись вместо кнопки «Сохранить» */
.savestat { font-size: 13px; color: var(--muted); }
.savestat.pending { color: var(--warn); }
.savestat.saved { color: var(--ok); }
.savestat.error { color: var(--danger); }
/* Числа без стрелок ▲▼ (цены вводятся с клавиатуры) */
input[type="number"] { -moz-appearance: textfield; appearance: textfield; }
input[type="number"]::-webkit-outer-spin-button, input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* ---------- Телефон: списки — карточками, таблицы с полями — в столбик ---------- */
.row > label.f, .row > .f { min-width: 0; max-width: 100%; }
.savestat { white-space: nowrap; }
@media (max-width: 600px) {
  /* Списки КП, договоров, счетов: строка → карточка. Сверху «№ · дата · сумма», ниже клиент и статус */
  table.list-sm, table.list-sm tbody { display: block; }
  table.list-sm thead { display: none; }
  table.list-sm tr { display: grid; grid-template-columns: auto 1fr auto; grid-template-areas: "a b d" "c c e"; gap: 2px 10px;
    padding: 10px 2px; border-bottom: 1px solid var(--line); align-items: center; }
  table.list-sm td { display: block; padding: 0; border: 0; }
  table.list-sm td.hide-sm { display: none; }
  table.list-sm td[colspan] { grid-column: 1 / -1; }
  table.list-sm .ga-a { grid-area: a; font-weight: 600; }
  table.list-sm .ga-b { grid-area: b; color: var(--muted); font-size: 13px; }
  table.list-sm .ga-c { grid-area: c; }
  table.list-sm .ga-d { grid-area: d; font-weight: 600; }
  table.list-sm .ga-e { grid-area: e; justify-self: end; }
  /* Анализ по договорам: + маржа расчётная, фактическая, оплачено — строкой с подписями */
  table.list-sm.an tr { grid-template-columns: 1fr 1fr 1fr; grid-template-areas: "a a d" "c c c" "f g h" "e e e"; }
  table.list-sm.an .ga-e { justify-self: start; }
  table.list-sm .ga-f { grid-area: f; } table.list-sm .ga-g { grid-area: g; } table.list-sm .ga-h { grid-area: h; }
  table.list-sm td[data-label]::before { content: attr(data-label); display: block; font-size: 11px; color: var(--muted); font-weight: 400; }
  table.list-sm.an td.num { text-align: left; }

  /* Таблицы с полями ввода (позиции КП, факт себестоимости, справочники): строка → блок, у каждого поля подпись */
  table.stack-sm, table.stack-sm tbody { display: block; }
  table.stack-sm thead { display: none; }
  table.stack-sm tr { display: flex; flex-wrap: wrap; gap: 6px 8px; padding: 10px 0; border-bottom: 1px solid var(--line); align-items: flex-end; }
  table.stack-sm td { display: block; border: 0; padding: 0 !important; flex: 1 1 70px; min-width: 0; }
  table.stack-sm td.full { flex-basis: 100%; }
  table.stack-sm td.act { flex: 0 0 auto; }
  table.stack-sm td[data-label]::before { content: attr(data-label); display: block; font-size: 11px; color: var(--muted); margin-bottom: 2px; }
  table.stack-sm td.num { text-align: left; }
  table.stack-sm input, table.stack-sm select { width: 100%; min-width: 0 !important; }
  table.stack-sm tr.total, table.stack-sm tr.plain { display: flex; justify-content: space-between; }
}

.show-sm { display: none; }
@media (max-width: 600px) { .show-sm { display: inline; } }
@media (max-width: 600px) { table.stack-sm td.auto-sm { display: none; } }
/* Договоры и счета на телефоне: третья строка карточки — КП, прораб / договор, PDF */
@media (max-width: 600px) {
  table.list-sm.ct tr { grid-template-areas: "a b d" "c c e" "f g g"; }
  table.list-sm.inv tr { grid-template-areas: "a b d" "c c e" "f g h"; }
  table.list-sm.ct .ga-g select { max-width: 100% !important; width: 100%; }
}
@media (max-width: 600px) { table.list-sm.kp tr { grid-template-areas: "a b d" "c c e" "f f f"; } }

/* ---------- Сваебои: переключатель разделов, карта, календарь ---------- */
h4 { font-size: 14px; margin: 14px 0 6px; }
.seg { display: inline-flex; flex-wrap: wrap; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--card); }
.seg a { padding: 6px 12px; text-decoration: none; color: var(--text); font-size: 14px; }
.seg a.on { background: var(--accent); color: #fff; }
.map { position: relative; z-index: 0; border-radius: 8px; border: 1px solid var(--line); background: #e9eef3; }
.map-pin span { display: block; width: 20px; height: 20px; margin: 0 0 0 2px; border-radius: 50% 50% 50% 0; background: #d6336c; transform: rotate(-45deg); border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.45); }
.map-base span { display: flex; align-items: center; justify-content: center; width: 28px; height: 28px; border-radius: 6px; color: #fff; font-size: 11px; font-weight: 700; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.45); }
.geo-results { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; margin-top: 6px; padding: 8px; background: var(--bg); border-radius: 6px; }
.geo-results .link { text-align: left; }
.cal-wrap { overflow-x: auto; margin-top: 10px; -webkit-overflow-scrolling: touch; }
.cal { display: grid; grid-auto-rows: 30px; min-width: 940px; font-size: 12px; }
.cal-h { text-align: center; color: var(--muted); border-bottom: 1px solid var(--line); line-height: 1.15; padding-top: 2px; }
.cal-h .wd { font-size: 10px; }
.cal-h.we, .cal-cell.we { background: rgba(127, 127, 127, .09); }
.cal-h.today { color: var(--accent); font-weight: 700; }
.cal-cell { border-left: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.cal-cell.today { background: var(--accent-soft); }
.cal-name { position: sticky; left: 0; z-index: 3; background: var(--card); border-bottom: 1px solid var(--line); border-left: 5px solid #868e96; padding: 6px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-h0 { border-left-color: transparent; color: var(--muted); font-weight: 400; }
.cal-bar { z-index: 2; margin: 3px 1px; border-radius: 5px; color: #fff; padding: 0 6px; font-size: 11px; line-height: 24px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-decoration: none; text-shadow: 0 1px 1px rgba(0,0,0,.35); }
.cal-bar.plan { opacity: .5; outline: 1px dashed rgba(0,0,0,.45); outline-offset: -1px; }
.cal-bar.done { opacity: .8; }
.opt-rig { border-left: 5px solid #868e96; padding: 6px 10px; margin: 8px 0; background: var(--bg); border-radius: 6px; }

/* ---------- Персональные данные ---------- */
.pd-todo { padding: 8px 0; border-bottom: 1px solid var(--line); }
.pd-todo.ok span { color: var(--muted); }
.pd-todo.ok b { color: var(--ok); }
.pd-link { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: baseline; margin: 6px 0; }
.pd-link span { min-width: 200px; color: var(--muted); font-size: 13px; }
.pd-link a { word-break: break-all; }
.pd-field { border-bottom: 1px solid var(--line); padding: 8px 0; }
.pd-entry { border-bottom: 1px solid var(--line); padding: 8px 0; }
.pd-entry.open { border-left: 4px solid var(--warn); padding-left: 8px; }
.pd-find { margin-top: 8px; padding: 10px; background: var(--bg); border-radius: 8px; }
.nav-badge { display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 5px; border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center; font-weight: 700; }
/* файл собирался долго — сохранить по нажатию */
.save-bar { position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 50; display: flex; gap: 10px; align-items: center;
  max-width: calc(100% - 32px); padding: 10px 14px; border-radius: 12px; background: var(--card); border: 1px solid var(--line); box-shadow: 0 6px 24px rgba(0,0,0,.25); }
.save-bar span { overflow: hidden; text-overflow: ellipsis; }

/* Нагрузки */
input.faded { opacity: .45; }
.bad { color: var(--danger); font-weight: 600; }
.good { color: var(--ok); font-weight: 600; }
details.card > summary { cursor: pointer; }
table.ld-layers td:first-child select { min-width: 240px; }
table.ld-res { font-size: 13px; }
table.ld-res th, table.ld-res td { padding: 6px; }
table.edit input[type="checkbox"] { width: 18px; min-width: 0; min-height: 0; padding: 0; margin: 8px 4px; }
tr.ld-block td { background: var(--accent-soft); }
.ld-block-grid { display: grid; gap: 6px 10px; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
.ld-block-grid input { min-width: 0 !important; }

/* Геология */
.geo-svg { overflow-x: auto; background: #fff; border: 1px solid var(--line); border-radius: 8px; }
.geo-svg svg { display: block; max-width: none; }
button.chip { min-height: 30px; padding: 4px 10px; border-radius: 99px; font-size: 13px; }
button.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
