/* ChartLab-specific tokens. Base palette (--bg, --fg, --accent, --border)
   comes from /shared/css/supercala-app-tokens.css. We keep the lighter
   accent-2 used by input-focus rings and the chart-domain colors
   (candle up/down, plot bg/grid/axis/text) — these need to differ from
   the suite-wide semantic green/red, and JS reads them via getComputedStyle
   to re-theme lightweight-charts on the next applyOptions call. */
:root {
  --accent-2: #33b5ec;
  --green: #26a69a;
  --green-faint: rgba(38, 166, 154, 0.15);
  --red: #ef5350;
  --red-faint: rgba(239, 83, 80, 0.15);
  /* --chart-bg is "transparent" so the lightweight-charts canvas lets
     the .chart-pane gradient (set below) read through. JS reads this
     token via getComputedStyle and passes it to chart.applyOptions. */
  --chart-bg: transparent;
  --chart-grid: #1f242d;
  --chart-axis: #30363d;
  --chart-text: #8b949e;
}
body[data-theme="light"] {
  --chart-bg: transparent;
  --chart-grid: #ebeef2;
  --chart-axis: #d0d7de;
  --chart-text: #59636e;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
body { display: grid; grid-template-rows: auto auto 1fr; height: 100vh; }
.hidden { display: none !important; }

button { font-family: inherit; font-size: inherit; cursor: pointer; }
input, select, textarea {
  font-family: inherit; font-size: inherit;
  background: var(--bg-elev-2); color: var(--fg);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 10px;
}
input:focus, select:focus { outline: none; border-color: var(--accent-2); }
.muted { color: var(--fg-dim); } .small { font-size: 12px; }

/* Topbar */
/* Background + backdrop-filter come from supercala-app-tokens.css so the
   page-fade gradient bleeds through. Don't reintroduce a solid bg here. */
.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 16px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
}
/* When mounted as the Charts module of the consolidated MyNetWorth
   (Caliper) service, the template inserts the module-tab strip as a
   fourth grid child between the brand and the search. */
.topbar.with-module-tabs { grid-template-columns: auto auto 1fr auto; }
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--fg); font-weight: 700; font-size: 15px; }
/* Topbar brand mark renders the same SVG as the favicon and the
   supercala.ai product card, so the three surfaces are visually
   one-and-the-same. The SVG carries its own rounded green-gradient
   background, so no pill / padding here. */
