:root {
  color-scheme: light;
  --bg: #f3f6f4;
  --ink: #17251c;
  --muted: #657168;
  --line: #d9e1db;
  --accent: #1f7a4d;
  --accent-dark: #155b39;
  --accent-soft: #eaf4ee;
  --blue: #176b87;
  --warn: #b42318;
  --panel: #ffffff;
  --sidebar: #173d2b;
  --sidebar-muted: #b9c9bf;
}

* { box-sizing: border-box; }

html { min-width: 320px; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  min-height: 100vh;
}

body.authenticated .app-shell { grid-template-columns: 248px minmax(0, 1fr); }

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 26px 18px 20px;
  background: var(--sidebar);
  color: #fff;
}

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

.brand { padding: 0 8px 24px; }

.brand-mark {
  flex: 0 0 40px;
  width: 40px;
  height: 40px;
  display: block;
  object-fit: contain;
}

.brand strong,
.brand small { display: block; }
.brand strong { font-size: 18px; }
.brand small { margin-top: 3px; color: var(--sidebar-muted); font-size: 12px; }

.main-nav {
  display: grid;
  gap: 6px;
}

.nav-item {
  min-height: 62px;
  display: grid;
  justify-items: start;
  align-content: center;
  gap: 3px;
  padding: 10px 14px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: #edf6f0;
  cursor: pointer;
  text-align: left;
}

.nav-item:hover { background: rgba(255, 255, 255, 0.08); }
.nav-item.active { background: #fff; color: var(--accent-dark); }
.nav-item:disabled { opacity: 0.45; cursor: not-allowed; }
.nav-item span { font-size: 15px; font-weight: 700; }
.nav-item small { color: var(--sidebar-muted); font-size: 12px; }
.nav-item.active small { color: var(--muted); }

.sidebar-footer {
  margin-top: auto;
  padding-top: 18px;
  border-top: 1px solid rgba(255, 255, 255, 0.14);
}

.logout-button {
  width: 100%;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.28);
}

.app {
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
  padding: 32px 38px 54px;
}

.topbar,
.list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.topbar {
  min-height: 68px;
  margin-bottom: 22px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}

.list-head { margin-bottom: 18px; }
.list-head h2 { margin-bottom: 0; }

#history > .list-head,
#aiConfig > .list-head,
#summary > .list-head,
#downloads > .list-head { display: none; }

.head-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 13px;
}

.batch-label {
  margin: 0;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--muted);
  font-size: 13px;
}

h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 27px; letter-spacing: 0; }
h2 { margin-bottom: 14px; font-size: 20px; letter-spacing: 0; }
h3 { letter-spacing: 0; }

