:root {
  --bg:      #0d1117;
  --surface: #161b22;
  --s2:      #21262d;
  --border:  #30363d;
  --text:    #e6edf3;
  --muted:   #8b949e;
  --accent:  #58a6ff;
  --buy:     #3fb950;
  --buy-dim: #0d2119;
  --sell:    #f85149;
  --sell-dim:#2d1117;
  --warn:    #d29922;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text); min-height: 100vh; font-size: 14px;
}

/* ── Header ── */
header {
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 12px 24px; display: flex; align-items: center; gap: 16px;
  position: sticky; top: 0; z-index: 20;
}
.logo { font-size: 18px; font-weight: 700; white-space: nowrap; }
.logo em { color: var(--accent); font-style: normal; }
.controls { display: flex; gap: 10px; align-items: center; margin-left: auto; flex-wrap: wrap; }
select {
  background: var(--s2); color: var(--text); border: 1px solid var(--border);
  padding: 7px 28px 7px 12px; border-radius: 6px; font-size: 13px; cursor: pointer;
  min-width: 210px; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238b949e' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center;
}
select:focus { outline: none; border-color: var(--accent); }
.btn {
  background: var(--accent); color: #0d1117; border: none; padding: 7px 18px;
  border-radius: 6px; font-size: 13px; font-weight: 700; cursor: pointer;
  white-space: nowrap; transition: filter .15s;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { filter: brightness(1.1); }
.btn:disabled { opacity: .5; cursor: not-allowed; filter: none; }

main { padding: 24px; max-width: 1600px; margin: 0 auto; }

/* ── Welcome ── */
.welcome { text-align: center; padding: 80px 24px; }
.welcome h2 { font-size: 28px; margin-bottom: 10px; }
.welcome p {
  color: var(--muted); margin-bottom: 28px; max-width: 500px;
  margin-left: auto; margin-right: auto; line-height: 1.6;
}
.welcome .btn { font-size: 15px; padding: 10px 28px; }

/* ── Stats ── */
.stats { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.stat-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 16px; min-width: 110px;
}
.stat-card .val { font-size: 22px; font-weight: 700; line-height: 1.2; }
.stat-card .lbl {
  font-size: 11px; color: var(--muted); text-transform: uppercase;
  letter-spacing: .05em; margin-top: 2px;
}
.stat-card.buy  .val { color: var(--buy); }
.stat-card.sell .val { color: var(--sell); }
.stat-card.mkt  { margin-left: auto; text-align: right; }
.stat-card.mkt  .val { font-size: 14px; color: var(--accent); }

/* ── Tables grid ── */
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 1100px) { .grid { grid-template-columns: 1fr; } }

.table-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; overflow: hidden;
}
.table-head {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.table-head.buy  { background: rgba(63,185,80,.07); }
.table-head.sell { background: rgba(248,81,73,.07); }
.table-head.hold { background: rgba(210,153,34,.07); }
.sig-badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px;
  border-radius: 20px; font-weight: 700; font-size: 12px;
}
.sig-badge.buy  { background: var(--buy-dim);  color: var(--buy); }
.sig-badge.sell { background: var(--sell-dim); color: var(--sell); }
.sig-badge.hold { background: rgba(210,153,34,.15); color: var(--warn); }
.th-title { font-weight: 600; font-size: 13px; }
.count-pill {
  margin-left: auto; background: var(--s2); color: var(--muted);
  border-radius: 20px; padding: 2px 8px; font-size: 12px;
}

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left; padding: 8px 10px; font-size: 11px; font-weight: 600;
  color: var(--muted); text-transform: uppercase; letter-spacing: .05em;
  background: var(--s2); white-space: nowrap;
}
tbody td { padding: 8px 10px; border-bottom: 1px solid rgba(48,54,61,.6); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }

@media (hover: hover) {
  tbody tr:hover td { background: rgba(255,255,255,.03); }
}

.sym  { font-weight: 700; font-size: 13px; }
.co   { font-size: 11px; color: var(--muted); white-space: nowrap; }
.pr   { font-weight: 600; }
.ch   { font-size: 11px; }
.pos  { color: var(--buy); }
.neg  { color: var(--sell); }

.rsi-wrap  { display: flex; align-items: center; gap: 6px; }
.rsi-num   { min-width: 30px; font-weight: 700; font-size: 13px; }
.rsi-track { flex: 1; min-width: 44px; height: 4px; background: var(--s2); border-radius: 2px; overflow: hidden; }
.rsi-fill  { height: 100%; border-radius: 2px; }

