:root {
    --bg:        #0a0c10;
    --surface:   #111318;
    --border:    #1e2228;
    --muted:     #3a3f4a;
    --text:      #e8eaf0;
    --text-dim:  #6b7280;
    --accent:    #00d4aa;

    /* Sector palette — 10 distinct colours */
    --c-financials:    #3b82f6;
    --c-energy:        #f59e0b;
    --c-health:        #10b981;
    --c-industrials:   #8b5cf6;
    --c-consumer-disc: #ec4899;
    --c-consumer-sta:  #f97316;
    --c-materials:     #06b6d4;
    --c-technology:    #a78bfa;
    --c-utilities:     #84cc16;
    --c-telecom:       #fb7185;
    --c-real-estate:   #fbbf24;
  }

  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

  body {
    background: var(--bg);
    color: var(--text);
    font-family: 'DM Mono', monospace;
    min-height: 100vh;
    overflow-x: hidden;
  }

  /* ── Header ── */
  header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 28px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    position: sticky;
    top: 0;
    z-index: 100;
  }

  .logo {
    display: flex;
    align-items: center;
    gap: 12px;
    font-family: 'Syne', sans-serif;
    font-weight: 800;
    font-size: 1.25rem;
    letter-spacing: -0.02em;
    color: var(--text);
  }
  .logo span { color: var(--accent); }
  #page-title { color: var(--text); margin: 0; padding: 0; font-size: inherit; font-weight: inherit; line-height: normal; }
  #page-title span { color: var(--accent); }
  .logo-link img {
    display: block;
    height: 32px;
    width: auto;
  }

  .meta {
    font-size: 0.7rem;
    color: var(--text-dim);
    line-height: 1.6;
    text-align: right;
  }
  #last-updated { color: var(--accent); }

  /* ── Controls ── */
  .controls {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 28px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    align-items: center;
  }

  .ctrl-group {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  label {
    font-size: 0.65rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.06em;
  }

  select {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--muted);
    border-radius: 4px;
    padding: 5px 10px;
    font-family: 'DM Mono', monospace;
    font-size: 0.72rem;
    cursor: pointer;
    outline: none;
    transition: border-color 0.15s;
  }
  select:hover { border-color: var(--accent); }

  .btn {
    background: transparent;
    color: var(--accent);
    border: 1px solid var(--accent);
    border-radius: 4px;
    padding: 5px 14px;
    font-family: 'DM Mono', monospace;
    font-size: 0.72rem;
    cursor: pointer;
    letter-spacing: 0.04em;
    transition: background 0.15s, color 0.15s;
  }
  .btn:hover { background: var(--accent); color: var(--bg); }
  .btn:disabled { opacity: 0.4; cursor: default; }

  #loading-bar {
    height: 2px;
    background: var(--accent);
    width: 0%;
    transition: width 0.4s ease;
    position: fixed;
    top: 0; left: 0;
    z-index: 999;
  }

  /* ── Legend ── */
  .legend {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 16px;
    padding: 8px 28px;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
  }

  .legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.65rem;
    color: var(--text-dim);
    cursor: pointer;
    transition: color 0.15s;
    user-select: none;
  }
  .legend-item:hover, .legend-item.active { color: var(--text); }
  .legend-item.dimmed { opacity: 0.35; }

  .legend-dot {
    width: 9px; height: 9px;
    border-radius: 50%;
    flex-shrink: 0;
  }

  /* ── Chart area ── */
  #chart-container {
    position: relative;
    width: 100%;
    overflow-x: hidden;
  }

  #chart-svg {
    display: block;
    width: 100%;
  }

  /* ── Tooltip ── */
  #tooltip {
    position: fixed;
    pointer-events: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    border-radius: 8px;
    padding: 13px 16px 13px 14px;
    font-size: 0.72rem;
    line-height: 1.6;
    width: 228px;
    opacity: 0;
    transition: opacity 0.15s;
    z-index: 200;
    box-shadow: 0 12px 40px rgba(0,0,0,0.7);
  }
  #tooltip.visible { opacity: 1; }

  .tt-name   { font-family: 'Syne', sans-serif; font-size: 0.88rem; font-weight: 700; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .tt-sector { font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.09em; margin-bottom: 10px; }
  .tt-divider{ border: none; border-top: 1px solid var(--border); margin: 8px 0; }
  .tt-row    { display: flex; justify-content: space-between; align-items: center; gap: 12px; min-height: 20px; }
  .tt-label  { color: var(--text-dim); }
  .tt-val    { font-weight: 500; text-align: right; }
  .tt-change-pill {
    display: inline-block;
    padding: 1px 7px;
    border-radius: 20px;
    font-weight: 600;
    font-size: 0.72rem;
  }
  .pos      { color: #34d399; }
  .neg      { color: #f87171; }
  .pos-pill { background: rgba(52,211,153,0.15); color: #34d399; }
  .neg-pill { background: rgba(248,113,113,0.15); color: #f87171; }
  .tt-analyst-pill {
    display: inline-block;
    padding: 1px 7px;
    border-radius: 20px;
    font-weight: 600;
    font-size: 0.72rem;
  }
  /* Legacy classes kept for any external references; badge now uses inline Engine colors */
  .rating-strong-buy  { background: rgba(0,212,170,0.18);  color: #00d4aa; }
  .rating-buy         { background: rgba(74,222,128,0.18); color: #4ade80; }
  .rating-watch       { background: rgba(250,204,21,0.18); color: #facc15; }
  .rating-neutral     { background: rgba(251,146,60,0.18); color: #fb923c; }
  .rating-caution     { background: rgba(248,113,113,0.18);color: #f87171; }
  .rating-avoid       { background: rgba(220,38,38,0.18);  color: #dc2626; }
  .rating-hold        { background: rgba(251,146,60,0.18); color: #fb923c; }
  .rating-sell        { background: rgba(248,113,113,0.18);color: #f87171; }
  .tt-dimval          { color: var(--text-dim); font-weight: 400; font-size: 0.72rem; }

  /* ── Axes labels ── */
  .axis-label {
    fill: var(--text-dim);
    font-family: 'DM Mono', monospace;
    font-size: 10px;
  }
  .axis-grid {
    stroke: var(--border);
    stroke-dasharray: 3 4;
  }

  /* ── Status bar ── */
  #status {
    text-align: center;
    padding: 60px;
    font-size: 0.8rem;
    color: var(--text-dim);
  }

  /* ── Error ── */
  .error-box {
    border: 1px solid #f87171;
    border-radius: 8px;
    padding: 20px;
    margin: 20px 28px;
    font-size: 0.78rem;
    color: #f87171;
    line-height: 1.7;
  }

  /* ── Tooltip sparkline ── */
  .tt-spark-hdr  { font-size: 0.58rem; color: var(--text-dim); letter-spacing: 0.08em; margin-bottom: 4px; }
  #tt-spark      { min-height: 56px; display: flex; align-items: center; justify-content: center; }
  .tt-spark-wait { font-size: 0.62rem; color: var(--text-dim); }

  /* ── Search box ── */
  .search-wrap { position: relative; }
  #stock-search {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--muted);
    border-radius: 4px;
    padding: 5px 28px 5px 10px;
    font-family: 'DM Mono', monospace;
    font-size: 0.72rem;
    width: 160px;
    outline: none;
    transition: border-color 0.15s, width 0.2s;
  }
  #stock-search:focus { border-color: var(--accent); width: 220px; }
  #stock-search::placeholder { color: var(--text-dim); }
  .search-clear {
    position: absolute; right: 7px; top: 50%; transform: translateY(-50%);
    background: none; border: none; color: var(--text-dim);
    cursor: pointer; font-size: 0.75rem; line-height: 1; padding: 0; display: none;
  }
  .search-clear.visible { display: block; }
  .search-dropdown {
    position: absolute; top: calc(100% + 4px); left: 0;
    background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
    max-height: 280px; overflow-y: auto; z-index: 300; display: none;
    box-shadow: 0 8px 32px rgba(0,0,0,0.65); min-width: 280px;
  }
  .search-dropdown.open { display: block; }
  .search-item {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 12px; cursor: pointer;
    border-bottom: 1px solid var(--border); transition: background 0.1s;
  }
  .search-item:last-child { border-bottom: none; }
  .search-item:hover, .search-item.kbd-active { background: rgba(0,212,170,0.07); }
  .search-ticker { font-size: 0.72rem; color: var(--accent); width: 68px; flex-shrink: 0; }
  .search-name   { font-size: 0.72rem; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .search-dot    { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }

  /* ── Stock detail modal ── */
  .stock-modal {
    position: fixed; inset: 0; background: var(--bg);
    z-index: 500; display: none; flex-direction: column; overflow: hidden;
  }
  .stock-modal.open { display: flex; }
  .modal-sticky {
    position: sticky; top: 0; background: var(--surface);
    border-bottom: 1px solid var(--border); padding: 14px 22px 12px;
    display: flex; align-items: flex-start; justify-content: space-between;
    gap: 16px; z-index: 10; flex-shrink: 0;
  }
  .modal-title-block { flex: 1; min-width: 0; }
  .modal-stock-name {
    font-family: 'Syne', sans-serif; font-size: 1.35rem; font-weight: 800;
    letter-spacing: -0.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .modal-ticker-row {
    font-size: 0.62rem; color: var(--text-dim); margin-top: 2px;
    text-transform: uppercase; letter-spacing: 0.07em;
  }
  .modal-price-block { text-align: right; flex-shrink: 0; }
  .modal-big-price {
    font-family: 'Syne', sans-serif; font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em;
  }
  .modal-close {
    background: transparent; border: 1px solid var(--muted); color: var(--text-dim);
    border-radius: 4px; width: 32px; height: 32px; font-size: 1rem; cursor: pointer;
    display: flex; align-items: center; justify-content: center; flex-shrink: 0;
    transition: border-color 0.15s, color 0.15s;
  }
  .modal-close:hover { border-color: var(--text); color: var(--text); }
  .modal-body { flex: 1; overflow-y: auto; padding: 22px 24px 40px; }
  .modal-panel {
    flex-shrink: 0; padding: 18px 24px 4px;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
  }
  .modal-metrics {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 10px; margin-bottom: 26px;
  }
  .modal-metric {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: 6px; padding: 10px 13px;
  }
  .modal-metric-label { font-size: 0.57rem; text-transform: uppercase; letter-spacing: 0.09em; color: var(--text-dim); margin-bottom: 5px; }
  .modal-metric-value { font-size: 0.88rem; font-weight: 500; }
  .modal-alpha-link { text-decoration: none; transition: opacity .15s; }
  .modal-alpha-link:hover { opacity: .75; }
  #modal-alpha-loading { color: var(--text-dim); font-size: 0.8rem; }
  .modal-sec-hdr {
    font-size: 0.58rem; text-transform: uppercase; letter-spacing: 0.1em;
    color: var(--text-dim); border-bottom: 1px solid var(--border);
    padding-bottom: 6px; margin-bottom: 14px;
  }
  #modal-spark    { margin-bottom: 26px; min-height: 76px; }
  .modal-desc-text {
    font-size: 0.78rem; line-height: 1.85; color: var(--text);
    max-width: 900px; margin-bottom: 10px;
  }
  .modal-desc-meta {
    display: flex; flex-wrap: wrap; gap: 8px 22px;
    font-size: 0.64rem; color: var(--text-dim); margin-bottom: 26px;
  }
  .modal-desc-meta a { color: var(--accent); text-decoration: none; }
  .modal-desc-meta a:hover { text-decoration: underline; }
  .news-list { display: flex; flex-direction: column; }
  .news-item {
    display: flex; gap: 13px; align-items: flex-start;
    padding: 13px 0; border-bottom: 1px solid var(--border);
  }
  .news-item:last-child { border-bottom: none; }
  .news-thumb {
    width: 72px; height: 48px; border-radius: 4px; object-fit: cover;
    flex-shrink: 0; background: var(--border);
    border: 2px solid #fff;
  }
  .news-content { flex: 1; min-width: 0; }
  .news-title {
    font-size: 0.78rem; font-weight: 500; color: var(--text);
    line-height: 1.45; text-decoration: none; display: block; margin-bottom: 4px;
  }
  .news-title:hover { color: var(--accent); }
  .news-byline { font-size: 0.62rem; color: var(--text-dim); margin-bottom: 5px; }
  .news-desc { font-size: 0.7rem; color: var(--text-dim); line-height: 1.5; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  .modal-loading, .modal-empty { font-size: 0.74rem; color: var(--text-dim); padding: 18px 0; }
  @media (max-width: 600px) {
    #stock-search { width: 110px; }
    #stock-search:focus { width: 140px; }
    .modal-stock-name { font-size: 1rem; }
    .modal-big-price  { font-size: 1.1rem; }
  }
