/* ═══════════════════════════════════════════
   CHART GLOBAL PANEL — chart_global_panel.css
   Reusable slide-in panel for historical metric charts
   ═══════════════════════════════════════════ */

/* ── Overlay (match stock global panel motion) ── */
#chartPanelOverlay {
  position: fixed;
  inset: 0;
  z-index: 8500;
  display: flex;
  justify-content: flex-end;
  background: rgba(0, 0, 0, 0);
  backdrop-filter: blur(0px);
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity .35s ease,
    visibility .35s ease,
    background .4s ease,
    backdrop-filter .45s ease;
}

#chartPanelOverlay.cgp-open {
  visibility: visible;
  opacity: 1;
  background: rgba(0, 0, 0, .4);
  backdrop-filter: blur(6px);
  pointer-events: auto;
}

/* ── Panel shell ── */
#chartGlobalPanel {
  width: min(920px, 96vw);
  height: 100%;
  min-width: 360px;
  max-width: 96vw;
  background: var(--surface-panel-stronger, #07071a);
  border-left: 1px solid var(--border, rgba(255, 255, 255, .08));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: 'Nunito Sans', 'Sarabun', sans-serif;
  font-size: 1rem;
  color: var(--text-primary, #f1f5f9);
  position: relative;
  transform: translateX(100%) scale(.98);
  opacity: 0;
  box-shadow: -20px 0 60px rgba(0, 0, 0, 0);
  transition: transform .4s cubic-bezier(.16, 1, .3, 1),
    opacity .35s ease,
    box-shadow .45s ease;
}

#chartPanelOverlay.cgp-open #chartGlobalPanel {
  transform: translateX(0) scale(1);
  opacity: 1;
  box-shadow: -20px 0 80px rgba(0, 0, 0, .5),
    -4px 0 20px rgba(124, 58, 237, .08);
}

@media (prefers-reduced-motion: reduce) {
  #chartPanelOverlay,
  #chartGlobalPanel {
    transition-duration: .01ms !important;
  }
}

