:root {
  color-scheme: light;
  --bg: #f4f7f6;
  --surface: #ffffff;
  --surface-strong: #f9fbfa;
  --text: #17211f;
  --muted: #5d6b68;
  --line: #d6dfdc;
  --line-strong: #aebbb7;
  --accent: #116b63;
  --accent-dark: #0b4f49;
  --accent-soft: #dff0ec;
  --rose: #d67c90;
  --paper-width: 148mm;
  --paper-height: 105mm;
  --print-offset-x: 0mm;
  --print-offset-y: 0mm;
  --print-scale: 1;
  --font-scale: 1;
  --page-width: 148mm;
  --page-height: 105mm;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-width: 320px;
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

.action-link,
button,
input,
textarea {
  font: inherit;
}

.action-link,
button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 14px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
}

.action-link:hover,
button:hover {
  border-color: var(--line-strong);
}

button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #ffffff;
}

button.primary:hover {
  background: var(--accent-dark);
}

.action-link.ghost,
button.ghost {
  background: transparent;
}

.app-shell {
  width: min(1560px, 100%);
  margin: 0 auto;
  padding: 18px;
}

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

.topbar h1 {
  margin: 0;
  font-size: clamp(1.55rem, 2.8vw, 2.2rem);
  font-weight: 750;
  letter-spacing: 0;
}

.subline,
.preview-head p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.93rem;
}

.top-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.notice {
  margin-bottom: 14px;
  border: 1px solid #dbc794;
  border-left: 5px solid #a77a20;
  border-radius: 7px;
  padding: 10px 12px;
  background: #fff8e6;
  color: #3d3321;
  line-height: 1.45;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(360px, 0.92fr) minmax(520px, 1.08fr);
  gap: 16px;
  align-items: start;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 12px 32px rgb(24 40 36 / 0.06);
}

.form-panel {
  padding: 16px;
}

.preview-panel {
  position: sticky;
  top: 12px;
  padding: 16px;
}

.toolbar,
.preview-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.segmented {
  display: flex;
  margin: 0;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
}

.segmented label {
  display: block;
  min-width: 86px;
}

.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented span {
  display: flex;
  min-height: 32px;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  padding: 0 12px;
  color: var(--muted);
  font-size: 0.94rem;
  cursor: pointer;
}

.segmented input:checked + span {
  background: var(--accent);
  color: #ffffff;
}

.status-chip {
  display: inline-flex;
  min-height: 34px;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--accent-soft);
  border-radius: 999px;
  padding: 0 12px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 0.92rem;
  font-weight: 720;
}

.status-chip input {
  width: 16px;
  min-height: 16px;
  accent-color: var(--accent);
}

.form-section {
  border-top: 1px solid var(--line);
  padding-top: 14px;
  margin-top: 14px;
}

.form-section:first-of-type {
  border-top: 0;
  padding-top: 0;
  margin-top: 0;
}

.quality-messages {
  border: 1px solid #d9ad6b;
  border-left: 5px solid #b36b16;
  border-radius: 7px;
  margin-bottom: 14px;
  padding: 10px 12px;
  background: #fff6e8;
  color: #3d2d1b;
  font-size: 0.9rem;
  line-height: 1.42;
}

.quality-messages[hidden] {
  display: none;
}

.quality-messages strong {
  display: block;
  margin-bottom: 5px;
}

.quality-messages ul {
  margin: 0;
  padding-left: 18px;
}

.form-section h2,
.preview-head h2,
.technical-panel summary {
  margin: 0 0 10px;
  font-size: 1rem;
  font-weight: 720;
  letter-spacing: 0;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.field-grid.three {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.field-grid.compact {
  gap: 8px;
}

.field-grid > label,
.field-grid > .preset-field,
.med-row label {
  display: flex;
  min-width: 0;
  flex-direction: column;
  gap: 5px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 650;
}

.field-grid .wide {
  grid-column: 1 / -1;
}

.field-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.field-label-row > label:first-child {
  min-width: 0;
}

.preset-toggle {
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 5px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
}

.preset-toggle input {
  width: 15px;
  min-height: 15px;
  accent-color: var(--accent);
}

.stamp-toggle {
  display: flex;
  width: fit-content;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 650;
}

.stamp-toggle input {
  width: 16px;
  min-height: 16px;
  accent-color: var(--accent);
}

.stamp-options {
  margin-top: 0;
}

.preset-field.is-print-disabled input,
.preset-field.is-print-disabled textarea {
  background: #f2f5f4;
  color: var(--muted);
}

.label-spacer {
  min-height: 1em;
}

input,
textarea {
  width: 100%;
  min-height: 37px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 7px 9px;
  background: #ffffff;
  color: var(--text);
}

textarea {
  min-height: 78px;
  resize: vertical;
}

input:disabled,
textarea:disabled {
  border-color: var(--line);
  background: #f2f5f4;
  color: var(--muted);
}

input:focus,
textarea:focus,
button:focus-visible,
summary:focus-visible {
  outline: 3px solid rgb(17 107 99 / 0.2);
  outline-offset: 2px;
  border-color: var(--accent);
}

.choice-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px 12px;
  margin-top: 12px;
}

.choice-grid label,
.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 30px;
  color: var(--text);
  font-size: 0.9rem;
}

