:root {
  --navy: #102b46;
  --navy-dark: #0a1e33;
  --teal: #0f766e;
  --teal-light: #e6f4f3;
  --blue: #176b87;
  --ink: #172b3a;
  --muted: #5b6e7f;
  --line: #d6e1e8;
  --bg: #f3f7fa;
  --white: #ffffff;
  --red: #b42318;
  --red-bg: #fff0ef;
  --red-line: #f0c4c0;
  --amber: #8a5a00;
  --amber-bg: #fff8e6;
  --amber-line: #f1d48a;
  --green: #166534;
  --green-bg: #eaf7ef;
  --green-line: #bfe3cb;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

.app-header {
  background: var(--navy);
  color: var(--white);
  border-bottom: 4px solid var(--teal);
  padding: 16px 20px;
}

.header-inner {
  max-width: 1320px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  background: var(--teal);
  color: var(--white);
  display: grid;
  place-items: center;
  font-size: 21px;
  font-weight: 800;
}

.app-header h1 {
  margin: 0;
  font-size: 21px;
  line-height: 1.2;
}

.app-header p {
  margin: 3px 0 0;
  font-size: 13px;
  color: #cfe3ec;
}

.tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tab {
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.08);
  color: #e4f0f5;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.tab.active {
  background: var(--white);
  color: var(--navy);
  border-color: var(--white);
}

.container {
  max-width: 1320px;
  margin: 18px auto 40px;
  padding: 0 18px;
}

.safety-banner {
  background: var(--amber-bg);
  border: 1px solid var(--amber-line);
  border-left: 5px solid #d69e19;
  border-radius: 10px;
  padding: 12px 16px;
  margin-bottom: 16px;
  font-size: 13px;
  color: #5c3d00;
}

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

.card {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: 0 1px 4px rgba(16, 43, 70, 0.05);
}

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

h2 {
  font-size: 16px;
  margin: 0 0 12px;
  color: var(--navy);
}

.card-head h2 {
  margin-bottom: 0;
}

h3 {
  font-size: 14px;
  margin: 18px 0 8px;
  color: var(--navy);
}

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

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

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

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

label,
.field-label {
  font-size: 12.5px;
  font-weight: 700;
  color: #364e61;
}

input,
select,
textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid #c3d2dc;
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--white);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
}

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

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid #8ad0cc;
  border-color: var(--teal);
}

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

.seg-option {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 8px 10px;
  border: 1px solid #c3d2dc;
  border-radius: 8px;
  background: #f8fbfd;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  color: var(--navy);
}

.seg-option input {
  width: auto;
  min-height: auto;
  margin: 0;
  accent-color: var(--teal);
}

.seg-option:has(input:checked) {
  background: var(--teal-light);
  border-color: var(--teal);
  color: var(--teal);
}

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

.check {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
}

.check input {
  width: 18px;
  height: 18px;
  min-height: 18px;
  margin-top: 2px;
  accent-color: var(--teal);
}

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

button,
.filebtn {
  border: 0;
  border-radius: 8px;
  padding: 11px 16px;
  min-height: 44px;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button:disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

.primary-navy {
  background: var(--navy);
  color: var(--white);
}

.primary-teal {
  background: var(--teal);
  color: var(--white);
}

.secondary {
  background: #e9f0f4;
  color: var(--navy);
}

.danger {
  background: var(--red-bg);
  color: var(--red);
  border: 1px solid var(--red-line);
}

.small-btn {
  min-height: 34px;
  padding: 6px 11px;
  font-size: 12.5px;
}

.link-btn {
  background: transparent;
  color: var(--teal);
  padding: 0;
  min-height: auto;
  font-size: 12.5px;
  text-decoration: underline;
}

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

.empty-state {
  background: #f7fafc;
  border: 1px dashed #c6d5df;
  border-radius: 10px;
  padding: 16px;
  color: var(--muted);
  font-size: 14px;
}

.provenance {
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12.5px;
  margin-bottom: 12px;
}

.provenance.calculated {
  background: #eef4f8;
  border: 1px solid #c9dce7;
  color: var(--navy);
}

.provenance.verified {
  background: var(--green-bg);
  border: 1px solid var(--green-line);
  color: var(--green);
}

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

.metric {
  background: #f8fbfd;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
}

.metric.highlight {
  background: var(--teal-light);
  border-color: #9ed4cf;
}

.metric strong {
  display: block;
  font-size: 22px;
  line-height: 1.2;
  color: var(--navy);
  margin: 4px 0 3px;
}

.metric small {
  display: block;
  font-size: 12px;
  color: var(--muted);
}

.alerts-stack {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.alert {
  padding: 10px 13px;
  border-radius: 8px;
  font-size: 13px;
  margin: 8px 0;
}

.alerts-stack .alert {
  margin: 0;
}

.alert ul {
  margin: 6px 0 0 18px;
  padding: 0;
}

.alert.stop {
  background: var(--red-bg);
  color: #8f1d14;
  border: 1px solid var(--red-line);
}

.alert.warn {
  background: var(--amber-bg);
  color: var(--amber);
  border: 1px solid var(--amber-line);
}

.alert.ok {
  background: var(--green-bg);
  color: var(--green);
  border: 1px solid var(--green-line);
}

.alert.info {
  background: #edf6f8;
  color: #135368;
  border: 1px solid #c3e0e8;
}

.alert-tag {
  display: inline-block;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.04em;
  padding: 2px 6px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.08);
  margin-right: 4px;
}

/* PRESCRIPTION SHEET PREVIEW */
.rx-sheet {
  margin-top: 14px;
  border: 1px solid #b8c9d4;
  border-radius: 10px;
  padding: 20px 22px;
  background: var(--white);
}

.rx-sheet.stale {
  opacity: 0.55;
}

.rx-sheet-bar {
  height: 6px;
  background: var(--navy);
  border-radius: 3px;
  margin-bottom: 12px;
}

.rx-sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  padding-bottom: 10px;
  border-bottom: 2px solid var(--teal);
}

