/* ============================================================================
 * HFChart — styles & tokens (clair par défaut, sombre sélectionné, pas dérivé)
 * Author : Simon-Pierre Boucher — contact@spboucher.ai
 * ----------------------------------------------------------------------------
 * Les couleurs de séries suivent la palette catégorielle validée (ordre FIXE).
 * Hausse/baisse : schéma "classic" (vert/rouge, avec encodage secondaire par
 * la géométrie des chandelles) ou "cvd" (bleu/rouge, validé CVD ΔE 19,2).
 * ==========================================================================*/

:root {
  color-scheme: light;
  --page: #f4f4f2;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --chip: #0b0b0b;
  --chip-text: #ffffff;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;
  --up: #0ca30c;
  --dn: #d03b3b;
  --accent-ui: #2a78d6;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --ink: #ffffff;
  --ink2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --chip: #ffffff;
  --chip-text: #0b0b0b;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
  --up: #0ca30c;
  --dn: #d03b3b;
  --accent-ui: #3987e5;
}

/* Schéma hausse/baisse CVD-sûr (paire divergente bleu↔rouge validée). */
:root[data-scheme="cvd"] { --up: #2a78d6; --dn: #e34948; }
:root[data-theme="dark"][data-scheme="cvd"] { --up: #3987e5; --dn: #e66767; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font: 13px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--ink);
}

body { display: flex; flex-direction: column; }

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

/* ------------------------------------------------------------------ topbar */
#topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 8px; margin-right: 8px; }
.brand .logo {
  background: var(--accent-ui);
  color: #fff;
  font-weight: 700;
  font-size: 11px;
  padding: 3px 6px;
  border-radius: 6px;
  letter-spacing: 0.5px;
}
.brand strong { font-size: 15px; }
.brand .provider {
  font-size: 11px;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 9px;
}
.brand .provider:hover { color: var(--accent-ui); text-decoration: none; }

button, select, input[type="search"], input[type="number"] {
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 5px 10px;
  cursor: pointer;
}
select { cursor: pointer; }
button:hover:not(:disabled), select:hover { border-color: var(--accent-ui); }
button:disabled { opacity: 0.35; cursor: not-allowed; }
button.active { background: var(--accent-ui); color: #fff; border-color: var(--accent-ui); }

.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: none; border-radius: 0; padding: 5px 9px; }
.seg button + button { border-left: 1px solid var(--border); }

.dropdown { position: relative; }
.dropdown .menu {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 40;
  min-width: 240px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 10px 32px rgba(0, 0, 0, 0.18);
  padding: 8px;
  max-height: 60vh;
  overflow: auto;
}
.dropdown.open .menu { display: block; }
.dropdown .menu label { display: flex; align-items: center; gap: 8px; padding: 5px 6px; border-radius: 6px; cursor: pointer; }
.dropdown .menu label:hover { background: var(--page); }
.dropdown .menu em { color: var(--muted); font-style: normal; font-size: 11px; margin-left: auto; }
.param-row { justify-content: space-between; }
.param-row input { width: 90px; cursor: text; }

/* ------------------------------------------------------------------ layout */
#layout { display: flex; flex: 1; min-height: 0; }

#sidebar {
  width: 218px;
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  min-height: 0;
}
#group-tabs { display: flex; flex-wrap: wrap; gap: 4px; }
#group-tabs button { padding: 3px 8px; font-size: 12px; border-radius: 999px; }
#search { cursor: text; }
#symbols {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
  border-top: 1px solid var(--border);
}
#symbols li {
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
#symbols li:hover { background: var(--page); }
#symbols li.active { background: var(--accent-ui); color: #fff; }
#symbols li.compared { outline: 2px solid var(--accent-ui); outline-offset: -2px; }
#sym-count { margin: 0; font-size: 11px; }
.hint {
  margin: 0;
  font-size: 11.5px;
  color: var(--ink2);
  background: var(--page);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 6px 8px;
  display: none;
}

#chart-area { flex: 1; position: relative; min-width: 0; display: flex; flex-direction: column; }
#chart-wrap { position: relative; flex: 1; min-height: 0; background: var(--surface); }
#chart-wrap.loading::after {
  content: "";
  position: absolute;
  top: 10px; right: 84px;
  width: 14px; height: 14px;
  border: 2px solid var(--grid);
  border-top-color: var(--accent-ui);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  z-index: 5;
}
@keyframes spin { to { transform: rotate(360deg); } }

