/**
 * Wizard Sub-Screen Styles
 * 
 * Desktop: Shows all sub-screens at once
 * Mobile: Shows one sub-screen at a time
 * 
 * Uses data-subscreen attributes to identify sub-screens
 */

/* =========================================================================
   BASE SUB-SCREEN STYLES
   ========================================================================= */

.wizard-subscreen {
  border: none;
  padding: 0;
  margin: 0 0 2rem 0;
  min-width: 0; /* Fix flexbox overflow */
}

.wizard-subscreen:last-of-type {
  margin-bottom: 0;
}

/* Fieldset reset */
fieldset.wizard-subscreen {
  border: none;
  padding: 0;
  margin: 0 0 2rem 0;
}

/* Legend as sub-screen title */
legend.subscreen-title,
.wizard-subscreen legend {
  display: block;
  width: 100%;
  padding: 0;
  margin: 0 0 0.5rem 0;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
  float: none; /* Reset legend float */
}

/* Sub-screen header */
.subscreen-header {
  margin-bottom: 1.5rem;
}

.subscreen-header h3,
.subscreen-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 0.5rem 0;
  padding: 0;
}

.subscreen-help {
  color: var(--text-muted);
  font-size: 0.9375rem;
  margin: 0 0 1rem 0;
  line-height: 1.5;
}

.subscreen-content {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* =========================================================================
   DESKTOP STYLES (min-width: 769px)
   Shows all sub-screens at once with dividers
   ========================================================================= */

@media (min-width: 769px) {
  .wizard-subscreen {
    display: block;
    padding-bottom: 2rem;
    border-bottom: 1px solid var(--border-light);
  }

  .wizard-subscreen:last-child {
    border-bottom: none;
    padding-bottom: 0;
  }

  /* Optional: Highlight current sub-screen */
  .wizard-subscreen.current {
    background: var(--bg-light);
    margin: 0 -1.5rem 2rem -1.5rem;
    padding: 1.5rem;
    border-radius: var(--radius-md);
    border-bottom: none;
  }

  /* Hide mobile-only components on desktop */
  .wizard-mobile-header,
  .wizard-action-bar {
    display: none !important;
  }

  /* Desktop step navigation stays visible */
  .step-navigation {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    margin-top: 2rem;
    padding-top: 2rem;
    border-top: 1px solid var(--border-light);
  }
}

/* =========================================================================
   MOBILE STYLES (max-width: 768px)
   Shows one sub-screen at a time
   ========================================================================= */

@media (max-width: 768px) {
  /* Hide all sub-screens by default */
  .wizard-subscreen {
    display: none;
    margin: 0;
    padding: 1rem;
    border-bottom: none;
  }

  /* Show only active sub-screen */
  .wizard-subscreen.active {
    display: block;
  }

  /* Conditional sub-screens - still hidden if dependency not met */
  .wizard-subscreen.active[data-depends-on]:not(.dependency-met) {
    display: none;
  }

  /* Sub-screen header styling for mobile */
  .subscreen-header h3,
  .subscreen-title {
    font-size: 1.375rem;
    text-align: center;
    margin-bottom: 0.75rem;
  }

  .subscreen-help {
    text-align: center;
    margin-bottom: 1.5rem;
  }

  /* Full-width inputs on mobile */
  .wizard-subscreen .form-input,
  .wizard-subscreen .form-select {
    width: 100%;
    font-size: 16px; /* Prevents iOS zoom on focus */
  }

  /* Larger touch targets for checkboxes */
  .wizard-subscreen .form-check,
  .wizard-subscreen .checkbox-label {
    padding: 1rem;
    background: var(--bg-light);
    border-radius: var(--radius-md);
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
    cursor: pointer;
    border: 1px solid var(--border-light);
    transition: border-color 0.2s ease, background-color 0.2s ease;
  }

  .wizard-subscreen .form-check:hover,
  .wizard-subscreen .checkbox-label:hover {
    border-color: var(--brand-ink);
  }

  .wizard-subscreen .form-check.checked,
  .wizard-subscreen .checkbox-label.checked {
    background: var(--brand-accent-soft);
    border-color: var(--brand-accent-strong);
  }

  .wizard-subscreen input[type="checkbox"],
  .wizard-subscreen input[type="radio"] {
    width: 24px;
    height: 24px;
    min-width: 24px;
    flex-shrink: 0;
    margin-top: 2px;
  }

  /* Hide desktop step navigation on mobile ONLY if action bar is present */
  /* Commented out until action bar is reliable - show both for now */
  /*
  .wizard-subscreen .step-navigation,
  .step-navigation {
    display: none !important;
  }
  */
  
  /* Make step-navigation mobile-friendly */
  .step-navigation {
    flex-direction: column !important;
    gap: 0.5rem !important;
    padding: 1rem 0 !important;
  }
  
  .step-navigation .btn {
    width: 100% !important;
    justify-content: center !important;
  }

  /* Add padding for fixed action bar */
  body.mode-wizard .wizard-step,
  body.mode-wizard .card-body {
    padding-bottom: 100px;
  }
}

/* =========================================================================
   MOBILE PROGRESS HEADER
   Sticky header showing progress through wizard
   ========================================================================= */

.wizard-mobile-header {
  display: none;
}

@media (max-width: 768px) {
  .wizard-mobile-header {
    display: block;
    position: sticky;
    top: 0;
    background: var(--bg-white);
    z-index: 100;
    padding: 1rem;
    border-bottom: 1px solid var(--border-light);
    box-shadow: var(--shadow-sm);
  }

  .wizard-mobile-header-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
  }

  .wizard-mobile-back {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 0.875rem;
    padding: 0.5rem;
    margin: -0.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.25rem;
    transition: color 0.2s ease;
  }

  .wizard-mobile-back:hover {
    color: var(--text-primary);
  }

  .wizard-mobile-back svg {
    width: 16px;
    height: 16px;
  }

  .wizard-mobile-title {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
  }

  .wizard-mobile-save {
    background: none;
    border: none;
    color: var(--brand-accent-strong);
    font-size: 0.875rem;
    font-weight: 500;
    padding: 0.5rem;
    margin: -0.5rem;
    cursor: pointer;
    transition: color 0.2s ease;
  }

  .wizard-mobile-save:hover {
    color: var(--brand-accent-dark);
  }

  /* Progress bar */
  .wizard-progress-bar {
    height: 4px;
    background: var(--border-light);
    border-radius: 2px;
    overflow: hidden;
  }

  .wizard-progress-fill {
    height: 100%;
    background: var(--brand-accent-strong);
    border-radius: 2px;
    transition: width 0.3s ease;
  }

  /* Progress text below bar */
  .wizard-progress-text {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
    margin-top: 0.5rem;
  }
}

