/* view_monthly_scripts_html's own rules, every one scoped to its root.
 * The rules at the end style single elements. The skeleton rules are
 * compound so they beat theme.css's .ch-skeleton family; the per-item MAR
 * modal rules are id-anchored so they beat the class rules around them, and
 * the Generate rule's two ids keep a disabled button's pointer cursor. */

:where(.ms-root) .ch-scripts-container {
  padding: var(--ch-space-lg);
  font-family: var(--ch-font-primary);
  background: var(--ch-bg-primary);
  min-height: 100vh;
}

:where(.ms-root) .ch-scripts-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--ch-space-lg);
  flex-wrap: wrap;
  gap: var(--ch-space-md);
}

:where(.ms-root) .ch-scripts-title {
  font-size: 28px;
  font-weight: 600;
  color: var(--ch-text-primary);
  margin: 0;
}

:where(.ms-root) .ch-main-content {
  display: flex;
  flex-direction: column;
  gap: var(--ch-space-md);
}

:where(.ms-root) .ch-main-area {
  min-width: 0;
}

@media (max-width: 768px) {
  :where(.ms-root) .ch-scripts-container {
    padding: var(--ch-space-md);
  }
}

/* Status Guide Button and Popup */
:where(.ms-root) .ch-guide-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 500;
  font-family: var(--ch-font-primary);
  background: var(--ch-bg-secondary);
  color: var(--ch-text-secondary);
  border: 1px solid var(--ch-border);
  border-radius: var(--ch-radius-md);
  cursor: pointer;
  transition: opacity var(--ch-transition-fast), transform var(--ch-transition-fast), background-color var(--ch-transition-fast), color var(--ch-transition-fast), border-color var(--ch-transition-fast), box-shadow var(--ch-transition-fast);
}

:where(.ms-root) .ch-guide-btn:hover {
  background: var(--ch-accent-secondary);
  color: var(--ch-accent-primary-text, #b45309);
  border-color: var(--ch-accent-primary);
}

:where(.ms-root) .ch-guide-btn svg {
  width: 16px;
  height: 16px;
}

:where(.ms-root) .ch-table-header-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: var(--ch-space-sm);
}

:where(.ms-root) .ch-guide-popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--ch-bg-overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--ch-z-modal);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--ch-transition-normal), visibility var(--ch-transition-normal);
  padding: var(--ch-space-md);
}

:where(.ms-root) .ch-guide-popup.visible {
  opacity: 1;
  visibility: visible;
}

:where(.ms-root) .ch-guide-modal {
  background: var(--ch-bg-card);
  border-radius: var(--ch-radius-lg);
  box-shadow: var(--ch-shadow-lg);
  max-width: 700px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  transform: scale(0.95);
  transition: transform var(--ch-transition-normal);
}

:where(.ms-root) .ch-guide-popup.visible .ch-guide-modal {
  transform: scale(1);
}

:where(.ms-root) .ch-guide-header {
  padding: var(--ch-space-md);
  border-bottom: 1px solid var(--ch-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

:where(.ms-root) .ch-guide-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--ch-text-primary);
  margin: 0;
}

:where(.ms-root) .ch-guide-close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--ch-text-muted);
  cursor: pointer;
  border-radius: var(--ch-radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity var(--ch-transition-fast), transform var(--ch-transition-fast), background-color var(--ch-transition-fast), color var(--ch-transition-fast), border-color var(--ch-transition-fast), box-shadow var(--ch-transition-fast);
}

:where(.ms-root) .ch-guide-close:hover {
  background: var(--ch-bg-secondary);
  color: var(--ch-text-primary);
}

:where(.ms-root) .ch-guide-content {
  padding: var(--ch-space-md);
}

:where(.ms-root) .ch-guide-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--ch-space-lg);
}

@media (max-width: 768px) {
  :where(.ms-root) .ch-guide-grid {
    grid-template-columns: 1fr;
  }
}

:where(.ms-root) .ch-guide-section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--ch-text-secondary);
  margin-bottom: var(--ch-space-sm);
  padding-bottom: var(--ch-space-xs);
  border-bottom: 1px solid var(--ch-border);
}

:where(.ms-root) .ch-guide-item {
  margin-bottom: 8px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--ch-text-primary);
}

:where(.ms-root) .ch-guide-item strong {
  color: var(--ch-text-primary);
}

:where(.ms-root) .ch-guide-item span {
  color: var(--ch-text-secondary);
}

