/* ==========================================================================
   MODAL COMPONENTS
   ========================================================================== */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  z-index: 2000;
  opacity: 0;
  visibility: hidden;
  transition: all var(--transition);
  overscroll-behavior: contain;
}

.modal-overlay.show {
  opacity: 1;
  visibility: visible;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 1200px;
  transform: scale(0.95);
  transition: transform var(--transition);
}

.modal-overlay.show .modal { 
  transform: scale(1); 
}

.modal-sm {
  max-width: 440px;
}

.modal-lg {
  max-width: 800px;
}

.modal-xl { 
  width: 95vw;
  max-width: 1200px;
}

.modal-xxl {
  width: 100%;
  max-width: 100% !important;
}

.modal-header {
  padding: var(--space-4);
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.modal-title {
  font-size: 1.25rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.modal-header-right {
  display: flex;
  align-items: center;
  gap: var(--space-4);
}

.modal-header-total {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--gray-100);
  border-radius: var(--radius);
}

.modal-header-total-label {
  font-size: 0.875rem;
  color: var(--text-secondary);
  font-weight: 500;
}

.modal-header-total-value {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--primary);
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text-secondary);
  cursor: pointer;
  padding: var(--space-2);
  border-radius: var(--radius);
  transition: all var(--transition);
  line-height: 1;
}

.modal-close:hover { 
  background: var(--gray-100); 
}

.modal-body {
  padding: var(--space-4);
  overflow-y: auto;
  flex: 1;
  overscroll-behavior: contain;
}

.modal-footer {
  padding: var(--space-4);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  background: var(--gray-50);
  flex-shrink: 0;
}

/* NOTE: forms must be placed INSIDE .modal-body, never wrapping both
   .modal-body and .modal-footer. See CLAUDE.md modal patterns.
   The rules below are a defensive fallback: when a form *does* wrap both,
   it must behave like a flex column so the footer stays visible. */
.modal form {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.modal-body .form-fieldset:last-of-type {
  margin-bottom: 0;
}

/* ==========================================================================
   STEP-UP MODAL (sudo mode password prompt)
   ========================================================================== */

.step-up-help {
  margin: 0 0 var(--space-3);
  color: var(--text-secondary, var(--gray-500, #6b7280));
  font-size: 0.875rem;
}

.step-up-error {
  margin-top: var(--space-2);
  color: var(--color-danger, #c62828);
  font-size: 0.875rem;
}

/* ==========================================================================
   TABBED MODALS — fixed height so tab switches don't resize the modal
   ========================================================================== */

.modal-tabbed {
  height: 90vh;
}

.modal-tabbed .modal-body {
  flex: 1;
  min-height: 0; /* allow flex child to shrink for overflow scroll */
}

/* Tab navigation bar — sits between header and body as a direct flex child */
.pack-modal-tab-nav {
  display: flex;
  gap: 0;
  padding: 0 var(--space-4);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.pack-modal-tab-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-secondary);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  white-space: nowrap;
  transition: all var(--transition);
}

.pack-modal-tab-btn:hover {
  color: var(--primary);
  background: var(--gray-50);
}

.pack-modal-tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  background: var(--gray-50);
}

/* Tab panes — only the active one is visible */
.pack-modal-tab-pane {
  display: none;
}

.pack-modal-tab-pane.active {
  display: block;
}

/* Enhanced Help Modal */
#conditionHelpModal .modal {
  max-width: 900px;
  max-height: 80vh;
  overflow-y: auto;
}

#conditionHelpModal .modal-body h4 {
  color: #1f2937;
  font-size: 16px;
  font-weight: 600;
  margin-top: 24px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid #e5e7eb;
}

#conditionHelpModal .modal-body h4:first-child {
  margin-top: 0;
}

#conditionHelpModal .modal-body code {
  background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
  padding: 3px 8px;
  border-radius: 4px;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 13px;
  color: #475569;
  border: 1px solid #cbd5e1;
  font-weight: 500;
}

/* Grid Utilities for Help Modal */
#conditionHelpModal .grid {
  display: grid;
}

#conditionHelpModal .grid-cols-2 {
  grid-template-columns: repeat(2, 1fr);
}

#conditionHelpModal .gap-2 {
  gap: 8px;
}

#conditionHelpModal .gap-6 {
  gap: 24px;
}

/* Modal accessibility */
.modal-open .entity-dropdown {
  z-index: 1060; /* Higher than modal backdrop */
}

/* Mobile Responsive */
@media (max-width: 768px) {
  .modal-lg,
  .modal-xl {
    max-width: 95vw;
    margin: var(--space-4);
  }
  
  #conditionHelpModal .grid-cols-2 {
    grid-template-columns: 1fr;
  }
}