@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500;600&display=swap');

:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --nav-bg: #0f172a;
  --nav-border: #1e293b;
  --text: #0f172a;
  --text-secondary: #334155;
  --muted: #64748b;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --green: #059669;
  --green-bg: #d1fae5;
  --green-text: #065f46;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --red-text: #991b1b;
  --amber: #d97706;
  --amber-bg: #fef3c7;
  --amber-text: #92400e;
  --blue-bg: #dbeafe;
  --blue-text: #1e40af;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.16);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) {
    --bg: #0b1120;
    --surface: #111827;
    --surface-alt: #1e293b;
    --nav-bg: #070b14;
    --nav-border: #1e293b;
    --text: #f1f5f9;
    --text-secondary: #cbd5e1;
    --muted: #94a3b8;
    --border: #1f2937;
    --border-strong: #334155;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --primary-soft: rgba(59, 130, 246, 0.14);
    --green: #34d399;
    --green-bg: rgba(16, 185, 129, 0.16);
    --green-text: #6ee7b7;
    --red: #f87171;
    --red-bg: rgba(239, 68, 68, 0.16);
    --red-text: #fca5a5;
    --amber: #fbbf24;
    --amber-bg: rgba(245, 158, 11, 0.16);
    --amber-text: #fcd34d;
    --blue-bg: rgba(59, 130, 246, 0.18);
    --blue-text: #93c5fd;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 14px 36px rgba(0, 0, 0, 0.65);
  }
}

html[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-alt: #1e293b;
  --nav-bg: #070b14;
  --nav-border: #1e293b;
  --text: #f1f5f9;
  --text-secondary: #cbd5e1;
  --muted: #94a3b8;
  --border: #1f2937;
  --border-strong: #334155;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --primary-soft: rgba(59, 130, 246, 0.14);
  --green: #34d399;
  --green-bg: rgba(16, 185, 129, 0.16);
  --green-text: #6ee7b7;
  --red: #f87171;
  --red-bg: rgba(239, 68, 68, 0.16);
  --red-text: #fca5a5;
  --amber: #fbbf24;
  --amber-bg: rgba(245, 158, 11, 0.16);
  --amber-text: #fcd34d;
  --blue-bg: rgba(59, 130, 246, 0.18);
  --blue-text: #93c5fd;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 14px 36px rgba(0, 0, 0, 0.65);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

code, pre, .mono {
  font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
}

/* Navigation */
.topbar {
  background: var(--nav-bg);
  border-bottom: 1px solid var(--nav-border);
  color: #f8fafc;
  padding: 0 1.5rem;
  min-height: 58px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  position: sticky;
  top: 0;
  z-index: 50;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: -0.01em;
  color: #fff;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: linear-gradient(135deg, #3b82f6, #10b981);
  display: inline-block;
}

.nav-links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem;
}

.nav-links a {
  color: #94a3b8;
  text-decoration: none;
  padding: 0.45rem 0.8rem;
  border-radius: 7px;
  font-size: 0.85rem;
  font-weight: 500;
  transition: all 0.15s ease;
}

.nav-links a:hover {
  color: #f8fafc;
  background: rgba(255, 255, 255, 0.08);
}

.nav-links a.active {
  background: var(--primary);
  color: #ffffff;
  font-weight: 600;
}

.theme-btn {
  background: rgba(255, 255, 255, 0.08);
  color: #e2e8f0;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 7px;
  padding: 0.36rem 0.72rem;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}
.theme-btn:hover { background: rgba(255, 255, 255, 0.16); }

/* Layout & Cards */
.container {
  max-width: 1580px;
  margin: 1.25rem auto 3rem;
  padding: 0 1.25rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.15rem 1.35rem;
  margin-bottom: 1.1rem;
  box-shadow: var(--shadow-sm);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 0.9rem;
}

