:root {
  --bg: #f2f4f7;
  --paper: #ffffff;
  --ink: #1a1a1a;
  --muted: #6b7280;
  --line: #e5e7eb;
  --sber: #21A038;
  --sber-dark: #148F2E;
  --sber-soft: #e8f7ec;
  --sber-mid: #d4f0db;
  --green: #148F2E;
  --red: #e11d48;
  --shadow: 0 8px 28px rgba(20, 40, 30, 0.08);
  --radius: 16px;
  --font: "Segoe UI", "Helvetica Neue", system-ui, -apple-system, sans-serif;
  --topbar-h: 60px;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(900px 420px at 8% -12%, #d8f5e0 0%, transparent 55%),
    radial-gradient(700px 380px at 100% 0%, #eaf7ee 0%, transparent 50%),
    var(--bg);
}

.hidden { display: none !important; }
.muted { color: var(--muted); margin: 0; }
h1, h2, h3 { margin: 0; letter-spacing: -0.02em; }
h1 { font-size: 1.7rem; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.1rem; }

.brand { display: flex; align-items: center; gap: 10px; }
.brand-lg { margin-bottom: 22px; }
.brand .mark {
  display: inline-flex;
  width: 28px; height: 28px;
  flex-shrink: 0;
  line-height: 0;
}
.brand .mark svg { display: block; width: 100%; height: 100%; }
.brand-lg .mark { width: 42px; height: 42px; }

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(420px, 100%);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 32px 28px;
  box-shadow: var(--shadow);
}
.login-card label, #modal-form label, .settings-card label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
  font-size: .86rem;
  color: var(--muted);
}
input, select, textarea {
  font: inherit;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  outline: none;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--sber);
  box-shadow: 0 0 0 3px rgba(33, 160, 56, .18);
}
textarea { min-height: 84px; resize: vertical; }
.hint { color: var(--muted); font-size: .78rem; }

.btn {
  font: inherit;
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 9px 14px;
  cursor: pointer;
  background: var(--sber-soft);
  color: var(--sber-dark);
}
.btn.primary {
  background: linear-gradient(180deg, #27b344, var(--sber-dark));
  color: #fff;
  border-color: var(--sber-dark);
}
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn.sm { padding: 6px 10px; font-size: .86rem; }
.btn.wide { width: 100%; padding: 11px 14px; font-weight: 600; }
.btn:disabled { opacity: .6; cursor: wait; }

.error { color: var(--red); font-size: .9rem; margin: 0 0 10px; }
.icon-btn {
  border: 0; background: transparent; font-size: 1.5rem;
  line-height: 1; cursor: pointer; color: var(--muted);
}
.icon-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--sber-dark);
  cursor: pointer;
  flex-shrink: 0;
  padding: 0;
}
.icon-action:hover {
  background: var(--sber-soft);
  border-color: #a8dbb4;
}
.icon-action.debit {
  color: #b42318;
}
.icon-action.debit:hover {
  background: #fef2f2;
  border-color: #fecaca;
}
.icon-action svg { display: block; }

