/* ============================================
   MOBILE OPTIMIZATIONS FOR RESUME BUILDER
   Comprehensive mobile-first approach
   ============================================ */

/* ============================================
   CSS CUSTOM PROPERTIES FOR MOBILE
   ============================================ */
:root {
  --mobile-header-height: 56px;
  --mobile-bottom-bar-height: 64px;
  --mobile-safe-area-bottom: env(safe-area-inset-bottom, 0px);
  --mobile-tap-target: 48px;
  --mobile-spacing-xs: 8px;
  --mobile-spacing-sm: 12px;
  --mobile-spacing-md: 16px;
  --mobile-spacing-lg: 24px;
}

/* ============================================
   SMALL MOBILE SCREENS (< 480px)
   ============================================ */
@media (max-width: 479px) {

  /* --- CONTAINER & LAYOUT --- */
  .container {
    padding-left: var(--mobile-spacing-sm);
    padding-right: var(--mobile-spacing-sm);
  }

  .resume-builder-container {
    padding-bottom: calc(var(--mobile-bottom-bar-height) + var(--mobile-safe-area-bottom) + 20px);
  }

  /* --- HERO SECTION: ULTRA COMPACT --- */
  .hero-section {
    padding: var(--mobile-spacing-md) 0 var(--mobile-spacing-lg) !important;
  }

  .hero-section h2 {
    font-size: 1.5rem !important;
    line-height: 1.2;
    margin-bottom: 0.25rem !important;
  }

  .hero-section p {
    font-size: 0.875rem !important;
    line-height: 1.4;
  }

  /* --- STEPPER: ICON-ONLY MODE WITH PROGRESS LINE --- */
  .stepper-expressive {
    /* Keep sticky behavior from resume-builder.css */
    margin: 0 0 var(--mobile-spacing-lg) 0;
    padding: var(--mobile-spacing-xs);
    justify-content: space-around;
    /* Ensure it stays on top on mobile */
    top: 0;
    z-index: 1000;
  }

  .step-item {
    flex: 0 0 auto;
    min-width: auto;
  }

  .step-indicator {
    width: 36px;
    height: 36px;
    font-size: 0.875rem;
  }

  .step-label {
    font-size: 0.65rem;
    max-width: 50px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .step-divider {
    flex: 1;
    min-width: 8px;
    margin: 0 2px;
  }

  /* Active step gets enlarged indicator */
  .step-item.is-active .step-indicator {
    transform: scale(1.15);
  }

  /* --- CARDS: FULL BLEED ON SMALL SCREENS --- */
  .card {
    margin-left: calc(-1 * var(--mobile-spacing-sm));
    margin-right: calc(-1 * var(--mobile-spacing-sm));
    border-radius: 0;
    padding: var(--mobile-spacing-md);
    border-left: none;
    border-right: none;
  }

  .card h2 {
    font-size: 1.25rem;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
  }

  /* --- TEMPLATE CATALOG: SINGLE COLUMN --- */
  .template-filters {
    overflow-x: auto;
    flex-wrap: nowrap;
    padding-bottom: 8px;
    margin-bottom: 0.75rem;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .template-filters::-webkit-scrollbar {
    display: none;
  }

  .filter-tab {
    flex-shrink: 0;
    padding: 0.375rem 0.75rem;
    font-size: 0.8rem;
  }

  .template-search input {
    font-size: 16px; /* Prevents iOS zoom on focus */
    padding: 0.875rem;
  }

  /* CRITICAL FIX: Template Catalog Grid - MUST be 1 column on small screens */
  .template-catalog-section .template-grid,
  #templateGrid.template-grid {
    grid-template-columns: 1fr !important;
    gap: 0.75rem;
    max-height: 55vh;
  }

  .template-card {
    padding: 1rem;
    flex-direction: row;
    align-items: center;
    gap: 1rem;
  }

  .template-card__visual-preview {
    width: 80px;
    height: 110px;
    flex-shrink: 0;
    margin-bottom: 0;
  }

  .template-card__content {
    flex: 1;
    min-width: 0;
  }

  .template-card__title {
    font-size: 1rem;
  }

  .template-card__apply {
    display: none; /* Entire card is tappable */
  }

  .template-skip {
    margin-top: 1rem;
    padding-top: 1rem;
  }

  /* --- UPLOAD SECTION: COMPACT BOXES --- */
  .upload-options {
    gap: 12px;
  }

  .upload-box,
  .linkedin-box {
    min-height: 120px;
    padding: var(--mobile-spacing-md);
  }

  .upload-icon {
    font-size: 32px;
    margin-bottom: 8px;
  }

  .upload-title {
    font-size: 0.95rem;
  }

  .upload-hint {
    font-size: 0.8rem;
    margin-top: 6px;
  }

  .upload-meta {
    font-size: 0.75rem;
  }

  .divider {
    padding: 8px 0;
  }

  .paste-toggle {
    padding: 12px 16px;
    font-size: 0.9rem;
  }

  .paste-box {
    padding: 12px;
  }

  .paste-actions {
    flex-direction: column;
    gap: 8px;
  }

  .paste-actions .btn {
    width: 100%;
    justify-content: center;
  }

  .demo-actions {
    flex-direction: column;
    gap: 8px;
    margin-top: 16px;
    padding-top: 12px;
  }

  .demo-buttons {
    width: 100%;
    justify-content: center;
  }

  /* --- PROCESSING/WORKFLOW SECTION --- */
  .workflow-step {
    border-radius: 8px;
  }

  .workflow-header {
    padding: 12px 14px;
    gap: 10px;
  }

  .workflow-number {
    width: 24px;
    height: 24px;
    font-size: 0.75rem;
  }

  .workflow-info h3 {
    font-size: 0.95rem;
  }

  .workflow-info p {
    font-size: 0.8rem;
  }

  .workflow-content {
    padding: 12px 14px;
  }

  .processing-section textarea.code-block {
    font-size: 0.75rem;
    height: 120px;
  }

  /* --- EDITOR SECTION: MOBILE-OPTIMIZED --- */
  .resume-preview.card {
    padding: 0;
    background: var(--md-sys-color-surface-variant);
    margin-bottom: 0;
  }

  .preview-header {
    padding: var(--mobile-spacing-md);
    gap: 10px;
    /* Disable sticky header to avoid conflict with sticky stepper */
    position: relative;
    top: auto;
    z-index: 1;
    background: #525659; /* Solid bg fallback */
  }

  .preview-header h2 {
    font-size: 1.1rem;
    display: none; /* Hide on small screens */
  }

  /* --- PREVIEW CONTROLS: HORIZONTAL SCROLL TOOLBAR --- */
  .preview-controls {
    width: 100%;
    margin-top: 0;
  }

  .preview-controls .icon-action-group {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 8px;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none; /* Firefox */
  }

  .preview-controls .icon-action-group::-webkit-scrollbar {
    display: none; /* Chrome/Safari */
  }

  /* Chip-style buttons for toolbar */
  .preview-controls .icon-action {
    flex: 0 0 auto;
    min-width: auto;
    width: auto;
    flex-direction: row;
    padding: 6px 12px;
    min-height: 36px;
    border-radius: 18px;
    gap: 6px;
  }

  /* Ensure icons and text fit well */
  .preview-controls .icon-action__icon {
    width: 18px;
    height: 18px;
  }

  .preview-controls .icon-action__icon svg {
    width: 16px;
    height: 16px;
  }

  .preview-controls .icon-action__label {
    font-size: 0.75rem;
    display: inline-block;
  }

  /* --- WYSIWYG TOOLBAR: COMPACT & SCROLLABLE --- */
  .wysiwyg-toolbar {
    /* Disable sticky toolbar to avoid conflict with sticky stepper */
    position: relative;
    top: auto;
    z-index: 1;
    margin: 0;
    padding: 8px 12px;
    border-radius: 0;
    gap: 6px;
    overflow-x: auto;
    flex-wrap: nowrap;
    background: var(--md-sys-color-surface);
    border-bottom: 1px solid var(--border-color);
  }

  .toolbar-group {
    flex-shrink: 0;
  }

  .toolbar-btn {
    width: 36px;
    height: 36px;
    min-width: 36px;
  }

  .toolbar-divider {
    height: 20px;
    margin: 0 2px;
  }

  .toolbar-spacer {
    display: none;
  }

  .save-indicator {
    flex-shrink: 0;
    padding: 4px 8px;
    font-size: 0.7rem;
  }

  /* --- PAPER PREVIEW: FIT TO SCREEN --- */
  .preview-wrapper {
    padding: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .paper-preview {
    min-width: 100%;
    width: 100%;
    min-height: auto;
    padding: 16px !important;
    font-size: 0.9em;
    transform-origin: top center;
  }

  .paper-preview .resume-document {
    padding: 0 !important;
  }

  .paper-preview .resume-header {
    padding: 16px !important;
  }

  .paper-preview .resume-name {
    font-size: 1.5rem !important;
    word-break: break-word;
  }

  .paper-preview .resume-contact {
    flex-direction: column;
    gap: 4px;
    font-size: 0.8rem;
  }

  .paper-preview .resume-contact span:not(:last-child)::after {
    display: none;
  }

  .paper-preview .resume-section-title {
    font-size: 0.9rem;
  }

  .paper-preview .resume-item-title {
    font-size: 0.95rem;
  }

  .paper-preview .resume-item-subtitle,
  .paper-preview .resume-item-date {
    font-size: 0.85rem;
  }

  .paper-preview .resume-document p,
  .paper-preview .resume-document li {
    font-size: 0.85rem;
  }

  /* --- DESIGN PANEL: BOTTOM SHEET --- */
  .design-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 85vh;
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -4px 30px rgba(0,0,0,0.25);
    margin: 0;
    z-index: 1000;
    padding: var(--mobile-spacing-md);
    padding-bottom: calc(var(--mobile-spacing-md) + var(--mobile-safe-area-bottom));
    overflow-y: auto;
  }

  .design-panel__header {
    position: relative; /* Disable sticky to prevent stacking issues */
    top: auto;
    background: var(--bg-color);
    padding-bottom: 12px;
    margin-bottom: 12px;
    z-index: 1;
  }

  .design-panel__header::before {
    content: '';
    position: absolute;
    top: -8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    background: var(--border-color);
    border-radius: 2px;
  }

  .design-tabs {
    gap: 6px;
    padding-bottom: 0;
    margin-bottom: 16px;
  }

  .design-tab {
    padding: 8px 14px;
    font-size: 0.85rem;
  }

  /* Design Panel Template Grid - Keep 3 columns for layout options (Modern/Classic/Minimal) */
  .design-panel .template-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }

  .template-option {
    padding: 6px;
  }

  .template-option span {
    font-size: 0.75rem;
  }

  .color-presets {
    gap: 8px;
  }

  .color-preset {
    width: 36px;
    height: 36px;
  }

  /* --- ATS PANEL: BOTTOM SHEET --- */
  .ats-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 75vh;
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -4px 30px rgba(0,0,0,0.25);
    margin: 0;
    z-index: 1000;
    padding: var(--mobile-spacing-md);
    padding-bottom: calc(var(--mobile-spacing-md) + var(--mobile-safe-area-bottom));
    overflow-y: auto;
  }

  .ats-panel__header::before {
    content: '';
    position: absolute;
    top: -8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    background: var(--border-color);
    border-radius: 2px;
  }

  /* --- DRAG/EDIT CONTROLS: ALWAYS VISIBLE --- */
  .section-header-controls,
  .resume-item-controls {
    opacity: 1;
    position: static;
    margin-top: 8px;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 6px;
  }

  .btn-section-move,
  .btn-section-delete,
  .btn-item-move,
  .btn-icon-danger {
    width: 32px;
    height: 32px;
  }

  .section-drag-handle,
  .item-drag-handle {
    display: none;
  }

  .resume-section-container.draggable-section,
  .relative-group {
    padding-left: 0;
  }

  .section-controls {
    opacity: 1;
    margin-top: 10px;
  }

  .btn-add-item {
    width: 100%;
    padding: 10px;
    font-size: 0.85rem;
  }

  .add-section-container {
    margin-top: 16px;
    padding-top: 16px;
  }

  .add-section-select {
    width: 100%;
    padding: 12px;
  }

  /* --- FOOTER: COMPACT --- */
  .site-footer {
    padding: 1rem 0 !important;
    margin-bottom: calc(var(--mobile-bottom-bar-height) + var(--mobile-safe-area-bottom));
  }

  .site-footer p {
    font-size: 0.75rem !important;
  }
}

