:root {
  /* Dark theme (default; also used for data-theme="dark" and system+dark). */
  --bg: #0b0e14; --panel: #131722; --panel-2: #1b2233; --border: #232a3b;
  --text: #d6deeb; --muted: #7d8aa5; --accent: #4f9cf9;
  --best: #1f6f43; --best-text: #54e08a; --worst: #6e2230; --worst-text: #ff6b81;
  --spike: #7a5a16; --arb: #2a2350; --arb-text: #b69bff;
  --ok: #54e08a; --warn: #ffd479; --err: #ff6b81; --off: #7d8aa5;
  --hover: #161c2b; --danger: #6e2230;
}
/* Light theme — applied explicitly, or via system preference when on "system". */
:root[data-theme="light"] {
  --bg: #f3f6fb; --panel: #ffffff; --panel-2: #eef2f8; --border: #d6deea;
  --text: #1b2433; --muted: #5b6678; --accent: #2563eb;
  --best: #cdeedd; --best-text: #147a45; --worst: #ffd6dd; --worst-text: #c22847;
  --spike: #ffe6a8; --arb: #e8e2ff; --arb-text: #5a3fb3;
  --ok: #147a45; --warn: #8a6300; --err: #c22847; --off: #8a93a5;
  --hover: #e7edf5; --danger: #dc2626;
}
@media (prefers-color-scheme: light) {
  :root[data-theme="system"] {
    --bg: #f3f6fb; --panel: #ffffff; --panel-2: #eef2f8; --border: #d6deea;
    --text: #1b2433; --muted: #5b6678; --accent: #2563eb;
    --best: #cdeedd; --best-text: #147a45; --worst: #ffd6dd; --worst-text: #c22847;
    --spike: #ffe6a8; --arb: #e8e2ff; --arb-text: #5a3fb3;
    --ok: #147a45; --warn: #8a6300; --err: #c22847; --off: #8a93a5;
  --hover: #e7edf5; --danger: #dc2626;
  }
}
* { box-sizing: border-box; }

