:root {
  --bg: #131722;
  --panel: #1e222d;
  --panel-2: #252932;
  --border: #2a2e39;
  --border-2: #363a45;
  --text: #d1d4dc;
  --muted: #787b86;
  --accent: #2962ff;
  --green: #26a69a;
  --green-soft: rgba(38,166,154,.15);
  --red: #ef5350;
  --red-soft: rgba(239,83,80,.15);
  --entry: #2962ff;
  --sl: #ef5350;
  --tp: #26a69a;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 13px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* dvh tracks the viewport as browser chrome shows and hides. svh would size
     to the chrome-expanded height and leave dead space when it collapses. */
  height: 100vh;
  height: 100dvh;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

/* ---------------- top bar ---------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  height: 52px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 7px; font-weight: 600; font-size: 15px; }
.brand-mark { color: var(--accent); font-size: 18px; }
.brand-name .accent { color: var(--accent); }

.symbol-box { display: flex; gap: 6px; position: relative; }
.picker-btn { padding: 7px 9px; font-size: 11px; line-height: 1; }

/* ---------------- symbol picker ---------------- */
/* `display:flex` below would otherwise beat the UA's `[hidden]{display:none}`,
   leaving the panel permanently on screen. */
.picker[hidden] { display: none; }
.picker {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 60;
  width: 380px; max-height: 460px; display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--border-2); border-radius: 8px;
  box-shadow: 0 12px 32px rgba(0,0,0,.55); overflow: hidden;
}
.picker-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px 0; font-size: 11px; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .6px;
}
.picker-close {
  background: transparent; border: none; color: var(--muted); cursor: pointer;
  font-size: 13px; line-height: 1; padding: 3px 5px; border-radius: 4px;
}
.picker-close:hover { background: var(--panel-2); color: var(--text); }
.picker-search {
  margin: 10px; padding: 8px 10px; background: var(--panel-2);
  border: 1px solid var(--border-2); border-radius: 5px; color: var(--text);
  outline: none; font-size: 13px; font-family: var(--font);
}
.picker-search:focus { border-color: var(--accent); }
.picker-sectors {
  display: flex; flex-wrap: wrap; gap: 4px; padding: 0 10px 8px;
  border-bottom: 1px solid var(--border);
}
.sector-chip {
  background: var(--panel-2); border: 1px solid transparent; color: var(--muted);
  font-size: 11px; font-weight: 600; padding: 3px 8px; border-radius: 20px; cursor: pointer;
}
.sector-chip:hover { color: var(--text); }
.sector-chip.active { background: var(--accent); color: #fff; }
.picker-list { flex: 1; overflow-y: auto; padding: 6px; }
.picker-list::-webkit-scrollbar { width: 8px; }
.picker-list::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 4px; }
.pick-row {
  display: flex; align-items: center; gap: 10px; padding: 7px 9px;
  border-radius: 5px; cursor: pointer;
}
.pick-row:hover, .pick-row.active { background: var(--panel-2); }
.pick-tkr { font-weight: 700; min-width: 58px; font-size: 12px; }
.pick-name { flex: 1; color: var(--muted); font-size: 12px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; }
.pick-sec { font-size: 10px; color: var(--muted); background: var(--panel-2);
  padding: 2px 6px; border-radius: 10px; }
.pick-row:hover .pick-sec { background: var(--border-2); }
.picker-foot { padding: 7px 12px; border-top: 1px solid var(--border);
  font-size: 11px; color: var(--muted); }
.picker-empty { text-align: center; color: var(--muted); font-size: 12px; padding: 20px 0; }
#symbol-input {
  width: 90px; text-transform: uppercase; font-weight: 600; letter-spacing: .5px;
  background: var(--panel-2); border: 1px solid var(--border-2); color: var(--text);
  border-radius: 5px; padding: 7px 9px; outline: none; font-size: 13px;
}
#symbol-input:focus { border-color: var(--accent); }

