:root {
  color-scheme: dark;
  --bg: #0d1014;
  --panel: #151a20;
  --panel-2: #10151b;
  --line: #27303a;
  --text: #eef3f7;
  --muted: #8f9aa6;
  --rapira: #2fbf71;
  --moex: #4da3ff;
  --spread: #f0b84d;
  --danger: #ff6b6b;
  --ok: #2fbf71;
  --wait: #d2a64b;
  --shadow: 0 18px 46px rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, Segoe UI, Roboto, Arial, sans-serif;
}

button,
input {
  font: inherit;
}

a {
  color: #b9d8ff;
  text-decoration: none;
}

a:hover {
  color: #ffffff;
}

.terminal-shell {
  width: min(1680px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 56px;
  margin-bottom: 14px;
}

h1,
h2 {
  margin: 0;
  font-weight: 650;
  letter-spacing: 0;
}

h1 {
  font-size: 23px;
  line-height: 1.2;
}

h2 {
  font-size: 14px;
  line-height: 1.2;
}

.subline {
  margin-top: 5px;
  color: var(--muted);
  font-size: 13px;
}

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

.range-controls {
  display: inline-grid;
  grid-template-columns: repeat(4, 44px);
  gap: 2px;
  min-height: 36px;
  padding: 2px;
  border: 1px solid #35404c;
  border-radius: 7px;
  background: #111820;
}

.range-button {
  min-width: 0;
  min-height: 30px;
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
}

.range-button:hover {
  background: #232d37;
  color: var(--text);
}

.range-button.active {
  background: #2d3742;
  color: var(--text);
}

.analytics-toggle {
  border-color: rgba(120, 154, 255, 0.45);
}

.analytics-toggle.active {
  background: #263456;
  border-color: rgba(120, 154, 255, 0.8);
  color: #dbe6ff;
}

button {
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid #35404c;
  border-radius: 6px;
  background: #1e2630;
  color: var(--text);
  cursor: pointer;
}

button:hover {
  background: #27313c;
}

button:disabled {
  cursor: wait;
  opacity: 0.65;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid #35404c;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.status-ok {
  border-color: rgba(47, 191, 113, 0.5);
  color: var(--ok);
}

.status-error {
  border-color: rgba(255, 107, 107, 0.5);
  color: var(--danger);
}

.status-wait {
  border-color: rgba(210, 166, 75, 0.5);
  color: var(--wait);
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 10px;
}

.metric-card {
  min-width: 0;
  padding: 13px 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.metric-card.accent {
  border-color: rgba(240, 184, 77, 0.45);
}

.metric-label {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
  margin-bottom: 8px;
}

.metric-value {
  min-height: 34px;
  font-size: 30px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.metric-value.compact {
  font-size: 20px;
  padding-top: 7px;
}

.metric-meta {
  margin-top: 8px;
  min-height: 16px;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chart-layout {
  display: grid;
  grid-template-rows: minmax(360px, 58vh) minmax(190px, 24vh);
  gap: 10px;
}

.chart-panel {
  min-width: 0;
  min-height: 0;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.panel-header {
  height: 54px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 6px;
}

.legend-item {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.legend-item::before {
  content: "";
  display: inline-block;
  width: 18px;
  height: 2px;
  margin-right: 6px;
  vertical-align: middle;
  background: currentColor;
}

.rapira-line {
  color: var(--rapira);
}

.moex-line {
  color: var(--moex);
}

.spread-line {
  color: var(--spread);
}

.analytics-ma {
  color: #c8d8e8;
}

.analytics-band {
  color: #789aff;
}

.analytics-level {
  color: #ff7a90;
}

.crosshair-readout {
  color: var(--muted);
  font-size: 12px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  min-width: 180px;
}

.chart {
  width: 100%;
  height: calc(100% - 54px);
}

.source-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-top: 10px;
}

.source-grid a {
  min-height: 38px;
  display: flex;
  align-items: center;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: #c8d8e8;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.error-box {
  margin-top: 10px;
  border: 1px solid rgba(255, 107, 107, 0.45);
  border-radius: 8px;
  background: rgba(255, 107, 107, 0.08);
  color: #ffb7b7;
  padding: 10px 12px;
  font-size: 13px;
}

.tv-fallback {
  height: 100%;
  display: grid;
  place-items: center;
  padding: 20px;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 980px) {
  .terminal-shell {
    padding: 12px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar-actions {
    justify-content: flex-start;
  }

  .metrics-grid,
  .source-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .chart-layout {
    grid-template-rows: 430px 230px;
  }
}

@media (max-width: 620px) {
  h1 {
    font-size: 20px;
  }

  .metrics-grid,
  .source-grid {
    grid-template-columns: 1fr;
  }

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

  .panel-header {
    height: auto;
    min-height: 70px;
    align-items: flex-start;
    flex-direction: column;
  }

  .crosshair-readout {
    text-align: left;
    min-width: 0;
  }

  .chart {
    height: calc(100% - 70px);
  }
}
