/* Phone workspace. Loaded after styles.css; desktop keeps its own layout. */
@media (min-width: 821px) {
  #mobile-more,
  #mobile-more-menu,
  .things-mobile-switcher,
  #account-tools-toggle,
  #tracker-filter-toggle { display: none !important; }
}

@media (max-width: 820px) {
  html, body { max-width: 100%; overflow-x: clip; }
  main { width: 100%; max-width: none; padding: 18px 16px calc(var(--tabbar-h) + var(--safe-bottom) + 30px); }
  main > .sec.on { min-height: calc(100dvh - var(--nav-h) - var(--safe-top) - var(--tabbar-h) - var(--safe-bottom) - 48px); }
  main, .sec, .card, .summary-grid, .analysis-grid, .tracker-summary,
  .things-controls-bar, .things-item-grid, .wl-config-grid { min-width: 0; }
  .sec { overflow-wrap: break-word; }
  .section-title { font-size: 25px; line-height: 1.2; letter-spacing: -.55px; }
  .section-sub { font-size: 13.5px; line-height: 1.45; margin-bottom: 16px; }
  .card { padding: 16px; margin-bottom: 12px; border-radius: 17px; }
  .card-title { font-size: 15px; line-height: 1.35; margin-bottom: 12px; }
  .card-title > span { line-height: 1.35; }
  .empty-state { padding: 24px 12px; }
  #nav { background: var(--surf); }

  /* Four daily destinations stay visible. Secondary destinations live in More. */
  #nav [role="tablist"] {
    right: 25%;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  #nav [role="tablist"] > #tab-analysis,
  #nav [role="tablist"] > #tab-wealth,
  #nav [role="tablist"] > #tab-africa { display: none; }
  #mobile-more {
    position: fixed; z-index: 102; right: 0; bottom: 0;
    width: 25%; height: calc(var(--tabbar-h) + var(--safe-bottom));
    display: inline-flex; flex-direction: column; align-items: center;
    justify-content: center; gap: 3px; min-width: 0; min-height: 54px;
    padding: 4px 0 var(--safe-bottom); border: 0;
    border-top: 1px solid var(--border); border-radius: 0;
    background: var(--surf); color: var(--muted);
    font: inherit; font-size: 10.5px; font-weight: 500;
  }
  #mobile-more svg { width: 22px; height: 22px; flex: none; }
  #mobile-more[aria-expanded="true"],
  body[data-tab="analysis"] #mobile-more,
  body[data-tab="wealth"] #mobile-more,
  body[data-tab="africa"] #mobile-more { color: var(--accent-text); font-weight: 600; }
  #mobile-more-menu {
    position: fixed; z-index: 102;
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 10px);
    display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px;
    padding: 10px; border: 1px solid var(--border); border-radius: 17px;
    background: var(--surf); box-shadow: var(--shadow-lg);
  }
  #mobile-more-menu[hidden], #mobile-more[hidden] { display: none !important; }
  #mobile-more-menu button {
    min-width: 0; min-height: 48px; padding: 8px 12px;
    border: 1px solid var(--border); border-radius: 12px;
    background: var(--surf2); color: var(--text);
    font: inherit; font-size: 14px; font-weight: 600;
    display: flex; align-items: center; justify-content: space-between;
    gap: 12px; text-align: left;
  }
  #mobile-more-menu button span { color: var(--muted); font-size: 11.5px; font-weight: 400; text-align: right; }
  #mobile-more-menu button[aria-current="page"] { border-color: var(--accent); color: var(--accent-text); background: var(--accent-soft); }

  /* Accounts: one clear balance story, then actions and the remaining records. */
  #account-kpis { grid-template-columns: minmax(0, 1fr); gap: 10px; margin-bottom: 12px; }
  #account-kpis > * { min-width: 0; }
  .kpi-hero { padding: 17px 18px; }
  .kpi-hero .kpi-value { font-size: clamp(33px, 9vw, 40px); }
  .safe-card { padding: 16px 18px; border-radius: 17px; gap: 4px; }
  .safe-amount { font-size: clamp(30px, 8vw, 38px); }
  .safe-row { min-height: 34px; }
  .kpi-list { border-radius: 17px; }
  .kpi-row { min-height: 60px; }
  .kpi-row .kpi-sub { max-width: 100%; }
  .quick-actions { gap: 8px; margin-bottom: 14px; }
  .quick-actions .btn { min-width: 0; min-height: 46px; padding-inline: 8px; font-size: 13.5px; }
  #account-tools-toggle,
  #tracker-filter-toggle {
    width: 100%; min-height: 44px; display: inline-flex; align-items: center;
    justify-content: space-between; gap: 10px; padding: 9px 13px;
    border: 1px solid var(--border); border-radius: 12px;
    background: var(--surf); color: var(--text);
    font: inherit; font-size: 13.5px; font-weight: 600;
    text-align: left;
  }
  #account-tools-toggle[aria-expanded="true"],
  #tracker-filter-toggle[aria-expanded="true"] { border-color: var(--brig); background: var(--surf2); }
  #account-tools[hidden], #tracker-extra-filters[hidden],
  #account-tools-toggle[aria-expanded="false"] + #account-tools,
  #tracker-filter-toggle[aria-expanded="false"] + #tracker-extra-filters { display: none !important; }
  #account-tools { min-width: 0; }
  #account-tools > .card:first-child { margin-top: 12px; }
  #backup-banner:not(.hidden) + #sync-setup { display: none; }
  .backup-banner { padding: 12px; gap: 10px; border-radius: 15px; }
  .backup-banner-icon { width: 32px; height: 32px; }
  .backup-banner-actions { margin-top: 8px; }
  .sync-setup { padding: 11px 13px; gap: 8px; }
  .bill-row { flex-wrap: wrap; }
  .bill-main { flex-basis: 54%; }
  .bill-pay { margin-left: auto; }

  /* Tracker: the selected period and the transaction log are the main task. */
  #sec-tracker.on { display: flex; flex-direction: column; }
  #sec-tracker > * { order: 10; }
  #sec-tracker > .section-title { order: 0; }
  #sec-tracker > .section-sub { order: 1; }
  #tracker-filter-card { order: 2; }
  #tracker-transactions-card { order: 3; }
  #tracker-budget-card { order: 4; }
  #analysis-grid { order: 5; }
  #sec-tracker .tracker-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  #sec-tracker .ts-card {
    min-width: 0; padding: 12px 13px; border-radius: 12px;
    background: var(--surf2); box-shadow: none;
  }
  #sec-tracker .ts-label { font-size: 11.5px; letter-spacing: 0; text-transform: none; margin-bottom: 4px; }
  #sec-tracker .ts-value { font-size: clamp(17px, 4.8vw, 20px); line-height: 1.15; }
  #sec-tracker .ts-card:last-child { grid-column: 1 / -1; }
  #sec-tracker .ts-card:last-child .ts-value { font-size: 22px; }
  #sec-tracker .filter-bar { min-width: 0; }
  #sec-tracker .filter-bar select,
  #sec-tracker .filter-bar input[type="date"] { width: 100%; min-width: 0; min-height: 44px; font-size: 16px; }
  #tracker-extra-filters {
    width: 100%; display: grid; grid-template-columns: minmax(0, 1fr);
    gap: 5px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--hair);
  }
  #tracker-extra-filters label { font-size: 12px; font-weight: 600; color: var(--muted); }
  #tracker-extra-filters select { margin-bottom: 5px; }
  #tracker-extra-filters .btn { justify-self: start; }
  #tracker-transactions-card .card-title { margin-bottom: 10px; }
  #txn-search { min-height: 44px; font-size: 16px; }
  #tracker-transactions-card .txn-type-chips { margin-bottom: 10px; }
  .txn-row { min-width: 0; }
  .txn-row .txn-desc, .txn-row .txn-meta { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .txn-row .txn-amount { white-space: nowrap; }
  .txn-day-card { padding-inline: 12px; border-radius: 14px; }
  .cat-row { min-width: 0; gap: 7px; }
  .cat-label { min-width: 0; flex: 0 1 38%; }
  .cat-bar-wrap { min-width: 8px; }
  .cat-amount { min-width: 0; white-space: nowrap; }
  .cat-pct { min-width: 0; }

  /* Things: a quick item action, compact signals, and readable purchase cards. */
  .things-mobile-switcher {
    display: flex; align-items: center; gap: 10px;
    margin-bottom: 12px; color: var(--muted);
    font-size: 13px; font-weight: 600;
  }
  #things-mobile-switcher { display: block; flex: 1; width: 100%; min-width: 0; min-height: 46px; padding: 10px 12px;
    border: 1px solid var(--border); border-radius: 12px; background: var(--surf); color: var(--text);
    font: inherit; font-size: 16px; }
  .things-section-nav { display: none; }
  #things-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 12px; }
  #things-kpis .kpi { min-width: 0; padding: 11px 12px; box-shadow: none; }
  #things-kpis .kpi-label { font-size: 11.5px; margin-bottom: 3px; }
  #things-kpis .kpi-value { font-size: 17px; }
  .things-controls-bar {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px; margin-bottom: 12px;
  }
  .things-controls-bar > * { min-width: 0; width: 100%; }
  #things-search { grid-column: 1 / -1; font-size: 16px !important; min-height: 44px; }
  #things-cat-filter, #things-sort { font-size: 16px !important; min-height: 44px; }
  #things-show-add-item { grid-column: 1 / -1; min-height: 44px; }
  .things-item-grid { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  #things-item-grid > .empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: clamp(220px, calc(100dvh - 340px), 540px); }
  .things-item-card { min-width: 0; padding: 14px; border-radius: 15px; }
  .things-item-name { overflow-wrap: anywhere; }
  #things-detail-view .card-title { justify-content: flex-start; }
  #things-detail-view .card-title > span { flex-basis: auto; margin: 0; }
  #things-detail-view .ratios-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #things-detail-view .ratio-card { min-width: 0; padding: 11px 12px; }
  #things-detail-view .ratio-value { font-size: 18px; }
  #things-detail-view .history-table, #things-detail-view .history-table tbody,
  #things-detail-view .history-table tr, #things-detail-view .history-table td { display: block; width: 100%; }
  #things-detail-view .history-table thead { display: none; }
  #things-detail-view .history-table tr {
    position: relative; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px 12px; padding: 12px 38px 12px 0; border-bottom: 1px solid var(--hair);
  }
  #things-detail-view .history-table td { min-width: 0; padding: 0; border: 0; font-size: 13px; overflow-wrap: anywhere; }
  #things-detail-view .history-table td::before { display: block; margin-bottom: 2px; color: var(--muted); font-size: 11px; font-weight: 500; }
  #things-detail-view .history-table td:nth-child(1)::before { content: 'Date'; }
  #things-detail-view .history-table td:nth-child(2)::before { content: 'Store'; }
  #things-detail-view .history-table td:nth-child(3)::before { content: 'Quantity'; }
  #things-detail-view .history-table td:nth-child(4)::before { content: 'Total'; }
  #things-detail-view .history-table td:nth-child(5)::before { content: 'Unit price'; }
  #things-detail-view .history-table td:last-child { position: absolute; top: 10px; right: 0; width: 36px; }
  #things-detail-view .history-table td:last-child::before { display: none; }
  #things-detail-view .history-table td:last-child button { min-width: 36px; min-height: 40px; }

  /* Wealth: settings have clear grouping while the dashboard stays readable. */
  #wl-config { padding: 16px; }
  #wl-config .wl-sub { margin: 7px 0 12px; font-size: 12.5px; line-height: 1.45; }
  #wl-config details { min-width: 0; border-top: 1px solid var(--hair); }
  #wl-config details > summary { min-height: 46px; padding: 10px 0; font-size: 14px; font-weight: 600; }
  #wl-config details[open] > summary { margin-bottom: 8px; }
  #wl-config .wl-config-grid { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  #wl-config .wl-config-field input { min-height: 46px; font-size: 16px; }
  #wl-config .wl-config-actions { gap: 8px; }
  #wl-config .wl-config-actions button { min-height: 44px; }
  #wl-config .wl-config-import { padding: 10px; }
  #wl-config .wl-config-import input[type="file"] { max-width: 100%; }
  #sec-wealth svg, #sec-analysis svg { max-width: 100%; }
  .wl-mode { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 5px; }
  .wl-mode button { min-width: 0; padding: 7px 2px; font-size: 12px; }

  /* Long labels and controls stay inside their card at narrow widths. */
  .history-table, .things-item-card, .bill-row, .goal-item,
  .acct-mgmt-item, .wl-row, .af-item { max-width: 100%; }
  .card input[type="file"] { max-width: 100%; }
  .txn-sheet { width: 100%; max-width: 100%; }
  .sheet-foot { flex-wrap: wrap; row-gap: 2px; }
}

@media (max-width: 360px) {
  main { padding-inline: 12px; }
  #nav { padding-inline: 12px; }
  #mobile-more { font-size: 9.5px; }
  .card { padding: 14px; }
  .kpi-row { gap: 8px; }
  .kpi-row .kpi-value { font-size: 14px; }
  .txn-icon { width: 32px; height: 32px; border-radius: 10px; }
  .txn-row { gap: 8px; }
  .txn-row .txn-desc { font-size: 14px; }
  .txn-row .txn-amount { font-size: 13.5px; }
  #things-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
