:root {
  --accent: #24a9a7;
  --accent-soft: rgba(36, 169, 167, 0.12);
  --bg: #ffffff;
  --bg-soft: #f7f8f9;
  --border: #e6e8eb;
  --text: #1a1f24;
  --text-muted: #6b7480;
  --text-faint: #9aa3ad;
  --price-line: #1a1f24;
  --negative: #d24b4b;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
  --radius: 6px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Roboto', -apple-system, BlinkMacSystemFont, sans-serif;
  font-weight: 400;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

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

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 30;
}
.topbar .brand {
  font-weight: 500;
  font-size: 15px;
  letter-spacing: -0.01em;
  color: var(--text);
  min-width: 220px;
}
.topbar .brand .dot {
  display: inline-block;
  width: 8px; height: 8px;
  background: var(--accent);
  border-radius: 50%;
  margin-right: 10px;
  vertical-align: middle;
}
.topbar .nav-search {
  flex: 1 1 360px;
  max-width: 480px;
}
.search.compact input {
  padding: 7px 12px;
  font-size: 13px;
  background: var(--bg-soft);
  border-color: transparent;
}
.search.compact input:focus {
  background: var(--bg);
  border-color: var(--accent);
}

.ticker-chip {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 5px 6px 5px 12px;
  width: 100%;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  transition: background 0.15s;
}
.ticker-chip:hover { background: rgba(36, 169, 167, 0.18); }
.ticker-chip-icon {
  color: var(--accent);
  font-size: 8px;
  line-height: 1;
}
.ticker-chip-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
  flex: 1 1 auto;
  min-width: 0;
}
.ticker-chip-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ticker-chip-meta {
  font-size: 11px;
  color: var(--text-muted);
  font-family: 'Roboto Mono', monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ticker-chip-action {
  font-size: 11px;
  color: var(--accent);
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 0 6px;
  opacity: 0;
  transition: opacity 0.15s;
}
.ticker-chip:hover .ticker-chip-action { opacity: 1; }
.ticker-chip-x {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 16px;
  line-height: 1;
  transition: background 0.15s, color 0.15s;
}
.ticker-chip-x:hover {
  background: rgba(0,0,0,0.08);
  color: var(--text);
}

.layout {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  min-height: calc(100vh - 49px);
}

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--bg);
  padding: 20px 12px;
}
.sidebar-label {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-faint);
  padding: 0 12px 8px;
}
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius);
  color: var(--text);
  text-decoration: none;
  border-left: 2px solid transparent;
  margin-left: -2px;
}
.sidebar-item:hover { background: var(--bg-soft); text-decoration: none; }
.sidebar-item.active {
  background: var(--accent-soft);
  border-left-color: var(--accent);
}
.sidebar-item.active .sidebar-num { color: var(--accent); }
.sidebar-num {
  font-family: 'Roboto Mono', monospace;
  font-size: 11px;
  color: var(--text-faint);
  font-weight: 500;
}
.sidebar-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.sidebar-title { font-size: 13px; font-weight: 500; color: var(--text); }
.sidebar-item.active .sidebar-title { color: var(--text); }
.sidebar-sub { font-size: 11px; color: var(--text-muted); }

.sidebar-coming-soon {
  margin-top: 16px;
  padding: 10px 14px;
  border-top: 1px dashed var(--border);
  font-size: 11px;
  color: var(--text-faint);
  font-style: italic;
}

main {
  padding: 32px;
  max-width: 1200px;
  width: 100%;
}

/* Hamburger button — hidden on desktop, shown on mobile */
.hamburger {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  width: 36px;
  height: 36px;
  padding: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
}
.hamburger span {
  display: block;
  width: 18px;
  height: 2px;
  margin: 0 auto;
  background: var(--text);
  border-radius: 1px;
}
.hamburger:hover { background: var(--bg-soft); opacity: 1; }

h1 { font-weight: 500; font-size: 22px; margin: 0 0 4px; letter-spacing: -0.01em; }
.study-header { margin-bottom: 20px; }
.study-header h1 { margin-bottom: 8px; }
.ticker-meta { margin-bottom: 8px; }
.ticker-meta:empty { display: none; }
.study-desc { margin: 4px 0 0; }
h2 { font-weight: 500; font-size: 16px; margin: 0 0 12px; }
.muted { color: var(--text-muted); font-size: 13px; }

.card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 16px; }

.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.row > * { flex: 0 0 auto; }
.grow { flex: 1 1 auto; min-width: 0; }

label {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
  font-weight: 500;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

input[type="text"], input[type="search"], select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  outline: none;
  transition: border-color 0.15s;
}
input:focus, select:focus { border-color: var(--accent); }

button, .btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.15s;
}
button:hover, .btn:hover { opacity: 0.9; text-decoration: none; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}

