/* ── 1400px: tablet / mobile ──────────────────────────────────── */

@media (max-width: 1400px) {
  body { overflow: auto; }

  /* Two columns: net asset across the top, maintenance + benchmark (both a
     single figure) side by side, and the wider "updated" tile across the
     bottom — so no tile is left alone next to an empty cell. */
  .stats-strip {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-areas:
      "asset   asset"
      "maint   bench"
      "updated updated";
    gap: 0.6rem;
  }

  .stat { min-width: unset; }

  .asset-value   { grid-area: asset; min-width: unset; }
  .stat--maint   { grid-area: maint; }
  .stat--bench   { grid-area: bench; }
  .stat--updated { grid-area: updated; }

  .layout {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }

  .panel--quotes { order: 1; }
  .panel--table  { order: 2; }

  .home-grid { flex-direction: column; }
}

/* ── 1180px: tighter header (no user-badge text, slimmer tabs) so all tabs fit ── */

@media (max-width: 1180px) {
  .user-badge__info { display: none; }
  .app-header { padding: 0 16px; }
  .app-header__brand { margin-right: 12px; }
  .nav-tab { padding: 10px 11px; font-size: 13px; }
}

/* ── 1020px: two-row header ───────────────────────────────────
   One row needs ~1010px for brand + six tabs + status. Below that, brand and
   status share the top row and the tabs get the full width underneath
   (scrolling sideways if even that isn't enough). */

@media (max-width: 1020px) {
  .app-header { flex-wrap: wrap; height: auto; row-gap: 2px; padding-top: 6px; }
  .app-header__brand,
  .app-header__status { height: 40px; }
  .app-header__status { flex: 1 1 0; min-width: 0; justify-content: flex-end; }
  /* Clip a long status inside the pill (tap it for the full text). */
  #status-pill { display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
  .app-nav { order: 3; flex: 1 0 100%; }
}

/* ── 880px: compact header ────────────────────────────────────── */

@media (max-width: 880px) {
  .app-header__word { font-size: 20px !important; }

  .panel-tabs { flex-direction: column; align-items: flex-start; }
  .panel__actions { width: 100%; justify-content: flex-start; }

  .user-badge { padding: 0.35rem 0.5rem; gap: 0.4rem; }

  .perf-grid { grid-template-columns: 1fr 1fr; }
  .perf-grid--three > :first-child { grid-column: 1 / -1; }
}

/* ── 840px: collapsible benchmark-style card table ─────────────── */

