:root {
  --ink: #1c2530;
  --muted: #64707d;
  --line: #d8dde3;
  --panel: #ffffff;
  --soft: #f4f6f8;
  --soft-red: #fff4f6;
  --aia-red: #d31145;
  --teal: #0f766e;
  --amber: #a16207;
  --blue: #2563eb;
  --shadow: 0 18px 45px rgba(28, 37, 48, 0.1);
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: #eef1f4;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  overflow-x: hidden;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
  touch-action: manipulation;
}

.shell {
  min-height: 100vh;
  padding: 22px;
}

.page {
  display: none;
  max-width: 1480px;
  margin: 0 auto;
}

.page-active {
  display: block;
}

.app-header,
.panel-heading,
.panel-actions,
.control-strip,
.chart-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.analysis-editor {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(340px, 0.9fr);
  gap: 18px;
  margin-bottom: 18px;
}

.optimiser-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(320px, 0.75fr);
  gap: 18px;
  margin-bottom: 18px;
}

.app-header {
  margin-bottom: 18px;
}

.analysis-header {
  margin-bottom: 12px;
}

.header-actions,
.confirm-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--aia-red);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  letter-spacing: 0;
}

h1,
h2,
h3 {
  margin: 0;
}

h1 {
  font-size: 30px;
}

h2 {
  font-size: 20px;
}

.setup-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.08fr) minmax(360px, 0.92fr);
  gap: 18px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.primary-panel {
  min-height: 520px;
}

.panel-heading {
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.tight-heading {
  padding-bottom: 14px;
}

.holding-rows {
  padding: 10px;
}

.compact-rows {
  padding: 10px 0 0;
}

.holding-row {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) 120px 44px;
  gap: 10px;
  align-items: end;
  padding: 10px;
  border-bottom: 1px solid var(--line);
}

.benchmark-rows {
  padding: 10px;
}

.benchmark-row {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) 44px;
  gap: 10px;
  align-items: end;
  padding: 10px;
  border-bottom: 1px solid var(--line);
}

.benchmark-row:last-child {
  border-bottom: 0;
}

.holding-row:last-child {
  border-bottom: 0;
}

label span {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

input,
select {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 9px 11px;
  outline: none;
}

select {
  appearance: auto;
}

input:focus,
select:focus {
  border-color: var(--aia-red);
  box-shadow: 0 0 0 3px rgba(211, 17, 69, 0.12);
}

.primary-button,
.secondary-button,
.ghost-button,
.remove-button,
.segmented button {
  min-height: 40px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 0 14px;
  font-weight: 800;
}

.primary-button {
  border-color: var(--aia-red);
  background: var(--aia-red);
  color: #fff;
}

.primary-button:disabled {
  border-color: var(--line);
  background: #c5cbd2;
  cursor: not-allowed;
}

.secondary-button:hover,
.ghost-button:hover,
.remove-button:hover,
.segmented button:hover {
  border-color: var(--aia-red);
}

.ghost-button {
  color: var(--muted);
}

.remove-button {
  width: 44px;
  padding: 0;
  color: var(--muted);
  font-size: 18px;
}

.panel-actions {
  justify-content: flex-start;
  flex-wrap: wrap;
  padding: 14px 20px 20px;
}

.weight-pill {
  min-width: 74px;
  border: 1px solid var(--line);
  background: var(--soft);
  padding: 8px 10px;
  text-align: center;
  font-weight: 900;
}

.valid-pill {
  border-color: rgba(15, 118, 110, 0.35);
  background: #effcf9;
  color: var(--teal);
}

.warning-pill {
  border-color: rgba(161, 98, 7, 0.35);
  background: #fff8e8;
  color: var(--amber);
}

.muted-pill {
  color: var(--muted);
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 16px 20px 8px;
}

.segmented button {
  width: 100%;
}

.segmented .segment-active {
  border-color: var(--aia-red);
  background: var(--soft-red);
  color: var(--aia-red);
}

.comparison-mode {
  padding: 12px 20px 20px;
}

.hidden {
  display: none;
}

.setup-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-top: 18px;
  padding: 18px 20px;
}

.inline-controls {
  width: 180px;
}

.confirm-block {
  display: flex;
  align-items: center;
  gap: 14px;
}

.confirm-actions {
  flex-shrink: 0;
}

