/* ===== AI Chart — Kronos forecast via lightweight-charts ==================
   View-only page (paid). The chart canvas colours come from chartOptions() in
   chart.js (light/dark presets); these rules cover the container + the HTML
   overlays (whale-level price labels and external-signal markers) and the
   Admin → Chart form. Dark mode follows the app's html[data-theme="dark"]. */

[data-tab="aichart"] .aichart-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.aichart-caption {
  color: var(--muted);
  font-size: .85rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  min-height: 1.2em;
  margin: 6px 0 10px;
}

.aichart-status {
  font-size: .85rem;
  margin: 0 0 10px;
  min-height: 1.1em;
}

.aichart-status.info {
  color: var(--muted);
}

.aichart-status.success {
  color: var(--primary);
}

.aichart-status.error {
  color: var(--error);
}

/* ----- Timeframe switcher (above the chart) ----- */
.aichart-toolbar {
  display: flex;
  align-items: center;
  gap: 8px 4px;
  flex-wrap: wrap;
  margin: 0 0 10px;
}

.aichart-toolbar .tab-group {
  display: flex;
  gap: 4px;
}

/* Tabs read as plain labels; only the active one looks like a button. */
.aichart-toolbar .chart-tab {
  padding: 4px 12px;
  font: 600 .85rem/1 inherit;
  color: var(--muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
}

.aichart-toolbar .chart-tab:hover {
  color: var(--text);
}

.aichart-toolbar .chart-tab.active {
  color: #fff;
  background: var(--primary);
  border-color: var(--primary);
}

/* ----- Moving-average toggles (after the timeframes, small gap + divider) ----- */
.aichart-toolbar .ma-group {
  display: flex;
  gap: 4px;
  margin-left: 12px;
  padding-left: 12px;
  border-left: 1px solid var(--border);
}

.ma-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  font: 600 .8rem/1 inherit;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: color .15s, background .15s, border-color .15s;
}

.ma-toggle:hover {
  color: var(--text);
}

/* A little line swatch whose thickness mirrors each MA's weight on the chart. */
.ma-toggle::before {
  content: '';
  width: 14px;
  border-top-style: solid;
  border-radius: 2px;
}

.ma-toggle.ma50::before {
  border-top-width: 2px;
  border-top-color: #4CAF50;
}

.ma-toggle.ma100::before {
  border-top-width: 3px;
  border-top-color: #F9A825;
}

.ma-toggle.ma200::before {
  border-top-width: 4px;
  border-top-color: #E53935;
}

.ma-toggle.active {
  color: var(--text);
  background: var(--surface-2);
  border-color: var(--primary);
}

.aichart-card {
  padding: 14px;
}

/* ----- Layout: chart + collapsible right-side watchlist (TradingView-style) ----- */
.aichart-layout {
  display: flex;
  gap: 14px;
  align-items: stretch;
}

.aichart-main {
  flex: 1 1 auto;
  min-width: 0;
}

.aichart-watchlist {
  flex: 0 0 21%;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  overflow: hidden;
  max-height: min(64vh, 600px);
}

/* Collapsed: the list folds away; the toolbar toggle brings it back. */
.aichart-layout.watch-collapsed .aichart-watchlist {
  display: none;
}

.watchlist-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  font-size: .8rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
  border-bottom: 1px solid var(--border);
}

.watchlist-body {
  overflow-y: auto;
  padding: 6px;
  flex: 0 1 auto;
  max-height: 45%;
}

.watchlist-group-label {
  padding: 8px 8px 4px;
  font-size: .7rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .05em;
}

.watchlist-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  padding: 7px 9px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font: inherit;
}

.watchlist-item:hover {
  background: var(--surface);
}

.watchlist-item.active {
  background: var(--surface);
  border-color: var(--primary);
}

.watchlist-item .wl-name {
  font-weight: 600;
  font-size: .9rem;
  line-height: 1.1;
}

.watchlist-item .wl-symbol {
  font-size: .72rem;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, monospace;
}

