:root {
  --bg0:    #0a0c10;
  --bg1:    #0f1218;
  --bg2:    #141820;
  --bg3:    #1a1f2a;
  --border: #232a38;
  --border2:#2e3748;
  --tx0:    #e8ecf4;
  --tx1:    #8b95aa;
  --tx2:    #4d5668;
  --c-new:   #2a78d6;
  --c-used:  #22c55e;
  --c-parts: #f5a623;
  --c-refurb: #8b5cf6;
  --c-scalp: #eab308;
  --c-down:  #ef4444;
  --c-up:    #22c55e;
  --c-flat:  #4d5668;
  --mono: 'IBM Plex Mono', monospace;
  --sans: 'Inter', sans-serif;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html,body { background: var(--bg0); color: var(--tx0); font-family: var(--sans); font-size: 13px; height: 100%; overflow: hidden; }
body { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
#topbar, #filterbar { flex-shrink: 0; }

/* ---- Top bar ---- */
#topbar {
  height: 56px; background: var(--bg1); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 0; padding: 0;
  flex-shrink: 0;
}
.tb-logo {
  display: flex; align-items: center; gap: 8px;
  padding: 0 20px; border-right: 1px solid var(--border);
  height: 100%; font-family: var(--mono); font-size: 13px; font-weight: 600;
  white-space: nowrap;
}
.tb-logo-mark {
  width: 50px; height: 50px;
  display: block; object-fit: contain;
}
.tb-nav { display:flex; height:100%; }
.tb-nav a {
  display:flex; align-items:center; padding: 0 16px;
  color: var(--tx1); font-size: 12px; border-right: 1px solid var(--border);
  text-decoration: none; white-space: nowrap;
}
.tb-nav a.active, .tb-nav a:hover { color: var(--tx0); background: var(--bg2); }
.tb-spacer { flex:1; }
.tb-social { display:flex; align-items:center; gap: 8px; padding: 0 16px; border-left: 1px solid var(--border); }
.tb-social a {
  width: 28px; height: 28px; border-radius: 5px;
  display:flex; align-items:center; justify-content:center;
  color: var(--tx1); background: var(--bg2); border: 1px solid var(--border);
  font-size: 13px; text-decoration: none; transition: color 0.1s, border-color 0.1s;
}
.tb-social a:hover { color: var(--tx0); border-color: var(--border2); }
.tb-social a.discord:hover { color: #5865F2; border-color: #5865F2; }
.tb-social a.youtube:hover { color: #FF0000; border-color: #FF0000; }
.tb-stat {
  display:flex; align-items:center; gap: 6px;
  padding: 0 16px; border-left: 1px solid var(--border);
  font-family: var(--mono); font-size: 11px; color: var(--tx1);
}
.tb-stat .val { color: var(--tx0); font-weight: 500; }

/* ---- Filter bar ---- */
#filterbar {
  height: 38px; background: var(--bg1); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px; padding: 0 12px;
  flex-shrink: 0;
}
.fb-label { font-family: var(--mono); font-size: 10px; color: var(--tx2); text-transform: uppercase; letter-spacing: .05em; white-space: nowrap; }
input.fb-search {
  height: 26px; background: var(--bg2); border: 1px solid var(--border); border-radius: 4px;
  padding: 0 10px; color: var(--tx0); font-family: var(--mono); font-size: 12px;
  width: 220px;
}
input.fb-search:focus { outline: none; border-color: var(--c-used); }
input.fb-search::placeholder { color: var(--tx2); }
select.fb-select {
  height: 26px; background: var(--bg2); border: 1px solid var(--border); border-radius: 4px;
  padding: 0 8px; color: var(--tx0); font-family: var(--mono); font-size: 11px; cursor: pointer;
}
.fb-sep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
.fb-spacer { flex:1; }

/* ---- Search syntax hint ---- */
.fb-hint {
  width: 18px; height: 18px; border-radius: 50%; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--tx2); font-family: var(--mono); font-size: 10px;
  cursor: help; user-select: none;
}
.fb-hint:hover { color: var(--tx0); border-color: var(--border2); }

/* ---- Favorites star ---- */
.fav-star {
  display: inline-block; width: 16px; margin-right: 6px;
  color: var(--tx2); cursor: pointer; font-size: 13px;
  transition: color 0.1s, transform 0.1s;
}
.fav-star:hover { transform: scale(1.25); color: #eab308; }
.fav-star.on { color: #eab308; }

/* ---- Saved searches ---- */
.fb-saved-del {
  height: 26px; width: 26px; border-radius: 4px;
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--tx2); font-family: var(--mono); font-size: 12px; cursor: pointer;
}
.fb-saved-del:hover { color: var(--c-down); border-color: var(--c-down); }
.fb-saved-save {
  height: 26px; padding: 0 8px; border-radius: 4px;
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--tx1); font-family: var(--mono); font-size: 11px; cursor: pointer;
  white-space: nowrap;
}
.fb-saved-save:hover { color: var(--tx0); border-color: var(--border2); }

/* ---- Category multi-select dropdown ---- */
.fb-cat-dropdown { position: relative; }
.fb-cat-toggle {
  height: 26px; padding: 0 10px; border-radius: 4px;
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--tx0); font-family: var(--mono); font-size: 11px;
  cursor: pointer; white-space: nowrap; display: flex; align-items: center; gap: 6px;
}
.fb-cat-toggle:hover { border-color: var(--border2); }
.fb-cat-toggle.has-filter { border-color: var(--c-used); color: var(--c-used); }
#deal-toggle.has-filter { border-color: #f97316; color: #f97316; }
.fb-cat-toggle .caret { font-size: 9px; opacity: 0.7; }
.fb-cat-panel {
  display: none; position: absolute; top: calc(100% + 4px); left: 0; z-index: 20;
  background: var(--bg2); border: 1px solid var(--border2); border-radius: 5px;
  padding: 6px; min-width: 170px; max-height: 260px; overflow-y: auto;
  box-shadow: 0 8px 24px rgba(0,0,0,.4);
}
.fb-cat-panel.open { display: block; }
.fb-cat-item {
  display: flex; align-items: center; gap: 7px;
  padding: 5px 6px; border-radius: 3px; cursor: pointer;
  font-family: var(--mono); font-size: 11px; color: var(--tx1);
}
.fb-cat-item:hover { background: var(--bg3); color: var(--tx0); }
.fb-cat-item input { accent-color: var(--c-used); cursor: pointer; }
.fb-cat-panel-footer {
  border-top: 1px solid var(--border); margin-top: 4px; padding-top: 4px;
  text-align: right;
}
.fb-cat-clear {
  font-family: var(--mono); font-size: 10px; color: var(--tx2);
  background: none; border: none; cursor: pointer; padding: 4px 6px;
}
.fb-cat-clear:hover { color: var(--tx0); }
.fb-count { font-family: var(--mono); font-size: 11px; color: var(--tx2); }

