/* ==========================================================
   responsive.css — Media Queries & Mobile Adaptations
   ========================================================== */

/* ============================================================
   LARGE TABLETS / SMALL DESKTOPS (max-width: 1024px)
   ============================================================ */
@media (max-width: 1024px) {
  /* Sidebar */
  .sidebar {
    width: var(--sidebar-collapsed);
    overflow: visible;
  }

  .sidebar-app-name,
  .sidebar-form-selector,
  .tab-btn span,
  .sidebar-user .user-meta,
  .sidebar-user .btn-ghost span,
  #activeFormBadge {
    display: none;
  }

  .sidebar-header {
    padding: var(--space-md);
    justify-content: center;
  }

  .sidebar-logo {
    justify-content: center;
  }

  .tab-btn {
    justify-content: center;
    padding: 12px;
    border-left-width: 0;
    border-bottom: 2px solid transparent;
  }

  .tab-btn.active {
    border-left-color: transparent;
    border-bottom-color: var(--color-primary);
  }

  .tab-btn .icon {
    width: 20px;
    height: 20px;
  }

  .sidebar-user {
    align-items: center;
    padding: var(--space-sm);
  }

  .sidebar-user-info {
    justify-content: center;
  }

  .sidebar-user .btn-ghost {
    padding: 6px;
    justify-content: center;
  }

  /* Main Content */
  .main-content {
    margin-left: var(--sidebar-collapsed);
    padding: var(--space-base);
  }

  /* Builder: stack to 2 columns */
  .builder-layout {
    grid-template-columns: 200px 1fr;
  }

  .builder-sidebar-right {
    grid-column: 1 / -1;
    position: static;
    max-height: none;
  }

  /* Summary cards */
  .summary-cards-grid {
    grid-template-columns: 1fr;
  }
}

/* ============================================================
   TABLETS (max-width: 768px)
   ============================================================ */
@media (max-width: 768px) {
  /* Sidebar: full collapse to off-screen */
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: var(--sidebar-width);
    transform: translateX(-100%);
    transition: transform var(--transition-slow);
    z-index: var(--z-overlay);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  /* Restore sidebar inner elements when open */
  .sidebar.open .sidebar-app-name,
  .sidebar.open .sidebar-form-selector,
  .sidebar.open .tab-btn span,
  .sidebar.open .sidebar-user .user-meta,
  .sidebar.open .sidebar-user .btn-ghost span {
    display: block;
  }

  .sidebar.open .tab-btn {
    justify-content: flex-start;
    padding: 10px var(--space-base);
    border-left: 3px solid transparent;
    border-bottom: none;
  }

  .sidebar.open .tab-btn.active {
    border-left-color: var(--color-primary);
    border-bottom-color: transparent;
  }

  .sidebar.open .sidebar-header {
    padding: var(--space-lg) var(--space-base);
  }

  .sidebar.open .sidebar-logo {
    justify-content: flex-start;
  }

  .sidebar.open .sidebar-user {
    align-items: flex-start;
    padding: var(--space-md) var(--space-base);
  }

  .sidebar.open .sidebar-user-info {
    justify-content: flex-start;
  }

  .sidebar.open .sidebar-user .btn-ghost {
    justify-content: flex-start;
    padding: 6px var(--space-sm);
  }

  /* Sidebar backdrop */
  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: calc(var(--z-overlay) - 1);
  }

  .sidebar-backdrop.visible {
    display: block;
  }

  /* Main Content: full width */
  .main-content {
    margin-left: 0;
    padding: var(--space-base);
  }

  /* Content header: stack */
  .content-header {
    flex-direction: column;
    gap: var(--space-sm);
  }

  .action-bar {
    width: 100%;
    flex-wrap: wrap;
  }

  /* Mobile menu toggle button (added by JS) */
  .mobile-menu-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    bottom: var(--space-lg);
    left: var(--space-lg);
    width: 48px;
    height: 48px;
    background: var(--color-primary);
    color: var(--color-text-inverse);
    border: none;
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-lg);
    z-index: calc(var(--z-overlay) - 2);
    cursor: pointer;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
  }

  .mobile-menu-toggle:hover {
    transform: scale(1.05);
    box-shadow: var(--shadow-xl);
  }

  .mobile-menu-toggle:active {
    transform: scale(0.95);
  }

  /* Builder: single column */
  .builder-layout {
    grid-template-columns: 1fr;
  }

  .builder-sidebar-left {
    position: static;
    max-height: none;
    flex-direction: row;
    flex-wrap: wrap;
    gap: var(--space-sm);
  }

  .builder-palette,
  .builder-section-list {
    flex: 1;
    min-width: 200px;
  }

  .builder-sidebar-right {
    position: static;
    max-height: none;
    grid-column: auto;
  }

  .builder-canvas {
    min-height: 300px;
  }

  /* Form field grids: collapse */
  .form-field-grid.cols-3,
  .form-field-grid.cols-4 {
    grid-template-columns: 1fr 1fr;
  }

  .canvas-field-grid.cols-3,
  .canvas-field-grid.cols-4 {
    grid-template-columns: 1fr 1fr;
  }

  /* Stat cards: 2 per row */
  .stat-cards-grid {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  }

  /* Records toolbar: stack */
  .records-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .records-search-group {
    max-width: 100%;
  }

  .records-filter-dropdowns {
    flex-wrap: wrap;
  }

  .records-actions {
    margin-left: 0;
    justify-content: flex-end;
  }

  /* Filter bar: stack */
  .filter-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-group {
    min-width: auto;
  }

  .filter-actions {
    margin-left: 0;
    justify-content: flex-end;
  }

  /* Pagination */
  .pagination {
    gap: var(--space-sm);
  }

  /* Modals: nearly full width */
  .modal-overlay {
    padding: var(--space-md);
  }

  .modal {
    max-width: 100%;
    max-height: 95vh;
  }

  .modal-sm {
    max-width: 100%;
  }

  .modal-lg {
    max-width: 100%;
  }

  .modal-xl {
    max-width: 100%;
    height: 95vh;
  }

  .modal-header {
    padding: var(--space-md) var(--space-base);
  }

  .modal-body {
    padding: var(--space-base);
  }

  .modal-footer {
    padding: var(--space-md) var(--space-base);
    flex-wrap: wrap;
  }

  /* Preview device switcher */
  .preview-device-switcher .btn span {
    display: none;
  }

  /* Forms grid */
  .forms-grid {
    grid-template-columns: 1fr;
  }

  /* Settings info grid */
  .settings-info-grid {
    grid-template-columns: 1fr 1fr;
  }

  /* Toast */
  .toast-container {
    top: var(--space-md);
    right: var(--space-md);
    left: var(--space-md);
  }

  .toast {
    min-width: auto;
    max-width: 100%;
  }

  /* Login card */
  .login-card {
    padding: var(--space-lg);
  }

  /* Table: allow horizontal scroll */
  .data-table thead th,
  .data-table tbody td {
    padding: 8px 10px;
    font-size: var(--font-size-sm);
  }
}

