/* ============================================================
   «Потолочная №1» — дизайн-система интерфейса (SPEC §8)
   Светлая, плотная, system-ui. Без внешних шрифтов и CDN.
   Классы используются и в app.js, и в admin.js.
   ============================================================ */

:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --border: #dfe3ea;
  --text: #1c2333;
  --muted: #6b7385;
  --accent: #2457D6;
  --accent-hover: #1c46b0;
  --accent-soft: #e8eefc;
  --danger: #d23f3f;
  --danger-soft: #fdecec;
  --ok: #1f9d55;
  --ok-soft: #e6f6ec;
  --warn: #9a6b12;
  --warn-soft: #fdf1dc;
  --zebra: #f8f9fc;
  --radius: 10px;
  --radius-sm: 8px;
  --shadow: 0 6px 24px rgba(20, 26, 40, 0.10);
  --shadow-sm: 0 1px 2px rgba(20, 26, 40, 0.06);
  --header-h: 52px;
  --control-h: 38px;
}

/* ---------- База ---------- */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.45;
}

body.modal-open { overflow: hidden; }

[hidden] { display: none !important; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { margin: 0 0 12px; line-height: 1.25; }
h1 { font-size: 20px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
p { margin: 0 0 10px; }

button { font: inherit; color: inherit; }

.muted { color: var(--muted); }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.small { font-size: 12.5px; }
.spacer { flex: 1 1 auto; }

/* ---------- Шапка и навигация ---------- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  max-width: 1280px;
  margin: 0 auto;
  height: 100%;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.brand {
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
  white-space: nowrap;
}
.brand:hover { text-decoration: none; color: var(--accent); }

.nav {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 1 1 auto;
}

.nav a {
  display: inline-flex;
  align-items: center;
  height: 34px;
  padding: 0 11px;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-weight: 500;
  white-space: nowrap;
}
.nav a:hover { background: var(--bg); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); }

.nav-user { display: none; }

.header-user {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
  white-space: nowrap;
}
.header-user .user-name { font-weight: 500; }

.app-header .burger {
  display: none; /* специфичность выше .btn — иначе .btn{display:inline-flex} показывает ☰ на десктопе */
  width: 38px;
  height: 38px;
  padding: 0;
  font-size: 20px;
  line-height: 1;
}

/* ---------- Контейнер вида ---------- */

#view {
  max-width: 1280px;
  margin: 0 auto;
  padding: 20px 16px 40px;
}

.page-title {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 14px;
  margin-bottom: 16px;
}
.page-title h1 { margin: 0; }

/* ---------- Кнопки ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  text-decoration: none;
  transition: background .12s, border-color .12s, color .12s;
}
.btn:hover { background: #f0f2f7; text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; transform: none; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn-ghost {
  background: transparent;
  border-color: transparent;
}
.btn-ghost:hover { background: rgba(28, 35, 51, 0.06); }

.btn-danger {
  color: var(--danger);
  border-color: #f0c4c4;
}
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }

.btn-sm {
  height: 30px;
  padding: 0 10px;
  font-size: 13px;
}

.btn-block { width: 100%; }

.btn-icon {
  width: 30px;
  padding: 0;
  font-size: 15px;
}

/* ---------- Поля ввода ---------- */

.input, .select, .textarea {
  display: block;
  width: 100%;
  height: var(--control-h);
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  line-height: normal;
  outline: none;
  transition: border-color .12s, box-shadow .12s;
}
.input::placeholder, .textarea::placeholder { color: #a0a6b5; }
.input:focus, .select:focus, .textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.input:disabled, .select:disabled, .textarea:disabled { background: var(--bg); color: var(--muted); }
.input.invalid, .select.invalid, .textarea.invalid { border-color: var(--danger); }
.input.invalid:focus { box-shadow: 0 0 0 3px var(--danger-soft); }

.textarea {
  height: auto;
  min-height: 80px;
  padding: 8px 10px;
  resize: vertical;
}

.select {
  padding-right: 30px;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='none' stroke='%236b7385' stroke-width='1.6' d='M1 1.5l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  cursor: pointer;
}

input[type="checkbox"], input[type="radio"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}
input[type="range"] { accent-color: var(--accent); }

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.field > label {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
}
.field .hint { font-size: 12px; color: var(--muted); }
.field-inline {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--control-h);
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 16px;
}
.form-grid .span-2 { grid-column: 1 / -1; }

.form-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 10px;
}

.form-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 16px;
}

