/* ---------- tokens (light first, dark under both the OS setting and the explicit toggle) */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #2a78d6;
  --accent-2: #eb6834;
  --neg: #e34948;
  --up-ink: #1c5cab;
  --down-ink: #b83232;
  --up-bg: rgba(42, 120, 214, 0.12);
  --down-bg: rgba(227, 73, 72, 0.12);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --accent-2: #d95926;
    --neg: #e66767;
    --up-ink: #86b6ef;
    --down-ink: #f09a9a;
    --up-bg: rgba(57, 135, 229, 0.2);
    --down-bg: rgba(230, 103, 103, 0.2);
    --shadow: none;
  }
}
:root[data-theme='dark'] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232322;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --accent: #3987e5;
  --accent-2: #d95926;
  --neg: #e66767;
  --up-ink: #86b6ef;
  --down-ink: #f09a9a;
  --up-bg: rgba(57, 135, 229, 0.2);
  --down-bg: rgba(230, 103, 103, 0.2);
  --shadow: none;
}

/* ---------- base */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, 'Segoe UI', sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
h2 { margin: 0; font-size: 16px; font-weight: 600; }
p { margin: 0; }
a { color: inherit; }
[hidden] { display: none !important; }

.topbar {
  display: flex; align-items: center; gap: 24px; flex-wrap: wrap;
  padding: 10px 20px; border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 650; letter-spacing: 0.01em; }
.brand-sep { color: var(--muted); font-weight: 400; }
.tabs { display: flex; gap: 4px; }
.tabs a {
  text-decoration: none; padding: 6px 12px; border-radius: 6px; color: var(--text-2);
}
.tabs a[aria-selected='true'] { background: var(--surface-2); color: var(--text); font-weight: 600; }
.theme-pick { margin-left: auto; display: flex; gap: 8px; align-items: center; color: var(--text-2); font-size: 13px; }

.view { max-width: 1240px; margin: 0 auto; padding: 18px 20px 32px; }
.foot { max-width: 1240px; margin: 0 auto; padding: 8px 20px 32px; color: var(--muted); font-size: 12px; }

/* ---------- controls */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; margin-bottom: 12px; }
.mode-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); overflow: hidden; }
.seg > button {
  border: 0; background: transparent; padding: 7px 14px; cursor: pointer; color: var(--text-2);
  border-right: 1px solid var(--border);
}
.seg > button:last-child { border-right: 0; }
.seg > button[aria-selected='true'], .seg > button[aria-checked='true'] { background: var(--accent); color: #fff; font-weight: 600; }
.seg-small > button { padding: 5px 12px; }
.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 6px 12px; cursor: pointer;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn-quiet { border-color: transparent; background: transparent; color: var(--text-2); }
.btn-quiet:hover:not(:disabled) { background: var(--surface-2); }
.btn[aria-pressed='true'] { background: var(--surface-2); border-color: var(--axis); color: var(--text); }
input[type='date'], input[type='number'], select {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 6px 10px; min-width: 0;
}
input:focus-visible, select:focus-visible, button:focus-visible, .chart-plot:focus-visible, .spread-plot:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.check { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); cursor: pointer; }
.hint { color: var(--muted); font-size: 13px; }
.sep { width: 1px; height: 20px; background: var(--border); }
.notice { padding: 8px 12px; border-radius: 8px; margin-bottom: 12px; background: var(--surface-2); color: var(--text-2); }
.notice[data-kind='error'] { background: var(--down-bg); color: var(--down-ink); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 12px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 4px 3px 8px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); font-size: 13px; font-variant-numeric: tabular-nums;
}
.chip-swatch { width: 10px; height: 10px; border-radius: 50%; }
.chip-meta { color: var(--muted); }
.chip-remove { border: 0; background: transparent; cursor: pointer; color: var(--muted); border-radius: 50%; width: 22px; height: 22px; line-height: 1; }
.chip-remove:hover { background: var(--surface-2); color: var(--text); }

/* ---------- cards & tiles */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; box-shadow: var(--shadow);
  transition: opacity 0.2s;
}
.card-flat { padding: 0; overflow: hidden; }
.card.is-loading #ts-chart, .card.is-loading #ts-spread, .card.is-loading .chart, .card.is-loading .table-view, .card.is-loading .stats { opacity: 0.45; }
.stack { display: grid; gap: 16px; }
.toolbar .push { margin-left: auto; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.card-sub { color: var(--text-2); font-size: 13px; margin-top: 2px; }
.card-tools { display: flex; align-items: center; gap: 10px; }
.source { color: var(--muted); font-size: 12px; margin-top: 10px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 12px; }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; box-shadow: var(--shadow);
}
.tile-value { font-size: 24px; font-weight: 600; line-height: 1.15; letter-spacing: -0.01em; }
.tile-label { color: var(--text-2); font-size: 13px; margin-top: 2px; }
.tile-sub { color: var(--muted); font-size: 12px; margin-top: 4px; }
.tile[data-kind='up'] .tile-value { color: var(--up-ink); }
.tile[data-kind='down'] .tile-value { color: var(--down-ink); }
.tile-hero .tile-value { font-size: 40px; }
.tile-small .tile-value { font-size: 20px; }

.badge {
  display: inline-flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: 999px; font-size: 13px;
  background: var(--surface-2); color: var(--text-2);
}
.badge[data-kind='contango'] { background: var(--up-bg); color: var(--up-ink); }
.badge[data-kind='backwardation'] { background: var(--down-bg); color: var(--down-ink); }
.badge-icon { font-size: 16px; }
.badge-word { font-weight: 650; }
.badge-detail { opacity: 0.85; font-variant-numeric: tabular-nums; }

