:root {
  --bg: #0d1117; --bg2: #161b22; --bg3: #1c2330; --border: #2a3140;
  --text: #e6edf3; --muted: #8b98a9; --accent: #2f7d5b; --accent2: #3fb27f;
  --up: #3fb27f; --down: #f2545b; --warn: #e3b341; --blue: #4d8fe0;
  --radius: 10px; --shadow: 0 2px 12px rgba(0,0,0,.35);
  color-scheme: dark;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
/* Light theme — activated by data-theme="light" on <html> (persisted in localStorage). */
:root[data-theme="light"] {
  --bg: #f4f6f9; --bg2: #ffffff; --bg3: #eaeef3; --border: #d6dce4;
  --text: #12181f; --muted: #5b6672; --accent: #1f7a54; --accent2: #2f9e6d;
  --up: #178a54; --down: #d1343b; --warn: #a5710a; --blue: #1667c7;
  --shadow: 0 2px 10px rgba(20,30,50,.10);
  color-scheme: light;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px;
  -webkit-text-size-adjust: 100%; }
body, .card, .sidebar, .topbar, .rec, .chip, .toast, input, select, textarea {
  transition: background-color .2s ease, color .2s ease, border-color .2s ease;
}
a { color: var(--blue); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea {
  font-family: inherit; background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px; font-size: 14px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent2); }

/* --- Auth --- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 32px; width: 100%; max-width: 380px; box-shadow: var(--shadow); }
.auth-card h1 { margin: 0 0 4px; font-size: 22px; }
.auth-card .sub { color: var(--muted); margin: 0 0 22px; font-size: 13px; }
.auth-card label { display: block; font-size: 12px; color: var(--muted); margin: 14px 0 5px; }
.demo-hint { margin-top: 16px; font-size: 12px; color: var(--muted); text-align: center; }

/* --- Layout --- */
.layout { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: var(--bg2); border-right: 1px solid var(--border); padding: 16px 12px;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { font-weight: 800; font-size: 18px; padding: 6px 10px 18px; display: flex; align-items: center; gap: 8px; }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.nav a { color: var(--muted); padding: 9px 12px; border-radius: 8px; display: flex; align-items: center;
  gap: 10px; font-weight: 500; }
.nav a:hover { background: var(--bg3); color: var(--text); }
.nav a.active { background: var(--accent); color: #fff; }
.nav .ico { width: 18px; text-align: center; }
.sidebar-foot { border-top: 1px solid var(--border); padding-top: 12px; font-size: 12px; color: var(--muted); }
.main { display: flex; flex-direction: column; min-width: 0; }

/* --- Topbar --- */
.topbar { display: flex; align-items: center; gap: 14px; padding: 12px 22px; background: var(--bg2);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.topbar .pagetitle { font-size: 18px; font-weight: 700; }
.topbar .spacer { flex: 1; }
.pv { text-align: right; } .pv .lbl { font-size: 11px; color: var(--muted); } .pv .val { font-weight: 700; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; flex: none; }
.dot.live { background: var(--up); box-shadow: 0 0 8px var(--up); }
.content { padding: 22px; }

/* Icon buttons (hamburger, theme) */
.iconbtn { background: var(--bg3); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; width: 38px; height: 38px; min-width: 38px; display: inline-flex;
  align-items: center; justify-content: center; font-size: 17px; line-height: 1; padding: 0; }
.iconbtn:hover { background: var(--bg); }
.hamburger { display: none; }  /* mobile only (see media query) */

/* Floating theme toggle — present on every screen incl. login. z-index sits BELOW
   the mobile drawer (50) + backdrop (40) so an open drawer cleanly covers it, but
   above normal content. */
.theme-toggle { position: fixed; left: 16px; bottom: 16px; z-index: 30;
  width: 42px; height: 42px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--bg2); color: var(--text); font-size: 19px; box-shadow: var(--shadow); }
.theme-toggle:hover { background: var(--bg3); }
/* In-app the toggle lives in the topbar (#themebtn-top); hide the floating one so it
   never overlaps the sidebar. On the login screen (no app shell) it stays floating. */
body.app-shell .theme-toggle { display: none; }

/* Drawer backdrop (mobile) */
.backdrop { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 40; }

/* --- Automation pill + kill switch --- */
.pill { padding: 5px 11px; border-radius: 20px; font-size: 12px; font-weight: 600; border: 1px solid var(--border); }
.pill.advisor { color: var(--blue); border-color: var(--blue); }
.pill.semi_auto { color: var(--warn); border-color: var(--warn); }
.pill.full_auto { color: var(--up); border-color: var(--up); }
.kill { background: var(--down); color: #fff; border: none; padding: 7px 12px; border-radius: 8px; font-weight: 700; }
.kill.off { background: var(--bg3); color: var(--muted); }

/* --- Cards / grid --- */
.grid { display: grid; gap: 16px; }
.cols-4 { grid-template-columns: repeat(4, 1fr); }
.cols-3 { grid-template-columns: repeat(3, 1fr); }
.cols-2 { grid-template-columns: repeat(2, 1fr); }

/* Card grids reflow down as width shrinks. */
@media (max-width: 1000px) { .cols-4, .cols-3 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 640px)  { .cols-4, .cols-3, .cols-2 { grid-template-columns: 1fr; } }

/* Tablet (769–1000px): collapse the sidebar to an icon rail. */
@media (min-width: 769px) and (max-width: 1000px) {
  .layout { grid-template-columns: 68px 1fr; }
  .brand span, .nav a span, .sidebar-foot { display: none; }
  .brand { justify-content: center; } .nav a { justify-content: center; }
}

/* Mobile (≤768px): sidebar becomes an off-canvas drawer + hamburger. */
@media (max-width: 768px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: 0; left: 0; height: 100vh; height: 100dvh; width: 250px; z-index: 50;
    transform: translateX(-100%); transition: transform .25s ease; box-shadow: var(--shadow);
  }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .backdrop { display: block; }
  .hamburger { display: inline-flex; }
  .topbar { padding: 10px 14px; gap: 10px; flex-wrap: wrap; }
  .topbar .pagetitle { font-size: 16px; }
  .content { padding: 14px; }
  .pv .lbl { font-size: 10px; }
  .theme-toggle { width: 40px; height: 40px; }
}

/* Phones (≤560px): drop the least-critical topbar items so nothing overflows. */
@media (max-width: 560px) {
  #autopill, .pv.pv-portfolio { display: none; }
  .kill { padding: 6px 10px; font-size: 12px; }
}
.card { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card h3 { margin: 0 0 12px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat .big { font-size: 26px; font-weight: 800; }
.stat .sub { font-size: 12px; color: var(--muted); margin-top: 3px; }

/* --- Tables --- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 11px; text-transform: uppercase; color: var(--muted); letter-spacing: .04em; }
tbody tr:hover { background: var(--bg3); }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* --- Misc --- */
.up { color: var(--up); } .down { color: var(--down); } .muted { color: var(--muted); }
.btn { background: var(--accent); color: #fff; border: none; padding: 9px 15px; border-radius: 8px; font-weight: 600; }
.btn:hover { background: var(--accent2); }
.btn.sm { padding: 5px 10px; font-size: 12px; }
.btn.ghost { background: var(--bg3); color: var(--text); }
.btn.buy { background: var(--up); } .btn.sell { background: var(--down); }
.btn.danger { background: var(--down); }
.badge { padding: 2px 8px; border-radius: 6px; font-size: 11px; font-weight: 600; background: var(--bg3); }
.badge.filled { color: var(--up); } .badge.pending { color: var(--warn); }
.badge.rejected, .badge.cancelled { color: var(--down); } .badge.open { color: var(--blue); }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.section-title { font-size: 16px; font-weight: 700; margin: 4px 0 14px; }
.rec { border: 1px solid var(--border); border-radius: 10px; padding: 14px; margin-bottom: 12px; background: var(--bg3); }
.rec .hd { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.rec .sym { font-size: 16px; font-weight: 700; }
.conf { font-size: 12px; color: var(--muted); }
.meter { height: 6px; border-radius: 4px; background: var(--bg); overflow: hidden; margin: 6px 0; }
.meter > span { display: block; height: 100%; background: var(--accent2); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.chip { background: var(--bg); border: 1px solid var(--border); padding: 3px 8px; border-radius: 20px; font-size: 11px; color: var(--muted); }
.chat-log { display: flex; flex-direction: column; gap: 10px; max-height: 55vh; overflow-y: auto; padding: 4px; }
.msg { padding: 10px 14px; border-radius: 12px; max-width: 80%; white-space: pre-wrap; line-height: 1.5; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; }
.msg.assistant { align-self: flex-start; background: var(--bg3); border: 1px solid var(--border); }
.empty { color: var(--muted); text-align: center; padding: 30px; }
.disclaimer { font-size: 11px; color: var(--muted); text-align: center; padding: 14px; }
label.f { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 5px; }

/* toasts */
#toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 10px; z-index: 100; }
.toast { background: var(--bg2); border: 1px solid var(--border); border-left: 3px solid var(--accent2);
  padding: 12px 16px; border-radius: 8px; box-shadow: var(--shadow); min-width: 240px; animation: slidein .25s ease; }
.toast.exit { border-left-color: var(--warn); } .toast.order { border-left-color: var(--up); }
.toast .t { font-weight: 700; font-size: 13px; } .toast .b { font-size: 12px; color: var(--muted); margin-top: 2px; }
@keyframes slidein { from { transform: translateX(40px); opacity: 0; } to { transform: none; opacity: 1; } }
svg.spark { display: block; }
