/* Theme variables: dark is default, light overrides via data-theme. */
:root,
:root[data-theme="dark"] {
  --bg: #25272c;
  --surface: #2d3036;
  --card: #202329;
  --accent: #b8f7e4;
  --accent-ink: #b8f7e4;            /* accent used as text/icon color */
  --accent-soft: rgba(184, 247, 228, 0.12);
  --accent-border: rgba(184, 247, 228, 0.45);
  --on-accent: #25272c;             /* text on accent-filled elements */
  --text: #f2f8f5;
  --muted: #9ca6a2;
  --border: #3a3e45;
  --green: #2fbf71;
  --red: #e5484d;
  --amber: #f0a63a;
  --mono: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
}

:root[data-theme="light"] {
  --bg: #fafbfa;
  --surface: #f1f5f3;
  --card: #ffffff;
  --accent: #b8f7e4;
  --accent-ink: #17795e;
  --accent-soft: rgba(23, 121, 94, 0.08);
  --accent-border: rgba(23, 121, 94, 0.35);
  --on-accent: #25272c;
  --text: #25272c;
  --muted: #68736f;
  --border: #dde5e1;
  --green: #188a52;
  --red: #d02c31;
  --amber: #b26a00;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  padding: 20px clamp(16px, 4vw, 48px) 40px;
  transition: background 0.2s ease, color 0.2s ease;
}

/* ---------- header ---------- */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 18px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo { width: 40px; height: 40px; }
.brand h1 { font-size: 18px; font-weight: 650; letter-spacing: 0.2px; }
.subtitle { color: var(--muted); font-size: 12px; }

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
  justify-content: flex-end;
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.topbar-actions > .btn,
.topbar-actions > a.btn,
.topbar-actions > .llm-model-wrap { flex-shrink: 0; }

