:root {
  color-scheme: light;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 18px;
  line-height: 1.55;
  color: #152235;
  background: #eef4f8;
  --navy: #12355b;
  --navy-strong: #0a2747;
  --teal: #087f7a;
  --teal-dark: #05625e;
  --surface: #ffffff;
  --surface-soft: #f4f8fb;
  --border: #b8c9d6;
  --muted: #53677a;
  --danger: #a61b2b;
  --focus: #f2b705;
  --shadow: 0 8px 24px rgba(18, 53, 91, 0.09);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background: var(--surface-soft);
}

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

button, select, input, textarea { min-height: 48px; }

button {
  border-radius: 0.65rem;
  border: 2px solid transparent;
  padding: 0.65rem 1rem;
  font-weight: 700;
  cursor: pointer;
}

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

:focus-visible {
  outline: 4px solid var(--focus);
  outline-offset: 3px;
}

.skip-link {
  position: fixed;
  top: 0.5rem;
  left: 0.5rem;
  z-index: 100;
  padding: 0.7rem 1rem;
  background: #fff;
  color: var(--navy-strong);
  transform: translateY(-150%);
}

.skip-link:focus { transform: none; }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.5rem;
  padding: 1rem max(1rem, calc((100% - 1120px) / 2));
  color: #fff;
  background: var(--navy);
}