/* ── Top bar ── */
.cgp-topbar {
  height: 52px; display: flex; align-items: center; gap: 10px;
  padding: 0 18px; flex-shrink: 0;
  border-bottom: 1px solid var(--surface-scrollbar-soft, rgba(255,255,255,.07));
  background: var(--surface-topbar, rgba(7,7,26,.98));
}
.cgp-topbar-sym {
  font-size: 1.125rem; font-weight: 800; padding: 4px 12px; border-radius: 7px;
  background: rgba(124,58,237,.18); color: var(--purple-light, #a78bfa);
  border: 1px solid rgba(124,58,237,.28);
}
.cgp-topbar-metric {
  font-size: 1rem; font-weight: 600;
  color: var(--text-soft, rgba(255,255,255,.55));
}
.cgp-topbar-sep {
  width: 1px; height: 18px;
  background: var(--surface-hover-strong, rgba(255,255,255,.08));
  flex-shrink: 0;
}
.cgp-close-btn {
  margin-left: auto;
  background: none; border: none;
  color: var(--text-faint, rgba(255,255,255,.3));
  cursor: pointer; font-size: 1.12rem; padding: 4px 8px; border-radius: 6px;
  line-height: 1; transition: color .15s;
}
.cgp-close-btn:hover { color: var(--text-primary, #f1f5f9); }

/* ── Scrollable body ── */
.cgp-body {
  flex: 1; overflow-y: auto; padding: 0;
  display: flex; flex-direction: column;
}
.cgp-body::-webkit-scrollbar { width: 3px; }
.cgp-body::-webkit-scrollbar-thumb {
  background: var(--surface-scrollbar-soft, rgba(255,255,255,.07)); border-radius: 99px;
}

/* ── Hero section ── */
.cgp-hero {
  padding: 24px 24px 20px;
  background: linear-gradient(135deg, rgba(124,58,237,.12) 0%, rgba(6,182,212,.06) 50%, rgba(7,7,26,0) 100%);
  border-bottom: 1px solid var(--surface-scrollbar-soft, rgba(255,255,255,.06));
}
.cgp-hero-top {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
}
.cgp-hero-left { flex: 1; }
.cgp-hero-kicker {
  font-size: 1rem; font-weight: 700; letter-spacing: .12em;
  text-transform: uppercase; color: var(--text-faint, rgba(255,255,255,.3));
  margin-bottom: 6px;
  display: flex; align-items: center; gap: 8px;
}
.cgp-hero-kicker i {
  color: var(--purple-light, #a78bfa); font-size: 1rem;
}
.cgp-hero-title {
  margin: 0; font-size: 1.25rem; font-weight: 800; line-height: 1.2;
  color: var(--text-primary, #f1f5f9);
}
.cgp-hero-sub {
  font-size: 1rem; color: var(--text-soft, rgba(255,255,255,.5));
  margin-top: 4px; line-height: 1.5;
}
.cgp-hero-value-box {
  flex: 0 0 auto; text-align: right;
}
.cgp-hero-value {
  font-size: 1.75rem; font-weight: 900; line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--text-primary, #f1f5f9);
}
.cgp-hero-change {
  font-size: 1rem; font-weight: 700; margin-top: 6px;
  padding: 3px 10px; border-radius: 99px; display: inline-block;
}
.cgp-hero-change.is-up {
  background: rgba(16,185,129,.12); color: var(--green, #10b981);
}
.cgp-hero-change.is-down {
  background: rgba(239,68,68,.12); color: var(--red, #ef4444);
}

/* Hero KPI cards */
.cgp-hero-kpis {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
  margin-top: 16px;
}
.cgp-hero-kpi {
  background: var(--surface-card-soft, rgba(255,255,255,.03));
  border: 1px solid var(--surface-scrollbar-soft, rgba(255,255,255,.06));
  border-radius: 10px; padding: 10px 14px;
}
.cgp-hero-kpi-label {
  font-size: 1rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: .4px; color: var(--text-faint, rgba(255,255,255,.3));
}
.cgp-hero-kpi-value {
  font-size: 1.38rem; font-weight: 900; line-height: 1.1;
  color: var(--text-primary, #f1f5f9); margin-top: 6px;
  font-variant-numeric: tabular-nums;
}
.cgp-hero-kpi-value.is-good { color: var(--green, #10b981); }
.cgp-hero-kpi-value.is-bad  { color: var(--red, #ef4444); }
.cgp-hero-kpi-sub {
  font-size: 1rem; line-height: 1.5;
  color: var(--text-softer, rgba(255,255,255,.4));
  margin-top: 4px;
}

/* ── Chart section ── */
.cgp-chart-section {
  position: relative;
  padding: 20px 24px;
  border-bottom: 1px solid var(--surface-scrollbar-soft, rgba(255,255,255,.06));
}
.cgp-chart-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px;
}
.cgp-chart-title {
  font-size: 1rem; font-weight: 700;
  color: var(--text-primary, #f1f5f9);
  display: flex; align-items: center; gap: 8px;
}
.cgp-chart-title i { color: var(--cyan, #06b6d4); }
.cgp-chart-period-bar { display: flex; gap: 4px; }
.cgp-chart-period-btn {
  padding: 4px 10px; border-radius: 6px;
  border: 1px solid transparent; background: transparent;
  color: var(--text-softer, rgba(255,255,255,.35));
  font-size: 1rem; font-weight: 600; cursor: pointer;
  font-family: inherit; transition: all .15s;
}
.cgp-chart-period-btn:hover { color: var(--text-soft, rgba(255,255,255,.6)); }
.cgp-chart-period-btn.active {
  background: rgba(124,58,237,.18); color: var(--purple-light, #a78bfa);
  border-color: rgba(124,58,237,.3);
}

/* Bar chart container */
.cgp-chart-canvas {
  position: relative; height: 240px;
  background: var(--surface-chart, rgba(5,5,21,.6));
  border-radius: 12px; border: 1px solid var(--surface-scrollbar-soft, rgba(255,255,255,.06));
  overflow: hidden; padding: 12px 16px 32px;
  display: flex; align-items: flex-end; gap: 6px;
}

/* Individual bars */
.cgp-bar-group {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px;
  position: relative; min-width: 0;
}
.cgp-bar {
  width: 100%; max-width: 48px;
  border-radius: 6px 6px 2px 2px;
  transition: height .5s cubic-bezier(.4,0,.2,1), opacity .3s;
  position: relative; cursor: pointer;
  min-height: 4px;
}
.cgp-bar:hover { opacity: .85; filter: brightness(1.15); }
.cgp-bar-value {
  position: absolute; top: -20px; left: 50%; transform: translateX(-50%);
  font-size: 1rem; font-weight: 700;
  color: var(--text-primary, #f1f5f9);
  white-space: nowrap; opacity: 0; transition: opacity .15s;
  pointer-events: none;
}
.cgp-bar:hover .cgp-bar-value { opacity: 1; }
.cgp-bar-label {
  font-size: 1rem; color: var(--text-softer, rgba(255,255,255,.35));
  text-align: center; white-space: nowrap;
}

/* Bar colors */
.cgp-bar.positive {
  background: linear-gradient(180deg, rgba(16,185,129,.9) 0%, rgba(16,185,129,.4) 100%);
}
.cgp-bar.negative {
  background: linear-gradient(180deg, rgba(239,68,68,.9) 0%, rgba(239,68,68,.4) 100%);
}

/* Forecast bar (special) */
.cgp-forecast-group {
  position: relative;
}
.cgp-forecast-group::before {
  content: '';
  position: absolute; left: -6px; top: 0; bottom: 0;
  width: 1px;
  background: repeating-linear-gradient(
    180deg,
    rgba(255,255,255,.1) 0px,
    rgba(255,255,255,.1) 4px,
    transparent 4px,
    transparent 8px
  );
}
.cgp-forecast-bar {
  animation: cgpForecastPulse 2s ease-in-out infinite;
}
.cgp-forecast-bar .cgp-bar-value {
  opacity: 1 !important;
}
@keyframes cgpForecastPulse {
  0%,100% { opacity: .7; }
  50% { opacity: 1; }
}

/* Y-axis guide lines */
.cgp-chart-gridlines {
  position: absolute; inset: 12px 16px 32px;
  display: flex; flex-direction: column; justify-content: space-between;
  pointer-events: none;
}
.cgp-chart-gridline {
  border-top: 1px dashed rgba(255,255,255,.06);
  position: relative;
}
.cgp-chart-gridline-label {
  position: absolute; left: -4px; top: -8px;
  font-size: 1rem; color: var(--text-faint, rgba(255,255,255,.2));
  font-variant-numeric: tabular-nums;
}

/* ── AI Analysis section ── */
.cgp-ai-section {
  padding: 20px 24px;
  border-bottom: 1px solid var(--surface-scrollbar-soft, rgba(255,255,255,.06));
}
.cgp-ai-header {
  display: flex; align-items: center; gap: 8px; margin-bottom: 12px;
}
.cgp-ai-icon {
  width: 32px; height: 32px; border-radius: 10px;
  background: linear-gradient(135deg, rgba(124,58,237,.2), rgba(6,182,212,.15));
  border: 1px solid rgba(124,58,237,.25);
  display: flex; align-items: center; justify-content: center;
  font-size: 1rem;
}
.cgp-ai-title {
  font-size: 1rem; font-weight: 700;
  color: var(--text-primary, #f1f5f9);
}
.cgp-ai-badge {
  margin-left: auto; font-size: 1rem; font-weight: 700;
  padding: 2px 8px; border-radius: 99px;
  background: rgba(6,182,212,.12); color: var(--cyan, #06b6d4);
  border: 1px solid rgba(6,182,212,.2);
}
.cgp-ai-bubble {
  background: var(--surface-card, rgba(255,255,255,.04));
  border: 1px solid var(--surface-scrollbar-soft, rgba(255,255,255,.07));
  border-radius: 14px; padding: 16px 18px;
  font-size: 1rem; line-height: 1.8;
  color: var(--text-soft, rgba(255,255,255,.55));
}
.cgp-ai-bubble p { margin: 8px 0; }
.cgp-ai-bubble p:first-child { margin-top: 0; }
.cgp-ai-bubble p:last-child { margin-bottom: 0; }
.cgp-ai-bubble strong {
  color: var(--text-primary, #f1f5f9); font-weight: 700;
}
.cgp-ai-bubble .highlight-good { color: var(--green, #10b981); font-weight: 600; }
.cgp-ai-bubble .highlight-bad  { color: var(--red, #ef4444); font-weight: 600; }
.cgp-ai-bubble .highlight-info { color: var(--cyan, #06b6d4); font-weight: 600; }

/* AI typing animation */
.cgp-ai-typing {
  display: inline-flex; gap: 4px; align-items: center;
  padding: 8px 0;
}
.cgp-ai-typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--purple-light, #a78bfa);
  animation: cgpTyping 1.2s ease-in-out infinite;
}
.cgp-ai-typing span:nth-child(2) { animation-delay: .15s; }
.cgp-ai-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes cgpTyping {
  0%,100% { opacity: .3; transform: scale(.8); }
  50% { opacity: 1; transform: scale(1); }
}

/* ── Comparison table ── */
.cgp-compare-section {
  padding: 20px 24px;
}
.cgp-compare-title {
  font-size: 1rem; font-weight: 700; color: var(--text-primary, #f1f5f9);
  display: flex; align-items: center; gap: 8px; margin-bottom: 12px;
}
.cgp-compare-title i { color: var(--purple-light, #a78bfa); }
.cgp-compare-table {
  width: 100%; border-collapse: collapse; font-size: 1rem;
}
.cgp-compare-table th {
  padding: 8px 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .4px; color: var(--text-softer, rgba(255,255,255,.35));
  text-align: right; border-bottom: 1px solid var(--surface-hover-strong, rgba(255,255,255,.08));
  background: rgba(0,0,0,.15);
}
.cgp-compare-table th:first-child { text-align: left; }
.cgp-compare-table td {
  padding: 8px 10px; text-align: right;
  color: var(--text-soft, rgba(255,255,255,.6));
  border-bottom: 1px solid rgba(255,255,255,.03);
  font-variant-numeric: tabular-nums;
}
.cgp-compare-table td:first-child {
  text-align: left; color: var(--text-primary, #f1f5f9); font-weight: 600;
}
.cgp-compare-table tr:hover td {
  background: var(--surface-hover, rgba(255,255,255,.025));
}

/* ── Responsive ── */
@media (max-width: 600px) {
  #chartGlobalPanel { width: 100vw; }
  .cgp-hero-kpis { grid-template-columns: 1fr; }
  .cgp-hero-top { flex-direction: column; }
  .cgp-hero-value-box { text-align: left; }
}

/* ── Clickable cell styling in deep table ── */
td.cgp-clickable {
  cursor: pointer; position: relative;
  transition: background .15s;
}
td.cgp-clickable:hover {
  background: rgba(124,58,237,.08) !important;
}
td.cgp-clickable::after {
  content: '📊';
  position: absolute; top: 2px; right: 2px;
  font-size: 1rem; opacity: 0;
  transition: opacity .15s;
  pointer-events: none;
}
td.cgp-clickable:hover::after { opacity: .6; }

/* ═══ METRIC OVERLAY COMPARE ═══ */
.cgp-overlay-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 24px; flex-wrap: wrap;
  border-bottom: 1px solid var(--surface-scrollbar-soft, rgba(255,255,255,.06));
  background: rgba(124,58,237,.04);
}
.cgp-overlay-pills {
  display: flex; gap: 6px; flex-wrap: wrap; flex: 1; min-width: 0;
}
.cgp-overlay-pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 10px; border-radius: 8px;
  font-size: 1rem; font-weight: 700;
  border: 1px solid rgba(255,255,255,.08);
  background: rgba(255,255,255,.04);
  color: var(--text-primary, #f1f5f9);
  transition: all .15s;
}
.cgp-overlay-pill .cgp-pill-dot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
}
.cgp-overlay-pill .cgp-pill-remove {
  background: none; border: none; color: var(--text-faint); cursor: pointer;
  font-size: .85rem; padding: 0 2px; line-height: 1; opacity: .5;
  transition: opacity .15s;
}
.cgp-overlay-pill .cgp-pill-remove:hover { opacity: 1; color: var(--red, #ef4444); }

/* Add stock button */
.cgp-add-stock-btn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 12px; border-radius: 8px;
  background: rgba(124,58,237,.12); border: 1px dashed rgba(124,58,237,.3);
  color: var(--purple-light, #a78bfa); font-size: 1rem; font-weight: 600;
  cursor: pointer; transition: all .15s; font-family: inherit;
  white-space: nowrap;
}
.cgp-add-stock-btn:hover {
  background: rgba(124,58,237,.22); border-color: rgba(124,58,237,.5);
}

/* Stock picker dropdown */
.cgp-picker-wrap {
  position: relative; display: inline-block;
}
.cgp-picker-dropdown {
  position: absolute; top: 100%; left: 0; z-index: 100;
  width: 260px; max-height: 280px;
  background: var(--surface-panel-modal, #0d0d25);
  border: 1px solid rgba(255,255,255,.1);
  border-radius: 12px; box-shadow: 0 12px 40px rgba(0,0,0,.5);
  display: none; flex-direction: column; overflow: hidden;
  margin-top: 6px;
}
.cgp-picker-dropdown.cgp-picker-open { display: flex; }
.cgp-picker-search {
  width: 100%; padding: 10px 14px; border: none;
  background: rgba(255,255,255,.04); color: var(--text-primary, #f1f5f9);
  font-size: 1rem; font-family: inherit;
  border-bottom: 1px solid rgba(255,255,255,.06);
  outline: none;
}
.cgp-picker-search::placeholder { color: var(--text-faint); }
.cgp-picker-list {
  overflow-y: auto; flex: 1; max-height: 220px;
}
.cgp-picker-list::-webkit-scrollbar { width: 3px; }
.cgp-picker-list::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,.1); border-radius: 99px;
}
.cgp-picker-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 14px; cursor: pointer;
  font-size: 1rem; color: var(--text-soft, rgba(255,255,255,.55));
  transition: background .1s;
}
.cgp-picker-item:hover { background: rgba(124,58,237,.1); }
.cgp-picker-item-sym {
  font-weight: 800; color: var(--text-primary, #f1f5f9);
  min-width: 70px;
}
.cgp-picker-item-name {
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap;
}

/* Grouped bar chart */
.cgp-bar-cluster {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px;
  position: relative; min-width: 0;
}
.cgp-bar-cluster-bars {
  display: flex; gap: 2px; align-items: flex-end; width: 100%;
  justify-content: center;
}
.cgp-bar-cluster-bars .cgp-bar {
  max-width: 28px;
}
.cgp-bar-cluster-bars .cgp-bar .cgp-bar-value {
  font-size: .85rem;
}

/* Legend for overlay */
.cgp-overlay-legend {
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
  padding: 8px 0 0;
}
.cgp-overlay-legend-item {
  display: flex; align-items: center; gap: 4px;
  font-size: 1rem; color: var(--text-soft, rgba(255,255,255,.5));
}
.cgp-overlay-legend-dot {
  width: 8px; height: 8px; border-radius: 50%;
}

/* Forecast popup styling */
.cgp-forecast-popup {
  position: absolute;
  right: 24px;
  bottom: 80px;
  width: min(340px, 90vw);
  background: var(--surface-panel-modal, #0d0d25);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 12px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: none;
  flex-direction: column;
  padding: 14px 16px;
  font-family: inherit;
  color: var(--text-primary, #f1f5f9);
  animation: cgpPopupFadeIn 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}
.cgp-forecast-popup.cgp-popup-open {
  display: flex;
}
@keyframes cgpPopupFadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
.cgp-forecast-popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  padding-bottom: 6px;
}
.cgp-forecast-popup-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--cyan, #06b6d4);
}
.cgp-forecast-popup-close {
  background: none;
  border: none;
  color: var(--text-softer, rgba(255, 255, 255, 0.4));
  cursor: pointer;
  font-size: 0.9rem;
  padding: 2px 6px;
  border-radius: 4px;
  transition: all 0.15s;
}
.cgp-forecast-popup-close:hover {
  color: var(--text-primary, #f1f5f9);
  background: rgba(255, 255, 255, 0.08);
}
.cgp-forecast-popup-body {
  font-size: 0.85rem;
  line-height: 1.5;
}
.cgp-popup-desc {
  margin: 0 0 10px 0;
  color: var(--text-soft, rgba(255, 255, 255, 0.7));
}
.cgp-popup-formula-box {
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 10px;
}
.cgp-formula-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  color: var(--purple-light, #a78bfa);
  margin-bottom: 4px;
  font-weight: 700;
}
.cgp-formula-expr {
  font-family: monospace;
  font-weight: 600;
  color: #fff;
}
.cgp-popup-steps {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}
.cgp-step-item {
  background: rgba(255, 255, 255, 0.02);
  padding: 6px 8px;
  border-radius: 6px;
  border-left: 2px solid rgba(167, 139, 250, 0.5);
}
.cgp-popup-details {
  border-top: 1px dashed rgba(255, 255, 255, 0.06);
  padding-top: 8px;
}
.cgp-popup-details ul {
  margin: 0;
  padding-left: 16px;
  color: var(--text-softer, rgba(255, 255, 255, 0.5));
}
.cgp-popup-details li {
  margin-bottom: 4px;
}