/* ============================================================
   MOBILE (max-width: 480px)
   ============================================================ */
@media (max-width: 480px) {
  /* Main content */
  .main-content {
    padding: var(--space-md);
  }

  /* Typography */
  .content-title {
    font-size: var(--font-size-lg);
  }

  h2 {
    font-size: var(--font-size-lg);
  }

  h3 {
    font-size: var(--font-size-md);
  }

  /* Stat cards: single column */
  .stat-cards-grid {
    grid-template-columns: 1fr;
  }

  .stat-card {
    padding: var(--space-md);
  }

  .stat-card-icon {
    width: 40px;
    height: 40px;
  }

  .stat-card-icon .icon {
    width: 20px;
    height: 20px;
  }

  .stat-card-value {
    font-size: var(--font-size-lg);
  }

  /* Summary cards: full width */
  .summary-cards-grid {
    grid-template-columns: 1fr;
  }

  /* Form field grids: single column */
  .form-field-grid.cols-2,
  .form-field-grid.cols-3,
  .form-field-grid.cols-4 {
    grid-template-columns: 1fr;
  }

  .canvas-field-grid.cols-2,
  .canvas-field-grid.cols-3,
  .canvas-field-grid.cols-4 {
    grid-template-columns: 1fr;
  }

  /* Form row 2: single column */
  .form-row-2 {
    grid-template-columns: 1fr;
  }

  /* Buttons: full width on very small screens */
  .action-bar .btn {
    flex: 1;
    min-width: 0;
  }

  /* Table: hide non-essential columns via data attribute */
  .data-table .hide-mobile {
    display: none;
  }

  .data-table thead th,
  .data-table tbody td {
    padding: 6px 8px;
    font-size: var(--font-size-xs);
  }

  /* Linked fields: stack */
  .linked-fields-row {
    flex-direction: column;
    gap: var(--space-sm);
  }

  .linked-fields-row .btn-icon {
    margin-top: 0;
    align-self: flex-end;
  }

  /* Multi-value rows */
  .multi-value-row {
    flex-direction: column;
    align-items: stretch;
  }

  .multi-value-row .btn-icon {
    align-self: flex-end;
  }

  /* Group by items: stack */
  .group-by-item {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-xs);
  }

  .group-by-label {
    min-width: auto;
    max-width: 100%;
  }

  .group-by-bar-wrapper {
    width: 100%;
  }

  .group-by-count {
    align-self: flex-end;
    margin-top: -20px;
  }

  /* Settings info grid: single column */
  .settings-info-grid {
    grid-template-columns: 1fr;
  }

  /* Settings actions: stack */
  .settings-actions-row {
    flex-direction: column;
  }

  .settings-actions-row .btn {
    width: 100%;
  }

  /* Login card: tighter */
  .login-card {
    padding: var(--space-lg) var(--space-base);
  }

  .login-logo-icon {
    width: 44px;
    height: 44px;
  }

  .login-app-name {
    font-size: var(--font-size-lg);
  }

  /* Modal footer: stack buttons */
  .modal-footer {
    flex-direction: column-reverse;
  }

  .modal-footer .btn {
    width: 100%;
  }

  /* Toast: full width */
  .toast-container {
    top: var(--space-sm);
    right: var(--space-sm);
    left: var(--space-sm);
  }

  .toast {
    min-width: auto;
  }

  /* Drop zone */
  .drop-zone {
    padding: var(--space-lg) var(--space-base);
    min-height: 150px;
  }

  .drop-zone .icon {
    width: 36px;
    height: 36px;
  }

  /* Mapping row: stack */
  .mapping-row {
    flex-direction: column;
    gap: var(--space-sm);
  }

  .mapping-arrow {
    transform: rotate(90deg);
    align-self: center;
  }

  .csv-column,
  .mapping-row .form-select {
    width: 100%;
  }

  /* Import preview stats */
  .import-preview-stats {
    flex-wrap: wrap;
    gap: var(--space-md);
  }

  /* Palette items: horizontal wrap on mobile */
  .palette-items {
    flex-direction: row;
    flex-wrap: wrap;
    gap: var(--space-xs);
  }

  .palette-item {
    flex: 0 0 auto;
    padding: 6px 8px;
    font-size: var(--font-size-xs);
  }

  /* Config tab buttons: smaller */
  .config-tab-btn {
    padding: 5px 4px;
    font-size: 10px;
  }

  /* Preview frame */
  .preview-frame.preview-tablet,
  .preview-frame.preview-mobile {
    max-width: 100%;
  }
}