.llm-model-wrap {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.llm-model-wrap select {
  max-width: 96px;
  min-width: 72px;
  padding: 5px 8px;
  font-size: 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
.llm-model-wrap select:focus {
  outline: 1px solid var(--accent-border);
}
.llm-model-wrap select:disabled {
  opacity: 0.65;
}

.rulebook-info {
  flex: 1 1 auto;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}

/* Seamless autoplay ticker for header status chips */
.chip-marquee {
  overflow: hidden;
  width: 100%;
  mask-image: linear-gradient(90deg, transparent, #000 16px, #000 calc(100% - 16px), transparent);
  -webkit-mask-image: linear-gradient(90deg, transparent, #000 16px, #000 calc(100% - 16px), transparent);
}
.chip-track {
  display: flex;
  width: max-content;
  animation: chip-marquee 28s linear infinite;
}
.chip-marquee:hover .chip-track,
.chip-marquee:focus-within .chip-track {
  animation-play-state: paused;
}
.chip-set {
  display: flex;
  gap: 8px;
  padding-right: 8px;
  flex-shrink: 0;
}
.chip {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  flex-shrink: 0;
}
.chip strong { color: var(--text); font-weight: 600; }
.chip.ok { border-color: rgba(47, 191, 113, 0.5); color: var(--green); }
.chip.warn { border-color: rgba(240, 166, 58, 0.5); color: var(--amber); }
.chip.err { border-color: rgba(229, 72, 77, 0.5); color: var(--red); }

@keyframes chip-marquee {
  from { transform: translateX(0); }
  to { transform: translateX(-50%); }
}

@media (prefers-reduced-motion: reduce) {
  .chip-track { animation: none; }
  .chip-set[aria-hidden="true"] { display: none; }
  .chip-marquee {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    mask-image: none;
    -webkit-mask-image: none;
  }
  .chip-marquee::-webkit-scrollbar { display: none; }
}

/* ---------- live quotes ticker tape ---------- */
.ticker-field {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 8px 6px 12px;
  margin-bottom: 16px;
  min-width: 0;
}
.ticker-field.hidden { display: none; }
.ticker-field > .btn { flex-shrink: 0; }
.ticker-tape {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
}
.quote {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  font-size: 12.5px;
  white-space: nowrap;
  flex-shrink: 0;
  padding-right: 22px;
}
.quote .q-sym { color: var(--text); font-weight: 650; letter-spacing: 0.02em; }
.quote .q-px { color: var(--muted); font-variant-numeric: tabular-nums; }
.quote .q-arrow { font-size: 10px; }
.quote .q-pct { font-weight: 600; font-variant-numeric: tabular-nums; }
.quote.up .q-arrow, .quote.up .q-pct { color: var(--green); }
.quote.down .q-arrow, .quote.down .q-pct { color: var(--red); }

/* ---------- charts page ---------- */
.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}
.chart-card { padding: 12px 12px 8px; min-width: 0; overflow: hidden; }
.chart-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}
.chart-title { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.chart-title strong { font-size: 14px; letter-spacing: 0.02em; }
.chart-quote {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.chart-quote .q-px { color: var(--muted); }
.chart-quote .q-arrow.up, .chart-quote .q-pct.up { color: var(--green); }
.chart-quote .q-arrow.down, .chart-quote .q-pct.down { color: var(--red); }
.chart-quote .q-arrow { font-size: 10px; }
.chart-quote .q-pct { font-weight: 600; }
.chart-canvas {
  width: 100%;
  height: 220px;
  max-width: 100%;
  overflow: hidden;
  position: relative;
}

/* theme toggle */
.btn.icon {
  width: 34px; height: 34px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 9px;
}
.btn.icon svg { width: 16px; height: 16px; stroke: var(--accent); fill: none; stroke-width: 2; }
.btn.icon:hover svg { stroke: var(--accent); }
:root[data-theme="light"] .btn.icon svg { stroke: #000; }
:root[data-theme="light"] .btn.icon:hover svg { stroke: #000; }
:root[data-theme="dark"] .icon-moon { display: none; }
:root[data-theme="light"] .icon-sun { display: none; }

/* ---------- cards & layout ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
}

.controls {
  display: flex;
  align-items: flex-end;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.control-group { display: flex; flex-direction: column; gap: 4px; }
.control-group label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.control-group input:not([type="checkbox"]):not([type="radio"]),
.control-group select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 13px;
  min-width: 130px;
}
.control-group input:not([type="checkbox"]):not([type="radio"]):focus,
.control-group select:focus { outline: 1px solid var(--accent-border); }
.control-group #rulebook {
  min-width: 0;
  width: auto;
  max-width: 200px;
}
.control-actions { display: flex; gap: 8px; margin-left: auto; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 550;
  cursor: pointer;
  transition: filter 0.15s, transform 0.05s;
  line-height: 1.2;
  box-sizing: border-box;
}
.btn:hover { filter: brightness(1.08); }
:root[data-theme="dark"] .btn:hover { filter: brightness(1.25); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: wait; }
.btn.busy {
  position: relative;
  pointer-events: none;
  opacity: 0.85;
  cursor: wait;
}
.btn.busy .btn-label { opacity: 0.85; }
.btn.busy .btn-spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  margin-right: 8px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  vertical-align: -2px;
  animation: btn-spin 0.7s linear infinite;
}
@keyframes btn-spin {
  to { transform: rotate(360deg); }
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.small { padding: 4px 10px; font-size: 12px; }
a.btn {
  text-decoration: none;
  color: inherit;
  -webkit-appearance: none;
}
a.btn:hover, a.btn:visited, a.btn:active { text-decoration: none; color: inherit; }
a.btn.primary,
a.btn.primary:hover,
a.btn.primary:visited,
a.btn.primary:active {
  color: var(--on-accent);
  background: var(--accent);
  border-color: var(--accent);
}
.btn.approve { background: rgba(47, 191, 113, 0.14); border-color: rgba(47, 191, 113, 0.5); color: var(--green); }
.btn.reject { background: rgba(229, 72, 77, 0.12); border-color: rgba(229, 72, 77, 0.5); color: var(--red); }

.banner {
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 14px;
  font-size: 13px;
  border: 1px solid;
}
.banner.info { background: var(--accent-soft); border-color: var(--accent-border); color: var(--accent-ink); }
.banner.error { background: rgba(229, 72, 77, 0.1); border-color: rgba(229, 72, 77, 0.4); color: var(--red); }
.hidden { display: none; }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(300px, 1fr);
  gap: 16px;
  align-items: start;
}
.proposals-panel, .side-panel { min-width: 0; }
@media (max-width: 980px) { .layout { grid-template-columns: 1fr; } }

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.panel-head h2 { font-size: 14px; font-weight: 650; letter-spacing: 0.03em; }
.panel-head-actions { display: flex; align-items: center; gap: 8px; }
.hint { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.signal-toggles.compact {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  margin-top: 6px;
  max-width: 280px;
}
.signal-toggles.compact .sig-chip {
  padding: 0;
  margin: 0;
  gap: 4px;
  font-size: 11px;
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0;
  align-items: center;
  line-height: 1.2;
}
.signal-toggles.compact .sig-chip input[type="checkbox"] {
  min-width: 0;
  width: 14px;
  height: 14px;
  margin: 0;
  padding: 0;
  flex-shrink: 0;
  accent-color: var(--accent-ink, var(--green));
}
.rules-panel { display: none; }
body.engine-btc #ctrl-provider,
body.engine-btc #ctrl-days { display: none; }
.page-charts .btc-chart-canvas { height: min(62vh, 560px); width: 100%; }
.page-charts #btc-chart-wrap { margin-bottom: 14px; }

.tabs { display: flex; gap: 4px; }
.tab {
  background: none;
  border: 1px solid transparent;
  border-radius: 7px;
  color: var(--muted);
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}
.tab.active { background: var(--card); border-color: var(--border); color: var(--text); }

/* ---------- proposals ---------- */
.proposals-list { display: flex; flex-direction: column; gap: 12px; }
.empty { color: var(--muted); padding: 24px; text-align: center; font-size: 13px; }

.proposal { padding: 14px 16px; }
.proposal-top {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
  row-gap: 8px;
}
.proposal-top .chip {
  padding: 5px 12px;
  line-height: 1.25;
  margin: 2px 0 4px;
}
.rank {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
}
.symbol { font-size: 18px; font-weight: 700; letter-spacing: 0.03em; }
.px { font-family: var(--mono); color: var(--muted); font-size: 13px; }

.status-badge {
  margin-left: auto;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 11px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.status-pending { background: rgba(240, 166, 58, 0.15); color: var(--amber); }
.status-approved { background: rgba(47, 191, 113, 0.15); color: var(--green); }
.status-rejected { background: rgba(229, 72, 77, 0.13); color: var(--red); }

.scorebar {
  height: 5px;
  background: var(--surface);
  border-radius: 4px;
  overflow: hidden;
  margin: 10px 0;
}
.scorebar > div { height: 100%; background: linear-gradient(90deg, var(--accent), #5bc9a0); }

.plan {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 8px;
  margin-top: 4px;
  margin-bottom: 10px;
}
.plan .cell {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
}
.plan .cell .k { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.plan .cell .v { font-family: var(--mono); font-size: 13px; }

.why { margin: 0 0 12px; padding-left: 18px; color: var(--muted); font-size: 12.5px; }
.why li { margin-bottom: 2px; }
.why li::marker { color: var(--accent-ink); }

.decision-row { display: flex; gap: 8px; align-items: center; }
.decided { font-size: 12px; color: var(--muted); }

/* ---------- side panel ---------- */
.side-panel { display: flex; flex-direction: column; gap: 16px; }

.metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
}
.metric {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}
.metric .k { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.metric .v { font-family: var(--mono); font-size: 15px; font-weight: 600; }
.metric .v.pos { color: var(--green); }
.metric .v.neg { color: var(--red); }
.data-table td.pos { color: var(--green); font-weight: 600; }
.data-table td.neg { color: var(--red); }
.bt-note { color: var(--muted); font-size: 11.5px; margin-top: 10px; }

.unread-badge {
  display: inline-block;
  background: var(--red);
  color: #fff;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 7px;
  vertical-align: 2px;
  margin-left: 4px;
}
.section-label {
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin: 12px 0 4px;
}
.audit-row.unread .audit-detail { color: var(--text); font-weight: 550; }
.warn-text { color: var(--amber) !important; }

.audit-body {
  max-height: 340px;
  overflow-x: hidden;
  overflow-y: auto;
  scrollbar-width: thin; /* Firefox */
  scrollbar-color: var(--border) transparent;
}
.audit-body::-webkit-scrollbar {
  width: 6px;
}
.audit-body::-webkit-scrollbar-track {
  background: transparent;
}
.audit-body::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 6px;
}
.audit-body::-webkit-scrollbar-thumb:hover {
  background: var(--muted);
}
.audit-row {
  display: flex;
  gap: 10px;
  padding: 6px 2px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
}
.audit-row:last-child { border-bottom: none; }
#audit .audit-row {
  cursor: pointer;
  border-radius: 6px;
  padding: 6px 8px;
  transition: background 0.12s ease, color 0.12s ease;
}
#audit .audit-row:hover {
  background: var(--accent-soft);
}
#audit .audit-row:hover .audit-detail,
#audit .audit-row:hover .audit-ts {
  color: var(--text);
}
#audit .audit-row:hover .audit-event {
  color: var(--accent-ink);
}
.audit-ts { font-family: var(--mono); color: var(--muted); white-space: nowrap; flex-shrink: 0; }
.audit-event { font-weight: 600; white-space: nowrap; flex-shrink: 0; }
.audit-detail {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  flex: 1 1 auto;
}

/* ---------- audit detail modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal-backdrop.hidden { display: none; }
.modal-card {
  width: min(520px, 100%);
  max-height: min(80vh, 640px);
  overflow: auto;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px 18px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.35);
}
.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.modal-head h3 {
  font-size: 15px;
  font-weight: 650;
  line-height: 1.3;
}
.modal-meta {
  color: var(--muted);
  font-size: 12px;
  font-family: var(--mono);
  margin-bottom: 12px;
}
.modal-body {
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
  color: var(--text);
}
.modal-body .audit-kv {
  display: grid;
  grid-template-columns: minmax(90px, 34%) 1fr;
  gap: 6px 12px;
  margin: 0;
}
.modal-body .audit-kv dt {
  color: var(--muted);
  font-size: 12px;
}
.modal-body .audit-kv dd {
  margin: 0;
  word-break: break-word;
}

.foot {
  margin-top: 24px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}

/* ---------- toasts ---------- */
.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 1000;
  max-width: min(360px, 90vw);
}
.toast {
  background: var(--card);
  border: 1px solid var(--accent-border);
  border-left: 4px solid var(--accent);
  border-radius: 10px;
  padding: 10px 14px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 13px;
  animation: toast-in 0.25s ease;
}
.toast strong { font-size: 13px; }
.toast span { color: var(--muted); font-size: 12.5px; }
.toast.order { border-left-color: var(--amber); border-color: rgba(240, 166, 58, 0.4); }
@keyframes toast-in { from { transform: translateY(12px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- engine status (bot heartbeat) ---------- */
.live-dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 6px var(--green);
  margin-left: 6px;
  vertical-align: 1px;
}
.engine-body { font-size: 13px; }
.engine-asset {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}
.engine-asset:first-of-type { margin-top: 8px; }
.engine-asset-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
}
.engine-meta {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 11.5px;
  margin: 4px 0;
}
.engine-rules, .engine-tfs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin: 4px 0;
}
.monitor-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.switch { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; font-size: 13px; cursor: pointer; }
.switch input { accent-color: var(--accent); width: 16px; height: 16px; }
.monitor-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
.monitor-every {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}
.monitor-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.mini-input {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 3px 6px;
  font-size: 12px;
  width: 56px;
}
select.mini-input { width: auto; }
.monitor-meta { display: flex; gap: 12px; flex-wrap: wrap; color: var(--muted); font-size: 11.5px; margin: 8px 0; }
.monitor-markets { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }

/* ---------- trading mode ---------- */
.trading-mode-body { font-size: 13px; }
.mode-toggle {
  display: inline-flex;
  gap: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}
.mode-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 8px 18px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  min-width: 88px;
}
.mode-btn + .mode-btn { border-left: 1px solid var(--border); }
.mode-btn.active {
  background: var(--accent);
  color: var(--on-accent);
}
.mode-btn.locked {
  opacity: 0.55;
  cursor: not-allowed;
}
.mode-btn:disabled:not(.locked) { opacity: 0.5; cursor: not-allowed; }
.chip.warn { background: rgba(240, 166, 58, 0.14); color: var(--amber); }
.warn-text { color: var(--amber); }

/* ---------- execution panel (legacy bits) ---------- */
.status-text-filled, .status-text-manual_filled { color: var(--green); }
.status-text-rejected, .status-text-canceled { color: var(--red); }
.status-text-submitted, .status-text-partial, .status-text-manual_pending { color: var(--amber); }

/* ---------- AI brief & assistant ---------- */
.ai-brief {
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 12.5px;
  line-height: 1.55;
  margin: 14px 0 16px;
}
.ai-brief .hint { display: block; margin-bottom: 4px; }
.decision-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
  row-gap: 10px;
  flex-wrap: wrap;
  margin-top: 14px;
}
.decision-wrap .decision-row { gap: 10px; }
.assistant-form { display: flex; gap: 8px; }
.assistant-form input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 13px;
  min-width: 0;
}
.assistant-answer {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12.5px;
  margin-bottom: 10px;
  white-space: pre-wrap;
}

/* ---------- rulebook studio ---------- */
.studio-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr);
  gap: 16px;
  align-items: start;
}
@media (max-width: 980px) { .studio-layout { grid-template-columns: 1fr; } }
.studio-textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 13px;
  resize: vertical;
}
.studio-textarea.yaml { font-family: var(--mono); font-size: 12.5px; }
.studio-actions { margin-top: 10px; flex-wrap: wrap; }
.studio-draft-pair {
  display: flex;
  gap: 8px;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
  flex-wrap: wrap;
}
.studio-draft-pair .btn { flex: 0 1 auto; white-space: nowrap; }
.draft-name { width: 140px; flex: 0 0 auto; }
#mode-transcript .bt-note {
  margin-top: 0;
  margin-bottom: 14px;
}
.chat-log {
  max-height: 340px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
}
.chat-msg {
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
  max-width: 92%;
  white-space: pre-wrap;
}
.chat-msg.user { background: var(--accent-soft); border: 1px solid var(--accent-border); align-self: flex-end; }
.chat-msg.assistant { background: var(--surface); border: 1px solid var(--border); align-self: flex-start; }
.chat-intro {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
  max-width: 92%;
  align-self: flex-start;
  padding: 2px 0 6px;
}