@media (max-width: 840px) {
  .table-wrap {
    overflow: visible;
    border: none;
  }

  /* Cards replace real table columns below this width — auto layout would
     otherwise size the table from the original desktop columns' content and
     overflow the panel, dragging the pledge sub-row's fields off-screen. */
  .holdings-table {
    table-layout: fixed;
    max-width: 100%;
  }

  .holdings-table thead { display: none; }
  /* The ETFs table stays a real table and scrolls sideways (see table.css). */
  .holdings-table.etf-table { table-layout: auto; max-width: none; }
  .holdings-table.etf-table thead { display: table-header-group; }

  /* ── Pledge sub-row: sits between holding cards as a flat block ── */
  .holdings-table tbody tr.pledge-sub-row {
    display: none;
    padding: 0;
    margin-top: -0.4rem;
    margin-bottom: 0.5rem;
    background: transparent;
    border: none;
    cursor: default;
    user-select: auto;
  }

  .holdings-table tbody tr.pledge-sub-row.pledge-sub-row--open {
    display: block;
  }

  .holdings-table tbody tr.pledge-sub-row td {
    display: block !important;
    padding: 0 !important;
    border: none !important;
  }

  /* ── Collapsed (default): compact summary row ── */
  .holdings-table tbody tr.holding-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.7rem 1rem;
    margin-bottom: 0.5rem;
    background: var(--input-bg);
    border: 1px solid var(--panel-border);
    cursor: pointer;
    user-select: none;
  }

  /* Hide all tds in collapsed state */
  .holdings-table tbody tr.holding-row td {
    display: none;
    padding: 0 !important;
    border: none !important;
  }

  /* Show symbol (td 2) */
  .holdings-table tbody tr.holding-row td:nth-child(2) {
    display: flex;
    flex: 0 0 auto;
  }

  /* Name (td 3) fills remaining space */
  #holdings-body tr.holding-row td:nth-child(3) {
    display: flex;
    flex: 1;
    min-width: 0;
  }

  /* Symbol: bold plain text. Sized to its content (ch-based) instead of
     "auto", which falls back to the browser's default ~20-character input
     width and starves the name column next to it down to nothing. */
  .holdings-table tbody tr.holding-row:not(.tr-expanded) td:nth-child(2) input {
    border: none !important;
    background: transparent !important;
    padding: 0 !important;
    font-weight: 700;
    font-size: 0.95rem;
    line-height: 1.5;
    height: 1.6rem;
    -webkit-appearance: none;
    appearance: none;
    pointer-events: none;
    color: var(--ink);
    width: 5.5ch;
  }

  /* Name: muted plain text, truncated. line-height alone isn't reliable
     here — Safari doesn't size single-line text inputs from line-height
     the way Chromium does, so a value that fixes CJK clipping (e.g.
     "台積電") in one engine can still clip Latin text (e.g. "NVIDIA
     Corporation") in the other. -webkit-appearance:none drops Safari's
     own native text-field metrics, and the explicit height is a floor
     every engine has to respect regardless of how it handles line-height.

     :not(.tr-expanded) matters here: this selector's #holdings-body ID
     otherwise outranks the .tr-expanded override further down (which has
     no ID), so its padding:0/border:none kept winning even after the row
     expanded — leaving the expanded input with no padding at all, so its
     focus outline's rounded corner clipped straight through the first
     character (e.g. the "N" in "NVIDIA Corporation"). */
  #holdings-body tr.holding-row:not(.tr-expanded) td:nth-child(3) input {
    border: none !important;
    background: transparent !important;
    padding: 0 !important;
    font-size: 0.85rem;
    font-weight: 400;
    line-height: 1.5;
    height: 1.6rem;
    -webkit-appearance: none;
    appearance: none;
    color: var(--muted);
    pointer-events: none;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Actions cell: always visible, with › chevron */
  .holdings-table tbody tr.holding-row td.holding-row__actions {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    margin-left: auto;
    flex-shrink: 0;
  }

  .holdings-table tbody tr.holding-row td.holding-row__actions::before {
    content: '›';
    font-size: 1.1rem;
    color: var(--muted);
    line-height: 1;
  }

  /* ── Expanded: 2-col grid matching benchmark style ── */
  .holdings-table tbody tr.holding-row.tr-expanded {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.6rem;
    cursor: default;
  }

  .holdings-table tbody tr.holding-row.tr-expanded td {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    width: 100%;
  }

  .holdings-table tbody tr.holding-row.tr-expanded td::before {
    content: attr(data-label);
    font-size: 0.78rem;
    color: var(--muted);
    font-weight: 500;
  }

  /* Restore inputs/selects in expanded state — plain text by default */
  .holdings-table tbody tr.holding-row.tr-expanded td input,
  .holdings-table tbody tr.holding-row.tr-expanded td select {
    border: 1px solid transparent !important;
    background: transparent !important;
    padding: 0.65rem 0.7rem !important;
    pointer-events: auto !important;
    font-weight: normal !important;
    font-size: 1rem !important;
    color: var(--ink) !important;
    width: 100% !important;
    max-width: unset !important;
    white-space: normal !important;
    overflow: visible !important;
    text-overflow: clip !important;
    cursor: pointer !important;
  }

  .holdings-table tbody tr.holding-row.tr-expanded td:not(.holding-row__actions):hover input:not(:focus),
  .holdings-table tbody tr.holding-row.tr-expanded td:not(.holding-row__actions):hover select:not(:focus) {
    border-color: var(--panel-border) !important;
    background: var(--input-bg) !important;
  }

  .holdings-table tbody tr.holding-row.tr-expanded td input:focus,
  .holdings-table tbody tr.holding-row.tr-expanded td select:focus {
    border-color: var(--wood) !important;
    background: var(--input-bg) !important;
    cursor: text !important;
    outline: none !important;
  }

  /* Reset symbol/name flex override in grid context */
  .holdings-table tbody tr.holding-row.tr-expanded td:nth-child(2) { flex: unset; }
  #holdings-body tr.holding-row.tr-expanded td:nth-child(3) { flex: unset; min-width: 0; }

  /* Actions cell: right-aligned, no chevron */
  .holdings-table tbody tr.holding-row.tr-expanded td.holding-row__actions {
    flex-direction: row;
    justify-content: flex-end;
    align-items: center;
    margin-left: 0;
    gap: 0.4rem;
    grid-column: 1 / -1;
  }
  .holdings-table tbody tr.holding-row.tr-expanded td.holding-row__actions::before { content: none; }

  /* Pledge tranche fields: 2-column grid on mobile so the pledge fields and
     the rate-change fields below them line up in the same columns (a plain
     flex-wrap % split drifts out of alignment between the two groups). */
  .pledge-tranche__fields,
  .rate-change-row__fields {
    display: grid;
    grid-template-columns: 1fr 1fr;
    /* Override the desktop flex rule's align-items: flex-end — Safari (unlike
       Chromium) still honors it once display becomes grid, bottom-aligning
       whichever label wraps to fewer lines and staggering the two headers
       vertically instead of keeping them flush on the same row. */
    align-items: start;
  }

  .pledge-tranche__fields label,
  .rate-change-row__fields label {
    min-width: 0;
  }

  /* 到期日 is the odd one out (5th field) — let it span the full row */
  .pledge-tranche__fields label:last-child {
    grid-column: 1 / -1;
  }

  /* Each rate-change row's own remove button (1.8rem + 0.5rem gap) narrows
     its fields grid relative to the pledge fields above, which has no such
     button competing for width. Reserve the same trailing space here so
     both grids compute identical column widths and line up. */
  .pledge-tranche__fields {
    padding-right: 2.3rem;
  }
}