.watchlist-item .wl-left {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.watchlist-item .wl-lock {
  color: var(--muted);
  font-size: .9rem;
  flex: none;
}

/* Locked instruments: visible but muted, with a lock; click prompts an upgrade. */
.watchlist-item.locked {
  cursor: not-allowed;
  opacity: .55;
}

.watchlist-item.locked:hover {
  background: transparent;
}

.watchlist-item.locked .wl-name {
  color: var(--muted);
}

.aichart-watch-toggle {
  display: none;
  /* only shown (via .show) when the list is collapsed */
  margin-left: auto;
  /* park it at the right end of the toolbar */
  align-items: center;
  padding: 2px 6px;
  font-size: 1.05rem;
  color: var(--muted);
  background: transparent;
  border: none;
  cursor: pointer;
  line-height: 1;
}

.aichart-watch-toggle:hover {
  color: var(--text);
}

.aichart-watch-toggle.show {
  display: inline-flex;
}

/* ----- Signals block (collapsible section under the instruments list) ----- */
.signals-head {
  border-top: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
}

.signals-head .sig-caret {
  font-size: .9rem;
  transition: transform .15s;
}

.aichart-watchlist.signals-collapsed .sig-caret {
  transform: rotate(-90deg);
}

.aichart-watchlist.signals-collapsed .signals-body {
  display: none;
}

.signals-body {
  overflow-y: auto;
  padding: 6px;
  flex: 1 1 auto;
  min-height: 80px;
}

.signal-row {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 6px 8px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font: inherit;
}

.signal-row:hover {
  background: var(--surface);
}

.signal-row .sig-dir {
  flex: none;
  width: 14px;
  text-align: center;
  font-size: .8rem;
  line-height: 1;
}

.signal-row.long .sig-dir {
  color: #26A69A;
}

.signal-row.short .sig-dir {
  color: #EF5350;
}

.signal-row .sig-mid {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  flex: 1 1 auto;
}

.signal-row .sig-name {
  font-weight: 600;
  font-size: .85rem;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.signal-row .sig-meta {
  font-size: .7rem;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, monospace;
}

.signal-row .sig-when {
  flex: none;
  font-size: .7rem;
  color: var(--muted);
}

@media (max-width: 720px) {
  .aichart-layout {
    flex-direction: column;
  }

  /* Let the panel grow instead of squeezing both lists into 220px: cap the
     instruments list and give the signals block a roomy height. It's
     collapsible, so the extra height costs nothing when folded away. */
  .aichart-watchlist {
    flex-basis: auto;
    max-height: none;
    order: -1;
  }

  .watchlist-body {
    max-height: 210px;
  }

  .signals-body {
    min-height: 240px;
    max-height: 60vh;
  }
}

#aiChart {
  position: relative;
  width: 100%;
  height: min(64vh, 600px);
  min-height: 380px;
}

#level-labels,
#signal-labels {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

#level-labels {
  z-index: 5;
}

#signal-labels {
  z-index: 6;
}

.level-label {
  position: absolute;
  font: 600 10px/1 ui-monospace, SFMono-Regular, monospace;
  white-space: nowrap;
  transform: translate(-100%, -118%);
  padding: 0 3px;
  border-radius: 3px;
  background: rgba(255, 255, 255, .8);
}

html[data-theme="dark"] .level-label {
  background: rgba(24, 34, 56, .82);
}

/* External-signal labels (TV-style label-up / label-down), placed by the rAF loop. */
.signal-label {
  position: absolute;
  font: 600 9px/1.18 ui-monospace, monospace;
  text-align: center;
  white-space: nowrap;
  padding: 2px 4px;
  border-radius: 3px;
  color: #fff;
}

.signal-label::after {
  content: '';
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
}

/* LONG: green box BELOW the bar, pointer up. */
.signal-label.long {
  background: #26A69A;
  transform: translate(-50%, 9px);
}

.signal-label.long::after {
  top: -9px;
  border-bottom-color: #26A69A;
}

/* SHORT: red box ABOVE the bar, pointer down. */
.signal-label.short {
  background: #EF5350;
  transform: translate(-50%, calc(-100% - 9px));
}

.signal-label.short::after {
  bottom: -9px;
  border-top-color: #EF5350;
}

.aichart-loading {
  display: none;
  color: var(--muted);
  font-size: .85rem;
}

.aichart-loading.show {
  display: inline;
}

/* Corridor badge — the same purple as the corridor band on the chart. */
.aichart-direction {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
  margin: 6px 0;
  /* breathing room above/below when the head wraps on mobile */
  padding: 2px 10px;
  border-radius: 999px;
  font-size: .8rem;
  font-weight: 700;
  letter-spacing: .02em;
  color: #fff;
  background: var(--muted);
}