.app { min-height: 100vh; }
.top-chrome {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 16px;
  min-height: var(--topbar-h);
  padding: 10px 18px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.menu-toggle {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
  padding: 10px;
  flex-shrink: 0;
}
.menu-toggle span {
  display: block;
  height: 2px;
  background: var(--ink);
  border-radius: 2px;
}
.nav { display: flex; gap: 6px; flex: 1; flex-wrap: wrap; align-items: center; }
.nav-btn {
  border: 0; background: transparent; font: inherit;
  padding: 8px 14px; border-radius: 999px; cursor: pointer; color: var(--muted);
}
.nav-btn.active {
  background: var(--sber-soft);
  color: var(--sber-dark);
  font-weight: 600;
}
.nav-backdrop {
  position: fixed; inset: 0; z-index: 8;
  background: rgba(15, 23, 20, .35);
}
.session { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.who { color: var(--muted); font-size: .92rem; }

main { width: min(1120px, calc(100% - 32px)); margin: 28px auto 48px; }
.view-head {
  display: flex; justify-content: space-between; align-items: flex-end;
  gap: 16px; margin-bottom: 18px;
}
.table-wrap {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: auto;
}
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--line); font-size: .95rem; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.actions-col { width: 48px; text-align: right; }
.balance-pos { color: var(--green); font-weight: 600; }
.balance-neg { color: var(--red); font-weight: 600; }
.pill {
  display: inline-block; padding: 3px 8px; border-radius: 999px;
  font-size: .75rem; background: #eef2f0; color: var(--muted);
}
.pill.on { background: var(--sber-soft); color: var(--sber-dark); }
.pill.off { background: #fbe7e5; color: var(--red); }
.pill.credit { background: var(--sber-soft); color: var(--sber-dark); }
.pill.debit { background: #fbe7e5; color: var(--red); }
.pill.refund { background: #e7eefb; color: #1d4f91; }
.pill.adjustment { background: #fef3c7; color: #92400e; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

.ledger td { vertical-align: top; }
.ledger .num { white-space: nowrap; }

.cards-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}
.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
  min-height: 120px;
}
.card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 6px;
}
.card-title {
  font-weight: 650;
  font-size: 1.05rem;
  margin: 0;
  letter-spacing: -0.01em;
}
.card-meta {
  color: var(--muted);
  font-size: .88rem;
  line-height: 1.45;
  margin: 0;
}
.card-pending {
  margin: 8px 0 0;
  padding: 0;
  list-style: none;
}
.card-pending li {
  color: #b45309;
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 10px;
  font-size: .86rem;
  padding: 6px 10px;
  margin-top: 6px;
}
.card-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
}
.card-amount {
  font-size: 1.35rem;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.card-foot {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-top: 10px;
}
.card-comment {
  color: var(--muted);
  font-size: .86rem;
  margin: 8px 0 0;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.settings-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px 22px 16px;
  max-width: 560px;
}
.settings-card h3 { margin-bottom: 14px; }

.check-row {
  display: flex !important;
  flex-direction: row !important;
  align-items: center;
  gap: 10px !important;
  margin-bottom: 14px;
}
.check-row input { width: auto; }

.modal {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(15, 23, 20, .4);
  display: grid; place-items: center; padding: 20px;
}
.modal-card {
  width: min(480px, 100%);
  background: var(--paper);
  border-radius: 18px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 20px 20px 16px;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

.toast {
  position: fixed; right: 18px; bottom: 18px; z-index: 30;
  background: #111827; color: #f9fafb;
  padding: 12px 14px; border-radius: 12px;
  box-shadow: var(--shadow); max-width: min(360px, calc(100% - 24px));
}
.toast.bad { background: #7f1d1d; }

@media (max-width: 860px) {
  .menu-toggle { display: inline-flex; position: relative; z-index: 50; }
  .top-chrome { z-index: 50; }
  .nav-backdrop { z-index: 45; }
  .nav {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(280px, 86vw);
    z-index: 48;
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    flex: none;
    align-items: stretch;
    gap: 4px;
    margin: 0;
    padding: 72px 14px 24px;
    background: #fff;
    border-right: 1px solid var(--line);
    box-shadow: var(--shadow);
    transform: translateX(-105%);
    transition: transform .2s ease;
    overflow: auto;
  }
  .nav.open { transform: translateX(0); }
  .nav-btn {
    text-align: left;
    border-radius: 12px;
    padding: 12px 14px;
  }
  .who { display: none; }
  .view-head { flex-direction: column; align-items: stretch; }
  .cards-grid { grid-template-columns: 1fr; }
  main { width: calc(100% - 24px); margin: 18px auto 40px; }
  body.nav-open { overflow: hidden; }
}

@media (max-width: 480px) {
  .card-amount { font-size: 1.2rem; }
  .session .btn { padding: 8px 10px; }
}

/* ---- clients table ---- */
.search { width: 100%; max-width: 420px; font: inherit; padding: 10px 14px; border: 1px solid var(--line); border-radius: 12px; margin-bottom: 14px; background: var(--paper); }
.search:focus { outline: 2px solid var(--sber); border-color: transparent; }
.clients-table td { vertical-align: middle; }
.cl-name { font-weight: 600; }
.cl-pending { font-size: .82rem; color: #8a6100; margin-top: 2px; }
.cl-bal { font-weight: 700; white-space: nowrap; }
.cl-link { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.cl-link a { color: var(--sber-dark); text-decoration: none; font-family: ui-monospace, Menlo, monospace; font-size: .88rem; }
.cl-link a:hover { text-decoration: underline; }
.cl-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.btn.debit-btn { background: var(--sber); color: #fff; border-color: var(--sber); }
.btn.danger { background: #fff; color: var(--red); border: 1px solid #fecdd3; }
.btn.danger:hover { background: #fff1f2; }
.modal-actions .btn.danger { background: var(--red); color: #fff; border-color: var(--red); }
.modal-card.wide { width: min(640px, 100%); }
.modal-card { max-height: calc(100vh - 40px); overflow: auto; }
.hist-box { display: flex; flex-direction: column; margin-bottom: 8px; }
.hist-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.hist-row:last-child { border-bottom: 0; }
.hist-main { flex: 1; min-width: 0; }
.hist-top { display: flex; gap: 10px; align-items: baseline; }
.hist-type { font-weight: 600; }
.hist-comment { font-size: .86rem; color: var(--muted); margin-top: 2px; word-break: break-word; }
.hist-amt { font-weight: 700; white-space: nowrap; }
.hist-edit { flex: 1; }
@media (max-width: 760px) {
  .clients-table thead { display: none; }
  .clients-table, .clients-table tbody, .clients-table tr, .clients-table td { display: block; width: 100%; }
  .clients-table tr { padding: 12px 14px; border-bottom: 1px solid var(--line); }
  .clients-table td { border: 0; padding: 4px 0; text-align: left; }
  .clients-table td[data-label="Телефон"]::before, .clients-table td[data-label="Ссылка"]::before { content: attr(data-label) ": "; color: var(--muted); font-size: .82rem; }
  .clients-table td.cl-bal { font-size: 1.2rem; }
  .cl-link a { overflow: hidden; text-overflow: ellipsis; }
  .cl-actions .btn { flex: 1 1 auto; }
}

/* ---- modal v2: above header, sticky head, scrollable body ---- */
.modal { z-index: 1000; padding: 16px; align-items: center; justify-items: center; }
.modal-card {
  display: flex; flex-direction: column;
  max-height: 85vh; max-height: 85dvh; overflow: hidden; padding: 0;
}
.modal-head {
  position: sticky; top: 0; z-index: 1; flex: none;
  margin: 0; padding: 14px 18px; background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.modal-head h3 { margin: 0; font-size: 1.05rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#modal-form { flex: 1 1 auto; min-height: 0; overflow: auto; -webkit-overflow-scrolling: touch; padding: 14px 18px 16px; }
.modal-card.wide { width: min(760px, 100%); }
.toast { z-index: 1100; }
/* ---- history table ---- */
.hist-wrap { overflow-x: auto; }
.hist-table { font-size: .88rem; }
.hist-table th, .hist-table td { padding: 6px 8px; }
.hist-table th { font-size: .72rem; }
.hist-table .nowrap { white-space: nowrap; }
.hist-table td.hist-comment { color: var(--muted); max-width: 260px; word-break: break-word; }
.hist-table .tech { font-size: .78rem; color: #9ca3af; cursor: help; }
.hist-table td.hist-act { width: 1%; white-space: nowrap; text-align: right; }
.hist-table .icon-action { width: 28px; height: 28px; }
.hist-table tr.editing td { background: var(--sber-soft); }
.hist-table input { font: inherit; font-size: .88rem; padding: 4px 6px; border: 1px solid var(--line); border-radius: 8px; width: 100%; min-width: 90px; background: #fff; }
@media (max-width: 760px) {
  .modal { padding: 8px; }
  .modal-card, .modal-card.wide { width: 100%; max-height: 90vh; max-height: 90dvh; }
}
