/* ============================================================================
   WiseTrader Toolbox — Documentation Design System
   ----------------------------------------------------------------------------
   All colours and theme tokens are defined as CSS custom properties below.
   Light mode is the default (:root). Dark mode overrides live under
   [data-theme="dark"]. To re-theme the whole site, edit only these variables.
   ========================================================================== */

/* ---- Design tokens: LIGHT (default) -------------------------------------- */
:root {
  /* Accent — confident teal/green (single source of truth) */
  --accent:          #0e9f6e;
  --accent-hover:    #0c8a5f;
  --accent-soft:     rgba(14, 159, 110, 0.12);
  --accent-contrast: #ffffff;

  /* Neutral surfaces */
  --bg:           #f6f7f9;   /* page background */
  --surface:      #ffffff;   /* cards, bars, sidebar */
  --surface-alt:  #f1f3f5;   /* code blocks, subtle fills */
  --surface-hover:#eef0f2;

  /* Text */
  --text:         #1b2129;   /* primary */
  --text-muted:   #5b6675;   /* secondary */
  --text-faint:   #8a94a3;   /* tertiary / captions */

  /* Lines */
  --border:       #e3e7eb;
  --border-strong:#d3d9df;

  /* Code */
  --code-bg:      #f4f6f8;
  --code-text:    #1b2129;
  --code-border:  #e3e7eb;

  /* AFL syntax highlighting (light = AmiBroker default editor scheme) */
  --tok-comment: #008000;
  --tok-string:  #A31515;
  --tok-keyword: #800000;
  --tok-func:    #0000FF;
  --tok-const:   #800000;
  --tok-prep:    #800080;
  --tok-number:  #000080;
  --tok-reserved:#800000;

  /* Effects */
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.12);
  --radius:    8px;
  --radius-sm: 6px;

  /* Layout metrics */
  --topbar-h:   60px;
  --sidebar-w:  280px;
  --toc-w:      220px;
  --measure:    760px;

  /* Typography */
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
             Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  --font-mono: "SFMono-Regular", "JetBrains Mono", "Fira Code", Consolas,
               "Liberation Mono", Menlo, Courier, monospace;
}

/* ---- Design tokens: DARK -------------------------------------------------- */
[data-theme="dark"] {
  --accent:          #14b981;
  --accent-hover:    #34d39e;
  --accent-soft:     rgba(20, 185, 129, 0.16);
  --accent-contrast: #06231a;

  --bg:           #0f1419;
  --surface:      #161c23;
  --surface-alt:  #1c242d;
  --surface-hover:#212a34;

  --text:         #e6eaef;
  --text-muted:   #a4b0bd;
  --text-faint:   #74808d;

  --border:       #262f3a;
  --border-strong:#323d4a;

  --code-bg:      #11171e;
  --code-text:    #e6eaef;
  --code-border:  #262f3a;

  /* AFL syntax highlighting (dark equivalents of the AmiBroker scheme:
     keywords / constants / reserved share the warm red family, functions stay
     blue, numbers get their own green so they read apart from calls). */
  --tok-comment: #6A9955;
  --tok-string:  #CE9178;
  --tok-keyword: #D16969;
  --tok-func:    #4FC1FF;
  --tok-const:   #D16969;
  --tok-prep:    #C586C0;
  --tok-number:  #B5CEA8;
  --tok-reserved:#D16969;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.55);
}

/* ============================================================================
   Base / reset
   ========================================================================== */
*, *::before, *::after { box-sizing: border-box; }

html { scroll-behavior: smooth; scroll-padding-top: calc(var(--topbar-h) + 16px); }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

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

img { max-width: 100%; height: auto; }