/* ============================================
   MEDIUM MOBILE SCREENS (480px - 767px)
   ============================================ */
@media (min-width: 480px) and (max-width: 767px) {

  /* Hero Section */
  .hero-section {
    padding: 1.5rem 0 2rem !important;
  }

  .hero-section h2 {
    font-size: 1.75rem !important;
  }

  /* Stepper */
  .stepper-expressive {
    margin: 1.5rem 0 2rem;
  }

  .step-label {
    font-size: 0.75rem;
    max-width: 70px;
  }

  /* Template Catalog Grid - 2 columns on medium mobile screens */
  .template-catalog-section .template-grid,
  #templateGrid.template-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 0.875rem;
  }

  /* Design Panel Template Grid - Keep 3 columns */
  .design-panel .template-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }

  .template-card {
    flex-direction: column;
  }

  .template-card__visual-preview {
    width: 100%;
    height: 160px;
    margin-bottom: 0.875rem;
  }

  /* Upload Options */
  .upload-box,
  .linkedin-box {
    min-height: 150px;
    padding: 24px;
  }

  .upload-icon {
    font-size: 40px;
  }

  /* Preview Controls - Horizontal Scroll */
  .preview-controls .icon-action-group {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 10px;
    padding-bottom: 4px;
    scrollbar-width: none;
  }

  .preview-controls .icon-action-group::-webkit-scrollbar {
    display: none;
  }

  .preview-controls .icon-action {
    flex: 0 0 auto;
    min-width: auto;
    width: auto;
    flex-direction: row;
    padding: 8px 14px;
    min-height: 40px;
    border-radius: 20px;
  }

  /* Paper Preview */
  .paper-preview {
    padding: 24px !important;
  }

  .paper-preview .resume-name {
    font-size: 1.6rem !important;
  }

  /* Design Panel */
  .design-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 75vh;
    border-radius: 20px 20px 0 0;
    margin: 0;
    z-index: 1000;
  }

  /* Cards */
  .card {
    padding: 20px;
    border-radius: 12px;
  }
}

