:root {
  --primary: #7c7566;
  --primary-dark: #655e51;
  --bg: #a5aaaf;
  --card: #fcfbf8;
  --border: #c8c2b5;
  --text: #322f29;
  --muted: #6e6a62;
  --danger: #a05c57;
  --green: #6f8a63;
  --amber: #967b50;
}

* { 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: 14px;
  line-height: 1.45;
}

h1, h2, h3 { margin: 0 0 8px; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; 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: 14px;
  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; }

/* ---------- Поля ---------- */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field span { font-size: 12px; color: var(--muted); font-weight: 500; }
input, select {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 14px;
  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"] { width: 16px; height: 16px; cursor: pointer; vertical-align: middle; }

/* ---------- Авторизация ---------- */
.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: 44px; }
.auth-sub { color: var(--muted); margin-bottom: 20px; }
.auth-card .field { text-align: left; }
.auth-hint { margin-top: 16px; color: var(--muted); font-size: 12px; }

/* ---------- Шапка ---------- */
.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: 22px; }
.app-title { font-size: 16px; }
.topbar-nav { display: flex; gap: 4px; }
.nav-btn {
  border: none;
  background: transparent;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  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: 13px; }

/* ---------- Контент ---------- */
.content { max-width: 1080px; margin: 0 auto; padding: 24px 20px 60px; }
.view { animation: fade .15s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.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;
}

/* ---------- Таблицы ---------- */
.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: 600; 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 { width: 120px; 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: 15px; line-height: 1;
}
.icon-btn:hover { background: #e6e3db; }

/* ---------- Статусы ---------- */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 500; }
.badge-draft { background: #eae7e1; color: #5f5b53; }
.badge-sent { background: #e9e1cc; color: #78633f; }
.badge-paid { background: #dfe7da; color: #48604a; }

.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; }
.span-2 { grid-column: span 2; }
@media (max-width: 640px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } .span-2 { grid-column: span 1; } }

.items-table th, .items-table td { padding: 6px 8px; }
.items-table input { padding: 6px 8px; }
.item-del { color: var(--danger); }

.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: 16px; }
.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: 14px;
  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; }
.group-title { margin-top: 22px; margin-bottom: 8px; scroll-margin-top: 76px; }
.subgroup-title { margin-top: 16px; margin-bottom: 6px; font-size: 14px; color: var(--text); font-weight: 600; }

.services-nav { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 6px; }
.nav-chip {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  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; }

/* ---------- Модальное окно ---------- */
.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: auto; }
.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); }

/* ---------- Печатный документ ---------- */
.invoice-doc { font-size: 13px; color: #000; }
.invoice-doc h1 { font-size: 18px; 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: 15px; 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: 12px; }

@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; }
}