/* ---- Main layout ---- */
#main { display: flex; flex-direction: column; flex: 1; min-height: 0; }
#table-wrap { flex: 1; overflow-y: auto; overflow-x: auto; -webkit-overflow-scrolling: touch; min-height: 0; }
#chart-panel {
  flex-shrink: 0; background: var(--bg1);
  border-top: 2px solid var(--border2);
  display: none;
  flex-direction: column;
}
#chart-panel.open { display: flex; height: 300px; }

/* ---- Table ---- */
table#t {
  width: 100%; border-collapse: collapse;
  font-family: var(--mono); font-size: 12px;
}
table#t thead tr {
  background: var(--bg1); position: sticky; top: 0; z-index: 5;
  border-bottom: 1px solid var(--border2);
}
table#t th {
  padding: 7px 12px; text-align: left;
  color: var(--tx2); font-size: 10px; font-weight: 500; text-transform: uppercase; letter-spacing: .05em;
  white-space: nowrap; cursor: pointer; user-select: none;
}
table#t th:hover { color: var(--tx1); }
table#t th.sorted { color: var(--tx0); }
table#t th .sort-arrow { margin-left: 4px; opacity: 0.5; }
table#t th.sorted .sort-arrow { opacity: 1; }
table#t td { padding: 6px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; vertical-align: middle; }
table#t .right { text-align: right; }
table#t td.right { font-family: var(--mono); }
table#t tr.data-row { cursor: pointer; }
table#t tr.data-row:hover td { background: var(--bg2); }
table#t tr.data-row.selected td { background: var(--bg3); border-bottom-color: var(--border2); }
table#t tr.data-row.selected td:first-child { border-left: 2px solid var(--c-used); }