/* ── Transactions ledger as cards (phones) ─────────────────────────
   Eight columns don't fit a phone; each row becomes a two-line card:
     [type] name ............ amount  [✎ ×]
     date   market · symbol   shares   @price
   Cells keep their table order (1 date, 2 holding, 3 type, 4 name,
   5 shares, 6 price, 7 amount, 8 actions) and are placed by grid area. */

@media (max-width: 840px) {
  .txn-table { table-layout: auto; }
  .txn-table tbody tr.txn-row {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto auto;
    grid-template-areas:
      "type name    amount actions"
      "date holding shares price";
    align-items: center;
    gap: 2px 10px;
    padding: 10px 4px;
    border-bottom: 1px dashed var(--row-line);
  }
  .txn-table tbody tr.txn-row td {
    padding: 0;
    border: none;
    min-width: 0;
  }
  .txn-table tbody tr.txn-row td:nth-child(1) { grid-area: date; font-size: 11.5px; color: var(--muted); white-space: nowrap; }
  .txn-table tbody tr.txn-row td:nth-child(2) { grid-area: holding; font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .txn-table tbody tr.txn-row td:nth-child(3) { grid-area: type; }
  .txn-table tbody tr.txn-row td:nth-child(4) { grid-area: name; font-family: var(--font-sans); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .txn-table tbody tr.txn-row td:nth-child(5) { grid-area: shares; font-size: 11.5px; color: var(--muted); text-align: right; white-space: nowrap; }
  .txn-table tbody tr.txn-row td:nth-child(6) { grid-area: price; font-size: 11.5px; color: var(--muted); text-align: right; white-space: nowrap; }
  .txn-table tbody tr.txn-row td:nth-child(7) { grid-area: amount; font-weight: 600; text-align: right; white-space: nowrap; }
  .txn-table tbody tr.txn-row td:nth-child(8) { grid-area: actions; }
  /* A trade reads "10 股 @697.00"; other types keep their own wording. */
  .txn-table tbody tr.txn-row--buy td:nth-child(5)::after,
  .txn-table tbody tr.txn-row--sell td:nth-child(5)::after,
  .txn-table tbody tr.txn-row--stockdiv td:nth-child(5)::after { content: ' 股'; }
  .txn-table tbody tr.txn-row--buy td:nth-child(6)::before,
  .txn-table tbody tr.txn-row--sell td:nth-child(6)::before { content: '@'; }
  .txn-table .txn-row-actions .icon-button { width: 28px; height: 28px; }
}

/* ── 720px: narrow phones ─────────────────────────────────────── */

@media (max-width: 720px) {
  .panel__head,
  .quote-card__top {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }

  /* Keep metrics horizontal — price + change read better side-by-side */
  .quote-card__metrics {
    flex-wrap: wrap;
    gap: 0.4rem;
  }

  .benchmark-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.65rem;
  }

  /* Phone village: less zoom so more cats fit across, and a flatter sign:
     a small cat mark, the title and level on the left, net asset on the
     right; the greeting makes way. */
  .village-hero { --village-height: 280px; --village-zoom: 1.5; }
  .village-hero .home-hero { gap: 10px; padding: 8px 12px; }
  .home-hero__mark { padding: 2px; }
  .home-hero__mark px-cat { zoom: 0.5; }
  .home-hero__text h1 { font-size: 1.05rem; line-height: 1.1; white-space: nowrap; }
  .home-hero__text .lede { display: none; }
  .home-hero__lv { margin-top: 0; }
  .home-hero__asset { padding-left: 0; border-left: none; }
  .home-hero__asset .eyebrow { display: none; }
  .home-hero__asset strong { font-size: 18px; }
  .home-hero__asset .stat-daily-change { font-size: 12px; }

  /* Town: the map runs on under the fixed bottom tab bar (#app drops its
     padding there), and the hint sits just above the bar. */
  #page-town { height: calc(100dvh - var(--header-h, 52px)); }
  .town-hint { top: auto; right: 50%; bottom: calc(76px + env(safe-area-inset-bottom)); transform: translateX(50%); white-space: nowrap; }

  /* ── Phone header + bottom tab bar ──────────────────────────────
     The header keeps just brand + status on one row; the six tabs move to a
     fixed bar along the bottom (icon + short label), like a native app.
     safe-area insets keep both clear of the notch / home indicator when
     installed to the home screen (black-translucent status bar). */
  .app-header {
    flex-wrap: nowrap;
    height: auto;
    padding: calc(6px + env(safe-area-inset-top)) 12px 6px;
  }
  .app-header__brand { margin-right: 0; }
  #status-pill { font-size: 11px; padding: 3px 6px; }
  .app-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 900; /* above page content, below modals (1000) */
    order: 0;
    flex: none;
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 2px;
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
    background: var(--wood);
    border-top: 3px solid var(--wood-dark);
    box-shadow: 0 -4px 14px rgba(61, 51, 38, 0.18);
    overflow: visible;
  }
  .nav-tab,
  .nav-tab--active {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    min-width: 0;
    padding: 5px 0 4px;
    border: none;
    border-radius: var(--radius-md);
    font-size: 11px;
  }
  .nav-tab--active { background: var(--bg); }
  .nav-tab__icon { display: block; font-size: 18px; line-height: 1; }
  .nav-tab__label { display: none; }
  .nav-tab__short { display: block; line-height: 16px; }
  /* Room for the fixed tab bar under the last panel. */
  #app { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  #app:has(#page-town.page--active) { padding-bottom: 0; }
  /* The installed app draws under a translucent status bar: give that strip a
     solid wood backing so scrolled content doesn't show behind the clock. */
  body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: env(safe-area-inset-top);
    background: var(--wood);
    z-index: 900;
  }
}