.aichart-direction.corridor {
  background: #7E57C2;
  /* A pill shape only works on one line; the corridor wraps on phones. */
  border-radius: 12px;
}

.aichart-direction .cor-seg {
  white-space: nowrap;
}

.aichart-direction .dir-detail {
  font-weight: 400;
  opacity: .9;
}

/* Multi-timeframe corridor strip (1h/4h/1d corridor widths side by side). */
.aichart-dirmatrix {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin: 20px 0;
}

.dirm-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 10px;
  border: 1px solid transparent;
  border-radius: 999px;
  font-size: .78rem;
  font-weight: 700;
  letter-spacing: .02em;
  color: #fff;
  cursor: pointer;
  font-family: inherit;
}

.dirm-pill.corridor {
  background: #7E57C2;
}

.dirm-pill.none {
  background: transparent;
  border-color: var(--border);
  color: var(--muted);
  font-weight: 500;
  cursor: default;
}

.dirm-pill.active {
  box-shadow: 0 0 0 2px var(--text);
}

.aichart-empty {
  color: var(--muted);
  padding: 40px 16px;
  text-align: center;
}

/* ----- Admin → Chart settings form ----- */
.chart-admin {
  max-width: 640px;
}

.chart-admin .field {
  margin-bottom: 14px;
}

.chart-admin select,
.chart-admin input[type="text"],
.chart-admin input[type="number"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
}

/* Calibration report block */
.chart-admin .cal-days {
  margin-bottom: 0;
}

.chart-admin .cal-days select {
  width: auto;
  padding: 6px 10px;
}

.chart-admin .orders-table td.pos {
  color: #18a957;
  font-weight: 600;
}

.chart-admin .orders-table td.neg {
  color: var(--error);
  font-weight: 600;
}

/* Instrument catalog table — compact inline-editable rows. */
.instr-table {
  width: 100%;
  min-width: 560px;
}

.instr-table th {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.instr-table td {
  padding: 6px 8px;
  vertical-align: middle;
}

.instr-table td.ci-on {
  text-align: center;
  width: 38px;
}

.instr-table td.ci-act {
  white-space: nowrap;
  width: 1%;
}

.instr-table input[type="text"],
.instr-table select {
  width: 100%;
  min-width: 70px;
  padding: 5px 7px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: .85rem;
}

.instr-table .ci-name {
  min-width: 90px;
}

.instr-table .ci-feature {
  min-width: 100px;
}

.instr-table .ci-warm {
  margin-right: 4px;
}

.chart-admin .row2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.chart-admin .switch {
  display: flex;
  margin-bottom: 12px;
}

.chart-admin .actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 6px;
}

.chart-admin .muted-status {
  font-size: .85rem;
  color: var(--muted);
  margin-top: 10px;
  min-height: 1.1em;
}

.chart-admin hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 18px 0;
}

@media (max-width: 520px) {
  .chart-admin .row2 {
    grid-template-columns: 1fr;
  }
}

/* ----- Instrument tabs above the chart (access model v2: base view free) ----- */
.aichart-insttabs {
  display: flex;
  gap: 6px;
  margin: 20px 0;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: thin;
}

.inst-tab {
  display: inline-flex;
  align-items: baseline;
  gap: 7px;
  flex: none;
  padding: 7px 14px;
  font: 600 .88rem/1 inherit;
  color: var(--muted);
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: border-color .15s, color .15s;
}

.inst-tab:hover {
  color: var(--text);
  border-color: var(--primary);
}

.inst-tab.active {
  color: var(--primary);
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 12%, var(--surface));
}

.inst-tab .inst-symbol {
  font: 500 .72rem/1 inherit;
  color: var(--muted);
}

.inst-tab.active .inst-symbol {
  color: var(--primary);
  opacity: .75;
}

/* ----- Upsell banner: names the paid overlays this user hasn't bought ----- */
.aichart-upsell {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin: 8px 0 4px;
  padding: 10px 14px;
  font-size: .88rem;
  color: var(--text);
  background: color-mix(in srgb, var(--primary) 8%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--primary) 35%, var(--border));
  border-radius: 12px;
}

.aichart-upsell .upsell-text {
  color: var(--muted);
}

.aichart-upsell .aichart-unlock {
  flex: none;
  padding: 6px 14px;
  font: 600 .84rem/1 inherit;
  color: #fff;
  background: var(--primary);
  border: none;
  border-radius: 8px;
  cursor: pointer;
}