:root {
  --bg: #eef2f5;
  --panel: #ffffff;
  --ink: #17202a;
  --muted: #607080;
  --line: #cfd8df;
  --soft-line: #e6ecef;
  --primary: #176b63;
  --primary-dark: #0f4e48;
  --accent: #9b5c22;
  --danger: #b42318;
  --shadow: 0 18px 50px rgba(24, 41, 54, 0.14);
}

* {
  box-sizing: border-box;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

html {
  min-height: 100%;
}

body {
  min-height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 980px);
  justify-content: center;
  gap: 20px;
  max-width: 1480px;
  min-height: 100vh;
  margin: 0 auto;
  padding: 20px;
}

body.preview-open .app-shell {
  grid-template-columns: minmax(0, 1fr) minmax(360px, 520px);
}

.workspace,
.preview-column {
  min-width: 0;
}

.topbar,
.tab-panel,
.preview-sticky,
.admin-section {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 20px;
  border-radius: 8px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 28px;
  line-height: 1.15;
}

h2 {
  margin-bottom: 6px;
  font-size: 20px;
}

h3 {
  margin-bottom: 14px;
  font-size: 17px;
}

.topbar-actions,
.admin-actions,
.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.primary-button,
.secondary-button,
.ghost-button,
.danger-button {
  min-height: 40px;
  padding: 0 14px;
  border-radius: 6px;
  border: 1px solid transparent;
  font-weight: 700;
  white-space: nowrap;
}

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

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

.secondary-button {
  background: #f2f7f5;
  border-color: #b7d0ca;
  color: var(--primary-dark);
}

.ghost-button {
  background: #ffffff;
  border-color: var(--line);
  color: var(--ink);
}

.danger-button {
  background: #fff4f2;
  border-color: #f1b7af;
  color: var(--danger);
}

.tabs {
  display: flex;
  gap: 8px;
  margin: 18px 0 0;
  padding: 0;
}

.tab-button {
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 8px 8px 0 0;
  background: #dfe7eb;
  color: var(--muted);
  font-weight: 700;
}

.tab-button.active {
  background: var(--panel);
  color: var(--ink);
}

.tab-panel {
  display: none;
  padding: 20px;
  border-radius: 0 8px 8px 8px;
}

.tab-panel.active {
  display: block;
}

.panel-heading {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: flex-start;
  margin-bottom: 18px;
}

.panel-heading p {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
}

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

.field {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.field span {
  color: #334155;
  font-size: 13px;
  font-weight: 700;
}

.span-2 {
  grid-column: 1 / -1;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid #b9c5ce;
  border-radius: 6px;
  background: #ffffff;
  color: var(--ink);
  padding: 10px 11px;
  outline: none;
}

input,
select {
  min-height: 42px;
}

textarea {
  min-height: 132px;
  resize: vertical;
  line-height: 1.4;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(23, 107, 99, 0.14);
}

.admin-grid {
  display: grid;
  grid-template-columns: minmax(280px, 0.8fr) minmax(360px, 1.2fr);
  gap: 18px;
}

.admin-section {
  border-radius: 8px;
  box-shadow: none;
  padding: 16px;
}

.backup-panel {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: flex-start;
  margin-top: 18px;
  padding: 16px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: #fbfcfd;
}

.backup-panel h3 {
  margin-bottom: 6px;
}

.backup-panel p {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
}

.inline-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 10px;
  align-items: center;
}

.template-form {
  grid-template-columns: 1fr;
}

.model-mode-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--soft-line);
  border-radius: 8px;
  background: #fbfcfd;
}

.model-mode-group legend {
  padding: 0 6px;
  color: #334155;
  font-size: 13px;
  font-weight: 700;
}

.model-mode-group label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 30px;
  font-weight: 700;
}

.model-mode-group input {
  width: auto;
  min-height: auto;
}

.submodel-editor {
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--soft-line);
  border-radius: 8px;
  background: #fbfcfd;
}

.submodel-heading,
.modal-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.submodel-heading h4 {
  margin: 0 0 4px;
  font-size: 15px;
}

.submodel-heading p,
.modal-heading p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}

.submodel-form {
  display: grid;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.modal-layer {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(15, 23, 42, 0.46);
}

.modal-card {
  width: min(720px, 100%);
  max-height: min(760px, calc(100vh - 40px));
  overflow: auto;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: var(--shadow);
}

.modal-card .list-item {
  grid-template-columns: minmax(0, 1fr) auto;
}

.list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.list-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: start;
  padding: 12px;
  border: 1px solid var(--soft-line);
  border-radius: 8px;
  background: #fbfcfd;
}

.item-title {
  margin: 0 0 4px;
  font-weight: 700;
  line-height: 1.3;
}

.item-meta {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
}

.empty-state {
  padding: 14px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  background: #fbfcfd;
}

.status-message {
  min-height: 22px;
  margin: 12px 2px 0;
  color: var(--primary-dark);
  font-weight: 700;
}

.status-message.error {
  color: var(--danger);
}

.hidden,
.file-input,
.print-only {
  display: none;
}

.preview-sticky {
  position: sticky;
  top: 20px;
  padding: 16px;
  border-radius: 8px;
}

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

.preview-title h2 {
  margin: 0;
}

.preview-title span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.paper-frame {
  overflow: auto;
  background: #d6dde2;
  border-radius: 6px;
  padding: 12px;
}

.preview-scale .rgo-print {
  transform-origin: top left;
}

.rgo-print {
  width: 794px;
  min-height: 1123px;
  margin: 0 auto;
  padding: 48px 40px 44px;
  background: #ffffff;
  border: 0;
  color: #000000;
  font-family: "Times New Roman", Times, serif;
  font-size: 16px;
}

