:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #e2e5ea;
  --text: #1e2430;
  --text-muted: #6b7482;
  --accent: #2f6feb;
  --accent-text: #ffffff;
  --danger: #c0392b;
  --ok: #1e9e5a;
  --menubar-h: 5vh;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1a1e26;
    --surface-2: #20252f;
    --border: #2b323d;
    --text: #e6e9ef;
    --text-muted: #97a1b0;
    --accent: #4d86f7;
    --danger: #e05a4b;
    --ok: #3ec27e;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}

h2, h3 { margin: 0; font-weight: 600; }
.muted { color: var(--text-muted); font-weight: 400; font-size: 0.85em; }

/* ---------- menu bar ---------- */
#menubar {
  height: var(--menubar-h);
  min-height: 34px;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
#menubar .brand { font-weight: 700; letter-spacing: 0.02em; }
.ticker-picker { display: flex; align-items: center; gap: 8px; font-size: 0.9em; color: var(--text-muted); }
.ticker-picker select {
  font: inherit;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  min-width: 160px;
}

/* ---------- workspace ---------- */
#workspace {
  height: calc(100vh - var(--menubar-h));
  display: flex;
  overflow: hidden;
}

#strategy-list {
  width: 20vw;
  min-width: 180px;
  border-right: 1px solid var(--border);
  background: var(--surface);
  overflow-y: auto;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
}
#brand-credit {
  margin-top: auto;
  padding-top: 14px;
  font-size: 0.8em;
  color: var(--text-muted);
  letter-spacing: 0.02em;
}
#strategy-list h2 { font-size: 0.8em; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-muted); margin-bottom: 10px; }
#strategy-items { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
#strategy-items li {
  padding: 9px 10px;
  border-radius: 7px;
  cursor: pointer;
  border: 1px solid transparent;
  line-height: 1.3;
}
#strategy-items li:hover { background: var(--surface-2); }
#strategy-items li.active { background: color-mix(in srgb, var(--accent) 14%, transparent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
#strategy-items li.disabled { color: var(--text-muted); cursor: not-allowed; }
#strategy-items li .name { font-weight: 600; }
#strategy-items li .desc { display: block; font-size: 0.82em; color: var(--text-muted); margin-top: 2px; }
#strategy-items li.muted { cursor: default; }

/* ---------- detail (params + results) ---------- */
#detail { width: 80vw; flex: 1; display: flex; flex-direction: column; overflow: hidden; }

#params-panel {
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: 14px 18px;
  max-height: 42vh;
  overflow-y: auto;
}
#params-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 12px; }
#params-title { font-size: 1.05em; }
#params-doc { margin: 3px 0 0; max-width: 60ch; }

#run-btn {
  font: inherit;
  font-weight: 600;
  padding: 9px 18px;
  border: 0;
  border-radius: 7px;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
#run-btn:disabled { opacity: 0.45; cursor: not-allowed; }

#params-form {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px 18px;
}
.param-row { display: flex; flex-direction: column; gap: 4px; }
.param-row label { font-size: 0.85em; color: var(--text-muted); display: flex; align-items: baseline; gap: 6px; }
.param-row label .ptype { font-size: 0.78em; opacity: 0.6; }
.param-row input[type="text"],
.param-row input[type="number"],
.param-row input[type="date"],
.param-row select {
  font: inherit;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  width: 100%;
}
.param-row.bool { flex-direction: row; align-items: center; gap: 8px; }
.param-row.bool label { order: 2; }
.param-row.changed input,
.param-row.changed select { border-color: var(--accent); }

/* ---------- results ---------- */
#results { flex: 1; overflow-y: auto; padding: 18px; }
#results-status.placeholder { color: var(--text-muted); }
#results-status.error {
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
  padding: 10px 12px;
  border-radius: 7px;
  white-space: pre-wrap;
}

.meta-grid { display: flex; flex-wrap: wrap; gap: 8px 22px; margin-bottom: 18px; font-size: 0.88em; }
.meta-grid div span { color: var(--text-muted); margin-right: 6px; }

.block { background: var(--surface); border: 1px solid var(--border); border-radius: 9px; padding: 14px 16px; margin-bottom: 16px; }
.block h3 { font-size: 0.95em; margin-bottom: 10px; }
.block-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.block-head h3 { margin: 0; }
.chart-tools { display: flex; gap: 6px; }
.chart-tools select,
.chart-tools button {
  font: inherit;
  font-size: 0.82em;
  padding: 3px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
}
.chart-tools button:hover { background: var(--bg); }
.chart { width: 100%; height: 380px; }
.block:fullscreen { background: var(--surface); padding: 18px; overflow: auto; }
.block:fullscreen #chart-price { height: calc(100vh - 150px); }
.chart-legend { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; font-size: 0.8em; color: var(--text-muted); margin-bottom: 8px; min-height: 1.2em; }
.chart-legend b { color: var(--text); font-weight: 600; }
.chart-legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }

.table-scroll { overflow-x: auto; max-height: 460px; overflow-y: auto; }
table { border-collapse: collapse; width: 100%; font-size: 0.86em; }
th, td { text-align: right; padding: 5px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th:first-child, td:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); }
thead th { position: sticky; top: 0; background: var(--surface-2); z-index: 1; font-weight: 600; }
tbody tr:hover td { background: var(--surface-2); }
td.pos { color: var(--ok); }
td.neg { color: var(--danger); }

/* ---------- loading overlay ---------- */
#loading-overlay {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--bg) 70%, transparent);
  backdrop-filter: blur(2px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  z-index: 1000;
}
#loading-overlay p { margin: 0; color: var(--text-muted); font-weight: 500; }
.spinner {
  width: 38px;
  height: 38px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

[hidden] { display: none !important; }