/* ============================================================================
   Top bar
   ========================================================================== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.topbar__brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  white-space: nowrap;
}
.topbar__brand:hover { color: var(--text); text-decoration: none; }
.topbar__logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px; height: 30px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 800;
  font-size: 0.95rem;
  flex: none;
}

/* Search */
.topbar__search { position: relative; flex: 1; max-width: 480px; margin: 0 auto; }
.search-input {
  width: 100%;
  height: 38px;
  padding: 0 14px 0 38px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%235b6675' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='8'/%3E%3Cpath d='m21 21-4.3-4.3'/%3E%3C/svg%3E") no-repeat 12px center;
  color: var(--text);
  font-family: inherit;
  font-size: 0.92rem;
}
.search-input::placeholder { color: var(--text-faint); }
.search-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0; right: 0;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  max-height: 60vh;
  overflow-y: auto;
  padding: 6px;
  z-index: 60;
}
.search-results[hidden] { display: none; }
.search-result {
  display: block;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.92rem;
}
.search-result:hover, .search-result.is-active {
  background: var(--accent-soft);
  color: var(--text);
  text-decoration: none;
}
.search-result__group {
  display: block;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  margin-top: 2px;
}
.search-empty { padding: 14px 10px; color: var(--text-faint); font-size: 0.9rem; }

.topbar__actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }

/* Icon buttons (theme toggle, hamburger) */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px; height: 38px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text); border-color: var(--border-strong); }
.icon-btn svg { width: 18px; height: 18px; }

.hamburger { display: none; }

/* Theme toggle shows sun in dark mode, moon in light mode */
.theme-toggle .icon-moon { display: inline-flex; }
.theme-toggle .icon-sun  { display: none; }
[data-theme="dark"] .theme-toggle .icon-moon { display: none; }
[data-theme="dark"] .theme-toggle .icon-sun  { display: inline-flex; }

/* ============================================================================
   Layout shell
   ========================================================================== */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  align-items: start;
}

/* ---- Sidebar ------------------------------------------------------------- */
.sidebar {
  position: sticky;
  top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  overflow-y: auto;
  padding: 18px 12px 40px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.nav-group { margin-bottom: 4px; }
.nav-group__toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  border: none;
  background: none;
  color: var(--text);
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  cursor: pointer;
  border-radius: var(--radius-sm);
}
.nav-group__toggle:hover { background: var(--surface-hover); }
.nav-group__chevron {
  width: 14px; height: 14px;
  flex: none;
  color: var(--text-faint);
  transition: transform .18s ease;
}
.nav-group.is-collapsed .nav-group__chevron { transform: rotate(-90deg); }
.nav-group.is-collapsed .nav-group__list { display: none; }

.nav-group__list { list-style: none; margin: 2px 0 8px; padding: 0; }
.nav-link {
  display: block;
  padding: 6px 10px 6px 22px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.9rem;
  border-left: 2px solid transparent;
}
.nav-link:hover { background: var(--surface-hover); color: var(--text); text-decoration: none; }
.nav-link.is-active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
  border-left-color: var(--accent);
}

/* ---- Main content -------------------------------------------------------- */
.main { min-width: 0; padding: 36px 40px 80px; }

.content-wrap {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 48px;
  max-width: calc(var(--measure) + var(--toc-w) + 48px);
  margin: 0 auto;
}
/* Right rail TOC appears only on wide screens */
@media (min-width: 1200px) {
  .content-wrap { grid-template-columns: minmax(0, 1fr) var(--toc-w); }
}

.article { max-width: var(--measure); min-width: 0; }

/* ---- Article typography -------------------------------------------------- */
.article h1 {
  font-size: 2.1rem;
  line-height: 1.2;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin: 0 0 .6em;
}
.article h2 {
  font-size: 1.45rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 2em 0 .7em;
  padding-bottom: .3em;
  border-bottom: 1px solid var(--border);
}
.article h3 { font-size: 1.15rem; font-weight: 700; margin: 1.8em 0 .6em; }
.article p { margin: 0 0 1.1em; color: var(--text); }
.article ul, .article ol { margin: 0 0 1.1em; padding-left: 1.4em; }
.article li { margin: .3em 0; }
.article strong { font-weight: 700; }
.article hr { border: none; border-top: 1px solid var(--border); margin: 2.4em 0; }