.score-pill { display: inline-block; padding: 2px 8px; border-radius: 20px; font-weight: 700; font-size: 12px; }
.score-pill.buy  { background: var(--buy-dim);  color: var(--buy); }
.score-pill.sell { background: var(--sell-dim); color: var(--sell); }
.score-pill.hold { background: rgba(210,153,34,.15); color: var(--warn); }
.sigs { color: var(--muted); font-size: 11px; line-height: 1.6; max-width: 180px; }

.chart-btn {
  background: var(--s2); border: 1px solid var(--border);
  color: var(--muted); padding: 4px 9px; border-radius: 6px;
  font-size: 12px; cursor: pointer; white-space: nowrap;
  transition: background .15s, color .15s, border-color .15s;
  -webkit-tap-highlight-color: transparent;
}
.chart-btn:hover  { background: var(--accent); color: #0d1117; border-color: var(--accent); }
.chart-btn.active { background: var(--accent); color: #0d1117; border-color: var(--accent); }

/* button sits below company name in the stock cell on desktop */
tbody td:first-child .chart-btn { margin-top: 7px; display: inline-flex; align-items: center; }

/* ── Chart popup modal ── */
#chartModal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, .75);
  backdrop-filter: blur(3px);
  align-items: center;
  justify-content: center;
  padding: 16px;
}
#chartModal.open { display: flex; }

.modal-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 1080px;
  max-height: calc(100vh - 32px);
  max-height: calc(100svh - 32px); /* svh excludes browser chrome — never clips the top */
  overflow: hidden;
  box-shadow: 0 24px 64px rgba(0,0,0,.6);
}

.modal-hdr {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--s2);
  flex-shrink: 0;
}
.modal-ticker { font-size: 20px; font-weight: 800; }
.modal-name   { color: var(--muted); font-size: 13px; }
.modal-cur    { color: var(--muted); font-size: 11px; background: var(--border); padding: 2px 6px; border-radius: 4px; }
.modal-close  {
  margin-left: auto;
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 30px; height: 30px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background .15s, color .15s;
  -webkit-tap-highlight-color: transparent;
}
.modal-close:hover { background: var(--sell-dim); color: var(--sell); border-color: var(--sell); }

/* S/R legend */
.sr-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  flex-shrink: 0;
}
.sr-item  { display: flex; align-items: center; gap: 5px; }
.sr-dot   { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.bb-swatch{ width: 18px; height: 2px; background: rgba(88,166,255,.6); border-radius: 1px; }

/* Scrollable chart body */
.modal-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 16px 16px;
}

.chart-lbl {
  font-size: 11px; color: var(--muted); text-transform: uppercase;
  letter-spacing: .04em; margin-bottom: 6px;
}
.chart-gap { height: 10px; }
#priceChartEl { width: 100%; }
#rsiChartEl   { width: 100%; }