:where(.ms-root) .ch-guide-example {
  margin-top: var(--ch-space-sm);
  padding: var(--ch-space-sm);
  background: var(--ch-bg-secondary);
  border-radius: var(--ch-radius-sm);
  font-size: 12px;
  color: var(--ch-text-secondary);
}

:where(.ms-root) .ch-guide-example strong {
  color: var(--ch-text-primary);
}

:where(.ms-root) .ch-guide-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: var(--ch-space-sm);
}

:where(.ms-root) .ch-guide-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 500;
  border-radius: var(--ch-radius-sm);
}

:where(.ms-root) .ch-guide-badge-acute {
  background: rgba(34, 197, 94, 0.15);
  color: var(--ch-success);
  border: 1px solid rgba(34, 197, 94, 0.3);
}

:where(.ms-root) .ch-guide-badge-monthly {
  background: rgba(234, 179, 8, 0.15);
  color: var(--ch-warning);
  border: 1px solid rgba(234, 179, 8, 0.3);
}

/* Loading overlay */
:where(.ms-root) .ch-loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: var(--ch-bg-overlay);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: var(--ch-z-overlay);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--ch-transition-normal), visibility var(--ch-transition-normal);
}

:where(.ms-root) .ch-loading-overlay.visible {
  opacity: 1;
  visibility: visible;
}

:where(.ms-root) .ch-loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--ch-border);
  border-top-color: var(--ch-accent-primary);
  border-radius: 50%;
  animation: pxSpin 1s linear infinite;
}

:where(.ms-root) .ch-loading-text {
  color: var(--ch-text-inverse);
  font-size: 14px;
  font-weight: 500;
}

/* Modal styles */
:where(.ms-root) .ch-modal-overlay {
  background: var(--ch-bg-overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--ch-z-modal);
  transition: opacity var(--ch-transition-normal), visibility var(--ch-transition-normal);
  padding: var(--ch-space-md);
}

/* Designer gate: the overlay covers the page and starts hidden only once
   initMonthlyScripts marks the root ms-live. The designer runs no module,
   so each modal panel and its slot sit in the page flow on the canvas. */
:where(.ms-root.ms-live) .ch-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  opacity: 0;
  visibility: hidden;
}

:where(.ms-root) .ch-modal-overlay.visible {
  opacity: 1;
  visibility: visible;
}

:where(.ms-root) .ch-modal {
  background: var(--ch-bg-card);
  border-radius: var(--ch-radius-lg);
  box-shadow: var(--ch-shadow-lg);
  max-width: 600px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  transform: scale(0.95);
  transition: transform var(--ch-transition-normal);
}

:where(.ms-root) .ch-modal-overlay.visible .ch-modal {
  transform: scale(1);
}

:where(.ms-root) .ch-modal-header {
  padding: var(--ch-space-md);
  border-bottom: 1px solid var(--ch-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

:where(.ms-root) .ch-modal-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--ch-text-primary);
  margin: 0;
}

:where(.ms-root) .ch-modal-close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--ch-text-muted);
  cursor: pointer;
  border-radius: var(--ch-radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity var(--ch-transition-fast), transform var(--ch-transition-fast), background-color var(--ch-transition-fast), color var(--ch-transition-fast), border-color var(--ch-transition-fast), box-shadow var(--ch-transition-fast);
}

:where(.ms-root) .ch-modal-close:hover {
  background: var(--ch-bg-secondary);
  color: var(--ch-text-primary);
}

:where(.ms-root) .ch-modal-body {
  padding: 0;
}

/* Reorder Form Button Section */
:where(.ms-root) .ch-reorder-section {
  display: flex;
  align-items: center;
  gap: var(--ch-space-md);
  padding: var(--ch-space-md);
  margin-bottom: var(--ch-space-md);
  background: var(--ch-bg-card);
  border: 1px solid var(--ch-border);
  border-radius: var(--ch-radius-lg);
}

:where(.ms-root) .ch-reorder-btn {
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 500;
  font-family: var(--ch-font-primary);
  background: var(--ch-accent-primary);
  color: var(--ch-text-inverse);
  border: none;
  border-radius: var(--ch-radius-md);
  cursor: pointer;
  transition: opacity var(--ch-transition-fast), transform var(--ch-transition-fast), background-color var(--ch-transition-fast), color var(--ch-transition-fast), border-color var(--ch-transition-fast), box-shadow var(--ch-transition-fast);
  white-space: nowrap;
  min-height: 44px;
}

:where(.ms-root) .ch-reorder-btn:hover {
  background: var(--ch-accent-primary-hover);
}