.cell-name { font-family: var(--sans); font-size: 13px; color: var(--tx0); }
.cell-cat {
  display: inline-block; font-size: 10px; padding: 1px 6px; border-radius: 3px;
  background: var(--bg3); color: var(--tx2); margin-top: 2px; font-family: var(--mono);
}
.cell-new  { color: var(--c-new); }
.cell-used { color: var(--c-used); }
.cell-parts { color: var(--c-parts); }
.cell-refurb { color: var(--c-refurb); }
.cell-demand { color: var(--tx1); }
.cell-margin-high { color: var(--c-used); font-weight: 500; }
.cell-margin-mid  { color: var(--c-parts); }
.cell-margin-low  { color: var(--tx1); }
.cell-na  { color: var(--tx2); }

.trend-badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; padding: 2px 6px; border-radius: 3px;
}
.trend-up   { background: rgba(34,197,94,.12);  color: var(--c-used); }
.trend-down { background: rgba(239,68,68,.12);  color: var(--c-down); }
.trend-flat { background: var(--bg3); color: var(--tx2); }
.trend-none { color: var(--tx2); font-size: 11px; }

/* ---- Chart panel ---- */
#chart-header {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 16px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
#chart-title { font-family: var(--mono); font-size: 12px; color: var(--tx0); flex: 1; }
.chart-close {
  width: 22px; height: 22px; border-radius: 3px;
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--tx1); cursor: pointer; display:flex;align-items:center;justify-content:center;
  font-size: 14px; line-height: 1;
}
.chart-close:hover { color: var(--tx0); border-color: var(--border2); }

.series-toggles { display: flex; gap: 8px; }
.stoggle {
  display: flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 3px; cursor: pointer;
  border: 1px solid var(--border); background: var(--bg2);
  font-family: var(--mono); font-size: 11px; color: var(--tx1);
  user-select: none;
}
.stoggle.on  { border-color: var(--series-color); color: var(--tx0); }
.stoggle .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--series-color); opacity: 0.4; }
.stoggle.on .dot { opacity: 1; }
/* Per-series color via data-series, not inline style (CSP style-src stays free of 'unsafe-inline') */
.stoggle[data-series="new"]    { --series-color: var(--c-new); }
.stoggle[data-series="used"]   { --series-color: var(--c-used); }
.stoggle[data-series="parts"]  { --series-color: var(--c-parts); }
.stoggle[data-series="refurb"] { --series-color: var(--c-refurb); }
.stoggle[data-series="scalp"]  { --series-color: var(--c-scalp); }
.hidden { display: none; }

#chart-body { flex: 1; display: flex; flex-direction: row; min-height: 0; }
#chart-canvas-wrap { flex: 1 1 0; padding: 12px 16px 8px; min-height: 0; min-width: 0; position: relative; }
#priceChart { width: 100% !important; height: 100% !important; }
#chart-placeholder {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--tx2); font-family: var(--mono); font-size: 12px; text-align: center; padding: 0 20px;
}

