/* Shared mobile layout rules. No timers, data fetching, or engine dependencies.
   Page-specific layouts live in their own stylesheets. */
body[data-auth-state="authenticated"] .app-footer {
  width: 100%; max-width: 100%; margin-inline: 0;
}
body .performance-page .table-wrap { min-width: 0; max-width: 100%; overflow-x: auto; }
@media (max-width: 1100px) {
  body :is(.report-filter-form, .trade-table-filter-form, .strategy-history-filter-form,
           .daily-report-filter-form, .loss-control-filter-form, .loss-guard-form) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  body :is(.notes-shell, .loss-impact-layout, .loss-control-bottom-grid) {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 760px) {
  html { scroll-padding-top: 12px; }
  body .page-shell { width: 100%; min-width: 0; padding-inline: 10px; }
  body .dashboard { min-width: 0; grid-template-columns: minmax(0, 1fr); gap: 14px; padding-block: 12px 24px; }
  body .dashboard > * { min-width: 0; max-width: 100%; grid-column: 1 / -1; }
  body .dashboard-grid { grid-template-columns: minmax(0, 1fr); }
  body[data-auth-state][data-system-theme] .panel,
  body .panel { min-width: 0; padding: 14px; }
  body :is(.panel-head, .table-panel-head, .notes-list-head, .theme-preview-toolbar,
           .theme-studio-section-head, .theme-studio-hero, .daily-chart-head) {
    flex-wrap: wrap; gap: 12px; min-width: 0;
  }
  body :is(.panel-head, .table-panel-head) > * { min-width: 0; max-width: 100%; }
  body :is(.panel-head h2, .panel-head h3) { overflow-wrap: anywhere; }
  body :is(.panel-head-actions, .table-panel-actions, .filter-actions,
           .trade-table-filter-actions, .daily-report-actions, .note-actions) {
    flex-wrap: wrap; gap: 8px; max-width: 100%; min-width: 0;
  }
  body :is(.panel-head-actions, .table-panel-actions, .filter-actions,
           .trade-table-filter-actions, .daily-report-actions) > button {
    flex: 1 1 auto; white-space: normal;
  }
  body :is(form, fieldset, form label, .metric-card, .trade-detail-body) { min-width: 0; }
  body :is(input, select, textarea) { box-sizing: border-box; min-width: 0; max-width: 100%; }
  body :is(input:not([type=checkbox]):not([type=radio]):not([type=range]), select, textarea) {
    min-height: 44px; font-size: 16px; /* Prevent input-focus zoom on iOS without disabling pinch zoom. */
  }
  body :is(button, .primary-button, .secondary-button, .ghost-button, .action-button) {
    min-height: 44px; touch-action: manipulation;
  }
  body :is(.dashboard-trade-actions .action-button, .manual-research-page .searchable-select-trigger) {
    min-height: 44px !important;
  }
  body :is(button, a, input, select, textarea, summary):focus-visible {
    outline: 2px solid var(--theme-accent-2, #38bdf8); outline-offset: 3px;
  }
  body :is(.metric-grid, .loss-impact-grid, .loss-research-summary-grid, .loss-research-grid,
           .loss-phase-grid, .loss-timing-grid, .loss-entry-path-grid, .guide-grid,
           .guide-grid-four, .flow-lane-grid, .daily-comparison-grid, .daily-report-overview,
           .daily-ml-card-grid, .daily-drawer-metrics, .daily-drawer-breakdown-grid,
           .strategy-history-overview, .strategy-history-ml-grid, .strategy-history-ml-estimate-grid) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  body[data-auth-state="authenticated"] .daily-report-overview { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  body :is(.metric-label, .metric-value, .metric-note, .form-message, .note-title,
           .note-body, .trade-detail-item, .trade-insight-summary strong) {
    white-space: normal; overflow-wrap: anywhere;
  }
  /* A wide ledger remains a ledger: swipe within its panel to reach every column. */
  body :is(.table-wrap, .table-scroll, .table-scroll-trades, .daily-strategy-performance) {
    width: 100%; max-width: 100%; min-width: 0; overflow-x: auto;
    overscroll-behavior-x: contain; scrollbar-width: thin;
  }
  body :is(.table-wrap, .table-scroll) table { max-width: none; }
  body :is(.table-wrap, .table-scroll) :is(button, .action-button) { min-height: 44px; min-width: 44px; }
  body :is(.table-pagination, .daily-history-footer, .history-pagination, .pagination-controls) {
    flex-wrap: wrap; gap: 8px;
  }
  body :is(pre, .json-preview) { min-width: 0; max-width: 100%; overflow-x: auto; }
  body .note-form { position: static; }
  body .notes-shell { gap: 14px; }
  body :is(.guide-timeline, .guide-action-list) { grid-template-columns: minmax(0, 1fr); padding-left: 0; }
  body :is(.guide-timeline article, .guide-action-list article, .guide-card, .guide-note-card) {
    min-width: 0; overflow-wrap: anywhere;
  }
  body .app-footer { flex-wrap: wrap; gap: 8px; padding: 16px 6px; overflow-wrap: anywhere; }

  /* Shared trade details, confirmation dialogs and report drawers. */
  body :is(.trade-detail-modal, .strategy-editor-modal, .admin-inspector-modal) {
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
             max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }
  body :is(.trade-detail-card, .strategy-editor-card, .action-confirm-card) {
    box-sizing: border-box; min-width: 0; width: 100%; max-width: 100%;
    max-height: calc(100vh - 24px); max-height: calc(100dvh - 24px);
    overflow-y: auto; overscroll-behavior-y: contain; padding: 14px;
  }
  body .action-confirm-card { padding: 0; }
  body .action-confirm-shell { padding: 16px; }
  body .action-confirm-glow { display: none; }
  body :is(.trade-detail-head, .strategy-editor-head, .action-confirm-head,
           .strategy-history-drawer-head) { gap: 10px; min-width: 0; align-items: start; }
  body :is(.trade-detail-head, .strategy-editor-head, .action-confirm-head) > div {
    min-width: 0; overflow-wrap: anywhere;
  }
  body :is(.trade-detail-close, .strategy-editor-close, .action-confirm-close) {
    flex: 0 0 44px; width: 44px; min-width: 44px; height: 44px;
  }
  body :is(.trade-detail-grid, .trade-detail-path-grid, .trade-detail-guard-rules,
           .trade-insight-summary, .trade-insight-detail-grid, .trade-insight-algorithm,
           .trade-insight-trail, .trade-insight-trail.is-simple) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  body .trade-insight-leg { display: flex; flex-wrap: wrap; gap: 10px; }
  body .trade-insight-leg > * { min-width: 0; overflow-wrap: anywhere; }
  body :is(.trade-insight-toolbar, .trade-detail-chart-toolbar, .action-confirm-actions) { flex-wrap: wrap; gap: 8px; }
  body :is(.trade-detail-body, .trade-insight, .action-confirm-body) { min-width: 0; overflow-wrap: anywhere; }
  body :is(.daily-report-drawer, .strategy-history-drawer) {
    width: 100%; max-width: 100%; max-height: 100vh; max-height: 100dvh;
    padding-bottom: env(safe-area-inset-bottom);
  }
  body :is(.daily-report-drawer-body, .strategy-history-drawer-body) { min-width: 0; padding: 14px; overscroll-behavior-y: contain; }
  body :is(.daily-drawer-strategy-list article, .daily-distribution-list article) { min-width: 0; overflow-wrap: anywhere; }
  body .daily-distribution-list article { grid-template-columns: minmax(0, 1fr) auto; }

  /* Keep the phone's compositor free of large backdrop filters and idle effects. */
  body :is(.hero, .panel, .metric-card, .trade-detail-card, .action-confirm-card,
           .strategy-editor-card, .login-card, .theme-live-preview,
           .trade-detail-backdrop, .action-confirm-backdrop,
           .strategy-history-drawer-backdrop, .daily-report-drawer-backdrop) {
    backdrop-filter: none !important; -webkit-backdrop-filter: none !important;
  }
  body :is(.background-orb, .login-market-aurora, .login-market-beams i,
           .login-market-chart, .login-market-network, .login-market-nodes circle) {
    animation: none !important;
  }
  body[data-auth-state="authenticated"] .dashboard-summary-sink.dashboard-compact-summary .dashboard-compact-grid {
    gap: 14px !important; padding: 4px !important;
  }
  body[data-auth-state="authenticated"] .dashboard-summary-sink.dashboard-compact-summary .dashboard-compact-tile {
    padding: 14px !important; min-height: 120px !important;
  }
  body[data-auth-state="authenticated"] .dashboard-summary-sink.dashboard-compact-summary .metric-note {
    white-space: normal !important;
  }
  body[data-auth-state="authenticated"] .dashboard-summary-sink.dashboard-compact-summary .dashboard-compact-stat-line {
    flex-wrap: wrap !important;
  }
  body[data-auth-state="authenticated"] .dashboard-summary-sink.dashboard-compact-summary .dashboard-engine-actions .action-button {
    min-height: 44px !important; font-size: .875rem !important;
  }
}

@media (max-width: 540px) {
  body :is(.report-filter-form, .trade-table-filter-form, .strategy-history-filter-form,
           .daily-report-filter-form, .loss-control-filter-form, .loss-guard-form,
           .notes-filter-row, .loss-research-grid, .loss-phase-grid, .loss-timing-grid,
           .loss-entry-path-grid, .guide-grid, .guide-grid-four, .flow-lane-grid,
           .daily-drawer-breakdown-grid, .trade-detail-grid, .trade-detail-guard-rules) {
    grid-template-columns: minmax(0, 1fr);
  }
  body :is(.report-filter-form, .trade-table-filter-form, .strategy-history-filter-form,
           .daily-report-filter-form, .loss-control-filter-form, .loss-guard-form) > * {
    grid-column: auto; min-width: 0;
  }
  body :is(.trade-insight-summary, .trade-insight-detail-grid, .trade-insight-algorithm,
           .trade-insight-trail, .trade-insight-trail.is-simple) { grid-template-columns: minmax(0, 1fr); }
  body .login-card { padding: 20px 16px; }
  body .panel-head h2 { font-size: 1.3rem; }
  body .panel-head-actions { width: 100%; }
  body .daily-report-streaks { grid-template-columns: minmax(0, 1fr); }
}

@media (prefers-reduced-motion: reduce) {
  .page-shell *, .trade-detail-modal *, dialog * {
    animation-duration: .01ms !important; animation-iteration-count: 1 !important;
    transition-duration: .01ms !important; scroll-behavior: auto !important;
  }
}