.brand-mark { width: 28px; height: 28px; display: block; border-radius: 6px; }
.brand-stack { display: inline-flex; flex-direction: column; line-height: 1.05; }
.brand-studio { font-weight: 400; font-size: 9.5px; letter-spacing: 0.4px; color: var(--fg-muted, #8a8d92); text-transform: uppercase; margin-top: 1px; }
.topbar-mid { display: flex; justify-content: center; }
.search-wrap { position: relative; width: 100%; max-width: 480px; }
#symbol-search { width: 100%; padding: 8px 12px; }
.search-dropdown {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0;
  background: var(--bg-elev-2); border: 1px solid var(--border);
  border-radius: 8px; max-height: 360px; overflow: auto;
  z-index: 1000;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}
.search-row { padding: 10px 12px; display: flex; gap: 8px; align-items: center; cursor: pointer; border-bottom: 1px solid var(--border); }
.search-row:last-child { border-bottom: none; }
.search-row:hover { background: var(--bg-elev-3); }
.search-row .sym { font-weight: 700; font-family: ui-monospace, monospace; }
.search-row .nm { color: var(--fg-dim); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search-row .ex { color: var(--fg-faint); font-size: 11px; }

/* Per-ticker icon (watchlist rows, search results, the active-symbol pill).
   Always sits on a light rounded chip: real logos are often dark-on-
   transparent and would vanish in dark mode, and the monogram fallback fills
   its whole box so the chip never shows behind it. Served by /api/symbol-icon
   which always returns an image, so no onerror handling is needed. */
.tkr-ico {
  width: 20px; height: 20px;
  flex: 0 0 auto;
  border-radius: 5px;
  background: #fff;
  object-fit: contain;
  vertical-align: middle;
}
.tkr-ico.pill-ico { width: 18px; height: 18px; margin-left: 8px; }
/* ChartLab's topbar is a CSS grid (not flex like the other tools), so
   the shared `margin-left: auto` doesn't anchor it. Override with
   justify-self so the cluster pins to the far right of its grid cell. */
.topbar-right { justify-self: end; }

.btn {
  padding: 7px 14px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--bg-elev-2); color: var(--fg);
  font-weight: 500;
  transition: border-color 0.12s, background 0.12s;
}
.btn:hover { border-color: var(--accent-2); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.15); }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ChartLab uses --accent-2 (warmer orange) for hover borders to match
   its overall accent palette. Override the shared icon-only hover. */
.btn.icon-only:hover { border-color: var(--accent-2); }

/* Toolbar */
.toolbar {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px;
  background: var(--bg-elev); border-bottom: 1px solid var(--border);
  overflow-x: auto; white-space: nowrap;
}
.tb-group { display: flex; align-items: center; gap: 2px; }
.tb-sep { width: 1px; height: 18px; background: var(--border); margin: 0 4px; }
.tb-grow { flex: 1; }
.tb {
  background: transparent; border: 1px solid transparent;
  padding: 5px 9px; border-radius: 5px;
  color: var(--fg-dim); font-size: 12.5px;
  font-family: inherit;
}
.tb:hover { background: var(--bg-elev-2); color: var(--fg); }
.tb.active { color: var(--fg); background: var(--bg-elev-3); border-color: var(--border); }
.tb:disabled { color: var(--fg-faint); cursor: not-allowed; opacity: 0.55; }
.tb:disabled:hover { background: transparent; color: var(--fg-faint); }
.tb.gated { position: relative; }
.tb.gated::after {
  content: ""; position: absolute; top: 3px; right: 3px;
  width: 5px; height: 5px; border-radius: 50%; background: var(--amber);
}
.tb.gated.unlocked::after { display: none; }

#symbol-pill .symbol-name { font-weight: 700; font-family: ui-monospace, monospace; padding: 0 8px; }
#symbol-pill .symbol-price { font-weight: 600; padding: 0 8px; }
#symbol-pill .symbol-change.up { color: var(--green); }
#symbol-pill .symbol-change.down { color: var(--red); }

/* Workspace */
.workspace {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100%;
  overflow: hidden;
}
.sidebar {
  display: flex; flex-direction: column;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  overflow: hidden;
}
.sidebar-header {
  padding: 10px 12px;
  display: flex; justify-content: space-between; align-items: center;
  border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--fg-dim);
  text-transform: uppercase; letter-spacing: 0.04em;
}
.watchlist-picker {
  padding: 4px 8px; font-size: 11px;
  background: var(--bg-elev-2); border-radius: 4px;
  text-transform: none; letter-spacing: 0;
  max-width: 130px;
}
.watchlist-rows { flex: 1; overflow: auto; }
.watchlist-row {
  padding: 10px 12px;
  display: grid; grid-template-columns: auto 1fr auto auto auto;
  gap: 8px;
  align-items: center;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-size: 13px;
}
.watchlist-row:hover { background: var(--bg-elev-2); }
.watchlist-row.active { background: var(--bg-elev-3); border-left: 2px solid var(--accent-2); }
.watchlist-row .sym { font-weight: 700; font-family: ui-monospace, monospace; }
.watchlist-row .price { font-family: ui-monospace, monospace; text-align: right; color: var(--fg); }
.watchlist-row .pct { font-family: ui-monospace, monospace; font-size: 11px; padding: 1px 6px; border-radius: 3px; min-width: 56px; text-align: right; }
.watchlist-row .pct.up { color: var(--green); background: var(--green-faint); }
.watchlist-row .pct.down { color: var(--red); background: var(--red-faint); }
/* Loading shimmer for quote cells — shown until the first /api/quotes
   lands (e.g. right after a MyNetWorth sync) instead of a bare "—".
   Tinted off currentColor so it adapts to both light and dark themes. */