/* Inline code */
.article :not(pre) > code {
  font-family: var(--font-mono);
  font-size: 0.875em;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0.12em 0.4em;
  color: var(--text);
}

/* ---- Function signature block ------------------------------------------- */
.signature {
  display: block;
  background: var(--surface-alt);
  border: 1px solid var(--border-strong);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 14px 18px;
  margin: 0 0 1.4em;
  overflow-x: auto;
}
.signature code {
  font-family: var(--font-mono);
  font-size: 1rem;
  color: var(--text);
  white-space: pre;
}
.signature .fn { color: var(--accent); font-weight: 700; }

/* ---- Parameters table ---------------------------------------------------- */
.params {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 1.6em;
  font-size: 0.94rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.params thead th {
  text-align: left;
  background: var(--surface-alt);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.params tbody td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.params tbody tr:last-child td { border-bottom: none; }
.params tbody td:first-child {
  font-family: var(--font-mono);
  font-size: 0.88rem;
  color: var(--accent);
  font-weight: 600;
  white-space: nowrap;
}

/* ---- AFL / code blocks --------------------------------------------------- */
.code {
  position: relative;
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  border-radius: var(--radius);
  margin: 0 0 1.6em;
  overflow: hidden;
}
.code code {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  line-height: 1.6;
  color: var(--code-text);
  padding: 16px 18px;
  overflow-x: auto;
  white-space: pre;
}
.code.afl::before {
  content: "AFL";
  position: absolute;
  top: 8px; left: 14px;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--text-faint);
  pointer-events: none;
}
.code.afl code { padding-top: 30px; }

/* AFL syntax-highlighting token classes (added by highlightAfl in app.js) */
.code .tok-comment { color: var(--tok-comment); }
.code .tok-string  { color: var(--tok-string); }
.code .tok-keyword { color: var(--tok-keyword); font-weight: 700; }
.code .tok-reserved { color: var(--tok-reserved); font-weight: 700; }
.code .tok-func    { color: var(--tok-func); }
.code .tok-const   { color: var(--tok-const); font-weight: 700; }
.code .tok-prep    { color: var(--tok-prep); }
.code .tok-number  { color: var(--tok-number); }

/* Copy button (added by app.js) */
.copy-btn {
  position: absolute;
  top: 8px; right: 8px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 9px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text-muted);
  font-family: var(--font-ui);
  font-size: 0.74rem;
  font-weight: 600;
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s, background .15s, color .15s;
}
.code:hover .copy-btn, .copy-btn:focus-visible { opacity: 1; }
.copy-btn:hover { background: var(--surface-hover); color: var(--text); }
.copy-btn.is-copied { color: var(--accent); border-color: var(--accent); }

