:root {
  --bg: #131722;
  --bg-2: #181c27;
  --panel: #1e222d;
  --panel-2: #262b37;
  --border: #2a2e39;
  --border-2: #363a45;
  --text: #d1d4dc;
  --text-2: #b2b5be;
  --muted: #787b86;
  --blue: #2962ff;
  --blue-2: #1e53e5;
  --up: #089981;
  --down: #f23645;
  --pred: #ff9800;
  --pos-text: #089981;
  --neg-text: #f23645;
  --accent-text: #7ea1ff;
  --up-tag: #22c3a6;
  --down-tag: #ff6b77;
  --veil: rgba(19, 23, 34, 0.7);
  --float: rgba(30, 34, 45, 0.95);
  --glow: #1a1f2c;
  --shadow: rgba(0, 0, 0, 0.45);
  --backdrop: rgba(0, 0, 0, 0.55);
  --eq-line: #5b8cff;
  --eq-area: rgba(91, 140, 255, 0.10);
  --chart-grid: #232733;
  --radius: 6px;
  --font: "IBM Plex Sans Thai", -apple-system, "Segoe UI", Roboto, "Noto Sans Thai", sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
  color-scheme: dark;
}

/* Light theme (TradingView-like). Chosen with the theme button; stored per browser. */
:root[data-theme="light"] {
  --bg: #ffffff;
  --bg-2: #f8f9fd;
  --panel: #ffffff;
  --panel-2: #f0f3fa;
  --border: #e0e3eb;
  --border-2: #d1d4dc;
  --text: #131722;
  --text-2: #434651;
  --muted: #6a6d78;
  --blue-2: #1e53e5;
  --pred: #e67700;
  --pos-text: #067a66;
  --neg-text: #d1263a;
  --accent-text: #2962ff;
  --up-tag: #067a66;
  --down-tag: #d1263a;
  --veil: rgba(255, 255, 255, 0.75);
  --float: rgba(255, 255, 255, 0.96);
  --glow: #eef2fb;
  --shadow: rgba(19, 23, 34, 0.14);
  --backdrop: rgba(19, 23, 34, 0.35);
  --eq-line: #2962ff;
  --eq-area: rgba(41, 98, 255, 0.08);
  --chart-grid: #eef0f5;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
svg { display: block; }

.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 0.92em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.pos { color: var(--pos-text) !important; }
.neg { color: var(--neg-text) !important; }
.row { display: flex; align-items: center; }
.gap { gap: 8px; }
.grow { flex: 1; }
.w100 { width: 100%; }
.mt { margin-top: 18px !important; }
.mt-s { margin-top: 10px; }

/* ---------- top bar ---------- */
.topbar {
  height: 48px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--text); white-space: nowrap; }
.brand svg { color: var(--blue); }
.brand b { color: var(--blue); font-weight: 600; }
.session-info { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; }
.nav { display: flex; gap: 4px; }
.nav-link { padding: 6px 12px; border-radius: var(--radius); text-decoration: none; color: var(--text-2); }
.nav-link:hover { background: var(--panel); color: var(--text); }
.nav-link.active { color: var(--text); background: var(--panel); }

.pill { display: inline-block; padding: 1px 7px; border-radius: 10px; background: var(--panel-2); color: var(--text-2); font-size: 12px; }
.badge { padding: 2px 8px; border-radius: 10px; font-size: 12px; }
.badge-live { background: rgba(41, 98, 255, 0.18); color: var(--accent-text); }
.badge-muted { background: var(--panel-2); color: var(--muted); }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 340px;
  height: calc(100vh - 48px);
  height: calc(100dvh - 48px);
}
.toolbar {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 0;
  overflow-y: auto;
  background: var(--bg);
}
.tool {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  background: transparent; border: 0; border-radius: var(--radius);
  color: var(--text-2); cursor: pointer;
}
.tool svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.tool svg circle { fill: var(--bg); }
.tool[data-tool="cursor"] svg path { fill: currentColor; stroke-width: 1; }
.tool:hover { background: var(--panel); color: var(--text); }
.tool.active { background: rgba(41, 98, 255, 0.16); color: var(--accent-text); }
.tool-pred { color: var(--pred); }
.tool-pred.active { background: rgba(255, 152, 0, 0.18); color: var(--pred); }
.tool-sep { width: 24px; height: 1px; background: var(--border); margin: 4px 0; flex: none; }
body.readonly .tool:not([data-tool="cursor"]):not([data-action="magnet"]) { opacity: 0.35; pointer-events: none; }

