/* MARGIN SCOPE v3: local table layers, responsive content and modal geometry. */
:root {
  --topbar-h: 72px;
  --layer-content: 0;
  --layer-sidebar: 20;
  --layer-topbar: 40;
  --layer-loading: 50;
  --layer-toast: 60;
}

html {
  scrollbar-gutter: stable;
  scroll-padding-top: calc(var(--topbar-h) + 12px);
}

.topbar {
  z-index: var(--layer-topbar);
  flex-shrink: 0;
  background: var(--panel);
  isolation: isolate;
}

.brand { flex-shrink: 0; white-space: nowrap; }

.sidebar {
  top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  height: calc(100dvh - var(--topbar-h));
  z-index: var(--layer-sidebar);
  overscroll-behavior: contain;
}

.workspace {
  min-height: calc(100vh - var(--topbar-h));
}

main {
  position: relative;
  z-index: var(--layer-content);
}

.loading { z-index: var(--layer-loading); }
.toast { z-index: var(--layer-toast); overflow-wrap: anywhere; }

.global-search,
.company-top > div,
.metric-group,
.metric-item > *,
.kpi,
.chart-grid > *,
.summary-grid > *,
.analysis-grid > *,
.scenario-inputs > *,
.scenario-results > *,
.panel-head > *,
.table-heading > *,
.seo-panel-head > *,
.seo-keyword-grid > *,
.seo-stock-list > * {
  min-width: 0;
}

.search-results {
  top: calc(100% + 7px);
  z-index: 1;
  max-height: min(360px, calc(100vh - var(--topbar-h) - 16px));
  max-height: min(360px, calc(100dvh - var(--topbar-h) - 16px));
  overscroll-behavior: contain;
}

.search-result span { min-width: 0; overflow-wrap: anywhere; }
.search-result strong { flex-shrink: 0; }
.search-result small { flex: 0 0 auto; }
.company-tools { flex-shrink: 0; }
.company-name h1, .company-sub { overflow-wrap: anywhere; }
.metric-item { flex-wrap: wrap; gap: 3px 7px; }
.metric-item dd { margin-left: auto; overflow-wrap: anywhere; }
.kpi-value, .analysis-number { overflow-wrap: anywhere; white-space: normal; }
.kpi-value small, .metric-item dd small { display: inline-block; white-space: nowrap; overflow-wrap: normal; }
.tab { flex-shrink: 0; }
.panel-head, .table-heading, .table-tools, .chart-foot, .table-foot { flex-wrap: wrap; }
.chart-legend > span { white-space: nowrap; }
.panel-subtitle, .table-heading p, .margin-date-note, .provenance > span { overflow-wrap: anywhere; }
.table-tools { flex-shrink: 0; }
.table-foot > span { min-width: 0; overflow-wrap: anywhere; }
.scenario-inputs { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.scenario-results { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.number-input, .select { min-width: 0; max-width: 100%; }
.distribution-note > div { min-width: 0; }

/* Every app table scrolls inside its own stacking context. */
.table-wrap {
  overflow: auto;
  position: relative;
  isolation: isolate;
  z-index: 0;
  min-width: 0;
  max-width: 100%;
}

.table-scroll, .table-wrap {
  background: var(--panel);
  -webkit-overflow-scrolling: touch;
}

.table-scroll:focus-visible, .table-wrap:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.table-scroll table, .table-wrap table {
  border-collapse: separate;
  border-spacing: 0;
}

.table-scroll thead th, .table-wrap thead th {
  background-clip: border-box;
  /* An opaque edge also covers fractional-pixel paint rounding at zoom. */
  box-shadow: 0 1px 0 var(--line);
}

table.margin-table thead .group-row th:first-child {
  box-shadow: 1px 0 0 var(--line), 0 1px 0 var(--line);
}

.table-wrap tbody { position: relative; z-index: 0; }
.table-wrap tbody td, .table-wrap tbody .heat-cell, .table-wrap tbody .heat { z-index: 0; }
.table-scroll tbody .heat-label, .table-wrap tbody .heat-label { z-index: 1; }
.table-scroll tbody .source-chip, .table-wrap tbody .source-chip { position: relative; z-index: 0; }

.seo-section, .seo-panel { min-width: 0; max-width: 100%; }
.seo-panel-head > div { flex: 1 1 320px; }
.seo-panel-head > .button { flex: 0 0 auto; white-space: normal; }
.seo-panel h2, .seo-panel p, .seo-faq-list summary { overflow-wrap: anywhere; }
.seo-keyword-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 210px), 1fr)); }
.seo-stock-list { grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr)); }
.seo-stock-list a { overflow-wrap: anywhere; }
.seo-ranking-scroll { width: 100%; }
.seo-ranking-table .seo-stock-cell { min-width: 180px; max-width: 280px; white-space: normal; overflow-wrap: anywhere; }