/* =========================================================================
   MOBILE ACTION BAR
   Fixed bottom bar with Back and Continue buttons
   ========================================================================= */

.wizard-action-bar {
  display: none;
}

@media (max-width: 768px) {
  .wizard-action-bar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    /* Glass effect - matching .step-navigation */
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-top: 1px solid rgba(0, 0, 0, 0.08);
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.08);
    padding: 0.75rem 1rem;
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
    gap: 1rem;
    z-index: 9990;
    /* Smooth scroll-aware transitions */
    transition: transform 0.3s ease, opacity 0.3s ease;
  }
  
  /* Hidden state for scroll-aware behavior */
  .wizard-action-bar.nav-hidden {
    transform: translateY(100%);
    opacity: 0;
    pointer-events: none;
  }

  .wizard-action-bar .btn-back,
  #wizardBackBtn {
    flex: 0 0 auto;
    padding: 0.875rem 1.25rem;
    background: transparent;
    border: 1px solid var(--border-light);
    color: var(--text-secondary);
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
  }

  .wizard-action-bar .btn-back:hover,
  #wizardBackBtn:hover {
    border-color: var(--brand-ink);
    color: var(--text-primary);
  }

  .wizard-action-bar .btn-next,
  #wizardNextBtn {
    flex: 1;
    padding: 0.875rem 1.5rem;
    background: var(--brand-accent-strong);
    border: none;
    color: white;
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
  }

  .wizard-action-bar .btn-next:hover,
  #wizardNextBtn:hover {
    background: var(--brand-accent-dark);
  }

  .wizard-action-bar .btn-next:disabled,
  #wizardNextBtn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .wizard-action-bar .btn-next.has-errors,
  #wizardNextBtn.has-errors {
    background: var(--brand-accent);
  }
}

