/*
 * compare.css - Phase 15 styles (selection, bar, compare page, table, chart, calculator).
 * Reads style.css root tokens read-only. Custom properties are page-scoped only.
 * Weights: 400/600 only. Sizes: 14/16/20/28px only. No animations or transitions.
 */

/* ---------- page-scoped custom properties ---------- */
.compare-page,
#compare-bar {
  --cmp-bar-h: 64px;
  --cmp-s1: #0072B2;
  --cmp-s2: #D55E00;
  --cmp-s3: #009E73;
  --cmp-s4: #CC79A7;
}

/* ---------- selection ---------- */
.cmp-name-wrap {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.cmp-pick {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  cursor: pointer;
  flex-shrink: 0;
}

.cmp-check {
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--primary);
}

.cmp-pick.is-dim {
  opacity: 0.5;
}

.cmp-check:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* .cmp-pick-box is decorative; the native checkbox is used, so the box is hidden. */
.cmp-pick-box {
  display: none;
}

.cmp-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 767px) {
  .data-table td.cmp-name-cell .cmp-name-wrap {
    align-items: flex-start;
    text-align: left;
    flex-wrap: wrap;
  }
}

/* ---------- compare bar ----------
 * z-index 1050 sits below the mobile nav (1090), header (1200) and modals (1500+).
 */
#compare-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1050;
  background: var(--surface);
  border-top: 1px solid var(--border-strong);
  box-shadow: var(--shadow-md);
  padding: 8px 16px calc(8px + env(safe-area-inset-bottom, 0px));
  font-size: 16px;
  font-weight: 400;
  line-height: 1.5;
  color: var(--text);
}

.cmp-bar[hidden] {
  display: none;
}

.cmp-bar-inner {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--cmp-bar-h);
  max-width: 1200px;
  margin: 0 auto;
}

.cmp-bar-status {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-wrap: anywhere;
}

.cmp-bar-count {
  font-size: 16px;
  font-weight: 600;
}

.cmp-bar-helper {
  font-size: 14px;
  font-weight: 400;
  color: var(--text-muted);
}

.cmp-bar-notice {
  font-size: 14px;
  font-weight: 400;
  color: var(--text);
  background: var(--surface-muted);
  border-radius: var(--radius-sm);
  padding: 4px 8px;
}

.cmp-bar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.cmp-bar-clear {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  padding: 8px 16px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.cmp-bar-go {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 96px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  background: var(--primary);
  border-radius: var(--radius-sm);
  text-decoration: none;
  cursor: pointer;
}

.cmp-bar-go[aria-disabled="true"] {
  background: var(--surface-muted);
  color: var(--text-soft);
  cursor: not-allowed;
}

.cmp-bar-clear:focus-visible,
.cmp-bar-go:focus-visible,
.cmp-btn:focus-visible,
.cmp-chip:focus-visible,
.cmp-input:focus-visible,
.cmp-select:focus-visible,
.cmp-chart-alt summary:focus-visible,
.cmp-table-wrap:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* var defined on #compare-bar, not body, hence the fallback */
body.has-compare-bar {
  padding-bottom: calc(var(--cmp-bar-h, 64px) + env(safe-area-inset-bottom, 0px) + 16px);
}

/* Phase 16: lift the fixed .toast (style.css, bottom 24px) above the bar */
body.has-compare-bar .toast {
  bottom: calc(var(--cmp-bar-h, 64px) + env(safe-area-inset-bottom, 0px) + 24px);
}

/* ---------- compare page ---------- */
.compare-page {
  padding-top: 48px;
  font-size: 16px;
  font-weight: 400;
  line-height: 1.5;
}

.compare-page > * + * {
  margin-top: 24px;
}

.compare-page h1 {
  font-size: 28px;
  font-weight: 600;
  line-height: 1.2;
  margin: 0;
}

.cmp-intro {
  font-size: 16px;
  font-weight: 400;
  color: var(--text-muted);
}

.cmp-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.cmp-toolbar[hidden] {
  display: none;
}

.cmp-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 16px;
}

.cmp-section-title {
  font-size: 20px;
  font-weight: 600;
  line-height: 1.2;
  margin: 0 0 16px;
}

.cmp-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  padding: 8px 16px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  text-decoration: none;
  cursor: pointer;
}

.cmp-notice {
  font-size: 14px;
  font-weight: 400;
  color: var(--text);
  background: var(--surface-muted);
  border-radius: var(--radius-sm);
  padding: 8px 16px;
}

.cmp-state {
  font-size: 16px;
  font-weight: 400;
}

.cmp-empty {
  padding: 48px 16px;
  text-align: center;
}

.cmp-error {
  padding: 24px 16px;
  text-align: center;
}

.cmp-error .cmp-error-msg,
.cmp-error > p:first-child {
  color: var(--danger);
}

.cmp-skeleton {
  height: 16px;
  margin: 8px 0;
  background: var(--surface-muted);
  border-radius: var(--radius-sm);
}

/* ---------- metric table ---------- */
.cmp-table-wrap {
  position: relative;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}

.cmp-table-wrap.is-scrollable::after {
  content: "";
  position: sticky;
  display: block;
  float: right;
  right: 0;
  top: 0;
  width: 24px;
  height: 100%;
  margin-left: -24px;
  background: linear-gradient(to right, transparent, var(--surface));
  pointer-events: none;
}

.cmp-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 16px;
  font-weight: 400;
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
}

