/* ─────────────────────────────────────────────
   opentrader.online — TradingView-style theme
   ───────────────────────────────────────────── */
:root {
  --font: "Trebuchet MS", -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}
body.dark {
  --bg: #131722;          /* app background      */
  --panel: #131722;       /* panels              */
  --panel-2: #1e222d;     /* hover / raised      */
  --border: #2a2e39;      /* borders & dividers  */
  --text: #d1d4dc;        /* primary text        */
  --text-sub: #787b86;    /* secondary text      */
  --accent: #2962ff;      /* TV blue             */
  --up: #26a69a;          /* bullish             */
  --down: #ef5350;        /* bearish             */
  --up-flash: rgba(38,166,154,.25);
  --down-flash: rgba(239,83,80,.25);
  --scroll: #363a45;
}
body.light {
  --bg: #f8f9fd;
  --panel: #ffffff;
  --panel-2: #eff1f7;
  --border: #e0e3eb;
  --text: #191919;
  --text-sub: #868993;
  --accent: #2962ff;
  --up: #26a69a;
  --down: #ef5350;
  --up-flash: rgba(38,166,154,.22);
  --down-flash: rgba(239,83,80,.22);
  --scroll: #c8cbd4;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
  -webkit-user-select: none; user-select: none;
}
svg { display: block; }
button {
  font-family: var(--font);
  font-size: 13px;
  color: var(--text);
  background: none;
  border: none;
  cursor: pointer;
}
::-webkit-scrollbar { width: 7px; height: 7px; }
::-webkit-scrollbar-thumb { background: var(--scroll); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

.spacer { flex: 1; }
.hidden { display: none !important; }

/* ═══════════ LAYOUT ═══════════ */
#app {
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;             /* dvh: not cut off by mobile address bars */
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) 0 env(safe-area-inset-left, 0px);
}
#main { flex: 1; display: flex; min-height: 0; position: relative; }
#chart-area { flex: 1; position: relative; min-width: 0; }
#chart { position: absolute; inset: 0; }

/* ═══════════ TOP BAR ═══════════ */
#topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}
.bar-row { display: flex; align-items: center; gap: 2px; min-width: 0; }
#quickbar { flex: 1; min-width: 0; }

.topbar-btn {
  display: flex; align-items: center; gap: 5px;
  height: 28px; padding: 0 7px;
  border-radius: 4px; white-space: nowrap; color: var(--text);
}
.topbar-btn:hover { background: var(--panel-2); }
.topbar-btn.active { background: var(--panel-2); }
.topbar-btn .caret { color: var(--text-sub); }

/* ƒx — function notation, the standard "indicators" mark */
.fx-icon {
  font-family: Georgia, 'Times New Roman', serif;
  font-style: italic; font-weight: bold;
  font-size: 15px; line-height: 1;
  letter-spacing: -0.5px;
  color: var(--text);
  margin-right: 1px;
}
.icon-btn { padding: 0 7px; }
.symbol-btn { font-weight: bold; font-size: 13.5px; }
.star-btn.faved svg { fill: var(--accent); stroke: var(--accent); }
.vsep { width: 1px; height: 18px; background: var(--border); margin: 0 3px; flex: 0 0 auto; }

/* brand pill — logo + name live in one rounded “unit” */
.brand {
  display: flex; align-items: center; gap: 6px;
  padding: 3px 12px 3px 5px;
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: default;
  margin-right: 4px;
  transition: border-color .15s, background .15s;
  flex: 0 0 auto;
}
.brand:hover { border-color: var(--accent); }
.brand-logo { border-radius: 7px; flex: 0 0 auto; }
.brand-name { font-weight: bold; font-size: 14px; letter-spacing: .3px; line-height: 1; }

/* interval / indicator / symbol picker rows */
.search-row.active { background: var(--panel-2); box-shadow: inset 2px 0 0 var(--accent); }
.search-row .chg { display: flex; align-items: center; justify-content: flex-end; }