/* --- Polish: smooth transitions, focus rings, tidy scrollbars --- */
button, .chip, .seg-btn, .link-btn, a, select, input, .card, .bigbtn, .home-card {
  transition: background-color .15s ease, border-color .15s ease, color .15s ease,
              box-shadow .15s ease, opacity .15s ease, transform .12s ease;
}
button:focus-visible, a:focus-visible, select:focus-visible, input:focus-visible,
.chip:focus-visible, .seg-btn:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
* { scrollbar-color: var(--border) transparent; scrollbar-width: thin; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; }
*::-webkit-scrollbar-thumb:hover { background: var(--muted); }
*::-webkit-scrollbar-track { background: transparent; }
.spinner { display: inline-block; width: 13px; height: 13px; border: 2px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; vertical-align: -2px; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }
body {
  margin: 0; background: var(--bg); color: var(--text); font-size: 13px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
a { color: var(--accent); text-decoration: none; }

/* App-shell (Live): the page itself doesn't scroll. The navbar, the filter
   panels and the view toggle stay fixed, and ONLY the grid's own wrapper scrolls
   — so its sticky header attaches to that wrapper and stays pinned while rows
   move under it. Reliable in Safari/Chrome/Firefox (sticky needs the header to
   live inside the scrolling element). */
body.app-shell { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
body.app-shell .wrap { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
body.app-shell #grid-region { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
body.app-shell .matrix-scroll { flex: 1 1 auto; min-height: 0; overflow: auto; }

/* Fullscreen: the grid region fills the screen (header row stays pinned because
   .matrix-scroll is still the scroller). Works across browsers incl. Safari. */
#grid-region:fullscreen,
#grid-region:-webkit-full-screen {
  background: var(--bg); padding: 10px 14px;
  display: flex; flex-direction: column; height: 100%; width: 100%;
}
#grid-region:fullscreen .matrix-scroll,
#grid-region:-webkit-full-screen .matrix-scroll { flex: 1 1 auto; min-height: 0; overflow: auto; }

header.topbar {
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
  padding: 12px 18px; background: var(--panel); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
header.topbar h1 { font-size: 15px; margin: 0; font-weight: 600; }
.version { font-size: 11px; color: var(--muted); font-weight: 500; }
header.topbar h1 .version { margin-left: 4px; }
.nav { display: flex; gap: 14px; }
.nav a { color: var(--muted); padding: 4px 8px; border-radius: 6px; }
.nav a.active, .nav a:hover { color: var(--text); background: var(--panel-2); }
.spacer { margin-left: auto; }
.status { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--err); }
.dot.live { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.banner { padding: 8px 18px; background: var(--arb); color: var(--arb-text); font-weight: 600; }
.banner.prod { background: var(--best); color: var(--best-text); }
.wrap { padding: 16px 18px 60px; }

button, select, input {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 7px 10px; font-size: 12px;
}
button { cursor: pointer; }
button.primary { background: var(--accent); color: #06101f; border-color: var(--accent); font-weight: 600; }
button.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
button.ghost { background: transparent; }
label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }

/* border-collapse:separate (not collapse) is required for sticky headers to
   render correctly in Safari; border-spacing:0 keeps the look identical. */
table { width: 100%; border-collapse: separate; border-spacing: 0; background: var(--panel); border-radius: 8px; overflow: hidden; }
th, td { text-align: right; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { background: var(--panel-2); color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; cursor: pointer; }
td.l, th.l { text-align: left; }
.num { font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--hover); }
.best { background: var(--best); color: var(--best-text); font-weight: 600; }
.worst { background: var(--worst); color: var(--worst-text); font-weight: 600; }
.spike-cell { background: var(--spike); color: var(--warn); font-weight: 700; }
.stale { opacity: .4; font-style: italic; }

.badge { display: inline-block; font-size: 10px; padding: 1px 7px; border-radius: 10px; margin-left: 6px; }
.badge.mt5 { background: var(--best); color: var(--best-text); }
.badge.api { background: rgba(79,156,249,.22); color: var(--accent); }
.badge.ctrader { background: rgba(182,155,255,.22); color: #b69bff; }
.badge.sim { background: var(--arb); color: var(--arb-text); }
.pill { display:inline-flex; align-items:center; gap:6px; }
.pill-on { color: var(--ok, #54e08a); font-weight: 600; font-size: 12px; }
.pill-off { color: var(--err, #e0544f); font-weight: 600; font-size: 12px; }
.pill .dot.connected { background: var(--ok); }
.pill .dot.offline { background: var(--off); }
.pill .dot.error { background: var(--err); }
.pill .dot.disabled { background: #444c63; }

/* Analytics */
.an-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 14px; }
.an-title { font-size: 14px; margin: 0 0 2px; }
.an-title .an-sub { color: var(--muted); font-weight: 400; font-size: 12px; }
.an-cap { margin: 0 0 10px; font-size: 11.5px; }
.an-cardhead { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.an-cardhead .an-title { flex: 1; }
.chart-type { font-size: 11px; padding: 2px 6px; }
.ai-out { margin-top: 14px; padding: 14px 16px; background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 8px; white-space: pre-wrap; line-height: 1.55; font-size: 13px; }

/* Stall monitor */
.stall-banner {
  background: var(--spike); color: #5a3d00; font-weight: 600;
  border: 1px solid rgba(180,120,0,.5); border-radius: 8px;
  padding: 8px 12px; margin: 0 0 10px; font-size: 12.5px;
}
:root[data-theme="dark"] .stall-banner,
:root:not([data-theme="light"]) .stall-banner { color: var(--warn); }
.stall-alert { outline: 2px solid var(--warn) !important; outline-offset: -2px; }
td.stall-alert, tr.stall-alert > td { background: var(--spike) !important; color: var(--warn) !important; }
.surge-alert { outline: 2px solid var(--err) !important; outline-offset: -2px; }
td.surge-alert, tr.surge-alert > td { background: var(--worst) !important; color: var(--worst-text) !important; font-weight: 700; }
.stall-tag { font-size: 10px; color: var(--warn); margin-left: 6px; font-weight: 700; }
.mon-ic { font-size: 11px; color: var(--accent); opacity: .85; }
.mon-ic.surge { color: var(--err); }
.mon-sec { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin: 10px 0; }
.log-list { max-height: 55vh; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
.log-row { display: flex; align-items: center; gap: 6px; padding: 7px 10px; border-bottom: 1px solid var(--border); font-size: 12.5px; }
.log-row:last-child { border-bottom: none; }
.log-time { margin-left: auto; color: var(--muted); font-size: 11px; white-space: nowrap; }
.mon-row { display: flex; align-items: center; gap: 8px; cursor: pointer; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px,1fr)); gap: 12px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 14px; }
.meta { display: flex; gap: 18px; flex-wrap: wrap; color: var(--muted); margin-bottom: 12px; }
.meta b { color: var(--text); }
.toolbar { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 14px; }
.empty { color: var(--muted); font-style: italic; padding: 8px 0; }

/* Auth */
.userbox { display: flex; align-items: center; gap: 8px; margin-left: 10px; }
.user-email { font-size: 12px; color: var(--muted); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-company { font-size: 12px; color: var(--accent, #54e08a); background: rgba(84,224,138,.12); border-radius: 10px; padding: 2px 8px; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.creds-banner { margin: 0 0 14px; padding: 10px 12px; border-radius: 8px; font-size: 13px; line-height: 1.6; background: rgba(255,212,121,.14); border: 1px solid rgba(255,212,121,.5); }
.creds-banner code { background: rgba(0,0,0,.18); padding: 1px 6px; border-radius: 4px; }
.ai-sheet-box { margin: 4px 0 18px; padding: 14px 16px; border-radius: 10px; background: rgba(79,156,249,.10); border: 1px solid rgba(79,156,249,.35); }
.ai-sheet-btns { display: flex; gap: 10px; flex-wrap: wrap; }
.ai-sheet-btns a { text-decoration: none; }
.imp-bar { background: rgba(255,212,121,.18); border-bottom: 1px solid rgba(255,212,121,.55); color: var(--text); padding: 7px 14px; font-size: 13px; text-align: center; }
.co-detail { padding: 6px 4px 10px; }
.co-detail-stats { display: flex; gap: 18px; flex-wrap: wrap; align-items: center; margin: 0 0 10px; font-size: 13px; }
.co-detail-cols { display: flex; gap: 24px; flex-wrap: wrap; }
.co-detail-cols > div { flex: 1; min-width: 260px; }
.co-detail-cols h4 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; color: var(--muted); }
table.mini { width: 100%; border-collapse: collapse; font-size: 12px; }
table.mini th, table.mini td { padding: 3px 8px; border-bottom: 1px solid var(--border, rgba(255,255,255,.08)); }
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 28px; width: min(380px, 94vw); }
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.login-field { display: block; margin: 0 0 12px; }
.login-field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.login-field input { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-2); color: var(--text); font-size: 14px; }
.login-field input:focus { outline: none; border-color: var(--accent); }
.login-error { color: var(--err); font-size: 12.5px; margin: 12px 0 0; }

/* Home */
.home { display:flex; flex-direction:column; align-items:center; justify-content:center; min-height: 70vh; gap: 28px; text-align:center; }
.home h1 { font-size: 30px; margin: 0; }
.home p { color: var(--muted); max-width: 560px; margin: 0; }
.home-buttons { display:flex; gap: 24px; flex-wrap: wrap; justify-content:center; }
.bigbtn { display:flex; flex-direction:column; gap:8px; align-items:center; justify-content:center;
  width: 240px; height: 150px; border-radius: 16px; font-size: 20px; font-weight: 700;
  border: 1px solid var(--border); background: var(--panel); color: var(--text); cursor:pointer; }
.bigbtn .ic { font-size: 40px; }
.bigbtn.live:hover { border-color: var(--err); box-shadow: 0 0 24px rgba(255,107,129,.25); }
.bigbtn.hist:hover { border-color: var(--accent); box-shadow: 0 0 24px rgba(79,156,249,.25); }
/* Home overview cards */
.home-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; width: 100%; max-width: 760px; }
.home-card { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; text-align: left;
  padding: 16px 18px; border: 1px solid var(--border); border-radius: 12px; background: var(--panel); color: var(--text); }
.home-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 6px 22px rgba(0,0,0,.18); }
.home-card .hc-ic { font-size: 22px; }
.home-card b { font-size: 15px; }
.home-card span:last-child { color: var(--muted); font-size: 12px; }

/* Help / FAQ page */
.help { max-width: 880px; margin: 0 auto; line-height: 1.6; }
.help h1 { font-size: 26px; margin: 6px 0 4px; }
.help .lede { color: var(--muted); margin: 0 0 18px; }
.help section { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 16px 20px; margin: 14px 0; }
.help h2 { font-size: 18px; margin: 4px 0 10px; border-bottom: 1px solid var(--border); padding-bottom: 8px; }
.help h3 { font-size: 14px; margin: 16px 0 4px; }
.help p { margin: 8px 0; }
.help ul, .help ol { margin: 8px 0; padding-left: 22px; }
.help li { margin: 4px 0; }
.help code { background: var(--panel-2); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; font-size: 12px; }
.help .toc { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 8px; }
.help .toc a { background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: 4px 12px; font-size: 12px; }
.help .faq-q { font-weight: 600; margin-top: 14px; }
.help .faq-a { color: var(--muted); margin: 4px 0 0; }
.help .faq-item:first-of-type .faq-q { margin-top: 0; }
.help-search { width: 100%; padding: 11px 14px; margin: 0 0 16px; border: 1px solid var(--border);
  border-radius: 10px; background: var(--panel); color: var(--text); font-size: 14px; }
.help-search:focus { outline: none; border-color: var(--accent); }
.help-noresults { color: var(--muted); font-style: italic; padding: 12px 0; }
.help mark { background: var(--spike); color: inherit; border-radius: 3px; padding: 0 2px; }
.help .pill-key { background: var(--panel-2); border:1px solid var(--border); border-radius: 6px; padding: 1px 6px; font-size: 11px; }

.count-pill { background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: 1px 10px; font-size: 12px; color: var(--muted); font-weight: 600; }
.class-filter { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 0 0 8px; }
.class-filter .cf-label { font-size: 11px; color: var(--muted); text-transform: uppercase; margin-right: 2px; }

.pw-wrap { display: flex; gap: 6px; align-items: stretch; }
.pw-wrap input { flex: 1; min-width: 0; }
.pw-eye { background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 4px 10px; cursor: pointer; color: var(--text); font-size: 14px; line-height: 1; }
.pw-eye:hover { background: var(--hover); }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 12px; padding: 18px; width: min(560px, 94vw); max-height: 90vh; overflow: auto; }
.map-table { font-size: 12px; }
.map-table th { cursor: default; }
.map-table td, .map-table th { padding: 5px 8px; }
.map-table input.map-name { width: 100%; }
.map-table input[type=checkbox] { cursor: pointer; }
.map-table tbody { display: block; max-height: 230px; overflow-y: auto; }
.map-table thead, .map-table tbody tr { display: table; width: 100%; table-layout: fixed; }
.map-table th:first-child, .map-table td:first-child { width: 48px; text-align: center; }
.map-table th:nth-child(2), .map-table td:nth-child(2) { width: 110px; }
dialog::backdrop { background: rgba(0,0,0,.55); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-grid .full { grid-column: 1 / -1; }
.field { display:flex; flex-direction:column; gap:4px; }
.row-actions { display:flex; gap:8px; justify-content:flex-end; margin-top: 14px; }
.chips { display:flex; flex-wrap:wrap; gap:6px; }
.chip { padding: 3px 8px; border:1px solid var(--border); border-radius: 12px; cursor:pointer; user-select:none; }
.chip.on { background: var(--accent); color:#06101f; border-color: var(--accent); }
.toast { position: fixed; bottom: 18px; right: 18px; background: var(--panel-2); border:1px solid var(--border); padding: 10px 14px; border-radius: 8px; opacity:0; transition: opacity .2s; }
.toast.show { opacity: 1; }

/* Live highlight / filter controls */
.hl-toolbar .chips { gap: 5px; }
.hl-toolbar .chip { font-size: 11px; }
.hl-controls { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-start; margin-bottom: 14px; }
.hl-panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; flex: 1 1 280px; min-width: 240px;
}
.hl-panel .chips { gap: 5px; }
.hl-panel .chip { font-size: 11px; }
.hl-modepanel { flex: 0 0 auto; min-width: 150px; }
.hl-head {
  display: flex; align-items: center; gap: 8px; margin-bottom: 8px;
  color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px;
}
.hl-head > span:first-child { font-weight: 700; color: var(--text); }
.hl-count { color: var(--accent); text-transform: none; letter-spacing: 0; }
.hl-actions { margin-left: auto; display: flex; gap: 4px; }
.link-btn { background: transparent; border: 0; color: var(--accent); cursor: pointer; font-size: 11px; padding: 2px 4px; }
.link-btn:hover { text-decoration: underline; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg-btn { border: 0; border-radius: 0; background: var(--panel-2); padding: 7px 12px; }
.seg-btn.active { background: var(--accent); color: #06101f; font-weight: 600; }
/* Highlighted rows pop; non-selected rows dim so the chosen ones stand out. */
tbody tr.hl > td { background: rgba(79,156,249,.16); }
tbody tr.hl > td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
tbody tr.hl:hover > td { background: rgba(79,156,249,.24); }
tbody tr.dim { opacity: .38; }
tbody tr.dim:hover { opacity: .75; }

/* Broker-comparison matrix (symbols × brokers) */
.matrix { min-width: max-content; }
.matrix th, .matrix td { text-align: right; font-variant-numeric: tabular-nums; }
/* The grid lives inside the scrolling .wrap; its header pins to the top of that
   scroll region (offset 0), and the symbol column to the left. */
.matrix-scroll { overflow: visible; }
#grid thead th {
  position: sticky; top: 0; z-index: 3; background: var(--panel-2);
}
/* Symbol column stays frozen on horizontal scroll (Matrix view). */
.matrix th.corner, .matrix td.sym {
  text-align: left; position: sticky; left: 0; z-index: 2;
  background: var(--panel); font-weight: 600;
}
/* Top-left cell is frozen in both directions, above everything else. */
.matrix thead th.corner {
  top: 0; left: 0; z-index: 4; background: var(--panel-2);
}
.matrix td.cell-empty { color: var(--muted); opacity: .35; text-align: center; }
.matrix th.hl-col { background: rgba(79,156,249,.22); color: var(--text); }
.matrix td.sym.hl-row { background: rgba(79,156,249,.14); box-shadow: inset 3px 0 0 var(--accent); }
.matrix td.hl-soft { outline: 1px solid rgba(79,156,249,.55); outline-offset: -1px; }
.matrix td.hl { outline: 2px solid var(--accent); outline-offset: -2px; font-weight: 700; }
.matrix td.dim { opacity: .32; }
.matrix td.dim:hover { opacity: .8; }
/* Reorder controls inside the matrix */
.ord-btn {
  background: transparent; border: 0; color: var(--muted); cursor: pointer;
  padding: 0 2px; font-size: 10px; line-height: 1; vertical-align: middle;
}
.ord-btn:hover { color: var(--accent); }

/* Theme picker in the navbar */
.theme-pick { display: flex; align-items: center; gap: 6px; }
.theme-pick label { margin: 0; }
.theme-pick select { padding: 4px 8px; }

/* Home page copyright footer */
.copyright {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 16px 40px; color: var(--muted); font-size: 12px; text-align: center;
}
.copyright span:first-child { font-weight: 600; color: var(--text); }
