:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --ink: #1d2430;
  --muted: #667085;
  --line: #dde1e7;
  --brand: #c8102e;
  --brand-ink: #ffffff;
  --accent: #516470;
  --result: #c8102e;
  --warn-bg: #fff7e6;
  --warn-ink: #7a4b00;
  --err-bg: #fdecec;
  --err-ink: #9b1c1c;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  font-family: "Segoe UI", Tahoma, Verdana, Arial, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 14px; line-height: 1.4; }
h1, h2, h3 { margin: 0; }
button, input, select { font: inherit; color: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 12px 20px; background: var(--card); border-bottom: 3px solid var(--brand);
}
.brand { display: flex; align-items: center; gap: 14px; }
.logo { height: 30px; }
.brand h1 { font-size: 18px; }
.version { color: var(--muted); font-size: 12px; }
.toolbar { display: flex; gap: 6px; flex-wrap: wrap; }

button, .btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 7px 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
  cursor: pointer; text-decoration: none; color: var(--accent); font-weight: 600; font-size: 13px;
}
button:hover, .btn:hover { border-color: var(--accent); }
button:focus-visible, select:focus-visible, input:focus-visible, .btn:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
button.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
button.primary:hover { filter: brightness(.95); }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover { border-color: var(--line); }
button.danger-ghost { color: var(--err-ink); }
button:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Project bar ---------- */
.projectbar {
  display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap;
  padding: 12px 20px; background: #fbfbfc; border-bottom: 1px solid var(--line);
}
.btn-group { display: flex; gap: 6px; flex-wrap: wrap; }
.grow { flex: 1 1 220px; }

/* ---------- Layout ---------- */
.layout {
  display: grid; gap: 16px; padding: 16px 20px;
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr) minmax(260px, 340px);
  align-items: start;
}
@media (max-width: 1100px) { .layout { grid-template-columns: minmax(280px, 340px) minmax(0, 1fr); } .history { grid-column: 1 / -1; } }
@media (max-width: 720px) { .layout { grid-template-columns: 1fr; padding: 12px 16px; } .topbar, .projectbar { padding: 12px 16px; } }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.card-head h2 { font-size: 15px; }
.muted { color: var(--muted); font-size: 12px; }

/* ---------- Form ---------- */
.field { display: flex; flex-direction: column; gap: 3px; margin-bottom: 9px; }
.field > span { font-size: 12px; font-weight: 600; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.field.inline { margin-bottom: 0; }
.field select, .field input {
  width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: 7px; background: #fff; min-height: 34px;
}
.field select.invalid, .field input.invalid { border-color: var(--err-ink); background: var(--err-bg); }
.info {
  width: 18px; height: 18px; padding: 0; border-radius: 50%; font-size: 11px; font-style: italic; font-family: Georgia, serif;
  line-height: 1; color: var(--accent);
}
fieldset.sub { border: 0; border-left: 3px solid var(--brand); margin: 4px 0 10px; padding: 4px 0 0 10px; }
fieldset.sub[hidden] { display: none; }
.actions { display: flex; gap: 8px; margin-top: 6px; }
.actions .primary { flex: 1; padding: 10px; }

/* ---------- Result ---------- */
.result-box { min-height: 56px; }
.result-line { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; padding: 6px 0; border-bottom: 1px dashed var(--line); }
.result-line:last-child { border-bottom: 0; }
.result-val { color: var(--result); font-weight: 700; font-size: 20px; white-space: nowrap; }
.result-lbl { color: var(--muted); }
.placeholder { color: var(--muted); padding: 8px 0; }
.note { border-radius: 7px; padding: 7px 10px; margin-top: 8px; font-size: 13px; }
.note.warn { background: var(--warn-bg); color: var(--warn-ink); }
.note.err { background: var(--err-bg); color: var(--err-ink); }

.diagram { margin-top: 12px; display: flex; justify-content: center; }
.fig { position: relative; display: block; width: 100%; margin: 0; container-type: inline-size; }
.fig img { display: block; width: 100%; height: auto; }
.fig .lbl {
  position: absolute; color: #f50606; font-weight: 700; white-space: nowrap;
  font-size: clamp(9px, 2.1cqw, 13px); line-height: 1; font-family: Consolas, "Courier New", monospace;
  background: rgba(255, 255, 255, .75); padding: 1px 2px; border-radius: 3px;
}

/* ---------- History ---------- */
.history-list { max-height: 560px; overflow: auto; }
.hist-empty { color: var(--muted); padding: 8px 0; }
table.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
table.grid th, table.grid td { text-align: left; padding: 6px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.grid th { font-size: 11px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: #fafbfc; position: sticky; top: 0; }
table.grid tr.active td { background: #fff3f4; }
.hist-row { cursor: pointer; }
.hist-row:hover td { background: #f7f8fa; }
.hist-res { color: var(--result); font-weight: 600; white-space: nowrap; }
.icon-btn { padding: 3px 7px; font-size: 12px; }

/* ---------- Dialog ---------- */
dialog { border: 0; border-radius: 12px; padding: 0; max-width: min(900px, calc(100vw - 32px)); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(16, 24, 40, .45); }
.dlg-inner { padding: 18px 20px; }
.dlg-inner h3 { font-size: 16px; margin-bottom: 10px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.opt-list { display: grid; gap: 6px; }
.opt { display: flex; gap: 10px; align-items: flex-start; padding: 8px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; background: #fff; text-align: left; font-weight: 400; color: var(--ink); justify-content: flex-start; }
.opt b { color: #c04000; min-width: 170px; }
.opt.sel { border-color: var(--brand); background: #fff3f4; }
.tt-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; }
.tt-grid .opt { flex-direction: column; align-items: center; }
.tt-grid img { max-width: 100%; }
.refs li { margin-bottom: 6px; }

/* ---------- Report ---------- */
.report { position: fixed; inset: 0; background: rgba(16,24,40,.45); overflow: auto; z-index: 50; padding: 24px 16px; }
.report-sheet { background: #fff; max-width: 1000px; margin: 0 auto; border-radius: 12px; padding: 20px 24px; }
.report-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; border-bottom: 2px solid var(--brand); padding-bottom: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.report-head img { height: 28px; }
.report-item { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 14px; break-inside: avoid; page-break-inside: avoid; }
.report-item h4 { margin: 0 0 8px; font-size: 14px; }
.report-body { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; align-items: start; }
@media (max-width: 720px) { .report-body { grid-template-columns: 1fr; } }
.kv { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.kv td { padding: 3px 6px; border-bottom: 1px solid #eef0f3; }
.kv td:first-child { color: var(--muted); width: 45%; }

/* ---------- Toast ---------- */
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1d2430; color: #fff; padding: 9px 16px; border-radius: 8px; z-index: 100; box-shadow: var(--shadow); max-width: calc(100vw - 32px); }

.foot { text-align: center; padding: 10px 0 24px; }
.foot img { max-width: 100%; }

/* ---------- Print ---------- */
@media print {
  body { background: #fff; }
  .no-print { display: none !important; }
  .card { box-shadow: none; }
  .layout { grid-template-columns: 1fr 1.4fr; padding: 0; }
  body.printing-report > *:not(#reportView) { display: none !important; }
  body.printing-report #reportView { position: static; background: #fff; padding: 0; overflow: visible; }
  body.printing-report .report-sheet { max-width: none; padding: 0; border-radius: 0; }
  .fig .lbl { background: transparent; }
  .toast, dialog { display: none !important; }
}