.search {
  position: relative;
  width: 100%;
}
.search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-height: 360px;
  overflow-y: auto;
  z-index: 10;
  display: none;
}
.search-results.open { display: block; }
.search-result {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.search-result:last-child { border-bottom: 0; }
.search-result:hover, .search-result.active { background: var(--bg-soft); }
.search-result .name { font-weight: 500; color: var(--text); }
.search-result .ticker { color: var(--text-muted); font-size: 12px; font-family: 'Roboto Mono', monospace; }

.studies-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  margin-top: 24px;
}
.study-card {
  display: block;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  transition: border-color 0.15s, transform 0.15s;
  color: var(--text);
}
.study-card:hover {
  border-color: var(--accent);
  text-decoration: none;
  transform: translateY(-1px);
}
.study-card .num {
  font-size: 11px;
  font-weight: 500;
  color: var(--accent);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 8px;
}
.study-card .title { font-weight: 500; font-size: 15px; margin-bottom: 4px; }
.study-card .desc { color: var(--text-muted); font-size: 13px; line-height: 1.5; }

.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}
.chip .x { cursor: pointer; opacity: 0.7; }
.chip .x:hover { opacity: 1; }

.chart-wrap {
  position: relative;
  height: 480px;
}

.meta {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  font-size: 13px;
  color: var(--text-muted);
}
.meta b { color: var(--text); font-weight: 500; }

.spinner {
  display: inline-block;
  width: 14px; height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty {
  text-align: center;
  padding: 64px 24px;
  color: var(--text-muted);
}

.error {
  background: #fff3f3;
  border: 1px solid #f5cccc;
  color: #b03030;
  padding: 12px 14px;
  border-radius: var(--radius);
  font-size: 13px;
}

.field-hint {
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 6px;
  line-height: 1.4;
}

/* Keyword chip input — stack chips below the input so they wrap within the field */
.kw-input {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}
.kw-input .chip-row { max-width: 100%; }

/* Brand-term suggester */
.brand-suggest {
  margin-top: 10px;
  padding: 10px 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-width: 100%;
  min-width: 0;
}
.brand-suggest-input {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 13px;
  padding: 7px 10px;
}
.brand-suggest-results {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
  max-height: 180px;
  overflow-y: auto;
}
.brand-suggest-results:empty { display: none; }
.brand-term-chip {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.brand-term-chip:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  opacity: 1;
}
.brand-term-chip.picked,
.brand-term-chip.picked:hover {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  opacity: 1;
}
.brand-suggest-status {
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 8px;
  min-height: 14px;
}

.toolbar { display: flex; gap: 12px; align-items: flex-start; flex-wrap: wrap; }
.toolbar .field { min-width: 200px; display: flex; flex-direction: column; }
.toolbar .field.wide { flex: 1 1 320px; }
.toolbar .field.run-field {
  margin-left: auto;
  min-width: 0;
  /* Push the Run button to align with the bottom of the inputs (skip the label row) */
  padding-top: 22px;
}
.toolbar .field.run-field label { display: none; }
.toolbar .field-hint { margin-top: 6px; }

.run-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 18px;
  background: linear-gradient(135deg, var(--accent) 0%, #1e8e8c 100%);
  color: #fff;
  border: 0;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.01em;
  cursor: pointer;
  box-shadow:
    0 1px 2px rgba(36, 169, 167, 0.2),
    0 4px 12px rgba(36, 169, 167, 0.18);
  transition: transform 0.15s, box-shadow 0.15s, filter 0.15s;
}
.run-btn:hover {
  opacity: 1;
  transform: translateY(-1px);
  filter: brightness(1.05);
  box-shadow:
    0 2px 4px rgba(36, 169, 167, 0.25),
    0 6px 18px rgba(36, 169, 167, 0.25);
}
.run-btn:active {
  transform: translateY(0);
  filter: brightness(0.97);
}
.run-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  filter: none;
}
.run-btn .sparkle {
  width: 14px;
  height: 14px;
  display: inline-block;
  flex-shrink: 0;
  animation: sparkle-pulse 2.4s ease-in-out infinite;
}
@keyframes sparkle-pulse {
  0%, 100% { transform: scale(1) rotate(0deg); opacity: 0.95; }
  50%      { transform: scale(1.18) rotate(15deg); opacity: 1; }
}