/* =========================================================================
   FIELD ERROR STYLES
   ========================================================================= */

.field-error,
.field-error:focus {
  border-color: var(--govuk-red) !important;
  box-shadow: 0 0 0 3px rgba(212, 53, 28, 0.15) !important;
}

.error-message {
  display: none;
  color: var(--govuk-red);
  font-size: 0.875rem;
  margin-top: 0.25rem;
  line-height: 1.4;
}

.error-message.visible,
.error-message[style*="display: block"] {
  display: block;
}

/* =========================================================================
   DEPENDENCY INDICATOR
   Shows when a sub-screen is locked/waiting for dependency
   ========================================================================= */

.wizard-subscreen[data-depends-on]:not(.dependency-met) {
  opacity: 0.5;
  pointer-events: none;
}

.wizard-subscreen[data-depends-on]:not(.dependency-met)::before {
  content: "Complete the previous step to unlock";
  display: block;
  text-align: center;
  padding: 1rem;
  background: var(--bg-subtle);
  border-radius: var(--radius-md);
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 1rem;
}

/* =========================================================================
   CONDITIONAL FIELD VISIBILITY
   ========================================================================= */

.wizard-subscreen [data-condition]:not(.condition-met) {
  display: none;
}

/* =========================================================================
   FORM ROW ADJUSTMENTS FOR MOBILE
   ========================================================================= */

@media (max-width: 768px) {
  .wizard-subscreen .form-row {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  .wizard-subscreen .form-row > .form-group {
    flex: none;
    width: 100%;
  }

  /* Input prefixes (£ symbol) */
  .wizard-subscreen .input-prefix {
    display: flex;
    align-items: stretch;
  }

  .wizard-subscreen .input-prefix .prefix {
    background: var(--bg-subtle);
    border: 1px solid var(--border-light);
    border-right: none;
    padding: 0.75rem 1rem;
    font-size: 1rem;
    color: var(--text-secondary);
    border-radius: var(--radius-md) 0 0 var(--radius-md);
    display: flex;
    align-items: center;
  }

  .wizard-subscreen .input-prefix .form-input {
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
    flex: 1;
  }
}

/* =========================================================================
   TRANSITION ANIMATIONS
   ========================================================================= */

@media (max-width: 768px) {
  .wizard-subscreen {
    animation: fadeIn 0.2s ease-out;
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
}

/* =========================================================================
   STEPPER ADJUSTMENTS FOR MOBILE
   Hide stepper on mobile, use progress bar instead
   ========================================================================= */

@media (max-width: 768px) {
  .stepper-wrapper {
    display: none;
  }
}

/* =========================================================================
   SAFE AREA INSETS (for notched phones)
   ========================================================================= */

@supports (padding-bottom: env(safe-area-inset-bottom)) {
  @media (max-width: 768px) {
    .wizard-action-bar {
      padding-bottom: calc(1rem + env(safe-area-inset-bottom));
    }

    body.mode-wizard .wizard-step,
    body.mode-wizard .card-body {
      padding-bottom: calc(100px + env(safe-area-inset-bottom));
    }
  }
}

/* =========================================================================
   CRITICAL SCROLL FIX
   Ensure form inputs don't block touch scrolling on iOS
   ========================================================================= */

.wizard-subscreen input,
.wizard-subscreen select,
.wizard-subscreen textarea {
    touch-action: auto !important;
}

/* Container-level fix: Allow vertical scrolling on all wizard containers */
.wizard-step,
.wizard-subscreen,
.subscreen-content,
.form-row,
.form-group,
.form-label,
label,
fieldset.wizard-subscreen {
    touch-action: pan-y !important;
    -webkit-overflow-scrolling: touch;
}