/* ---------- user guide ---------- */
.guide-layout {
  display: grid;
  grid-template-columns: minmax(200px, 240px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
  margin-bottom: 28px;
}
.guide-toc {
  position: sticky;
  top: 12px;
  padding: 14px 16px;
}
.guide-toc-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  margin: 0 0 10px;
  padding: 0;
  border: none;
  background: transparent;
  color: inherit;
  cursor: default;
  text-align: left;
}
.guide-toc-title {
  font-size: 13px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.guide-toc-chevron {
  display: none;
  width: 10px;
  height: 10px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  transition: transform 0.18s ease;
  flex-shrink: 0;
  margin-top: -4px;
}
.guide-toc ol {
  margin: 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
}
.guide-toc a {
  color: var(--accent-ink);
  text-decoration: none;
}
.guide-toc a:hover { text-decoration: underline; }
.guide-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
}
.guide-section h2 {
  font-size: 17px;
  margin: 0 0 10px;
}
.guide-section h3 {
  font-size: 14px;
  margin: 14px 0 6px;
}
.guide-section p,
.guide-section li {
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--text);
}
.guide-section p { margin: 0 0 10px; }
.guide-section ul,
.guide-section ol {
  margin: 0 0 10px;
  padding-left: 20px;
}
.guide-section li { margin-bottom: 4px; }
.guide-section code {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}
.guide-callout {
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--accent-ink);
  margin: 8px 0 0 !important;
}