/* ============================================================
   PRINT STYLES
   ============================================================ */
@media print {
  /* Hide non-essential UI */
  .sidebar,
  .sidebar-backdrop,
  .mobile-menu-toggle,
  .action-bar,
  .records-toolbar,
  .filter-bar,
  .pagination,
  .modal-overlay,
  .toast-container,
  .loading-overlay,
  #loginScreen,
  .tab-btn,
  .btn,
  .cell-actions,
  .form-card-actions,
  .config-tab-buttons,
  .builder-palette,
  .builder-sidebar-right,
  .builder-add-section {
    display: none !important;
  }

  /* Full width content */
  .main-content {
    margin-left: 0 !important;
    padding: 0 !important;
  }

  #appContainer {
    display: block !important;
  }

  /* Show only the active tab */
  .tab-content {
    display: none !important;
  }

  .tab-content.active {
    display: block !important;
  }

  /* Reset backgrounds */
  body {
    background: white !important;
    color: black !important;
    font-size: 12pt;
  }

  .card,
  .form-section,
  .stat-card,
  .summary-card,
  .settings-section,
  .recent-entries-section,
  .table-responsive {
    box-shadow: none !important;
    border: 1px solid #ccc !important;
    break-inside: avoid;
  }

  .stat-cards-grid {
    grid-template-columns: repeat(3, 1fr) !important;
  }

  .summary-cards-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Table styling for print */
  .data-table thead th {
    background: #f0f0f0 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .data-table tbody tr {
    border-bottom: 1px solid #ddd !important;
  }

  .data-table tbody tr:hover {
    background: transparent !important;
  }

  /* Tags: border instead of bg color */
  .tag,
  .badge {
    border: 1px solid #999 !important;
    background: transparent !important;
    color: black !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  /* Links */
  a {
    color: black !important;
    text-decoration: underline;
  }

  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #666;
  }

  /* Page breaks */
  .form-section,
  .settings-section {
    page-break-inside: avoid;
  }

  h2, h3 {
    page-break-after: avoid;
  }

  /* Ensure content header shows */
  .content-header {
    border-bottom: 2px solid black;
    padding-bottom: 8px;
    margin-bottom: 16px;
  }

  .content-title {
    font-size: 18pt;
  }

  /* Stat card print */
  .stat-card {
    padding: 8px !important;
  }

  .stat-card-icon {
    width: 32px !important;
    height: 32px !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  /* Form fields print */
  .form-input,
  .form-select,
  .form-textarea {
    border: 1px solid #ccc !important;
    box-shadow: none !important;
  }

  /* Group by bars */
  .group-by-bar {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}

/* ============================================================
   HIGH CONTRAST / REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  .toast {
    animation: none !important;
  }

  .toast-exit {
    animation: none !important;
    opacity: 0;
  }
}

@media (prefers-contrast: high) {
  :root {
    --color-border: #999;
    --color-border-light: #aaa;
    --color-text-muted: #555;
    --color-text-secondary: #333;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.25);
  }

  .btn-outline {
    border-width: 2px;
  }

  .form-input,
  .form-select,
  .form-textarea {
    border-width: 2px;
  }

  .tag,
  .badge {
    border: 1px solid currentColor;
  }
}