/* ============================================
   ALL MOBILE SCREENS (< 768px)
   ============================================ */
@media (max-width: 767px) {

  /* --- BASE TOUCH OPTIMIZATIONS --- */

  /* Minimum tap targets */
  button,
  .btn,
  a.btn,
  input[type="submit"],
  input[type="button"],
  .filter-tab,
  .template-card,
  .step-item,
  .design-tab {
    min-height: 44px;
  }

  /* Input zoom prevention (iOS) */
  input[type="text"],
  input[type="email"],
  input[type="search"],
  textarea,
  select {
    font-size: 16px;
  }

  /* Smooth scrolling */
  html {
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
  }

  /* Prevent text selection on interactive elements */
  button,
  .btn,
  .step-item,
  .template-card,
  .filter-tab {
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }

  /* Active state feedback */
  button:active,
  .btn:active,
  .template-card:active,
  .filter-tab:active,
  .step-item:active {
    transform: scale(0.98);
    transition: transform 0.1s ease;
  }

  /* --- APP BAR / HEADER --- */
  .app-bar-top {
    position: relative;
    z-index: 1000;
  }

  .app-bar-content {
    height: 56px;
  }

  .brand-lockup .icon-base {
    width: 28px !important;
    height: 28px !important;
  }

  .brand-text h1 {
    font-size: 1.1rem !important;
  }

  /* --- LEGACY STEPPER STYLES (if used) --- */
  .stepper {
    display: flex;
    overflow-x: auto;
    padding-bottom: 10px;
    gap: 12px;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .stepper::-webkit-scrollbar {
    display: none;
  }

  .step {
    flex: 0 0 auto;
    min-width: 120px;
    scroll-snap-align: start;
    padding: 10px;
  }

  /* --- SECTION NAVIGATION --- */
  .section-nav {
    margin-bottom: 12px;
  }

  .section-nav .btn {
    padding: 10px 14px;
    font-size: 0.9rem;
  }

  /* --- WORKFLOW STEPS --- */
  .workflow-steps {
    gap: 12px;
  }

  /* --- TIPS SECTION --- */
  .tips-section {
    margin-top: 16px;
  }

  .tips-section summary {
    padding: 10px 14px;
    font-size: 0.9rem;
  }

  .tips-section ul {
    padding: 0 14px 14px 28px;
    font-size: 0.85rem;
  }

  /* --- INLINE FIELDS --- */
  .inline-field {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  /* --- FORM GROUPS --- */
  .form-group {
    margin-bottom: 14px;
  }

  .form-group label {
    font-size: 0.85rem;
    margin-bottom: 6px;
  }

  .form-group input,
  .form-group textarea,
  .form-group select {
    padding: 12px;
    font-size: 16px; /* Prevent iOS zoom */
  }

  /* Range sliders */
  input[type="range"] {
    height: 32px;
    margin-top: 4px;
  }

  /* --- REFERENCES GRID --- */
  .references-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  /* --- TEMPLATE CARD META --- */
  .template-card__meta {
    padding-top: 0.5rem;
  }

  .meta-badge {
    font-size: 0.6rem;
    padding: 0.1rem 0.4rem;
  }

  /* --- PRINT HIDDEN (ensure nothing bleeds) --- */
  @media print {
    .mobile-only {
      display: none !important;
    }
  }
}

/* ============================================
   LANDSCAPE MOBILE OPTIMIZATIONS
   ============================================ */
@media (max-width: 767px) and (orientation: landscape) {

  /* Reduce vertical space in landscape */
  .hero-section {
    padding: 0.75rem 0 1rem !important;
  }

  .hero-section h2 {
    font-size: 1.25rem !important;
  }

  .hero-section p {
    display: none; /* Hide tagline in landscape to save space */
  }

  .stepper-expressive {
    margin: 0.5rem 0 1rem;
  }

  .step-indicator {
    width: 28px;
    height: 28px;
    font-size: 0.75rem;
  }

  .step-label {
    display: none; /* Hide labels in landscape */
  }

  .step-divider {
    margin-bottom: 0;
    top: 0;
  }

  /* Template Catalog Grid - Horizontal scroll in landscape for better usability */
  .template-catalog-section .template-grid,
  #templateGrid.template-grid {
    display: flex !important;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 12px;
    max-height: none;
    grid-template-columns: unset !important;
  }

  .template-catalog-section .template-card,
  #templateGrid .template-card {
    flex: 0 0 200px;
    flex-direction: column;
  }

  /* Design Panel Template Grid - Keep grid layout */
  .design-panel .template-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }

  /* Upload boxes side by side */
  .upload-options {
    flex-direction: row;
    gap: 12px;
  }

  .upload-box,
  .linkedin-box {
    min-height: 100px;
    flex: 1;
  }

  /* Preview header inline */
  .preview-header {
    flex-direction: row;
    align-items: center;
    padding: 8px 12px;
  }

  .preview-header h2 {
    display: block;
    font-size: 1rem;
    margin: 0;
    flex-shrink: 0;
  }

  .preview-controls {
    display: flex;
    flex-wrap: nowrap;
    gap: 6px;
    overflow-x: auto;
  }

  .preview-controls .btn {
    flex: 0 0 auto;
    min-height: 36px;
    padding: 6px 10px;
    font-size: 0.75rem;
  }

  /* Bottom sheets smaller */
  .design-panel,
  .ats-panel {
    max-height: 60vh;
  }
}