.tf-group { display: flex; gap: 2px; background: var(--panel-2); padding: 3px; border-radius: 6px; }
.tf {
  background: transparent; border: none; color: var(--muted); cursor: pointer;
  padding: 5px 10px; border-radius: 4px; font-size: 12px; font-weight: 600;
}
.tf:hover { color: var(--text); }
.tf.active { background: var(--accent); color: #fff; }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.src-pill {
  font-size: 11px; color: var(--muted); background: var(--panel-2);
  padding: 4px 9px; border-radius: 20px; border: 1px solid var(--border);
  max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.acct { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.2; }
.acct-label { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.acct-val { font-weight: 700; font-size: 14px; }

/* ---------------- buttons ---------------- */
.btn {
  border: none; border-radius: 5px; cursor: pointer; font-family: var(--font);
  font-weight: 600; font-size: 12px; padding: 7px 12px; color: var(--text);
  background: var(--panel-2); transition: background .12s, opacity .12s;
}
.btn:hover { background: var(--border-2); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #1e53e5; }
.btn-ghost { background: transparent; border: 1px solid var(--border-2); }
.btn-ghost:hover { background: var(--panel-2); }
.btn-xs { padding: 3px 8px; font-size: 11px; }
.btn-danger { background: var(--red); color: #fff; width: 100%; }
.btn-danger:hover { background: #e53935; }
.btn-place { width: 100%; padding: 11px; font-size: 13px; color: #fff; background: var(--green); }
.btn-place.short { background: var(--red); }
.cancel-btn { width: 100%; margin-top: 7px; }
.cancel-btn[hidden] { display: none; }

/* ---------------- layout ---------------- */
.layout { flex: 1; display: flex; min-height: 0; }
.chart-col { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chart-wrap { flex: 1; min-height: 0; display: flex; position: relative; }
.draw-tools {
  width: 38px; flex-shrink: 0; display: flex; flex-direction: column; gap: 4px;
  padding: 6px 4px; background: var(--panel); border-right: 1px solid var(--border);
}
.draw-tool {
  background: transparent; border: 1px solid transparent; color: var(--muted);
  border-radius: 5px; cursor: pointer; font-size: 15px; padding: 5px 0; line-height: 1;
}
.draw-tool:hover { background: var(--panel-2); color: var(--text); }
.draw-tool[aria-pressed="true"] { background: var(--accent); color: #fff; }

.chart { flex: 1; min-height: 0; position: relative; cursor: crosshair; }

/* ---------------- drawings ---------------- */
.draw-layer { position: absolute; inset: 0; z-index: 2; pointer-events: none; }
.draw-rect {
  position: absolute; pointer-events: auto; touch-action: none;
  background: rgba(41,98,255,.12); border: 1px solid rgba(41,98,255,.65);
  border-radius: 2px;
}
.draw-rect:hover { background: rgba(41,98,255,.2); }
.draw-rect-x {
  position: absolute; top: -9px; right: -9px; width: 18px; height: 18px;
  border-radius: 50%; border: none; cursor: pointer; font-size: 9px; line-height: 1;
  background: var(--panel-2); color: var(--text); opacity: 0; transition: opacity .12s;
  display: flex; align-items: center; justify-content: center;
}
.draw-rect:hover .draw-rect-x { opacity: 1; }
.draw-rect-x:hover { background: var(--red); color: #fff; }
.chart-legend {
  position: absolute; top: 8px; left: 10px; z-index: 4; pointer-events: none;
  display: flex; flex-wrap: wrap; gap: 14px; align-items: center;
  font-size: 12px; font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 2px rgba(0,0,0,.6);
}
.chart-legend .lg-title { font-weight: 700; color: var(--text); }
.chart-legend .lg-ohlc { font-weight: 600; }
.chart-legend .lg-ma { font-weight: 600; }

/* ---------------- position tool (TradingView style) ---------------- */
.pos-tool { position: absolute; inset: 0; z-index: 3; pointer-events: none; }
/* Zones are invisible at rest and only tint while their handle is dragged,
   matching TradingView's position tool. */
.pt-zone {
  position: absolute; pointer-events: none; background: transparent;
  transition: background .1s linear;
}
.pt-reward[data-live="1"] { background: rgba(38,166,154,.22); }
.pt-risk[data-live="1"]   { background: rgba(239,83,80,.22); }

.pt-handle {
  position: absolute; transform: translateY(-50%); display: flex; align-items: stretch;
  pointer-events: auto; cursor: ns-resize; border-radius: 3px; overflow: hidden;
  font-size: 11px; font-weight: 700; line-height: 1; user-select: none;
  box-shadow: 0 2px 8px rgba(0,0,0,.45); white-space: nowrap;
  /* Claim the gesture so dragging a level never scrolls the page instead. */
  touch-action: none;
  -webkit-user-select: none;
}
.pt-handle > span { padding: 4px 7px; display: flex; align-items: center; }
.pt-qty { background: rgba(19,23,34,.92); color: var(--text);
  border-right: 1px solid rgba(255,255,255,.12); }
.pt-val { background: rgba(19,23,34,.92); color: var(--text); }
.pt-money { color: #fff; }
.pt-entry .pt-val { color: #fff; background: var(--entry); }
.pt-entry[data-state="short"] .pt-val { background: var(--red); }
.pt-entry[data-state="long"] .pt-val { background: var(--green); }
.pt-tp .pt-val, .pt-tp .pt-money { background: var(--tp); color: #fff; }
.pt-sl .pt-val, .pt-sl .pt-money { background: var(--sl); color: #fff; }
.pt-handle:hover { filter: brightness(1.15); }

/* A filled trade's tool fades back so it stops competing with price action.
   Only opacity changes — the levels stay exactly where they are. */
.pos-tool[data-filled="1"] .pt-handle { opacity: .45; transition: opacity .12s; }
.pos-tool[data-filled="1"] .pt-handle:hover { opacity: 1; }

.replay-bar {
  height: 48px; flex-shrink: 0; display: flex; align-items: center; gap: 8px;
  padding: 0 12px; background: var(--panel); border-top: 1px solid var(--border);
}
.start-at { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 11px; }
.start-at input {
  background: var(--panel-2); border: 1px solid var(--border-2); color: var(--text);
  border-radius: 5px; padding: 5px 7px; outline: none; font-size: 12px;
  font-family: var(--font); color-scheme: dark;
}
.start-at input:focus { border-color: var(--accent); }

.speed { display: flex; align-items: center; gap: 7px; color: var(--muted); font-size: 11px; }
.speed input { accent-color: var(--accent); }
.progress-wrap { margin-left: auto; display: flex; align-items: center; gap: 10px; min-width: 200px; }
.progress { flex: 1; height: 5px; background: var(--panel-2); border-radius: 3px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); transition: width .1s; }
.bar-counter { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 74px; text-align: right; }

/* ---------------- side ---------------- */
.side {
  width: 320px; flex-shrink: 0; background: var(--bg); border-left: 1px solid var(--border);
  overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 12px;
}
.side::-webkit-scrollbar { width: 8px; }
.side::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 4px; }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 12px; }
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  font-weight: 700; font-size: 12px; text-transform: uppercase; letter-spacing: .6px;
  color: var(--muted); margin-bottom: 12px;
}
.last-price { color: var(--text); font-size: 13px; font-weight: 700; letter-spacing: 0; text-transform: none; }

.side-toggle { display: flex; gap: 4px; background: var(--panel-2); padding: 3px; border-radius: 6px; margin-bottom: 12px; }
.st-btn { flex: 1; background: transparent; border: none; color: var(--muted); font-weight: 700; padding: 8px; border-radius: 4px; cursor: pointer; }
.st-long.active { background: var(--green); color: #fff; }
.st-short.active { background: var(--red); color: #fff; }

.field-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 9px; }
.field-row label { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 7px; }
.field-row input, .field-row select {
  width: 130px; background: var(--panel-2); border: 1px solid var(--border-2); color: var(--text);
  border-radius: 5px; padding: 7px 9px; outline: none; font-size: 13px; font-family: var(--font);
}
.field-row input:focus, .field-row select:focus { border-color: var(--accent); }
.dot { width: 9px; height: 9px; border-radius: 2px; display: inline-block; }
.dot-entry { background: var(--entry); }
.dot-sl { background: var(--sl); }
.dot-tp { background: var(--tp); }

.qty-wrap { display: flex; gap: 4px; align-items: center; }
.qty-wrap #qty-input { width: 74px; }
.qty-wrap #qty-unit { width: 78px; }
.qty-wrap #risk-pct { width: 52px; text-align: center; background: var(--panel-2);
  border: 1px solid var(--border-2); color: var(--text); border-radius: 5px;
  padding: 7px 4px; outline: none; font-size: 12px; font-family: var(--font); }
.qty-wrap #risk-pct:focus { border-color: var(--accent); }
.qty-wrap #size-btn { white-space: nowrap; padding: 6px 8px; }
.pct-sign { color: var(--muted); font-size: 12px; }
.size-note { font-size: 11px; color: var(--muted); text-align: right; margin: -4px 0 8px; }
.buying-power { font-size: 11px; color: var(--muted); text-align: right;
  margin: -4px 0 9px; font-variant-numeric: tabular-nums; }
.buying-power b { color: var(--text); }
.buying-power.over b { color: var(--red); }

.metrics { display: flex; gap: 8px; margin: 12px 0; }
.metrics > div {
  flex: 1; background: var(--panel-2); border-radius: 6px; padding: 8px; text-align: center;
}
.metrics span { display: block; font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.metrics b { font-size: 14px; font-variant-numeric: tabular-nums; }

.balance-row { margin-bottom: 12px; }
.bal-input { display: flex; align-items: center; gap: 4px; background: var(--panel-2);
  border: 1px solid var(--border-2); border-radius: 5px; padding: 0 9px; }
.bal-input span { color: var(--muted); font-size: 12px; }
.bal-input input { width: 106px; background: transparent; border: none; color: var(--text);
  padding: 7px 0; outline: none; font-size: 13px; font-family: var(--font); }

.warn { color: #f6c744; }
.level-warning {
  font-size: 11px; color: #f6c744; background: rgba(246,199,68,.1);
  border: 1px solid rgba(246,199,68,.3); border-radius: 5px;
  padding: 7px 9px; margin: 0 0 10px; line-height: 1.35;
}
.level-warning[hidden] { display: none; }

.hint { font-size: 11px; color: var(--muted); margin: 4px 0 12px; line-height: 1.4; }
.hint b { color: var(--text); }

/* position panel */
.pos-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; margin-bottom: 12px; }
.pos-grid .pg { display: flex; flex-direction: column; }
.pos-grid .pg span { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.pos-grid .pg b { font-size: 14px; font-variant-numeric: tabular-nums; }

/* stats */
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.stats-grid .sg span { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; display: block; }
.stats-grid .sg b { font-size: 15px; font-variant-numeric: tabular-nums; }

/* journal */
.journal-list { display: flex; flex-direction: column; gap: 6px; max-height: 240px; overflow-y: auto; }
.journal-empty { color: var(--muted); font-size: 12px; text-align: center; padding: 16px 0; }
.j-item {
  display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: center;
  background: var(--panel-2); border-radius: 6px; padding: 8px 10px; font-size: 12px;
}
.j-side { font-weight: 700; font-size: 10px; padding: 2px 6px; border-radius: 4px; }
.j-side.long { background: var(--green-soft); color: var(--green); }
.j-side.short { background: var(--red-soft); color: var(--red); }
.j-mid { color: var(--muted); font-size: 11px; line-height: 1.3; }
.j-mid b { color: var(--text); }
.j-pnl { font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }

.pos { color: var(--green); }
.neg { color: var(--red); }

/* toast */
.toast {
  position: fixed; bottom: 66px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--border-2); color: var(--text);
  padding: 10px 18px; border-radius: 8px; font-size: 13px; z-index: 50;
  box-shadow: 0 8px 24px rgba(0,0,0,.5); max-width: 480px;
}
.toast.err { border-color: var(--red); }
.toast.ok { border-color: var(--green); }

/* ---------------- mobile / small screens ---------------- */
.panel-toggle { display: none; }
.side-scrim { display: none; }
.side-grabber { display: none; }

@media (max-width: 1100px) {
  .side { width: 280px; }
}

@media (max-width: 820px) {
  /* The side panel becomes a bottom sheet so the chart keeps the full width. */
  .layout { position: relative; }
  .chart-col { width: 100%; }

  .side {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 45;
    width: auto; max-height: 80vh; max-height: 80dvh;
    border-left: none; border-top: 1px solid var(--border-2);
    border-radius: 14px 14px 0 0;
    background: var(--panel);
    box-shadow: 0 -10px 30px rgba(0,0,0,.5);
    transform: translateY(101%);
    /* visibility (not just transform) so the closed sheet is untabbable,
       invisible to assistive tech and cannot swallow taps meant for the chart */
    visibility: hidden;
    transition: transform .22s ease, visibility 0s linear .22s;
    padding: 6px 12px calc(12px + env(safe-area-inset-bottom));
    overscroll-behavior: contain;
  }
  body.panel-open .side {
    transform: translateY(0);
    visibility: visible;
    transition: transform .22s ease, visibility 0s;
  }

  .side-grabber {
    display: block; width: 38px; height: 4px; border-radius: 2px;
    background: var(--border-2); margin: 4px auto 8px; flex-shrink: 0;
  }

  .side-scrim {
    display: block; position: fixed; inset: 0; z-index: 44;
    background: rgba(0,0,0,.45);
  }
  .side-scrim[hidden] { display: none; }

  /* Floating button that raises the sheet. */
  .panel-toggle {
    display: flex; align-items: center; gap: 7px;
    position: absolute; right: 10px; bottom: 10px; z-index: 40;
    max-width: 62vw;
    background: var(--accent); color: #fff; border: none; cursor: pointer;
    border-radius: 22px; padding: 11px 16px; font-size: 13px; font-weight: 700;
    font-family: var(--font); box-shadow: 0 6px 18px rgba(0,0,0,.45);
    min-height: 44px;
  }
  body.panel-open .panel-toggle { display: none; }
  .panel-toggle .pt-eq {
    font-weight: 600; opacity: .85; font-variant-numeric: tabular-nums;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }

  /* Topbar wraps instead of clipping; timeframes stay reachable by scrolling. */
  .topbar { height: auto; flex-wrap: wrap; gap: 8px; padding: 8px 10px; row-gap: 8px; }
  .brand-name { display: none; }
  .symbol-box { order: 1; }
  .topbar-right { order: 2; gap: 10px; }
  .src-pill { display: none; }
  .tf-group {
    order: 3; width: 100%; overflow-x: auto; flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  .tf-group::-webkit-scrollbar { display: none; }
  .tf { flex: 0 0 auto; min-height: 34px; padding: 6px 12px; }

  /* Replay bar wraps onto rows so every control stays on screen. Scrolling
     hid Play off the right edge, which is the one button that must be there. */
  .replay-bar {
    height: auto; flex-wrap: wrap; overflow: visible;
    gap: 6px; padding: 7px 10px calc(7px + env(safe-area-inset-bottom));
    justify-content: flex-start;
  }
  .replay-bar .btn { flex: 0 0 auto; min-height: 38px; }
  /* Transport controls first; secondary controls fall to the next row. */
  #replay-back { order: 1; }
  #replay-play { order: 2; }
  #replay-step { order: 3; }
  #replay-restart { order: 4; }
  #replay-random { order: 5; }
  .start-at { order: 6; }
  .speed { order: 7; flex: 0 0 auto; }
  .speed input { width: 84px; }
  .progress-wrap { order: 8; width: 100%; margin-left: 0; min-width: 0; }

  /* Bigger, thumb-friendly controls. */
  .field-row input, .field-row select { min-height: 40px; }
  .st-btn { min-height: 42px; }
  .btn-place, .btn-danger { min-height: 46px; }
  .draw-tools { width: 44px; }
  .draw-tool { min-height: 40px; font-size: 17px; }
  .pt-handle > span { padding: 7px 8px; }   /* larger drag target */
  .draw-rect-x { width: 26px; height: 26px; opacity: 1; font-size: 12px; }

  /* Anchor the picker to the viewport, not to the symbol box — anchored to the
     box it started at its left edge and ran off the right of the screen. */
  .picker {
    position: fixed; top: auto; left: 8px; right: 8px; width: auto;
    max-height: 60vh;
    max-height: 60dvh;
  }
  .journal-list { max-height: 180px; }
  .side { overflow-y: auto; -webkit-overflow-scrolling: touch; }
  #position-panel { order: -1; }   /* live position first once you're in a trade */
  .side-grabber { order: -2; }
}

@media (max-width: 420px) {
  .symbol-box { flex: 1; }
  #symbol-input { width: 100%; min-width: 0; }
  .acct-label { font-size: 9px; }
  .acct-val { font-size: 12px; }
  .qty-wrap { flex-wrap: wrap; justify-content: flex-end; }
  .field-row { flex-wrap: wrap; gap: 4px; }
  .field-row input, .field-row select { width: 128px; }
}

/* Pointer-coarse devices get no hover affordances. */
@media (hover: none) {
  .draw-rect-x { opacity: 1; }
  .pos-tool[data-filled="1"] .pt-handle { opacity: .6; }
}
