/* Phone layout (screen only). Loaded after styles.css and theme-tron.css so these
   rules win ties. The goal is less scrolling: dense 2-up grids, slimmer cards,
   and long repeated lists (events, trades, asset setup) that don't take a screen each. */
/* Phone-only chrome is hidden everywhere else. */
.m-tabbar, .m-subnav, td.m-summary, .m-event-select { display: none; }

@media screen and (max-width: 640px) {
  /* ---- Home: nine tiles become compact rows ---- */
  .feature-grid { grid-template-columns: 1fr; gap: 10px; }
  .feature-card {
    display: grid;
    grid-template-columns: 40px minmax(0, 1fr);
    column-gap: 12px;
    row-gap: 2px;
    align-items: center;
    padding: 14px;
    border-radius: 18px;
  }
  .feature-icon { grid-row: 1 / span 2; width: 40px; height: 40px; border-radius: 12px; }
  .feature-card h3 { margin: 0; font-size: 1rem; }
  .feature-card p {
    margin: 0;
    font-size: 0.8rem;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .feature-link { display: none; }

  /* ---- Summary tiles: two per row ---- */
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi-card { min-height: 0; padding: 12px; gap: 6px; }

  /* ---- Live price tiles: two per row, symbol + price + source only ---- */
  .live-prices-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .price-card { min-height: 0; padding: 10px 12px; gap: 4px; }
  .price-card-top { gap: 6px; }
  .price-card h4 { font-size: 0.95rem; }
  .price-card .pill { padding: 2px 7px; font-size: 0.62rem; }
  .price-symbol { display: none; }
  .price-value { font-size: 1.15rem; }
  .price-meta { font-size: 0.72rem; }
  .price-meta div:nth-child(n+2) { display: none; }

  /* ---- Editable record cards (assets, trades, holdings, lots) ---- */
  .record-card { padding: 12px; gap: 10px; }
  .record-card-head { flex-direction: row; align-items: center; }
  .record-card .record-fields,
  .record-card-asset .record-fields,
  .record-card-transaction .record-fields,
  .record-card-holding .record-fields,
  .record-card-lot .record-fields,
  .record-card-match .record-fields {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px 10px;
  }
  .record-field.span-2,
  .record-field.span-3,
  .record-field.full { grid-column: 1 / -1; }
  /* asset setup: pair every field instead of giving Asset / Quote Symbol a row each */
  .record-card-asset .record-field.span-2 { grid-column: auto; }
  .record-card-asset .record-card-subtitle { display: none; }
  .record-input, .record-select { padding-top: 7px; padding-bottom: 7px; }
  .record-field.metric-field { padding: 8px 10px; }
  .record-field.metric-field .record-value { font-size: 0.95rem; }
  .record-actions { width: auto; justify-content: flex-end; }
  .record-actions .danger-link { width: auto; }

  /* ---- Trade / activity tables that stack into cards ---- */
  .tx-table tr,
  .admin-portfolio-table tr {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 2px 10px;
    padding: 10px 12px;
    margin-bottom: 10px;
  }
  .tx-table td,
  .admin-portfolio-table td {
    display: grid;
    gap: 2px;
    justify-content: stretch;
    align-items: start;
    padding: 4px 0;
    border-bottom: 0;
    min-width: 0;
  }
  .tx-table td:nth-last-child(2) { grid-column: span 2; }
  .tx-table td:last-child { align-self: end; justify-self: end; }
  .tx-table input[type="date"] { font-size: 0.78rem; padding-inline: 6px; }
  .admin-portfolio-table td:first-child { grid-column: 1 / -1; }
  .admin-portfolio-table td:first-child::before { display: none; }
  .admin-portfolio-table tr { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tx-table td .tx-num,
  .tx-table td .tx-ccy,
  .admin-portfolio-table td:not(:first-child) { text-align: left; justify-content: stretch; }

  /* ---- Dashboard: events, goals, rings, checklist ---- */
  .life-plan-events-list { gap: 8px; grid-auto-rows: auto; align-items: start; }
  .life-plan-event-card { min-height: 0; padding: 10px 12px; gap: 6px; justify-content: flex-start; }
  .life-plan-event-card h3 { font-size: 0.92rem; -webkit-line-clamp: 1; }
  .life-plan-event-card .event-detail { margin-top: 2px; font-size: 0.8rem; -webkit-line-clamp: 2; }
  .life-plan-event-actions { gap: 4px; margin-top: 0; }
  .life-plan-event-actions .btn { padding: 6px 6px; min-height: 34px; line-height: 1.2; font-size: 0.74rem; }
  .life-plan-coverage-rings { gap: 8px; }
  .coverage-ring-card { padding: 10px 6px; gap: 6px; }
  .coverage-ring-chart { max-width: 96px; }
  .coverage-ring-title { min-height: 0; }
  .coverage-ring-card.is-empty .coverage-ring-chart { display: none; }
  .plan-checklist-steps { padding: 10px 14px 14px; }
  .plan-checklist-step { padding: 10px 0; gap: 10px; }
  .plan-checklist-blurb { font-size: 0.82rem; margin-top: 2px; }

  /* ---- Activity lists ---- */
  .admin-activity-list { gap: 6px; }
  .admin-activity-list article { padding: 8px 10px; }

  /* ---- Tap targets: nothing a thumb has to hit should be under ~34px tall ---- */
  .life-plan-switcher-icon-btn,
  .life-plan-switcher-name,
  .equity-toggle-btn,
  .life-plan-legend-btn,
  .tx-delete { min-height: 34px; }
  .life-plan-switcher-icon-btn { min-width: 34px; }
  .life-plan-legend-group-toggle { min-height: 28px; padding-block: 6px; }
  .tx-input { min-height: 36px; }

  /* ======================================================================
     Navigation: slim top bar, page chips, bottom tab bar
     ====================================================================== */
  body { padding-bottom: calc(76px + env(safe-area-inset-bottom, 0px)); }

  .hero {
    flex-wrap: nowrap;
    min-height: 56px;
    padding: 8px 12px 8px 64px;
    border-radius: 18px;
    gap: 10px;
  }
  .hero .liquid-logo.is-small { width: 32px; height: 32px; }
  .hero .eyebrow { display: none; }
  .hero-copy { flex-wrap: nowrap; gap: 8px; min-width: 0; }
  .hero .profile-switcher { flex: 0 1 auto; flex-basis: auto; margin: 0 0 0 auto; min-width: 0; }
  .hero .profile-switcher #profile-chip,
  .hero .profile-switcher .profile-chip { max-width: 100%; }

  .m-subnav {
    display: flex;
    gap: 8px;
    position: sticky;
    top: 0;
    z-index: 40;
    margin: 10px -4px 0;
    padding: 8px 4px;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    background: linear-gradient(to bottom, color-mix(in srgb, var(--bg) 92%, transparent) 70%, transparent);
  }
  .m-subnav[hidden] { display: none; }
  .m-subnav::-webkit-scrollbar { display: none; }
  .m-chip {
    flex: 0 0 auto;
    padding: 8px 14px;
    min-height: 36px;
    border-radius: 999px;
    border: 1px solid rgba(var(--wash), 0.16);
    background: rgba(var(--wash), 0.05);
    color: var(--muted);
    font: inherit;
    font-size: 0.82rem;
    font-weight: 600;
    white-space: nowrap;
  }
  .m-chip.is-active {
    color: var(--btn-ink);
    background: var(--btn-grad);
    border-color: transparent;
  }

  .m-tabbar {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 900;
    display: grid;
    grid-template-columns: repeat(var(--m-tabs, 4), 1fr);
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom, 0px));
    background: var(--bar-bg);
    -webkit-backdrop-filter: blur(18px);
    backdrop-filter: blur(18px);
    border-top: 1px solid rgba(var(--wash), 0.12);
  }
  .m-tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    min-height: 54px;
    border: 0;
    border-radius: 14px;
    background: transparent;
    color: var(--muted);
    font: inherit;
    font-size: 0.68rem;
    font-weight: 600;
    letter-spacing: 0.02em;
  }
  .m-tab[hidden] { display: none; }
  .m-tab svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
  .m-tab.is-active { color: var(--ink); background: rgba(var(--wash), 0.08); }
  .m-tab.is-active svg { stroke: var(--accent); }

  /* The floating save pill sits above the tab bar instead of behind it. */
  .save-indicator { bottom: calc(74px + env(safe-area-inset-bottom, 0px)); right: 12px; }
  body.sidebar-open .m-tabbar { display: none; }

  /* ======================================================================
     Tap-to-open cards, rows and sections
     ====================================================================== */
  .m-zone, .m-acc-head { position: relative; cursor: pointer; -webkit-tap-highlight-color: transparent; }
  .m-zone::after,
  .m-acc-title::after {
    content: "";
    position: absolute;
    right: 4px; top: 50%;
    width: 8px; height: 8px;
    border-right: 2px solid var(--muted);
    border-bottom: 2px solid var(--muted);
    transform: translateY(-70%) rotate(45deg);
    transition: transform 0.18s ease;
    pointer-events: none;
  }
  .m-open > .m-zone::after,
  .m-acc.m-open .m-acc-title::after,
  tr.m-open > .m-summary::after { transform: translateY(-30%) rotate(225deg); }
  .m-card > .m-zone,
  .m-acc > .m-acc-head { padding-right: 26px; }
  /* section headings: title on its own full-width line, arrow at the far right of that line.
     (Eyebrows are dropped here so the title is always the first line.) */
  .m-acc > .m-acc-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-start; gap: 8px 12px; padding-right: 0; }
  .m-acc > .m-acc-head > :first-child { flex: 1 1 100%; width: 100%; min-width: 0; }
  .m-acc-head .eyebrow { display: none; }
  .m-acc-title { display: block; position: relative; margin: 0; padding-right: 34px; }
  .m-acc-title::after { right: 6px; top: 50%; transform: translateY(-65%) rotate(45deg); }
  .m-acc.m-open .m-acc-title::after { transform: translateY(-30%) rotate(225deg); }

  /* folded = one tidy row (title + arrow), everything else in the heading hidden */
  .m-acc:not(.m-open) { padding: 0 !important; }
  .m-acc:not(.m-open) > .m-acc-head { margin: 0; padding: 15px 16px; min-height: 54px; }
  .m-acc:not(.m-open) > .m-acc-head > :not(:first-child) { display: none; }
  .m-acc:not(.m-open) .m-acc-title { font-size: 1.05rem; line-height: 1.25; }
  .m-acc.m-open > .m-acc-head { padding-top: 2px; }

  /* the rest of a folded section stays hidden; the heading row above is all you see */
  .m-acc:not(.m-open) > :not(.m-acc-head) { display: none !important; }
  .m-acc.m-open > .m-acc-head { margin-bottom: 12px; }

  /* Event cards: title + one line of detail; actions appear when opened. */
  .life-plan-event-card.m-card:not(.m-open) .life-plan-event-actions,
  .life-plan-event-card.m-card:not(.m-open) .event-lock-note,
  .life-plan-event-card.m-card:not(.m-open) .gsh-premium-preview { display: none; }
  .life-plan-event-card.m-card:not(.m-open) .event-detail { -webkit-line-clamp: 1; }
  .life-plan-event-card.m-card { gap: 10px; }
  .life-plan-event-card.m-card > .m-zone h3 { margin: 0; }

  /* Asset setup: closed = name, status, source; open = every editable field. */
  .record-card-asset.m-card:not(.m-open) > .record-fields,
  .record-card-asset.m-card:not(.m-open) > .record-actions { display: none; }
  .record-card.m-card > .record-card-head { gap: 8px; }
  .record-card-asset .record-card-subtitle { display: block; }

  /* Holdings: closed = the two numbers that matter (value, total P/L). */
  .record-card-holding.m-card:not(.m-open) .record-field:not(.metric-field),
  .record-card-holding.m-card:not(.m-open) .record-field.metric-field:nth-child(6),
  .record-card-holding.m-card:not(.m-open) .record-field.metric-field:nth-child(7) { display: none; }
  .record-card-holding.m-card:not(.m-open) { gap: 8px; }

  /* Trades, lots, matches, fund holdings: one summary line per row. */
  .tx-table tr.m-row,
  .admin-portfolio-table tr.m-row { gap: 2px 10px; padding: 0 12px; }
  .tx-table tr.m-row > td.m-summary,
  .admin-portfolio-table tr.m-row > td.m-summary {
    display: block;
    grid-column: 1 / -1;
    position: relative;
    padding: 12px 22px 12px 0;
    border-bottom: 0;
  }
  .tx-table tr.m-row.m-open > td.m-summary,
  .admin-portfolio-table tr.m-row.m-open > td.m-summary { border-bottom: 1px dashed var(--line); margin-bottom: 6px; }
  .tx-table tr.m-row:not(.m-open) > td:not(.m-summary),
  .admin-portfolio-table tr.m-row:not(.m-open) > td:not(.m-summary) { display: none; }
  .tx-table tr.m-row.m-open,
  .admin-portfolio-table tr.m-row.m-open { padding-bottom: 10px; }
  /* the Fund name repeats the summary line above it */
  .admin-portfolio-table tr.m-row.m-open > td.m-summary + td { display: none; }
  .m-sum-main { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
  .m-sum-title { font-weight: 700; font-size: 0.95rem; display: inline-flex; align-items: center; gap: 6px; min-width: 0; }
  .m-sum-val { font-weight: 700; font-size: 0.95rem; white-space: nowrap; }
  .m-sum-sub { margin-top: 2px; font-size: 0.76rem; color: var(--muted); }
  .m-tag { font-style: normal; font-size: 0.64rem; letter-spacing: 0.08em; text-transform: uppercase; padding: 2px 7px; border-radius: 999px; background: rgba(var(--wash), 0.08); color: var(--muted); }
  .m-sum-val.is-positive { color: var(--success, #3ddc97); }
  .m-sum-val.is-negative { color: var(--danger, #ff6b86); }

  /* Client directory: the client list leads; the fund-sync tool folds below the search. */
  .admin-portfolio-directory-panel { display: flex; flex-direction: column; }
  .admin-portfolio-directory-panel > .admin-portfolio-heading { order: 0; }
  .admin-portfolio-directory-panel > .admin-portfolio-heading p { display: none; }
  .admin-portfolio-directory-panel > .admin-portfolio-toolbar { order: 1; }
  .admin-portfolio-directory-panel > .admin-portfolio-client-list { order: 2; }
  .admin-portfolio-directory-panel > .status-banner { order: 3; }
  .admin-portfolio-directory-panel > .admin-portfolio-client-form { order: 4; }
  .admin-portfolio-directory-panel > .admin-fund-price-sync { order: 5; }

  /* the open drawer must sit above the sticky page chips and the tab bar */
  .sidebar { z-index: 1100; }
  .sidebar-overlay { z-index: 1050; }

  /* menu button centred in the slim top bar, level with the logo */
  .hero { align-items: center; padding: 8px 12px 8px 62px; }
  .hamburger-btn { top: 50%; left: 10px; width: 40px; height: 40px; }
  .hero-copy { align-items: center; }
  .hero .liquid-logo.is-small { flex: none; align-self: center; }
  /* nobody to switch between (client login): there's room, so name the app */
  .hero-copy:not(:has(#profile-chip)) .eyebrow { display: block; margin: 0; font-size: 0.72rem; letter-spacing: 0.1em; white-space: nowrap; }

  /* Events: one dropdown instead of nine stacked pills */
  .life-plan-event-tabs { display: none; }
  .m-event-select {
    display: block;
    width: 100%;
    min-height: 44px;
    margin: 4px 0 12px;
    padding: 10px 40px 10px 14px;
    border-radius: 14px;
    border: 1px solid rgba(var(--wash), 0.2);
    background-color: rgba(var(--wash), 0.06);
    color: var(--ink);
    font: inherit;
    font-size: 0.95rem;
    font-weight: 600;
  }

  /* Coverage & Income: one swipeable row of small rings (filled ones first) */
  .life-plan-coverage-hint {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    font-size: 0.8rem;
  }
  .life-plan-coverage-rings {
    display: flex;
    flex-wrap: nowrap;
    justify-content: flex-start;
    gap: 8px;
    overflow-x: auto;
    scroll-snap-type: x proximity;
    margin: 0 -6px;
    padding: 4px 6px 10px;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .life-plan-coverage-rings::-webkit-scrollbar { display: none; }
  .coverage-ring-card {
    flex: 0 0 112px;
    max-width: 112px;
    scroll-snap-align: start;
    padding: 10px 6px;
    gap: 4px;
  }
  .coverage-ring-card.is-empty { order: 1; }
  .coverage-ring-card.is-empty .coverage-ring-chart { display: block; opacity: 0.55; }
  .coverage-ring-card .coverage-ring-chart { max-width: 78px; }
  .coverage-ring-title { font-size: 0.66rem; min-height: 2.4em; }
  .coverage-ring-total { font-size: 0.62rem; white-space: nowrap; }
  .coverage-ring-legend { display: none; }
  .life-plan-coverage-rings { align-items: stretch; }
  .coverage-ring-empty-note { display: none; }

  /* Cash / CPF / Investments legend: compact rows (label + one swipeable chip row) */
  .life-plan-chart-legend { gap: 4px; min-width: 0; max-width: 100%; margin-bottom: 6px; }
  .life-plan-legend-group {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    width: 100%;
    box-sizing: border-box;
    min-width: 0;
    padding: 0;
    border: 0;
    background: transparent;
  }
  .life-plan-legend-group-toggle {
    flex: 0 0 72px;
    min-height: 0;
    padding: 0;
    font-size: 0.6rem;
    line-height: 1.25;
    letter-spacing: 0.08em;
    text-align: left;
    white-space: normal;
  }
  .life-plan-legend-group-chips {
    display: flex;
    flex: 1 1 auto;
    flex-wrap: nowrap;
    gap: 6px;
    overflow-x: auto;
    min-width: 0;
    scrollbar-width: none;
  }
  .life-plan-legend-group-chips::-webkit-scrollbar { display: none; }
  .life-plan-legend-btn { flex: 0 0 auto; white-space: nowrap; padding: 5px 9px; min-height: 32px; font-size: 0.72rem; gap: 5px; }
  .life-plan-legend-swatch { width: 9px; height: 9px; }

  /* Asset History: controls only when the section is open, laid out in a clean stack */
  .m-acc:not(.m-open) > .m-acc-head .chart-controls,
  .m-acc:not(.m-open) > .m-acc-head .admin-fund-controls { display: none; }
  .m-acc > .m-acc-head .chart-controls { display: grid; gap: 10px; width: 100%; margin-top: 10px; }
  .chart-select { width: 100%; box-sizing: border-box; min-height: 44px; }
  .chart-range-btns { box-sizing: border-box; }
  .chart-range-btns {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    width: 100%;
    gap: 6px;
    padding: 4px;
  }
  .chart-range-btns .range-btn { min-height: 38px; padding: 6px 4px; text-align: center; }

  /* toasts sit above the tab bar */
  .toast-region { bottom: calc(74px + env(safe-area-inset-bottom, 0px)); }

  /* ======================================================================
     Spacing pass: nothing sits flush against an edge
     ====================================================================== */
  /* "Add Asset" / "Add Transaction" style buttons in an open section: full width, with air around them */
  .m-acc.m-open > .m-acc-head > button,
  .m-acc.m-open > .m-acc-head > .btn { width: 100%; margin: 2px 0 4px; }
  .m-acc.m-open > .m-acc-head { margin-bottom: 14px; }
  .m-acc.m-open { padding-bottom: 18px; }

  /* profile form sections are not panels: give each its own breathing room and a divider */
  .life-profile-section.m-acc { padding: 14px 0; border-top: 1px solid var(--line); }
  .life-profile-section.m-acc:first-child { border-top: 0; padding-top: 4px; }
  .life-profile-section.m-acc:not(.m-open) { padding: 0 !important; }
  .life-profile-section.m-acc:not(.m-open) > .m-acc-head { padding: 14px 0; }

  /* live price tiles: the status pill must stay inside the tile */
  .price-card-top { min-width: 0; align-items: center; }
  .price-card-top > div:first-child { min-width: 0; flex: 1 1 auto; }
  .price-card h4 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .price-card .pill { flex: none; }
  .life-profile-sections { gap: 0; }

  /* Compare / Save-vs-Invest charts: the container must grow with its 360px chart,
     otherwise the chart spills over the summary box beneath it. */
  .life-plan-chart-container:has(> .compare-chart-host) { height: auto !important; margin-bottom: 12px; }
  .life-plan-allocation-benchmark:empty { display: none; }

  /* full-screen dialogs (add/edit event, guided tour) own the whole screen: tab bar and save pill step aside */
  body:has(.life-plan-modal:not([hidden])) .m-tabbar,
  body:has(.life-plan-modal:not([hidden])) .save-indicator,
  body:has(#guide-overlay:not([hidden])) .m-tabbar { display: none; }
  body:has(.life-plan-modal:not([hidden])) .m-subnav { visibility: hidden; }
}