/* Mobile: foldable sticky Contents bar */
@media (max-width: 860px) {
  .guide-layout { grid-template-columns: 1fr; }
  .guide-toc {
    position: sticky;
    top: 0;
    z-index: 30;
    padding: 0;
    overflow: hidden;
  }
  .guide-toc-toggle {
    cursor: pointer;
    padding: 12px 14px;
    margin: 0;
    min-height: 44px;
    box-sizing: border-box;
  }
  .guide-toc-toggle:active { filter: brightness(1.08); }
  .guide-toc-chevron { display: block; }
  .guide-toc ol {
    padding: 0 14px 12px 32px;
    border-top: 1px solid var(--border);
    padding-top: 10px;
  }
  .guide-toc.is-folded ol { display: none; }
  .guide-toc.is-folded .guide-toc-chevron {
    transform: rotate(-45deg);
    margin-top: 2px;
  }
}
.foot a { color: var(--accent-ink); }

/* ---------- login page ---------- */
.login-wrap {
  min-height: calc(100vh - 60px);
  display: grid;
  place-items: center;
}
.login-card {
  width: min(420px, 94vw);
  text-align: center;
  padding: 28px 28px 32px;
}
.login-logo { margin-bottom: 14px; }
.login-logo img { width: min(280px, 78%); display: block; margin: 0 auto; }
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="light"] .logo-dark { display: none; }

