/*
 * CHOPPD phone layout.
 *
 * Everything here sits inside a max-width media query, so laptop and desktop
 * windows are untouched. To revert, delete the <link ... mobile.css> line in index.html.
 */

@media (max-width: 640px) {
  /* ---------- base ---------- */

  .app-shell {
    width: calc(100vw - 24px);
    padding: 10px 0 32px;
  }

  /* iOS zooms the page when a focused field's text is under 16px. */
  input,
  select,
  textarea,
  .library-controls select,
  .library-controls input,
  .icu-actions .compact-button,
  .icu-fields .field input {
    font-size: 16px;
  }

  /* Hover-only affordances don't exist on touch. */
  .plan-add-ride { opacity: 1; }

  /* ---------- header ---------- */

  .topbar {
    grid-template-columns: 1fr;
    gap: 10px;
    margin-bottom: 12px;
    padding: 0 0 10px;
  }

  .topbar > div:first-child {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 2px 10px;
  }

  .topbar h1 { font-size: 1.15rem; }
  .topbar h2 { margin: 0; }
  .topbar em { flex-basis: 100%; }

  .topbar-utils {
    justify-content: flex-start;
    gap: 8px 12px;
  }

  .topbar-utils .account-strip {
    flex: 1 1 100%;
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 6px 8px;
    text-align: left;
  }

  #account-status {
    flex: 1 1 100%;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .topbar-utils .theme-toggle { margin-left: auto; }

  /* ---------- tabs + device chips ---------- */

  .nav-row {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    margin-bottom: 14px;
  }

  .app-nav {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
  }

  .app-nav .secondary-button,
  .app-nav button {
    min-height: 40px;
    padding: 0 4px;
    font-size: 0.85rem;
    justify-content: center;
  }

  .nav-long { display: none; }

  .device-strip-wrap {
    margin-left: 0;
    width: 100%;
  }

  .device-strip {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  .device-button {
    min-width: 0;
    min-height: 40px;
    flex: none;
  }

  .device-button > span:last-child {
    min-width: 0;
    overflow: hidden;
  }

  .device-button small {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* ---------- panels: less nested padding so content gets the width ---------- */

  .builder-panel,
  .preview-section,
  .planner-panel,
  .catalog-panel,
  .history-panel,
  .completion-panel {
    padding: 12px;
  }

  .library-section {
    padding: 8px;
  }

  .section-heading {
    flex-wrap: wrap;
    gap: 8px;
  }

  /* ---------- workout builder ---------- */

  .form-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: end;
    gap: 10px;
  }

  .interval-row {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 44px;
    padding: 10px;
  }

  .interval-row > .row-field:first-child {
    grid-column: 1 / -1;
  }

  .interval-row .remove-interval {
    min-height: 40px;
  }

  /* ---------- library + history cards: buttons in one row, not a stack ---------- */

  .catalog-card {
    padding: 12px;
    gap: 10px;
  }

  .catalog-actions {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .catalog-actions button,
  .catalog-actions a {
    width: auto;
    flex: 1 1 0;
    min-width: 72px;
    min-height: 40px;
  }

  .history-detail-header {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .history-detail-header button {
    width: auto;
    flex: 1 1 auto;
  }

  /* ---------- history ---------- */

  .icu-panel > summary {
    position: relative;
    flex-wrap: wrap;
    padding: 8px 30px 8px 10px;
  }

  .icu-panel > summary::after {
    position: absolute;
    top: 6px;
    right: 10px;
  }

  .conn-pills {
    flex-wrap: wrap;
    min-width: 0;
  }

  .conn-body {
    grid-template-columns: 1fr;
  }

  .history-stats,
  .history-summary {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .history-chart { padding: 10px 8px 4px; }
  .history-chart-head { flex-wrap: wrap; }

  .wk-chart { gap: 3px; }
  .wk-seg { font-size: 0.6rem; }
  .wk-total { font-size: 0.66rem; }
  /* 12 weekly labels don't fit side by side; label every other week (keeps this week). */
  .wk-col:nth-child(odd) .wk-date { visibility: hidden; }
  .wk-date { font-size: 0.64rem; }
  .wk-now { font-size: 0.56rem; }

  .history-chip { min-height: 36px; }

  /* ---------- plan ---------- */

  .plan-cell {
    gap: 4px;
  }

  .plan-cell-date {
    font-size: 0.75rem;
  }

  .plan-add-ride {
    min-height: 34px;
    font-size: 0.8rem;
  }

  .plan-calendar .plan-session {
    font-size: 0.9rem;
    padding: 8px;
  }

  .plan-calendar .plan-session-actions {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  .plan-calendar .plan-session-actions .compact-button,
  .plan-session-actions .compact-button {
    min-height: 36px;
    padding: 0 4px;
    font-size: 0.8rem;
  }

  .plan-week > summary small {
    margin-left: 0;
  }

  .plan-day {
    grid-template-columns: auto 1fr 64px auto;
  }

  /* ---------- ride screen ---------- */

  /* While riding, drop the header so the whole ride screen fits without scrolling. */
  body:has(#ride-view:not([hidden])) .topbar {
    display: none;
  }

  .ride-grid {
    gap: 8px;
  }

  .target-block {
    min-height: 0;
    padding: 14px;
    gap: 10px;
  }

  .target-block h2 {
    font-size: 1.5rem;
  }

  .target-watts span {
    font-size: 4.2rem;
  }

  /* the "% FTP" number sits in the caption; keep it caption-sized */
  .target-watts small span {
    font-size: 1rem;
    line-height: 1;
  }

  .target-block .eyebrow { margin: 0; }
  .target-block h2 { margin: 0; }

  .intensity-adjuster,
  .trainer-mode-control {
    gap: 6px;
  }

  .intensity-adjuster button,
  .resistance-adjuster button,
  .mode-toggle button {
    min-height: 40px;
    min-width: 56px;
  }

  .time-row {
    flex-direction: row;
    justify-content: space-between;
  }

  .metric-board {
    grid-template-rows: none;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .metric-tile {
    padding: 8px 12px;
  }

  .metric-tile strong {
    font-size: 2rem;
  }

  .ride-view {
    gap: 8px;
  }

  .ride-actions {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  .ride-actions button {
    width: 100%;
    min-height: 48px;
    padding: 0 4px;
  }

  /* ---------- sign-in + dialogs ---------- */

  .auth-card-left,
  .auth-card-right {
    padding: 18px;
  }

  .plan-dialog {
    width: calc(100vw - 24px);
    padding: 16px;
  }

  .plan-dialog .builder-actions {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .plan-dialog .builder-actions button {
    width: auto;
    flex: 1 1 0;
  }
}