/* ---- Affiliate sidebar ---- */
#chart-affiliate {
  flex: 1 1 0; max-width: 340px; min-width: 250px; flex-shrink: 0; border-left: 1px solid var(--border);
  padding: 12px; display: flex; flex-direction: column; gap: 8px;
  overflow-y: auto;
}
.aff-label { font-family: var(--mono); font-size: 10px; color: var(--tx2); text-transform: uppercase; letter-spacing: .05em; }
#aff-links { display: flex; flex-direction: column; gap: 6px; }
.aff-link {
  display: flex; flex-direction: row; align-items: center; gap: 8px;
  padding: 6px 8px; border-radius: 4px; text-decoration: none;
  border: 1px solid var(--border); background: var(--bg2);
  font-family: var(--mono); transition: border-color 0.1s, background 0.1s;
}
.aff-thumb {
  width: 36px; height: 36px; border-radius: 4px; flex-shrink: 0;
  background: var(--bg3); object-fit: contain;
}
.aff-info { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.aff-info .aff-title {
  font-size: 11px; color: var(--tx0); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.aff-info .aff-price { font-size: 11px; font-weight: 600; white-space: nowrap; }
.aff-info .aff-cond { font-size: 10px; font-weight: 400; color: var(--tx2); }
.aff-link.aff-new   .aff-price { color: var(--c-new); }
.aff-link.aff-used  .aff-price { color: var(--c-used); }
.aff-link.aff-parts .aff-price { color: var(--c-parts); }
.aff-link.aff-refurb .aff-price { color: var(--c-refurb); }
.aff-link:hover { background: var(--bg3); border-color: var(--border2); }
.aff-link.aff-disabled { opacity: 0.45; cursor: default; pointer-events: none; }

/* ---- Buy-it layout modes (user-selectable, saved locally) ----
   4x1 (default/stack): compact horizontal rows in the narrow sidebar.
   2x2 / 1x4: eBay-style tiles beside the graph — big image on top,
   name under it, price under that. */
#chart-body.aff-grid #chart-affiliate { max-width: 480px; min-width: 400px; }
#chart-body.aff-grid #aff-links { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
#chart-body.aff-row #chart-affiliate { max-width: 780px; min-width: 620px; }
#chart-body.aff-row #aff-links { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
#chart-panel.open.aff-tall { height: 400px; }

/* Tile styling shared by 2x2 and 1x4 */
#chart-body.aff-grid .aff-link, #chart-body.aff-row .aff-link {
  flex-direction: column; align-items: stretch; gap: 6px; padding: 8px;
}
#chart-body.aff-grid .aff-thumb, #chart-body.aff-row .aff-thumb {
  width: 100%; height: 96px; border-radius: 4px;
}
#chart-body.aff-grid .aff-info, #chart-body.aff-row .aff-info { gap: 3px; }
#chart-body.aff-grid .aff-info .aff-title, #chart-body.aff-row .aff-info .aff-title {
  white-space: normal; overflow: hidden; display: -webkit-box;
  -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 1.3;
}
.aff-link.aff-deal { border-color: #f97316; box-shadow: 0 0 0 1px #f97316 inset; }
.aff-deal-badge { font-size: 11px; }

/* ---- Scrollbar ---- */
#table-wrap::-webkit-scrollbar { width: 6px; }
#table-wrap::-webkit-scrollbar-track { background: var(--bg1); }
#table-wrap::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 3px; }

/* ---- Empty / loading ---- */
.state-msg {
  padding: 40px; text-align: center;
  font-family: var(--mono); font-size: 12px; color: var(--tx2);
}

/* ---- GTM noscript fallback (only rendered with JS disabled) ---- */
.gtm-noscript-frame { display: none; visibility: hidden; }

/* ================================================================
   Tablet (<=860px) — everything stays visible, just reflows onto more
   rows so nothing overflows. No hamburger yet; there's enough width
   here to wrap sensibly without hiding controls.
   ================================================================ */
.tb-hamburger { display: none; }

@media (max-width: 860px) {
  #topbar { flex-wrap: wrap; height: auto; }
  .tb-logo { height: 56px; }
  .tb-nav { height: 56px; }
  .tb-social { height: 56px; }
  .tb-spacer { flex-basis: 100%; height: 0; } /* forces stats onto their own row */
  .tb-stat { height: 32px; padding: 0 12px; }

  #filterbar { flex-wrap: wrap; height: auto; padding: 8px 12px; row-gap: 8px; }
  input.fb-search { flex: 1 1 100%; order: -1; width: auto; }
  .fb-sep { display: none; }
  .fb-spacer { display: none; }
  .fb-count { flex-basis: 100%; text-align: right; }

  /* Table keeps every column — shrinks padding/font first, still
     scrolls horizontally past min-width rather than squeezing illegible. */
  table#t { min-width: 520px; font-size: 11px; }
  table#t th, table#t td { padding: 5px 8px; }

  #chart-header { flex-wrap: wrap; height: auto; row-gap: 8px; }
  .series-toggles { flex-wrap: wrap; }

  #chart-panel.open, #chart-panel.open.aff-tall {
    height: auto; max-height: 85vh; max-height: 85dvh;
  }
  #chart-body { flex-direction: column; overflow-y: auto; }
  #chart-canvas-wrap { min-height: 220px; flex: none; }

  /* Ignore the desktop-saved 2x2/1x4 tile width preference — a narrow
     phone screen doesn't have room for either, so always fall back to
     a horizontal row of image-only tiles (no title/price text) below
     the chart instead of a vertical list. */
  #chart-affiliate,
  #chart-body.aff-grid #chart-affiliate,
  #chart-body.aff-row #chart-affiliate {
    max-width: 100%; min-width: 0;
    border-left: none; border-top: 1px solid var(--border);
  }
  #aff-links,
  #chart-body.aff-grid #aff-links,
  #chart-body.aff-row #aff-links {
    display: flex; flex-direction: row; flex-wrap: wrap; justify-content: center;
  }
  .aff-link,
  #chart-body.aff-grid .aff-link,
  #chart-body.aff-row .aff-link {
    flex-direction: column; padding: 0; width: 76px; border-width: 1px;
  }
  .aff-info { display: none; }
  .aff-thumb,
  #chart-body.aff-grid .aff-thumb,
  #chart-body.aff-row .aff-thumb {
    width: 76px; height: 76px; border-radius: 6px;
  }
}

