:root {
  --bg: #f3f6f8;
  --card: #ffffff;
  --text: #13232b;
  --muted: #5f717a;
  --line: #e1e8ec;
  --brand: #0b5f7a;
  --brand-2: #0e7c9c;
  --brand-soft: #e3f1f6;
  --red: #c8372d;
  --red-soft: #fdecea;
  --amber: #a86400;
  --amber-soft: #fff3dc;
  --green: #1f7a4a;
  --green-soft: #e4f4ea;
  --wa: #1fa855;
  --shadow: 0 1px 2px rgba(16, 40, 52, .06), 0 2px 8px rgba(16, 40, 52, .05);
  --r: 14px;
  --tabs-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1518;
    --card: #152126;
    --text: #e4edf0;
    --muted: #8fa3ab;
    --line: #233339;
    --brand: #4fb3d4;
    --brand-2: #6cc4e0;
    --brand-soft: #16323c;
    --red: #ff7b6e;
    --red-soft: #3a1d1a;
    --amber: #f0b45a;
    --amber-soft: #3a2c14;
    --green: #6ad19a;
    --green-soft: #173225;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
a { color: var(--brand); text-decoration: none; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }

#app { max-width: 640px; margin: 0 auto; padding-bottom: calc(var(--tabs-h) + 24px + env(safe-area-inset-bottom)); }

/* ---------- шапка ---------- */
.top {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  display: flex; align-items: center; gap: 8px;
  min-height: 56px;
}
.top h1 { font-size: 24px; margin: 0; flex: 1; letter-spacing: -.01em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top.sub h1 { font-size: 18px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  display: inline-grid; place-items: center;
  border: 0; background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn:active { background: var(--line); }
.top .icon-btn:first-child { margin-left: -8px; }

.wrap { padding: 0 16px; }
.sec-title { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 22px 4px 8px; display: flex; align-items: center; justify-content: space-between; }
.sec-title a { text-transform: none; letter-spacing: 0; font-size: 14px; }

/* ---------- карточки ---------- */
.card { background: var(--card); border-radius: var(--r); box-shadow: var(--shadow); }
.card + .card { margin-top: 10px; }
.pad { padding: 14px 16px; }
.list { background: var(--card); border-radius: var(--r); box-shadow: var(--shadow); overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; color: var(--text);
  border-top: 1px solid var(--line);
}
.row:first-child { border-top: 0; }
.row:active { background: var(--bg); }
.row .grow { flex: 1; min-width: 0; }
.row .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .s { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row > svg:last-child { color: var(--muted); width: 18px; height: 18px; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 700; font-size: 15px;
  background: var(--brand-soft); color: var(--brand);
}
.avatar.biz { border-radius: 11px; }

/* ---------- метки ---------- */
.pill { display: inline-flex; align-items: center; gap: 4px; font-size: 12.5px; font-weight: 600; padding: 3px 9px; border-radius: 99px; white-space: nowrap; }
.pill.over { background: var(--red-soft); color: var(--red); }
.pill.week { background: var(--amber-soft); color: var(--amber); }
.pill.month { background: var(--brand-soft); color: var(--brand); }
.pill.ok { background: var(--green-soft); color: var(--green); }
.pill.gray { background: var(--line); color: var(--muted); }
.pill.debt { background: var(--red-soft); color: var(--red); }

/* ---------- сводка на главной ---------- */
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.summary .card { padding: 12px; text-align: left; margin: 0; }
.summary b { display: block; font-size: 26px; line-height: 1.1; }
.summary span { font-size: 13px; color: var(--muted); }
.summary .over b { color: var(--red); }
.summary .week b { color: var(--amber); }
.summary .month b { color: var(--brand); }

/* ---------- карточка "кому менять" ---------- */
.due { padding: 14px 16px 12px; }
.due-head { display: flex; align-items: flex-start; gap: 10px; }
.due-head .grow { flex: 1; min-width: 0; }
.due-head .t { font-weight: 650; font-size: 17px; color: var(--text); }
.due-head .s { color: var(--muted); font-size: 14px; margin-top: 1px; }
.due-items { margin: 10px 0 0; padding: 0; list-style: none; }
.due-items li { display: flex; justify-content: space-between; gap: 10px; padding: 5px 0; font-size: 15px; border-top: 1px dashed var(--line); }
.due-items li:first-child { border-top: 0; }
.due-items .when { font-size: 14px; color: var(--muted); white-space: nowrap; }
.due-items .when.over { color: var(--red); font-weight: 600; }
.due-items .when.week { color: var(--amber); font-weight: 600; }
.reminded { font-size: 13px; color: var(--muted); margin-top: 8px; display: flex; align-items: center; gap: 5px; }
.reminded svg { width: 15px; height: 15px; color: var(--wa); }

.actions { display: flex; gap: 8px; margin-top: 12px; }
.actions .btn { flex: 1; }

/* ---------- кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-height: 44px; padding: 0 14px; border-radius: 12px;
  border: 0; font: inherit; font-weight: 600; font-size: 15px; cursor: pointer;
  background: var(--brand-soft); color: var(--brand);
}
.btn svg { width: 19px; height: 19px; }
.btn:active { filter: brightness(.94); }
.btn.primary { background: var(--brand); color: #fff; }
.btn.wa { background: var(--wa); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { background: var(--red-soft); color: var(--red); }
.btn.block { width: 100%; }
.btn.big { min-height: 52px; font-size: 16px; }
.btn[disabled] { opacity: .55; }
@media (prefers-color-scheme: dark) { .btn.primary { color: #06212b; } }

.quick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 4px; }
.quick .btn { flex-direction: column; gap: 4px; min-height: 64px; font-size: 13px; }
.quick .btn svg { width: 22px; height: 22px; }

/* ---------- клиент ---------- */
.hero { padding: 4px 16px 0; }
.hero h2 { margin: 0; font-size: 26px; letter-spacing: -.01em; line-height: 1.15; }
.hero .meta { color: var(--muted); margin-top: 4px; display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; font-size: 15px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; white-space: pre-wrap; word-break: break-word; }
.note { white-space: pre-wrap; font-size: 15px; }

.sys { padding: 14px 16px; }
.sys-head { display: flex; align-items: center; gap: 10px; }
.sys-head .ic { width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); }
.sys-head .grow { flex: 1; min-width: 0; }
.sys-head .t { font-weight: 650; }
.sys-head .s { color: var(--muted); font-size: 14px; }
.stages { margin-top: 10px; display: grid; gap: 1px; background: var(--line); border-radius: 10px; overflow: hidden; }
.stage { background: var(--card); display: flex; align-items: center; gap: 10px; padding: 9px 12px; font-size: 15px; }
.stage .grow { flex: 1; min-width: 0; }
.stage .s { color: var(--muted); font-size: 13px; }
.bar { height: 4px; border-radius: 4px; background: var(--line); margin-top: 6px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--green); border-radius: 4px; }
.bar i.week { background: var(--amber); }
.bar i.over { background: var(--red); }
.bar i.month { background: var(--brand-2); }

.visit { padding: 12px 16px; display: block; color: var(--text); border-top: 1px solid var(--line); }
.visit:first-child { border-top: 0; }
.visit .top-line { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.visit .t { font-weight: 600; }
.visit .sum { font-weight: 650; white-space: nowrap; }
.visit .s { color: var(--muted); font-size: 14px; margin-top: 2px; }

/* ---------- фото ---------- */
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 6px; }
.thumbs.small { grid-template-columns: repeat(auto-fill, 56px); margin-top: 8px; }
.thumb { aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--line); border: 0; padding: 0; cursor: pointer; position: relative; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.add { display: grid; place-items: center; color: var(--brand); background: var(--brand-soft); }
.thumb .x { position: absolute; top: 3px; right: 3px; width: 24px; height: 24px; border-radius: 50%; background: rgba(0,0,0,.55); color: #fff; display: grid; place-items: center; }
.thumb .x svg { width: 14px; height: 14px; }

.viewer { position: fixed; inset: 0; z-index: 50; background: #000; display: flex; flex-direction: column; color: #fff; }
.viewer .vbar { display: flex; align-items: center; gap: 8px; padding: calc(8px + env(safe-area-inset-top)) 8px 8px; }
.viewer .vbar .icon-btn { color: #fff; }
.viewer .vbar .grow { flex: 1; font-size: 14px; opacity: .8; }
.viewer .vimg { flex: 1; display: grid; place-items: center; overflow: hidden; touch-action: pan-y; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer .cap { padding: 10px 16px calc(14px + env(safe-area-inset-bottom)); font-size: 15px; text-align: center; min-height: 44px; }

/* ---------- формы ---------- */
form .card { padding: 4px 16px; }
.field { display: block; padding: 10px 0; border-top: 1px solid var(--line); }
.field:first-child { border-top: 0; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input, .field textarea, .field select {
  width: 100%; border: 0; background: transparent; color: var(--text);
  font: inherit; font-size: 17px; padding: 2px 0; outline: none;
}
.field textarea { resize: vertical; min-height: 70px; }
.field select { appearance: none; -webkit-appearance: none; padding-right: 24px; background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235f717a' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E") right center / 18px no-repeat; }
.field input::placeholder, .field textarea::placeholder { color: var(--muted); opacity: .6; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.two > .field, .field input { min-width: 0; }
.two .field + .field { border-top: 1px solid var(--line); }
.two > .field:nth-child(2) { border-top: 0; }
.two:not(:first-child) > .field { border-top: 1px solid var(--line); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 0 2px; }
.chip {
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: 7px 12px; border-radius: 99px; font: inherit; font-size: 14px; cursor: pointer;
}
.chip.on { background: var(--brand); border-color: var(--brand); color: #fff; }
@media (prefers-color-scheme: dark) { .chip.on { color: #06212b; } }
.chips.scroll { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 4px 16px 2px; }
.chips.scroll::-webkit-scrollbar { display: none; }

.stage-edit { display: grid; grid-template-columns: 1fr 64px 36px; gap: 8px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.stage-edit:first-of-type { border-top: 0; }
.stage-edit input { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--bg); color: var(--text); font: inherit; font-size: 15px; width: 100%; min-width: 0; }
.stage-edit .since { grid-column: 1 / 3; display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.stage-edit .since input { flex: 1; }
.stage-edit .icon-btn { width: 36px; height: 36px; color: var(--muted); }

.checkrow { display: flex; align-items: center; gap: 12px; padding: 11px 0; border-top: 1px solid var(--line); cursor: pointer; }
.checkrow:first-child { border-top: 0; }
.checkrow input { width: 22px; height: 22px; accent-color: var(--brand); flex: none; }
.checkrow .grow { flex: 1; }
.checkrow .s { font-size: 13px; color: var(--muted); }

.toggle { display: flex; align-items: center; justify-content: space-between; padding: 12px 0; border-top: 1px solid var(--line); }
.toggle input { width: 46px; height: 28px; appearance: none; -webkit-appearance: none; background: var(--line); border-radius: 99px; position: relative; cursor: pointer; transition: background .15s; flex: none; }
.toggle input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.toggle input:checked { background: var(--green); }
.toggle input:checked::after { transform: translateX(18px); }

.form-actions { padding: 18px 16px 0; display: grid; gap: 10px; }
.hint { font-size: 13px; color: var(--muted); padding: 6px 4px 0; }

/* ---------- поиск ---------- */
.search { display: flex; align-items: center; gap: 8px; background: var(--card); border-radius: 12px; padding: 0 12px; box-shadow: var(--shadow); }
.search svg { color: var(--muted); width: 19px; height: 19px; }
.search input { flex: 1; border: 0; background: transparent; font: inherit; font-size: 16px; padding: 12px 0; outline: none; color: var(--text); }

/* ---------- пусто ---------- */
.empty { text-align: center; padding: 48px 24px; color: var(--muted); }
.empty svg { width: 48px; height: 48px; color: var(--brand); opacity: .7; stroke-width: 1.5; }
.empty h3 { color: var(--text); margin: 12px 0 6px; font-size: 18px; }
.empty p { margin: 0 0 18px; }

/* ---------- деньги ---------- */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stat-grid .card { padding: 14px; margin: 0; }
.stat-grid b { display: block; font-size: 22px; }
.stat-grid span { color: var(--muted); font-size: 13px; }

/* ---------- вход ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login .box { width: 100%; max-width: 360px; }
.logo { width: 72px; height: 72px; border-radius: 20px; display: grid; place-items: center; margin: 0 auto 14px; background: linear-gradient(160deg, #1592b8, #0b5f7a); color: #fff; box-shadow: 0 8px 24px rgba(11,95,122,.3); }
.logo svg { width: 38px; height: 38px; stroke-width: 1.8; }
.login h1 { text-align: center; margin: 0; font-size: 28px; }
.login p.sub { text-align: center; color: var(--muted); margin: 4px 0 24px; }
.login .err { color: var(--red); font-size: 14px; min-height: 20px; margin: 10px 4px 0; }

/* ---------- нижние вкладки ---------- */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around; align-items: center;
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(14px); -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-top: 1px solid var(--line);
}
#tabs a { flex: 1; max-width: 120px; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11.5px; color: var(--muted); padding: 6px 0; }
#tabs a.on { color: var(--brand); }
#tabs a svg { width: 24px; height: 24px; }
#tabs .tab-add { flex: none; width: 52px; height: 52px; max-width: none; border-radius: 16px; background: var(--brand); color: #fff; justify-content: center; box-shadow: 0 4px 14px rgba(11,95,122,.35); }
@media (prefers-color-scheme: dark) { #tabs .tab-add { color: #06212b; } }
#tabs .tab-add svg { width: 26px; height: 26px; stroke-width: 2.4; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + 18px + env(safe-area-inset-bottom)); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #13232b; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 15px;
  transition: opacity .2s, transform .2s; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.spin { display: grid; place-items: center; min-height: 60vh; color: var(--muted); }
.offline { margin: 0 16px 10px; padding: 8px 12px; border-radius: 10px; background: var(--amber-soft); color: var(--amber); font-size: 14px; }
