/* ── Reset & Variables ──────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0d1117;
  --bg2:       #161b22;
  --bg3:       #21262d;
  --border:    #30363d;
  --text:      #e6edf3;
  --text2:     #8b949e;
  --accent:    #58a6ff;
  --green:     #3fb950;
  --red:       #f85149;
  --yellow:    #d29922;

  --sidebar-w: 220px;
  --topbar-h:  64px;
  --radius:    8px;
  --shadow:    0 4px 16px rgba(0,0,0,.35);
}

html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5; }

/* ── Layout ─────────────────────────────────────────── */
.layout { display: flex; height: 100vh; overflow: hidden; }

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  padding: 0;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 18px 16px;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -.2px;
  border-bottom: 1px solid var(--border);
}

.brand-icon { width: 20px; height: 20px; color: var(--accent); flex-shrink: 0; }

.sidebar-nav {
  flex: 1;
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius);
  color: var(--text2);
  text-decoration: none;
  font-size: 13.5px;
  font-weight: 500;
  transition: background .15s, color .15s;
}
.nav-item svg { width: 16px; height: 16px; flex-shrink: 0; }
.nav-item:hover { background: var(--bg3); color: var(--text); }
.nav-item.active { background: rgba(88,166,255,.12); color: var(--accent); }

.sidebar-footer {
  padding: 14px 18px;
  border-top: 1px solid var(--border);
}

.api-status {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--text2);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--border);
  flex-shrink: 0;
  transition: background .3s;
}
.status-dot.connected    { background: var(--green); box-shadow: 0 0 6px var(--green); }
.status-dot.connecting   { background: var(--yellow); }
.status-dot.error        { background: var(--red); }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

/* Topbar */
.topbar {
  height: var(--topbar-h);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  flex-shrink: 0;
  gap: 12px;
}

.topbar-left { display: flex; align-items: center; gap: 12px; }
.topbar-right { display: flex; align-items: center; gap: 8px; }

.page-title { font-size: 18px; font-weight: 700; letter-spacing: -.3px; }
.last-updated { font-size: 12px; color: var(--text2); }

/* Sections */
.section { display: none; flex: 1; overflow-y: auto; padding: 24px; gap: 20px; flex-direction: column; }
.section.active { display: flex; }

/* ── Buttons ────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius);
  border: none;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity .15s, background .15s;
}
.btn svg { width: 14px; height: 14px; }
.btn:active { opacity: .8; }

.btn-primary { background: var(--accent); color: #0d1117; }
.btn-primary:hover { background: #79c0ff; }

.btn-ghost { background: var(--bg3); color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--border); }

/* ── Cards ──────────────────────────────────────────── */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.card--accent { border-color: rgba(88,166,255,.4); background: rgba(88,166,255,.06); }

.card-label { font-size: 12px; color: var(--text2); text-transform: uppercase; letter-spacing: .6px; margin-bottom: 8px; }
.card-value { font-size: 26px; font-weight: 700; letter-spacing: -.5px; }
.card-sub    { font-size: 12px; color: var(--text2); margin-top: 4px; }

/* ── Panels ─────────────────────────────────────────── */
.panel {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  font-weight: 600;
  font-size: 13.5px;
  border-bottom: 1px solid var(--border);
}

.panel-link { font-size: 12px; color: var(--accent); text-decoration: none; font-weight: 400; }
.panel-link:hover { text-decoration: underline; }

.panel-actions { display: flex; gap: 8px; align-items: center; }

.panel-body { padding: 18px; }

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 800px) { .row { grid-template-columns: 1fr; } }

/* ── Info grid ──────────────────────────────────────── */
.info-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 16px;
  align-items: baseline;
}

.info-key { font-size: 12px; color: var(--text2); }
.info-val { font-size: 13px; font-weight: 500; }

.info-placeholder { color: var(--text2); font-size: 13px; grid-column: 1/-1; }

/* ── Allocation chart ───────────────────────────────── */
.allocation-body {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}
#allocationChart { flex-shrink: 0; }

.legend { display: flex; flex-direction: column; gap: 6px; flex: 1; min-width: 120px; }
.legend-item { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.legend-name { color: var(--text2); flex: 1; }
.legend-pct  { font-weight: 600; }

/* ── Table ──────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }

.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }

.data-table th {
  text-align: left;
  padding: 8px 12px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--text2);
  border-bottom: 1px solid var(--border);
  font-weight: 500;
  white-space: nowrap;
}
.data-table th.num { text-align: right; }

.data-table td {
  padding: 11px 12px;
  border-bottom: 1px solid rgba(48,54,61,.6);
  vertical-align: middle;
}
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: rgba(255,255,255,.025); }

.data-table td.num { text-align: right; font-variant-numeric: tabular-nums; }

.ticker-badge {
  display: inline-block;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 7px;
  font-weight: 700;
  font-size: 12px;
  letter-spacing: .3px;
  font-family: ui-monospace, monospace;
}

.pos-name { color: var(--text2); font-size: 12px; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.positive { color: var(--green); }
.negative { color: var(--red); }
.neutral  { color: var(--text2); }

.pnl-badge {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
}
.pnl-main { font-weight: 600; }
.pnl-pct  { font-size: 11px; opacity: .8; }

.empty-state { text-align: center; color: var(--text2); padding: 32px 0 !important; }

/* ── Form ───────────────────────────────────────────── */
.form-group { margin-bottom: 18px; }
.form-label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; }
.form-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 12px;
  color: var(--text);
  font-size: 13.5px;
  outline: none;
  transition: border-color .15s;
}
.form-input:focus { border-color: var(--accent); }
.form-hint { font-size: 12px; color: var(--text2); margin-top: 6px; line-height: 1.6; }