.card-title {
  margin: 0;
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.card-sub {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

/* KPI Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 0.85rem;
  margin-bottom: 1.1rem;
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.95rem 1.15rem;
  box-shadow: var(--shadow-sm);
  position: relative;
  overflow: hidden;
}

.kpi::before {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: var(--primary);
  opacity: 0.8;
}

.kpi-label {
  font-size: 0.73rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.kpi-val {
  font-size: 1.28rem;
  font-weight: 700;
  margin-top: 0.25rem;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.kpi-sub {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.15rem;
  font-variant-numeric: tabular-nums;
}

/* Tables */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
}

th, td {
  padding: 0.58rem 0.7rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}

th {
  background: var(--surface-alt);
  font-weight: 600;
  font-size: 0.74rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  position: sticky;
  top: 0;
  z-index: 2;
}

tbody tr:hover td { background: var(--primary-soft); }
tbody tr:last-child td { border-bottom: none; }

.sticky-col-1 {
  position: sticky;
  left: 0;
  z-index: 3;
  background: var(--surface);
  border-right: 1px solid var(--border);
}
th.sticky-col-1 {
  z-index: 4;
  background: var(--surface-alt);
}

.num { text-align: right; font-variant-numeric: tabular-nums; }
.pos { color: var(--green); font-weight: 600; }
.neg { color: var(--red); font-weight: 600; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Buttons & Inputs */
.btn {
  background: var(--primary);
  color: #ffffff;
  border: 1px solid transparent;
  padding: 0.48rem 0.95rem;
  border-radius: 7px;
  cursor: pointer;
  font-size: 0.84rem;
  font-weight: 600;
  font-family: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  transition: all 0.15s ease;
}
.btn:hover { background: var(--primary-hover); text-decoration: none; }

.btn-secondary, .btn-sec {
  background: var(--surface-alt);
  color: var(--text);
  border: 1px solid var(--border-strong);
}
.btn-secondary:hover, .btn-sec:hover { background: var(--border); }

.btn-sm { padding: 0.3rem 0.65rem; font-size: 0.78rem; }
.btn-xs { padding: 0.2rem 0.5rem; font-size: 0.74rem; }

input, select, textarea {
  background: var(--surface);
  color: var(--text);
  padding: 0.46rem 0.65rem;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  font-size: 0.85rem;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.form-row { display: flex; flex-wrap: wrap; gap: 0.7rem; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 0.25rem; }
.field label { font-size: 0.76rem; color: var(--muted); font-weight: 600; }

/* Alerts & Badges */
.alert-ok {
  background: var(--green-bg);
  border: 1px solid var(--green);
  color: var(--green-text);
  padding: 0.7rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-weight: 500;
}
.alert-err {
  background: var(--red-bg);
  border: 1px solid var(--red);
  color: var(--red-text);
  padding: 0.7rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-weight: 500;
}

.badge {
  padding: 0.18rem 0.55rem;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 600;
  background: var(--surface-alt);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}
.badge-green { background: var(--green-bg); color: var(--green-text); border-color: transparent; }
.badge-blue { background: var(--blue-bg); color: var(--blue-text); border-color: transparent; }
.badge-amber { background: var(--amber-bg); color: var(--amber-text); border-color: transparent; }
.badge-red { background: var(--red-bg); color: var(--red-text); border-color: transparent; }

.hover-pill {
  cursor: help;
  border-bottom: 1px dashed var(--border-strong);
  padding-bottom: 1px;
}

.pulse-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
  animation: pulse 1.4s infinite ease-in-out;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.8); }
}

/* Global Unclipped Floating Popover */
.floating-popover {
  position: fixed;
  z-index: 9999;
  display: none;
  max-width: 440px;
  min-width: 260px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 0.85rem 1rem;
  box-shadow: var(--shadow-lg);
  font-size: 0.82rem;
  line-height: 1.45;
  pointer-events: none;
}
.floating-popover h4 {
  margin: 0 0 0.45rem 0;
  font-size: 0.84rem;
  color: var(--primary);
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.3rem;
}
.pop-row {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.2rem 0;
  border-bottom: 1px dashed var(--border);
}
.pop-row:last-child { border-bottom: none; }