.range-group {
  display: inline-flex;
  align-self: flex-start;
  width: fit-content;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.range-group button {
  background: var(--bg);
  color: var(--text-muted);
  border: 0;
  border-right: 1px solid var(--border);
  border-radius: 0;
  padding: 7px 14px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
}
.range-group button:last-child { border-right: 0; }
.range-group button.active {
  background: var(--accent);
  color: #fff;
}
.range-group button:hover:not(.active) { background: var(--bg-soft); color: var(--text); }

.no-ticker {
  text-align: center;
  padding: 80px 24px;
  color: var(--text-muted);
}
.no-ticker .hint { margin-top: 8px; font-size: 13px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
}
.icon-btn:hover {
  color: var(--text);
  border-color: var(--text-faint);
  background: var(--bg-soft);
}
.icon-btn[disabled] { opacity: 0.4; cursor: not-allowed; }

.export-actions {
  display: flex;
  gap: 8px;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #1a1f24;
  color: #fff;
  padding: 9px 16px;
  border-radius: 6px;
  font-size: 13px;
  z-index: 50;
  opacity: 0;
  transition: opacity .15s;
  pointer-events: none;
}
.toast.show { opacity: 1; }

.warnings {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.warning-item {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  padding: 4px 2px;
  font-size: 11px;
  color: var(--text-faint);
  line-height: 1.4;
}
.warning-item .warning-icon {
  flex: 0 0 auto;
  color: var(--text-faint);
  font-size: 11px;
  line-height: 1;
  margin-top: 1px;
}
.warning-item b { color: var(--text-muted); font-weight: 500; }

/* ---------------- Top domains list (Study 8) ---------------- */
.top-domains-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 4px 16px;
}
.top-domain-row {
  display: grid;
  grid-template-columns: 36px minmax(80px, 1fr) auto;
  gap: 10px;
  align-items: baseline;
  padding: 6px 8px;
  background: transparent;
  border: 0;
  border-radius: var(--radius);
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  transition: background 0.12s;
}
.top-domain-row:hover {
  background: var(--bg-soft);
  opacity: 1;
}
.top-domain-rank {
  font-family: 'Roboto Mono', monospace;
  font-size: 11px;
  color: var(--text-faint);
  font-weight: 500;
}
.top-domain-name {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.top-domain-cats {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------------- Artifacts feed ---------------- */
.artifacts-toolbar {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.artifacts-list {
  display: flex;
  flex-direction: column;
}
.artifact-row {
  display: grid;
  grid-template-columns: 80px minmax(160px, 1.4fr) minmax(180px, 1fr) minmax(0, 2.4fr);
  gap: 16px;
  align-items: baseline;
  padding: 12px 8px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  text-decoration: none;
  transition: background 0.12s;
}
.artifact-row:last-child { border-bottom: 0; }
.artifact-row:hover {
  background: var(--bg-soft);
  text-decoration: none;
}
.art-time {
  font-size: 11px;
  color: var(--text-faint);
  font-family: 'Roboto Mono', monospace;
  white-space: nowrap;
}
.art-company { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.art-name {
  font-weight: 500;
  color: var(--text);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.art-tk {
  font-size: 11px;
  color: var(--text-muted);
  font-family: 'Roboto Mono', monospace;
}
.art-study {
  font-size: 12px;
  color: var(--accent);
  font-weight: 500;
}
.art-params {
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.art-kw {
  display: inline-block;
  padding: 1px 8px;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 10px;
  font-size: 11px;
  margin-left: 4px;
}

@media (max-width: 760px) {
  .artifact-row {
    grid-template-columns: 1fr;
    gap: 4px;
    padding: 14px 6px;
  }
  .art-time { order: -1; }
}

/* ---------------------------------------------------------------- */
/* Responsive                                                       */
/* ---------------------------------------------------------------- */

/* Tablet & narrow desktop: tighten sidebar but keep it visible */
@media (max-width: 1024px) {
  .layout { grid-template-columns: 200px minmax(0, 1fr); }
  main { padding: 24px; }
  .topbar .brand { min-width: auto; }
}

/* Mobile breakpoint: sidebar becomes a slide-in drawer */
@media (max-width: 760px) {
  .topbar {
    padding: 10px 14px;
    gap: 12px;
    flex-wrap: nowrap;
  }
  .hamburger { display: inline-flex; flex-shrink: 0; }
  .topbar .brand { display: none; }
  .topbar .nav-search {
    flex: 1 1 auto;
    min-width: 0;
    max-width: none;
  }

  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 53px; /* topbar height */
    left: 0;
    bottom: 0;
    width: 240px;
    z-index: 25;
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    overflow-y: auto;
  }
  .sidebar.open { transform: translateX(0); }

  main {
    padding: 16px;
    max-width: 100%;
  }

  /* Toolbar fields go full width on mobile so each control is tappable */
  .toolbar { gap: 14px; }
  .toolbar .field,
  .toolbar .field.wide {
    min-width: 0;
    width: 100%;
    flex: 1 1 100%;
  }
  .toolbar .field button#run-btn { width: 100%; justify-content: center; }

  /* Wiki page input + chips wrap properly */
  .wiki-input .row { flex-direction: column; align-items: stretch !important; }
  .wiki-input .search { flex: 1 1 100% !important; }

  /* Cards and headers */
  .card { padding: 16px; }
  h1 { font-size: 18px; }
  .study-desc { font-size: 12px; }

  .chart-wrap { height: 360px; }

  /* Chart card header (status + export buttons) wraps cleanly */
  .export-actions { flex-wrap: wrap; }

  /* Studies grid */
  .studies-grid { grid-template-columns: 1fr; gap: 12px; }
}

/* Very narrow phones — extra tightening */
@media (max-width: 420px) {
  .chart-wrap { height: 320px; }
  .ticker-chip-action { display: none; } /* "change" hint hidden, X still works */
}