.watchlist-row .skel {
  display: inline-block;
  height: 11px;
  border-radius: 3px;
  vertical-align: middle;
  color: var(--fg-dim);
  background-color: color-mix(in srgb, currentColor 12%, transparent);
  background-image: linear-gradient(90deg,
    transparent 20%,
    color-mix(in srgb, currentColor 20%, transparent) 50%,
    transparent 80%);
  background-size: 200% 100%;
  background-repeat: no-repeat;
  animation: wl-shimmer 1.25s ease-in-out infinite;
}
@keyframes wl-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
@media (prefers-reduced-motion: reduce) {
  .watchlist-row .skel { animation: none; }
}
.watchlist-row .remove { background: transparent; border: none; color: var(--fg-faint); padding: 0 4px; opacity: 0; transition: opacity 0.12s; }
.watchlist-row:hover .remove { opacity: 1; }
.watchlist-row .remove:hover { color: var(--red); }
.watchlist-add {
  display: flex; gap: 4px; padding: 8px 10px; border-top: 1px solid var(--border);
}
.watchlist-add input { flex: 1; }
.sidebar-footer { padding: 8px 10px; border-top: 1px solid var(--border); }

/* Chart pane */
/* Paint the SuperCala blue→neutral fade behind the chart canvas. The
   chart's own --chart-bg is transparent (see :root) so the gradient
   reads through; grid/axis/text tokens still drive the in-canvas chrome. */
.chart-pane {
  display: flex; flex-direction: column;
  position: relative;
  background: var(--bg-fade), var(--bg);
  min-height: 0;
}
.chart-host { flex: 3; min-height: 0; position: relative; transition: opacity 200ms ease; }
/* Cross-fade the chart area on a ticker switch (loadSymbol toggles
   .chart-swapping): fade out, swap data while hidden, fade back in — so
   the change reads as a deliberate transition instead of a snap, which
   matters in screen recordings. Opacity only; a transform/scale on the
   chart canvas reads blurry mid-animation. Covers candles, volume,
   indicators, and the drawing overlay since they all live inside
   .chart-host, and the sub-panes (RSI/MACD) get the class too. */
.chart-host.chart-swapping { opacity: 0; }

/* Ticker-switch progress sweep: a thin accent bar that runs across the top of
   the chart pane while loadSymbol() fetches + swaps data, so a switch reads as
   a deliberate transition (and gives feedback on slow loads) on top of the
   cross-fade. Animation only runs while .loading, and only transform/opacity
   are animated — both GPU-cheap and, unlike the chart canvas, a non-bitmap
   element so the motion stays crisp. */