.cmp-table caption {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.cmp-table th,
.cmp-table td,
.cmp-chart-table th,
.cmp-chart-table td {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.cmp-table th[scope="row"] {
  position: sticky;
  left: 0;
  z-index: 1;
  width: 88px;
  min-width: 88px;
  background: var(--surface-soft);
  border-right: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
}

.cmp-table th[scope="col"] {
  min-width: 116px;
  font-size: 14px;
  font-weight: 600;
  background: var(--surface-soft);
  overflow-wrap: anywhere;
}

.cmp-etf-code {
  display: block;
  font-size: 14px;
  font-weight: 400;
  color: var(--text-muted);
}

.cmp-value {
  font-weight: 400;
}

td[data-best="true"] .cmp-value {
  font-weight: 600;
}

.cmp-best {
  display: inline-block;
  margin-left: 4px;
  padding: 0 4px;
  font-size: 14px;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-soft);
  border-radius: var(--radius-sm);
}

.cmp-scroll-hint {
  font-size: 14px;
  font-weight: 400;
  color: var(--text-muted);
}

@media (max-width: 639px) {
  .compare-page {
    padding-left: 16px;
    padding-right: 16px;
  }

  .cmp-table-wrap {
    margin-left: -16px;
    margin-right: -16px;
    max-width: none;
  }
}

/* ---------- overlay chart ---------- */
.cmp-chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.cmp-chip {
  min-height: 44px;
  min-width: 44px;
  padding: 8px 16px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 400;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.cmp-chip[aria-pressed="true"] {
  background: var(--primary-soft);
  border: 2px solid var(--primary);
  font-weight: 600;
}

.cmp-chart-figure {
  margin: 16px 0 0;
}

.cmp-chart-svg {
  display: block;
  width: 100%;
  height: auto;
}

.cmp-line {
  fill: none;
  stroke-width: 2.5;
  stroke-linejoin: round;
}

.cmp-line-1 {
  stroke: var(--cmp-s1);
}

.cmp-line-2 {
  stroke: var(--cmp-s2);
  stroke-dasharray: 8 4;
}

.cmp-line-3 {
  stroke: var(--cmp-s3);
  stroke-dasharray: 2 4;
  stroke-linecap: round;
}

.cmp-line-4 {
  stroke: var(--cmp-s4);
  stroke-dasharray: 10 3 2 3;
}

.cmp-marker {
  stroke: #fff;
  stroke-width: 2;
}

.cmp-marker-1 {
  fill: var(--cmp-s1);
}

.cmp-marker-2 {
  fill: var(--cmp-s2);
}

.cmp-marker-3 {
  fill: var(--cmp-s3);
}

.cmp-marker-4 {
  fill: var(--cmp-s4);
}

.cmp-legend {
  list-style: none;
  margin: 16px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 14px;
  font-weight: 400;
}

.cmp-legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.cmp-swatch {
  flex-shrink: 0;
  width: 32px;
  height: 8px;
}

.cmp-legend-note {
  font-size: 14px;
  font-weight: 400;
  color: var(--text-muted);
}

.cmp-chart-alt {
  margin-top: 16px;
  max-width: 100%;
  overflow-x: auto;
}

.cmp-chart-alt summary {
  display: flex;
  align-items: center;
  min-height: 44px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.cmp-chart-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

/* ---------- calculator ---------- */
.cmp-calc-form {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

.cmp-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.cmp-label {
  font-size: 14px;
  font-weight: 600;
}

.cmp-input,
.cmp-select {
  width: 100%;
  min-height: 44px;
  padding: 8px;
  font-family: inherit;
  font-size: 16px;
  font-weight: 400;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
}

.cmp-input[aria-invalid="true"] {
  border-color: var(--danger);
}

.cmp-hint {
  font-size: 14px;
  font-weight: 400;
  color: var(--text-muted);
}

.cmp-field-error {
  font-size: 14px;
  font-weight: 400;
  color: var(--danger);
}

.cmp-calc-result {
  margin-top: 24px;
}

.cmp-calc-headline {
  font-size: 20px;
  font-weight: 600;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
  margin: 0 0 16px;
}

.cmp-calc-table th[scope="row"] {
  position: static;
  width: auto;
}

.cmp-calc-table th[scope="col"] {
  min-width: 0;
}

.cmp-calc-table td,
.cmp-calc-table th[scope="col"]:first-child {
  white-space: nowrap;
}

@media (max-width: 639px) {
  .cmp-calc-table th,
  .cmp-calc-table td {
    padding: 8px;
  }
}

.cmp-calc-meta {
  font-size: 14px;
  font-weight: 400;
  color: var(--text-muted);
  margin-top: 8px;
}

.cmp-warning {
  font-size: 14px;
  font-weight: 400;
  color: var(--text);
  background: var(--surface-muted);
  border-radius: var(--radius-sm);
  padding: 8px 16px;
  margin-top: 8px;
}

.cmp-disclaimer {
  font-size: 14px;
  font-weight: 400;
  color: var(--text-muted);
  background: var(--surface-muted);
  border-left: 4px solid var(--border-strong);
  padding: 8px 16px;
  margin-top: 16px;
}

/* ---------- >= 640px ---------- */
@media (min-width: 640px) {
  .compare-page > * + * {
    margin-top: 32px;
  }

  .cmp-card {
    padding: 24px;
  }

  .cmp-scroll-hint {
    display: none;
  }

  .cmp-calc-form {
    grid-template-columns: 1fr 1fr;
  }
}