.chart-col { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chart-wrap { position: relative; flex: 1; min-height: 0; }
.chart { position: absolute; inset: 0; }
body.pred-mode .chart { cursor: crosshair; }
.chart-empty {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center;
  background: radial-gradient(ellipse at center, var(--glow) 0%, var(--bg) 70%);
  padding: 24px; z-index: 2;
}
.chart-empty h2 { margin: 0 0 8px; font-weight: 600; }
.chart-empty p { margin: 4px 0; color: var(--text-2); }
.chart-loading {
  position: absolute; inset: 0; display: flex; gap: 10px; align-items: center; justify-content: center;
  background: var(--veil); z-index: 5; color: var(--text-2);
}
.spinner { width: 20px; height: 20px; border: 2px solid var(--border-2); border-top-color: var(--blue); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.mode-hint {
  position: absolute; top: 34px; left: 50%; transform: translateX(-50%);
  background: var(--float); border: 1px solid var(--border-2); border-radius: 20px;
  padding: 4px 14px; font-size: 12px; color: var(--text); z-index: 3; pointer-events: none; white-space: nowrap;
  max-width: calc(100% - 24px); overflow: hidden; text-overflow: ellipsis;
}
body.pred-mode .mode-hint { border-color: var(--pred); color: var(--pred); }
.ro-banner {
  position: absolute; top: 34px; right: 72px; z-index: 3; pointer-events: none;
  background: rgba(255, 152, 0, 0.12); border: 1px solid rgba(255, 152, 0, 0.5); color: var(--pred);
  border-radius: 20px; padding: 3px 12px; font-size: 12px;
}

/* ---------- replay bar ---------- */
.replay-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 6px 10px; border-top: 1px solid var(--border); background: var(--bg-2);
}
.rb-group { display: flex; align-items: center; gap: 6px; }
.rb-info { display: flex; align-items: center; gap: 10px; flex: 1; justify-content: center; min-width: 0; white-space: nowrap; }
.save-status { font-size: 12px; color: var(--muted); }
.btn-icon { width: 34px; height: 30px; padding: 0; display: grid; place-items: center; }
.btn-icon svg { width: 16px; height: 16px; fill: currentColor; }
.ic-pause { display: none !important; }
body.playing .ic-play { display: none !important; }
body.playing .ic-pause { display: block !important; }
body.playing #btn-play { background: var(--blue); border-color: var(--blue); color: #fff; }

/* ---------- buttons / inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 32px; padding: 0 12px; border-radius: var(--radius);
  border: 1px solid var(--border-2); background: var(--panel); color: var(--text);
  cursor: pointer; text-decoration: none; white-space: nowrap; user-select: none;
}
.btn:hover:not(:disabled) { background: var(--panel-2); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-sm { height: 26px; padding: 0 10px; font-size: 12px; }
.btn-primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--blue-2); }
.btn-ghost { background: transparent; }
.btn-ghost.on { border-color: var(--pred); color: var(--pred); }
.btn-buy { background: var(--up); border-color: var(--up); color: #fff; height: 40px; font-weight: 600; }
.btn-buy:hover:not(:disabled) { background: #07806c; }
.btn-sell { background: var(--down); border-color: var(--down); color: #fff; height: 40px; font-weight: 600; }
.btn-sell:hover:not(:disabled) { background: #d42f3d; }
.btn-buy .mono, .btn-sell .mono { font-weight: 400; opacity: 0.9; }
.btn-finish { border-color: rgba(255, 152, 0, 0.6); color: var(--pred); background: transparent; }
.btn-finish:hover:not(:disabled) { background: rgba(255, 152, 0, 0.12) !important; }
.icon-btn { background: transparent; border: 0; color: var(--muted); cursor: pointer; font-size: 16px; }

.seg { display: inline-flex; border: 1px solid var(--border-2); border-radius: var(--radius); overflow: hidden; }
.seg button { background: transparent; border: 0; padding: 0 10px; height: 30px; cursor: pointer; color: var(--text-2); border-right: 1px solid var(--border-2); }
.seg button:last-child { border-right: 0; }
.seg button.on { background: var(--panel-2); color: var(--text); }
.seg button:disabled { opacity: 0.45; cursor: not-allowed; }
.seg-wide { display: flex; }
.seg-wide button { flex: 1; }
/* timeframe picker: 2 rows of 5 */
#tf { display: grid; grid-template-columns: repeat(5, 1fr); }
#tf button { border-right: 1px solid var(--border-2); border-bottom: 1px solid var(--border-2); padding: 0; }
#tf button:nth-child(5n) { border-right: 0; }
#tf button:nth-last-child(-n + 5) { border-bottom: 0; }
#tf button:disabled { text-decoration: line-through; }

.input {
  width: 100%; height: 32px; padding: 0 9px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border-2); border-radius: var(--radius);
  font: inherit; outline: none;
}
.input:focus { border-color: var(--blue); }
.input:disabled { opacity: 0.5; }
.input-sm { height: 26px; width: 110px; font-size: 12px; }
select.input { padding-right: 4px; }
input[type="datetime-local"].input { font-family: var(--mono); font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; min-width: 0; }
.field > span { font-size: 12px; color: var(--muted); }
.tp-lbl { color: var(--up) !important; }
.sl-lbl { color: var(--down) !important; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid2 .field { margin-bottom: 6px; }
.hint { color: var(--muted); font-size: 13px; margin: 4px 0; }
.hint-box { height: 32px; display: flex; align-items: center; color: var(--text-2); font-size: 12px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chk { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.chk input { accent-color: var(--blue); width: 15px; height: 15px; }
kbd { background: var(--panel-2); border: 1px solid var(--border-2); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; font-family: var(--mono); font-size: 11px; }

/* ---------- side panel ---------- */
.side { border-left: 1px solid var(--border); overflow-y: auto; background: var(--bg); }
.panel { padding: 12px 14px; border-bottom: 1px solid var(--border); }
.panel-title { margin: 0 0 10px; font-size: 13px; font-weight: 600; color: var(--text); display: flex; align-items: center; gap: 8px; }
.panel h4 { margin: 14px 0 6px; font-size: 12px; color: var(--muted); font-weight: 500; text-transform: uppercase; letter-spacing: 0.03em; }
.tag-pred { color: var(--pred); font-weight: 400; font-size: 12px; }
.acct { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.acct > div { display: flex; flex-direction: column; min-width: 0; }
.acct .lbl { font-size: 11px; color: var(--muted); }
.acct b { font-family: var(--mono); font-size: 13px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; }

.acct-sub { margin: 6px 0 0; }
.pf-card { border: 1px solid var(--border-2); border-radius: 8px; padding: 10px 12px; background: var(--bg-2); }
.pf-main { display: flex; flex-direction: column; gap: 1px; }
.pf-main .lbl { font-size: 11px; color: var(--muted); }
.pf-main b { font-size: 20px; font-weight: 500; }
.pf-actions { display: flex; gap: 6px; margin-top: 8px; }
.nav-btn { background: transparent; border: 0; cursor: pointer; font: inherit; }
.theme-btn { display: inline-flex; align-items: center; gap: 6px; }
.theme-btn svg { flex: none; }

/* ---------- equity curve ---------- */
.pf-section { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 14px; margin-bottom: 16px; }
.pf-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.pf-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.pf-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin-bottom: 12px; }
.pf-stats .lbl { font-size: 12px; color: var(--muted); }
.pf-stats .val { font-family: var(--mono); font-size: 18px; }
.eq-wrap { position: relative; height: 260px; }
.eq-wrap svg { width: 100%; height: 100%; display: block; overflow: visible; }
.eq-tip {
  position: absolute; pointer-events: none; background: var(--panel-2); border: 1px solid var(--border-2);
  border-radius: 6px; padding: 6px 9px; font-size: 12px; line-height: 1.5; white-space: nowrap; z-index: 2;
  box-shadow: 0 6px 18px var(--shadow);
}
.eq-empty { height: 100%; display: grid; place-items: center; color: var(--muted); text-align: center; }

.pos-box { margin-top: 10px; padding: 10px; border: 1px solid var(--border-2); border-radius: var(--radius); background: var(--bg-2); }
.pos-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.pos-head b { margin-left: auto; }
.pos-grid { display: grid; grid-template-columns: auto 1fr auto 1fr; gap: 2px 8px; margin-bottom: 8px; color: var(--muted); }
.side-tag { font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 3px; }
.side-tag.buy { background: rgba(8, 153, 129, 0.16); color: var(--up-tag); }
.side-tag.sell { background: rgba(242, 54, 69, 0.14); color: var(--down-tag); }

.list { display: flex; flex-direction: column; gap: 6px; }
.list-item {
  position: relative; display: block; padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius);
  text-decoration: none; background: var(--bg-2);
}
.list-item:hover { border-color: var(--blue); }
.li-go { position: absolute; right: 10px; top: 8px; color: var(--accent-text); font-size: 12px; }
.trade-sum { display: flex; gap: 10px; align-items: center; color: var(--muted); padding-bottom: 6px; border-bottom: 1px dashed var(--border); margin-bottom: 2px; }
.trade-sum b { margin-left: auto; font-family: var(--mono); }
.trade-row { display: grid; grid-template-columns: auto 1fr auto; grid-template-rows: auto auto; gap: 0 8px; align-items: center; padding: 4px 0; border-bottom: 1px solid var(--border); }
.trade-row .side-tag { grid-row: span 2; }
.trade-row b { grid-row: span 2; grid-column: 3; grid-row-start: 1; }
.trade-row > span:nth-child(3) { grid-column: 2; }

.ind-list { display: flex; flex-direction: column; gap: 6px; }
.ind { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 26px; }

.kv { width: 100%; border-collapse: collapse; font-size: 13px; }
.kv td { padding: 4px 0; border-bottom: 1px solid var(--border); vertical-align: top; }
.kv td:first-child { color: var(--muted); padding-right: 10px; }
.kv td:last-child { text-align: right; }
.res-hit { display: inline-block; padding: 3px 10px; border-radius: 12px; font-weight: 600; margin-bottom: 6px; }
.res-hit.hit { background: rgba(8, 153, 129, 0.16); color: var(--up-tag); }
.res-hit.miss { background: rgba(242, 54, 69, 0.14); color: var(--down-tag); }

/* ---------- modal + toast ---------- */
.modal-backdrop { position: fixed; inset: 0; background: var(--backdrop); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal { width: 100%; max-width: 420px; background: var(--panel); border: 1px solid var(--border-2); border-radius: 10px; box-shadow: 0 20px 60px var(--shadow); max-height: calc(100vh - 32px); display: flex; flex-direction: column; }
.modal-wide { max-width: 520px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 15px; }
.modal-body { padding: 14px 16px; overflow-y: auto; }
.modal-body p { margin: 0 0 8px; }
.modal-body h4 { margin: 14px 0 6px; font-size: 12px; color: var(--muted); font-weight: 500; }
.modal-body h4:first-child { margin-top: 0; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); flex-wrap: wrap; }

.toasts { position: fixed; bottom: 60px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; z-index: 200; align-items: center; pointer-events: none; width: max-content; max-width: calc(100vw - 32px); }
.toast { background: var(--panel-2); border: 1px solid var(--border-2); border-left: 3px solid var(--blue); padding: 8px 14px; border-radius: var(--radius); box-shadow: 0 8px 24px var(--shadow); transition: opacity 0.3s, transform 0.3s; font-size: 13px; }
.toast-ok { border-left-color: var(--up); }
.toast-error { border-left-color: var(--down); }
.toast.hide { opacity: 0; transform: translateY(6px); }

/* ---------- history page ---------- */
.page { max-width: 1200px; margin: 0 auto; padding: 16px; }
.page-body { height: auto; min-height: 100%; overflow: auto; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 14px; }
.filters .field { margin: 0; min-width: 160px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 16px; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; }
.stat .lbl { font-size: 12px; color: var(--muted); }
.stat .val { font-size: 22px; font-family: var(--mono); margin-top: 2px; }
.stat .sub { font-size: 12px; color: var(--muted); }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; white-space: nowrap; }
table.data th { text-align: left; font-weight: 500; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--border); background: var(--bg-2); position: sticky; top: 0; }
table.data td { padding: 8px 10px; border-bottom: 1px solid var(--border); }
table.data tr.click { cursor: pointer; }
table.data tr.click:hover td { background: var(--panel); }
table.data td.num, table.data th.num { text-align: right; }
.empty { padding: 30px; text-align: center; color: var(--muted); }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 44px minmax(0, 1fr) 300px; }
}
/* ---------- hypothesis notes ---------- */
textarea.input { height: auto; min-height: 56px; padding: 6px 8px; resize: vertical; line-height: 1.45; font-family: inherit; width: 100%; }
.note-form { display: flex; flex-direction: column; gap: 6px; }
.note { border: 1px solid var(--border); border-left: 3px solid var(--nc); border-radius: var(--radius); padding: 7px 8px; display: flex; flex-direction: column; gap: 5px; background: var(--panel); }
.note + .note { margin-top: 6px; }
.note-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.note-no { background: var(--nc); color: #fff; border-radius: 3px; padding: 0 5px; font-size: 11px; font-weight: 600; }
.note-del { margin-left: auto; font-size: 13px; }
.note-hyp { white-space: pre-wrap; word-break: break-word; }
.note-move { color: var(--text-2); }
.note-res { border-left: 2px solid; padding-left: 7px; display: flex; flex-direction: column; gap: 3px; }
.note-res-text { white-space: pre-wrap; word-break: break-word; color: var(--text-2); }
.verdict { font-weight: 600; font-size: 12px; }
.note-verdicts { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.note-verdicts .btn { color: var(--vc); border-color: color-mix(in srgb, var(--vc) 45%, transparent); }
.note-verdicts .btn.on { background: color-mix(in srgb, var(--vc) 18%, transparent); }
.note-actions { display: flex; gap: 10px; }
.link-btn { background: none; border: 0; padding: 0; color: var(--muted); font-size: 12px; cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.link-btn:hover { color: var(--text); }
.notes-row td { background: var(--bg-2); white-space: normal; padding: 8px 14px 12px; }
.notes-row .note-mini-list { max-width: 900px; }
.note-mini-list { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.note-mini { border-left: 3px solid var(--nc); padding: 3px 0 3px 8px; display: flex; flex-direction: column; gap: 2px; }

@media (max-width: 820px) {
  .topbar { gap: 8px; padding: 0 8px; }
  .brand span { display: none; }
  .hide-sm { display: none; }
  .layout {
    display: flex; flex-direction: column; height: auto; min-height: calc(100dvh - 48px);
  }
  .toolbar {
    flex-direction: row; overflow-x: auto; overflow-y: hidden; border-right: 0; border-bottom: 1px solid var(--border);
    padding: 4px 6px; order: 0; flex: none;
  }
  .tool-sep { width: 1px; height: 24px; margin: 0 4px; }
  .chart-col { order: 1; flex: none; }
  .chart-wrap { height: 62vh; height: 62dvh; flex: none; }
  .side { order: 2; border-left: 0; overflow: visible; }
  .rb-info { order: 3; flex-basis: 100%; justify-content: flex-start; }
  .ro-banner { right: 8px; top: 8px; }
  .mode-hint { top: 8px; white-space: normal; text-align: center; width: max-content; }
}