.login-title { font-size: 22px; font-weight: 700; letter-spacing: 0.14em; }
.login-tagline {
  color: var(--accent-ink);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.22em;
  margin: 2px 0 14px;
}
.login-desc { color: var(--muted); font-size: 13px; margin-bottom: 20px; }

#login-form { display: flex; flex-direction: column; gap: 10px; }
#login-form input {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 11px 12px;
  font-size: 14px;
  text-align: center;
}
#login-form input:focus { outline: 1px solid var(--accent-border); }
.login-btn { padding: 11px; font-size: 14px; }
.login-error { color: var(--red); font-size: 13px; margin-top: 12px; }

/* ---------- responsive: phones ---------- */
@media (max-width: 640px) {
  body { padding: 12px 12px 32px; }

  /* header stacks; status tape full width, action buttons centered below it */
  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    margin-bottom: 0;
  }
  /* Studio / Charts / Guide: restore space under header actions before content */
  .topbar:not(:has(.topbar-actions)) {
    margin-bottom: 20px;
  }
  .brand h1 { font-size: 16px; }
  .topbar-right {
    flex-wrap: wrap;
    justify-content: flex-end;
    align-items: center;
    gap: 0;
    max-width: 100%;
    min-width: 0;
  }
  /* Studio / Charts / Guide header chips + buttons need breathing room */
  .topbar:not(:has(.topbar-actions)) .topbar-right {
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
  }
  /* Charts only: Guide + Dashboard first (natural size); meta chip below */
  .page-charts .topbar-right > .header-btn-row {
    display: inline-flex;
    flex: 0 0 auto;
    flex-wrap: nowrap;
    align-items: center;
    gap: 10px;
    order: 1;
  }
  .page-charts .topbar-right > .header-btn-row > .btn,
  .page-charts .topbar-right > .header-btn-row > a.btn {
    flex: 0 0 auto;
  }
  .page-charts .topbar-right > .chip {
    flex: 1 1 100%;
    min-width: 0;
    order: 2;
    text-align: center;
  }
  .rulebook-info {
    position: static;
    flex: 1 1 100%;
    order: 1;
    min-width: 0;
    max-width: none;
    width: 100%;
    overflow: hidden;
    pointer-events: auto;
  }
  .topbar-actions {
    flex: 1 1 100%;
    order: 2;
    display: flex;
    width: 100%;
    justify-content: space-between;
    align-items: center;
    flex-wrap: nowrap;
    gap: 5px;
    /* equal air above/below so buttons sit between status tape and symbol ticker */
    margin-top: 16px;
    margin-bottom: 16px;
    min-height: 44px;
    box-sizing: border-box;
  }
  .topbar-actions > .btn,
  .topbar-actions > a.btn {
    flex: 1 1 0;
    min-width: 0;
    justify-content: center;
    padding-left: 4px;
    padding-right: 4px;
    text-align: center;
  }
  /* theme toggle: compact width, same height as Guide / Studio / Sign out */
  .topbar-actions > .btn.icon {
    flex: 0 0 auto;
    width: 28px;
    height: 36px;
    min-width: 28px;
    min-height: 36px;
    padding: 0;
    border-radius: 8px;
    box-sizing: border-box;
  }
  .topbar-actions > .btn.icon svg {
    width: 14px;
    height: 14px;
  }
  /* model select: wide enough for GPT / OPUS / KIMI (not a crushed square) */
  .topbar-actions > .llm-model-wrap {
    flex: 0 0 auto;
    width: 58px;
    min-width: 58px;
  }
  .llm-model-wrap select {
    width: 58px;
    height: 36px;
    min-width: 58px;
    max-width: 58px;
    min-height: 36px;
    padding: 0 4px;
    font-size: 11px;
    font-weight: 650;
    letter-spacing: 0;
    line-height: 1.2;
    text-align: center;
    text-align-last: center;
    border-radius: 8px;
    box-sizing: border-box;
    appearance: none;
    -webkit-appearance: none;
    background-image: none;
  }
  .ticker-field { margin-top: 0; }
  .ticker-tape {
    margin-top: 0;
  }
  .chip-marquee {
    mask-image: linear-gradient(90deg, transparent, #000 14px, #000 calc(100% - 6px), transparent);
    -webkit-mask-image: linear-gradient(90deg, transparent, #000 14px, #000 calc(100% - 6px), transparent);
  }

  /* controls become a 2-column grid with full-width action buttons */
  .controls { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
  .control-group input:not([type="checkbox"]):not([type="radio"]),
  .control-group select { width: 100%; min-width: 0; }
  .control-group #rulebook { max-width: none; width: 100%; }
  /* swap History (days) ↔ Rulebook on phones only */
  .controls > .control-group:nth-child(1) { order: 1; } /* provider */
  .controls > .control-group:nth-child(2) { order: 4; } /* history → bottom-right */
  .controls > .control-group:nth-child(3) { order: 3; } /* equity */
  .controls > .control-group:nth-child(4) { order: 2; } /* rulebook → top-right */
  .controls > .control-actions { order: 5; }
  .control-actions { grid-column: 1 / -1; margin-left: 0; }
  .control-actions .btn { flex: 1; padding: 12px; }
  /* selected provider label: slightly smaller on phones (short name is applied in JS) */
  #provider,
  #rulebook { font-size: 13px !important; }

  /* engine heartbeat controls */
  .monitor-row { gap: 12px; align-items: stretch; }
  .monitor-controls {
    width: 100%;
    gap: 12px;
  }
  .monitor-every { gap: 8px; }
  .monitor-actions {
    width: 100%;
    gap: 10px;
  }
  .monitor-actions .btn { flex: 1; }

  /* prevent iOS focus auto-zoom: inputs must be >= 16px */
  input, select { font-size: 16px !important; }

  .panel-head { flex-wrap: wrap; gap: 6px; }
  .tabs { overflow-x: auto; max-width: 100%; scrollbar-width: none; }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { white-space: nowrap; }

  /* proposal cards: badge stays on the title row, details drop below */
  .proposal-top .status-badge { order: 3; }
  .proposal-top .px { order: 4; width: 100%; }
  .symbol { font-size: 16px; }
  .plan { grid-template-columns: repeat(2, 1fr); }
  .decision-row .btn { flex: 1; padding: 12px; }
  /* studio: Validate → draft name → Backtest + Save on one row */
  .studio-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }
  .studio-actions > #btn-validate {
    flex: 1 1 100%;
  }
  .studio-actions .studio-draft-pair {
    flex: 1 1 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }
  .studio-actions .draft-name {
    flex: 1 1 100%;
    width: 100%;
    order: -1;
    text-align: center;
  }
  .studio-actions .draft-name::placeholder {
    text-align: center;
  }
  /* Ask AI + Capture: smaller placeholder text only (field size unchanged) */
  #assistant-q::placeholder,
  #chat-input::placeholder,
  #transcript::placeholder {
    font-size: 11px;
  }
  .studio-actions .studio-draft-pair .btn {
    flex: 1 1 0;
    min-width: 0;
    padding: 12px 8px;
    font-size: 12px;
  }
  /* extra breathing room around AI brief + action buttons on touch screens */
  .ai-brief { margin: 16px 0 18px; }
  .decision-wrap { margin-top: 16px; gap: 12px; }
  .decision-wrap > .btn { width: 100%; }

  .metrics { grid-template-columns: repeat(2, 1fr); }

  /* audit/alert rows wrap instead of truncating on small screens */
  .audit-row { flex-wrap: wrap; row-gap: 2px; min-width: 0; }
  .audit-detail {
    white-space: normal;
    overflow: visible;
    overflow-wrap: anywhere;   /* long JSON tokens must not widen the page */
    word-break: break-word;
    max-width: 100%;
  }

  .login-card { padding: 22px 18px 26px; }
  .login-logo img { width: min(240px, 82%); }

  .charts-grid { grid-template-columns: 1fr; gap: 10px; }
  .chart-card {
    padding: 12px 12px 16px;
    overflow: hidden; /* keep candlesticks / price scale inside the outline */
  }
  .chart-canvas {
    height: 200px;
    max-width: 100%;
    overflow: hidden;
  }

  /* theme toggle: compact width, same height as text header buttons */
  .topbar-actions .btn.icon {
    width: 28px;
    height: 36px;
    min-width: 28px;
    min-height: 36px;
    padding: 0;
    border-radius: 8px;
    box-sizing: border-box;
  }
}