/* ================================================================
   Phone (<=640px) — only genuinely narrow screens get the hamburger.
   Collapsed state shows just the logo/name + hamburger; tapping it
   reveals BOTH the topbar's nav/social/stats AND the entire filterbar
   (search included) in one dropdown — nothing else in the bar.
   ================================================================ */
@media (max-width: 640px) {
  #topbar { height: 56px; overflow: hidden; }
  .tb-nav, .tb-social, .tb-stat, .tb-spacer { display: none; }

  .tb-hamburger {
    display: flex; align-items: center; justify-content: center;
    width: 44px; height: 56px; margin-left: auto;
    background: none; border: none; cursor: pointer;
    color: var(--c-used); font-size: 20px;
  }
  .tb-hamburger[aria-expanded="true"] { color: var(--c-new); }

  #filterbar { display: none; }

  body.mobile-menu-open #topbar { flex-wrap: wrap; height: auto; overflow: visible; }
  body.mobile-menu-open .tb-nav,
  body.mobile-menu-open .tb-social,
  body.mobile-menu-open .tb-stat { display: flex; flex-basis: 100%; }
  body.mobile-menu-open .tb-nav { flex-direction: column; height: auto; }
  body.mobile-menu-open .tb-nav a { border-right: none; border-top: 1px solid var(--border); padding: 10px 20px; }
  body.mobile-menu-open .tb-social { padding: 10px 20px; border-left: none; border-top: 1px solid var(--border); }
  body.mobile-menu-open .tb-stat { height: auto; border-left: none; border-top: 1px solid var(--border); padding: 8px 20px; }

  body.mobile-menu-open #filterbar {
    display: flex; flex-wrap: wrap; height: auto; padding: 12px; row-gap: 10px;
  }
  body.mobile-menu-open input.fb-search { flex-basis: 100%; width: auto; }
  body.mobile-menu-open .fb-label { flex-basis: 100%; }
  body.mobile-menu-open .fb-sep { display: none; }
  body.mobile-menu-open .fb-spacer { display: none; }
  body.mobile-menu-open .fb-count { flex-basis: 100%; text-align: right; }
}