.status-text {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.control-strip {
  flex-wrap: wrap;
  margin-bottom: 14px;
  padding: 10px 14px;
}

.date-controls {
  display: grid;
  grid-template-columns: 160px 160px;
  gap: 12px;
  margin-left: auto;
}

.period-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.period-buttons button {
  min-height: 34px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 0 12px;
  font-weight: 900;
}

.period-buttons .segment-active {
  border-color: var(--aia-red);
  background: var(--soft-red);
  color: var(--aia-red);
}

.chart-panel {
  margin-bottom: 18px;
}

.chart-heading {
  padding: 18px 20px 10px;
}

.chart-wrap {
  position: relative;
  height: 430px;
  padding: 0 16px 16px;
}

.chart-meta {
  padding: 0 20px 14px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  border: 1px solid var(--line);
  background: #fff;
}

.chart-tooltip {
  position: absolute;
  min-width: 240px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.98);
  box-shadow: 0 18px 42px rgba(28, 37, 48, 0.18);
  padding: 12px;
  pointer-events: none;
  font-size: 12px;
  z-index: 2;
}

.tooltip-date {
  margin-bottom: 8px;
  color: var(--ink);
  font-weight: 900;
}

.tooltip-line {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  margin-top: 8px;
}

.tooltip-line span {
  color: var(--muted);
  font-weight: 800;
}

.tooltip-line strong {
  color: var(--ink);
}

.tooltip-subline {
  margin: 2px 0 0 19px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.legend-swatch {
  width: 12px;
  height: 12px;
}

.top-button {
  position: fixed;
  right: 22px;
  bottom: 22px;
  min-width: 74px;
  min-height: 42px;
  border: 1px solid var(--line);
  background: var(--ink);
  color: #fff;
  box-shadow: 0 16px 34px rgba(28, 37, 48, 0.22);
  font-weight: 900;
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
  transition: opacity 160ms ease, transform 160ms ease;
  z-index: 8;
}

.top-button-visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.top-button:hover,
.top-button:focus-visible {
  background: var(--aia-red);
}

.stats-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 18px;
  margin-bottom: 18px;
}

.optimiser-form {
  display: grid;
  grid-template-columns: repeat(2, minmax(180px, 1fr));
  gap: 14px;
  padding: 16px 20px 4px;
}

.optimiser-help {
  align-self: start;
}

.optimiser-copy {
  display: grid;
  gap: 10px;
  padding: 16px 20px 20px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.45;
}

.optimiser-copy p {
  margin: 0;
}

.optimiser-results-panel {
  margin-bottom: 18px;
}

.optimiser-results {
  padding: 0 20px 20px;
}

.optimiser-stat-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
}

.optimiser-stat {
  min-width: 140px;
  border: 1px solid var(--line);
  background: var(--soft);
  padding: 12px;
}

.optimiser-stat span,
.optimiser-alt-card span,
.optimiser-alt-card small,
.optimiser-row span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.optimiser-stat strong {
  display: block;
  margin-top: 7px;
  font-size: 20px;
}

.optimiser-table {
  overflow-x: auto;
  border: 1px solid var(--line);
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
}

.optimiser-row {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) minmax(160px, 0.5fr);
  gap: 16px;
  align-items: center;
  min-width: 560px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}

.optimiser-row:last-child {
  border-bottom: 0;
}

.optimiser-head {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.optimiser-weight-bar {
  height: 8px;
  margin-bottom: 6px;
  background: var(--soft);
}

.optimiser-weight-bar span {
  display: block;
  height: 100%;
  background: var(--aia-red);
}

.optimiser-alt {
  margin-top: 18px;
}

.optimiser-alt h3 {
  margin-bottom: 10px;
  font-size: 15px;
}

.optimiser-alt-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(140px, 1fr));
  gap: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
}

.optimiser-alt-card {
  min-width: 140px;
  border: 1px solid var(--line);
  padding: 12px;
}

.optimiser-alt-card strong {
  display: block;
  margin-top: 6px;
  font-size: 18px;
}

.performance-panel {
  margin-bottom: 18px;
}

.performance-table {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
  padding: 0 20px 20px;
}

.performance-grid {
  display: grid;
  grid-template-columns: minmax(190px, 1.2fr) repeat(8, minmax(88px, 0.7fr));
  min-width: 890px;
}

.performance-cell {
  min-height: 40px;
  border-bottom: 1px solid var(--line);
  padding: 9px 8px;
  text-align: right;
  font-size: 14px;
}

.performance-cell:first-child {
  text-align: left;
  font-weight: 900;
}

.performance-head {
  color: var(--ink);
  font-weight: 900;
}

.performance-subtle {
  color: var(--muted);
  font-size: 12px;
}

.stats-table {
  display: grid;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
  padding: 0 20px 20px;
}

.stats-row,
.stats-head {
  display: grid;
  grid-template-columns: minmax(190px, 1fr) minmax(120px, 0.7fr) minmax(120px, 0.7fr);
  gap: 12px;
  align-items: center;
  min-height: 44px;
  border-bottom: 1px solid var(--line);
}

