:root {
  color-scheme: light dark;
  --bg: hsl(42 20% 97%);
  --surface: hsl(0 0% 100%);
  --surface-soft: hsl(44 24% 94%);
  --ink: hsl(220 28% 12%);
  --muted: hsl(218 12% 43%);
  --line: hsl(220 15% 85%);
  --line-strong: hsl(218 15% 70%);
  --accent: hsl(166 76% 34%);
  --accent-strong: hsl(166 78% 25%);
  --amber: hsl(39 90% 48%);
  --red: hsl(356 72% 53%);
  --blue: hsl(214 75% 48%);
  --violet: hsl(260 55% 55%);
  --track: hsl(220 14% 89%);
  --chart-grid: hsl(220 14% 86%);
  --chart-text: hsl(218 12% 40%);
  --series-cnn: hsl(166 76% 34%);
  --series-vix: hsl(356 72% 53%);
  --series-k: hsl(214 75% 48%);
  --series-nasdaq-up: hsl(166 72% 36%);
  --series-nasdaq-down: hsl(356 70% 54%);
  --series-nasdaq-line: hsl(260 55% 55%);
  --shadow: hsl(220 20% 18% / 0.12);
  --radius: 8px;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: hsl(220 22% 8%);
    --surface: hsl(220 18% 12%);
    --surface-soft: hsl(220 16% 16%);
    --ink: hsl(45 18% 92%);
    --muted: hsl(220 10% 68%);
    --line: hsl(220 12% 25%);
    --line-strong: hsl(220 12% 38%);
    --accent: hsl(166 72% 48%);
    --accent-strong: hsl(166 72% 58%);
    --amber: hsl(40 86% 56%);
    --red: hsl(356 72% 62%);
    --blue: hsl(212 76% 64%);
    --violet: hsl(260 68% 68%);
    --track: hsl(220 12% 24%);
    --chart-grid: hsl(220 12% 24%);
    --chart-text: hsl(220 10% 70%);
    --series-cnn: hsl(166 72% 48%);
    --series-vix: hsl(356 72% 62%);
    --series-k: hsl(212 76% 64%);
    --series-nasdaq-up: hsl(166 70% 52%);
    --series-nasdaq-down: hsl(356 70% 64%);
    --series-nasdaq-line: hsl(260 68% 68%);
    --shadow: hsl(220 30% 4% / 0.42);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: linear-gradient(180deg, var(--bg), color-mix(in hsl, var(--bg), var(--surface-soft) 48%));
  color: var(--ink);
}

button,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

a {
  color: inherit;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 18px clamp(18px, 4vw, 48px);
  border-bottom: 1px solid var(--line);
  background: color-mix(in hsl, var(--bg), transparent 10%);
  backdrop-filter: blur(18px);
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--accent);
  font-size: 0.76rem;
  font-weight: 780;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.45rem, 3vw, 2.5rem);
  line-height: 1.05;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 0;
  font-size: clamp(1.45rem, 3vw, 2.6rem);
  line-height: 1.05;
  letter-spacing: 0;
}

h3 {
  margin-bottom: 0;
  font-size: 1.08rem;
  line-height: 1.2;
  letter-spacing: 0;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 10px;
}

.status-pill,
.interval-select,
.icon-button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  color: var(--muted);
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--amber);
}

.status-pill.is-ok .status-dot {
  background: var(--accent);
}

.status-pill.is-error .status-dot {
  background: var(--red);
}

.status-pill.is-loading .status-dot {
  animation: pulseDot 900ms ease-in-out infinite;
}

.interval-select {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  color: var(--muted);
}

.interval-select select {
  width: 82px;
  border: 0;
  background: transparent;
  color: var(--ink);
}

.icon-button {
  padding: 0 14px;
  transition:
    transform 160ms ease,
    border-color 160ms ease;
}

.icon-button:hover {
  transform: translateY(-1px);
  border-color: var(--accent);
}

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: clamp(18px, 4vw, 48px);
}

.ratio-workspace {
  display: grid;
  grid-template-columns: minmax(0, 0.78fr) minmax(460px, 1.15fr);
  align-items: stretch;
  gap: clamp(18px, 3vw, 32px);
  min-height: 520px;
}

.ratio-copy,
.signal-panel,
.summary-panel,
.history-section,
.chart-panel,
.table-section,
.factor-section {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 18px 44px var(--shadow);
}

.ratio-copy {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: clamp(24px, 5vw, 48px);
}

.ratio-copy h2 {
  margin-bottom: 16px;
  font-size: clamp(4.2rem, 11vw, 9rem);
  line-height: 0.9;
  color: var(--accent-strong);
}

.ratio-formula {
  margin-bottom: 18px;
  color: var(--ink);
  font-size: clamp(1.05rem, 2vw, 1.35rem);
}

.freshness {
  margin-bottom: 0;
  color: var(--muted);
  line-height: 1.6;
}