:where(.ms-root) .ch-reorder-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

:where(.ms-root) .ch-reorder-btn:disabled:hover {
  background: var(--ch-accent-primary);
}

:where(.ms-root) .ch-reorder-text {
  font-size: 13px;
  color: var(--ch-text-secondary);
  line-height: 1.4;
}

/* Per-item MAR modal flag badges - use the global .mar-flag-* rules in theme.css (single source). */

/* === Per-item MAR modal: hub-style edit row === */
:where(.ms-root) .mar-item-edit {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 2px;
}
:where(.ms-root) .mar-item-edit label { font-size: 0.78rem; white-space: nowrap; }
:where(.ms-root) .mar-item-edit input[type="number"],
:where(.ms-root) .mar-item-edit input[type="text"],
:where(.ms-root) .mar-item-edit textarea {
  padding: 3px 6px;
  border: 1.5px solid #aaa;
  border-radius: 4px;
  background: var(--ch-bg-input);
  color: var(--ch-text-primary);
  font-size: 0.8rem;
  font-family: inherit;
  line-height: 1.3;
  box-sizing: border-box;
  height: 26px;
}
:where(.ms-root) .mar-item-edit textarea {
  height: auto;
  min-height: 26px;
  field-sizing: content;
  max-height: 120px;
  overflow-y: auto;
}
:where(.ms-root) .mar-item-edit input:focus,
:where(.ms-root) .mar-item-edit textarea:focus {
  border-color: var(--ch-accent-primary);
  outline: none;
}

/* Per-item MAR modal med-row UI (chip pill, PRN chip, dose-input,
   schedule-row, flag badges) uses the SHARED .mar-* component
   styled by theme/assets/theme.css (single source). No per-form
   duplicates here — a change in theme.css propagates automatically. */