/* ---- Callouts ------------------------------------------------------------ */
.callout {
  position: relative;
  border: 1px solid var(--border);
  border-left: 4px solid var(--text-faint);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 14px 16px 14px 18px;
  margin: 0 0 1.6em;
}
.callout > :last-child { margin-bottom: 0; }
.callout__label {
  display: block;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 4px;
}
.callout-tip     { border-left-color: var(--accent); background: var(--accent-soft); }
.callout-tip     .callout__label { color: var(--accent); }
.callout-note    { border-left-color: #3b82f6; background: rgba(59,130,246,0.08); }
.callout-note    .callout__label { color: #3b82f6; }
.callout-warning { border-left-color: #f59e0b; background: rgba(245,158,11,0.10); }
.callout-warning .callout__label { color: #d97706; }
[data-theme="dark"] .callout-warning .callout__label { color: #fbbf24; }

/* ---- Figures / screenshots ----------------------------------------------- */
.screenshot {
  margin: 0 0 1.8em;
  padding: 0;
}
.screenshot img {
  display: block;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  background: var(--surface-alt);
}
.screenshot figcaption {
  margin-top: 10px;
  font-size: 0.85rem;
  color: var(--text-faint);
  text-align: center;
}

/* ---- Prev / next nav ----------------------------------------------------- */
.page-nav {
  display: flex;
  gap: 14px;
  margin-top: 56px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}
.page-nav a {
  flex: 1;
  display: block;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
.page-nav a:hover { border-color: var(--accent); box-shadow: var(--shadow-sm); text-decoration: none; }
.page-nav .page-nav__next { text-align: right; }
.page-nav__dir { display: block; font-size: 0.75rem; color: var(--text-faint); margin-bottom: 3px; }
.page-nav__title { font-weight: 600; color: var(--accent); }

/* ============================================================================
   On-this-page TOC (right rail)
   ========================================================================== */
.toc {
  position: sticky;
  top: calc(var(--topbar-h) + 36px);
  align-self: start;
  font-size: 0.85rem;
  display: none;
}
@media (min-width: 1200px) { .toc { display: block; } }
.toc[hidden] { display: none; }
.toc__title {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  margin-bottom: 10px;
}
.toc ul { list-style: none; margin: 0; padding: 0; border-left: 1px solid var(--border); }
.toc li { margin: 0; }
.toc a {
  display: block;
  padding: 4px 0 4px 14px;
  margin-left: -1px;
  border-left: 2px solid transparent;
  color: var(--text-muted);
}
.toc a:hover { color: var(--text); text-decoration: none; }
.toc a.is-active { color: var(--accent); border-left-color: var(--accent); font-weight: 600; }
.toc .toc-h3 a { padding-left: 28px; font-size: 0.82rem; }

/* ============================================================================
   Home page
   ========================================================================== */
.hero {
  text-align: center;
  padding: 56px 20px 40px;
  max-width: 760px;
  margin: 0 auto;
}
.hero__eyebrow {
  display: inline-block;
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 5px 12px;
  border-radius: 999px;
  margin-bottom: 18px;
}
.hero h1 {
  font-size: 2.8rem;
  line-height: 1.1;
  font-weight: 800;
  letter-spacing: -0.03em;
  margin: 0 0 .4em;
}
.hero p { font-size: 1.15rem; color: var(--text-muted); margin: 0 auto; max-width: 560px; }

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 18px;
  max-width: 960px;
  margin: 24px auto 0;
}
.card {
  display: block;
  padding: 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  box-shadow: var(--shadow-sm);
  transition: transform .15s, box-shadow .15s, border-color .15s;
}
.card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  border-color: var(--accent);
  text-decoration: none;
  color: var(--text);
}
.card__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px; height: 40px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--accent);
  margin-bottom: 14px;
}
.card__icon svg { width: 22px; height: 22px; }
.card__title { font-size: 1.05rem; font-weight: 700; margin: 0 0 6px; }
.card__desc { font-size: 0.9rem; color: var(--text-muted); margin: 0; }

/* ============================================================================
   Mobile / responsive
   ========================================================================== */
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 40;
  opacity: 0;
  visibility: hidden;
  transition: opacity .2s;
}
.scrim.is-visible { opacity: 1; visibility: visible; }

@media (max-width: 860px) {
  .hamburger { display: inline-flex; }
  .topbar__search { max-width: none; }
  .topbar__brand .topbar__brand-text { display: none; }

  .layout { grid-template-columns: minmax(0, 1fr); }

  .sidebar {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    width: 84vw;
    max-width: var(--sidebar-w);
    height: calc(100vh - var(--topbar-h));
    z-index: 45;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: var(--shadow-lg);
  }
  .sidebar.is-open { transform: translateX(0); }

  .main { padding: 24px 18px 64px; }
  .hero h1 { font-size: 2.1rem; }
}

@media (max-width: 520px) {
  .topbar { gap: 10px; padding: 0 12px; }
  .topbar__search { display: none; }
}
