:root {
  --bg: #f4f6f8;
  --card-bg: #ffffff;
  --border: #dde3e8;
  --text: #1f2933;
  --muted: #667080;
  --accent: #2f6fed;
  --accent-hover: #1e5bd6;
  --danger: #c0392b;
  --review: #e2963f;
  --review-bg: #f3c99a;
  --radius: 8px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.app {
  max-width: 1300px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

.app-header h1 {
  margin: 0 0 4px;
  font-size: 22px;
}

.subtitle {
  margin: 0 0 20px;
  color: var(--muted);
}

.layout {
  display: flex;
  gap: 24px;
  align-items: flex-start;
}

.form-col {
  flex: 1 1 55%;
  min-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.output-col {
  flex: 1 1 45%;
  min-width: 320px;
  position: sticky;
  top: 20px;
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}

.card h2 {
  margin: 0 0 12px;
  font-size: 16px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
}

.hint {
  color: var(--muted);
  font-size: 12.5px;
  margin: -4px 0 12px;
}

.field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.field label:not(.switch) {
  flex: 1;
  font-weight: 500;
}

.field select,
.field input[type="text"] {
  flex: 0 0 auto;
}

select, input[type="text"], textarea {
  font-family: inherit;
  font-size: 13.5px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}

select { min-width: 160px; }

.toggle-field label:first-child { cursor: default; }

.subfield {
  margin: -4px 0 14px 0;
  padding: 10px 12px;
  background: #f8fafc;
  border: 1px dashed var(--border);
  border-radius: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-start;
}

.subfield.hidden, .hidden { display: none !important; }

.subfield select { min-width: 140px; }

/* bloki reaktywności (Monocyty/Limfocyty): pola jedno pod drugim, nie w jednym wierszu */
#mono-detail, #limfo-detail {
  flex-direction: column;
  align-items: stretch;
}

#mono-detail > .field, #limfo-detail > .field {
  width: 100%;
}

.chip-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  width: 100%;
}

.chip-btn {
  border: 1px solid var(--accent);
  color: var(--accent);
  background: #fff;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12.5px;
  cursor: pointer;
}

.chip-btn:hover { background: #eaf1ff; }

.chip-btn.added {
  opacity: 0.4;
  cursor: not-allowed;
  border-color: var(--muted);
  color: var(--muted);
}

.chip-rows {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}

.chip-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
}

.chip-row .chip-label {
  flex: 0 0 auto;
  font-weight: 500;
  min-width: 150px;
}

.chip-row input[type="text"] {
  flex: 1;
  min-width: 90px;
}

.chip-row select.unit-select,
.subfield select.unit-select {
  flex: 0 0 auto;
  min-width: 68px;
  width: 68px;
}

.chip-row .chip-remove {
  border: none;
  background: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  padding: 2px 4px;
}

.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 14px 0;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  cursor: pointer;
}

.checkbox-label.small { font-size: 12px; color: var(--muted); }

.inline-label {
  font-size: 12.5px;
  color: var(--muted);
}

.inline-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

textarea#ery-hipochromia-uwagi,
textarea#opt-przypis {
  width: 100%;
  resize: vertical;
}

/* switch */
.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  min-width: 40px;
  max-width: 40px;
  height: 22px;
  flex: 0 0 40px;
}

.switch input { opacity: 0; width: 0; height: 0; }

.slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: #ccd3da;
  transition: .15s;
  border-radius: 999px;
}

/* przełączniki, których domyślny (nieustawiony) stan i tak trafia do wyniku —
   blady pomarańcz sygnalizuje "jeszcze nietknięte", znika po pierwszej zmianie */
.switch.needs-review .slider {
  background-color: var(--review-bg);
}

select.needs-review {
  border-color: var(--review);
  border-width: 1.5px;
  box-shadow: 0 0 0 1px var(--review);
}

.slider:before {
  position: absolute;
  content: "";
  height: 16px; width: 16px;
  left: 3px; bottom: 3px;
  background-color: white;
  transition: .15s;
  border-radius: 50%;
}

.switch input:checked + .slider { background-color: var(--accent); }
.switch input:checked + .slider:before { transform: translateX(18px); }

.btn {
  border: none;
  border-radius: 6px;
  padding: 9px 16px;
  font-size: 13.5px;
  cursor: pointer;
  font-weight: 500;
}

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

.btn-secondary {
  background: #eef1f4;
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: #e2e6ea; }

.output-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.output-header h2 { margin: 0; font-size: 16px; }

#output {
  width: 100%;
  min-height: 420px;
  resize: vertical;
  line-height: 1.5;
  font-size: 14px;
}

.output-actions {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.feedback {
  min-height: 18px;
  color: #2e8b57;
  font-size: 12.5px;
  margin: 8px 0 0;
}

#print-area { display: none; }

@media print {
  .app-header, .layout .form-col, .output-actions, .output-header { display: none !important; }
  body * { visibility: hidden; }
  #print-area, #print-area * { visibility: visible; }
  #print-area {
    display: block;
    position: absolute;
    top: 0; left: 0;
    width: 100%;
    white-space: pre-wrap;
    font-family: "Segoe UI", Arial, sans-serif;
    font-size: 13px;
    padding: 20px;
  }
}

@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .output-col { position: static; width: 100%; }
}

@media (max-width: 520px) {
  .app { padding: 16px 12px 40px; }
  .field { flex-direction: column; align-items: flex-start; }
  .field select, .field input[type="text"] { width: 100%; min-width: 0; }
  .inline-group input[type="text"] { width: 70px !important; }
  .chip-row { flex-wrap: wrap; }
  .chip-row .chip-label { min-width: 0; }
}