/* === Per-item MAR modal: End of cycle / quick-action buttons === */
:where(.ms-root) .mar-eoc-btn {
  padding: 6px 10px; font-size: 11px; font-weight: 500;
  border: 1px solid var(--ch-border); border-radius: var(--ch-radius-sm);
  background: var(--ch-bg-secondary); color: var(--ch-text-secondary);
  cursor: pointer; white-space: nowrap; transition: all 0.15s;
}
:where(.ms-root) .mar-eoc-btn:hover { border-color: var(--ch-accent-primary); color: var(--ch-accent-primary-text, #b45309); }
:where(.ms-root) .mar-eoc-btn:disabled { opacity: 0.4; cursor: default; }
:where(.ms-root) .mar-eoc-btn:disabled:hover { border-color: var(--ch-border); color: var(--ch-text-secondary); }
:where(.ms-root) #mar-item-generate-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* === Per-item MAR modal: field validation === */
:where(.ms-root) #mar-item-modal-content input.invalid,
:where(.ms-root) #mar-item-modal-content textarea.invalid { border: 2px solid #dc2626 !important; background: #fef2f2 !important; }
:where(.ms-root) #mar-item-modal-content .mar-schedule-row.invalid { outline: 2px solid #dc2626; border-radius: 4px; }
:where(.ms-root) .mar-field-error {
  display: block; color: #dc2626; font-size: 0.72rem; margin-top: 2px;
  line-height: 1.2; font-weight: 500;
}

/* === Per-item MAR modal: warnings/cautions === */
:where(.ms-root) .mar-wc-details { font-size: 0.78rem; margin-top: 1px; color: var(--ch-text-primary); }
:where(.ms-root) .mar-wc-details > summary {
  cursor: pointer; color: var(--ch-text-secondary);
  user-select: none; font-size: 0.72rem;
  list-style: none !important;
}
:where(.ms-root) .mar-wc-details > summary::-webkit-details-marker { display: none !important; }
:where(.ms-root) .mar-wc-details > summary::marker { content: '' !important; }
:where(.ms-root) .mar-wc-details > summary::before {
  content: '\25B6'; display: inline-block; margin-right: 6px;
  transition: transform 0.15s ease; font-size: 0.7rem;
}
:where(.ms-root) .mar-wc-details[open] > summary::before { transform: rotate(90deg); }
:where(.ms-root) .mar-wc-has-content { color: #e65100 !important; }

@media (max-width: 768px) {
  /* Qty + 0-btn + Unit share row 1; Instructions wraps to its own full-width row 2.
     Mirrors the med-row's mobile pattern (commit 611b5892) for cross-surface consistency. */
  :where(.ms-root) .mar-item-edit { flex-flow: row wrap; align-items: flex-start; column-gap: 8px; row-gap: 6px; }
  :where(.ms-root) .mar-item-edit > label:nth-of-type(1),
  :where(.ms-root) .mar-item-edit > label:nth-of-type(2) {
    width: auto; flex: 0 0 auto; white-space: nowrap;
    display: inline-flex; align-items: center; gap: 4px;
  }
  :where(.ms-root) .mar-item-edit > label:nth-of-type(3) { flex: 1 1 100% !important; width: 100%; }
  :where(.ms-root) .mar-item-edit > label:nth-of-type(3) textarea { width: 100%; }
  :where(.ms-root) .mar-schedule-row { padding-left: 0; }
  /* .mar-rt-chip-label mobile padding lives in theme/assets/theme.css */
}

/* ==== Replacements for the template's inline style= attributes ==== */
:where(.ms-root) .ms-home-progress { margin-bottom: 16px; }
:where(.ms-root) .ms-home-progress-bar { width: 100%; }
:where(.ms-root) .ch-skeleton.ms-skel-hero { height: 120px; border-radius: var(--ch-radius-lg); margin-bottom: var(--ch-space-md); }
:where(.ms-root) .ch-skeleton.ms-skel-line-60 { width: 60%; height: 1.2em; margin-bottom: var(--ch-space-sm); }
:where(.ms-root) .ch-skeleton.ms-skel-line-40 { width: 40%; height: 1.2em; margin-bottom: var(--ch-space-md); }
:where(.ms-root) .ch-skeleton.ms-skel-row { height: 40px; border-radius: var(--ch-radius-md); margin-bottom: var(--ch-space-sm); }
:where(.ms-root) .ms-guide-section-gap { margin-top: var(--ch-space-md); }
:where(.ms-root) #mar-item-modal { z-index: 350; }
:where(.ms-root) #mar-item-modal .ch-modal { width: 90vw; max-width: 700px; max-height: 90vh; border-radius: var(--ch-radius-lg); display: flex; flex-direction: column; }
:where(.ms-root) #mar-item-modal .ch-modal-header { flex-shrink: 0; }
:where(.ms-root) #mar-item-modal .ch-modal-body { padding: var(--ch-space-md); overflow-y: auto; flex: 1; }
:where(.ms-root) #mar-item-modal-content { font-family: var(--ch-font-primary); font-size: 14px; color: var(--ch-text-primary); max-width: 900px; margin: 0 auto; }
:where(.ms-root) #mar-item-patient-info { margin-bottom: var(--ch-space-md); }
:where(.ms-root) #mar-item-rows-slot { margin-bottom: var(--ch-space-md); }
/* The portal accent on both dependency-editor mount roots; the editors read it from here. */
:where(.ms-root) #ctm-engine-slot, :where(.ms-root) #mar-item-rows-slot { --pxm-accent: var(--ch-accent-primary, #f68b1f); }
:where(.ms-root) #mar-item-modal-content .ms-mi-dates { display: flex; gap: var(--ch-space-sm); flex-wrap: wrap; margin-bottom: var(--ch-space-md); }
:where(.ms-root) #mar-item-modal-content .ms-mi-field { display: flex; flex-direction: column; gap: 4px; }
:where(.ms-root) #mar-item-modal-content .ms-mi-label { font-size: 12px; font-weight: 500; color: var(--ch-text-secondary); }
:where(.ms-root) #mar-item-start-date, :where(.ms-root) #mar-item-end-date { padding: 8px 12px; border: 1px solid var(--ch-border); border-radius: var(--ch-radius-md); font-family: var(--ch-font-primary); background: var(--ch-bg-input); color: var(--ch-text-primary); }
:where(.ms-root) #mar-item-modal-content .ms-mi-end-row { display: flex; align-items: center; gap: 4px; }
:where(.ms-root) #mar-item-modal-content .ms-mi-actions { display: flex; justify-content: flex-end; gap: var(--ch-space-sm); }
:where(.ms-root) #mar-item-cancel-btn { padding: 8px 20px; font-family: var(--ch-font-primary); background: var(--ch-bg-secondary); color: var(--ch-text-secondary); border: 1px solid var(--ch-border); border-radius: var(--ch-radius-md); cursor: pointer; }
:where(.ms-root) #mar-item-modal-content #mar-item-generate-btn { padding: 8px 20px; font-family: var(--ch-font-primary); background: var(--ch-accent-primary); color: var(--ch-text-inverse); border: none; border-radius: var(--ch-radius-md); cursor: pointer; font-weight: 500; }