/* ---------- Карточки ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow-sm);
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 12px;
}

.card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.card-head .card-title { margin: 0; }
.card-head .actions { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

/* ---------- Таблицы ---------- */

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}
.table th, .table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}
.table th {
  background: #f1f3f8;
  font-weight: 600;
  font-size: 12.5px;
  color: #3c4356;
  white-space: nowrap;
}
.table tbody tr:nth-child(even) { background: var(--zebra); }
.table tbody tr:last-child td { border-bottom: none; }
.table tfoot td {
  font-weight: 600;
  background: #f7f8fb;
  border-top: 1px solid var(--border);
  border-bottom: none;
}
.table .right { text-align: right; }
.table.clickable tbody tr { cursor: pointer; }
.table.clickable tbody tr:hover { background: var(--accent-soft); }
.table .num { color: var(--muted); width: 1%; }
.table .cell-muted { color: var(--muted); font-size: 12.5px; }

/* Инпуты внутри таблицы — компактнее */
.table .input, .table .select { height: 34px; }
.table td.col-item { min-width: 260px; }
.table td.col-qty { min-width: 170px; }

/* ---------- Бейджи ---------- */

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: #eef0f5;
  color: var(--muted);
  font-size: 11.5px;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
  vertical-align: middle;
}
.badge-auto { background: var(--accent-soft); color: var(--accent); }
.badge-admin { background: var(--warn-soft); color: var(--warn); }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }

/* ---------- Тулбар, пустое состояние ---------- */

.toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}
.toolbar .input, .toolbar .select { width: auto; min-width: 220px; }
.toolbar .search { flex: 1 1 240px; max-width: 420px; }

.empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.empty .btn { margin-top: 12px; }

.loading {
  padding: 30px;
  text-align: center;
  color: var(--muted);
}

/* ---------- Тосты ---------- */

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: calc(100vw - 32px);
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  min-width: 240px;
  max-width: 380px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  box-shadow: var(--shadow);
  font-size: 13.5px;
  cursor: pointer;
  animation: toast-in .2s ease-out;
}
.toast-ok { border-left-color: var(--ok); }
.toast-error { border-left-color: var(--danger); }
.toast-info { border-left-color: var(--accent); }
.toast-out { opacity: 0; transform: translateY(8px); transition: opacity .25s, transform .25s; }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

/* ---------- Модалка ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(20, 26, 40, 0.45);
  animation: fade-in .15s ease-out;
}

.modal {
  width: min(520px, 100%);
  max-height: 90vh;
  overflow: auto;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 20px;
}
.modal-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 12px;
}
.modal-body { font-size: 14px; }
.modal-body p:last-child { margin-bottom: 0; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

/* ---------- Экран входа ---------- */

.login-wrap {
  min-height: calc(100vh - var(--header-h) - 60px);
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  width: 100%;
  max-width: 380px;
  margin: 0;
}
.login-card .card-title { font-size: 18px; text-align: center; }
.login-card .field { margin-bottom: 12px; }
.login-hint {
  margin-top: 14px;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  color: var(--muted);
}
.login-hint code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--text);
}

/* ---------- Калькулятор ---------- */

.calc-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 20px;
  align-items: start;
}
.calc-main { min-width: 0; }

.section-head {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin: 4px 0 12px;
}
.section-head h2 { margin: 0; }
.section-head .actions { margin-left: auto; display: flex; gap: 6px; }

.room-card { border-left: 3px solid var(--accent); }
.room-card .room-name { font-size: 15px; font-weight: 600; max-width: 320px; }

.dims-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px 14px;
  margin-bottom: 12px;
}

.calc-value {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: var(--control-h);
  flex-wrap: nowrap;
}
.calc-value .btn-sm { padding: 0 7px; }
.calc-value .value { font-weight: 600; font-size: 15px; }
.calc-value .value.is-empty { color: var(--muted); font-weight: 400; }
.calc-value .input { width: 110px; }

.room-error {
  color: var(--danger);
  font-size: 13px;
  margin: -4px 0 10px;
}

.room-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 12px;
}
.room-footer .room-total {
  margin-left: auto;
  font-weight: 600;
  font-size: 15px;
}

.lines-table .select { min-width: 240px; max-width: 100%; }