/* Sub-Tabs */
.subtab-bar {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.75rem;
  margin-bottom: 1rem;
}
.subtab-btn {
  background: var(--surface-alt);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  padding: 0.48rem 0.95rem;
  border-radius: 8px;
  font-size: 0.84rem;
  font-weight: 600;
  cursor: pointer;
}
.subtab-btn.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* Dossier Reader + Jump TOC */
.dossier-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 1.1rem;
  align-items: start;
}
.toc-panel {
  position: sticky;
  top: 72px;
  max-height: 75vh;
  overflow-y: auto;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
}
.toc-panel a {
  display: block;
  font-size: 0.78rem;
  color: var(--text-secondary);
  padding: 0.32rem 0.45rem;
  border-radius: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.toc-panel a:hover {
  background: var(--primary-soft);
  color: var(--primary);
  text-decoration: none;
}

.md-body {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.5rem 1.85rem;
  max-height: 76vh;
  overflow-y: auto;
  font-size: 0.92rem;
  line-height: 1.7;
  color: var(--text);
}
.md-body h1 { font-size: 1.38rem; border-bottom: 2px solid var(--border); padding-bottom: 0.4rem; margin-top: 0; }
.md-body h2 { font-size: 1.15rem; border-bottom: 1px solid var(--border); padding-bottom: 0.3rem; margin-top: 1.6rem; color: var(--primary); }
.md-body h3 { font-size: 1.02rem; margin-top: 1.25rem; }
.md-body p { margin: 0.65rem 0; }
.md-body ul, .md-body ol { padding-left: 1.4rem; margin: 0.55rem 0; }
.md-body li { margin-bottom: 0.32rem; }
.md-body table {
  display: block;
  overflow-x: auto;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 7px;
  margin: 1rem 0;
}
.md-body blockquote {
  margin: 0.85rem 0;
  padding: 0.6rem 1rem;
  border-left: 3px solid var(--primary);
  background: var(--primary-soft);
  border-radius: 0 7px 7px 0;
}
.md-body hr { border: none; border-top: 1px solid var(--border); margin: 1.3rem 0; }

.prose-raw {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  white-space: pre-wrap;
  max-height: 76vh;
  overflow-y: auto;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.82rem;
}

/* Chat */
.chat-msg {
  padding: 0.85rem 1.05rem;
  border-radius: 10px;
  margin-bottom: 0.75rem;
  border: 1px solid var(--border);
}
.chat-user { background: var(--primary-soft); border-color: rgba(37, 99, 235, 0.25); }
.chat-assistant { background: var(--surface-alt); }
.chat-content p:first-child { margin-top: 0; }
.chat-content p:last-child { margin-bottom: 0; }
.chat-content table {
  display: block;
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  margin: 0.6rem 0;
}

@media (max-width: 960px) {
  .dossier-layout, .chat-grid, .charts-grid { grid-template-columns: 1fr !important; }
  .toc-panel { position: static; max-height: 220px; }
}

/* Rich Markdown Typography for Chat Responses */
.chat-msg {
  padding: 1rem 1.2rem;
  border-radius: 10px;
  margin-bottom: 0.9rem;
  border: 1px solid var(--border);
  line-height: 1.68;
  font-size: 0.92rem;
}
.chat-content h1, .chat-content h2 {
  font-size: 1.08rem;
  margin: 1.15rem 0 0.45rem;
  padding-bottom: 0.28rem;
  border-bottom: 1px solid var(--border);
  color: var(--primary);
}
.chat-content h3, .chat-content h4 {
  font-size: 0.96rem;
  margin: 0.95rem 0 0.35rem;
  color: var(--text);
}
.chat-content h1:first-child, .chat-content h2:first-child, .chat-content h3:first-child {
  margin-top: 0;
}
.chat-content p { margin: 0.55rem 0; }
.chat-content ul, .chat-content ol {
  padding-left: 1.45rem;
  margin: 0.5rem 0;
}
.chat-content li { margin-bottom: 0.35rem; }
.chat-content blockquote {
  margin: 0.75rem 0;
  padding: 0.55rem 0.95rem;
  border-left: 3px solid var(--primary);
  background: var(--primary-soft);
  border-radius: 0 7px 7px 0;
}
.chat-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1rem 0;
}
.chat-content table {
  width: 100%;
  display: block;
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 0.8rem 0;
  font-size: 0.84rem;
}
.chat-content th, .chat-content td {
  white-space: normal;
  min-width: 90px;
  padding: 0.5rem 0.65rem;
  vertical-align: top;
}


