/* Compact Settings interiors: preserve readable controls and reserve footer space. */
@layer desktop-screen-fit {
  @media (min-width: 1101px) {
    /* Move the menu up without shrinking either matched Settings panel. */
    html body #settings-section.banymay-settings .settings-side-panel {
      padding-top: 12px !important;
      padding-bottom: 184px !important;
      justify-content: flex-start !important;
    }
    html body #settings-section.banymay-settings .settings-side-nav {
      margin-top: 2.5cm !important;
    }
    html body #settings-section.banymay-settings :is(#platform-settings-card, #account-settings-card) :is(.settings-platform-layout, .settings-account-layout) {
      display: flex !important;
      flex-direction: column !important;
      gap: 0 !important;
      justify-content: flex-start !important;
    }
    html body #settings-section.banymay-settings :is(#platform-settings-card, #account-settings-card) .settings-form-section {
      flex: 0 0 auto !important;
      min-height: 0 !important;
      padding: 12px 20px !important;
      margin: 0 !important;
      row-gap: 10px !important;
    }
    html body #settings-section.banymay-settings :is(#platform-settings-card, #account-settings-card) .settings-section-intro {
      min-height: 0 !important;
    }
    html body #settings-section.banymay-settings :is(#platform-settings-card, #account-settings-card) .settings-section-intro p {
      margin-top: 5px !important;
      line-height: 1.3 !important;
      max-width: none !important;
    }
    html body #settings-section.banymay-settings :is(#platform-settings-card, #account-settings-card) .settings-section-fields {
      row-gap: 10px !important;
    }
    html body #settings-section.banymay-settings :is(#platform-settings-card, #account-settings-card) :is(.settings-actions--platform, .account-actions--editor) {
      flex: 0 0 auto !important;
      margin-top: auto !important;
      padding: 10px 20px !important;
      min-height: 0 !important;
    }
    html body #settings-section.banymay-settings #account-settings-card button[hidden] {
      display: none !important;
    }
    html body #settings-section.banymay-settings .settings-meta:has(.settings-meta-item:empty) {
      display: none !important;
    }
    html body #settings-section.banymay-settings #contact-us-card .contact-form--reference {
      padding: 20px !important;
      gap: 16px !important;
    }
    html body #settings-section.banymay-settings #contact-us-card .contact-form-fields {
      gap: 12px 20px !important;
    }
    html body #settings-section.banymay-settings #contact-us-card .contact-input-wrap--textarea,
    html body #settings-section.banymay-settings #contact-us-card .contact-input-wrap--textarea textarea {
      min-height: 90px !important;
      height: 90px !important;
    }
    html body #settings-section.banymay-settings #contact-us-card .contact-form-actions {
      margin: 0 !important;
      padding: 0 !important;
    }
    html body #settings-section.banymay-settings #billing-settings-card .billing-panel,
    html body #settings-section.banymay-settings .download-data-page {
      gap: 12px !important;
      padding: 16px !important;
    }
    html body #settings-section.banymay-settings :is(.billing-hero, .billing-expired-features, .billing-expired-security, .download-data-hero, .download-export-options, .download-data-security) {
      padding-block: 14px !important;
      min-height: 0 !important;
    }
    html body #settings-section.banymay-settings #chart-history-card > .settings-content {
      flex: 1 1 0 !important;
      min-height: 0 !important;
    }
    html body #settings-section.banymay-settings #chart-history-card .chart-history-panel {
      display: flex !important;
      flex-direction: column !important;
      flex: 1 1 0 !important;
      min-height: 0 !important;
      padding: 16px !important;
      gap: 12px !important;
    }
    html body #settings-section.banymay-settings #chart-history-card .chart-history-controls {
      flex: 0 0 auto !important;
      margin: 0 !important;
    }
    html body #settings-section.banymay-settings #chart-history-card .chart-history-table-wrap {
      flex: 1 1 0 !important;
      min-height: 0 !important;
      max-height: none !important;
      overflow: auto !important;
    }
  }
}