.app-footer .footer-nav a,
.app-footer .footer-nav button,
.app-footer .footer-nav span {
  flex: 0 1 auto;
  min-width: 0;
  max-width: 100%;
  white-space: normal;
  overflow-wrap: anywhere;
  text-align: left;
}

/* Keep price labels in a separate key so equal/nearby prices cannot collide. */
.distance-track-wrap { min-width: 0; padding: 18px 12px 0; }
.distance-key {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin-top: 18px;
  text-align: center;
}
.distance-key > span { min-width: 0; font-size: 10px; color: var(--muted); }
.distance-key b { display: block; color: var(--text); font-size: 12px; overflow-wrap: anywhere; }
.distance-key i { display: inline-block; width: 7px; height: 7px; margin-right: 4px; border-radius: 50%; }

/* Native showModal() uses the browser top layer, above every page z-index. */
html:has(dialog[open]) { overflow: hidden; }
dialog {
  max-width: calc(100vw - 24px);
  max-height: 85vh;
  max-height: min(85dvh, calc(100dvh - 24px));
  overflow: hidden;
}
dialog[open] { display: flex; flex-direction: column; }
.dialog-head { flex: 0 0 auto; }
.dialog-head h2 { min-width: 0; overflow-wrap: anywhere; }
.dialog-close { flex-shrink: 0; }
.dialog-body { flex: 1 1 auto; min-height: 0; min-width: 0; max-height: none; overscroll-behavior: contain; }
.dialog-body p, .dialog-body .import-result { overflow-wrap: anywhere; }
.dialog-body .table-scroll { max-width: 100%; }
.dialog-actions > * { min-width: 0; max-width: 100%; }
.dialog-actions > a { white-space: normal; }

@media (max-width: 1100px) {
  .metrics .metric-group { padding-left: 12px; padding-right: 12px; }
}

@media (max-width: 960px) {
  :root { --topbar-h: 64px; }
  .topbar { gap: 12px; }
  .global-search { flex-basis: 160px; }
  .searchbox kbd { display: none; }
  .analysis-number { font-size: 25px; }
  .top-actions .import-open { padding: 8px; }
  .top-actions .import-open span { display: none; }
}

@media (max-width: 700px) {
  :root { --topbar-h: 104px; }
  .global-search { flex-basis: 100%; }
  .topbar .search-results { top: calc(100% + 7px); }
  .company-top > div:first-child { flex: 1 1 auto; }
  .company-tools { max-width: 64px; }
  .kpi-value { font-size: 23px; }
  .table-tools { width: 100%; }
  .panel-head { gap: 8px 12px; }
  .price-date { margin-left: 0; flex-wrap: wrap; }
  .margin-date-badge { white-space: normal; }
  .distance-stat { flex-wrap: wrap; gap: 6px 12px; }
  .distance-track-wrap { padding: 16px 8px 0; }
  .pane-heading { flex-wrap: wrap; gap: 4px 10px; }
  .pane-heading span { text-align: left; }
  .dialog-body { padding: 16px; }
}

@media (max-width: 360px) {
  .topbar { padding-left: 10px; padding-right: 10px; }
  main { padding-left: 10px; padding-right: 10px; }
  .tabs { gap: 18px; }
  .kpi { padding: 12px; }
  .kpi-value { font-size: 20px; }
  .scenario-inputs { grid-template-columns: 1fr; }
}
