/* ===== variables ===== */
:root {
  --bg: #0d1117;
  --panel: #161b22;
  --line: #30363d;
  --line-soft: #21262d;
  --text: #e6edf3;
  --muted: #7d8590;
  --muted-2: #6e7681;
  --accent: #58a6ff;
  --pnl-up: #2ea043;
  --pnl-dn: #cf222e;
  --warn: #d29922;

  --src-tg: #58a6ff;
  --src-ib: #bc8cff;
  --src-cm: #ffa657;
  --src-oc: #56d364;
  --src-de: #f2cc60;
  --src-la: #ff7eb6;

  /* darkened cell base colors per source */
  --cell-tg: #1c5fa3;
  --cell-ib: #6f3fb1;
  --cell-cm: #b3651a;
  --cell-oc: #2d8049;
  --cell-de: #998138;
  --cell-la: #a64575;
  --cell-mixed: #444c56;
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 13px;
  height: 100vh;
  overflow: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.app-body {
  display: grid;
  grid-template-columns: 260px 1fr;
  height: 100vh;
  transition: grid-template-columns 0.18s ease;
}
.app-body.fullscreen-mode {
  grid-template-columns: 1fr;
}
.app-body.fullscreen-mode .sidebar,
.app-body.fullscreen-mode .topbar,
.app-body.fullscreen-mode .legend,
.app-body.fullscreen-mode .detail-panel,
.app-body.fullscreen-mode .audit-drawer {
  display: none !important;
}
.app-body.fullscreen-mode .main {
  padding: 0;
  gap: 0;
}
.app-body.fullscreen-mode .treemap-wrap {
  border: 0;
  border-radius: 0;
}

/* floating exit button shown only in fullscreen */
.fullscreen-exit {
  position: fixed;
  top: 10px;
  right: 12px;
  z-index: 30;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(13, 17, 23, 0.55);
  color: rgba(230, 237, 243, 0.85);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  transition: background 0.15s, color 0.15s, transform 0.15s;
}
.fullscreen-exit:hover {
  background: rgba(13, 17, 23, 0.85);
  color: #fff;
  transform: scale(1.08);
}

/* ===== sidebar ===== */
.sidebar {
  background: var(--bg);
  border-right: 1px solid var(--line-soft);
  padding: 14px 14px 24px;
  overflow-y: auto;
}
.sidebar-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  padding: 3px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
}
.sidebar-tab {
  flex: 1;
  text-align: center;
  padding: 5px 0;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  border-radius: 4px;
  user-select: none;
  transition: background 0.12s, color 0.12s;
}
.sidebar-tab:hover { color: var(--text); }
.sidebar-tab.on {
  background: var(--bg);
  color: var(--text);
  box-shadow: 0 0 0 1px var(--line);
}
.filter-section { margin-bottom: 18px; }
.filter-section h4 {
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin: 0 0 8px;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.filter-section h4 .clear {
  color: var(--accent);
  cursor: pointer;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  font-size: 11px;
}
.filter-row {
  display: flex; align-items: center; gap: 7px;
  padding: 4px 0; font-size: 12px; cursor: pointer;
  color: #c9d1d9;
  user-select: none;
}
.filter-row:hover { color: var(--text); }
.filter-row .cb {
  width: 13px; height: 13px;
  border: 1px solid #484f58;
  border-radius: 2px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.filter-row .cb.on,
.filter-row .cb.partial { background: var(--accent); border-color: var(--accent); }
.filter-row .cb.on::after {
  content: "";
  width: 7px;
  height: 4px;
  border-left: 1.5px solid #fff;
  border-bottom: 1.5px solid #fff;
  transform: translateY(-1px) rotate(-45deg);
}
.filter-row .cb.partial::after {
  content: "";
  width: 7px;
  height: 0;
  border-top: 1.5px solid #fff;
}
.filter-row .count {
  margin-left: auto;
  color: var(--muted-2);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}
.filter-row.indent { padding-left: 16px; font-size: 11px; color: #8b949e; }
.filter-row .acct-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.filter-row .debank-link-sm {
  font-size: 10px;
  padding: 0 4px;
  color: var(--src-onchain, #56d364);
  text-decoration: none;
  border: 1px solid rgba(86, 211, 100, 0.35);
  border-radius: 3px;
  background: rgba(86, 211, 100, 0.10);
  flex-shrink: 0;
  line-height: 1.4;
}
.filter-row .debank-link-sm:hover { background: rgba(86, 211, 100, 0.22); }

.src-dot {
  width: 9px; height: 9px;
  border-radius: 2px;
  flex-shrink: 0;
}
.dot-tg { background: var(--src-tg); }
.dot-ib { background: var(--src-ib); }
.dot-cm { background: var(--src-cm); }
.dot-oc { background: var(--src-oc); }
.dot-de { background: var(--src-de); }
.dot-la { background: var(--src-la); }
.dot-mixed { background: var(--cell-mixed); }

.toggle {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 0; font-size: 12px; cursor: pointer; user-select: none;
}
.toggle .switch {
  width: 28px; height: 16px;
  background: var(--line);
  border-radius: 8px;
  position: relative;
  transition: background 0.15s;
}
.toggle .switch.on { background: var(--accent); }
.toggle .switch::after {
  content: "";
  width: 12px; height: 12px;
  background: #fff;
  border-radius: 50%;
  position: absolute; top: 2px; left: 2px;
  transition: 0.15s;
}
.toggle .switch.on::after { left: 14px; }
.toggle-hint { font-size: 10px; color: var(--muted-2); margin: -4px 0 6px; }

.group-tabs { display: flex; flex-wrap: wrap; gap: 4px; }
.group-tab {
  padding: 4px 9px;
  font-size: 11px;
  border-radius: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  cursor: pointer;
  color: #8b949e;
  user-select: none;
}
.group-tab.on {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ===== main ===== */
.main {
  position: relative;
  padding: 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  min-height: 0;
}

.topbar { display: flex; align-items: baseline; gap: 22px; flex-wrap: wrap; }
.topbar .lbl {
  font-size: 9px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}
.topbar .total .num {
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.5px;
  font-variant-numeric: tabular-nums;
}
.topbar .total .drift {
  font-size: 10px;
  color: var(--muted);
  letter-spacing: 0.2px;
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
}
.topbar .total .ctx {
  font-size: 11px;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
}
.topbar .filtered .num-sm {
  font-size: 14px;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.topbar .day-pnl .num-sm {
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}
.topbar .day-pnl .num-sm.pnl-up { color: var(--pnl-up); }
.topbar .day-pnl .num-sm.pnl-dn { color: var(--pnl-dn); }
.topbar .stale {
  margin-left: auto;
  font-size: 11px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}
.topbar .refresh-btn {
  background: var(--panel);
  border: 1px solid var(--line);
  color: #c9d1d9;
  padding: 5px 12px;
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
}
.topbar .refresh-btn:hover { background: var(--line); }
.topbar .refresh-btn:disabled { opacity: 0.6; cursor: wait; }
.topbar .expand-btn {
  background: var(--panel);
  border: 1px solid var(--line);
  color: #c9d1d9;
  padding: 5px 12px;
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
}
.topbar .expand-btn:hover { background: var(--line); }

.audit-badge {
  font-size: 11px;
  color: var(--muted);
  padding: 4px 10px;
  border-radius: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  cursor: pointer;
  user-select: none;
}
.audit-badge.has {
  background: rgba(210, 153, 34, 0.15);
  color: var(--warn);
  border-color: var(--warn);
}
.audit-badge:hover { filter: brightness(1.15); }

.legend {
  display: flex;
  gap: 14px;
  font-size: 11px;
  color: var(--muted);
  align-items: center;
  flex-wrap: wrap;
}
.legend .item { display: flex; align-items: center; gap: 5px; }
.legend .item.right { margin-left: auto; color: var(--muted-2); }

/* ===== treemap ===== */
.treemap-wrap {
  position: relative;
  flex: 1;
  min-height: 320px;
  background: var(--bg);
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  overflow: hidden;
}
.treemap-empty, .treemap-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 13px;
  pointer-events: none;
}

.cell {
  position: absolute;
  border-radius: 3px;
  padding: 6px 8px 8px;
  color: #fff;
  overflow: hidden;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transition: outline 0.1s;
  background: var(--cell-mixed);
  outline: 1px solid rgba(0,0,0,0.35);
  outline-offset: -1px;
}
.cell:hover { outline: 2px solid var(--accent); outline-offset: -2px; z-index: 2; }
.cell.selected { outline: 2px solid var(--accent); outline-offset: -2px; z-index: 2; }
.cell.flagged { outline: 1px dashed var(--src-cm); outline-offset: -2px; }
.cell.unpriced { background: linear-gradient(135deg, var(--cell-mixed) 0%, #2a2f36 100%); color: #b8bfc8; }

.cell .ticker { font-size: 11px; font-weight: 600; letter-spacing: 0; line-height: 1.2; }
.cell .name { font-size: 10px; opacity: 0.85; line-height: 1.2; }
.cell .qty { font-size: 10px; opacity: 0.8; font-variant-numeric: tabular-nums; line-height: 1.2; }
.cell-content {
  position: relative;
  z-index: 2;
  overflow: hidden;
  pointer-events: none;
}
.cell-footer {
  position: relative;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  align-items: end;
  font-size: 10px;
  opacity: 0.9;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
.cell.large .ticker { font-size: 16px; }
.cell.large .name { font-size: 12px; }
.cell.large .qty { font-size: 12px; }
.cell.large .cell-footer { font-size: 13px; font-weight: 500; }
.cell.medium .ticker { font-size: 13px; }
.cell.tiny .ticker { font-size: 9px; }
.cell.tiny .name, .cell.tiny .qty, .cell.tiny .cell-footer { display: none; }
.cell.micro .ticker { font-size: 8px; }
.cell.micro .name, .cell.micro .qty, .cell.micro .cell-footer { display: none; }

.cell .subcells {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
}
.cell .subcell {
  position: absolute;
  border-right: 1px solid rgba(255,255,255,0.24);
  border-bottom: 1px solid rgba(255,255,255,0.24);
  overflow: hidden;
}
.cell.dark-text .subcell {
  border-color: rgba(10, 20, 33, 0.2);
}
.cell .subcell span {
  display: block;
  position: absolute;
  padding: 4px 6px;
  font-size: 10px;
  font-weight: 700;
  line-height: 1.1;
  opacity: 0.68;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.cell .subcell.compact span {
  font-size: 9px;
  padding: 3px 5px;
}

/* cell background is set via inline style by app.js (stable key palette).
   bg-mixed is the fallback when no value-based color was computed.
   dark-text flips text to near-black on bright (yellow/orange) cells. */
.cell.bg-mixed { background: var(--cell-mixed); }
.cell.dark-text { color: #1e1b4b; }
.cell.dark-text .stale-mark { color: #1e1b4b; }

/* ===== detail panel ===== */
.detail-panel {
  position: absolute;
  right: 18px; bottom: 18px;
  width: 420px;
  max-height: 60%;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.55);
  z-index: 10;
}
.detail-panel .head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 10px;
}
.detail-panel h3 { margin: 0; font-size: 14px; color: var(--text); }
.detail-panel .close {
  color: var(--muted); cursor: pointer; font-size: 18px; line-height: 1;
}
.detail-panel .big-row {
  display: flex; justify-content: space-between;
  font-size: 16px; font-weight: 600; margin-bottom: 4px;
}
.detail-panel .big-row .pnl { color: var(--pnl-up); }
.detail-panel .meta-row {
  display: flex; justify-content: space-between;
  font-size: 11px; color: var(--muted); margin-bottom: 6px;
  font-variant-numeric: tabular-nums;
}
.detail-panel .meta-row .lbl-sm { font-size: 9px; text-transform: uppercase; letter-spacing: 0.6px; }
.detail-panel .meta-row .pnl-up { color: var(--pnl-up); }
.detail-panel .meta-row .pnl-dn { color: var(--pnl-dn); }
.detail-row {
  display: grid;
  grid-template-columns: 14px 1fr 90px 50px;
  padding: 5px 0;
  font-size: 11px;
  gap: 8px;
  align-items: center;
}
.detail-row .qty {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: #c9d1d9;
}
.detail-row .pct {
  text-align: right;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  font-size: 10px;
}
.detail-row .src-label {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.detail-row .src-label > span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.detail-row .debank-link {
  flex-shrink: 0;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 4px;
  background: rgba(86, 211, 100, 0.12);
  color: var(--src-onchain, #56d364);
  text-decoration: none;
  border: 1px solid rgba(86, 211, 100, 0.35);
  line-height: 1.4;
}
.detail-row .debank-link:hover {
  background: rgba(86, 211, 100, 0.22);
}
.detail-row.finding-row {
  display: block;
  color: var(--warn);
  font-size: 11px;
  padding: 4px 0;
}
.section-h {
  font-size: 9px;
  text-transform: uppercase;
  color: var(--muted);
  letter-spacing: 0.6px;
  margin-top: 12px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}

/* ===== audit drawer ===== */
.audit-drawer {
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: 420px;
  background: var(--panel);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  z-index: 20;
  box-shadow: -8px 0 24px rgba(0,0,0,0.4);
}
.drawer-head {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.drawer-head h3 { margin: 0; font-size: 14px; }
.drawer-head .close { cursor: pointer; font-size: 20px; color: var(--muted); }
.drawer-tabs {
  display: flex;
  border-bottom: 1px solid var(--line);
  padding: 0 16px;
}
.drawer-tab {
  padding: 8px 12px;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
}
.drawer-tab.on { color: var(--text); border-bottom-color: var(--accent); }
.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px 24px;
}
.drawer-summary {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
}
.badge {
  font-size: 10.5px;
  padding: 3px 7px;
  border-radius: 10px;
  background: var(--line-soft);
  color: var(--muted);
}
.badge.err  { background: rgba(207, 34, 46, 0.2); color: #ff7b72; }
.badge.warn { background: rgba(210, 153, 34, 0.2); color: var(--warn); }
.badge.info { background: rgba(88, 166, 255, 0.2); color: var(--accent); }
.badge.ok   { background: rgba(46, 160, 67, 0.2); color: var(--pnl-up); }

.finding {
  background: var(--bg);
  border: 1px solid var(--line);
  border-left: 3px solid var(--muted);
  border-radius: 4px;
  padding: 8px 10px;
  margin-bottom: 8px;
}
.finding.sev-error   { border-left-color: var(--pnl-dn); }
.finding.sev-warning { border-left-color: var(--warn); }
.finding.sev-info    { border-left-color: var(--accent); }
.finding-head {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 4px;
}
.finding .rule { font-family: ui-monospace, "SF Mono", Menlo, monospace; }
.finding .sev  { text-transform: uppercase; font-size: 10px; }
.finding .msg  { font-size: 12px; color: var(--text); margin-bottom: 4px; }
.finding .fix  { font-size: 11px; color: var(--accent); margin-bottom: 4px; }
.finding .refs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  font-size: 10px;
  color: var(--muted-2);
}
.finding .ref {
  background: var(--line-soft);
  padding: 1px 6px;
  border-radius: 3px;
  font-family: ui-monospace, monospace;
  font-size: 10px;
}

/* scrollbar polish */
.sidebar::-webkit-scrollbar,
.drawer-body::-webkit-scrollbar,
.detail-panel::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb,
.drawer-body::-webkit-scrollbar-thumb,
.detail-panel::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }

.toggle .switch.partial {
  background: linear-gradient(90deg, var(--accent) 0%, var(--accent) 50%, var(--line) 50%, var(--line) 100%);
}
.merge-divider {
  border-top: 1px solid var(--line-soft);
  margin: 6px 0;
}

.focus-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--accent);
  color: #fff;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  user-select: none;
}
.focus-chip:hover { filter: brightness(1.1); }
.focus-chip .x {
  font-size: 14px;
  line-height: 1;
  margin-left: 2px;
}

/* ===== stale indicators ===== */
.stale-mark {
  position: absolute;
  top: 4px; right: 6px;
  font-size: 11px;
  color: #f1c757;
  pointer-events: auto;
}
.stale-suffix {
  margin-left: 6px;
  font-size: 11px;
  color: var(--muted-2);
}
.detail-row.stale-row {
  background: rgba(210, 153, 34, 0.08);
}
.stale-tag {
  grid-column: 1 / -1;
  font-size: 10px;
  color: var(--warn);
  font-style: italic;
}