/* custom-interval builder footer */
.dialog-footer {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  font-size: 12.5px; color: var(--text-sub);
  flex: 0 0 auto;
}
.footer-label { font-weight: bold; color: var(--text); }
.dialog-footer input[type="number"] {
  width: 82px; padding: 6px 8px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 4px;
  color: var(--text); font-family: var(--font); font-size: 13px; outline: none;
}
.dialog-footer input[type="number"]:focus, .unit-select:focus { border-color: var(--accent); }
.unit-select {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 4px;
  padding: 6px 8px; font-family: var(--font); font-size: 13px; outline: none;
  cursor: pointer;
}
.footer-btn {
  margin-left: auto; padding: 6px 16px;
  background: var(--accent); color: #fff; font-weight: bold;
  border-radius: 4px; font-size: 13px;
}
.footer-btn:hover { filter: brightness(1.12); }

/* ═══════════ DRAW TOOLBAR ═══════════ */
#drawbar {
  flex: 0 0 56px;
  display: flex; flex-direction: column; align-items: center;
  padding: 6px 0; gap: 2px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  z-index: 20;
}
.tool-btn {
  width: 40px; height: 38px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 5px; color: var(--text-sub);
}
.tool-btn:hover { background: var(--panel-2); color: var(--text); }
.tool-btn.active { background: var(--panel-2); color: var(--accent); }
.tool-btn[data-on="1"] { color: var(--accent); }
.tool-sep { width: 28px; height: 1px; background: var(--border); margin: 6px 0; }

/* ═══════════ CHART LEGEND ═══════════ */
#chart-legend {
  position: absolute; top: 8px; left: 9px;
  z-index: 29; pointer-events: none;
  line-height: 1.55;
}
.legend-head { font-size: 13px; font-weight: bold; color: var(--text); }
.legend-head .dot { color: var(--text-sub); font-weight: normal; margin: 0 4px; }
#lg-interval { color: var(--text-sub); font-weight: normal; }
.badge {
  margin-left: 4px; padding: 0 5px; border-radius: 3px;
  font-size: 10px; font-weight: normal; text-transform: uppercase;
  color: var(--down); background: var(--down-flash);
}
.legend-ohlc { font-size: 12px; color: var(--text); }
.legend-ohlc .k { color: var(--text-sub); margin-left: 8px; margin-right: 3px; }
.legend-ohlc .k:first-child { margin-left: 0; }

/* ═══════════ SIDEBAR ═══════════ */
#sidebar {
  flex: 0 0 var(--panel-w, 300px);
  position: relative;
  display: flex; flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--border);
  z-index: 20;
}
#sidebar .panel-resizer {
  position: absolute; left: -3px; top: 0; bottom: 0; width: 6px;
  cursor: col-resize; z-index: 5;
  touch-action: none;
}
#sidebar .panel-resizer:hover, #sidebar .panel-resizer.on {
  background: rgba(41, 98, 255, .28);
}
#sidebar.collapsed { display: none; }

/* right toolbox — mirrors the left draw rail */
#rightbar {
  flex: 0 0 44px;
  display: flex; flex-direction: column; align-items: center;
  padding: 6px 0; gap: 2px;
  background: var(--panel);
  border-left: 1px solid var(--border);
  z-index: 21;
}
#rightbar .rb-btn {
  width: 38px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 5px; color: var(--text-sub);
}
#rightbar .rb-btn:hover { background: var(--panel-2); color: var(--text); }
#rightbar .rb-btn.active { background: var(--panel-2); color: var(--accent); }
.side-panel { display: none; flex: 1; overflow-y: auto; min-height: 0; }
.side-panel.active { display: block; }