/* MOBILE PWA RESPONSIVE */
.user-bar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.82rem;
  color: #d1d5db;
}
.logout-link {
  color: #fca5a5;
  text-decoration: none;
  border: 1px solid rgba(252, 165, 165, 0.35);
  padding: 0.25rem 0.55rem;
  border-radius: 6px;
}
.logout-link:hover { background: rgba(185, 28, 28, 0.35); color: #fff; }
@media (max-width: 768px) {
  .topbar { padding: 0.65rem 0.85rem; gap: 0.5rem; }
  .brand { font-size: 0.96rem; }
  .nav-links {
    width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.2rem;
  }
  .nav-links a { white-space: nowrap; font-size: 0.84rem; padding: 0.38rem 0.65rem; }
  .container { margin: 0.75rem auto; padding: 0 0.65rem; }
  .card { padding: 0.85rem; }
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.55rem; }
  .kpi-val { font-size: 1.05rem; }
  input, select, textarea, .btn { font-size: 16px; }
}


/* MOBILE HEADER & DOSSIER POLISH */
.topbar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}
@media (max-width: 768px) {
  .topbar-top { width: 100%; }
  .user-email { display: none; }
  .md-body { padding: 1rem 1.05rem; max-height: none; font-size: 0.9rem; }
  .charts-grid > .card > div[style*="height:295px"] { height: 240px !important; }
}


/* MOBILE DOSSIER CONTAINMENT & READABLE TABLES */
.dossier-layout > * {
  min-width: 0;
  max-width: 100%;
}
.md-body {
  min-width: 0;
  max-width: 100%;
  overflow-x: hidden;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.md-body pre, .prose-raw {
  max-width: 100%;
  overflow-x: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.md-table-scroll {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 0.9rem 0;
  background: var(--surface-alt);
}
.md-table-scroll table {
  display: table;
  width: 100%;
  margin: 0;
  border: none;
  border-radius: 0;
}
.md-body th, .md-body td {
  white-space: normal;
  min-width: 95px;
  max-width: 260px;
  vertical-align: top;
  line-height: 1.45;
  overflow-wrap: break-word;
  word-break: normal;
}

@media (max-width: 960px) {
  .dossier-layout {
    grid-template-columns: minmax(0, 1fr) !important;
  }
}

@media (max-width: 768px) {
  .container {
    overflow-x: hidden;
  }
  .toc-panel {
    max-height: 155px;
    padding: 0.55rem 0.65rem;
  }
  .md-body {
    padding: 0.85rem 0.9rem;
    font-size: 0.92rem;
    line-height: 1.65;
  }
  .md-body h1 { font-size: 1.2rem; }
  .md-body h2 { font-size: 1.05rem; margin-top: 1.25rem; }
  .md-body h3 { font-size: 0.96rem; }
  .md-body th, .md-body td {
    padding: 0.45rem 0.55rem;
    font-size: 0.8rem;
    min-width: 85px;
    max-width: 210px;
  }
}

/* MOBILE PORTFOLIO CHAT CONTAINMENT */
.chat-grid > * {
  min-width: 0;
  max-width: 100%;
}
.chat-msg, .chat-content {
  min-width: 0;
  max-width: 100%;
  overflow-x: hidden;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.chat-content pre, .chat-content code {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-width: 100%;
}
.chat-content .md-table-scroll table {
  display: table;
  width: 100%;
  margin: 0;
  border: none;
  border-radius: 0;
}
.chat-content th, .chat-content td {
  white-space: normal;
  min-width: 82px;
  max-width: 220px;
  overflow-wrap: break-word;
  word-break: normal;
}
@media (max-width: 960px) {
  .chat-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }
  .chat-grid > .card {
    position: static !important;
    top: auto !important;
  }
}
@media (max-width: 768px) {
  .chat-msg {
    padding: 0.75rem 0.85rem;
    font-size: 0.9rem;
  }
  #portfolioChatScroll {
    max-height: 62vh !important;
  }
}

/* ANALYST DISCUSSION MOBILE & TABLE POLISH */
.chat-msg, .chat-content {
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
}
.chat-content table {
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}
@media (max-width: 768px) {
  #tickerChatScroll {
    max-height: none !important;
    overflow-y: visible !important;
  }
  .chat-msg {
    padding: 0.85rem 0.9rem;
    font-size: 0.9rem;
  }
  .chat-msg > div:first-child {
    flex-wrap: wrap;
    gap: 0.4rem;
  }
}
