:root {
  --cw-accent: #0d625c;
  --cw-accent-dark: #0a4d48;
  --cw-ink: #17201e;
  --cw-muted: #5e6b67;
  --cw-line: #ced8d4;
  --cw-line-strong: #b8c7c2;
  --cw-soft: #edf5f2;
  --cw-soft-strong: #e2efeb;
  --cw-surface: #f5f7f6;
  --cw-paper: #ffffff;
  --cw-warning: #8a5a12;
  --cw-warning-soft: #fff7e6;
  --cw-danger: #9a3535;
}

* { box-sizing: border-box; }

body.cw-r2 {
  margin: 0;
  background: var(--cw-surface);
  color: var(--cw-ink);
  font: 14px/1.38 system-ui, -apple-system, "Segoe UI", sans-serif;
}

.cw-r2 button,
.cw-r2 input,
.cw-r2 textarea,
.cw-r2 select { font: inherit; }

.cw-r2 button {
  min-height: 44px;
  border: 1px solid var(--cw-accent);
  border-radius: 6px;
  background: var(--cw-accent);
  color: #fff;
  padding: 7px 11px;
  cursor: pointer;
  font-weight: 700;
  white-space: nowrap;
}

.cw-r2 button:hover { background: var(--cw-accent-dark); }
.cw-r2 button.secondary { border-color: var(--cw-line-strong); background: #eef2ef; color: #26332f; }
.cw-r2 button.secondary:hover { border-color: #9eafa9; background: #e3e9e6; }
.cw-r2 button.danger { border-color: #c58d8d; background: #fff; color: var(--cw-danger); }
.cw-r2 button:disabled { cursor: not-allowed; border-color: #ced7d3; background: #e5eae7; color: #71807b; }
.cw-r2 :focus-visible { outline: 3px solid #e1a43e; outline-offset: 2px; }

.cw-r2 input,
.cw-r2 textarea,
.cw-r2 select {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--cw-line-strong);
  border-radius: 6px;
  background: #fff;
  color: #17201e;
  padding: 8px 9px;
}

.cw-r2 input,
.cw-r2 select { min-height: 38px; }
.cw-r2 textarea { min-height: 68px; resize: vertical; }
.cw-r2 label { display: grid; gap: 4px; min-width: 0; font-weight: 650; }

.cw-shell { min-height: 100vh; padding: 0 8px 8px; }

.cw-top {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: 150px minmax(880px, 1fr) auto;
  gap: 12px;
  align-items: end;
  margin: 0 -8px;
  padding: 8px;
  border-bottom: 1px solid var(--cw-line);
  background: #fff;
  box-shadow: 0 2px 8px rgb(30 49 43 / 7%);
}

.cw-brand { align-self: center; }
.cw-brand h1 { margin: 0; font-size: 20px; line-height: 1.08; }
.cw-brand p { margin: 5px 0 0; color: var(--cw-muted); font-size: 11.5px; line-height: 1.25; }

.cw-encounter {
  display: grid;
  grid-template-columns: minmax(138px, 1.05fr) 115px 185px 125px minmax(135px, .85fr) 148px 108px minmax(230px, 1.5fr);
  gap: 7px;
  align-items: end;
  min-width: 0;
}

.cw-encounter label { gap: 3px; font-size: 11.5px; }
.cw-date-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 5px; }
.cw-date-row button { min-height: 44px; padding-inline: 9px; }
.cw-actions { display: flex; gap: 7px; align-items: end; justify-content: flex-end; }

.cw-account {
  position: fixed;
  z-index: 40;
  top: 74px;
  right: 14px;
  width: min(920px, calc(100vw - 28px));
  max-height: calc(100vh - 90px);
  overflow: auto;
  padding: 14px;
  border: 1px solid var(--cw-line-strong);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 18px 50px rgb(20 39 34 / 22%);
}

.cw-account-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 14px; }
.cw-account-head p { margin: 5px 0 0; }
.cw-login { display: grid; grid-template-columns: minmax(210px, 1fr) minmax(210px, 1fr) auto; gap: 9px; align-items: end; margin-top: 12px; }
.cw-login .cw-shared-login { grid-column: 1 / -1; display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: 8px; }
.cw-login .cw-shared-login input { width: 16px; min-height: 16px; height: 16px; }
.cw-registration { margin-top: 10px; padding-top: 8px; border-top: 1px solid #d9e4e0; }
.cw-registration .cw-login { grid-template-columns: repeat(3, minmax(180px, 1fr)) auto; }
.cw-register-aggregate { grid-column: 1 / -1; display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 8px; align-items: start; }
.cw-issued { margin-top: 10px; padding: 10px; border: 1px solid #a9c3ba; border-radius: 5px; background: #edf6f2; }
.cw-issued code { user-select: all; font-size: 14px; }
.cw-login label { font-size: 12px; }
.cw-badge { display: inline-block; border-radius: 999px; padding: 4px 8px; background: var(--cw-soft-strong); color: var(--cw-accent-dark); font-size: 11px; font-weight: 800; }
.cw-badge.off { background: #f5e5e3; color: #7f3030; }
.cw-boundary { margin: 12px 0 0; padding: 9px 11px; border: 1px solid #e7cf91; border-radius: 6px; background: var(--cw-warning-soft); color: #4c3a19; font-size: 12px; }

.cw-tabs { display: none; gap: 7px; margin: 8px 0; }
.cw-tabs button[aria-selected="true"] { background: var(--cw-accent); color: #fff; }
.cw-tabs button[aria-selected="false"] { background: #fff; color: var(--cw-accent); }

.cw-workspace {
  display: grid;
  grid-template-columns: minmax(620px, 54%) minmax(520px, 46%);
  gap: 12px;
  align-items: start;
  width: 100%;
  max-width: none;
  margin-top: 10px;
  padding: 0;
}

.cw-panel {
  min-width: 0;
  border: 1px solid var(--cw-line);
  border-radius: 8px;
  background: #fff;
}

.cw-panel-head {
  position: sticky;
  top: 0;
  z-index: 8;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  padding: 13px 14px 11px;
  border-bottom: 1px solid var(--cw-line);
  background: #fff;
}

.cw-panel-head h2 { margin: 0; font-size: 18px; }
.cw-panel-head p { margin: 4px 0 0; }
.cw-panel-actions { display: flex; gap: 7px; }
.cw-author { max-height: calc(100vh - 84px); overflow: auto; scroll-behavior: smooth; }
.cw-author > .cw-section,
.cw-author > .cw-optional-context { margin-inline: 14px; }

.cw-optional-context { padding: 10px 0 2px; }
.cw-optional-context label { grid-template-columns: auto minmax(180px, 1fr); align-items: center; font-size: 12px; }
.cw-optional-context input { min-height: 34px; padding-block: 6px; }

.cw-template-meta { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; padding: 11px 0 9px; }
.cw-template-meta h3 { margin: 0; font-size: 15px; }
.cw-template-meta p { margin: 3px 0 0; }
.cw-quick-variant { display: flex; align-items: flex-end; gap: 8px; min-width: min(100%, 430px); }
.cw-quick-variant label { flex: 1; min-width: 0; }
.cw-quick-variant select { width: 100%; }
.cw-authored-quick { margin: 2px 0 8px; border: 1px solid #cbdad5; border-radius: 6px; background: #f7faf8; }
.cw-authored-quick > summary { cursor: pointer; padding: 9px 10px; font-size: 12px; font-weight: 750; }
.cw-authored-body { display: grid; gap: 9px; padding: 0 10px 10px; }
.cw-authored-body h3 { margin: 4px 0 7px; font-size: 13px; }
.cw-personal-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
.cw-personal-grid .cw-card { margin: 0; padding: 8px; }
.cw-personal-grid .cw-card p { margin: 5px 0 0; }

.cw-section-nav {
  position: sticky;
  top: 64px;
  z-index: 7;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  padding: 8px 0;
  border-block: 1px solid #dce4e0;
  background: rgb(255 255 255 / 97%);
}

.cw-section-nav button { min-height: 44px; padding: 4px 8px; font-size: 11.5px; }

.cw-pathology-section {
  scroll-margin-top: 116px;
  padding: 15px 0 13px;
  border-top: 1px solid #dce4e0;
}

.cw-pathology-title { display: flex; justify-content: space-between; gap: 12px; align-items: center; margin-bottom: 9px; }
.cw-pathology-title h3 { margin: 0; font-size: 15px; }
.cw-pathology-title span { border: 1px solid #d0dbd7; border-radius: 999px; padding: 2px 7px; color: #75827e; font-size: 10.5px; }
.cw-pathology-actions { display: flex; align-items: center; gap: 5px; }
.cw-icon-button { width: 32px; min-width: 32px; min-height: 32px; padding: 0; font-size: 18px; line-height: 1; }
.cw-custom-control { border-color: #9fc8bc; background: #f1faf6; }
.cw-section-customizer { margin-top: 10px; padding: 10px; border: 1px solid #a9c6bd; border-radius: 6px; background: #f6faf8; }
.cw-custom-form { display: grid; grid-template-columns: minmax(180px, .75fr) minmax(260px, 1.25fr); gap: 8px; align-items: end; }
.cw-custom-form .cw-inline { grid-column: 1 / -1; }
.cw-custom-row { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 7px; align-items: center; padding: 6px 0; border-bottom: 1px solid #dce7e3; }
.cw-custom-row span { min-width: 0; }
.cw-custom-row small { display: block; overflow: hidden; color: var(--cw-muted); text-overflow: ellipsis; white-space: nowrap; }
.cw-control-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
.cw-control { min-width: 0; }
.cw-control-text-long,
.cw-control-multi_choice,
.cw-compound { grid-column: 1 / -1; }
.cw-control textarea { min-height: 48px; }
.cw-control-text-long textarea { min-height: 76px; }

.cw-check {
  display: flex !important;
  min-height: 36px;
  align-items: center;
  gap: 9px !important;
  padding: 7px 9px;
  border: 1px solid #cddbd6;
  border-radius: 6px;
  background: #f7faf8;
  font-size: 12px;
  font-weight: 500 !important;
}

.cw-check:hover { border-color: #9fbbb3; background: var(--cw-soft); }
.cw-check:has(input:checked) { border-color: #78aaa0; background: #e5f3ef; color: #083f3a; font-weight: 700 !important; }
.cw-check input { flex: 0 0 16px; width: 16px; min-height: 16px; height: 16px; margin: 0; padding: 0; accent-color: var(--cw-accent); }
.cw-choice-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; margin-top: 5px; }
.cw-choice-list > strong { grid-column: 1 / -1; font-size: 12px; }
.cw-choice-list.compact { grid-template-columns: repeat(auto-fit, minmax(145px, 1fr)); }

.cw-compound {
  padding: 10px;
  border: 1px solid #cddbd6;
  border-radius: 6px;
  background: #fbfdfc;
}

.cw-compound h4 { margin: 0 0 8px; font-size: 13px; }
.cw-compound-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(145px, 1fr)); gap: 8px; }
.cw-compound-field { font-size: 11.5px; }
.cw-compound-field:has(.cw-choice-list) { grid-column: 1 / -1; }
.cw-compound-choice-group { min-inline-size: 0; margin: 0; padding: 0; border: 0; }
.cw-compound-choice-group legend { padding: 0; font-size: 11.5px; font-weight: 700; }

.cw-section { padding: 12px 0; }
.cw-section > h3 { margin: 0 0 7px; font-size: 15px; }
.cw-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; }
.cw-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.cw-fields { display: grid; gap: 9px; }
.cw-card { margin: 8px 0; padding: 9px; border: 1px solid var(--cw-line); border-radius: 6px; background: #fbfdfc; }
.cw-card p { margin: 5px 0; }
.cw-inline { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.cw-inline label { display: flex; align-items: center; gap: 6px; font-size: 12px; }
.cw-inline input[type="checkbox"] { width: 16px; min-height: 16px; height: 16px; }

.cw-final {
  position: sticky;
  top: 82px;
  display: flex;
  height: calc(100vh - 92px);
  flex-direction: column;
  overflow: hidden;
  padding: 0 14px 10px;
  background: #edf2ef;
}

.cw-final .cw-panel-head { margin: 0 -14px 10px; background: #edf2ef; }
.cw-final-head { position: static; flex: 0 0 auto; flex-wrap: wrap; }
.cw-final-head h2 { font-size: 18px; }
.cw-final-head .cw-status { max-width: 460px; }
.cw-professional { display: grid; grid-template-columns: repeat(3, minmax(105px, 1fr)); gap: 7px; flex: 1 1 350px; max-width: 520px; }
.cw-professional label { gap: 2px; color: var(--cw-muted); font-size: 10.5px; }
.cw-professional input { min-height: 34px; padding: 6px 8px; background: #fff; }
.cw-final textarea {
  flex: 1 1 auto;
  width: 100%;
  min-height: 360px;
  padding: 30px 34px;
  border-color: #d1d7d4;
  border-radius: 4px;
  background: #fff;
  font: 14px/1.52 Georgia, "Times New Roman", serif;
  box-shadow: inset 0 0 0 1px rgb(50 70 63 / 2%);
}

.cw-final-tools { display: flex; flex-wrap: wrap; gap: 7px; justify-content: flex-end; }
.cw-final-note { flex: 0 0 auto; margin: 7px 0 0; }
.cw-recompose { border-color: #b36b19 !important; background: #b36b19 !important; }
.cw-status { min-height: 1.2em; color: var(--cw-muted); font-size: 12px; }
.cw-help { color: var(--cw-muted); font-size: 11.5px; }
.cw-alert { margin: 8px 0; padding: 8px 10px; border-left: 4px solid #d58d1f; background: var(--cw-warning-soft); color: #493615; }

.cw-settings { margin-top: 14px; border-top: 1px solid var(--cw-line); padding-top: 10px; }
.cw-settings summary { cursor: pointer; font-weight: 750; }
.cw-settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; padding-top: 12px; }
.cw-modal { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; padding: 20px; background: rgba(18, 32, 29, .58); }
.cw-modal-card { width: min(900px, 100%); max-height: calc(100vh - 40px); overflow: auto; padding: 18px; border: 1px solid #b8c9c3; border-radius: 6px; background: #fff; box-shadow: 0 18px 60px rgba(15, 31, 27, .24); }
.cw-modal-card textarea { min-height: 300px; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12px; }
.cw-profile-list { max-height: 210px; overflow: auto; border: 1px solid var(--cw-line); border-radius: 6px; padding: 6px; }
.cw-profile-row { display: flex; align-items: center; gap: 7px; padding: 4px; border-bottom: 1px solid #edf2f0; }
.cw-profile-row:last-child { border-bottom: 0; }
.cw-profile-row input { width: 16px; min-height: 16px; height: 16px; }
.cw-order { display: flex; gap: 4px; margin-left: auto; }
.cw-order button { min-height: 30px; padding: 2px 7px; }
.cw-admin { margin-top: 12px; padding: 14px; }
.cw-admin table { width: 100%; border-collapse: collapse; font-size: 12px; }
.cw-admin th,
.cw-admin td { padding: 6px; border-bottom: 1px solid var(--cw-line); text-align: left; }
.cw-hidden { display: none !important; }
.cw-loading { display: grid; min-height: 100vh; place-content: center; text-align: center; }
.cw-loading h1 { margin: 0; }
.cw-shell > footer { width: 100%; max-width: none; margin: 0; padding: 8px 4px 0; }

@media (max-width: 1550px) {
  .cw-top { grid-template-columns: 150px 1fr; align-items: center; }
  .cw-actions { grid-column: 2; grid-row: 1; }
  .cw-encounter { grid-column: 1 / -1; grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .cw-account { top: 210px; }
  .cw-author { max-height: calc(100vh - 216px); }
  .cw-final { top: 212px; height: calc(100vh - 222px); }
}

@media (max-width: 1100px) {
  .cw-workspace { grid-template-columns: minmax(490px, 1fr) minmax(500px, 1fr); }
  .cw-control-grid { grid-template-columns: 1fr; }
  .cw-custom-form { grid-template-columns: 1fr; }
  .cw-custom-form .cw-inline { grid-column: auto; }
  .cw-control-text-long,
  .cw-control-multi_choice,
  .cw-compound { grid-column: auto; }
}

@media (max-width: 1099px) {
  .cw-template-meta { align-items: stretch; flex-direction: column; }
  .cw-quick-variant { min-width: 0; width: 100%; }
  .cw-personal-grid { grid-template-columns: 1fr; }
  .cw-r2 button { min-height: 44px; }
  .cw-shell { padding: 0 8px 8px; }
  .cw-top { position: static; grid-template-columns: 1fr; gap: 9px; margin-inline: -8px; }
  .cw-brand { display: flex; align-items: baseline; gap: 10px; }
  .cw-brand p { margin: 0; }
  .cw-actions { grid-column: auto; grid-row: auto; justify-content: flex-start; overflow-x: auto; }
  .cw-encounter { grid-column: auto; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cw-template-field { grid-column: 1 / -1; }
  .cw-account { top: 12px; right: 10px; width: calc(100vw - 20px); max-height: calc(100vh - 24px); }
  .cw-login { grid-template-columns: 1fr; }
  .cw-registration .cw-login { grid-template-columns: 1fr; }
  .cw-tabs { display: flex; }
  .cw-workspace { display: block; margin-top: 0; }
  .cw-workspace[data-tab="author"] .cw-final,
  .cw-workspace[data-tab="final"] .cw-author { display: none; }
  .cw-author { max-height: none; overflow: visible; }
  .cw-final { position: static; height: calc(100vh - 78px); min-height: 620px; }
  .cw-panel-head { position: static; }
  .cw-section-nav { top: 0; }
  .cw-grid,
  .cw-grid.three,
  .cw-choice-list,
  .cw-settings-grid { grid-template-columns: 1fr; }
  .cw-control-grid { grid-template-columns: 1fr; }
  .cw-final textarea { min-height: 48vh; padding: 22px 18px; }
}

@media (max-width: 560px) {
  .cw-modal { padding: 8px; }
  .cw-modal-card { max-height: calc(100vh - 16px); padding: 12px; }
  .cw-modal-card textarea { min-height: 220px; }
  .cw-encounter { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cw-encounter > label:first-child,
  .cw-encounter > .cw-template-field,
  .cw-encounter > #locationWrap { grid-column: 1 / -1; }
  .cw-template-field { grid-column: auto; }
  .cw-date-row { grid-template-columns: minmax(0, 1fr); }
  .cw-date-row button { display: none; }
  .cw-date-row input { min-width: 0; font-size: 11px; }
  .cw-actions button { flex: 0 0 auto; }
  .cw-optional-context label { grid-template-columns: 1fr; }
  .cw-panel-head,
  .cw-final-head { display: block; }
  .cw-panel-actions,
  .cw-final-tools { margin-top: 8px; justify-content: flex-start; }
  .cw-professional { grid-template-columns: 1fr; margin-top: 8px; max-width: none; }
  .cw-section-nav { flex-wrap: nowrap; overflow-x: auto; }
}

@media print {
  .cw-r2 .cw-top,
  .cw-r2 .cw-account,
  .cw-r2 .cw-tabs,
  .cw-r2 .cw-author,
  .cw-r2 .cw-final-head,
  .cw-r2 .cw-final-note,
  .cw-r2 .cw-status,
  .cw-r2 .cw-help,
  .cw-r2 .cw-settings,
  .cw-r2 .cw-admin,
  .cw-r2 footer { display: none !important; }
  .cw-r2 { background: #fff; }
  .cw-shell { padding: 0; }
  .cw-workspace { display: block; margin: 0; }
  .cw-final { display: block; height: auto; overflow: visible; border: 0; background: #fff; padding: 0; }
  .cw-final textarea { height: auto; min-height: 0; overflow: visible; border: 0; padding: 0; box-shadow: none; white-space: pre-wrap; }
}