/* ── 600px: small phones ──────────────────────────────────────── */

@media (max-width: 600px) {
  .page { padding: 0.9rem 0.75rem 1.5rem; }
  #page-home { padding: 0 0 1.5rem; }
  .home-body { padding: 0 0.75rem; }

  .app-header { padding: calc(6px + env(safe-area-inset-top)) 12px 6px; }
  .app-header__brand { margin-right: 4px; }
  .app-header__status { gap: 6px; }
  .app-header__word { font-size: 18px !important; }

  .user-badge { padding: 0.4rem 0.6rem; gap: 0.4rem; }
  .user-badge__avatar { width: 1.4rem; height: 1.4rem; font-size: 0.7rem; }
  .user-badge__name   { font-size: 0.72rem; }
  .user-badge__status { font-size: 0.62rem; }

  .stat { padding: 0.8rem 0.9rem; }
  .stat strong { font-size: 1.45rem; }

  .panel { padding: 0.85rem; }

  .panel-tab { padding: 0.45rem 0.75rem; font-size: 0.8rem; }

  .panel__actions { gap: 0.4rem; }
  .panel__actions .button,
  .panel__actions .button--ghost { padding: 0.45rem 0.65rem; font-size: 0.76rem; }

  .quote-card { padding: 0.8rem; }
  .quote-card__metrics strong { font-size: 1.2rem; }

  .benchmark-grid { grid-template-columns: 1fr 1fr; }

  .txn-summary { flex-direction: column; }
}

/* ── 450px: very small phones ─────────────────────────────────── */

@media (max-width: 450px) {
  .app-header__word { font-size: 16px !important; }

  .user-badge__info { display: none; }
  .user-badge { padding: 0.35rem 0.5rem; gap: 0.4rem; }

  .stat strong { font-size: 1.25rem; }

  .panel-tab { padding: 0.4rem 0.6rem; font-size: 0.76rem; }
}
