/* Presentation only. Loaded last so workspace breakpoints share one layout. */
:root {
  --topbar: 72px;
}

html {
  scroll-padding-top: calc(var(--topbar) + 16px);
}

.view > *,
.surface,
.surface-header > *,
.page-heading > *,
.heading-actions,
.config-layout > *,
.config-form > *,
.config-list-row > *,
.hedge-command-center > *,
.payoff-workspace > *,
.straddle-premium-workspace,
.chart-panel,
.chart-host,
.canvas-wrap {
  min-width: 0;
}

.page-heading,
.surface-header,
.heading-actions,
.config-actions,
.config-form-actions,
.control-actions {
  flex-wrap: wrap;
}

.page-heading > :first-child {
  flex: 1 1 240px;
}

.page-heading > .heading-actions {
  flex: 0 1 auto;
  max-width: 100%;
}

.page-heading h1,
.surface-header h2,
.drawer-header h2,
.inline-status,
.stale-banner,
.alert-title,
.alert-message,
.config-help {
  overflow-wrap: anywhere;
}

.data-table-wrap,
.drawer-table {
  min-width: 0;
  max-width: 100%;
  overscroll-behavior-x: contain;
}

.search-control,
.select-control,
.select-control select,
.date-control,
.config-form input,
.config-form select {
  min-width: 0;
  max-width: 100%;
}

/* Header: give the quotes their own grid track, clear of account actions. */
.topbar {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 14px;
  height: var(--topbar);
}

.sidebar-collapse,
.menu-button {
  grid-column: 1;
  grid-row: 1;
  margin-right: 0;
}

.topbar-actions {
  grid-column: 3;
  grid-row: 1;
  min-width: 0;
  margin-left: 0;
}

.topbar .market-strip {
  position: relative;
  inset: auto;
  grid-column: 2;
  grid-row: 1;
  justify-self: center;
  width: 100%;
  max-width: 660px;
  min-width: 0;
  transform: none;
}

.topbar .market-index-details {
  max-width: calc(100vw - 24px);
  max-height: calc(100dvh - var(--topbar) - 20px);
  overflow-y: auto;
}

.stale-banner {
  top: var(--topbar);
}

#view-pnl .pnl-controls,
.hedge-account-rail,
.payoff-rail {
  top: calc(var(--topbar) + 16px);
}

.hedge-account-rail {
  max-height: calc(100dvh - var(--topbar) - 32px);
}

/* Charts and configuration must also fit when the desktop menu is expanded. */
.straddle-unified-header,
.straddle-axis-guide,
.straddle-live-values,
.payoff-title-row,
.payoff-range-presets,
.payoff-sync {
  flex-wrap: wrap;
}

.straddle-unified-panel > .surface-header {
  width: auto;
}

.straddle-axis-guide span {
  white-space: normal;
}

.straddle-live-values strong {
  white-space: normal;
  overflow-wrap: anywhere;
}

.payoff-shock-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
}

.option-limit-matrix {
  grid-template-columns: minmax(54px, .7fr) repeat(2, minmax(0, 1fr));
}

.app-confirm-dialog {
  max-height: calc(100dvh - 36px);
  overflow-y: auto;
}

.global-alert-popup {
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
}

@media (max-width: 1600px) {
  #config-lead-weights .config-lead-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #config-lead-weights .config-lead-row > span {
    grid-column: 1 / -1;
    overflow-wrap: anywhere;
  }
}

@media (max-width: 1400px) {
  :root {
    --topbar: 136px;
  }

  .topbar {
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: 44px minmax(0, 1fr);
    gap: 8px 12px;
    padding-block: 8px;
  }

  .topbar-actions {
    grid-column: 2;
    justify-self: end;
  }

  .topbar .market-strip {
    grid-column: 1 / -1;
    grid-row: 2;
    max-width: none;
  }

  .market-index-summary {
    min-height: 60px;
  }

  .config-layout,
  .hedge-command-center {
    grid-template-columns: minmax(0, 1fr);
  }

  .hedge-account-rail {
    position: static;
    max-height: none;
  }

  .hedge-account-list {
    max-height: 300px;
  }

  .config-pnl-window-row {
    grid-template-columns: 44px minmax(0, 1fr) auto;
  }

  .config-pnl-window-session,
  .config-pnl-window-toggle {
    grid-column: 1 / -1;
  }
}