.chart-loader {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 4px;
  z-index: 20;
  overflow: hidden;
  opacity: 0;
  transition: opacity 150ms ease;
  pointer-events: none;
}
.chart-loader.loading { opacity: 1; }
.chart-loader::before {
  content: "";
  position: absolute;
  top: 0; bottom: 0;
  left: 0;
  width: 35%;
  border-radius: 3px;
  background: linear-gradient(90deg, transparent, var(--accent-2, #33b5ec), transparent);
  box-shadow: 0 0 8px var(--accent-2, #33b5ec);
}
.chart-loader.loading::before { animation: chart-loader-sweep 0.9s ease-in-out infinite; }
@keyframes chart-loader-sweep {
  0%   { transform: translateX(-120%); }
  100% { transform: translateX(420%); }
}
@media (prefers-reduced-motion: reduce) {
  .chart-loader.loading::before { animation: none; }
}
.draw-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;  /* JS toggles to ``auto`` while a draw tool is active */
  z-index: 5;
}
.draw-overlay.active { pointer-events: auto; cursor: crosshair; }
.cursor-tip {
  position: absolute;
  pointer-events: none;
  background: rgba(31, 36, 45, 0.96);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-family: ui-monospace, monospace;
  font-size: 11.5px;
  line-height: 1.5;
  z-index: 100;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  white-space: nowrap;
}
.cursor-tip .ct-time { color: var(--fg-dim); margin-bottom: 2px; }
.cursor-tip .ct-up { color: var(--green); }
.cursor-tip .ct-down { color: var(--red); }
body[data-theme="light"] .cursor-tip {
  background: rgba(255, 255, 255, 0.97);
  color: #1f2328;
  box-shadow: 0 4px 14px rgba(31, 35, 40, 0.15);
}
.chart-host.sub-pane {
  flex: 1; min-height: 120px;
  border-top: 1px solid var(--border);
}
.chart-legend {
  position: absolute; top: 8px; left: 12px;
  font-family: ui-monospace, monospace;
  font-size: 12px;
  color: var(--fg-dim);
  pointer-events: none;
  z-index: 10;
  display: flex; flex-direction: column; gap: 2px;
}
.chart-legend .li-row { display: flex; gap: 8px; }
.chart-legend .li-label { min-width: 56px; }
.chart-legend .li-up { color: var(--green); }
.chart-legend .li-down { color: var(--red); }

/* Active studies (indicators + running scripts), TradingView-style legend rows */
.chart-legend .legend-study {
  display: flex; align-items: center; gap: 6px;
  pointer-events: auto;
  padding: 1px 4px 1px 2px;
  margin-top: 1px;
  border-radius: 4px;
}
.chart-legend .legend-study:hover { background: rgba(127, 127, 127, 0.14); }
.chart-legend .lg-swatch {
  display: inline-block; width: 14px; height: 3px;
  border-radius: 2px; flex: none;
}
.chart-legend .lg-icon { font-size: 11px; line-height: 1; }
.chart-legend .lg-name { color: var(--fg); }
.chart-legend .lg-val { color: var(--fg-dim); }
.chart-legend .lg-plot {
  display: inline-flex; align-items: center; gap: 4px;
  color: var(--fg-dim);
}
.chart-legend .lg-x {
  pointer-events: auto;
  background: transparent; border: none; cursor: pointer;
  color: var(--fg-dim);
  font-size: 14px; line-height: 1;
  width: 16px; height: 16px; border-radius: 4px;
  padding: 0; margin-left: 2px;
  opacity: 0; transition: opacity 0.1s ease;
}
.chart-legend .legend-study:hover .lg-x { opacity: 1; }
.chart-legend .lg-x:hover { color: var(--red); background: rgba(127, 127, 127, 0.2); }
/* Email-alert subscribe on the active signal — visible (not hover-only) so the
   feature is discoverable. */
.chart-legend .lg-bell {
  pointer-events: auto;
  background: transparent; border: none; cursor: pointer;
  color: var(--fg-dim);
  font-size: 12px; line-height: 1;
  width: 18px; height: 18px; border-radius: 4px;
  padding: 0; margin-left: 2px;
  opacity: 0.7; transition: opacity 0.1s ease, background 0.1s ease;
}
.chart-legend .legend-study:hover .lg-bell { opacity: 1; }
.chart-legend .lg-bell:hover { background: rgba(127, 127, 127, 0.2); }

/* Modal + toast */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 5000;
}
.modal-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 100%; max-width: 420px;
  position: relative;
}
.modal-card h2 { margin: 0 0 8px; }
.modal-card p { margin: 0 0 12px; color: var(--fg-dim); }
.modal-close {
  position: absolute; top: 8px; right: 8px;
  background: transparent; border: none;
  color: var(--fg-dim); font-size: 22px;
  width: 32px; height: 32px; border-radius: 6px;
}
.modal-close:hover { background: var(--bg-elev-2); color: var(--fg); }

.toast {
  position: fixed;
  bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg-elev-3);
  color: var(--fg);
  padding: 10px 16px; border-radius: 8px;
  border: 1px solid var(--border);
  z-index: 6000;
  font-size: 13px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}