.form-actions { display: flex; gap: 8px; margin-top: 4px; }

.form-message {
  margin-top: 12px;
  font-size: 13px;
  min-height: 18px;
}
.form-message.success { color: var(--green); }
.form-message.error   { color: var(--red); }

.search-input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  color: var(--text);
  font-size: 13px;
  outline: none;
  width: 180px;
}
.search-input:focus { border-color: var(--accent); }

.select-input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  color: var(--text);
  font-size: 13px;
  outline: none;
  cursor: pointer;
}
.select-input:focus { border-color: var(--accent); }

/* ── Sidebar nav group labels ───────────────────────── */
.nav-group-label {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: .8px;
  color: var(--text2);
  text-transform: uppercase;
  padding: 14px 10px 4px;
  opacity: .55;
  user-select: none;
}
.sidebar-nav .nav-group-label:first-child { padding-top: 6px; }

/* ── Discover: layout ───────────────────────────────── */
#section-discover {
  align-items: stretch;
}

/* ── Discover: search bar ───────────────────────────── */
.discover-search-wrap {
  width: 100%;
  max-width: 680px;
  align-self: center;
}

.discover-search-input {
  width: 100%;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 20px;
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.discover-search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(88,166,255,.12);
}
.discover-search-input::placeholder { color: var(--text2); }

/* ── Discover: results ──────────────────────────────── */
.discover-results {
  max-width: 680px;
  align-self: center;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.discover-result-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 13px 16px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.discover-result-item:hover {
  background: var(--bg3);
  border-color: rgba(88,166,255,.5);
}

.discover-result-symbol {
  font-family: ui-monospace, monospace;
  font-weight: 700;
  font-size: 13px;
  color: var(--accent);
  min-width: 76px;
}
.discover-result-name {
  flex: 1;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.discover-result-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 3px;
}
.discover-result-type {
  font-size: 11px;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: .4px;
}
.exchange-badge {
  display: inline-block;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 7px;
  font-size: 10.5px;
  color: var(--text2);
  font-weight: 600;
  letter-spacing: .3px;
}

.discover-loading { color: var(--text2); font-size: 13px; padding: 16px 0; }
.discover-empty   { color: var(--text2); font-size: 13px; padding: 16px 0; }
.discover-error   { color: var(--red);   font-size: 13px; padding: 16px 0; }

/* ── Discover: stock detail ─────────────────────────── */
.discover-detail {
  max-width: 760px;
  align-self: center;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.discover-detail.hidden { display: none; }

.detail-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text2);
  cursor: pointer;
  background: none;
  border: none;
  padding: 0;
  margin-bottom: 2px;
}
.detail-back:hover { color: var(--text); }

.detail-header {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.detail-logo {
  width: 52px;
  height: 52px;
  border-radius: 10px;
  border: 1px solid var(--border);
  object-fit: contain;
  background: var(--bg3);
  flex-shrink: 0;
}
.detail-logo-placeholder {
  width: 52px;
  height: 52px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  color: var(--text2);
  flex-shrink: 0;
}

.detail-title { flex: 1; min-width: 0; }
.detail-company-name { font-size: 20px; font-weight: 700; letter-spacing: -.3px; line-height: 1.2; }
.detail-ticker-row { display: flex; align-items: center; gap: 6px; margin-top: 5px; flex-wrap: wrap; }

.detail-price-block { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.detail-price {
  font-size: 30px;
  font-weight: 700;
  letter-spacing: -.5px;
  font-variant-numeric: tabular-nums;
}
.detail-change { font-size: 13.5px; font-weight: 600; }

/* Stats grid */
.detail-stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.detail-stat {
  background: var(--bg2);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.detail-stat-label {
  font-size: 11px;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: .5px;
}
.detail-stat-value {
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Description */
.detail-description {
  font-size: 13.5px;
  color: var(--text2);
  line-height: 1.75;
}
.detail-description.clamped {
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.detail-expand-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 12px;
  cursor: pointer;
  padding: 4px 0 0;
  display: block;
}
.detail-expand-btn:hover { text-decoration: underline; }

@media (max-width: 600px) {
  .detail-stats-grid { grid-template-columns: repeat(2, 1fr); }
  .detail-price { font-size: 24px; }
}

/* ── Scrollbar ──────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