@media (max-width: 1000px) {
  .intraday-workspace-grid,
  .payoff-workspace {
    grid-template-columns: minmax(0, 1fr);
  }

  .intraday-toolbar,
  .intraday-history-state {
    flex-wrap: wrap;
  }

  .intraday-shadow-field {
    min-width: 0;
    flex: 1 1 300px;
  }

  .intraday-history-state {
    justify-content: flex-start;
    text-align: left;
  }

  .payoff-rail {
    position: static;
    max-height: none;
  }

  .payoff-ticker-list {
    display: flex;
    overflow-x: auto;
  }

  .payoff-ticker {
    flex: 0 0 210px;
  }

  .straddle-unified-title {
    flex-basis: 100%;
  }

  .account-select-tools {
    width: 100%;
    justify-content: flex-start;
  }

  .account-select-tools > * {
    flex: 1 1 200px;
  }
}

@media (max-width: 900px) {
  .sidebar {
    width: min(var(--sidebar), calc(100vw - 44px));
    height: 100dvh;
    padding-bottom: max(18px, env(safe-area-inset-bottom));
  }

  .brand,
  .sidebar-footer {
    flex-shrink: 0;
  }

  .nav-item {
    min-height: 44px;
  }

  .page-heading > :first-child {
    flex-basis: auto;
  }

  #view-pnl .pnl-controls {
    position: static;
  }

  .straddle-premium-chart,
  .straddle-canvas-wrap {
    height: clamp(300px, 55svh, 480px);
    min-height: 0;
  }

  .hedge-expiry-row {
    min-width: 0;
    grid-template-columns: 110px repeat(2, minmax(0, 1fr));
  }

  .hedge-side-head,
  .hedge-side-foot,
  .hedge-panel-heading {
    flex-wrap: wrap;
  }
}