.choice-grid input,
.toggle input {
  width: 16px;
  min-height: 16px;
  accent-color: var(--accent);
}

.medication-list {
  display: grid;
  gap: 10px;
}

.med-row {
  display: grid;
  grid-template-columns: 30px minmax(150px, 1.25fr) minmax(80px, 0.55fr) minmax(110px, 0.75fr) minmax(78px, 0.45fr);
  gap: 8px;
  align-items: end;
}

.med-row::before {
  content: attr(data-index);
  display: grid;
  align-self: stretch;
  min-width: 28px;
  border: 1px solid var(--line);
  border-radius: 7px;
  place-items: center;
  background: var(--surface-strong);
  color: var(--muted);
  font-weight: 750;
}

.preview-stage {
  width: fit-content;
  max-width: 100%;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(45deg, #f1f4f3 25%, transparent 25%),
    linear-gradient(-45deg, #f1f4f3 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #f1f4f3 75%),
    linear-gradient(-45deg, transparent 75%, #f1f4f3 75%);
  background-color: #e8eeec;
  background-position:
    0 0,
    0 6px,
    6px -6px,
    -6px 0;
  background-size: 12px 12px;
  padding: 0;
}

.print-viewport {
  width: fit-content;
  margin: 0;
}

.print-sheet {
  position: relative;
  width: var(--paper-width);
  height: var(--paper-height);
  overflow: hidden;
  background: #fffefe;
  box-shadow: 0 10px 22px rgb(14 24 21 / 0.18);
  transform-origin: top left;
}

.template-image {
  position: absolute;
  inset: 0;
  z-index: 0;
  width: 100%;
  height: 100%;
  object-fit: fill;
  pointer-events: none;
}

.template-image[hidden] {
  display: none;
}

.print-sheet.rosa {
  background: #fff7f9;
}

.print-sheet.privat {
  background: #f8fbff;
}

.template-lines {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: block;
  opacity: 1;
  pointer-events: none;
}

.print-sheet.has-real-template .template-lines {
  display: none;
}

.print-sheet.rosa .template-lines {
  background:
    linear-gradient(var(--rose), var(--rose)) 5mm 5mm / 62mm 0.35mm no-repeat,
    linear-gradient(var(--rose), var(--rose)) 5mm 14mm / 62mm 0.35mm no-repeat,
    linear-gradient(var(--rose), var(--rose)) 5mm 25mm / 62mm 0.35mm no-repeat,
    linear-gradient(var(--rose), var(--rose)) 72mm 8mm / 68mm 0.35mm no-repeat,
    linear-gradient(var(--rose), var(--rose)) 72mm 26mm / 68mm 0.35mm no-repeat,
    linear-gradient(var(--rose), var(--rose)) 7mm 39mm / 134mm 0.35mm no-repeat,
    linear-gradient(var(--rose), var(--rose)) 7mm 62mm / 134mm 0.35mm no-repeat,
    linear-gradient(var(--rose), var(--rose)) 7mm 84mm / 84mm 0.35mm no-repeat,
    linear-gradient(90deg, transparent 0 42%, rgb(214 124 144 / 0.24) 42% 58%, transparent 58%) 0 0 / 8mm 100%;
}

.print-sheet.privat .template-lines {
  background:
    linear-gradient(#85a8d4, #85a8d4) 8mm 11mm / 128mm 0.35mm no-repeat,
    linear-gradient(#85a8d4, #85a8d4) 8mm 27mm / 128mm 0.35mm no-repeat,
    linear-gradient(#85a8d4, #85a8d4) 8mm 44mm / 128mm 0.35mm no-repeat,
    linear-gradient(#85a8d4, #85a8d4) 8mm 80mm / 88mm 0.35mm no-repeat;
}

.calibration-lines {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: none;
  pointer-events: none;
  background-image:
    linear-gradient(to right, rgb(17 107 99 / 0.28) 0.2mm, transparent 0.2mm),
    linear-gradient(to bottom, rgb(17 107 99 / 0.28) 0.2mm, transparent 0.2mm),
    linear-gradient(to right, rgb(17 107 99 / 0.46) 0.28mm, transparent 0.28mm),
    linear-gradient(to bottom, rgb(17 107 99 / 0.46) 0.28mm, transparent 0.28mm);
  background-size: 5mm 5mm, 5mm 5mm, 10mm 10mm, 10mm 10mm;
}

.print-sheet.calibration .calibration-lines,
.print-sheet.print-guides .calibration-lines {
  display: block;
}

.field-layer {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
}

.print-field {
  position: absolute;
  left: calc(var(--x) * 1mm);
  top: calc(var(--y) * 1mm);
  width: calc(var(--w) * 1mm);
  height: calc(var(--h, 4) * 1mm);
  color: #111111;
  font-family: Arial, Helvetica, sans-serif;
  font-size: calc(var(--fs, 3) * 1mm * var(--font-scale));
  line-height: 1.18;
  white-space: pre-wrap;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.print-field.overflowing {
  outline: 0.24mm solid rgb(179 88 22 / 0.5);
  outline-offset: -0.12mm;
}

.print-field.checkbox-mark {
  width: calc(var(--w, 4) * 1mm);
  height: calc(var(--h, 4) * 1mm);
  display: grid;
  place-items: center;
  border: 0;
  font-weight: 800;
  line-height: 1;
}

.print-field.guide-frame {
  outline: 0.18mm dashed rgb(17 107 99 / 0.42);
  outline-offset: 0;
}

.print-field.label-hint {
  display: none;
}

.print-sheet.calibration .print-field.label-hint {
  display: block;
  color: var(--accent-dark);
  font-size: 2mm;
}

.technical-panel {
  border-top: 1px solid var(--line);
  margin-top: 16px;
  padding-top: 14px;
}

.technical-panel summary {
  width: fit-content;
  border-radius: 6px;
  color: var(--accent-dark);
  cursor: pointer;
}

.technical-body {
  display: grid;
  gap: 12px;
  padding-top: 6px;
}

.technical-toggles {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.calibration-actions {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) auto;
  gap: 8px 10px;
  align-items: end;
}

.compact-button {
  min-height: 37px;
  white-space: nowrap;
}

.helper-text {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.35;
}

.position-editor {
  margin-top: 0;
}

.position-editor summary {
  width: fit-content;
  border-radius: 6px;
  color: var(--accent-dark);
  cursor: pointer;
  font-weight: 720;
}

.position-table-wrap {
  overflow: auto;
  max-height: 360px;
  margin-top: 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
}

.position-table {
  width: 100%;
  min-width: 520px;
  border-collapse: collapse;
  font-size: 0.84rem;
}

.position-table th,
.position-table td {
  border-bottom: 1px solid var(--line);
  padding: 7px;
  text-align: left;
}

.position-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-strong);
  color: var(--muted);
}

.position-table td:first-child {
  color: var(--text);
  font-weight: 650;
}

.position-table input {
  min-height: 30px;
  padding: 4px 6px;
}

#printArea {
  display: none;
}

@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .preview-panel {
    position: static;
  }
}

@media (max-width: 720px) {
  .app-shell {
    padding: 12px;
  }

  .topbar,
  .toolbar,
  .preview-head {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions {
    justify-content: stretch;
  }

  .top-actions button {
    flex: 1;
  }

  .segmented label {
    flex: 1;
    min-width: 0;
  }

  .field-grid,
  .field-grid.three,
  .choice-grid,
  .med-row,
  .calibration-actions {
    grid-template-columns: 1fr;
  }

  .med-row::before {
    min-height: 28px;
  }

  .preview-stage {
    padding: 0;
  }
}

@media print {
  html,
  body {
    width: var(--page-width);
    height: var(--page-height);
    margin: 0;
    overflow: hidden;
    background: #ffffff;
  }

  .app-shell {
    display: none;
  }

  #printArea {
    display: block;
    width: var(--page-width);
    height: var(--page-height);
    margin: 0;
    overflow: hidden;
  }

  .print-sheet {
    width: var(--page-width);
    height: var(--page-height);
    box-shadow: none;
    background: transparent !important;
    transform: translate(var(--print-offset-x), var(--print-offset-y)) scale(var(--print-scale));
  }

  .template-lines {
    display: none;
  }

  .template-image {
    display: none !important;
  }

  .print-sheet.print-guides .template-lines {
    display: block;
  }

  .print-sheet.has-real-template .template-lines {
    display: none;
  }

  .print-sheet.calibration .calibration-lines {
    display: none;
  }

  .print-sheet.print-guides .calibration-lines {
    display: block;
  }

  .print-field.guide-frame {
    outline: 0;
  }

  .print-field.overflowing {
    outline: 0;
  }
}