/* watchlist */
.wl-head {
  display: grid; grid-template-columns: 1fr auto auto;
  gap: 10px; padding: 6px 12px;
  font-size: 11px; color: var(--text-sub);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--panel);
}
.wl-col-last, .wl-col-chg { width: 72px; text-align: right; }
#wl-list { padding-bottom: 20px; }
.wl-row {
  display: grid; grid-template-columns: 1fr auto auto;
  gap: 10px; padding: 7px 12px; align-items: center;
  cursor: pointer; border-radius: 0;
}
.wl-row:hover { background: var(--panel-2); }
.wl-row.active { background: var(--panel-2); box-shadow: inset 2px 0 0 var(--accent); }
.wl-sym { min-width: 0; }
.wl-row .sym { display: block; font-weight: bold; }
.wl-row .desc { display: block; font-size: 11px; color: var(--text-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wl-row .last { width: 72px; text-align: right; font-variant-numeric: tabular-nums; }
.wl-row .chg { width: 72px; text-align: right; font-variant-numeric: tabular-nums; }
.up   { color: var(--up); }
.down { color: var(--down); }
.wl-row.flash-up   { background: var(--up-flash); transition: none; }
.wl-row.flash-down { background: var(--down-flash); transition: none; }
.wl-row { transition: background .4s; }

/* details */
#panel-details { padding: 12px 14px; }
.det-title { font-size: 16px; font-weight: bold; }
.det-sub { font-size: 12px; color: var(--text-sub); margin: 2px 0 12px; }
.det-row {
  display: flex; justify-content: space-between;
  padding: 7px 0; border-bottom: 1px solid var(--border);
}
.det-row span { color: var(--text-sub); }
.det-row b { font-weight: normal; font-variant-numeric: tabular-nums; text-align: right; }

/* ═══════════ STATUS BAR ═══════════ */
#statusbar {
  height: 26px; flex: 0 0 auto;
  display: flex; align-items: center; gap: 7px;
  padding: 0 12px; font-size: 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  color: var(--text-sub);
}
#clock { font-variant-numeric: tabular-nums; }

/* ═══════════ DIALOGS & MENUS ═══════════ */
.overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.45);
  display: flex; align-items: flex-start; justify-content: center;
}
.dialog { background: var(--panel); border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 8px 40px rgba(0,0,0,.5); }
.search-dialog { width: 520px; max-width: 92vw; margin-top: 10vh; overflow: hidden; }
.search-box {
  display: flex; align-items: center; gap: 9px;
  padding: 12px 14px; color: var(--text-sub);
  border-bottom: 1px solid var(--border);
}
.search-box input {
  flex: 1; background: none; border: none; outline: none;
  font-family: var(--font); font-size: 14px; color: var(--text);
}
.search-esc {
  font-size: 11px; color: var(--text-sub);
  border: 1px solid var(--border); border-radius: 3px; padding: 1px 5px;
}
.search-list { max-height: 52vh; overflow-y: auto; padding: 6px 0; }
.search-row {
  display: grid; grid-template-columns: auto 1fr auto auto;
  align-items: center; gap: 10px; padding: 8px 14px; cursor: pointer;
}
.search-row:hover, .search-row.sel { background: var(--panel-2); }
.search-row .sym { font-weight: bold; }
.search-row .desc { color: var(--text-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-row .price { font-variant-numeric: tabular-nums; }
.search-row .chg { width: 62px; text-align: right; font-variant-numeric: tabular-nums; }
.tag { display: inline-block; font-size: 9px; color: var(--text-sub); border: 1px solid var(--border); padding: 0 4px; border-radius: 2px; margin-left: 5px; vertical-align: middle; }
.search-empty { padding: 24px; text-align: center; color: var(--text-sub); }

.menu {
  position: fixed; z-index: 100;
  min-width: 190px; padding: 5px 0;
  background: var(--panel);
  border: 1px solid var(--border); border-radius: 6px;
  box-shadow: 0 8px 30px rgba(0,0,0,.45);
}
.menu-title { padding: 6px 14px 8px; font-size: 12px; font-weight: bold; color: var(--text); border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.menu-item {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 14px; cursor: pointer; color: var(--text);
}
.menu-item:hover { background: var(--panel-2); }
.menu-item input { accent-color: var(--accent); width: 15px; height: 15px; cursor: pointer; }
.ct-item.active { color: var(--accent); }

/* ═══════════ TOAST ═══════════ */
#toast {
  position: fixed; left: 50%; bottom: 44px; transform: translate(-50%, 12px);
  z-index: 200; padding: 8px 16px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 5px;
  color: var(--text); font-size: 12.5px;
  opacity: 0; pointer-events: none;
  transition: opacity .18s, transform .18s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.lib-error {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--text-sub); font-size: 14px; text-align: center; padding: 24px;
}


/* ═══════════ AI CHAT PANEL (right sidebar) ═══════════
   Adapted from the reply-component, restyled for the app theme.
   Single-model conversation — no multi-AI broadcasting. */
.side-panel.chat-panel.active {
  display: flex; flex-direction: column;
  gap: 8px; min-height: 0;
  padding: 10px;
  overflow: hidden;
}
button:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.chat-artifact {
  flex: 1; min-height: 0;
  overflow-y: auto; overflow-x: hidden;
  padding: 2px;
}
.chat-note { color: var(--text-sub); font-size: 13px; }

.conv { display: flex; flex-direction: column; gap: 12px; }
.u-row { display: flex; justify-content: flex-end; }
.bubble-user {
  max-width: 85%;
  background: var(--panel-2); color: var(--text);
  font-size: 13px; line-height: 1.45;
  border-radius: 14px; padding: 8px 12px;
  white-space: pre-wrap; word-break: break-word;
}

.ai-msg { display: flex; flex-direction: column; gap: 5px; }
.ai-meta {
  display: flex; align-items: center; gap: 6px;
  font-size: 11px; color: var(--text-sub);
  font-weight: bold; text-transform: none;
}
.ai-msg.thinking .ai-bubble { color: var(--text-sub); font-style: italic; }
.ai-bubble { font-size: 13px; line-height: 1.6; color: var(--text); word-break: break-word; }
.ai-bubble p { margin: 0 0 8px; }
.ai-bubble p:last-child { margin-bottom: 0; }
.ai-bubble ul, .ai-bubble ol { margin: 6px 0; padding-left: 20px; }
.ai-bubble li { margin: 3px 0; }
.ai-bubble.ai-error { color: #f59d6e; }

.rbar { display: flex; align-items: center; justify-content: flex-end; gap: 8px; margin-top: 2px; }
.tok { font-size: 10px; color: var(--text-sub); font-variant-numeric: tabular-nums; }

/* composer */
.composer {
  flex: 0 0 auto;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 5px;
  display: flex; align-items: flex-end; gap: 5px;
}
.composer textarea {
  flex: 1; min-width: 0;
  box-sizing: border-box;
  background: transparent; border: 0; outline: 0; resize: none;
  color: var(--text); font-family: inherit; font-size: 14px;
  height: 20px; min-height: 20px; max-height: 120px; line-height: 20px;
  padding: 4px 2px; overflow-y: auto; scrollbar-width: none;
}
.composer textarea::-webkit-scrollbar { display: none; }
.composer textarea::placeholder { color: var(--text-sub); }

.model-pill {
  flex: 0 0 auto; display: flex; align-items: center; gap: 5px;
  align-self: flex-start;
  padding: 3px 9px;
  border: 1px solid var(--border); border-radius: 999px;
  background: transparent; color: var(--text);
  font-family: var(--font); font-size: 11px; font-weight: bold;
  cursor: pointer; white-space: nowrap; overflow: hidden;
}
.model-pill:hover { border-color: var(--text-sub); }
.model-pill .caret { color: var(--text-sub); flex: 0 0 auto; }
#model-name { overflow: hidden; text-overflow: ellipsis; }
.mdot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

.send-btn {
  width: 28px; height: 28px; flex-shrink: 0;
  border-radius: 50%; border: 0;
  background: #fff; color: #000;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center; padding: 0;
}
.send-btn:hover { background: var(--accent); color: #fff; }
.send-btn svg { width: 14px; height: 14px; display: block; }

.menu-sub { margin-left: auto; font-size: 10px; color: var(--text-sub); }
.menu-item.active { color: var(--accent); }
.menu-item .mdot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.menu-q {
  padding: 6px 14px 8px; font-size: 10px;
  color: var(--text-sub); border-top: 1px solid var(--border); margin-top: 4px;
  font-variant-numeric: tabular-nums;
}

/* ═══════════ RESPONSIVE LAYOUT ═══════════
   desktop  > 1100px : drawbar | chart | panel (300px)
   tablet   761–1100 : same three columns, narrower panel that pushes the chart
                       (price axis is never covered)
   phone    ≤ 760px  : same rail | chart row — but the right panel is a modal
                       bottom sheet (Chat / Info via bottom nav). */

/* elements that only exist on phone */
#sheet-scrim, .sheet-grip { display: none; }

/* ── phone ── */
@media (max-width: 760px) {

  /* top bar: identical to desktop — it just scrolls when the row is too wide */
  #topbar { overflow-x: auto; scrollbar-width: none; }
  #topbar::-webkit-scrollbar { display: none; }
  .bar-row { flex: 0 0 auto; }

  .topbar-btn { height: 40px; padding: 0 9px; }
  .brand { padding: 3px 10px 3px 5px; }

  /* chart: #main stays a row (rail | chart); the sheet is fixed overlay */
  #chart-area { min-height: 0; }
  #chart-legend { font-size: 11px; line-height: 1.5; }
  .legend-ohlc { font-size: 10.5px; }

  /* left toolbox: same as desktop — a fixed vertical rail on the
     chart's left edge. It never hides; the sheet simply
     slides over the whole app as a modal. */
  #drawbar {
    flex: 0 0 42px;
    padding: 4px 0; gap: 1px;
  }
  #drawbar .tool-btn { width: 36px; height: 34px; }

  /* bottom sheet (--kb / --vvh are kept current by app.js so
     the sheet rides above the on-screen keyboard) */
  #sidebar {
    position: fixed; left: 0; right: 0; bottom: var(--kb, 0px);
    flex: none; width: auto;
    height: min(72dvh, calc(var(--vvh, 100dvh) - 12px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    border-left: none; border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -10px 34px rgba(0,0,0,.4);
    z-index: 60;
    transform: translateY(0);
    transition: transform .24s cubic-bezier(.2,.8,.2,1), visibility 0s;
  }
  #sidebar.collapsed {
    display: flex;                      /* override desktop display:none so it can slide */
    transform: translateY(105%);
    visibility: hidden; pointer-events: none;
    box-shadow: none;
    transition: transform .24s cubic-bezier(.2,.8,.2,1), visibility 0s .24s;
  }
  .sheet-grip { display: block; flex: 0 0 auto; height: 22px; touch-action: none; cursor: grab; }
  .sheet-grip::after {
    content: ""; display: block; width: 36px; height: 4px; margin: 9px auto 0;
    border-radius: 2px; background: var(--border);
  }
  body.sheet-open #sheet-scrim {
    display: block; position: fixed; inset: 0; z-index: 55;
    background: rgba(0,0,0,.4);
  }
  .rb-btn { width: 40px; height: 44px; }

  /* sheet is a fixed-height overlay on phones: no resizer there */
  #sidebar .panel-resizer { display: none; }

  /* 16px inputs: stops iOS from zooming the page on focus */
  input, textarea, select,
  .composer textarea, .search-box input,
  .dialog-footer input[type="number"], .unit-select { font-size: 16px; }
  .composer textarea { line-height: 22px; height: 22px; min-height: 22px; }
  .send-btn { width: 34px; height: 34px; }
  .model-pill { padding: 6px 10px; font-size: 12px; }

  /* dialogs & menus */
  .search-dialog { width: 96vw; max-width: 96vw; margin-top: 4vh; }
  .search-list { max-height: 48dvh; }
  .search-row { padding: 11px 14px; }
  .menu-item { padding: 12px 16px; }
  #toast { bottom: calc(68px + env(safe-area-inset-bottom, 0px)); }
}

@media (prefers-reduced-motion: reduce) {
  #sidebar, #sidebar.collapsed { transition: none; }
}