#legend {
  position: absolute;
  top: 8px; left: 10px;
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  pointer-events: none;
  font-size: 12.5px;
  background: color-mix(in srgb, var(--surface) 74%, transparent);
  backdrop-filter: blur(8px) saturate(1.2);
  -webkit-backdrop-filter: blur(8px) saturate(1.2);
  border: 1px solid var(--border);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.07);
  border-radius: 9px;
  padding: 4px 10px;
  max-width: calc(100% - 110px);
}
#legend .sym { font-weight: 700; font-size: 14px; }
#legend .k { color: var(--muted); margin-right: 1px; }
#legend b.up { color: var(--up); font-weight: 600; }
#legend b.dn { color: var(--dn); font-weight: 600; }
#legend #ohlcv { font-variant-numeric: tabular-nums; }
.chip-sym { display: inline-flex; align-items: center; gap: 5px; font-weight: 600; }
.chip-sym i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

#footer {
  padding: 5px 12px;
  font-size: 11px;
  color: var(--muted);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

/* ------------------------------------------------------------------ panels */
.overlay-panel {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, 0.42);
  padding: 4vh 4vw;
}
.overlay-panel.open { display: flex; }
.overlay-panel .panel {
  margin: auto;
  width: min(1100px, 100%);
  max-height: 92vh;
  overflow: auto;
  background: var(--page);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px 20px;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; }
.panel-head h2 { margin: 0; font-size: 17px; }

#gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 12px;
  margin-top: 12px;
}
.g-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-align: left;
  padding: 10px;
  border-radius: 10px;
  background: var(--surface);
}
.g-card canvas { width: 100%; height: 96px; display: block; }
.g-card strong { font-size: 13px; }
.g-card span { font-size: 11px; color: var(--muted); }
.g-card.active {
  /* neutralise le style bouton .active (fond bleu) : la sélection se marque
     par un anneau, le mini-chart garde ses couleurs lisibles */
  background: var(--surface);
  color: inherit;
  border-color: var(--border);
  outline: 2px solid var(--accent-ui);
  outline-offset: 1px;
}

.table-scroll { overflow: auto; max-height: 74vh; margin-top: 10px; }
#table-view table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
#table-view th, #table-view td { padding: 4px 10px; text-align: right; border-bottom: 1px solid var(--border); }
#table-view th:first-child, #table-view td:first-child { text-align: left; }
#table-view th { position: sticky; top: 0; background: var(--page); color: var(--ink2); }

/* ------------------------------------------------------------------ toast */
.toast {
  position: fixed;
  bottom: 40px;
  left: 50%;
  transform: translate(-50%, 20px);
  background: var(--chip);
  color: var(--chip-text);
  padding: 8px 16px;
  border-radius: 9px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 90;
  max-width: 80vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: #d03b3b; color: #fff; }

/* ------------------------------------------------------------------ export */
#export-code {
  width: 100%;
  height: 260px;
  margin-top: 10px;
  font: 11.5px/1.5 ui-monospace, "SF Mono", Menlo, monospace;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  resize: vertical;
  white-space: pre;
  cursor: text;
}
.export-actions { display: flex; align-items: center; gap: 12px; margin-top: 10px; }
button.primary { background: var(--accent-ui); color: #fff; border-color: var(--accent-ui); }
#export-tabs { margin-top: 10px; }
#export-note { margin: 10px 0 0; }

/* ------------------------------------------------------------------ embed (iframe ?embed=1) */
body.embed #topbar, body.embed #sidebar, body.embed #footer { display: none; }
body.embed #layout { height: 100vh; }

/* ------------------------------------------------------------------ mobile */
#btn-symbols { display: none; }

@media (max-width: 900px) {
  .brand .provider { display: none; }
}

@media (max-width: 760px) {
  #topbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 6px;
    padding: 6px 8px;
    scrollbar-width: none;
  }
  #topbar::-webkit-scrollbar { display: none; }
  #topbar button, #topbar select { padding: 6px 9px; flex: none; }
  .brand { margin-right: 2px; }
  .brand strong { display: none; }
  .seg { flex: none; }

  #btn-symbols { display: inline-block; font-size: 16px; padding: 5px 10px; }
  #sidebar {
    position: fixed;
    top: 0; bottom: 0; left: 0;
    width: min(300px, 84vw);
    z-index: 55;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.25);
    border-right: 1px solid var(--border);
  }
  body.drawer-open #sidebar { transform: none; }
  #drawer-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 54;
    background: rgba(0, 0, 0, 0.35);
  }
  body.drawer-open #drawer-backdrop { display: block; }
  #symbols li { padding: 9px 10px; }        /* cibles tactiles ≥ 36px */
  #group-tabs button { padding: 6px 11px; }

  #legend { font-size: 11.5px; gap: 6px; max-width: calc(100% - 70px); }
  #legend .sym { font-size: 13px; }
  .overlay-panel { padding: 2vh 2vw; }
  .overlay-panel .panel { padding: 12px 14px; max-height: 96vh; }
  #gallery-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; }
  .g-card canvas { height: 74px; }
  #footer { display: none; }
  #export-code { height: 200px; font-size: 10.5px; }
}