.rgo-hospital {
  position: relative;
  height: 154px;
  display: grid;
  grid-template-columns: 176px minmax(0, 1fr);
  align-items: stretch;
  border: 3px solid #000000;
  background: #eeeeee;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 27px;
  font-weight: 700;
  text-align: center;
}

.rgo-hospital span {
  display: flex;
  grid-column: 2;
  align-items: center;
  justify-content: center;
  padding: 0 18px;
}

.hospital-logo {
  position: static;
  grid-column: 1;
  grid-row: 1 / 3;
  align-self: stretch;
  justify-self: stretch;
  width: 100%;
  height: 100%;
  max-height: none;
  padding: 22px 24px;
  background: #ffffff;
  object-fit: contain;
}

.rgo-title {
  height: auto;
  display: flex;
  grid-column: 2;
  align-items: center;
  justify-content: center;
  border: 0;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 22px;
  font-weight: 700;
  text-align: center;
}

.rgo-row {
  display: grid;
  min-height: 20px;
  border-left: 3px solid #000000;
  border-right: 3px solid #000000;
  border-bottom: 1px solid #111111;
}

.patient-row {
  grid-template-columns: 140px 1fr;
  min-height: 36px;
  margin-top: 14px;
  border-top: 3px solid #000000;
  border-bottom: 3px solid #000000;
}

.patient-row .rgo-label {
  font-size: 18px;
}

.two-col {
  grid-template-columns: 140px 1fr 140px 1fr;
}

.two-col.block-top {
  margin-top: 14px;
}

.details-row {
  grid-template-columns: 156px 1fr;
}

.details-row.block-top {
  margin-top: 14px;
}

.block-top {
  border-top: 3px solid #000000;
}

.block-bottom {
  border-bottom: 3px solid #000000;
}

.rgo-label,
.rgo-value {
  display: flex;
  align-items: center;
  min-width: 0;
  min-height: 29px;
  padding: 3px 8px;
  border-right: 1px solid #111111;
  line-height: 1.2;
}

.rgo-label {
  justify-content: center;
  background: #eeeeee;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 22px;
  font-weight: 700;
  text-align: center;
}

.two-col .rgo-label {
  font-size: 12px;
}

.two-col .rgo-value {
  justify-content: center;
  text-align: center;
}

.details-row .rgo-label {
  font-size: 11px;
  padding-left: 4px;
  padding-right: 4px;
  white-space: nowrap;
}

.details-row .rgo-value {
  font-size: 14px;
}

.right-label {
  border-left: 1px solid #111111;
}

.rgo-value {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.two-col .rgo-value:last-child,
.details-row .rgo-value:last-child,
.patient-row .rgo-value:last-child {
  border-right: 0;
}

.muted {
  color: #555555;
}

.rgo-spacer {
  height: 0;
  border: 0;
}

.rgo-description-label {
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 14px;
  padding: 7px;
  border-top: 3px solid #000000;
  border-left: 3px solid #000000;
  border-right: 3px solid #000000;
  border-bottom: 1px solid #111111;
  background: #eeeeee;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 22px;
  font-weight: 700;
}

.rgo-description-hint {
  min-height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 8px;
  border-left: 3px solid #000000;
  border-right: 3px solid #000000;
  border-bottom: 1px solid #111111;
  font-size: 9px;
  font-weight: 700;
  text-align: center;
}

.rgo-description {
  min-height: 502px;
  padding: 12px 8px;
  border-left: 3px solid #000000;
  border-right: 3px solid #000000;
  border-bottom: 3px solid #000000;
  font-size: 14px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.12;
}

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

  .preview-sticky {
    position: static;
  }

  .preview-column {
    order: 2;
  }
}

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

  .topbar,
  .panel-heading,
  .backup-panel {
    display: grid;
  }

  .form-grid,
  .admin-grid,
  .inline-form {
    grid-template-columns: 1fr;
  }

  .span-2 {
    grid-column: auto;
  }

  .tabs {
    overflow-x: auto;
  }

  .list-item {
    grid-template-columns: 1fr;
  }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 10mm;
  }

  body {
    background: #ffffff;
  }

  .no-print {
    display: none !important;
  }

  .print-only {
    display: block;
  }

  .rgo-print {
    width: 100%;
    min-height: auto;
    margin: 0;
    padding: 0;
    border: 0;
    box-shadow: none;
    font-size: 12px;
    page-break-after: avoid;
  }

  .rgo-hospital {
    height: 116px;
    grid-template-columns: 132px minmax(0, 1fr);
    font-size: 20px;
  }

  .hospital-logo {
    width: 100%;
    max-height: none;
    padding: 16px 18px;
  }

  .rgo-title {
    height: auto;
    font-size: 17px;
  }

  .patient-row {
    grid-template-columns: 104px 1fr;
    min-height: 28px;
  }

  .patient-row .rgo-label {
    font-size: 13px;
  }

  .two-col {
    grid-template-columns: 104px 1fr 104px 1fr;
  }

  .details-row {
    grid-template-columns: 116px 1fr;
  }

  .rgo-label,
  .rgo-value {
    min-height: 18px;
    padding: 2px 6px;
  }

  .rgo-label,
  .rgo-description-label {
    font-size: 15px;
    margin-top: 10px;
  }

  .two-col .rgo-label {
    font-size: 9px;
  }

  .details-row .rgo-label {
    font-size: 8px;
  }

  .details-row .rgo-value {
    font-size: 11px;
  }

  .rgo-description {
    min-height: 470px;
    font-size: 11px;
  }
}
