/* XAU Bot - dark, low-glare trading desk */
:root {
  --bg: #0f131a;
  --panel: #141922;
  --panel-2: #191f2a;
  --line: #232b37;
  --line-2: #2c3644;
  --text: #c9d1d9;
  --text-strong: #e6edf3;
  --muted: #7d8794;
  --faint: #566070;
  --amber: #f2a93b;
  --coral: #ff7a59;
  --blue: #4c8dff;
  --up: #26a69a;
  --down: #ef5350;
  --up-bg: rgba(38, 166, 154, .14);
  --down-bg: rgba(239, 83, 80, .14);
  --amber-bg: rgba(242, 169, 59, .12);
  --radius: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
b, .num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.up { color: var(--up); }
.down { color: var(--down); }
.amber { color: var(--amber); }
a { color: var(--blue); }

/* ---------- layout ---------- */
.app {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 0;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  min-width: 0;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; color: var(--text-strong); letter-spacing: .2px; }
.brand-lg { font-size: 20px; }
.ticker { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.ticker .sym { font-weight: 650; color: var(--text-strong); }
.ticker .tf { color: var(--muted); }
.ticker .price { font: 600 15px var(--mono); color: var(--text-strong); }
.ticker .src { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 260px; }
.mode-pill {
  padding: 3px 10px; border-radius: 999px; font-weight: 700; font-size: 11px; letter-spacing: .6px;
  background: var(--amber-bg); color: var(--amber); border: 1px solid rgba(242, 169, 59, .35);
}
.mode-pill.live { background: var(--down-bg); color: #ff8a80; border-color: rgba(239, 83, 80, .5); }
.acct { display: flex; gap: 18px; margin-left: auto; }
.acct div { display: flex; flex-direction: column; line-height: 1.2; }
.acct label { font-size: 10.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.acct b { font: 600 13.5px var(--mono); color: var(--text-strong); }

.main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 410px;
  min-height: 0;
}
.chart-col { display: grid; grid-template-rows: auto 1fr auto; min-width: 0; min-height: 0; }
.chart-toolbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 6px 10px; border-bottom: 1px solid var(--line); min-width: 0; flex-wrap: wrap;
}
.legend { font: 12px var(--mono); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.legend b { color: var(--text); font-weight: 500; margin-right: 8px; }
.toggles { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: 999px;
  border: 1px solid var(--line-2); color: var(--muted); cursor: pointer; user-select: none; font-size: 12px;
}
.chip input { display: none; }
.chip.on { color: var(--text); background: var(--panel-2); }
.sw { display: inline-block; width: 14px; height: 3px; border-radius: 2px; }
.sw-trend { background: var(--amber); }
.sw-vwap { background: var(--coral); }
.sw-mean { background: repeating-linear-gradient(90deg, #9aa4b2 0 3px, transparent 3px 6px); }
.sw-ema { background: var(--blue); }
.chart { position: relative; min-height: 260px; }
.session-strip {
  border-top: 1px solid var(--line); padding: 7px 12px; color: var(--muted); font-size: 12.5px;
  display: flex; gap: 14px; flex-wrap: wrap; align-items: center; min-height: 34px;
}
.session-strip b { color: var(--text); }

.side {
  border-left: 1px solid var(--line);
  background: var(--panel);
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 0;
}
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button {
  flex: 1; background: none; border: 0; border-bottom: 2px solid transparent; padding: 10px 4px;
  color: var(--muted); cursor: pointer; font-weight: 550;
}
.tabs button.active { color: var(--text-strong); border-bottom-color: var(--amber); }
.tabs button:hover { color: var(--text); }
.tab-body { overflow: auto; min-height: 0; overscroll-behavior: contain; }
.tab { display: none; padding: 12px 14px 18px; }
.tab.active { display: block; }

.statusbar {
  display: flex; gap: 18px; padding: 5px 12px; border-top: 1px solid var(--line); background: var(--panel);
  color: var(--muted); font-size: 12px; flex-wrap: wrap;
}
.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--faint); margin-right: 6px; vertical-align: 1px; }
.dot.ok { background: var(--up); }
.dot.err { background: var(--down); }
.dot.warn { background: var(--amber); }

/* ---------- building blocks ---------- */
h3 { margin: 4px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .7px; color: var(--muted); font-weight: 650; }
h2 { margin: 0 0 4px; font-size: 15px; color: var(--text-strong); }
.card {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 11px 12px; margin-bottom: 12px;
}
.card.flat { background: transparent; }
.row { display: flex; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.kv { display: flex; flex-direction: column; }
.kv label { font-size: 11px; color: var(--muted); }
.kv b { font: 600 13px var(--mono); color: var(--text-strong); }
.badge {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px;
  font-size: 11.5px; font-weight: 650; border: 1px solid var(--line-2); color: var(--muted); white-space: nowrap;
}
.badge.up { color: var(--up); background: var(--up-bg); border-color: rgba(38, 166, 154, .35); }
.badge.down { color: var(--down); background: var(--down-bg); border-color: rgba(239, 83, 80, .35); }
.badge.amber { color: var(--amber); background: var(--amber-bg); border-color: rgba(242, 169, 59, .35); }
.badge.blue { color: var(--blue); background: rgba(76, 141, 255, .12); border-color: rgba(76, 141, 255, .35); }

.btn {
  border: 1px solid var(--line-2); background: var(--panel-2); color: var(--text); border-radius: 6px;
  padding: 6px 12px; cursor: pointer; font-weight: 550;
}
.btn:hover { border-color: #3a4656; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: #3a2d14; border-color: rgba(242, 169, 59, .55); color: #ffd18a; }
.btn.primary:hover { background: #47371a; }
.btn.danger { background: #3a1716; border-color: rgba(239, 83, 80, .55); color: #ffb4ad; }
.btn.ghost { background: transparent; }
.btn.small { padding: 3px 9px; font-size: 12px; }
.btn.wide { width: 100%; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.field > span { font-size: 12px; color: var(--muted); }
.field small { color: var(--faint); font-size: 11.5px; line-height: 1.35; }
input[type=text], input[type=number], input[type=password], input[type=time], select {
  background: #0f141b; border: 1px solid var(--line-2); border-radius: 6px; padding: 6px 8px; width: 100%;
  color: var(--text-strong); outline: none;
}
input:focus, select:focus { border-color: rgba(242, 169, 59, .6); }
.inline-unit { display: flex; align-items: center; gap: 6px; }
.inline-unit span { color: var(--muted); font-size: 12px; white-space: nowrap; }
.days { display: flex; gap: 4px; flex-wrap: wrap; }
.days label {
  border: 1px solid var(--line-2); border-radius: 5px; padding: 3px 7px; cursor: pointer; font-size: 12px; color: var(--muted);
}
.days label.on { color: var(--text-strong); background: #253042; border-color: #3a4a60; }
.days input { display: none; }
.switch { position: relative; width: 36px; height: 20px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch i { position: absolute; inset: 0; background: #2a3341; border-radius: 999px; transition: .15s; cursor: pointer; }
.switch i::after { content: ""; position: absolute; left: 3px; top: 3px; width: 14px; height: 14px; border-radius: 50%; background: #8b95a3; transition: .15s; }
.switch input:checked + i { background: #4a3a1a; }
.switch input:checked + i::after { left: 19px; background: var(--amber); }

.steps { margin: 0; padding-left: 20px; }
.steps li { margin: 0 0 6px; color: var(--text); }
.steps li::marker { color: var(--amber); font-weight: 700; }
.group-title { margin: 14px 0 8px; font-size: 12px; font-weight: 650; color: var(--amber); }

table.list { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.list th { text-align: left; color: var(--muted); font-weight: 550; padding: 4px 6px; border-bottom: 1px solid var(--line); }
table.list td { padding: 6px 6px; border-bottom: 1px solid #1c232e; font-variant-numeric: tabular-nums; }
table.list tr.clickable { cursor: pointer; }
table.list tr.clickable:hover td { background: #1b222d; }
table.list tr.sel td { background: #222a36; }
.right { text-align: right; }

.dir { display: inline-flex; align-items: center; gap: 6px; font-weight: 650; }
.hw { display: grid; grid-template-columns: auto 1fr auto; gap: 8px 10px; align-items: center; }
.hw .who { color: var(--muted); }
.progress { height: 6px; background: #222a35; border-radius: 4px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--amber); }
.rbar { position: relative; height: 22px; margin: 10px 0 4px; }
.rbar .track { position: absolute; left: 0; right: 0; top: 9px; height: 4px; border-radius: 3px;
  background: linear-gradient(90deg, rgba(239,83,80,.55) 0%, rgba(239,83,80,.55) var(--z, 25%), rgba(38,166,154,.55) var(--z, 25%), rgba(38,166,154,.55) 100%); }
.rbar .tick { position: absolute; top: 4px; width: 2px; height: 14px; background: #9aa4b2; }
.rbar .tick.be { background: var(--amber); }
.rbar .now { position: absolute; top: 2px; width: 10px; height: 18px; margin-left: -5px; border-radius: 3px; background: var(--text-strong); box-shadow: 0 0 0 2px var(--bg); }
.rbar-labels { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); }

.log { list-style: none; margin: 0; padding: 0; font-size: 12px; }
.log li { padding: 5px 0; border-bottom: 1px solid #1c232e; display: grid; grid-template-columns: 74px 1fr; gap: 8px; }
.log li time { color: var(--faint); font-family: var(--mono); font-size: 11.5px; }
.log li.trade span { color: var(--text-strong); }
.log li.error span { color: #ff9a92; }
.log li.warn span { color: #f7c46c; }

.seg { display: inline-flex; border: 1px solid var(--line-2); border-radius: 7px; overflow: hidden; }
.seg button { background: transparent; border: 0; padding: 6px 14px; cursor: pointer; color: var(--muted); font-weight: 600; }
.seg button.on { background: #253042; color: var(--text-strong); }
.seg button.on.live { background: #3a1716; color: #ffb4ad; }

.radio-list label { display: flex; gap: 9px; padding: 8px 9px; border: 1px solid var(--line); border-radius: 7px; margin-bottom: 6px; cursor: pointer; }
.radio-list label.on { border-color: rgba(242, 169, 59, .5); background: #1c1a15; }
.radio-list input { margin-top: 3px; accent-color: var(--amber); }
.radio-list b { color: var(--text-strong); font-weight: 600; display: block; }
.radio-list small { color: var(--muted); }

details.broker { border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; background: var(--panel-2); }
details.broker > summary { cursor: pointer; padding: 10px 12px; list-style: none; display: flex; justify-content: space-between; align-items: center; }
details.broker > summary::-webkit-details-marker { display: none; }
details.broker .body { padding: 0 12px 12px; }
.note { font-size: 12px; color: var(--muted); background: #121821; border: 1px solid var(--line); border-radius: 7px; padding: 8px 10px; margin: 8px 0; }
.note b { color: var(--text); }
.result-ok { color: var(--up); font-size: 12px; margin-top: 6px; }
.result-err { color: #ff9a92; font-size: 12px; margin-top: 6px; white-space: pre-wrap; }
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 8px 0 12px; }
.stat { background: #121821; border: 1px solid var(--line); border-radius: 7px; padding: 7px 9px; }
.stat label { display: block; font-size: 11px; color: var(--muted); }
.stat b { font: 600 14px var(--mono); color: var(--text-strong); }
canvas.equity { width: 100%; height: 90px; display: block; background: #121821; border: 1px solid var(--line); border-radius: 7px; }
.empty { color: var(--faint); padding: 14px 4px; text-align: center; }
.detail-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }

/* ---------- toasts ---------- */
.toasts { position: fixed; right: 14px; bottom: 40px; display: flex; flex-direction: column; gap: 8px; z-index: 50; max-width: min(380px, calc(100vw - 28px)); }
.toast { background: #1d2430; border: 1px solid var(--line-2); border-left: 3px solid var(--amber); border-radius: 7px; padding: 9px 12px; box-shadow: 0 6px 24px rgba(0,0,0,.35); font-size: 12.5px; }
.toast.ok { border-left-color: var(--up); }
.toast.err { border-left-color: var(--down); }

/* ---------- login ---------- */
.login-body { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { width: min(360px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 26px 22px; }
.login-card .error { color: #ff9a92; min-height: 1.4em; margin: 10px 0 0; }

/* Phone/tablet-only elements stay hidden on desktop. */
.mnav, .chart-close, .mobile-only, .mode-pill .dot { display: none; }

/* ---------- phones & tablets: app-style screen ----------
   The page never scrolls. Top bar + content + bottom tab bar fill the screen; the Live tab
   shows the chart above the monitor, other tabs use the full height, and opening a trade
   brings the chart back on top (stacked detail view). Each panel scrolls on its own. */
@media (max-width: 960px) {
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  .app {
    height: 100vh;
    height: 100dvh;
    grid-template-rows: auto minmax(0, 1fr) auto;
    padding-top: env(safe-area-inset-top);
  }
  .statusbar, .session-strip, .tabs, #logout-btn, .ticker .src { display: none; }

  .topbar { flex-wrap: nowrap; gap: 10px; padding: 6px 12px; min-height: 48px; }
  .ticker { min-width: 0; flex: 0 1 auto; }
  .mode-pill { display: inline-flex; align-items: center; flex: none; }
  .mode-pill .dot { display: inline-block; margin-right: 5px; }
  .acct { margin-left: auto; gap: 12px; flex: none; }
  .acct div:first-child { display: none; } /* balance is on the Account tab */
  .acct label { font-size: 9.5px; }
  .acct b { font-size: 12.5px; }

  .main { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); overflow: hidden; }
  .chart-col { display: none; }
  .side { border-left: 0; min-height: 0; grid-template-rows: minmax(0, 1fr); }
  .tab-body { overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .tab { padding: 12px 12px 18px; }

  .app[data-view="monitor"] .main { grid-template-rows: minmax(240px, 52%) minmax(0, 1fr); }
  .app.detail .main { grid-template-rows: minmax(220px, 44%) minmax(0, 1fr); }
  .app[data-view="monitor"] .chart-col,
  .app.detail .chart-col { display: grid; grid-template-rows: auto minmax(0, 1fr); }
  .app[data-view="monitor"] .side,
  .app.detail .side { border-top: 1px solid var(--line); }
  .chart { min-height: 0; }

  .chart-toolbar { flex-wrap: nowrap; padding: 5px 8px; gap: 8px; }
  .toggles { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; min-width: 0; }
  .toggles::-webkit-scrollbar { display: none; }
  .chip, #btn-latest { flex: none; padding: 5px 10px; }
  .app.detail .chart-close { display: inline-flex; flex: none; }
  .app.detail .legend { display: none; }

  .mnav {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    border-top: 1px solid var(--line);
    background: var(--panel);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .mnav button {
    background: none; border: 0; color: var(--muted); cursor: pointer;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    min-height: 54px; padding: 6px 2px; font-size: 10.5px; font-weight: 600;
    -webkit-tap-highlight-color: transparent;
  }
  .mnav svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  .mnav button.active { color: var(--amber); }

  .detail-grid { grid-template-columns: repeat(3, 1fr); }
  .toasts { left: 12px; right: 12px; bottom: calc(66px + env(safe-area-inset-bottom)); max-width: none; }
  .mobile-only { display: block; }
  .btn, .chip, .seg button, .days label { min-height: 32px; }
}

/* Phones held sideways: chart and panel side by side, slimmer tab bar. */
@media (max-width: 960px) and (orientation: landscape) and (max-height: 540px) {
  .app[data-view="monitor"] .main,
  .app.detail .main { grid-template-rows: minmax(0, 1fr); grid-template-columns: minmax(0, 1fr) minmax(280px, 40%); }
  .app[data-view="monitor"] .side,
  .app.detail .side { border-top: 0; border-left: 1px solid var(--line); }
  .topbar { min-height: 40px; padding-top: 4px; padding-bottom: 4px; }
  .mnav button { min-height: 40px; flex-direction: row; gap: 6px; font-size: 11.5px; }
  .mnav svg { width: 18px; height: 18px; }
}

@media (max-width: 520px) {
  .topbar { gap: 8px; padding: 6px 10px; }
  .brand, .ticker .tf { display: none; }
  .ticker .sym { font-size: 12px; }
  .ticker .price { font-size: 14px; }
  .acct { gap: 10px; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .legend { display: none; }
  table.list { font-size: 12px; }
  table.list th, table.list td { padding: 5px 4px; }
}