.brand { display: flex; align-items: center; gap: 0.8rem; min-width: 0; }
.brand > div { min-width: 0; }
.brand img { flex: 0 0 auto; border-radius: 0.7rem; }
.brand strong { display: block; font-size: 1.35rem; line-height: 1.2; }
.brand span { display: block; font-size: 0.9rem; color: #dceaf5; }

.language-picker { display: grid; gap: 0.2rem; font-weight: 700; }
.language-picker span { font-size: 0.82rem; }
.language-picker select {
  border: 2px solid #d7e6f1;
  border-radius: 0.55rem;
  padding: 0.45rem 2.2rem 0.45rem 0.65rem;
  color: var(--navy-strong);
  background: #fff;
}

.main-nav {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  max-width: 1120px;
  margin: 0 auto;
  padding: 0.7rem 1rem;
  gap: 0.55rem;
  background: rgba(244, 248, 251, 0.97);
  border-bottom: 1px solid var(--border);
}

.main-nav button { color: var(--navy-strong); background: #fff; border-color: var(--border); }
.main-nav button.active { color: #fff; background: var(--navy); border-color: var(--navy); }

main { max-width: 1120px; margin: 0 auto; padding: 1.4rem 1rem 4rem; }
.view { display: grid; gap: 1.25rem; }
h1, h2 { line-height: 1.2; color: var(--navy-strong); }
h1 { margin: 0; font-size: clamp(1.65rem, 5vw, 2.25rem); }
h2 { margin: 0 0 0.8rem; font-size: clamp(1.25rem, 3vw, 1.55rem); }
p { margin: 0.45rem 0; }

.section-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
}

.section-heading p { max-width: 760px; color: var(--muted); }
.count-badge { white-space: nowrap; font-weight: 800; color: var(--navy); }

.selection-list, .management-list, .history-list { display: grid; gap: 0.8rem; }
.order-compose { display: grid; gap: 1.25rem; }

.selection-card {
  display: grid;
  grid-template-columns: 30px 1fr;
  gap: 0.85rem;
  align-items: start;
  padding: 1rem;
  border: 2px solid var(--border);
  border-radius: 0.9rem;
  background: var(--surface);
  box-shadow: 0 3px 12px rgba(18, 53, 91, 0.04);
  cursor: pointer;
}

.selection-card:hover { border-color: var(--teal); }
.selection-card:has(input:checked) { border-color: var(--teal); background: #eaf8f6; }
.selection-card input { width: 28px; height: 28px; margin: 0.1rem 0 0; accent-color: var(--teal); }
.item-title { display: block; overflow-wrap: anywhere; font-weight: 800; font-size: 1.08rem; }
.item-purpose { display: block; overflow-wrap: anywhere; color: var(--muted); margin-top: 0.2rem; }

.management-card, .history-card, .panel, .preview-card, .empty-state {
  padding: 1.15rem;
  border: 1px solid var(--border);
  border-radius: 0.9rem;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.management-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 1rem;
}

.management-card .button-row { justify-content: flex-end; }
.section-actions { justify-content: flex-end; }

.field { display: grid; gap: 0.35rem; font-weight: 700; }
.field small { color: var(--muted); font-weight: 500; }
.field input, .field select, .field textarea {
  width: 100%;
  border: 2px solid #8fa6b8;
  border-radius: 0.65rem;
  padding: 0.7rem 0.8rem;
  color: #102033;
  background: #fff;
}

.field textarea { resize: vertical; min-height: 5.5rem; }
#preview-body { min-height: 18rem; line-height: 1.5; background: #f9fbfc; }

.form-stack { display: grid; gap: 1rem; }
.form-stack > .primary { justify-self: start; min-width: 10rem; }

.mail-service-choice {
  display: grid;
  gap: 0.65rem;
  margin: 0.4rem 0;
  padding: 1rem;
  border: 2px solid var(--border);
  border-radius: 0.8rem;
  background: var(--surface-soft);
}
.mail-service-choice legend {
  padding: 0 0.35rem;
  color: var(--navy-strong);
  font-size: 1.08rem;
  font-weight: 800;
}
.mail-service-choice p { color: var(--muted); }

.preview-meta { display: grid; gap: 0.75rem; margin: 0 0 1rem; }
.preview-meta div { display: grid; grid-template-columns: minmax(7rem, 22%) 1fr; gap: 0.5rem; }
.preview-meta dt { font-weight: 800; }
.preview-meta dd { margin: 0; overflow-wrap: anywhere; }

.button-row { display: flex; flex-wrap: wrap; gap: 0.65rem; align-items: center; }
.primary { color: #fff; background: var(--teal); border-color: var(--teal); }
.primary:hover { background: var(--teal-dark); border-color: var(--teal-dark); }
.secondary { color: var(--navy-strong); background: #fff; border-color: var(--navy); }
.secondary:hover { background: #e9f0f6; }
.danger-secondary { color: var(--danger); background: #fff; border-color: var(--danger); }
.danger-secondary:hover { background: #fff0f2; }
.text-button { color: var(--navy); background: transparent; border-color: var(--border); }

.empty-state { text-align: center; padding: 1.35rem 1rem; box-shadow: none; }
.empty-state p { margin-bottom: 1rem; }
.start-state { border-style: dashed; }
.local-note { color: var(--teal-dark); font-weight: 700; }
.open-help { color: var(--muted); }
.muted, .version { color: var(--muted); }
.readiness {
  margin: 0 0 1rem;
  padding: 0.75rem 0.85rem;
  border-left: 5px solid var(--focus);
  background: #fff8d8;
  font-weight: 700;
}
.readiness.ready { border-left-color: var(--teal); background: #eaf8f6; color: var(--teal-dark); }
.compact-action { padding-inline: 0.75rem; }
.import-summary { margin: 0; padding-left: 1.4rem; }
.import-summary li + li { margin-top: 0.45rem; }
.warning-note {
  padding: 0.75rem 0.85rem;
  border-left: 5px solid var(--danger);
  background: #fff0f2;
  color: #721321;
  font-weight: 700;
}

.status {
  max-width: 1088px;
  margin: 0.8rem auto 0;
  padding: 0.8rem 1rem;
  border: 2px solid var(--teal);
  border-radius: 0.7rem;
  background: #eaf8f6;
  font-weight: 700;
}

.status.error { border-color: var(--danger); background: #fff0f2; color: #721321; }

.update-notice {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  max-width: 1088px;
  margin: 0.8rem auto 0;
  padding: 0.9rem 1rem;
  border: 2px solid var(--teal);
  border-radius: 0.7rem;
  background: #eaf8f6;
  box-shadow: var(--shadow);
}
.update-notice p { margin: 0; font-weight: 700; }
.update-notice button { flex: 0 0 auto; }

.history-card summary { cursor: pointer; font-weight: 800; }
.history-summary { display: grid; grid-template-columns: 1fr auto; gap: 0.7rem; align-items: center; }
.history-card ul { margin-bottom: 0; }
.history-meta {
  display: grid;
  gap: 0.45rem;
  margin: 1rem 0;
}
.history-meta div {
  display: grid;
  grid-template-columns: minmax(7rem, 22%) 1fr;
  gap: 0.6rem;
}
.history-meta dt { font-weight: 800; }
.history-meta dd { margin: 0; overflow-wrap: anywhere; }
.history-detail-heading { margin: 1rem 0 0.45rem; font-size: 1rem; }
.history-card pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  padding: 0.8rem;
  border-radius: 0.6rem;
  background: var(--surface-soft);
  font: inherit;
}

dialog {
  width: min(640px, calc(100% - 2rem));
  max-height: calc(100vh - 2rem);
  max-height: calc(100dvh - 2rem);
  overflow-y: auto;
  overscroll-behavior: contain;
  border: 0;
  border-radius: 1rem;
  padding: 1.3rem;
  color: inherit;
  box-shadow: 0 24px 80px rgba(5, 25, 48, 0.3);
}

dialog::backdrop { background: rgba(4, 20, 38, 0.62); }

@media (min-width: 760px) {
  .selection-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .preview-meta { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 720px) {
  :root { font-size: 18px; }
  .app-header { align-items: center; flex-flow: row wrap; gap: 0.6rem; padding: 0.65rem 0.8rem; }
  .brand { flex: 1 1 9rem; gap: 0.55rem; }
  .brand img { width: 36px; height: 36px; border-radius: 0.5rem; }
  .brand strong { font-size: 1.2rem; }
  .brand .tagline { display: none; }
  .language-picker { display: block; flex: 1 1 7.5rem; margin-left: auto; }
  .language-picker > span {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }
  .language-picker select { width: 100%; min-height: 44px; padding-block: 0.3rem; }
  .main-nav { grid-template-columns: repeat(2, minmax(0, 1fr)); position: static; gap: 0.4rem; padding: 0.55rem 0.8rem; }
  .main-nav button { min-height: 48px; padding: 0.45rem 0.35rem; font-size: 0.94rem; border-width: 1px; }
  .section-heading { align-items: stretch; flex-direction: column; }
  .section-heading > button { width: 100%; }
  .section-actions { width: 100%; }
  .section-actions > button { flex: 1 1 13rem; }
  .management-card { grid-template-columns: 1fr; }
  .management-card .button-row { justify-content: stretch; }
  .management-card .button-row button { flex: 1; }
  .button-row > button { flex: 1 1 13rem; }
  .preview-card .button-row > button { flex-basis: 100%; }
  .update-notice { align-items: stretch; flex-direction: column; margin-inline: 0.8rem; }
  .update-notice button { width: 100%; }
  .form-stack > .primary { width: 100%; }
  .preview-meta div { grid-template-columns: 1fr; }
  .history-meta div { grid-template-columns: 1fr; gap: 0.1rem; }
  h1 { font-size: 1.55rem; }
  main { padding: 0.9rem 0.8rem 2.5rem; }
  .view { gap: 1rem; }
}

@media print {
  :root { font-size: 12pt; }
  body { background: #fff; }
  .app-header, .main-nav, #status, #view-order, #view-history, #view-settings,
  #add-medication, #print-medications, .management-card .button-row,
  #view-medications .section-heading p { display: none !important; }
  main { max-width: none; margin: 0; padding: 0; }
  #view-medications { display: grid !important; gap: 0.5rem; }
  #view-medications .section-heading { display: block; }
  .management-list { gap: 0; }
  .management-card {
    display: block;
    break-inside: avoid;
    padding: 0.55rem 0;
    border: 0;
    border-bottom: 1px solid #777;
    border-radius: 0;
    box-shadow: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}

@media (forced-colors: active) {
  .selection-card:has(input:checked) { border-width: 4px; }
}
