/* Калькулятор механической обработки — сдержанная инженерная тема */
:root {
  --bg: #f4f5f6;
  --panel: #ffffff;
  --panel-2: #fafbfb;
  --line: #d9dee3;
  --line-strong: #c3cbd2;
  --text: #1d2830;
  --muted: #6a7883;
  --muted-2: #8b979f;
  --accent: #2b5c78;
  --accent-hover: #224b62;
  --accent-soft: #eaf1f5;
  --ok: #2f7d5c;
  --ok-soft: #eaf4ef;
  --warn: #8a6326;
  --warn-soft: #f7f1e6;
  --danger: #a63d3d;
  --danger-soft: #f8ecec;
  --radius: 4px;
  --mono: "SF Mono", "Cascadia Mono", Consolas, "Roboto Mono", "Liberation Mono", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
.hidden { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; }
p { margin: 0 0 8px; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- верхняя панель ---------- */
.topbar {
  background: #24333d;
  color: #eef2f4;
  height: 54px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 12px; font-size: 14px; letter-spacing: .01em; }
.brand .mark {
  width: 28px; height: 28px;
  border: 1px solid #5d7383;
  color: #cfe0ea;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: .04em;
}
.brand .sub { color: #91a3ae; font-size: 12px; margin-left: 4px; }
.userbox { display: flex; align-items: center; gap: 12px; font-size: 12.5px; color: #b9c7d0; }

/* ---------- навигация ---------- */
.nav {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.nav-inner { display: flex; gap: 2px; }
.tab {
  appearance: none;
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: 12px 16px 10px;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
}
.tab:hover { color: var(--text); background: var(--panel-2); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* ---------- каркас ---------- */
.wrap { max-width: 1380px; margin: 0 auto; padding: 0 24px; }
main.wrap { padding-top: 22px; padding-bottom: 48px; }
.view { display: none; }
.view.active { display: block; }

.layout { display: grid; grid-template-columns: minmax(0, 1fr) 430px; gap: 20px; align-items: start; }
.col { min-width: 0; display: flex; flex-direction: column; gap: 20px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 46px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
}
.panel-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: #33424c; }
.panel-head .hint { font-size: 12px; color: var(--muted); }
.panel-body { padding: 16px; }
.panel-foot { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; }

/* ---------- форма ---------- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.form-grid .full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  font-weight: 600;
}
.field .hint { font-size: 11.5px; color: var(--muted-2); min-height: 14px; }
.field-sep { margin-top: 12px; }
.field-sep input { max-width: 240px; }
input[type="text"], input[type="number"], input[type="search"], select, textarea {
  font: inherit;
  font-size: 13.5px;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 3px;
  padding: 7px 9px;
  height: 34px;
  width: 100%;
  outline: none;
  transition: border-color .12s, box-shadow .12s;
}
textarea { height: auto; min-height: 84px; resize: vertical; font-family: var(--mono); font-size: 12px; line-height: 1.5; }
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(43, 92, 120, .12);
}
input[type="number"] { font-family: var(--mono); }
select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, #6a7883 50%), linear-gradient(135deg, #6a7883 50%, transparent 50%); background-position: calc(100% - 14px) 14px, calc(100% - 9px) 14px; background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; padding-right: 26px; }
select:disabled, input:disabled { background: #f2f4f5; color: var(--muted); }
input.invalid, select.invalid { border-color: var(--danger); }

.form-actions { display: flex; gap: 10px; margin-top: 16px; align-items: center; }
.form-actions .spacer { flex: 1; }

/* ---------- кнопки ---------- */
.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 34px;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  border-radius: 3px;
  background: #fff;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .12s, border-color .12s, color .12s;
}
.btn:hover { background: #f3f5f6; border-color: #adb8c1; }
.btn:active { background: #e9edf0; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { color: var(--danger); border-color: #d9b6b6; background: #fff; }
.btn-danger:hover { background: var(--danger-soft); border-color: #c99b9b; }
.btn-inverse { background: transparent; color: #cfdae1; border-color: #4d6270; }
.btn-inverse:hover { background: #2e414d; border-color: #6a8394; }
.btn-sm { height: 28px; padding: 0 10px; font-size: 12px; }
.btn-link { border: 0; background: none; color: var(--accent); padding: 0 4px; height: auto; font-size: 12.5px; }
.btn-link:hover { background: none; text-decoration: underline; }
.btn-icon { width: 28px; padding: 0; }

/* ---------- результаты ---------- */
.result-panel .panel-body { padding: 0; }
.empty-state { padding: 38px 20px; text-align: center; color: var(--muted); font-size: 13px; }
.empty-state .big { font-size: 15px; color: #44535e; margin-bottom: 6px; font-weight: 600; }
.empty-state code { font-family: var(--mono); background: #f0f2f3; padding: 1px 5px; border-radius: 3px; }

.kpis { display: grid; grid-template-columns: repeat(3, 1fr); border-bottom: 1px solid var(--line); }
.kpi { padding: 14px 14px 12px; border-right: 1px solid var(--line); }
.kpi:last-child { border-right: 0; }
.kpi .label { font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 4px; }
.kpi .value { font-family: var(--mono); font-size: 20px; font-weight: 600; letter-spacing: -.02em; color: #1b2b35; }
.kpi .value .unit { font-size: 11px; color: var(--muted); font-weight: 400; margin-left: 3px; }
.kpi .sub { font-size: 11px; color: var(--muted-2); margin-top: 3px; }

.res-meta { padding: 10px 16px; border-bottom: 1px solid var(--line); font-size: 12px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 14px; }
.res-meta strong { color: #33424c; font-weight: 600; }

.section-title {
  padding: 12px 16px 6px;
  font-size: 10.5px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.section-title span { text-transform: none; letter-spacing: 0; font-size: 11.5px; color: var(--muted-2); }

.data-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.data-table th {
  text-align: left;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  font-weight: 600;
  padding: 6px 16px;
  border-bottom: 1px solid var(--line);
  background: #fbfcfc;
  white-space: nowrap;
}
.data-table td { padding: 7px 16px; border-bottom: 1px solid #edf0f2; vertical-align: top; }
.data-table tr:last-child td { border-bottom: 0; }
.data-table .num { font-family: var(--mono); text-align: right; white-space: nowrap; }
.data-table .muted { color: var(--muted); }
.doc-sub { font-size: 11px; color: var(--muted); margin-top: 1px; }
.table-scroll { overflow-x: auto; }
.table-scroll .data-table { min-width: 560px; }
.ref-hardness th:nth-child(1), .ref-hardness td:nth-child(1) { width: 170px; }
.ref-hardness th:nth-child(2), .ref-hardness td:nth-child(2) { width: 150px; }
.ref-hardness th:nth-child(3), .ref-hardness td:nth-child(3) { width: auto; }

.check { display: flex; align-items: center; gap: 8px; font-size: 13px; height: 34px; }
.check input[type="checkbox"] { width: 15px; height: 15px; margin: 0; accent-color: var(--accent); }
.data-table tr.op-row { cursor: pointer; }
.data-table tr.op-row:hover td { background: #f7f9fa; }
.data-table tr.op-row td:first-child { position: relative; }
.data-table tr.op-row td:first-child::before {
  content: "›";
  position: absolute;
  left: 4px;
  color: var(--muted-2);
  transition: transform .12s;
  font-size: 14px;
}
.data-table tr.op-row.open td:first-child::before { transform: rotate(90deg); }
.data-table tr.op-detail td { background: #f8fafb; padding: 0 16px 10px 30px; }
.data-table tr.op-detail .formula {
  font-family: var(--mono);
  font-size: 11.5px;
  color: #3d4c57;
  background: #fff;
  border: 1px solid var(--line);
  border-left: 2px solid var(--accent);
  padding: 8px 10px;
  margin: 0 0 8px;
  white-space: pre-wrap;
  word-break: break-word;
}
.data-table tr.op-detail .detail { font-size: 11.5px; color: var(--muted); }

.totals { padding: 4px 16px 12px; }
.totals .row { display: flex; justify-content: space-between; gap: 16px; padding: 5px 0; font-size: 12.5px; border-bottom: 1px dotted #e3e8eb; }
.totals .row:last-child { border-bottom: 0; }
.totals .row .num { font-family: var(--mono); }
.totals .row.total { border-top: 1px solid var(--line-strong); border-bottom: 0; margin-top: 4px; padding-top: 8px; font-weight: 600; font-size: 13.5px; }
.totals .row.price { font-size: 15px; font-weight: 600; color: var(--accent); }
.totals .row .muted { color: var(--muted); font-size: 11.5px; }

/* фиксированная раскладка, чтобы длинные формулы не растягивали панель */
.result-panel .data-table { table-layout: fixed; width: 100%; }
.result-panel .data-table td, .result-panel .data-table th { overflow-wrap: anywhere; }
.result-panel .data-table .num { width: 128px; }
.result-panel .data-table tr.op-detail td { padding-left: 30px; }
.result-panel .formula { white-space: pre-wrap; overflow-wrap: anywhere; }

.notes { margin: 0; padding: 10px 16px 14px 30px; font-size: 11.5px; color: var(--muted); border-top: 1px solid var(--line); }
.notes li { margin-bottom: 3px; }

/* ---------- методика ---------- */
details.formula-block { border-bottom: 1px solid var(--line); }
details.formula-block:last-child { border-bottom: 0; }
details.formula-block summary {
  cursor: pointer;
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 600;
  color: #33424c;
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
details.formula-block summary::-webkit-details-marker { display: none; }
details.formula-block summary::after { content: "+"; font-family: var(--mono); color: var(--muted); font-weight: 400; }
details.formula-block[open] summary::after { content: "−"; }
details.formula-block summary:hover { background: var(--panel-2); }
.formula-body { padding: 0 16px 14px; font-size: 12.5px; color: #3b4a55; }
.formula-body .fx {
  font-family: var(--mono);
  font-size: 12px;
  background: #f7f9fa;
  border: 1px solid var(--line);
  border-left: 2px solid var(--accent);
  padding: 8px 10px;
  margin: 6px 0 10px;
  white-space: pre-wrap;
  color: #23323c;
}
.formula-body ul { margin: 4px 0 10px; padding-left: 18px; }
.formula-body li { margin-bottom: 3px; }
.formula-body p { margin: 0 0 8px; }

/* ---------- история ---------- */
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar .search { max-width: 320px; }
.toolbar .spacer { flex: 1; }
.history-table td { vertical-align: middle; }
.history-table .doc-name { font-weight: 600; color: #26343d; }
.history-table .doc-sub { font-size: 11px; color: var(--muted); }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }

/* ---------- справочники ---------- */
.subtabs { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 14px; }
.subtab {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  font: inherit;
  font-size: 12.5px;
  padding: 6px 12px;
  border-radius: 3px;
  cursor: pointer;
}
.subtab:hover { color: var(--text); border-color: var(--line-strong); }
.subtab.active { background: var(--accent-soft); border-color: #b9cfdb; color: var(--accent); font-weight: 600; }

.ref-table td { vertical-align: middle; }
.ref-table .code { font-family: var(--mono); font-size: 12px; font-weight: 600; }
.ref-table .actions { text-align: right; white-space: nowrap; }
.badge { display: inline-block; font-size: 10.5px; padding: 1px 6px; border: 1px solid var(--line-strong); border-radius: 2px; color: var(--muted); background: #fbfcfc; white-space: nowrap; }
.badge.ok { color: var(--ok); border-color: #bcd9cb; background: var(--ok-soft); }
.badge.warn { color: var(--warn); border-color: #ddcba9; background: var(--warn-soft); }

.template-list { font-family: var(--mono); font-size: 11.5px; color: #46555f; line-height: 1.55; }
.template-list .arrow { color: var(--muted-2); }

/* ---------- модальные окна ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(24, 34, 41, .38);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 60px 16px 30px;
  z-index: 100;
  overflow-y: auto;
}
.modal {
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  width: 100%;
  max-width: 620px;
  box-shadow: 0 18px 48px rgba(20, 30, 38, .18);
}
.modal-head { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.modal-head h3 { font-size: 14px; }
.modal-body { padding: 16px 18px; max-height: 62vh; overflow-y: auto; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; background: var(--panel-2); }
.modal .form-grid { gap: 10px 14px; }
.modal-error { color: var(--danger); font-size: 12.5px; margin-top: 10px; min-height: 16px; }
.close-x { appearance: none; border: 0; background: none; font-size: 20px; line-height: 1; color: var(--muted); cursor: pointer; padding: 2px 6px; }
.close-x:hover { color: var(--text); }

/* ---------- уведомления ---------- */
#toast-root { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast {
  min-width: 220px;
  max-width: 380px;
  background: #24333d;
  color: #eef2f4;
  border-left: 3px solid #5d8aa3;
  padding: 10px 14px;
  font-size: 12.5px;
  box-shadow: 0 6px 18px rgba(20, 30, 38, .2);
  animation: toast-in .18s ease-out;
}
.toast.error { border-left-color: #c46a6a; }
.toast.ok { border-left-color: #4f9a78; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.app-foot {
  margin-top: 26px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  font-size: 11.5px;
  color: var(--muted-2);
  line-height: 1.5;
}

/* ---------- логин ---------- */
.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: #eef0f2; padding: 20px; }
.login-card { width: 100%; max-width: 380px; background: #fff; border: 1px solid var(--line-strong); border-radius: var(--radius); }
.login-head { background: #24333d; color: #eef2f4; padding: 20px 22px; }
.login-head .brand { margin-bottom: 10px; }
.login-head h1 { font-size: 15px; font-weight: 600; line-height: 1.35; }
.login-body { padding: 22px; }
.login-body .field { margin-bottom: 12px; }
.login-body .btn { width: 100%; margin-top: 6px; }
.login-error { color: var(--danger); font-size: 12.5px; min-height: 18px; margin-top: 8px; }
.login-foot { padding: 12px 22px; border-top: 1px solid var(--line); font-size: 11px; color: var(--muted-2); display: flex; justify-content: space-between; }

/* ---------- адаптив ---------- */
@media (max-width: 1120px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .result-col { order: 2; }
  .layout > .col:first-child { order: 1; }
}
@media (max-width: 720px) {
  .wrap { padding: 0 14px; }
  .topbar { padding: 0 14px; }
  .brand .sub { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: 1fr; }
  .kpi { border-right: 0; border-bottom: 1px solid var(--line); }
  .kpi:last-child { border-bottom: 0; }
  .data-table th, .data-table td { padding-left: 10px; padding-right: 10px; }
  .modal-body { max-height: 70vh; }
}

/* ---------- печать ---------- */
@media print {
  .topbar, .nav, .formulas-panel, .form-actions, .panel-head .head-actions, .no-print { display: none !important; }
  body { background: #fff; }
  .layout { display: block; }
  .col:first-child { display: none; }
  .panel { border-color: #bbb; break-inside: avoid; }
  .result-col { width: 100%; }
  .data-table tr.op-detail { display: table-row !important; }
  .data-table tr.op-detail.hidden { display: table-row !important; }
}