.stats-head {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.stats-row:last-child {
  border-bottom: 0;
}

.metric-name {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.metric-detail {
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  line-height: 1.25;
}

.tooltip-label {
  display: inline;
  color: var(--muted);
  border-bottom: 1px dotted rgba(100, 112, 125, 0.7);
  cursor: help;
  text-underline-offset: 4px;
}

.tooltip-label:hover,
.tooltip-label:focus {
  color: var(--ink);
  border-bottom-color: var(--aia-red);
  outline: none;
}

.info-tooltip {
  position: fixed;
  z-index: 50;
  max-width: 340px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  box-shadow: 0 18px 42px rgba(28, 37, 48, 0.2);
  padding: 13px 14px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1.45;
}

.info-tooltip::before {
  content: "";
  position: absolute;
  top: -6px;
  left: 18px;
  width: 10px;
  height: 10px;
  border-left: 1px solid var(--line);
  border-top: 1px solid var(--line);
  background: #fff;
  transform: rotate(45deg);
}

.info-tooltip-title {
  margin-bottom: 5px;
  color: var(--ink);
  font-size: 12px;
  font-weight: 900;
}

.info-tooltip-body {
  color: var(--muted);
}

.metric-value {
  font-size: 16px;
  font-weight: 900;
  padding: 6px 8px;
}

.metric-best {
  color: #075f57;
  background: #e9faf6;
}

.metric-worse {
  color: #a11235;
  background: #fff0f3;
}

.metric-neutral {
  background: #f8fafc;
}

.composition-panel {
  margin-bottom: 12px;
}

.composition-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 18px;
  padding: 0 20px 20px;
}

.composition-card {
  border: 1px solid var(--line);
  background: #fff;
}

.composition-card h3 {
  padding: 14px;
  border-bottom: 1px solid var(--line);
  font-size: 16px;
}

.composition-source {
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.35;
}

.source-details {
  margin-top: 8px;
}

.source-details summary {
  width: fit-content;
  color: var(--ink);
  font-weight: 900;
  cursor: pointer;
}

.source-details summary:hover {
  color: var(--aia-red);
}

.source-links {
  display: grid;
  gap: 8px;
  margin-top: 10px;
  padding: 10px;
  border: 1px solid var(--line);
  background: #fff;
}

.source-links a {
  color: var(--aia-red);
  font-weight: 900;
  text-decoration: none;
  overflow-wrap: anywhere;
}

.source-links a:hover {
  text-decoration: underline;
}

.composition-section {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}

.composition-section:last-child {
  border-bottom: 0;
}

.composition-section-title {
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.exposure-row {
  display: grid;
  grid-template-columns: minmax(110px, 1fr) minmax(90px, 1fr) 58px;
  gap: 10px;
  align-items: center;
  min-height: 28px;
  font-size: 12px;
}

.exposure-label {
  font-weight: 800;
}

.exposure-bar {
  height: 8px;
  background: var(--soft);
}

.exposure-bar span {
  display: block;
  height: 100%;
  background: var(--aia-red);
}

.composition-line {
  display: grid;
  grid-template-columns: 1fr 80px;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}

.composition-line:last-child {
  border-bottom: 0;
}

.composition-weight {
  text-align: right;
  font-weight: 900;
}

.exposure-comparison-card {
  overflow: hidden;
}

.exposure-compare-section {
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

.exposure-compare-section:last-child {
  border-bottom: 0;
}

.exposure-compare-table {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
}

.exposure-compare-row {
  display: grid;
  grid-template-columns: minmax(180px, 1.2fr) minmax(150px, 0.85fr) minmax(180px, 1fr);
  gap: 12px;
  align-items: center;
  min-width: 540px;
  padding: 8px 0;
  border-bottom: 1px solid #edf0f3;
  font-size: 12px;
}

.exposure-compare-row:last-child {
  border-bottom: 0;
}

.exposure-compare-head {
  color: var(--muted);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.exposure-compare-head div {
  overflow-wrap: anywhere;
}

.exposure-compare-row strong {
  display: block;
  margin-top: 5px;
  font-size: 12px;
}

.exposure-benchmark-cell {
  min-width: 0;
}

.exposure-cell-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 5px;
}

.exposure-cell-footer strong {
  margin-top: 0;
}

.benchmark-bar span {
  background: var(--blue);
}

.exposure-diff {
  width: fit-content;
  min-width: 64px;
  padding: 4px 7px;
  background: var(--soft);
  color: var(--muted);
  font-weight: 900;
  text-align: center;
  font-size: 11px;
}

.exposure-over {
  background: #e9f8f6;
  color: #075f57;
}

.exposure-under {
  background: #fff6df;
  color: #8a4f00;
}

.empty-state {
  padding: 22px 14px;
  color: var(--muted);
  font-weight: 700;
}

.note {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 12px;
}

@media (max-width: 1080px) {
  .setup-grid,
  .stats-layout,
  .analysis-editor,
  .optimiser-layout,
  .composition-grid {
    grid-template-columns: 1fr;
  }

  .control-strip {
    align-items: flex-start;
  }

  .date-controls {
    margin-left: 0;
  }
}

@media (max-width: 720px) {
  .shell {
    padding: 0;
  }

  .page {
    max-width: none;
  }

  h1 {
    font-size: 24px;
  }

  h2 {
    font-size: 17px;
  }

  .app-header,
  .setup-footer,
  .control-strip,
  .chart-heading {
    display: block;
  }

  .app-header,
  .setup-footer,
  .panel,
  .chart-panel {
    border-left: 0;
    border-right: 0;
    box-shadow: none;
  }

  .app-header {
    padding: 18px 16px;
    margin-bottom: 0;
  }

  .panel-heading {
    align-items: flex-start;
    padding: 16px;
  }

  .primary-panel {
    min-height: 0;
  }

  .control-strip {
    padding: 10px 12px;
  }

  .app-header .secondary-button,
  .app-header .ghost-button,
  .confirm-block,
  .confirm-actions,
  .header-actions,
  .date-controls,
  .inline-controls {
    width: 100%;
    margin-top: 12px;
  }

  .holding-rows,
  .benchmark-rows {
    padding: 6px 0;
  }

  .holding-row {
    grid-template-columns: minmax(0, 1fr) 92px 40px;
    gap: 8px;
    padding: 10px 12px;
  }

  .benchmark-row {
    grid-template-columns: minmax(0, 1fr) 40px;
    gap: 8px;
    padding: 10px 12px;
  }

  label span {
    font-size: 11px;
  }

  input,
  select {
    min-height: 42px;
    font-size: 16px;
    padding: 9px 10px;
  }

  .remove-button {
    width: 40px;
    min-height: 42px;
    align-self: end;
  }

  .date-controls {
    grid-template-columns: 1fr;
  }

  .period-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }

  .period-buttons button:last-child {
    grid-column: 1 / -1;
  }

  .panel-actions {
    display: grid;
    grid-template-columns: 1fr;
    padding: 12px 16px 16px;
  }

  .panel-actions button,
  .confirm-actions button,
  .header-actions button {
    width: 100%;
  }

  .confirm-block {
    display: grid;
  }

  .confirm-actions,
  .header-actions {
    justify-content: stretch;
  }

  .confirm-actions button,
  .header-actions button {
    flex: 1;
  }

  .chart-wrap {
    height: 320px;
    padding: 0 10px 10px;
  }

  .chart-meta {
    padding: 0 16px 12px;
  }

  .legend {
    justify-content: flex-start;
    margin-top: 12px;
  }

  .chart-tooltip {
    max-width: calc(100vw - 32px);
    min-width: 0;
  }

  .performance-table,
  .stats-table,
  .optimiser-results,
  .composition-grid {
    padding-left: 12px;
    padding-right: 12px;
  }

  .optimiser-form {
    grid-template-columns: 1fr;
    padding: 14px 16px 4px;
  }

  .optimiser-copy {
    padding: 14px 16px 16px;
  }

  .optimiser-stat-grid,
  .optimiser-alt-grid {
    grid-template-columns: repeat(5, minmax(132px, 1fr));
  }

  .optimiser-row {
    grid-template-columns: minmax(220px, 1fr) minmax(130px, 0.5fr);
    min-width: 430px;
  }

  .performance-cell {
    font-size: 12px;
  }

  .metric-value {
    font-size: 13px;
  }

  .source-details summary {
    width: 100%;
  }

  .top-button {
    right: 14px;
    bottom: 14px;
    min-width: 60px;
    min-height: 38px;
  }
}

@media (max-width: 420px) {
  .holding-row {
    grid-template-columns: minmax(0, 1fr) 82px 38px;
    gap: 7px;
  }

  .benchmark-row {
    grid-template-columns: minmax(0, 1fr) 38px;
  }

  .primary-button,
  .secondary-button,
  .ghost-button,
  .remove-button,
  .segmented button {
    padding: 0 10px;
  }

  .chart-wrap {
    height: 290px;
  }

  .performance-grid {
    min-width: 780px;
  }

  .stats-row,
  .stats-head {
    gap: 8px;
  }
}
