:root {
  --bg: #0e1320;
  --panel: #141a2a;
  --panel-2: #1b2236;
  --border: #252e47;
  --text: #dfe5f2;
  --muted: #8a94ad;
  --accent: #2ec4a7;
  --accent-ink: #062b24;
  --up: #22b38a;
  --down: #ec5b6e;
  --grid: #1a2133;
  --shadow: 0 10px 40px rgba(0, 0, 0, .45);
  --radius: 10px;
  --font: "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif;
}

:root[data-theme="light"] {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-2: #eef1f7;
  --border: #dde2ec;
  --text: #1b2333;
  --muted: #667089;
  --accent: #0e9f86;
  --accent-ink: #ffffff;
  --up: #129c75;
  --down: #d9435a;
  --grid: #eef1f6;
  --shadow: 0 10px 40px rgba(20, 30, 60, .15);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 16px; margin-inline-end: 6px; }
.spacer { flex: 1; }
.btn, .symbol-btn, .icon-btn {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; height: 34px;
}
.btn:hover, .symbol-btn:hover, .icon-btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.symbol-btn { font-weight: 600; min-width: 130px; }
.icon-btn { padding: 6px 8px; justify-content: center; min-width: 34px; }
.icon-btn.small { height: 28px; min-width: 28px; padding: 2px 6px; font-size: 18px; line-height: 1; }
.tf-group { display: inline-flex; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.tf-group button { background: none; border: 0; padding: 6px 10px; height: 32px; color: var(--muted); }
.tf-group button.active { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.wl-toggle { display: none; }

/* ---------- layout ---------- */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 48px 1fr 270px;
  grid-template-areas: "tools chart wl";
}
.tools { grid-area: tools; display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 0; background: var(--panel); border-inline-end: 1px solid var(--border); }
.tools button { width: 36px; height: 36px; display: grid; place-items: center; background: none; border: 1px solid transparent; border-radius: 8px; color: var(--muted); }
.tools button:hover { color: var(--text); background: var(--panel-2); }
.tools button.active { color: var(--accent); border-color: var(--accent); background: var(--panel-2); }
.tool-sep { width: 24px; height: 1px; background: var(--border); margin: 6px 0; }

.chart-area { grid-area: chart; min-width: 0; min-height: 0; position: relative; }
.chart-wrap { position: absolute; inset: 0; }
.chart { position: absolute; inset: 0; }
.draw-layer { position: absolute; left: 0; top: 0; z-index: 3; pointer-events: none; touch-action: none; }
.legend {
  position: absolute; left: 10px; top: 8px; z-index: 4; pointer-events: none;
  font-size: 12px; line-height: 1.6; color: var(--muted); font-family: ui-monospace, Consolas, monospace;
  text-shadow: 0 0 6px var(--bg);
}
.legend .sym { color: var(--text); font-weight: 600; font-family: var(--font); font-size: 14px; margin-right: 8px; }
.legend .up { color: var(--up); } .legend .down { color: var(--down); }
.legend .row { white-space: nowrap; }
.legend b { font-weight: 500; color: var(--text); }
.status {
  position: absolute; inset: 0; z-index: 5; display: grid; place-items: center; text-align: center; padding: 20px;
  background: color-mix(in srgb, var(--bg) 70%, transparent); color: var(--text); font-family: var(--font); direction: rtl;
}
html[dir="ltr"] .status { direction: ltr; }

/* ---------- watchlist ---------- */
.watchlist { grid-area: wl; background: var(--panel); border-inline-start: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.wl-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.wl-head h2 { font-size: 14px; margin: 0; font-weight: 600; }
.wl-list { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1; }
.wl-list li {
  display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center;
  padding: 8px 12px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.wl-list li:hover { background: var(--panel-2); }
.wl-list li.active { background: var(--panel-2); box-shadow: inset 3px 0 0 var(--accent); }
html[dir="rtl"] .wl-list li.active { box-shadow: inset -3px 0 0 var(--accent); }
.wl-sym { font-weight: 600; direction: ltr; text-align: start; }
.wl-sym small { display: block; color: var(--muted); font-weight: 400; font-size: 11px; }
.wl-price { font-family: ui-monospace, Consolas, monospace; direction: ltr; text-align: end; }
.wl-price small { display: block; font-size: 11px; }
.wl-price .up { color: var(--up); } .wl-price .down { color: var(--down); }
.wl-del { background: none; border: 0; color: var(--muted); font-size: 16px; padding: 0 2px; opacity: 0; }
.wl-list li:hover .wl-del { opacity: 1; }
.wl-foot a { color: var(--muted); }
.wl-foot { margin: 0; padding: 8px 12px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--border); }

/* ---------- dialogs ---------- */
.dlg {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--panel); color: var(--text);
  width: min(560px, calc(100vw - 24px)); max-height: min(80vh, 720px); box-shadow: var(--shadow);
}
.dlg::backdrop { background: rgba(0, 0, 0, .5); }
.dlg-inner { display: flex; flex-direction: column; max-height: min(80vh, 720px); padding: 14px; gap: 10px; }
.dlg-head { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.dlg-head h3 { margin: 0; font-size: 16px; }
.dlg h4 { margin: 6px 0 0; font-size: 13px; color: var(--muted); font-weight: 500; }
.dlg input[type="search"], .dlg input[type="password"], .dlg input[type="text"], .dlg input[type="number"] {
  width: 100%; background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 9px 10px; font: inherit;
}
.dlg input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.search-tabs { display: flex; gap: 6px; }
.search-tabs button { background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: 4px 12px; color: var(--muted); }
.search-tabs button.active { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }
.search-results { list-style: none; margin: 0; padding: 0; overflow: auto; min-height: 200px; }
.search-results li { display: flex; align-items: center; gap: 10px; padding: 9px 8px; border-radius: 8px; cursor: pointer; }
.search-results li:hover, .search-results li.hl { background: var(--panel-2); }
.search-results .s-sym { font-weight: 600; direction: ltr; }
.search-results .s-name { color: var(--muted); font-size: 12px; flex: 1; }
.search-results .s-tag { font-size: 11px; color: var(--muted); border: 1px solid var(--border); border-radius: 4px; padding: 1px 6px; }
.search-results .s-star { background: none; border: 0; color: var(--muted); font-size: 18px; }
.search-results .s-star.on { color: #f4b740; }
.ind-catalog { display: flex; flex-wrap: wrap; gap: 6px; }
.ind-catalog button { background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; }
.ind-catalog button:hover { border-color: var(--accent); }
.ind-active { list-style: none; margin: 0; padding: 0; overflow: auto; display: flex; flex-direction: column; gap: 6px; }
.ind-active li { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px; }
.ind-active .ind-name { font-weight: 600; min-width: 90px; }
.ind-active label { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.ind-active input[type="number"] { width: 64px; padding: 4px 6px; }
.ind-active input[type="color"] { width: 26px; height: 26px; border: 0; background: none; padding: 0; }
.ind-active .ind-del { margin-inline-start: auto; background: none; border: 0; color: var(--down); font-size: 16px; }
.ind-empty { color: var(--muted); font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.hint { margin: 0; color: var(--muted); font-size: 12px; }
.dlg-actions { display: flex; justify-content: flex-end; }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .topbar { gap: 6px; padding: 6px 8px; }
  .brand-name { display: none; }
  .symbol-btn { min-width: 0; flex: 1; }
  .tf-group { order: 10; width: 100%; justify-content: space-between; }
  .tf-group button { flex: 1; }
  #indBtn span { display: none; }
  .spacer { display: none; }
  .wl-toggle { display: inline-flex; }
  .layout { grid-template-columns: 1fr; grid-template-rows: 1fr auto; grid-template-areas: "chart" "tools"; }
  .tools { flex-direction: row; justify-content: center; border: 0; border-top: 1px solid var(--border); padding: 4px 0 max(4px, env(safe-area-inset-bottom)); }
  .tool-sep { width: 1px; height: 24px; margin: 0 6px; }
  .watchlist {
    position: fixed; inset-block: 0; inset-inline-end: 0; width: min(300px, 85vw); z-index: 20;
    box-shadow: var(--shadow); display: none;
  }
  body.wl-open .watchlist { display: flex; }
  .wl-del { opacity: 1; }
}