.panel,
.rx-card,
.history-item {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.panel { padding: 24px; margin-bottom: 18px; }

.login-panel {
  width: min(430px, calc(100vw - 32px));
  margin: max(8vh, 54px) auto 0;
  padding: 30px;
}

.login-brand {
  margin-bottom: 30px;
  padding-bottom: 22px;
  border-bottom: 1px solid var(--line);
}

.login-brand h1 { font-size: 23px; }
.login-brand p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.login-panel form { display: grid; gap: 5px; }

.upload-panel { max-width: 980px; }

.upload-box {
  display: grid;
  place-items: center;
  gap: 10px;
  min-height: 250px;
  padding: 30px;
  border: 1px dashed #8a9b8c;
  border-radius: 8px;
  background: #fbfcfb;
  text-align: center;
  cursor: pointer;
  transition: background-color 0.16s ease, border-color 0.16s ease, box-shadow 0.16s ease;
}

.upload-box:hover,
.upload-box.dragging {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px rgba(31, 122, 77, 0.2);
}

.upload-box input { display: none; }
.upload-box span { color: var(--accent); font-size: 22px; font-weight: 750; }
.upload-box small { color: var(--muted); }

button,
.button {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 16px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 650;
  text-decoration: none;
  cursor: pointer;
}

button:hover,
.button:hover { background: var(--accent-dark); }
button:disabled { opacity: 0.55; cursor: wait; }
button.secondary { background: var(--blue); }
button.danger { background: var(--warn); }

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

.actions > button,
.actions > .button { min-width: 150px; }
.mobile-shortcuts { display: none; }
.mobile-return { display: none; }

.steps {
  display: grid;
  grid-template-columns: repeat(4, minmax(130px, 1fr));
  gap: 10px;
  margin: 20px 0;
  padding: 0;
  list-style-position: inside;
}

.steps li {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fafcfb;
}

.steps span { color: var(--accent); font-weight: 700; }

.metrics {
  display: grid;
  grid-template-columns: repeat(2, minmax(180px, 280px));
  gap: 12px;
}

.metrics div {
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fafbf9;
}

.metrics strong { display: block; font-size: 30px; }
.metrics span { color: var(--muted); }

.card-list,
.history-list { display: grid; gap: 14px; }

.rx-card {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 13px 16px;
  padding: 20px;
}

.rx-card > .card-title,
.rx-card > .field-wide { grid-column: 1 / -1; }

.card-title {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.card-title h3 { margin-bottom: 0; font-size: 18px; }
.card-title span { color: var(--muted); font-size: 13px; }

label {
  display: grid;
  gap: 6px;
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

input,
textarea,
select {
  width: 100%;
  padding: 10px 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 15px;
}

input:focus,
textarea:focus,
select:focus {
  outline: 2px solid rgba(31, 122, 77, 0.16);
  border-color: var(--accent);
}

textarea { min-height: 84px; resize: vertical; }
textarea[name="prescription_text"] { min-height: 112px; }

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

.config-grid .field-wide { grid-column: 1 / -1; }
.check-group { display: flex; align-items: center; gap: 20px; padding-top: 20px; }
.check-row { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.check-row input { width: 18px; height: 18px; }

.problem textarea[name="error_notes"] { border-color: var(--warn); }

.history-item { padding: 18px 20px; }
.history-item h3 { margin-bottom: 7px; font-size: 17px; }
.history-item p { margin-bottom: 0; color: var(--muted); }

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

.history-list { max-height: calc(100vh - 160px); overflow: auto; padding-right: 4px; }
.history-list .history-item { padding: 15px; cursor: default; }
.history-list .history-item.selected { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.history-list .history-item p { font-size: 13px; line-height: 1.55; }
.history-list .actions { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; margin-top: 12px; }
.history-list .actions > button,
.history-list .actions > .button { min-width: 0; padding-inline: 9px; }

.preview-pane {
  position: sticky;
  top: 24px;
  min-width: 0;
  min-height: 560px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  overflow: hidden;
}

.preview-empty {
  min-height: 560px;
  display: grid;
  place-content: center;
  gap: 6px;
  padding: 30px;
  color: var(--muted);
  text-align: center;
}

.preview-empty p { margin: 0; font-size: 13px; }

.preview-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
}

.preview-head h2 { max-width: 520px; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preview-downloads { display: flex; gap: 8px; }
.preview-downloads .button { min-height: 36px; padding-inline: 12px; }

.preview-tabs {
  display: flex;
  gap: 2px;
  padding: 10px 20px 0;
  border-bottom: 1px solid var(--line);
  background: #fafcfb;
}

.preview-tab {
  min-height: 38px;
  border-radius: 5px 5px 0 0;
  background: transparent;
  color: var(--muted);
}

.preview-tab:hover { background: var(--accent-soft); color: var(--accent-dark); }
.preview-tab.active { background: #fff; color: var(--accent-dark); box-shadow: 0 -2px 0 var(--accent) inset; }

.word-preview,
.excel-preview {
  height: calc(100vh - 280px);
  min-height: 430px;
  overflow: auto;
}

.word-preview {
  width: min(760px, calc(100% - 42px));
  margin: 22px auto;
  padding: 34px 42px 54px;
  background: #fff;
  box-shadow: 0 2px 16px rgba(23, 61, 43, 0.1);
}

.word-preview > h3 { margin-bottom: 24px; text-align: center; }
.word-preview article { padding: 14px 0; border-bottom: 1px solid #edf0ed; }
.word-preview article:last-child { border-bottom: 0; }
.word-prescription { display: grid; gap: 3px; margin: 9px 0 0; line-height: 1.85; font-family: "Songti SC", SimSun, serif; }
.word-prescription-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); column-gap: 12px; }
.word-prescription-row span { min-width: 0; overflow-wrap: anywhere; }

.excel-preview { padding: 16px; }
.excel-preview table { width: max-content; min-width: 100%; border-collapse: separate; border-spacing: 0; font-size: 13px; }
.excel-preview th,
.excel-preview td { max-width: 300px; padding: 9px 11px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); vertical-align: top; white-space: pre-wrap; }
.excel-preview thead { position: sticky; top: 0; z-index: 5; }
.excel-preview th { position: static; background: #e7f0e8; color: var(--accent-dark); font-weight: 700; white-space: nowrap; }
.excel-preview th:first-child,
.excel-preview td:first-child { position: sticky; left: 0; z-index: 1; border-left: 1px solid var(--line); background: #fff; }
.excel-preview th:first-child { position: static; z-index: auto; background: #e7f0e8; }
.excel-preview tr:first-child th { border-top: 1px solid var(--line); }
.excel-preview .excel-filter-row th { padding: 6px; background: #f6faf8; }
.excel-preview .excel-filter-row input { width: 100%; min-width: 88px; height: 30px; padding: 4px 8px; border-color: #cbdad5; border-radius: 4px; background: #fff; font-size: 12px; font-weight: 400; }
.excel-preview .excel-filter-row input::placeholder { color: #91a09d; }

.bulk-actions {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 16px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 -8px 24px rgba(23, 61, 43, 0.08);
  z-index: 5;
}

.bulk-actions p { margin: 0 0 0 auto; text-align: right; }
.save-dialog { width: min(440px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); box-sizing: border-box; padding: 24px; border: 1px solid #d5e2df; border-radius: 8px; color: #243632; background: #fff; }
.save-dialog::backdrop { background: rgb(17 35 30 / 40%); }
.save-dialog h2 { margin: 0 0 16px; font-size: 20px; }
.save-dialog h2:focus { outline: none; }
.save-dialog[data-state="working"] h2::before { content: ""; display: inline-block; width: 16px; height: 16px; margin-right: 10px; border: 2px solid #dcece8; border-top-color: #147d70; border-radius: 50%; vertical-align: -2px; animation: save-spin 1s linear infinite; }
@keyframes save-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .save-dialog[data-state="working"] h2::before { animation: none; } }
.save-dialog p { margin: 12px 0; white-space: pre-line; overflow-wrap: anywhere; line-height: 1.6; }
.save-dialog progress { display: block; width: 100%; height: 12px; margin: 20px 0; accent-color: #147d70; }
.save-dialog progress::-webkit-progress-bar { background: #e5eeeb; border-radius: 4px; }
.save-dialog progress::-webkit-progress-value { background: #147d70; border-radius: 4px; }
.save-dialog progress[hidden], .save-dialog button[hidden] { display: none; }
.save-dialog #bulkSaveHint { color: #667773; font-size: 14px; }
.save-dialog button { margin-top: 8px; }
.save-dialog[data-state="error"] h2 { color: #a53b30; }
.patient-identity-dialog { width: min(900px, calc(100vw - 24px)); overflow-y: auto; }
.patient-identity-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.patient-identity-head h2 { margin: 0; }
.patient-identity-dialog h3 { font-size: 16px; margin: 20px 0 10px; }
.patient-identity-dialog button, .patient-identity-dialog span, .patient-identity-dialog p { overflow-wrap: anywhere; white-space: normal; }
.patient-identity-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid #d5e2df; }
.patient-identity-row span { min-width: 0; }
.patient-identity-row button { flex-shrink: 0; }
.patient-identity-field { border: 0; border-top: 1px solid #d5e2df; padding: 12px 0; min-width: 0; }
.patient-identity-field legend { padding: 0 6px 0 0; font-weight: 600; }
.patient-identity-field label { display: flex; align-items: flex-start; gap: 8px; padding: 8px; white-space: pre-wrap; overflow-wrap: anywhere; }
.patient-identity-field input, .patient-identity-confirm input { width: 20px; height: 20px; flex: 0 0 20px; margin: 0; }
.patient-identity-field.has-conflict { border-top-color: #bf8c30; }
.patient-identity-field.has-conflict label { background: #fff4df; }
.patient-identity-dialog textarea { display: block; width: 100%; min-height: 80px; margin-top: 8px; }
.patient-identity-confirm { display: flex; align-items: flex-start; gap: 8px; margin: 16px 0; }
.patient-identity-source button { display: block; width: 100%; text-align: left; }
.patient-identity-history { padding: 14px 0; border-bottom: 1px solid #d5e2df; }
@media (max-width: 480px) {
  .patient-identity-dialog { padding: 16px; }
  .patient-identity-row { align-items: flex-start; flex-direction: column; gap: 4px; }
}
.status-line { color: var(--muted); }
.status-line, #progressDetail, #catalogStatus, #catalogImportStatus, #historyFailureDetail, .image-load-error { overflow-wrap: anywhere; }
#historyFailureDetail, .image-load-error { color: #a14720; line-height: 1.6; }
.hidden { display: none !important; }

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

  .sidebar {
    position: sticky;
    z-index: 20;
    width: 100%;
    height: auto;
    padding: 12px 16px;
  }

  .brand { padding: 0 0 10px; }
  .brand-mark { flex-basis: 36px; width: 36px; height: 36px; }
  .brand strong { font-size: 16px; }

  .main-nav {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 4px;
  }

  .nav-item {
    min-height: 44px;
    justify-items: center;
    padding: 6px 4px;
    text-align: center;
  }

  .nav-item small { display: none; }
  .sidebar-footer { position: absolute; top: 10px; right: 14px; padding: 0; border: 0; }
  .logout-button { width: auto; min-height: 36px; padding-inline: 11px; }
  .brand { padding-right: 92px; }

  .app { padding: 24px 22px 48px; }
  .steps { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .history-workspace { grid-template-columns: 1fr; }
  .history-list { max-height: none; }
  .preview-pane { position: static; min-height: 0; }
  .preview-empty { min-height: 220px; }
  .word-preview, .excel-preview { height: auto; max-height: 70vh; }
}

@media (max-width: 640px) {
  .app { padding: 18px max(12px, env(safe-area-inset-left)) calc(42px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-right)); }
  .topbar { min-height: auto; margin-bottom: 16px; padding-bottom: 14px; }
  .topbar h1 { font-size: 23px; }
  .batch-label { display: none; }
  .panel, .rx-card, .history-item { padding: 14px; }
  .login-panel { margin-top: 20px; padding: 22px; }
  .upload-box { min-height: 150px; padding: 20px 10px; }
  .upload-box span { font-size: 19px; }
  .mobile-shortcuts { display: flex; }
  .mobile-return { display: inline-flex; }
  #history > .list-head,
  #aiConfig > .list-head,
  #summary > .list-head,
  #downloads > .list-head { display: flex; }

  .main-nav { overflow-x: auto; }
  .nav-item span { font-size: 13px; }

  .steps,
  .metrics,
  .rx-card,
  .config-grid { grid-template-columns: 1fr; }

  .rx-card > .card-title,
  .rx-card > .field-wide,
  .config-grid .field-wide { grid-column: auto; }

  .check-group { display: grid; gap: 10px; padding-top: 0; }
  .actions { display: grid; grid-template-columns: 1fr; }
  .actions > button, .actions > .button { min-width: 0; width: 100%; }
  #summary .actions { grid-template-columns: 1fr 1fr; }

  .bulk-actions {
    display: grid;
    margin-inline: -4px;
    padding: 12px;
  }

  .bulk-actions button { width: 100%; }
  .bulk-actions p { margin-left: 0; text-align: center; }
  textarea { min-height: 92px; }
  textarea[name="prescription_text"], textarea[name="error_notes"] { min-height: 116px; }
  .preview-head { align-items: flex-start; flex-direction: column; padding: 14px; }
  .preview-downloads { width: 100%; }
  .preview-downloads .button { flex: 1; }
  .word-preview { width: 100%; margin: 0; padding: 24px 18px 40px; box-shadow: none; }
}

.captcha-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 148px 68px;
  gap: 8px;
  align-items: stretch;
}
.captcha-row input { min-width: 0; }
.captcha-row img { width: 148px; height: 52px; border: 1px solid var(--line); border-radius: 6px; background: #f7faf8; object-fit: contain; }
.captcha-refresh { min-height: 52px; padding-inline: 8px; background: #edf5f3; color: var(--accent-dark); font-size: 12px; }
.captcha-refresh:hover { background: #dcece7; }

.user-workspace {
  height: calc(100vh - 150px);
  min-height: 560px;
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.user-browser { min-height: 0; display: flex; flex-direction: column; border-right: 1px solid var(--line); background: #f7faf8; }
.user-browser-head { display: flex; justify-content: space-between; gap: 12px; padding: 18px; border-bottom: 1px solid var(--line); }
.user-browser-head span { color: var(--muted); font-size: 11px; }
.user-list { min-height: 0; overflow-y: auto; }
.user-item { width: 100%; min-height: 76px; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 10px; justify-items: start; padding: 14px 16px; border-bottom: 1px solid var(--line); border-radius: 0; background: transparent; color: var(--ink); text-align: left; }
.user-item:hover { background: #eef6f3; }
.user-item.selected { background: #e1f1ec; box-shadow: inset 3px 0 0 var(--accent); }
.user-item.inactive { opacity: .58; }
.user-item strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-item span { color: var(--accent-dark); font-size: 11px; }
.user-item small { grid-column: 1 / -1; color: var(--muted); font-size: 11px; }
.user-editor { min-width: 0; overflow-y: auto; }
.user-editor-head { display: flex; align-items: center; justify-content: space-between; gap: 18px; padding: 20px 24px; border-bottom: 1px solid var(--line); }
.user-editor-head h2 { margin: 0; }
.user-active-row { display: flex; align-items: center; gap: 8px; color: var(--ink); }
.user-active-row input { width: 18px; height: 18px; }
.user-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; padding: 24px; }
.permission-summary { margin: 0 24px; padding: 16px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.permission-summary p { margin: 7px 0 0; color: var(--muted); }
.user-editor-footer { display: flex; align-items: center; justify-content: flex-end; gap: 16px; padding: 20px 24px; }
.user-editor-footer p { flex: 1; margin: 0; }

@media (max-width: 980px) {
  .main-nav { grid-template-columns: repeat(5, minmax(0, 1fr)); }
  .user-workspace { height: auto; grid-template-columns: 1fr; }
  .user-browser { max-height: 300px; border-right: 0; border-bottom: 1px solid var(--line); }
}

@media (max-width: 640px) {
  .captcha-row { grid-template-columns: minmax(0, 1fr) 116px; }
  .captcha-row img { width: 116px; }
  .captcha-refresh { grid-column: 1 / -1; min-height: 40px; }
  .user-fields { grid-template-columns: 1fr; padding: 18px; }
  .user-editor-head { align-items: stretch; flex-direction: column; padding: 18px; }
  .permission-summary { margin-inline: 18px; }
  .user-editor-footer { align-items: stretch; flex-direction: column; padding: 18px; }
}

@media (max-width: 380px) {
  #summary .actions { grid-template-columns: 1fr; }
  .brand small { display: none; }
}

/* Desktop workspace */
:root {
  --bg: #eef1ef;
  --sidebar: #ffffff;
  --sidebar-muted: #76817a;
  --accent: #176b4d;
  --accent-dark: #0f533b;
  --accent-soft: #e8f2ed;
  --line: #d7ddda;
}

body.authenticated .app-shell { grid-template-columns: 218px minmax(0, 1fr); }

.sidebar {
  padding: 22px 14px 16px;
  border-right: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
}

.brand { padding: 0 8px 28px; }
.brand strong { font-size: 17px; }
.brand small { color: var(--muted); }

.main-nav { gap: 3px; }
.nav-item {
  position: relative;
  min-height: 58px;
  padding: 9px 12px 9px 18px;
  color: #334139;
}
.nav-item:hover { background: #f3f6f4; }
.nav-item.active { background: var(--accent-soft); color: var(--accent-dark); }
.nav-item.active::before {
  content: "";
  position: absolute;
  inset: 9px auto 9px 0;
  width: 3px;
  border-radius: 2px;
  background: var(--accent);
}
.nav-item small,
.nav-item.active small { color: var(--muted); }

.sidebar-footer { border-color: var(--line); }
.logout-button { border: 1px solid var(--line); background: #fff; color: #4d5a52; }
.logout-button:hover { background: #f4f6f5; color: var(--warn); }

.app { max-width: none; padding: 26px 32px 40px; }
.topbar { min-height: 62px; margin-bottom: 18px; padding-bottom: 15px; }
.topbar h1 { font-size: 25px; }
.batch-label { background: transparent; }

.history-workspace {
  grid-template-columns: 310px minmax(0, 1fr);
  gap: 0;
  min-height: calc(100vh - 135px);
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  overflow: hidden;
}

.history-browser {
  min-width: 0;
  border-right: 1px solid var(--line);
  background: #f8faf9;
}

.history-browser-head { padding: 16px; border-bottom: 1px solid var(--line); background: #fff; }
.history-browser-head > div { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.history-browser-head strong { font-size: 15px; }
.history-browser-head span { color: var(--muted); font-size: 12px; }
.history-browser-head input { height: 36px; padding: 7px 10px; background: #f8faf9; font-size: 13px; }

.history-list { max-height: calc(100vh - 232px); padding: 0; gap: 0; overflow-y: auto; }
.history-row {
  width: 100%;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 5px 10px;
  padding: 14px 16px;
  border-bottom: 1px solid #e5e9e6;
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  text-align: left;
}
.history-row:hover { background: #f0f5f2; }
.history-row.selected { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.history-row-title { overflow: hidden; font-size: 14px; font-weight: 700; text-overflow: ellipsis; white-space: nowrap; }
.history-row time { color: var(--muted); font-size: 11px; white-space: nowrap; }
.history-status { width: max-content; color: var(--accent-dark); font-size: 12px; font-weight: 650; }
.status-failed { color: var(--warn); }
.status-processing { color: var(--blue); }
.history-row-detail { overflow: hidden; color: var(--muted); font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.history-empty { padding: 28px 16px; color: var(--muted); text-align: center; }

.preview-pane {
  position: relative;
  top: auto;
  min-height: 0;
  border: 0;
  border-radius: 0;
  background: #e9edeb;
}
.preview-head { min-height: 76px; padding: 14px 18px; background: #fff; }
.preview-head h2 { font-size: 18px; }
.preview-downloads { flex-wrap: wrap; justify-content: flex-end; }
.preview-downloads button,
.preview-downloads .button { min-height: 34px; padding-inline: 11px; font-size: 12px; }
.preview-downloads .ghost-danger { border: 1px solid #e4b9b5; background: #fff; color: var(--warn); }
.preview-downloads .ghost-danger:hover { background: #fff1f0; }
.preview-tabs { padding: 8px 18px 0; background: #fff; }
.preview-tab { padding-inline: 16px; }

.word-preview,
.excel-preview { height: calc(100vh - 258px); min-height: 480px; }
.word-preview {
  width: 100%;
  margin: 0;
  padding: 34px clamp(28px, 4vw, 64px) 64px;
  border: 0;
  box-shadow: none;
}
.excel-preview { padding: 0; background: #fff; }

.upload-panel { max-width: none; border: 0; background: transparent; padding: 0; }

@media (max-width: 980px) {
  body.authenticated .app-shell { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--line); }
  .brand { padding-bottom: 10px; }
  .nav-item { padding: 6px 4px; }
  .nav-item.active::before { display: none; }
  .history-workspace { grid-template-columns: 1fr; }
  .history-browser { border-right: 0; border-bottom: 1px solid var(--line); }
  .history-list { max-height: 340px; }
  .preview-pane { min-height: 480px; }
}

@media (max-width: 640px) {
  .app { padding: 16px 12px 42px; }
  .history-workspace { border-radius: 6px; }
  .preview-head { align-items: stretch; }
  .preview-downloads { display: grid; grid-template-columns: 1fr 1fr; }
  .preview-downloads button,
  .preview-downloads .button { width: 100%; }
  .word-preview { width: 100%; margin: 0; padding: 24px 18px 40px; }
}

/* Reference-aligned visual system */
:root {
  --ink: #17212b;
  --muted: #7f8b94;
  --line: #e1e8e5;
  --bg: #f7f9f8;
  --accent: #1c9b90;
  --accent-dark: #0e756c;
  --accent-soft: #e2f2ee;
  --blue: #5998df;
}

body { font-size: 13px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC", sans-serif; }
body.authenticated .app-shell { grid-template-columns: 224px minmax(0, 1fr); }

.sidebar { padding: 27px 16px 18px; border: 0; background: #f0f5f3; }
.brand { gap: 11px; padding: 0 12px 24px; }
.brand .brand-mark { width: 36px; height: 36px; flex-basis: 36px; }
.brand strong { font-size: 19px; font-weight: 800; }
.brand small { margin-top: 5px; color: #8ba19e; font-size: 8px; letter-spacing: 1px; }

.workspace-switcher {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-bottom: 23px;
  padding: 10px 9px;
  border: 1px solid #dbe7e2;
  border-radius: 9px;
  background: #e8f0ed;
}
.workspace-avatar { width: 28px; height: 28px; display: grid; flex: 0 0 28px; place-items: center; border-radius: 7px; background: #1e7770; color: #fff; font-weight: 700; }
.workspace-switcher strong,
.workspace-switcher small { display: block; }
.workspace-switcher strong { font-size: 11px; }
.workspace-switcher small { margin-top: 3px; color: #91a19f; font-size: 9px; }
.workspace-chevron { margin-left: auto; color: #6f8580; }

.main-nav { gap: 5px; }
.nav-item { min-height: 48px; display: flex; align-items: center; padding: 11px 13px; border-radius: 7px; color: #61736f; }
.nav-item span { font-size: 13px; }
.nav-item small { display: none; }
.nav-item.active { background: #fff; color: #116f67; box-shadow: 0 4px 12px rgba(31, 82, 74, 0.06); }
.nav-item.active::before { display: none; }
.nav-item:hover { background: rgba(255, 255, 255, 0.68); }

.sidebar-note { margin: 22px 7px 0; padding: 18px 7px; border-top: 1px solid #dbe5e2; color: #859592; }
.sidebar-note span { font-size: 9px; font-weight: 700; letter-spacing: 1px; }
.sidebar-note p { margin: 7px 0 0; font-size: 9px; line-height: 1.7; }
.sidebar-footer { display: flex; align-items: center; gap: 8px; padding: 14px 4px 0; border-color: #dbe5e2; }
.user-summary { min-width: 0; display: flex; flex: 1; align-items: center; gap: 8px; }
.user-summary > span { width: 28px; height: 28px; display: grid; place-items: center; border-radius: 7px; background: #1e7770; color: #fff; font-weight: 700; }
.user-summary strong,
.user-summary small { display: block; }
.user-summary strong { font-size: 10px; }
.user-summary small { margin-top: 2px; color: #91a19f; font-size: 9px; }
.logout-button { width: auto; min-height: 30px; padding: 0 9px; border: 0; background: transparent; font-size: 10px; }

.app { display: flex; flex-direction: column; min-height: 100vh; padding: 0; background: var(--bg); }
.site-footer { margin-top: auto; padding: 14px 20px max(18px, env(safe-area-inset-bottom)); text-align: center; font-size: 12px; }
.site-footer a { color: var(--muted); text-decoration: none; }
.site-footer a:hover,
.site-footer a:focus-visible { color: var(--accent-dark); text-decoration: underline; }
.topbar { height: 66px; min-height: 66px; margin: 0; padding: 0 32px; border-bottom: 1px solid #e7ecea; background: #fff; }
.topbar h1 { font-size: 14px; font-weight: 700; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.service-status { margin: 0; padding: 7px 10px; border: 1px solid #d7e9e2; border-radius: 18px; background: #fff; color: #3d9778; font-size: 9px; }
.service-status span { display: inline-block; width: 6px; height: 6px; margin-right: 5px; border-radius: 50%; background: #4fbd84; }
.batch-label { padding: 6px 9px; border-color: #e2e9e6; font-size: 9px; }
body.authenticated .view-section { max-width: none; margin: 28px 20px; padding-inline: 0; }

#summary .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
#summary .metrics div { min-height: 112px; display: grid; align-content: center; }
#summary .actions { max-width: 680px; }
#progress .steps { width: 100%; }
#aiConfig .config-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
body.authenticated #aiConfig { padding: 26px; }
.ai-settings-tabs { display: flex; flex-wrap: wrap; gap: 20px; border-bottom: 1px solid #dfe8e5; margin-bottom: 20px; }
.ai-settings-tabs button { min-width: 0; padding: 12px 0; border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: transparent; color: #65747c; box-shadow: none; }
.ai-settings-tabs button[aria-selected="true"] { color: #087b72; border-bottom-color: #087b72; }
#bailianFields { margin: 0; padding: 0; border: 0; min-width: 0; }
.provider-section-title { font-size: 14px; margin: 24px 0 14px; }
.provider-section-title:first-child { margin-top: 0; }
#bailianFields:disabled { opacity: .65; }
#bailianStatus { overflow-wrap: anywhere; }
#bailianEndpoint { color: #65747c; background: #f7f9f8; text-overflow: ellipsis; }
#downloads .actions { max-width: 520px; }

.upload-panel { display: grid; grid-template-columns: minmax(0, 1fr); align-items: start; gap: 0; }
.upload-box { display: grid; grid-template-columns: 44px minmax(0, 1fr) auto; justify-items: stretch; align-items: center; gap: 16px; min-height: 112px; padding: 24px; border: 1px dashed #becfca; border-radius: 8px; background: #fff; text-align: left; cursor: default; }
.upload-box:hover { border-color: #97b6ab; background: #fff; box-shadow: none; }
.upload-box.dragging { border-color: var(--accent-dark); background: #edf7f3; box-shadow: inset 0 0 0 1px var(--accent-dark); }
.upload-box .upload-file-mark { width: 44px; height: 50px; display: grid; place-items: center; border: 1px solid #cce5dd; border-radius: 6px; background: #e9f5ef; color: #176f60; font-size: 10px; }
.upload-copy { min-width: 0; }
.upload-copy h2 { margin: 0 0 6px; font-size: 18px; color: var(--ink); }
.upload-copy p { margin: 0; color: #657773; font-size: 12px; }
#chooseFileBtn { min-height: 44px; gap: 10px; }
#chooseFileBtn img { filter: brightness(0) invert(1); }

.upload-overview { grid-column: 1 / -1; display: grid; gap: 0; }
.dashboard-metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); padding: 20px 0; border-bottom: 1px solid var(--line); }
.dashboard-metrics > div { padding: 0 24px; border-right: 1px solid #dfe7e3; }
.dashboard-metrics > div:last-child { border: 0; }
.dashboard-metrics span { display: block; color: #657773; font-size: 12px; }
.dashboard-metrics strong { display: block; margin: 6px 0 0; font-size: 23px; }
.recent-panel { min-width: 0; padding: 20px 0 0; }
.recent-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.recent-heading h2 { margin: 0; font-size: 16px; }
.recent-heading button { min-height: 44px; padding: 0 8px; background: transparent; color: #187b70; font-size: 12px; }
.recent-heading button:hover { background: transparent; color: var(--accent-dark); }
.recent-list { border-top: 1px solid var(--line); }
.recent-row { width: 100%; min-height: 64px; display: grid; grid-template-columns: minmax(0, 2fr) minmax(90px, .7fr) 155px 48px; gap: 16px; align-items: center; padding: 14px 12px; border-bottom: 1px solid var(--line); border-radius: 0; background: #fff; color: #687a78; text-align: left; }
.recent-row:hover { background: #f7faf8; }
.recent-row > span:first-child strong { display: block; }
.recent-row > span:first-child { min-width: 0; }
.recent-row > span:first-child strong { color: #374947; font-size: 13px; overflow-wrap: anywhere; }
.recent-status { color: #916118; font-size: 12px; }
.recent-row time { color: #657773; font-size: 12px; }
.recent-action { color: #187b70; font-size: 12px; text-align: right; }
.recent-empty { margin: 0; padding: 24px 12px; color: #657773; border-bottom: 1px solid var(--line); }
.mobile-shortcuts { grid-column: 1 / -1; }

.history-workspace { min-height: calc(100vh - 134px); border-color: #e3eae8; border-radius: 10px; }
.history-browser { background: #f6f9f8; }
.history-browser-head { padding: 15px; }
.history-row { padding: 13px 15px; }
.history-row.selected { background: #e4f2ee; box-shadow: inset 3px 0 0 var(--accent); }
.preview-pane { background: #edf2f0; }
.preview-head { min-height: 70px; }
.preview-tab.active { color: var(--accent-dark); box-shadow: 0 -2px 0 var(--accent) inset; }

.section-kicker { margin: 0 0 4px; color: #8b9b98; font-size: 9px; font-weight: 700; letter-spacing: .8px; text-transform: uppercase; }

#summary { padding: 0; overflow: hidden; }
#summary > .list-head { margin: 0; padding: 18px 22px; border-bottom: 1px solid #e5ebe9; }
#summary > .list-head h2,
#cards > .list-head h2,
#baselines > .list-head h2 { margin: 0; font-size: 20px; }
.summary-dashboard { display: grid; grid-template-columns: minmax(360px, 1.4fr) minmax(320px, 1fr) 220px; gap: 22px; align-items: center; padding: 28px; }
.summary-message { min-width: 0; }
.summary-state { display: inline-flex; padding: 5px 8px; border-radius: 4px; background: #e5f4ef; color: #187b70; font-size: 10px; font-weight: 700; }
.summary-message h3 { margin: 12px 0 7px; font-size: 19px; }
.summary-message p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.6; }
#summary .metrics { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
#summary .metrics div { min-height: 104px; padding: 16px; align-content: center; background: #f8faf9; }
#summary .metrics span,
#summary .metrics small { display: block; color: var(--muted); font-size: 10px; }
#summary .metrics strong { margin: 4px 0 2px; font-size: 30px; }
#summary .summary-actions { width: 100%; max-width: none; display: grid; grid-template-columns: 1fr; gap: 9px; }
#summary .summary-actions button { width: 100%; }
.review-open-button { display: grid; place-items: center; min-height: 44px; }
.recent-row.is-loading .recent-action { font-size: 0; }
.recent-row.is-loading .recent-action::after { content: ''; display: inline-block; width: 20px; height: 20px; background: url('/static/assets/lucide/refresh-cw.svg') center / contain no-repeat; animation: save-spin 1s linear infinite; }
.review-open-button > span { grid-area: 1 / 1; }
.review-button-busy { display: inline-flex; align-items: center; gap: 8px; visibility: hidden; }
.review-button-busy img { flex: 0 0 16px; }
.reading-status { position: fixed; z-index: 90; bottom: max(24px, env(safe-area-inset-bottom)); right: 24px; max-width: min(420px, calc(100% - 48px)); display: flex; align-items: center; gap: 12px; padding: 14px 18px; border: 1px solid #c9dcd8; border-radius: 6px; background: #fff; color: #17695e; box-shadow: 0 3px 14px #0001; pointer-events: none; }
.reading-status[hidden] { display: none; }
.reading-status img { flex: 0 0 20px; animation: save-spin 1s linear infinite; }
.reading-status.error { color: #a42c27; }
.reading-status.error img { display: none; }
@media (prefers-reduced-motion: reduce) { .reading-status img, .recent-row.is-loading .recent-action::after { animation: none; } }
.review-open-button:not(.secondary) .review-button-busy img { filter: brightness(0) invert(1); }
.review-open-button.is-loading:disabled { opacity: 1; cursor: progress; }
.review-open-button.is-loading .review-button-label { visibility: hidden; }
.review-open-button.is-loading .review-button-busy { visibility: visible; }
.review-open-button.is-loading .review-button-busy img { animation: save-spin 1s linear infinite; }
.review-load-status { grid-column: 1 / -1; min-height: 1.5em; margin: 0; color: var(--muted); font-size: 12px; line-height: 1.5; overflow-wrap: anywhere; }
.review-load-status.error { color: #a03228; }
@media (prefers-reduced-motion: reduce) { .review-open-button.is-loading .review-button-busy img { animation: none; } }

#cards > .list-head,
#baselines > .list-head { margin-bottom: 14px; }
.review-workspace { height: calc(100vh - 232px); min-height: 560px; display: grid; grid-template-columns: 240px minmax(0, 1fr); border: 1px solid #dfe7e4; border-radius: 8px; background: #fff; overflow: hidden; }
.review-queue { min-width: 0; min-height: 0; overflow: hidden; border-right: 1px solid #dfe7e4; background: #f6f9f8; }
.review-queue-head { min-height: 58px; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; border-bottom: 1px solid #dfe7e4; background: #fff; }
.review-queue-head strong { font-size: 14px; }
.review-queue-head span { color: var(--muted); font-size: 11px; }
.card-nav { height: calc(100% - 58px); min-height: 0; overflow-y: scroll; scrollbar-gutter: stable; }
.card-nav-item { width: 100%; min-height: 64px; display: grid; grid-template-columns: 64px minmax(0, 1fr); gap: 4px 10px; justify-content: stretch; align-content: center; justify-items: start; padding: 10px 16px; border: 0; border-bottom: 1px solid #e4ebe8; border-radius: 0; background: transparent; color: var(--ink); text-align: left; }
.card-nav-item:hover { background: #edf5f2; }
.card-nav-item.selected { background: #dff0eb; box-shadow: inset 3px 0 0 var(--accent); }
.card-nav-item .card-nav-bed { grid-column: 1; grid-row: 1 / 3; align-self: center; color: #60716d; font-size: 12px; font-weight: 500; overflow-wrap: anywhere; }
.card-nav-item strong { grid-column: 2; font-size: 14px; line-height: 1.4; overflow-wrap: anywhere; }
.card-nav-item .card-nav-status { grid-column: 2; color: #738481; font-size: 11px; font-weight: 400; }
.card-nav-item.problem .card-nav-status { color: var(--warn); }
.card-nav-item.saved .card-nav-status { color: var(--accent-dark); }
.review-editor { min-height: 0; display: block; padding: 20px; overflow-y: scroll; scrollbar-gutter: stable; background: #f7f9f8; }
.card-nav::-webkit-scrollbar,
.review-editor::-webkit-scrollbar { width: 9px; }
.card-nav::-webkit-scrollbar-track,
.review-editor::-webkit-scrollbar-track { background: #edf2f0; }
.card-nav::-webkit-scrollbar-thumb,
.review-editor::-webkit-scrollbar-thumb { border: 2px solid #edf2f0; border-radius: 8px; background: #a9bbb6; }
.card-nav::-webkit-scrollbar-thumb:hover,
.review-editor::-webkit-scrollbar-thumb:hover { background: #7f9892; }
.review-editor .rx-card { display: none; margin: 0; border: 0; box-shadow: none; }
.review-editor .rx-card.active { display: grid; }
.review-editor .rx-card textarea[name="prescription_text"] { min-height: 120px; }
.formula-analysis { margin-top: 2px; padding: 18px 0 4px; border-top: 1px solid #dfe7e4; }
.formula-analysis-title { margin-bottom: 14px; color: #183b36; font-size: 15px; font-weight: 800; }
.formula-analysis-body { display: grid; gap: 14px; }
.formula-component { padding: 2px 0 2px 14px; border-left: 3px solid #2b9b8f; }
.formula-component h4,
.formula-overall h4 { margin: 0 0 8px; color: #174d46; font-size: 14px; }
.formula-component p,
.formula-overall p { margin: 4px 0 0; color: #4e615e; font-size: 13px; line-height: 1.75; }
.formula-component strong { color: #253f3b; }
.formula-overall { padding-top: 13px; border-top: 1px dashed #cfdcd8; }
.formula-analysis-empty { margin: 0; color: #8a9996; font-size: 12px; }
#cards .bulk-actions { margin-top: 10px; padding: 10px 12px; box-shadow: none; }
#cards .bulk-actions > button { flex-shrink: 0; white-space: nowrap; }

.baseline-workspace { height: calc(100vh - 162px); min-height: 600px; display: grid; grid-template-columns: 310px minmax(0, 1fr); border: 1px solid #dfe7e4; border-radius: 8px; background: #fff; overflow: hidden; }
.baseline-browser { min-width: 0; min-height: 0; overflow: hidden; border-right: 1px solid #dfe7e4; background: #f6f9f8; }
.baseline-browser-head { padding: 15px; border-bottom: 1px solid #dfe7e4; background: #fff; }
.baseline-browser-head > div { display: flex; justify-content: space-between; margin-bottom: 11px; }
.baseline-browser-head strong { font-size: 14px; }
.baseline-browser-head span { color: var(--muted); font-size: 11px; }
.baseline-browser-head input { height: 36px; background: #f7faf8; font-size: 12px; }
.baseline-list { height: calc(100% - 88px); min-height: 0; overflow-y: scroll; scrollbar-gutter: stable; }
.baseline-item { width: 100%; min-height: 66px; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 5px 8px; padding: 12px 15px; border: 0; border-bottom: 1px solid #e4ebe8; border-radius: 0; background: transparent; color: var(--ink); text-align: left; }
.baseline-item:hover { background: #edf5f2; }
.baseline-item.selected { background: #dff0eb; box-shadow: inset 3px 0 0 var(--accent); }
.baseline-item strong { overflow: hidden; font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.baseline-item span { color: #788885; font-size: 10px; }
.baseline-item small { grid-column: 1 / -1; overflow: hidden; color: #9aa6a4; font-size: 9px; text-overflow: ellipsis; white-space: nowrap; }
.baseline-item.discharged { opacity: .62; }
.baseline-detail { min-width: 0; min-height: 0; overflow-y: scroll; scrollbar-gutter: stable; background: #f7f9f8; }
.baseline-list::-webkit-scrollbar,
.baseline-detail::-webkit-scrollbar { width: 9px; }
.baseline-list::-webkit-scrollbar-track,
.baseline-detail::-webkit-scrollbar-track { background: #edf2f0; }
.baseline-list::-webkit-scrollbar-thumb,
.baseline-detail::-webkit-scrollbar-thumb { border: 2px solid #edf2f0; border-radius: 8px; background: #a9bbb6; }
.baseline-list::-webkit-scrollbar-thumb:hover,
.baseline-detail::-webkit-scrollbar-thumb:hover { background: #7f9892; }
.baseline-empty { height: 100%; display: grid; place-content: center; gap: 6px; color: var(--muted); text-align: center; }
.baseline-empty p { margin: 0; font-size: 12px; }
.baseline-form { min-height: 100%; background: #fff; }
.baseline-detail-head { min-height: 76px; display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 14px 20px; border-bottom: 1px solid #e4ebe8; }
.baseline-detail-head h2 { margin: 0; font-size: 19px; }
.status-field { width: 150px; }
.status-field select { height: 36px; padding-block: 6px; }
.baseline-provenance { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); border-bottom: 1px solid #e4ebe8; background: #f7faf9; }
.baseline-provenance > div { min-width: 0; display: grid; gap: 4px; padding: 13px 20px; }
.baseline-provenance > div + div { border-left: 1px solid #e4ebe8; }
.baseline-provenance span { color: var(--muted); font-size: 10px; }
.baseline-provenance strong { overflow: hidden; font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.baseline-provenance small { overflow: hidden; color: #83918f; font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.baseline-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 13px 16px; padding: 20px; }
.baseline-fields > .field-wide { grid-column: 1 / -1; }
.baseline-fields textarea { min-height: 72px; }
.baseline-fields #baselinePrescription { min-height: 118px; }
.baseline-footer { position: sticky; bottom: 0; display: flex; align-items: center; justify-content: flex-end; gap: 16px; padding: 12px 20px; border-top: 1px solid #dfe7e4; background: rgba(255, 255, 255, .96); }
.baseline-footer p { flex: 1; margin: 0; }

button,
.button { border-radius: 6px; background: var(--accent-dark); }
button:hover,
.button:hover { background: #095f58; }
button.secondary { background: #edf5f3; color: #247f77; }
button.danger { background: #ef7270; }

@media (max-width: 1300px) {
  .summary-dashboard { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #summary .summary-actions { grid-column: 1 / -1; grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 1050px) {
  .review-workspace { grid-template-columns: 230px minmax(0, 1fr); }
  .baseline-workspace { grid-template-columns: 260px minmax(0, 1fr); }
}

@media (max-width: 980px) {
  body.authenticated .app-shell { grid-template-columns: 1fr; }
  .sidebar { border-bottom: 1px solid var(--line); }
  .workspace-switcher,
  .sidebar-note,
  .user-summary { display: none; }
  .sidebar-footer { top: 10px; }
  .main-nav { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .nav-item { justify-content: center; }
  body.authenticated .view-section { margin: 22px 16px; padding-inline: 0; }
  body.authenticated #aiConfig { padding: 18px; }
  .review-workspace,
  .baseline-workspace { height: auto; grid-template-columns: 1fr; }
  .review-queue,
  .baseline-browser { border-right: 0; border-bottom: 1px solid #dfe7e4; }
  .card-nav,
  .baseline-list { max-height: 260px; }
}

@media (max-width: 680px) {
  .baseline-provenance { grid-template-columns: 1fr; }
  .baseline-provenance > div + div { border-top: 1px solid #e4ebe8; border-left: 0; }
}

@media (max-width: 640px) {
  .topbar { padding-inline: 14px; }
  .service-status { display: none; }
  body.authenticated .view-section { margin: 16px 12px; padding-inline: 0; }
  .upload-box { grid-template-columns: 40px minmax(0, 1fr); min-height: 0; gap: 12px; padding: 16px; }
  .upload-box .upload-file-mark { width: 40px; height: 46px; }
  .upload-copy h2 { font-size: 16px; }
  #chooseFileBtn { grid-column: 1 / -1; width: 100%; }
  #summary .metrics,
  #aiConfig .config-grid { grid-template-columns: 1fr; }
  .summary-dashboard { grid-template-columns: 1fr; padding: 18px; }
  #summary .summary-actions { grid-column: auto; grid-template-columns: 1fr; }
  .baseline-fields,
  .review-editor .rx-card { grid-template-columns: 1fr; }
  .baseline-fields > .field-wide,
  .review-editor .rx-card > .card-title,
  .review-editor .rx-card > .field-wide { grid-column: 1; }
  .baseline-detail-head { align-items: stretch; flex-direction: column; }
  .status-field { width: 100%; }
  .dashboard-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px 0; padding: 16px 0; }
  .dashboard-metrics > div { padding: 0 16px; }
  .dashboard-metrics > div:nth-child(2) { border-right: 0; }
  .recent-row { grid-template-columns: minmax(0, 1fr) auto; gap: 8px 12px; }
  .recent-row .recent-status { grid-row: 2; font-size: 11px; }
  .recent-row time { grid-row: 2; font-size: 11px; text-align: right; }
  .recent-action { grid-column: 2; grid-row: 1; }
}
/* Identity evidence stays with the prescription being reviewed. */
.identity-review { padding: 16px; border-left: 3px solid #b66b14; background: #fff8ec; }
.identity-review p { margin: 8px 0; }
.identity-review pre { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 180px; overflow-y: auto; font: inherit; }
.identity-review .identity-confirm, .identity-review .identity-defer { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.identity-review .identity-defer.hidden { display: none; }
.identity-review .identity-confirm, .identity-review .identity-defer { min-height: 44px; }
.card-nav-item.name-pending .card-nav-status { color: #986015; }
.identity-review input[type="checkbox"] { width: 18px; height: 18px; flex: 0 0 18px; }

.rx-highlight-editor { position: relative; display: block; min-width: 0; background: #fff; border-radius: 6px; }
.rx-highlight-backdrop { position: absolute; inset: 1px; overflow: hidden; pointer-events: none; border-radius: 5px; }
.rx-highlight-editor textarea, .rx-highlight-text {
  font: inherit; font-size: 15px; line-height: 1.65; letter-spacing: 0;
  padding: 10px 11px; white-space: pre-wrap; overflow-wrap: break-word; tab-size: 8;
}
.rx-highlight-editor textarea { position: relative; display: block; background: transparent; }
.rx-highlight-text { display: block; position: relative; color: transparent; box-sizing: border-box; }
.review-mark { color: transparent; background: #fff0bb; border-bottom: 2px solid #b37a15; }
.review-mark.dose { background: #ffe0d8; border-color: #c34b35; }
.review-mark.located { outline: 2px solid #0f766e; }
.review-field-located { outline: 2px solid #b37a15 !important; background: #fff8df; }
.review-issues { display: grid; margin-top: 6px; }
.review-issues[hidden] { display: none; }
.review-issue { padding: 12px 0; border-top: 1px solid var(--line); font-size: 13px; min-width: 0; }
.review-issue strong { color: #956213; }
.review-issue.dose strong { color: #b53826; }
.review-issue.service strong, .review-issue.note strong { color: #536777; }
.review-issue p { margin: 5px 0 8px; line-height: 1.65; overflow-wrap: anywhere; }
.review-issue-targets { display: flex; flex-wrap: wrap; gap: 8px 16px; color: var(--muted); }
.review-issue-targets .review-locate { display: inline-flex; align-items: center; gap: 5px; padding: 4px 0; min-width: 0; background: transparent; color: var(--accent); text-align: left; overflow-wrap: anywhere; border: 0; font-size: 13px; }
.review-locate img { width: 16px; height: 16px; flex: 0 0 16px; }
.review-location-status { margin: 8px 0 0; color: var(--accent); font-size: 13px; }
.retry-medicine-review[hidden] { display: none; }

.rx-review-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; min-width: 0; align-items: stretch; }
.duplicate-dose-suggestions section { padding: 12px 0; border-top: 1px solid var(--border, #dce6e2); overflow-wrap: anywhere; }
.duplicate-dose-suggestions p { margin: 8px 0; line-height: 1.6; }
.rx-medicine-field { min-width: 0; grid-template-rows: 22px minmax(0, 1fr); align-content: start; }
.review-editor .rx-medicine-field textarea[name="prescription_text"] { height: 100%; min-height: 0; resize: none; }
.rx-review-row .rx-highlight-editor { min-height: 0; }
.rx-review-panel { display: grid; grid-template-rows: 28px minmax(0, 1fr) auto; height: 360px; max-height: 48vh; min-height: 260px; min-width: 0; }
.rx-review-heading { display: flex; justify-content: space-between; align-items: start; gap: 12px; font-size: 14px; }
.review-issue-count { font-size: 12px; color: var(--muted); white-space: nowrap; }
.catalog-review-card .rx-review-heading { flex-wrap: wrap; gap: 4px 12px; }
.rx-review-scroll { min-height: 0; overflow-y: auto; overscroll-behavior: contain; scrollbar-gutter: stable; padding-right: 8px; }
.rx-review-panel .review-issues { margin-top: 0; }
.rx-review-panel .review-issue:first-child { border-top: 0; padding-top: 0; }
.rx-review-actions { padding-top: 8px; }
.rx-review-actions .review-location-status:empty { display: none; }
.review-empty { margin: 10px 0 16px; color: var(--muted); font-size: 13px; }
.review-empty[hidden] { display: none; }
.review-notes-editor { border-top: 1px solid var(--line); margin-top: 8px; padding-top: 10px; font-size: 13px; }
.review-notes-editor summary { cursor: pointer; color: var(--accent-dark); padding: 4px 0; }
.review-notes-editor label { margin-top: 8px; }
.review-notes-editor textarea { min-height: 160px; }
.rx-review-scroll::-webkit-scrollbar { width: 8px; }
.rx-review-scroll::-webkit-scrollbar-thumb { background: #a9bbb6; border-radius: 4px; }
@media (max-width: 1199px) {
  .rx-review-row { grid-template-columns: minmax(0, 1fr); gap: 14px; }
  .rx-medicine-field { grid-template-rows: auto 220px; }
  .rx-review-panel { height: auto; max-height: none; min-height: 0; padding: 12px 0 0; border-left: 0; border-top: 1px solid var(--line); }
  .rx-review-scroll { max-height: 320px; }
}

.review-mark.catalog-green { background: #dff2e4; border: 0; }
.review-mark.catalog-yellow { background: #fff0bb; border: 0; }
.review-mark.catalog-red { background: #fbdedb; border: 0; }
.review-mark.catalog-underlined { border-bottom: 1px solid #596d64; }
.catalog-medicine { padding: 12px 0; border-top: 1px solid var(--line); min-width: 0; }
.catalog-medicine:first-child { border-top: 0; }
.catalog-medicine summary { min-height: 44px; cursor: pointer; overflow-wrap: anywhere; }
.catalog-medicine summary strong { font-size: 15px; }
.catalog-medicine summary span { display: block; font-size: 12px; padding: 4px 0; }
.catalog-medicine p { margin: 6px 0; line-height: 1.6; overflow-wrap: anywhere; }
.catalog-medicine.catalog-red summary, .catalog-warning, .catalog-unparsed { color: #a03228; }
.catalog-medicine.catalog-yellow summary { color: #885915; }
.catalog-medicine.catalog-green summary { color: #276347; }
.catalog-medicine.catalog-deleted summary, .catalog-basis { color: #657770; }
.catalog-history { margin-top: 12px; padding-top: 8px; border-top: 1px solid var(--line); color: #657770; overflow-wrap: anywhere; }
.catalog-history strong { font-size: 13px; }
.catalog-history p { font-size: 13px; }
.catalog-history-section { border-top: 1px solid var(--line); margin-top: 12px; }
.catalog-history-section > summary { min-height: 44px; padding: 12px 0; cursor: pointer; color: #657770; overflow-wrap: anywhere; }
.catalog-medicine input[type="search"], .catalog-medicine select { width: 100%; min-height: 44px; font-size: 16px; margin-top: 8px; }
.catalog-medicine select, .review-patient-picker { appearance: none; -webkit-appearance: none; height: 44px; padding-right: 38px; background-image: url('/static/assets/lucide/chevron-down.svg'); background-position: right 12px center; background-size: 16px; background-repeat: no-repeat; }
.catalog-choice-label { display: grid; gap: 4px; margin-top: 8px; }
.catalog-choice-label input { min-height: 44px; width: 100%; min-width: 0; font-size: 16px; }
.catalog-dose-fields { min-width: 0; }
.catalog-dose-input { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: stretch; }
.catalog-dose-input input { border-radius: 4px 0 0 4px; }
.catalog-dose-unit { display: flex; align-items: center; justify-content: center; min-width: 44px; padding: 0 12px; border: 1px solid var(--line); border-left: 0; border-radius: 0 4px 4px 0; background: #f5f8f7; color: #657770; font-size: 16px; white-space: nowrap; }
.catalog-choice-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.catalog-choice-actions button, .catalog-icon { min-height: 44px; }
.catalog-icon { width: 44px; height: 44px; padding: 12px; display: inline-grid; place-items: center; }
.catalog-icon img { width: 18px; height: 18px; }
.catalog-keep { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 8px 0; }
.catalog-keep input { flex: 0 0 20px; width: 20px; height: 20px; }
.catalog-review-card .rx-highlight-editor textarea, .catalog-review-card .rx-highlight-text { font-size: 16px; }
.catalog-review-card .rx-review-panel { height: 480px; max-height: 58vh; }
.catalog-review-card .rx-review-scroll { max-height: none; }
.catalog-review-card .rx-review-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.catalog-review-card .rx-medicine-field { grid-template-rows: 22px minmax(0, 1fr); }
.catalog-review-card .rx-review-panel { grid-template-rows: 28px minmax(0, 1fr) auto; display: grid; padding-top: 0; border-top: 0; }
.review-patient-navigation { display: none; }
@media (max-width: 1199px) {
  #cards .review-workspace { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); height: calc(100vh - 235px); min-height: 500px; }
  #cards .review-queue { border-right: 0; border-bottom: 1px solid var(--line); }
  #cards .review-queue-head { display: none; }
  #cards .card-nav { display: none; }
  .review-patient-navigation { display: grid; grid-template-columns: 44px minmax(0, 1fr) 104px; align-items: center; gap: 8px; padding: 8px 12px; }
  .review-patient-navigation button { height: 44px; padding: 0 10px; display: inline-flex; justify-content: center; align-items: center; gap: 6px; white-space: nowrap; }
  #reviewNextBtn img { filter: brightness(0) invert(1); }
  #reviewPreviousBtn img { transform: rotate(180deg); }
  .review-patient-picker { display: block; min-width: 0; width: 100%; margin: 0; min-height: 44px; font-size: 16px; text-overflow: ellipsis; }
  #cards .review-editor { padding: 12px; }
  .catalog-review-card .rx-review-panel { height: 360px; max-height: none; }
}
@media (max-width: 819px) {
  .catalog-review-card .rx-review-row { grid-template-columns: minmax(0, 1fr); }
  .catalog-review-card .rx-medicine-field { grid-template-rows: auto 180px; }
  .catalog-review-card .rx-review-panel { height: 420px; border-top: 1px solid var(--line); padding-top: 12px; }
}