@media (max-width: 680px) {
  :root {
    --topbar: 152px;
    --ui-page-x: 12px;
  }

  .topbar {
    padding-inline: var(--ui-page-x);
  }

  .topbar-actions {
    gap: 6px;
  }

  .topbar .live-state {
    display: flex;
  }

  .topbar .icon-button,
  .drawer-header .icon-button {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
  }

  .topbar-logout {
    min-height: 44px;
    padding-inline: 10px;
  }

  .market-strip-grid {
    gap: 6px;
  }

  .market-index-summary--premium {
    min-height: 82px;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto auto;
    align-items: center;
    gap: 3px;
    padding: 7px;
  }

  .market-index-label {
    gap: 5px;
    font-size: 9px;
  }

  .market-index-summary--premium .market-index-price {
    overflow: visible;
    font-size: clamp(11px, 3.3vw, 14px);
  }

  .market-index-compact-premium {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 3px;
    padding: 4px 0 0;
    border-left: 0;
    border-top: 1px solid var(--ui-line-soft);
  }

  .market-index-compact-premium small {
    height: auto;
    font-size: 7px;
    line-height: 1.2;
    letter-spacing: 0;
  }

  .market-index-compact-premium small::after {
    content: none;
  }

  .market-index-compact-premium strong {
    overflow: visible;
    font-size: 11px;
  }

  .topbar .market-index-details {
    position: fixed;
    inset: calc(var(--topbar) + 6px) 10px auto;
    width: auto;
    transform: none;
  }

  .page,
  body.accounts-view-active .page {
    padding: 18px var(--ui-page-x) max(32px, env(safe-area-inset-bottom));
  }

  .view.active {
    animation: none;
    transform: none;
  }

  .page-heading {
    gap: 14px;
    margin-bottom: 16px;
  }

  .page-heading > *,
  .page-heading > .heading-actions {
    width: 100%;
  }

  .heading-actions {
    gap: 8px;
  }

  .heading-actions > button {
    flex: 1 1 130px;
  }

  .surface-header {
    gap: 10px;
    align-items: flex-start;
  }

  .surface-header > .row-count {
    margin-left: 0;
  }

  .primary-button,
  .secondary-button,
  .danger-button,
  .picker-trigger,
  .compact-picker .secondary-button,
  .picker-footer .primary-button,
  .picker-option,
  .straddle-picker-trigger,
  .select-control select,
  .date-control,
  .search-control,
  .toggle-control,
  .segmented button {
    min-height: 44px;
  }

  .primary-button,
  .secondary-button,
  .danger-button {
    white-space: normal;
  }

  /* All table tabs: full-width search, paired filters, local table scrolling. */
  .table-toolbar,
  .chart-toolbar,
  .pnl-controls-main {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    align-items: stretch;
  }

  .table-toolbar > *,
  .chart-toolbar > *,
  .pnl-controls-main > * {
    min-width: 0;
    max-width: 100%;
    width: 100%;
    margin-left: 0;
  }

  .table-toolbar > .search-control,
  .table-toolbar > .select-control:has(input[type="date"]),
  .table-toolbar > .row-count,
  .table-toolbar > .segmented,
  .chart-toolbar > *,
  .pnl-controls-main > .search-control,
  .pnl-controls-main > .account-picker {
    grid-column: 1 / -1;
  }

  .table-toolbar .search-control,
  .table-toolbar .account-picker,
  .pnl-symbol-search,
  .pnl-controls-main .account-picker {
    min-width: 0;
  }

  .select-control select,
  .date-control {
    width: 100%;
  }

  .segmented {
    flex-wrap: wrap;
    gap: 3px;
    overflow: visible;
  }

  .segmented button {
    flex: 1 1 auto;
  }

  .pagination,
  .pagination-controls {
    flex-wrap: wrap;
  }

  .pagination .icon-button {
    width: 40px;
    height: 40px;
  }

  /* Overview and intraday: preserve clear labels and unclipped values. */
  .overview-summary-switch {
    flex-wrap: wrap;
  }

  .overview-hedge,
  .surface:not(.table-surface):not(.straddle-unified-panel):not(.hedge-console):not(.hedge-account-rail) {
    padding: 14px;
  }

  .intraday-status-panel.surface,
  .intraday-slippage-panel.surface {
    padding: 0;
  }

  .intraday-toolbar > *,
  .intraday-history-state {
    width: 100%;
  }

  .intraday-toolbar .intraday-shadow-field {
    flex-basis: auto;
  }

  .intraday-shadow-field .select-control {
    flex-basis: auto;
  }

  /* Accounts: keep identification visible without covering the data columns. */
  #view-accounts {
    --mobile-account-column-width: clamp(96px, 28vw, 116px);
  }

  #view-accounts #accounts-table th:first-child,
  #view-accounts #accounts-table td:first-child {
    width: var(--mobile-account-column-width);
    min-width: var(--mobile-account-column-width);
    max-width: var(--mobile-account-column-width);
    padding-inline: 6px;
    overflow: hidden;
  }

  #view-accounts #accounts-table .account-cell strong {
    max-width: calc(var(--mobile-account-column-width) - 12px);
    font-size: 11px;
  }

  #view-accounts #accounts-table .account-cell > span {
    max-width: calc(var(--mobile-account-column-width) - 12px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Straddle / payoff: controls wrap above a viewport-sized plotting area. */
  .straddle-control.straddle-picker,
  .straddle-risk-account,
  .straddle-live-values {
    width: 100%;
  }

  .straddle-live-values {
    justify-content: flex-start;
  }

  .straddle-live-values > span {
    flex: 1 1 110px;
    min-width: 0;
  }

  .straddle-unified-title {
    min-width: 0;
  }

  .straddle-legend-item,
  .straddle-scope-tab,
  .payoff-mode button,
  .payoff-range-presets button,
  .payoff-sync button {
    min-height: 40px;
  }

  .payoff-card-head {
    padding: 14px;
  }

  .payoff-metric strong {
    font-size: 15px;
    overflow-wrap: anywhere;
    white-space: normal;
  }

  .payoff-canvas {
    height: clamp(280px, 50svh, 380px);
  }

  /* Hedge: expiry, call and put cards stack without a second horizontal desk. */
  .hedge-console-tabs {
    flex-wrap: wrap;
    overflow: visible;
  }

  .hedge-console-tabs button {
    min-height: 44px;
    flex: 1 1 120px;
  }

  .hedge-expiry-row {
    grid-template-columns: minmax(0, 1fr);
  }

  .hedge-expiry-identity {
    border-right: 0;
    border-bottom: 1px solid var(--ui-line);
  }

  .hedge-side-card {
    border-right: 0;
    border-bottom: 1px solid var(--ui-line-soft);
  }

  .hedge-policy-section,
  .hedge-manual-layout > * {
    min-width: 0;
  }

  .hedge-console-header .heading-actions {
    width: 100%;
  }

  /* Slippage: two frozen columns would cover almost the entire phone screen. */
  #view-slippage .operations-table th:nth-child(2),
  #view-slippage .operations-table td:nth-child(2) {
    position: static;
  }

  #view-slippage .kpi-grid,
  .alerts-help-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .alert-content,
  .alert-meta {
    overflow-wrap: anywhere;
  }

  .control-actions > button {
    width: 100%;
  }

  /* Configuration: fit nested editors as well as their outer panels. */
  .config-form {
    padding: 14px 0;
    grid-template-columns: minmax(0, 1fr);
  }

  .config-form input:not([type="checkbox"]),
  .config-form select {
    min-height: 44px;
  }

  .config-add-row,
  .config-actions,
  .config-form-actions {
    flex-wrap: wrap;
    gap: 8px;
  }

  .config-add-row > *,
  .config-form-actions > button {
    flex: 1 1 140px;
  }

  .config-list-row {
    gap: 8px;
    padding: 10px;
  }

  #config-lead-weights input[type="time"] {
    grid-column: 1 / -1;
  }

  .config-pnl-window-time-range {
    grid-template-columns: minmax(0, 1fr);
  }

  .config-pnl-window-time-arrow {
    display: none;
  }

  /* Pickers and drawers use the available height, including short screens. */
  .picker-menu,
  #account-column-menu,
  #account-total-menu,
  .straddle-picker-menu,
  .straddle-picker-menu--account {
    position: fixed;
    inset: calc(var(--topbar) + 8px) 10px auto;
    width: auto;
    max-height: calc(100dvh - var(--topbar) - 20px);
    display: flex;
    flex-direction: column;
    overflow: auto;
  }

  .account-picker {
    min-height: 44px;
  }

  .account-picker > button[aria-expanded="true"] {
    position: fixed;
    inset: calc(var(--topbar) + 8px) 10px auto;
    z-index: 31;
    width: auto;
    min-height: 48px;
    background: var(--ui-panel-alt);
  }

  .account-picker .picker-menu,
  #account-column-menu,
  #account-total-menu {
    top: calc(var(--topbar) + 62px);
    max-height: calc(100dvh - var(--topbar) - 74px);
  }

  .picker-head,
  .picker-footer,
  .straddle-picker-menu-head {
    flex-shrink: 0;
  }

  .picker-options,
  #account-total-menu .custom-total-options,
  #account-column-menu .picker-options,
  .straddle-picker-options {
    min-height: 0;
    max-height: none;
    overflow-y: auto;
  }

  .drawer,
  .drawer--wide {
    width: 100%;
    max-height: 100dvh;
  }

  .drawer-header,
  .drawer-body {
    padding: 14px;
  }

  .drawer-header > :first-child {
    min-width: 0;
  }

  .drawer-position-tools {
    flex-wrap: wrap;
    width: 100%;
  }

  .drawer-section-head {
    flex-direction: column;
    align-items: stretch;
  }

  .drawer-position-tools .drawer-position-search {
    min-width: 0;
    width: 100%;
    min-height: 44px;
    flex-basis: 100%;
    padding: 0 12px;
    border: 1px solid var(--ui-line);
    border-radius: 8px;
    background: var(--ui-panel-alt);
    color: var(--ui-copy);
  }

  .drawer-position-status {
    width: 100%;
  }

  .app-confirm-dialog {
    gap: 10px;
    padding: 16px;
  }

  .app-confirm-actions {
    flex-wrap: wrap;
  }
}

@media (max-width: 380px) {
  .topbar .live-state {
    gap: 0;
  }

  .topbar #live-label {
    display: none;
  }

  .market-index-compact-premium {
    flex-wrap: wrap;
  }
}

@media (max-height: 540px) and (max-width: 1000px) {
  .topbar,
  .stale-banner {
    position: relative;
    top: auto;
  }

  .picker-menu,
  #account-column-menu,
  #account-total-menu,
  .straddle-picker-menu,
  .straddle-picker-menu--account,
  .topbar .market-index-details {
    position: fixed;
    inset: 10px;
    width: auto;
    max-width: none;
    max-height: calc(100dvh - 20px);
    overflow-y: auto;
    transform: none;
  }

  .account-picker > button[aria-expanded="true"] {
    top: 10px;
  }

  .account-picker .picker-menu,
  #account-column-menu,
  #account-total-menu {
    top: 64px;
    max-height: calc(100dvh - 74px);
  }
}

/* Prevent mobile browsers from zooming the page when editing a field. */
@media (max-width: 900px) and (pointer: coarse) {
  input:not([type="checkbox"]):not([type="range"]),
  select,
  textarea {
    font-size: 16px;
  }
}