.signal-board {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.signal-panel {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 100%;
  padding: clamp(20px, 3vw, 30px);
}

.signal-panel.primary {
  border-color: color-mix(in hsl, var(--accent), var(--line) 46%);
}

.panel-head,
.section-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.panel-head span,
.section-head span {
  color: var(--muted);
}

.panel-head strong {
  color: var(--accent);
  text-align: right;
  white-space: nowrap;
}

.value-row {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 28px 0 22px;
}

.value-row strong {
  font-size: clamp(3.4rem, 7vw, 6.6rem);
  line-height: 0.9;
  letter-spacing: 0;
}

.value-row span {
  color: var(--muted);
  font-weight: 700;
}

.meter {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--track);
}

.meter span {
  display: block;
  width: var(--meter-value, 0%);
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--red), var(--amber), var(--accent));
  transition: width 360ms ease;
}

.meter.volatility span {
  background: linear-gradient(90deg, var(--accent), var(--amber), var(--red));
}

dl {
  display: grid;
  gap: 10px;
  margin: 28px 0 0;
}

dl div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

dt {
  color: var(--muted);
}

dd {
  margin: 0;
  text-align: right;
  font-weight: 760;
}

.panel-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  margin-top: 16px;
}

.summary-panel {
  padding: 24px;
}

.summary-panel > p:not(.eyebrow) {
  margin: 12px 0 0;
  color: var(--muted);
  line-height: 1.7;
}

.source-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.source-badges.compact {
  justify-content: flex-end;
  margin-top: 8px;
}

.source-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 30px;
  padding: 0 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 0.78rem;
  white-space: normal;
}

.source-badge strong {
  color: var(--ink);
  font-weight: 780;
}

.source-badge em {
  color: var(--accent);
  font-style: normal;
  font-weight: 780;
}

.source-badge.is-cache em {
  color: var(--blue);
}

.source-badge.is-live {
  border-color: color-mix(in hsl, var(--accent), var(--line) 48%);
}

.source-badge.is-degraded,
.source-badge.is-stale {
  border-color: color-mix(in hsl, var(--amber), var(--line) 48%);
}

.source-badge.is-degraded em,
.source-badge.is-stale em {
  color: var(--amber);
}

.factor-section {
  margin-top: 16px;
  padding: 24px;
}

.history-section,
.table-section {
  margin-top: 16px;
  padding: 24px;
}

.section-head {
  align-items: center;
  margin-bottom: 20px;
}

.section-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  color: var(--muted);
}

.chart-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.chart-panel {
  min-width: 0;
  padding: 18px;
  box-shadow: none;
}

.k-panel {
  margin-top: 16px;
}

.chart-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.chart-head span {
  color: var(--muted);
  font-weight: 780;
  text-align: right;
  white-space: nowrap;
}

.chart-summary {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}

.chart-summary span:first-child {
  color: var(--series-k);
}

.chart-frame {
  position: relative;
  width: 100%;
  height: 300px;
}

.chart-frame.large {
  height: 360px;
}

.empty-state {
  display: grid;
  place-items: center;
  min-height: 100%;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  color: var(--muted);
  text-align: center;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

table {
  width: 100%;
  min-width: 840px;
  border-collapse: collapse;
  background: var(--surface);
}

th,
td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  white-space: nowrap;
}

th {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 780;
  text-transform: uppercase;
}

th:first-child,
td:first-child,
th:nth-child(3),
td:nth-child(3) {
  text-align: left;
}

tbody tr:last-child td {
  border-bottom: 0;
}

tbody tr:hover {
  background: var(--surface-soft);
}

.factor-list {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.factor-item {
  min-height: 138px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.factor-title {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 0.92rem;
}

.factor-title strong {
  color: var(--ink);
  text-align: right;
}

.factor-score {
  margin: 18px 0 12px;
  font-size: 2.1rem;
  font-weight: 820;
  line-height: 1;
}

.factor-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.source-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 16px;
}

.source-strip a {
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
  text-decoration: none;
}

.source-strip a:hover {
  border-color: var(--accent);
  color: var(--ink);
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  width: min(420px, calc(100vw - 40px));
  padding: 14px 16px;
  border: 1px solid color-mix(in hsl, var(--red), var(--line) 36%);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 18px 42px var(--shadow);
}

@keyframes pulseDot {
  0%,
  100% {
    transform: scale(1);
    opacity: 0.65;
  }

  50% {
    transform: scale(1.35);
    opacity: 1;
  }
}

@media (max-width: 980px) {
  .topbar {
    position: static;
    align-items: flex-start;
    flex-direction: column;
  }

  .toolbar {
    justify-content: flex-start;
  }

  .ratio-workspace,
  .panel-grid {
    grid-template-columns: 1fr;
  }

  .signal-board,
  .factor-list,
  .chart-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 640px) {
  .app-shell {
    padding: 14px;
  }

  .topbar {
    padding: 16px 14px;
  }

  .toolbar,
  .status-pill,
  .interval-select,
  .icon-button {
    width: 100%;
  }

  .status-pill,
  .interval-select,
  .icon-button {
    justify-content: center;
  }

  .signal-board,
  .factor-list,
  .chart-grid {
    grid-template-columns: 1fr;
  }

  .section-head {
    flex-direction: column;
  }

  .section-meta,
  .source-badges.compact {
    align-items: flex-start;
    justify-content: flex-start;
  }

  .ratio-copy h2 {
    font-size: clamp(3.7rem, 20vw, 5.5rem);
  }

  .chart-frame,
  .chart-frame.large {
    height: 260px;
  }
}