/* Управление количеством: − поле + и ползунок */
.qty-wrap { display: flex; flex-direction: column; gap: 4px; }
.qty-control {
  display: inline-flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface);
}
.qty-control:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.qty-control .qty-btn {
  width: 34px;
  border: none;
  border-radius: 0;
  background: #f4f5f9;
  font-size: 17px;
  cursor: pointer;
  flex: 0 0 auto;
  height: 34px;
}
.qty-control .qty-btn:hover { background: #e8eaf1; }
.qty-control .qty-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.qty-control .qty-input {
  width: 74px;
  height: 34px;
  border: none;
  border-radius: 0;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  text-align: center;
  padding: 0 4px;
  box-shadow: none;
  font-weight: 500;
}
.qty-control .qty-input:focus { box-shadow: none; }
.qty-control .qty-input.invalid { color: var(--danger); background: var(--danger-soft); }
.qty-range { width: 100%; max-width: 170px; margin: 2px 0 0; height: 20px; }
.qty-meta { display: flex; align-items: center; gap: 6px; min-height: 22px; }
.qty-reset { height: 22px; padding: 0 6px; font-size: 14px; }

/* Панель итогов */
.summary-bar {
  position: sticky;
  bottom: 16px;
  align-self: end;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
}
.summary-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 5px 0;
}
.summary-row .label { color: var(--muted); }
.summary-row .value { font-weight: 600; white-space: nowrap; }
.summary-total {
  border-top: 1px solid var(--border);
  margin-top: 6px;
  padding-top: 10px;
}
.summary-total .label { color: var(--text); font-weight: 700; font-size: 15px; }
.summary-total .value { font-size: 21px; font-weight: 700; }
.summary-total-right { display: inline-flex; align-items: center; gap: 6px; }
.summary-discount { display: flex; align-items: center; gap: 8px; }
.summary-discount .input { width: 96px; text-align: right; }
.summary-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 8px;
  padding: 8px 10px;
  background: var(--danger-soft);
  border-radius: var(--radius-sm);
}
.summary-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}
.summary-toggle { display: none; }

/* Сегментный переключатель (% / ₽) */
.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.seg button {
  height: 34px;
  min-width: 38px;
  padding: 0 10px;
  border: none;
  background: var(--surface);
  cursor: pointer;
  font-weight: 600;
  color: var(--muted);
}
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--accent); color: #fff; }
.seg button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---------- Просмотр расчёта ---------- */

.calc-view-head {
  display: flex;
  align-items: flex-start;
  gap: 12px 20px;
  flex-wrap: wrap;
}
.calc-view-head .meta { color: var(--muted); font-size: 13px; }
.calc-view-head .actions {
  margin-left: auto;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.kv {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 16px;
  font-size: 14px;
}
.kv dt { color: var(--muted); margin: 0; }
.kv dd { margin: 0; white-space: pre-line; overflow-wrap: anywhere; }

.totals {
  margin-left: auto;
  width: min(420px, 100%);
}
.totals .summary-row { padding: 4px 0; }
.totals .summary-total .value { font-size: 19px; }

/* ---------- Мобильный режим ---------- */

@media (max-width: 900px) {
  .calc-layout { grid-template-columns: minmax(0, 1fr); }
  .totals { width: 100%; }
}

@media (max-width: 768px) {
  #view { padding: 14px 12px 32px; }

  .app-header .burger { display: inline-flex; margin-left: auto; }

  .nav {
    display: none;
    position: absolute;
    top: var(--header-h);
    left: 0;
    right: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 2px;
    padding: 8px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
  }
  .nav.open { display: flex; }
  .nav a { height: 40px; }
  .nav .nav-user {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 11px 4px;
    margin-top: 4px;
    border-top: 1px solid var(--border);
  }
  .nav .nav-user .user-name { flex: 1; font-weight: 500; }
  .header-user { display: none; }

  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: auto; }
  .grid-2 { grid-template-columns: 1fr; }

  .card { padding: 12px; }
  .toolbar .search { max-width: none; }
  .toolbar .input { min-width: 0; width: 100%; }

  .calc-page { padding-bottom: 140px; }

  .summary-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 80;
    border-radius: 12px 12px 0 0;
    border-bottom: none;
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    box-shadow: 0 -6px 24px rgba(20, 26, 40, 0.14);
  }
  .summary-bar:not(.expanded) .summary-details { display: none; }
  .summary-toggle { display: inline-flex; }
  .summary-total { margin-top: 0; padding-top: 4px; border-top: none; }
  .summary-actions { flex-direction: row; margin-top: 8px; }
  .summary-actions .btn { flex: 1 1 auto; }

  .toasts { bottom: 150px; right: 12px; }
  .modal { padding: 16px; }
  .calc-view-head .actions { margin-left: 0; width: 100%; }
  .calc-view-head .actions .btn { flex: 1 1 auto; }
}

@media (max-width: 420px) {
  .dims-grid { grid-template-columns: 1fr 1fr; }
  .room-card .room-name { max-width: none; }
}

@media print {
  .app-header, .summary-bar, .toasts { display: none !important; }
}