/* ---------- chart */
.chart { position: relative; }
.chart-plot { position: relative; width: 100%; }
.chart-svg { display: block; width: 100%; height: auto; }
.chart-grid { stroke: var(--grid); stroke-width: 1; }
.chart-axis { stroke: var(--axis); stroke-width: 1; }
.chart-zero { stroke: var(--axis); stroke-width: 1.5; }
.chart-tick { fill: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.chart-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; transition: opacity 0.15s; }
.chart-line.is-dim, .chart-marker.is-dim { opacity: 0.18; }
.chart-line.is-focus { stroke-width: 3; }
.chart-marker { stroke: var(--surface); stroke-width: 2; transition: opacity 0.15s; }
.chart-baseline { stroke: var(--text-2); stroke-width: 1; stroke-dasharray: 4 4; opacity: 0.7; }
.chart-baseline-label { fill: var(--text-2); font-size: 12px; }
.chart-vline { stroke: var(--axis); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-vline-label { fill: var(--text-2); font-size: 11px; }
.chart-direct-label { fill: var(--text-2); font-size: 12px; }
.chart-crosshair { stroke: var(--text-2); stroke-width: 1; opacity: 0.5; }
.chart-hover-dot { stroke: var(--surface); stroke-width: 2; }
.chart-hit { cursor: crosshair; }
.chart-tip {
  position: absolute; pointer-events: none; z-index: 3; min-width: 140px; max-width: 320px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12); font-size: 13px;
}
.tip-head { color: var(--text-2); font-size: 12px; margin-bottom: 4px; }
.tip-big { font-size: 20px; font-weight: 600; }
.tip-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.tip-row { display: flex; align-items: center; gap: 8px; padding: 2px 0; }
.tip-key { display: inline-block; width: 14px; height: 2px; border-radius: 1px; flex: none; }
.tip-val { font-variant-numeric: tabular-nums; min-width: 48px; }
.tip-name { color: var(--text-2); font-size: 12px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 6px; margin-top: 10px; }
.legend-item {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid transparent; background: transparent;
  border-radius: 6px; padding: 3px 8px; cursor: pointer; color: var(--text-2); font-size: 13px; font-variant-numeric: tabular-nums;
}
.legend-item:hover { background: var(--surface-2); }
.legend-item[aria-pressed='false'] { opacity: 0.45; text-decoration: line-through; }
.legend-key { display: inline-block; width: 16px; height: 2px; border-radius: 1px; }
.legend-action { color: var(--accent); }

/* ---------- month-to-month spread panel (shares the main chart's x geometry) */
.spread { margin-top: 8px; }
.spread-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 10px; margin-bottom: 2px; }
.spread-title { font-size: 13px; font-weight: 600; color: var(--text-2); }
.spread-sub { font-size: 12px; color: var(--muted); }
.spread-plot { position: relative; width: 100%; }
.spread-bar { transition: opacity 0.15s; }
.spread-bar.is-hover { opacity: 0.7; }
.spread-value { fill: var(--text-2); font-size: 11px; font-weight: 600; font-variant-numeric: tabular-nums; }
.spread-xlab { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.spread-hit { cursor: crosshair; }

/* ---------- tables */
.table-view { margin-top: 12px; }
.table-scroll { overflow-x: auto; }
.data-table { border-collapse: collapse; width: 100%; font-size: 13px; }
.data-table th, .data-table td { padding: 7px 10px; border-bottom: 1px solid var(--grid); text-align: left; white-space: nowrap; }
.data-table thead th { color: var(--text-2); font-weight: 600; }
.data-table tbody th { font-weight: 500; }
.data-table td.num, .data-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table .unit { color: var(--muted); font-weight: 400; }
.greeks td.num { text-align: right; }

/* ---------- calculator */
.calc-grid { display: grid; grid-template-columns: minmax(280px, 380px) 1fr; gap: 16px; margin-bottom: 16px; }
.calc-form { display: flex; flex-direction: column; gap: 14px; align-self: start; }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { color: var(--text-2); font-size: 12px; }
.field input, .field select { width: 100%; }
.field-row { display: flex; gap: 6px; }
.field-row input { flex: 1 1 60px; min-width: 0; }
.field-row select { flex: none; width: auto; }
.calc-results { display: flex; flex-direction: column; gap: 12px; transition: opacity 0.2s; }
.calc-results.is-dim { opacity: 0.4; }
.price-pair, .dpair { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.kv { margin: 8px 0 0; display: grid; gap: 4px; }
.kv div { display: flex; justify-content: space-between; gap: 8px; }
.kv dt { color: var(--text-2); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; }
.kv dd[data-m='ITM'] { color: var(--up-ink); font-weight: 600; }
.kv dd[data-m='OTM'] { color: var(--muted); font-weight: 600; }
.kv dd[data-m='ATM'] { font-weight: 600; }

@media (max-width: 860px) {
  .calc-grid { grid-template-columns: 1fr; }
  .theme-pick { margin-left: 0; }
  .view { padding: 14px 12px 24px; }
}
@media (max-width: 520px) {
  .field-grid, .price-pair, .dpair { grid-template-columns: 1fr; }
}

/* ---------- header tools (language + theme) */
.topbar-tools { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.topbar-tools .theme-pick { margin-left: 0; }
.lang-toggle > button[aria-pressed='true'] { background: var(--accent); color: #fff; font-weight: 600; }
.lang-toggle > button { min-width: 42px; }
@media (max-width: 860px) {
  .topbar-tools { margin-left: 0; }
}
