:root {
  --primary: #7cb342;
  --primary-dark: #689f38;
  --bg: #a5aaaf;
  --card: #fcfbf8;
  --border: #c8c2b5;
  --text: #322f29;
  --muted: #6e6a62;
  --danger: #a05c57;
  --green: #859c7a;
  --amber: #8bc34a;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg);
  background-image:
    repeating-linear-gradient(
      180deg,
      rgba(255, 255, 255, 0.08) 0px,
      rgba(255, 255, 255, 0.08) 1px,
      rgba(0, 0, 0, 0.14) 2px,
      rgba(0, 0, 0, 0) 3px
    ),
    linear-gradient(165deg, #b9bdc1 0%, #a5aaaf 48%, #93979c 100%);
  color: var(--text);
  font-size: 12px;
  line-height: 1.45;
}

h1, h2, h3 { margin: 0 0 8px; }
h1 { font-size: 19px; }
h2 { font-size: 15px; }
h3 { font-size: 13px; color: var(--muted); font-weight: 600; margin-top: 4px; }

/* ---------- Кнопки ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  background: transparent;
  color: var(--text);
  transition: background .15s, border-color .15s, color .15s;
  white-space: nowrap;
}
.btn:hover { background: #e6e3db; }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { border-color: var(--border); background: var(--card); }
.btn-ghost:hover { border-color: #cbc7bf; }
.btn-danger { color: var(--danger); border-color: var(--border); }
.btn-danger:hover { background: #f3deda; }
.btn-block { width: 100%; justify-content: center; }
.btn-sm { padding: 4px 9px; font-size: 11px; border-radius: 6px; }
.pay-unlink { color: var(--danger); padding: 0 4px; font-weight: 700; }

/* ---------- Поля ---------- */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field span { font-size: 10px; color: var(--muted); font-weight: 500; }
input, select {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 12px;
  font-family: inherit;
  color: var(--text);
  background: #fff;
  width: 100%;
}
input:focus, select:focus { outline: 2px solid rgba(37, 99, 235, .25); border-color: var(--primary); }
input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  cursor: pointer;
  vertical-align: middle;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #faf7ee;
  position: relative;
  flex: none;
}
input[type="checkbox"]:checked::after {
  content: "+";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: #000;
  font-size: 15px;
  font-weight: 700;
  line-height: 1;
}

/* ---------- Авторизация ---------- */
.auth-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #a5aaaf;
  background-image:
    repeating-linear-gradient(180deg, rgba(255, 255, 255, 0.08) 0px, rgba(255, 255, 255, 0.08) 1px, rgba(0, 0, 0, 0.14) 2px, rgba(0, 0, 0, 0) 3px),
    linear-gradient(165deg, #b9bdc1 0%, #a5aaaf 48%, #93979c 100%);
  padding: 20px;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 12px 40px rgba(30, 41, 59, .08);
  text-align: center;
}
.auth-logo { font-size: 37px; }
.auth-sub { color: var(--muted); margin-bottom: 20px; }
.auth-card .field { text-align: left; }
.auth-hint { margin-top: 16px; color: var(--muted); font-size: 10px; }