/* ── Misc ── */
.loading { text-align: center; padding: 80px 24px; color: var(--muted); }
.spinner {
  width: 36px; height: 36px; border: 3px solid var(--s2);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin .7s linear infinite; margin: 0 auto 14px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading .sub { margin-top: 6px; font-size: 12px; }
.empty { text-align: center; padding: 28px; color: var(--muted); font-size: 13px; }
.disclaimer {
  margin-top: 24px; padding: 10px 14px; background: var(--surface);
  border: 1px solid var(--border); border-left: 3px solid var(--warn);
  border-radius: 6px; color: var(--muted); font-size: 12px; line-height: 1.5;
}
.error-msg { color: var(--sell); padding: 40px; text-align: center; }

/* suppress TradingView attribution logo injected by lightweight-charts */
a[href*="tradingview.com"] { display: none !important; }

/* ── Tooltips — JS-rendered at position:fixed to escape overflow clipping ── */
[data-tip] { cursor: help; }

#js-tip {
  position: fixed;
  z-index: 9999;
  max-width: 240px;
  background: var(--s2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 11px;
  font-weight: 400;
  line-height: 1.5;
  padding: 7px 10px;
  border-radius: 7px;
  box-shadow: 0 4px 16px rgba(0,0,0,.5);
  pointer-events: none;
  white-space: normal;
  word-break: break-word;
}


/* ════════════════════════════════════════
   MOBILE  ≤ 640px
   ════════════════════════════════════════ */
@media (max-width: 640px) {

  /* Header: logo on its own row, controls fill below */
  header { flex-wrap: wrap; padding: 10px 14px; gap: 8px; }
  .logo { flex: 0 0 100%; font-size: 16px; }
  .controls { margin-left: 0; width: 100%; }
  select { flex: 1; min-width: 0; font-size: 16px; /* prevent iOS auto-zoom */ }
  .btn { flex-shrink: 0; }

  /* Main */
  main { padding: 12px; }

  /* Welcome */
  .welcome { padding: 40px 8px; }
  .welcome h2 { font-size: 22px; }
  .welcome .btn { width: 100%; max-width: 280px; }

  /* Stats: 2-column grid */
  .stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
  .stat-card { min-width: 0; padding: 9px 12px; }
  .stat-card .val { font-size: 18px; }
  .stat-card.mkt { margin-left: 0; text-align: left; }
  .stat-card.mkt .val { font-size: 13px; }

  /* ── Table → Card list ──
     display:contents on td:nth-child(1) lets its children (.sym, .co, .chart-btn)
     participate directly in the tr flex layout, so flex `order` can push the
     chart button to the very bottom of the card regardless of DOM position.
  */
  .table-card { border-radius: 0; border-left: none; border-right: none; }
  .table-wrap { overflow-x: visible; }
  table, tbody { display: block; width: 100%; }
  thead { display: none; }

  tbody tr {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    padding: 12px;
    margin: 8px 0;
    background: var(--s2);
    border-radius: 10px;
    border: 1px solid var(--border);
  }
  tbody td { border-bottom: none; padding: 0; vertical-align: top; }
  tbody tr:last-child td { border-bottom: none; }

  /* Dissolve the stock cell so .sym, .co, .chart-btn become direct flex children */
  tbody td:nth-child(1) { display: contents; }

  /* Ticker — full width, first */
  tbody td:nth-child(1) .sym {
    order: 1; flex: 0 0 100%;
    font-size: 15px; padding-bottom: 2px;
  }
  /* Company name — full width, second; bottom border separates from data rows */
  tbody td:nth-child(1) .co {
    order: 2; flex: 0 0 100%;
    white-space: normal;
    padding-bottom: 10px; margin-bottom: 8px;
    border-bottom: 1px solid var(--border);
  }

  /* Price — left 55%, row 3 */
  tbody td:nth-child(2) { order: 3; flex: 0 0 55%; padding: 4px 0; }

  /* 7D change — right 45%, row 3, with tiny label */
  tbody td:nth-child(4) {
    order: 3; flex: 0 0 45%; padding: 4px 0;
    display: flex; flex-direction: column; align-items: flex-end; gap: 1px;
  }
  tbody td:nth-child(4)::before {
    content: "7 DAY";
    font-size: 9px; color: var(--muted); letter-spacing: .05em;
  }

  /* RSI — left 55%, row 4 */
  tbody td:nth-child(3) { order: 4; flex: 0 0 55%; padding: 4px 0; }
  .rsi-track { min-width: 60px; }

  /* Score — right 45%, row 4 */
  tbody td:nth-child(6) {
    order: 4; flex: 0 0 45%; padding: 4px 0;
    display: flex; justify-content: flex-end; align-items: center;
  }

  /* SMA — hidden on mobile */
  tbody td:nth-child(5) { display: none; }

  /* Signals — full width, row 5, top border */
  tbody td:nth-child(7) {
    order: 5; flex: 0 0 100%;
    padding: 8px 0 0; margin-top: 6px;
    border-top: 1px solid var(--border);
  }
  .sigs { max-width: none; font-size: 11px; line-height: 1.7; }

  /* Chart button — full width, LAST row, top border */
  tbody td:nth-child(1) .chart-btn {
    order: 6; flex: 0 0 100%;
    margin-top: 10px; padding: 9px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 8px; font-size: 13px;
    border-color: var(--accent); color: var(--accent);
    min-height: 0;
  }

  /* Disclaimer */
  .disclaimer { margin-top: 14px; font-size: 11px; }

  /* ── Modal: slide up as bottom sheet ── */
  #chartModal { padding: 0; align-items: flex-end; }
  .modal-panel {
    border-radius: 18px 18px 0 0;
    max-height: 92vh;
    max-height: 92svh; /* svh = visible area only, never overflows above screen */
  }
  .modal-hdr { padding: 14px; }
  .modal-ticker { font-size: 17px; }
  .modal-name   { font-size: 12px; }
  .modal-close  { width: 34px; height: 34px; font-size: 15px; }
  .sr-legend { padding: 6px 14px; gap: 8px; font-size: 11px; }
  .modal-body { padding: 10px 12px 20px; }
}