/* Scripts panel — slide-in from the right of the chart pane. */
.scripts-panel {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 540px;
  max-width: 90vw;
  background: var(--bg-elev);
  border-left: 1px solid var(--border);
  z-index: 1500;
  display: flex; flex-direction: column;
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.45);
}
.scripts-tabs {
  display: flex; align-items: stretch;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev-2);
}
.scripts-tab {
  flex: 1; padding: 11px 12px;
  background: transparent; border: none;
  color: var(--fg-dim); font-size: 13px;
  border-bottom: 2px solid transparent;
}
.scripts-tab.active { color: var(--fg); border-bottom-color: var(--accent-2); }
.scripts-close {
  background: transparent; border: none;
  color: var(--fg-dim); font-size: 22px;
  width: 38px;
}
.scripts-close:hover { color: var(--fg); background: var(--bg-elev-3); }
.scripts-tab-pane { display: none; flex: 1; flex-direction: column; padding: 12px 14px; min-height: 0; overflow: auto; }
.scripts-tab-pane.active { display: flex; }
.scripts-meta { margin-bottom: 8px; }
.scripts-meta input { width: 100%; font-weight: 600; }
.script-editor {
  flex: 1; min-height: 240px;
  border: 1px solid var(--border); border-radius: 6px;
  overflow: hidden;
  background: var(--bg);
}
.script-editor .cm-editor { height: 100%; font-size: 13px; }
.script-editor .cm-scroller { font-family: ui-monospace, "SF Mono", Consolas, monospace !important; }
.scripts-actions {
  display: flex; gap: 6px; align-items: center;
  margin: 10px 0 6px;
  flex-wrap: wrap;
}
.scripts-actions .actions-grow { flex: 1; }
.scripts-actions .btn { font-size: 12.5px; padding: 6px 10px; }
.script-status { padding-top: 4px; }
.script-error {
  margin-top: 8px;
  padding: 8px 10px;
  background: rgba(239, 83, 80, 0.12);
  color: var(--red);
  border: 1px solid var(--red);
  border-radius: 6px;
  font-family: ui-monospace, monospace;
  font-size: 12px;
  white-space: pre-wrap;
}
.scripts-list { display: flex; flex-direction: column; gap: 6px; }
.scripts-list .script-row {
  padding: 10px 12px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  display: flex; flex-direction: column; gap: 2px;
}
.scripts-list .script-row:hover { border-color: var(--accent-2); }
.scripts-list .script-row .title { font-weight: 600; }
.scripts-list .script-row .desc { font-size: 12px; color: var(--fg-dim); }
.scripts-list .script-row .actions { margin-top: 6px; display: flex; gap: 6px; }
.scripts-list .script-row .actions button {
  background: transparent; border: none;
  color: var(--fg-dim); padding: 1px 6px; border-radius: 4px;
  font-size: 11px;
}
.scripts-list .script-row .actions button:hover { color: var(--red); }

.alerts-badge {
  background: var(--red); color: white;
  border-radius: 999px; padding: 0 6px;
  font-size: 10px; font-weight: 700;
  margin-left: 2px;
}
.alerts-tab { cursor: pointer; }
.alerts-tab.active { color: var(--fg) !important; border-bottom-color: var(--accent-2) !important; }
.alerts-pane.hidden { display: none; }
.alert-row {
  padding: 10px 12px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  display: grid; grid-template-columns: 1fr auto auto;
  gap: 8px; align-items: center;
  margin-bottom: 6px;
}
.alert-row.disabled { opacity: 0.5; }
.alert-row .a-title { font-weight: 600; }
.alert-row .a-sub { font-size: 11px; color: var(--fg-dim); margin-top: 2px; }
.alert-row .a-actions { display: flex; gap: 4px; }
.alert-row .a-actions button {
  background: transparent; border: none; color: var(--fg-dim);
  padding: 2px 8px; border-radius: 4px; cursor: pointer;
}
.alert-row .a-actions button:hover { color: var(--fg); }
.alert-row .a-actions button.del:hover { color: var(--red); }
.history-row {
  padding: 8px 10px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: 4px;
}
.history-row.unread { border-left: 3px solid var(--accent-2); }
.history-row .h-when { font-size: 11px; color: var(--fg-dim); }

.error {
  background: rgba(239, 83, 80, 0.12);
  color: var(--red);
  border: 1px solid var(--red);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 13px;
}

@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: 200px 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); }
  .scripts-panel { width: 100%; }
}

/* ---- ChartLab Coach voice widget ---- */
/* The widget injects .vc-pill into <body>. Hidden until the app says the user
   is signed in; locked (greyed) until they're on Pro — clicks in that state are
   routed to the upgrade modal by a capture-phase gate. */
.vc-pill { display: none !important; }
.vc-toast { display: none !important; }
body.cl-voice-visible .vc-pill { display: flex !important; }
body.cl-voice-visible .vc-toast { display: block !important; }
body.cl-voice-locked #supercala-voice-btn { filter: grayscale(0.6) brightness(0.9); }