.rx-sheet-title {
  font-size: 16px;
  font-weight: 800;
  color: var(--navy);
  letter-spacing: 0.02em;
}

.rx-sheet-sub {
  font-size: 12px;
  color: var(--teal);
  font-weight: 600;
}

.rx-sheet-date {
  font-size: 13px;
  color: var(--ink);
}

.rx-patient-box {
  margin-top: 12px;
  padding: 10px 12px;
  background: #f5f9fb;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 13.5px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.rx-patient-box span {
  color: var(--muted);
  font-size: 12.5px;
}

.rx-patient-meta {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
}

.rx-body {
  margin-top: 14px;
}

.rx-symbol {
  font-size: 30px;
  font-weight: 800;
  font-family: Georgia, "Times New Roman", serif;
  color: var(--navy);
  line-height: 1;
  margin-bottom: 10px;
}

.rx-med-grid {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 14px;
}

.rx-med-grid span {
  color: var(--muted);
  display: inline-block;
  min-width: 124px;
  font-size: 13px;
}

.rx-sig-box {
  margin-top: 12px;
  padding: 11px 13px;
  border: 1px solid var(--teal);
  background: #f7fcfb;
  border-radius: 8px;
}

.rx-sig-heading {
  font-weight: 800;
  color: var(--navy);
  font-size: 13px;
  margin-bottom: 4px;
}

.rx-sig-box p {
  margin: 0;
  font-size: 14.5px;
  white-space: pre-wrap;
}

.rx-quantity-row {
  margin-top: 12px;
  font-size: 14.5px;
  display: flex;
  gap: 10px;
  align-items: baseline;
}

.rx-quantity-row strong {
  color: var(--teal);
  font-size: 16px;
}

.rx-ref-row {
  margin-top: 8px;
  font-size: 12px;
  color: var(--muted);
}

.rx-prescriber-block {
  margin-top: 22px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 13.5px;
}

.rx-ptr {
  margin-left: 12px;
  color: var(--muted);
  font-size: 12.5px;
}

.rx-signature-line {
  min-width: 210px;
  text-align: center;
}

.rx-signature-line .line {
  border-bottom: 1px solid var(--ink);
  height: 24px;
  margin-bottom: 4px;
}

.rx-signature-line span {
  font-size: 11px;
  color: var(--muted);
}

.rx-sheet-footer {
  margin-top: 16px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
  font-size: 11px;
  color: var(--muted);
}

/* TABLES & GOVERNANCE */
.tablewrap {
  overflow-x: auto;
  margin-top: 16px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  text-align: left;
  padding: 10px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}

th {
  color: #496073;
  background: #f7fafc;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.table-actions {
  white-space: nowrap;
}

.pill {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 20px;
  font-size: 10.5px;
  font-weight: 800;
  background: var(--amber-bg);
  color: var(--amber);
}

.pill.approved {
  background: var(--green-bg);
  color: var(--green);
}

.note {
  font-size: 13px;
  color: var(--muted);
}

.ref-links li {
  margin-bottom: 6px;
}

.ref-links a {
  color: var(--blue);
  font-weight: 600;
}

.app-footer {
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  margin: 26px 0 10px;
}

.hidden {
  display: none !important;
}

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

@media (max-width: 600px) {
  .form-grid,
  .metrics-grid,
  .segmented {
    grid-template-columns: 1fr;
  }
  .field.full {
    grid-column: auto;
  }
}

/* PRINT-FRIENDLY A4 PRESCRIPTION LAYOUT */
@media print {
  @page {
    size: A4 portrait;
    margin: 14mm;
  }

  body {
    background: #ffffff;
    color: #000000;
  }

  .app-header,
  .safety-banner,
  .calc-column,
  #rxEditorControls,
  #rxValidationBox,
  #rxActionBar,
  #catalog,
  #sources,
  .app-footer {
    display: none !important;
  }

  .container {
    max-width: none;
    margin: 0;
    padding: 0;
  }

  .workspace-grid {
    display: block;
  }

  .rx-card {
    border: 0;
    box-shadow: none;
    padding: 0;
    margin: 0;
  }

  .rx-sheet {
    display: block !important;
    border: 1px solid #cbd5e1;
    border-radius: 0;
    margin: 0;
    padding: 18px 22px;
    min-height: 255mm;
    position: relative;
  }

  .rx-sheet-bar {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}