/* comfortable touch targets on touch devices */
@media (pointer: coarse) {
  .btn { min-height: 42px; }
  .btn.small { min-height: 36px; }
  .btn.icon { width: 42px; height: 42px; }
  .tab { padding: 8px 12px; }
}
/* phones: theme toggle matches sibling button height */
@media (pointer: coarse) and (max-width: 640px) {
  .topbar-actions .btn.icon {
    width: 28px;
    height: 36px;
    min-width: 28px;
    min-height: 36px;
    padding: 0;
    border-radius: 8px;
  }
  .topbar-actions .btn.icon svg {
    width: 14px;
    height: 14px;
  }
  .topbar-actions .llm-model-wrap {
    width: 58px;
    min-width: 58px;
  }
  .topbar-actions .llm-model-wrap select {
    width: 58px;
    min-width: 58px;
    max-width: 58px;
    height: 36px;
    min-height: 36px;
  }
}

/* ---------- BTC Timing page ---------- */
.btc-live-quote {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 10px;
  margin-right: 8px;
  font-variant-numeric: tabular-nums;
}
.btc-live-quote .btc-sym { color: var(--muted); font-size: 12px; font-weight: 600; }
.btc-live-quote strong { font-size: 18px; letter-spacing: -0.02em; line-height: 1.2; }
.btc-live-quote .chip {
  padding: 5px 11px;
  line-height: 1.25;
}
.btc-controls { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end; margin-bottom: 14px; }
.btc-controls .btc-meta { flex: 1 1 100%; color: var(--muted); font-size: 12px; line-height: 1.45; }
.btc-controls .chk { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); padding-top: 18px; }
.btc-data-mode { min-width: 0; }
.btc-data-mode #data-mode-label { line-height: 1.35; }
.btc-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(260px, 0.9fr);
  gap: 14px;
  align-items: start;
}
.btc-layout .span { grid-column: 1 / -1; }
.btc-chart-card { min-width: 0; }
.btc-chart-canvas { height: min(52vh, 480px); width: 100%; }
.btc-side { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.btc-bias-row, .btc-signal-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  row-gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.btc-bias-row:last-child, .btc-signal-row:last-child { border-bottom: 0; }
.btc-bias-row .chip,
.btc-signal-row .chip,
.btc-paper-row .chip,
.btc-wallet-row .chip {
  padding: 5px 11px;
  line-height: 1.25;
}
.btc-signal-list, .btc-bias-list { max-height: 280px; overflow: auto; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.data-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.data-table th, .data-table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); }
.data-table th { color: var(--muted); font-weight: 600; }
.chip.bad { border-color: rgba(229, 72, 77, 0.5); color: var(--red); }
.btc-paper-open { display: flex; flex-direction: column; gap: 12px; }
.btc-paper-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  row-gap: 8px;
}
.btc-paper-fills {
  margin: 0;
  padding-top: 6px;
  line-height: 1.45;
}
.btc-paper-stats { margin-top: 10px; line-height: 1.45; }
.btc-paper-wallet {
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}
.btc-wallet-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.btc-equity {
  font-size: 1.25rem;
  letter-spacing: -0.02em;
  line-height: 1.2;
}
.chk.tight { font-size: 12px; color: var(--muted); gap: 6px; display: inline-flex; align-items: center; }
.btc-overlay-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 14px;
  padding: 0 0 10px;
}
.btc-chart-canvas.adx-pane { height: 90px; margin-top: 8px; }
.btc-chart-canvas.adx-pane.hidden { display: none; }
.page-btc .panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 12px;
}
.page-btc .panel-head .hint {
  min-width: 0;
  line-height: 1.4;
}
@media (max-width: 900px) {
  .btc-layout { grid-template-columns: 1fr; }
  .btc-chart-canvas { height: 280px; }
  .btc-live-quote { width: 100%; order: 3; margin-right: 0; }
  /* Keep TF tabs compact; put Live/Synthetic on its own row so it doesn't wrap into a 80px column */
  .page-btc .btc-data-mode {
    flex: 1 1 100%;
    width: 100%;
  }
  .page-btc .btc-data-mode .chk {
    padding-top: 0;
    width: 100%;
  }
  .page-btc .btc-data-mode #data-mode-label {
    white-space: nowrap;
  }
  .page-btc .panel-head .hint {
    flex: 1 1 100%;
    word-break: break-word;
  }
  .page-btc .btc-controls .control-actions {
    display: flex;
    flex-direction: column;
    width: 100%;
    margin-left: 0;
    gap: 8px;
  }
  .page-btc .btc-controls .control-actions .btn {
    flex: none;
    width: 100%;
    white-space: nowrap;
  }
}
@media (max-width: 640px) {
  body.page-btc {
    padding-bottom: calc(28px + env(safe-area-inset-bottom, 0px));
  }
}
