/* Mobile / phone — one column, boxes fit screen, no page horizontal swipe.
   Only chart canvases and wide tables may scroll internally. */

@media (max-width: 900px) {
  html, body {
    overflow-x: hidden;
    max-width: 100vw;
    width: 100%;
  }

  * {
    max-width: 100%;
  }

  /* Form controls / buttons can exceed if not constrained; keep sensible */
  input, select, textarea, button, canvas {
    max-width: 100%;
  }

  .topbar {
    height: auto;
    min-height: 52px;
    flex-wrap: wrap;
    gap: 8px 10px;
    padding: 10px 10px;
    width: 100%;
    max-width: 100vw;
    box-sizing: border-box;
  }
  .brand-name { font-size: 12px; }
  .tabs {
    order: 3;
    flex: 1 1 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 4px;
    max-width: 100%;
  }
  .tab-btn {
    padding: 10px 12px;
    font-size: 12px;
    white-space: nowrap;
    flex-shrink: 0;
    max-width: none; /* allow tab text */
  }
  .bot-control {
    margin-left: auto;
    gap: 8px;
    flex-wrap: wrap;
  }
  .mode-select select {
    min-height: 36px;
    font-size: 13px;
  }
  .bot-toggle {
    min-height: 36px;
    padding: 8px 12px;
  }

  main {
    padding: 8px 6px 28px;
    width: 100%;
    max-width: 100vw;
    box-sizing: border-box;
    overflow-x: hidden;
  }

  .tab-panel,
  .tab-panel.active {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }

  /* Force single column; never wider than viewport */
  .dash-grid,
  .init-grid,
  .perf-body,
  .dash-main {
    display: flex !important;
    flex-direction: column !important;
    grid-template-columns: none !important;
    gap: 10px;
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }

  .panel,
  .group-picker.panel,
  .plot-panel,
  .equity-panel,
  .trades-panel,
  .perf-panel {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    box-sizing: border-box;
    overflow-x: hidden; /* box itself never forces page scroll */
    margin-bottom: 10px;
  }

  /* ---- Groups: vertical list, full width, no horizontal swipe ---- */
  .group-picker.panel {
    margin-bottom: 8px;
  }
  .group-list {
    max-height: 200px;
    display: block !important;
    flex-direction: column !important;
    overflow-x: hidden !important;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
  }
  .group-item {
    min-width: 0 !important;
    max-width: 100% !important;
    width: 100%;
    flex-shrink: 1;
    border-right: none !important;
    border-bottom: 1px solid var(--line-soft);
    padding: 10px 12px;
    box-sizing: border-box;
  }
  .group-item-main {
    min-width: 0;
    overflow: hidden;
  }
  .group-item .g-name,
  .group-item .g-symbols,
  .group-item .name,
  .group-item .meta {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
  }
  #group-search {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  /* ---- Charts: panel fits screen; only the canvas area may pan ---- */
  .plot-panel .chart-wrap,
  .equity-panel .equity-wrap,
  .chart-wrap,
  .equity-wrap {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    box-sizing: border-box;
    position: relative;
    /* allow horizontal pan of long history without expanding the page */
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
  }

  .plot-panel .chart-wrap {
    height: 240px !important;
    min-height: 200px;
    padding: 4px 4px 12px !important;
  }
  .equity-panel .equity-wrap,
  .equity-wrap {
    height: 160px !important;
    padding: 4px 4px 8px !important;
  }
  .radar-wrap {
    height: 180px !important;
    width: 100% !important;
    max-width: 100%;
  }

  .plot-panel .chart-wrap canvas,
  .equity-wrap canvas,
  .chart-wrap canvas,
  #residual-chart,
  #equity-canvas {
    max-width: none; /* canvas may be wider for history pan */
    display: block;
  }

  .panel-head {
    flex-wrap: wrap;
    gap: 6px;
    padding: 10px 10px;
    max-width: 100%;
  }
  .panel-head h2 {
    font-size: 12.5px;
  }
  .plot-meta {
    font-size: 10.5px;
    padding: 0 10px 8px;
    word-break: break-word;
    max-width: 100%;
  }

  /* ---- Tables: outer box fits; only .table-scroll pans ---- */
  .table-scroll,
  .table-wrap,
  .perf-table-wrap {
    display: block;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    box-sizing: border-box;
  }

  /* Trades table: denser so more fits without feeling huge */
  #trades-table,
  #perf-table,
  #candidates-table,
  table,
  table.data-table {
    font-size: 10.5px;
    /* min-width only inside scroll container — does not expand page */
    min-width: 0;
    width: max-content;
    max-width: none;
  }

  /* Prefer fitting trades into viewport when possible */
  #trades-table {
    width: 100%;
    min-width: 100%;
    table-layout: fixed;
  }
  #trades-table th,
  #trades-table td {
    padding: 5px 4px;
    font-size: 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* Approximate column weights for trades on narrow screens */
  #trades-table th:nth-child(1),
  #trades-table td:nth-child(1) { width: 8%; }   /* Dir */
  #trades-table th:nth-child(2),
  #trades-table td:nth-child(2) { width: 18%; }  /* Opened */
  #trades-table th:nth-child(3),
  #trades-table td:nth-child(3) { width: 18%; }  /* Closed */
  #trades-table th:nth-child(4),
  #trades-table td:nth-child(4) { width: 12%; }  /* Reason */
  #trades-table th:nth-child(5),
  #trades-table td:nth-child(5) { width: 10%; }  /* Entry Z */
  #trades-table th:nth-child(6),
  #trades-table td:nth-child(6) { width: 10%; }  /* Close Z */
  #trades-table th:nth-child(7),
  #trades-table td:nth-child(7) { width: 12%; }  /* PnL */
  #trades-table th:nth-child(8),
  #trades-table td:nth-child(8) { width: 12%; }  /* Mode */

  table th,
  table.data-table th {
    padding: 6px 6px;
    font-size: 10px;
  }
  table td,
  table.data-table td {
    padding: 5px 6px;
    font-size: 10.5px;
  }

  /* Perf table stays scrollable inside its box (many columns) */
  #perf-table {
    min-width: 640px;
    width: max-content;
    table-layout: auto;
  }
  #candidates-table {
    min-width: 600px;
    width: max-content;
  }

  .perf-filters {
    padding: 10px;
  }
  .perf-filters .filter-row {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .perf-filters label {
    flex-wrap: wrap;
  }
  .perf-filters select,
  .perf-filters input[type="number"] {
    min-height: 36px;
    font-size: 13px;
    min-width: 0;
    flex: 1;
  }

  .group-detail {
    border-left: none !important;
    border-top: 1px solid var(--line);
    padding: 12px;
    max-width: 100%;
  }

  .param-form {
    grid-template-columns: 1fr !important;
    gap: 10px;
    padding: 12px;
    width: 100%;
    max-width: 100%;
  }
  .param-form input,
  .param-form select,
  .param-form textarea {
    min-height: 40px;
    font-size: 14px;
    padding: 10px 12px;
    width: 100%;
    box-sizing: border-box;
  }
  .param-form textarea {
    min-height: 100px;
  }

  .method-switch {
    flex-direction: column;
    gap: 6px;
    padding: 10px 12px 0;
  }
  .method-btn {
    min-height: 44px;
    font-size: 13px;
  }

  .init-actions {
    flex-direction: column;
    padding: 0 12px 12px;
  }
  .btn-primary,
  .btn-secondary,
  .btn-danger {
    min-height: 44px;
    width: 100%;
    font-size: 14px;
    padding: 12px 16px;
  }

  .symbol-checklist {
    grid-template-columns: 1fr 1fr;
    max-height: 240px;
  }
  .symbol-check-item {
    min-height: 36px;
    font-size: 12px;
  }
  .symbol-picker-head {
    flex-direction: column;
    align-items: stretch;
  }
  .symbol-picker-head input {
    max-width: none;
    width: 100%;
    min-height: 40px;
  }

  .init-progress-wrap {
    margin: 0 12px 12px;
  }

  .actions-cell {
    flex-direction: column;
    align-items: stretch;
  }
  .actions-cell button {
    width: 100%;
    min-height: 36px;
  }

  .manual-group-panel,
  .manual-group-inner {
    max-width: 100%;
    box-sizing: border-box;
  }
}

@media (max-width: 480px) {
  body {
    font-size: 12px;
  }
  .brand-name {
    letter-spacing: 0;
    font-size: 11px;
  }
  .brand-mark {
    font-size: 16px;
  }
  .tab-btn {
    padding: 8px 10px;
    font-size: 11.5px;
  }
  main {
    padding: 6px 4px 24px;
  }
  .plot-panel .chart-wrap {
    height: 210px !important;
  }
  .equity-panel .equity-wrap,
  .equity-wrap {
    height: 140px !important;
  }
  .symbol-checklist {
    grid-template-columns: 1fr;
  }
  #trades-table th,
  #trades-table td {
    font-size: 9.5px;
    padding: 4px 3px;
  }
}

/* Prevent iOS zoom on focus */
@media (max-width: 900px) {
  input,
  select,
  textarea {
    font-size: 16px !important;
  }
}