/* ---------- Шапка ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  height: 56px;
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-left { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.logo { font-size: 19px; }
.app-title { font-size: 14px; }
.topbar-nav { display: flex; gap: 4px; }
.nav-btn {
  border: none;
  background: transparent;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 12px;
  cursor: pointer;
  color: var(--muted);
  font-weight: 500;
}
.nav-btn:hover { background: #e6e3db; color: var(--text); }
.nav-btn.active { background: #e3dfd6; color: var(--primary); }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.user-name { color: var(--muted); font-size: 11px; }
.refresh-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: #e6e3db;
  color: var(--primary);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.4s ease, background 0.2s;
}
.refresh-btn:hover { background: #dcd7cb; transform: rotate(120deg); }
.refresh-btn:active { transform: rotate(240deg); }

/* ---------- Контент ---------- */
.content { max-width: 1400px; margin: 0 auto; padding: 24px 20px 60px; }
.view { animation: fade .15s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.toolbar { display: flex; gap: 10px; margin-bottom: 16px; }
.search { max-width: 360px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  margin-bottom: 16px;
}

/* ---------- Сворачиваемые блоки ---------- */
.collapsible-head { display: flex; align-items: center; justify-content: space-between; cursor: pointer; user-select: none; }
.collapsible-head h3 { margin: 0; }
.collapse-arrow { font-size: 14px; color: var(--muted); transition: transform .15s ease; }
.collapsible-head.collapsed .collapse-arrow { transform: rotate(-90deg); }

/* ---------- Редактируемые тарифы ---------- */
.rate-input { width: 88px; padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; text-align: right; }
.rate-input.rate-th { width: 70px; text-align: left; }
.rate-input:focus { outline: none; border-color: var(--primary); }

/* ---------- Таблицы ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; background: #fff; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.table th { background: #f7f5f1; font-size: 12px; color: var(--muted); font-weight: 700; white-space: nowrap; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: #f8fafc; }
.table .num { text-align: right; }
.table .actions-col { min-width: 120px; white-space: nowrap; text-align: right; }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }
.icon-btn {
  border: none; background: transparent; cursor: pointer;
  border-radius: 6px; padding: 6px 8px; font-size: 13px; line-height: 1;
  text-decoration: none; color: inherit;
}
.icon-btn:hover { background: #e6e3db; }

/* ---------- Статусы ---------- */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 10px; font-weight: 500; }
.badge-draft { background: #eae7e1; color: #5f5b53; }
.badge-sent { background: #ffedd5; color: #c2410c; }
.badge-partial { background: #fef3c7; color: #92400e; }
.badge-paid { background: #e4ebe0; color: #637865; }
.badge-code { background: #dce6ee; color: #3a5a78; }

.empty { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- Редактор ---------- */
.editor-head { display: flex; align-items: center; gap: 14px; margin-bottom: 16px; }
.editor-head h2 { margin: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 16px; }
.grid-4 { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 0 16px; }
.span-2 { grid-column: span 2; }
@media (max-width: 900px) { .grid-4 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 640px) { .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; } .span-2 { grid-column: span 1; } }

.calc-result-box {
  display: flex; flex-direction: column; justify-content: center; gap: 4px;
  margin-bottom: 12px; padding: 9px 12px;
  background: linear-gradient(135deg, #7cb342 0%, #8bc34a 100%);
  border: 1px solid #689f38; border-radius: 8px;
  box-shadow: 0 2px 8px rgba(124, 179, 66, 0.28);
}
.calc-result-box span { font-size: 10px; color: rgba(255, 255, 255, 0.85); font-weight: 500; }
.calc-result-box b { font-size: 20px; line-height: 1.1; color: #fff; }
.calc-breakdown { display: flex; flex-wrap: wrap; gap: 8px 18px; margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--border); font-size: 11px; color: var(--muted); }
.calc-breakdown b { color: var(--text); font-weight: 600; }

/* акцентные заголовки калькулятора */
#view-calculator .card h3 { color: var(--text); }
#view-calculator .card h3::before {
  content: ""; display: inline-block; width: 4px; height: 13px;
  border-radius: 2px; background: linear-gradient(180deg, #8bc34a, #7cb342);
  margin-right: 8px; vertical-align: -1px;
}

.items-table th, .items-table td { padding: 6px 8px; }
.items-table input { padding: 6px 8px; }
.item-del { color: var(--danger); }
.i-drag { cursor: grab; color: var(--muted); font-size: 14px; user-select: none; width: 28px; text-align: center; }
.table tr.dragging { opacity: 0.45; }

.totals { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--border); }
.totals-row { display: flex; justify-content: flex-end; align-items: center; gap: 20px; padding: 3px 0; }
.totals-row span { color: var(--muted); }
.totals-row b { min-width: 110px; text-align: right; }
.totals-total { font-size: 14px; }
.totals-total b { color: var(--primary); }
.vat-input { width: 70px; display: inline-block; padding: 4px 6px; text-align: center; }

.editor-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }

/* ---------- Детали / печать ---------- */
.detail-actions { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.detail-actions-right { display: flex; gap: 10px; }
.print-area { background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 34px 38px; }

/* ---------- Настройки ---------- */
.add-user-row { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.add-user-row input { flex: 1; min-width: 120px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #3c3a35;
  color: #faf9f6;
  padding: 11px 18px;
  border-radius: 10px;
  font-size: 12px;
  z-index: 100;
  box-shadow: 0 8px 24px rgba(0,0,0,.2);
  max-width: 90%;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--green); }

/* ---------- Выбор контрагента / прайс ---------- */
.select-row { display: flex; gap: 8px; }
.select-row select { flex: 1; }
.items-toolbar { display: flex; gap: 8px; margin-top: 10px; }

.service-form, .cp-form { display: flex; gap: 8px; flex-wrap: wrap; }
.service-form input, .cp-form input { flex: 1; min-width: 120px; }
.section-manager { display: flex; align-items: center; gap: 8px; margin: 10px 0 0; }
.section-manager-label { font-size: 11px; color: var(--muted); font-weight: 600; white-space: nowrap; }
.group-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 22px; margin-bottom: 8px; }
.group-head .group-title { margin: 0; scroll-margin-top: 8px; }
.subgroup-title { margin-top: 16px; margin-bottom: 6px; font-size: 12px; color: var(--text); font-weight: 600; }

.price-card {
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 180px);
}
.services-nav { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 0; }
.services-scroll {
  overflow-y: auto;
  flex: 1;
  min-height: 0;
  margin-top: 8px;
  padding-right: 4px;
}
.nav-chip {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 11px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.nav-chip:hover { background: #e9e6df; border-color: var(--primary); }
.nav-chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.scroll-top {
  position: fixed;
  right: max(16px, calc((100vw - 1400px) / 2 - 60px));
  bottom: 24px;
  z-index: 90;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .18);
  transition: background .15s, transform .15s;
}
.scroll-top:hover { background: #e9e6df; transform: translateY(-2px); }

/* ---------- Модальное окно ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(75, 67, 55, .42);
  display: flex; align-items: center; justify-content: center; z-index: 200; padding: 20px;
}
.modal-overlay[hidden] { display: none; }
.modal {
  background: #fff; border-radius: 14px; width: 100%; max-width: 560px;
  max-height: 82vh; display: flex; flex-direction: column;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; }
.modal-body { padding: 8px 20px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.modal .search { max-width: none; width: 100%; }

.pick-item { display: flex; align-items: center; gap: 10px; padding: 10px 2px; border-bottom: 1px solid var(--border); }
.pick-item:last-child { border-bottom: none; }
.pick-item input[type="checkbox"] { width: 16px; height: 16px; flex: none; }
.pick-item .pick-name { flex: 1; }
.pick-item .pick-price { color: var(--muted); white-space: nowrap; }
.modal-empty { padding: 24px; text-align: center; color: var(--muted); }

/* ---------- Платежи / уведомления ---------- */
.hint { color: var(--muted); font-size: 11px; margin: 0 0 10px; }
.pay-url { background: var(--bg-soft, #f4f4f2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin-top: 8px; }
.pay-url code { word-break: break-all; font-size: 10px; color: var(--text, #333); }
.pay-item { padding: 10px 2px; border-bottom: 1px solid var(--border); }
.pay-item:last-child { border-bottom: none; }
.pay-item .pay-amount { font-weight: 600; }
.pay-item .pay-meta { color: var(--muted); font-size: 10px; margin-top: 2px; }
.pay-item .pay-sms { color: var(--muted); font-size: 10px; margin-top: 4px; word-break: break-word; white-space: pre-wrap; }
.sms-text { font-size: 10px; color: var(--muted); word-break: break-word; white-space: pre-wrap; max-width: 520px; }

/* Поля «Количество»: фиксированная ширина, высота ~-15% */
.s-qty, .asm-qty, .i-qty { width: 70px; }
.s-qty, .asm-qty { padding-top: 6px; padding-bottom: 6px; }
.i-qty { padding-top: 4px; padding-bottom: 4px; }

/* Фиксированные колонки прайса: не сдвигаются при изменении наименования */
.services-table { table-layout: fixed; width: 100%; }
.services-table .name-cell { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.services-table th.center { text-align: center; }
.services-table .qty-cell { text-align: center; }

/* Жирнее: наименования, покупатель, плательщик */
.name-cell { font-weight: 600; }
.bold-cell { font-weight: 600; }
.nowrap { white-space: nowrap; }
.mark { display: inline-flex; align-items: center; justify-content: center; width: 16px; height: 16px; border-radius: 4px; font-size: 12px; font-weight: 700; margin-right: 6px; vertical-align: middle; }
.mark-plus { background: #e4ebe0; color: #637865; }
.mark-minus { background: #f3deda; color: #a05c57; }

/* ---------- Заявки (почта) ---------- */
.request-card { margin-bottom: 12px; cursor: grab; }
.request-card.dragging { opacity: 0.5; }
.req-head { display: flex; align-items: baseline; gap: 8px; position: relative; }
.req-head b { flex: 1; }
.req-drag { cursor: grab; color: var(--muted); font-size: 14px; user-select: none; }
.req-date { color: var(--muted); font-size: 10px; white-space: nowrap; }
.req-status {
  border: none;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.req-status.st-pending { background: #ffedd5; color: #c2410c; }
.req-status.st-invoiced { background: #e4ebe0; color: #637865; }
.req-status-col { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; align-self: center; }
.req-paid-row { display: flex; align-items: center; gap: 6px; }
.req-status.st-none { background: transparent; color: var(--muted); border: 1px dashed #b6b2a7; }
.req-date-input { font-size: 11px; padding: 2px 4px; border: 1px solid #ccc; border-radius: 6px; width: 132px; }
.req-inv { position: absolute; left: 9cm; top: 0; white-space: nowrap; color: var(--muted); font-size: 12px; cursor: pointer; }
.req-inv:hover { text-decoration: underline; }
.req-inv-link { text-decoration: underline dotted; }
.req-view-btn {
  border: 1px solid #b6b2a7;
  background: #f7f5f1;
  color: var(--text);
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 10px;
  cursor: pointer;
  margin-right: 4px;
  vertical-align: middle;
}
.req-view-btn:hover { background: #e6e3db; }
.req-inv-gap { display: inline-block; width: 1cm; }
.req-inv-amount { font-weight: 600; color: var(--text); margin-left: 12px; }
.req-sender { color: var(--muted); font-size: 11px; margin-top: 2px; }
.req-body { margin-top: 8px; white-space: pre-wrap; word-break: break-word; }
.req-files { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 8px; }
.req-file { display: inline-block; padding: 6px 10px; background: #e6e3db; border-radius: 8px; color: var(--text); text-decoration: none; font-size: 12px; }
.req-file:hover { background: #dcd7cb; }

/* Строки прайса компактнее: ячейки описания и количества ниже (~15%) */
.services-scroll .table th,
.services-scroll .table td { padding-top: 8px; padding-bottom: 8px; }

/* ---------- Печатный документ ---------- */
.invoice-doc { font-size: 11px; color: #000; }
.invoice-doc h1 { font-size: 15px; text-align: center; margin: 0 0 18px; }
.invoice-doc .doc-cols { display: flex; gap: 30px; margin-bottom: 14px; }
.invoice-doc .doc-col { flex: 1; }
.invoice-doc .doc-label { font-weight: 600; margin-bottom: 2px; }
.invoice-doc .doc-line { margin: 1px 0; }
.invoice-doc .doc-table { width: 100%; border-collapse: collapse; margin: 12px 0; }
.invoice-doc .doc-table th, .invoice-doc .doc-table td { border: 1px solid #000; padding: 6px 8px; text-align: left; }
.invoice-doc .doc-table .num, .invoice-doc .doc-table .r { text-align: right; }
.invoice-doc .doc-totals { margin-left: auto; width: 320px; }
.invoice-doc .doc-totals .trow { display: flex; justify-content: space-between; padding: 3px 0; }
.invoice-doc .doc-totals .grand { font-weight: 700; font-size: 13px; border-top: 1px solid #000; margin-top: 4px; padding-top: 6px; }
.invoice-doc .doc-words { margin: 12px 0; }
.invoice-doc .doc-signs { display: flex; gap: 40px; margin-top: 40px; }
.invoice-doc .doc-sign { flex: 1; border-top: 1px solid #000; padding-top: 4px; font-size: 10px; }

@media print {
  body { background: #fff; }
  .topbar, .no-print, .toolbar, .editor-head, .editor-actions, #toast {
    display: none !important;
  }
  .content { max-width: none; padding: 0; }
  section.view { display: none !important; }
  #view-detail { display: block !important; }
  .print-area { border: none; border-radius: 0; padding: 0; }
}