/* First cascade layer: important fit rules also override older page layers. */
@layer desktop-screen-fit {
  @media (min-width: 1101px) {
    /* Match the sidebar height without letting long subpages enlarge it. */
    html body #settings-section.banymay-settings .settings-page--control-center > .settings-main-panel {
      contain: none !important;
      align-self: start !important;
      min-height: 0 !important;
      overflow: auto !important;
    }
    html body #settings-section.banymay-settings .settings-page--control-center > .settings-main-panel {
      display: flex !important;
      flex-direction: column !important;
      overflow: hidden !important;
    }
    html body #settings-section.banymay-settings .settings-page--control-center .settings-card-stack {
      display: flex !important;
      flex-direction: column !important;
      flex: 1 1 0 !important;
      min-height: 0 !important;
      overflow: hidden !important;
    }
    html body #settings-section.banymay-settings .settings-page--control-center .settings-card-stack > .settings-card[open]:not([hidden]) {
      display: flex !important;
      flex-direction: column !important;
      flex: 1 1 0 !important;
      min-height: 0 !important;
      overflow: auto !important;
      box-sizing: border-box !important;
    }
    html body #settings-section.banymay-settings .settings-page--control-center .settings-card-stack > .settings-card[open]:not([hidden]) > .settings-content {
      display: flex !important;
      flex-direction: column !important;
      flex: 1 0 auto !important;
      box-sizing: border-box !important;
    }
    html body #settings-section.banymay-settings .settings-page--control-center .settings-card-stack > .settings-card[open]:not([hidden]) > .settings-content > :is(.settings-platform-layout, .settings-account-layout, .billing-panel, .download-data-page, .chart-history-panel, .contact-form) {
      flex: 1 0 auto !important;
      box-sizing: border-box !important;
    }
    /* Rows were 55px: 30px action + 24px padding + 1px border.
       Keep the action target and text size; reduce the row to 44px (-20%). */
    html body #settings-section.banymay-settings #chart-history-card .chart-history-table tbody tr:not(.chart-history-empty-row) td {
      height: 44px !important;
      box-sizing: border-box !important;
      padding-block: 6.5px !important;
      line-height: 20px !important;
    }
    html body #settings-section.banymay-settings #chart-history-card .chart-history-action {
      vertical-align: middle !important;
    }
    /* Platform Settings: reference layout only; retain existing theme tokens. */
    html body #settings-section #platform-settings-card .settings-form-section {
      grid-template-columns: minmax(170px, 20%) minmax(0, 1fr) !important;
      column-gap: 30px !important;
      padding: 24px 20px !important;
      align-items: center !important;
    }
    html body #settings-section #platform-settings-card .settings-section-fields {
      grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
      gap: 18px 30px !important;
      max-width: none !important;
      width: 100% !important;
      min-width: 0 !important;
      align-items: center !important;
    }
    html body #settings-section #platform-settings-card .settings-field {
      display: grid !important;
      grid-template-columns: minmax(110px, .85fr) minmax(0, 1.15fr) !important;
      align-items: center !important;
      gap: 14px !important;
      min-width: 0 !important;
      margin: 0 !important;
    }
    html body #settings-section #platform-settings-card .settings-field > label {
      margin: 0 !important;
      text-align: left !important;
    }
    html body #settings-section #platform-settings-card .settings-field > :not(label) {
      box-sizing: border-box !important;
      width: 100% !important;
      min-width: 0 !important;
      max-width: 100% !important;
    }
    html body #settings-section #platform-settings-card .settings-actions--platform {
      display: flex !important;
      justify-content: flex-end !important;
      gap: 12px !important;
      padding: 16px 20px !important;
    }
    /* Fill short app screens so the legal footer rests at the viewport bottom. */
    html body:has(> .container > .section.active) {
      display: flex !important;
      flex-direction: column !important;
      box-sizing: border-box !important;
      min-height: 100vh !important;
      min-height: 100dvh !important;
    }
    html body:has(> .container > .section.active) > .container {
      flex: 1 0 auto !important;
    }
    html body:has(> .container > .section.active) > .app-footer {
      position: static !important;
      flex: 0 0 auto !important;
      box-sizing: border-box !important;
      width: calc(100% - 48px) !important;
      margin: 8px auto !important;
    }
    :root { --desktop-page-gutter: clamp(24px, 2.35vw, 52px); --desktop-page-gap: 24px; }
    body > .container {
      box-sizing: border-box !important;
      width: 100% !important;
      max-width: none !important;
      margin-inline: 0 !important;
      padding-inline: 0 !important;
    }
    .container > .section {
      box-sizing: border-box !important;
      /* Retire the legacy full-bleed left:50% / negative-margin centering. */
      left: auto !important;
      right: auto !important;
      transform: none !important;
      width: 100% !important;
      max-width: none !important;
      min-width: 0 !important;
      margin-inline: 0 !important;
      padding-inline: var(--desktop-page-gutter) !important;
    }
    .container > #dashboard-section .dashboard-content {
      box-sizing: border-box !important;
      /* A viewport-wide summary must not set the grid's intrinsic track width. */
      grid-template-columns: minmax(0, 1fr) !important;
      width: 100% !important;
      max-width: none !important;
      min-width: 0 !important;
      padding-inline: 0 !important;
    }
    .container > #dashboard-section .dashboard-main-grid {
      box-sizing: border-box !important;
      /* Percentage widths already follow the containing block under CSS zoom. */
      width: 100% !important;
      max-width: 100% !important;
      min-width: 0 !important;
      margin-top: 5.2631579mm !important;
      zoom: .95 !important;
      grid-template-columns: minmax(0, 1.08fr) minmax(0, .72fr) minmax(0, .92fr) !important;
    }
    .container > #dashboard-section .dashboard-main-grid > *,
    .container > #dashboard-section .dashboard-advanced-metrics-panel {
      box-sizing: border-box !important;
      min-width: 0 !important;
      max-width: 100% !important;
    }
    .container > #dashboard-section .dashboard-advanced-metrics-panel { width: 100% !important; }
    .container > #dashboard-section .dashboard-summary { margin-bottom: 0 !important; }
    /* Deposits: measured spacing, without stacked legacy margins and gaps. */
    html body .container > #deposits-section {
      padding-top: 0 !important;
    }
    html body .container > #deposits-section .deposits-page-shell {
      gap: 3mm !important;
      margin-top: 0 !important;
      padding-top: 0 !important;
    }
    html body .container > #deposits-section :is(
      .portfolio-summary-strip, .deposits-invested-band, .deposits-history-card
    ) {
      margin-top: 0 !important;
      margin-bottom: 0 !important;
    }
    .container > .section .portfolio-summary-strip {
      box-sizing: border-box !important;
      width: 100% !important;
      max-width: 100% !important;
      margin-inline: 0 !important;
      left: auto !important;
      transform: none !important;
    }
    .container > #dashboard-section .dashboard-performance {
      height: auto !important;
      min-height: 0 !important;
      grid-template-rows: auto auto auto !important;
      padding-bottom: 20px !important;
    }
    .container > #dashboard-section .dashboard-chart-frame {
      height: clamp(240px, 30vh, 360px) !important;
      min-height: 0 !important;
    }
    html:not([data-appearance="light"]) .container > #settings-section {
      background: #0d151b !important;
    }
    html:not([data-appearance="light"]) #settings-section :is(
      .settings-page, .settings-side-panel, .settings-main-panel,
      .settings-card-stack, .settings-card, .settings-content,
      .settings-platform-layout, .settings-form-section
    ) {
      background: #0b1e27 !important;
      box-shadow: none !important;
    }
    .container > #stocks-section .stocks-page-shell,
    .container > #deposits-section > .deposits-page-shell,
    .container > #profile-section > .profile-page,
    .container > #settings-section > .settings-page {
      box-sizing: border-box !important;
      width: 100% !important;
      max-width: none !important;
      min-width: 0 !important;
      margin-inline: 0 !important;
      padding-inline: 0 !important;
    }
    .container > #sectors-section .allocation-container,
    .container > #withdrawals-section .wd-desktop-toolbar,
    .container > #dividends-section .dividends-chart-card,
    .container > #stocks-gains-section .realized-pl-desktop-toolbar,
    .container > #leaderboard-section .leaderboard-page-heading {
      box-sizing: border-box !important;
      width: 100% !important;
      max-width: none !important;
      min-width: 0 !important;
      margin-inline: 0 !important;
    }
    .container > #sectors-section .allocation-container,
    .container > #withdrawals-section .wd-desktop-toolbar,
    .container > #dividends-section .dividends-chart-card,
    .container > #stocks-gains-section .realized-pl-desktop-toolbar,
    .container > #leaderboard-section .leaderboard-page-heading,
    .container > #profile-section > .profile-page,
    .container > #settings-section > .settings-page {
      margin-top: var(--desktop-page-gap) !important;
    }
    .container > #deposits-section .deposits-invested-band,
    .container > #stocks-section .stocks-page-header { margin-top: var(--desktop-page-gap) !important; }
    html body .container > #withdrawals-section > .portfolio-summary-strip,
    html body .container > #dividends-section > .portfolio-summary-strip,
    html body .container > #settings-section > .settings-portfolio-summary {
      margin-bottom: 0 !important;
    }
    html body .container > #withdrawals-section > .wd-desktop-toolbar,
    html body .container > #stocks-gains-section > .realized-pl-desktop-toolbar {
      margin-top: 3mm !important;
    }
    html body .container > #dividends-section {
      row-gap: 3mm !important;
    }
    html body .container > #dividends-section > .dv-desktop-heading {
      /* Its title and actions are relocated; the empty flex item added an extra gap. */
      display: none !important;
    }
    html body .container > #dividends-section > .dividends-chart-card {
      margin-top: 0 !important;
    }
    html body .container > #settings-section > .settings-page.settings-page--control-center {
      margin-top: 3mm !important;
      padding: 0 !important;
      align-items: start !important;
      column-gap: 3mm !important;
      row-gap: 3mm !important;
    }
    html body .container > #settings-section > .settings-page.settings-page--control-center > :is(.settings-side-panel, .settings-main-panel) {
      margin: 0 !important;
    }
    /* The visible subpage must meet the grid edge, without nested outer gutters. */
    html body #settings-section.banymay-settings .settings-main-panel,
    html body #settings-section.banymay-settings .settings-card-stack,
    html body #settings-section.banymay-settings .settings-card-stack > .settings-card,
    html body #settings-section.banymay-settings .settings-card-stack > .settings-card > .settings-content {
      padding: 0 !important;
      margin: 0 !important;
      min-width: 0 !important;
    }
    html body #settings-section.banymay-settings .settings-view-rule {
      display: none !important;
    }
    html body #settings-section.banymay-settings #contact-us-card .contact-form--reference {
      width: 100% !important;
      max-width: none !important;
      margin: 0 !important;
    }
    /* The desktop table wrapper, not its nested toolbar, is the visible panel edge. */
    html body .container > #withdrawals-section > .desktop-table-shell,
    html body .container > #stocks-gains-section > .desktop-table-shell {
      margin-top: 3mm !important;
    }
    html body .container > #withdrawals-section > .portfolio-summary-strip,
    html body .container > #stocks-gains-section > .portfolio-summary-strip {
      margin-bottom: 0 !important;
    }
    .container > #profile-section > .profile-page {
      margin-top: calc(var(--desktop-page-gap) - 1cm) !important;
    }
    #profile-section .rr-assumptions > summary {
      display: grid !important;
      grid-template-columns: 20px minmax(170px, .85fr) minmax(320px, 1.55fr) minmax(390px, 1.55fr) !important;
      align-items: center !important;
      min-height: 42px !important;
      height: 42px !important;
      padding: 0 16px !important;
      column-gap: 0 !important;
    }
    #profile-section .rr-assumptions .rr-assumptions-chevron { grid-column: 1; }
    #profile-section .rr-assumptions > summary > strong { grid-column: 2; }
    #profile-section .rr-assumption-caveat,
    #profile-section .rr-assumption-summary-values {
      box-sizing: border-box !important;
      min-width: 0 !important;
      height: 100% !important;
      display: block !important;
      padding: 0 16px !important;
      border-left: 1px solid rgba(151, 185, 198, .28) !important;
      white-space: nowrap !important;
      overflow: hidden !important;
      text-overflow: ellipsis !important;
      line-height: 42px !important;
    }
    #profile-section .rr-assumption-caveat { grid-column: 3; color: #aebfc8 !important; font-size: 12px !important; }
    #profile-section .rr-assumption-summary-values { grid-column: 4; margin-left: 0 !important; color: #d4dfe4 !important; font-size: 11px !important; }
    #profile-section .rr-cash-confirmation { display: flex !important; align-items: center; gap: 8px; }
    #profile-section .rr-cash-confirmation input { width: 16px; height: 16px; margin: 0; accent-color: #23dee6; }
    #profile-section .rr-kpi-strip { margin-top: -3mm !important; }
    #profile-section .rr-summary[role="status"][hidden] { display: none !important; }
  }
  @media (min-width: 761px) {
    [data-screen-fit] {
      box-sizing: border-box !important;
      min-height: 0 !important;
      max-width: 100% !important;
    }
    [data-screen-fit] .allocation-container,
    [data-screen-fit] .wd-desktop-toolbar,
    [data-screen-fit] .deposits-history-card {
      margin-top: clamp(12px, 2vh, 24px) !important;
    }
    [data-screen-fit] .dashboard-chart-frame {
      height: clamp(170px, 25vh, 320px) !important;
      min-height: 0 !important;
    }
    [data-screen-fit] .dividends-chart-canvas-wrap {
      flex: 0 0 auto !important;
      height: clamp(160px, 26vh, 320px) !important;
      min-height: 0 !important;
      max-height: none !important;
    }
    [data-screen-fit] .dividends-chart-card {
      min-height: 0 !important;
    }
    [data-screen-fit] .deposits-chart-canvas-wrap,
    [data-screen-fit] .admin-chart-canvas-wrap {
      height: clamp(170px, 28vh, 340px) !important;
      min-height: 0 !important;
    }
    [data-screen-fit] .dashboard-content {
      padding-bottom: 12px !important;
      row-gap: 10px !important;
    }
    [data-screen-fit] .my-plan-view,
    [data-screen-fit] .settings-main-panel {
      min-width: 0 !important;
    }
    [data-screen-fit].login-container {
      padding-top: clamp(16px, 4vh, 48px) !important;
      padding-bottom: 16px !important;
    }
    [data-screen-fit] .register-header {
      position: relative !important;
      top: auto !important;
      left: auto !important;
      transform: none !important;
    }
    [data-screen-fit] .login-layout,
    [data-screen-fit] .register-layout {
      min-height: 0 !important;
      margin-top: clamp(16px, 4vh, 44px) !important;
    }
    body:has([data-screen-fit]) > .app-footer {
      margin-top: 8px !important;
      margin-bottom: 8px !important;
      padding-block: 8px !important;
    }
    @media (max-height: 850px) {
      [data-screen-fit] .my-plan-panel,
      [data-screen-fit] .rr-panel {
        padding-top: 10px !important;
        padding-bottom: 10px !important;
      }
      [data-screen-fit] .my-plan-retirement-row {
        min-height: 36px !important;
        padding-block: 6px !important;
      }
      [data-screen-fit] .wd-total-row,
      [data-screen-fit] .wd-desktop-footer {
        min-height: 48px !important;
        padding-block: 10px !important;
      }
      [data-screen-fit] .dashboard-insight-card {
        min-height: 48px !important;
        padding-block: 5px !important;
      }
    }
  }
}