/* ============================================
   HIGH DPI / RETINA OPTIMIZATIONS
   ============================================ */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {

  /* Thinner borders on retina */
  .card,
  .template-card,
  .upload-box,
  .linkedin-box,
  .workflow-step {
    border-width: 0.5px;
  }

  .step-divider {
    height: 1px;
  }
}

/* ============================================
   REDUCED MOTION PREFERENCES
   ============================================ */
@media (prefers-reduced-motion: reduce) {

  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  html {
    scroll-behavior: auto;
  }

  button:active,
  .btn:active,
  .template-card:active {
    transform: none;
  }
}

/* ============================================
   DARK MODE MOBILE ADJUSTMENTS
   ============================================ */
@media (max-width: 767px) and (prefers-color-scheme: dark) {

  .preview-header {
    background: rgba(30, 41, 59, 0.98);
  }

  .design-panel,
  .ats-panel {
    box-shadow: 0 -4px 30px rgba(0, 0, 0, 0.5);
  }

  .design-panel__header::before,
  .ats-panel__header::before {
    background: rgba(255, 255, 255, 0.2);
  }
}

/* ============================================
   MOBILE OVERLAY (for bottom sheets)
   ============================================ */
.mobile-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 999;
  opacity: 0;
  transition: opacity 0.3s ease;
  -webkit-tap-highlight-color: transparent;
}

.mobile-overlay.active {
  display: block;
  opacity: 1;
}

@media (max-width: 767px) {
  .mobile-overlay {
    display: none;
  }

  .mobile-overlay.active {
    display: block;
  }
}

/* Hide overlay on desktop */
@media (min-width: 768px) {
  .mobile-overlay {
    display: none !important;
  }
}

/* ============================================
   iOS SAFE AREA SUPPORT
   ============================================ */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  @media (max-width: 767px) {

    .resume-builder-container {
      padding-bottom: calc(80px + env(safe-area-inset-bottom));
    }

    .design-panel,
    .ats-panel {
      padding-bottom: calc(16px + env(safe-area-inset-bottom));
    }

    .site-footer {
      padding-bottom: env(safe-area-inset-bottom) !important;
    }
  }
}
