/* ============================================================
   QuantLens — Design System
   ============================================================ */

/* ── Tokens: Dark ── */
:root {
  --bg: #09090b;
  --surface: #131316;
  --card: #1a1a1f;
  --border: #27272d;
  --border-subtle: rgba(255, 255, 255, 0.06);
  --text: #fafafa;
  --text-2: #a1a1aa;
  --text-3: #63636b;
  --accent: #818cf8;
  --accent-bg: rgba(129, 140, 248, 0.1);
  --up: #22c55e;
  --down: #ef4444;
  --up-bg: rgba(34, 197, 94, 0.08);
  --down-bg: rgba(239, 68, 68, 0.08);
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  --font: "Inter", system-ui, -apple-system, sans-serif;
  --nav-h: 72px;
  --topbar-h: 56px;
  --pad: 16px;
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --r-full: 9999px;
  --safe-bottom: calc(env(safe-area-inset-bottom, 0px));
}
html.light {
  --bg: #fafafa;
  --surface: #f4f4f5;
  --card: #ffffff;
  --border: #e4e4e7;
  --border-subtle: rgba(0, 0, 0, 0.05);
  --text: #18181b;
  --text-2: #71717a;
  --text-3: #a1a1aa;
  --accent: #4f46e5;
  --accent-bg: rgba(79, 70, 229, 0.08);
  --up: #16a34a;
  --down: #dc2626;
  --up-bg: rgba(22, 163, 74, 0.06);
  --down-bg: rgba(220, 38, 38, 0.06);
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.1);
}

/* ── Splash (visible before Alpine loads, overrides x-cloak) ── */
body[x-cloak] #splash {
  display: flex !important;
  align-items: center;
  justify-content: center;
  position: fixed;
  inset: 0;
  z-index: 10001;
  background: var(--bg);
}
#splash {
  display: none;
}
.splash-spin {
  width: 32px;
  height: 32px;
  color: var(--accent);
  animation: splash-rotate 0.9s linear infinite;
}
@keyframes splash-rotate {
  to {
    transform: rotate(360deg);
  }
}

/* ── Reset ── */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* ── Base ── */
html {
  font-family: var(--font);
  background: var(--bg);
  color-scheme: dark;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
html.light {
  color-scheme: light;
}
body {
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: calc(var(--nav-h) + var(--safe-bottom));
  transition:
    background-color 0.3s,
    color 0.3s;
}
::selection {
  background: var(--accent-bg);
  color: var(--text);
}

/* ── Scrollbar ── */
::-webkit-scrollbar {
  width: 0;
  height: 0;
}

/* ── Navigation Bar ── */
.nav-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding: 0 16px var(--safe-bottom);
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: space-around;
  z-index: 1000;
  border-top: 1px solid var(--border-subtle);
}
.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 10px 16px 8px;
  min-width: 56px;
  color: var(--text-3);
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  cursor: pointer;
  border-radius: var(--r-md);
  transition: color 0.2s;
  position: relative;
}
.nav-item .m-icon {
  font-family: "Material Symbols Rounded";
  font-weight: 300;
  font-style: normal;
  font-size: 24px;
  line-height: 1;
  font-variation-settings:
    "FILL" 0,
    "wght" 300;
  transition: font-variation-settings 0.2s;
}
.nav-item.active {
  color: var(--accent);
}
.nav-item.active .m-icon {
  font-variation-settings:
    "FILL" 0,
    "wght" 500;
}
.nav-item::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 2px;
  border-radius: 1px;
  background: var(--accent);
  opacity: 0;
  transition: opacity 0.2s;
}
.nav-item.active::after {
  opacity: 1;
}

/* ── Top Bar ── */
.top-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) 8px 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border-subtle);
}
.top-bar-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition:
    color 0.15s,
    background 0.15s;
}
.top-bar-btn:hover {
  color: var(--text);
  background: var(--border-subtle);
}
.top-bar-btn .m-icon {
  font-family: "Material Symbols Rounded";
  font-weight: 300;
  font-size: 22px;
  font-variation-settings:
    "FILL" 0,
    "wght" 300;
}
.top-bar-title {
  font-size: 16px;
  font-weight: 500;
  flex: 1;
  padding: 0 40px 0 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── View Management ── */
.hidden {
  display: none;
}
[x-cloak] {
  display: none !important;
}
#detailView {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1100;
  background: var(--bg);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: var(--safe-bottom);
}
#mainView {
  padding-top: 12px;
}
.view-content {
  padding: 12px var(--pad) 32px;
  max-width: 1040px;
  margin: 0 auto;
}

/* ── Cards ── */
.card {
  background: var(--card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-lg);
  margin-bottom: 12px;
  overflow: hidden;
}
.card-body {
  padding: 20px;
}

/* ── Dividers ── */
.divider {
  height: 1px;
  background: var(--border-subtle);
  margin: 16px 0;
}

/* ── Segmented Buttons ── */
.seg-row {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  overflow: hidden;
  margin-bottom: 12px;
}
.seg-btn {
  font-size: 13px;
  font-weight: 500;
  font-family: var(--font);
  padding: 6px 14px;
  border: none;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  min-width: 38px;
  transition: all 0.15s;
}
.seg-btn:hover {
  color: var(--text);
}
.seg-btn:not(:last-child) {
  border-right: 1px solid var(--border);
}
.seg-btn.active {
  background: var(--accent-bg);
  color: var(--accent);
}

/* ── Filter Chips ── */
.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}
.chip {
  font-size: 13px;
  font-weight: 500;
  font-family: var(--font);
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: var(--r-full);
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  transition: all 0.15s;
}
.chip:hover {
  border-color: var(--text-3);
  color: var(--text);
}
.chip.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.chip-tag {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 5px;
  border: 1px solid;
  border-radius: 3px;
  margin-left: 4px;
  opacity: 0.7;
  vertical-align: middle;
}

/* ── Toggle Switch ── */
.switch {
  appearance: none;
  width: 44px;
  height: 26px;
  border-radius: 13px;
  background: var(--border);
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
  outline: none;
  flex-shrink: 0;
}
.switch:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.switch:checked {
  background: var(--accent);
}
.switch::after {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  top: 3px;
  left: 3px;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}
.switch:checked::after {
  transform: translateX(18px);
}

/* ── Toolbar ── */
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}
.toolbar .seg-row {
  margin-bottom: 0;
}

/* ── List Sections / Headers ── */
.section-hdr {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: baseline;
  column-gap: 12px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-3);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 8px 16px 6px;
}
.sort-ctls {
  white-space: nowrap;
  display: flex;
  gap: 12px;
}
.sort-ctl {
  color: var(--text-2);
  cursor: pointer;
  font-weight: 500;
  transition: color 0.15s;
}
.sort-ctl:hover {
  color: var(--text);
}

/* ── List Rows ── */
.list-row {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  min-height: 48px;
  cursor: pointer;
  transition: background-color 0.15s;
}
.list-row:hover {
  background: var(--border-subtle);
}

/* ── Portfolio List ── */
.folio {
  background: var(--card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-lg);
  margin-bottom: 12px;
  overflow: hidden;
}
.folio-row {
  display: flex;
  align-items: flex-start;
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.12s;
}
.folio-row:hover {
  background: var(--border-subtle);
}
.folio-divider {
  margin: 0 16px;
  border: none;
  border-top: 1px solid var(--border-subtle);
  height: 0;
}

/* ── Portfolio Row internals ── */
.pl-body {
  flex: 1;
  min-width: 0;
}
.pl-headline {
  font-size: 15px;
  font-weight: 500;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
}
.pl-supporting {
  font-size: 13px;
  color: var(--text-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 1px;
}
.pl-trailing {
  text-align: right;
  flex-shrink: 0;
  margin-left: 16px;
}
.pl-price {
  font-size: 15px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  line-height: 1.4;
}
.pl-change {
  font-size: 13px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  margin-top: 1px;
}
.up {
  color: var(--up);
}
.down {
  color: var(--down);
}
.pl-change.up {
  color: var(--up);
}
.pl-change.down {
  color: var(--down);
}

/* ── Portfolio Strategy Sub-rows ── */
.folio-strat {
  display: flex;
  align-items: center;
  padding: 8px 16px 8px 16px;
  gap: 8px;
  cursor: pointer;
  transition: background-color 0.12s;
}
.folio-strat:hover {
  background: var(--border-subtle);
}
.folio-bh {
  display: flex;
  align-items: center;
  padding: 8px 16px 8px 16px;
  gap: 8px;
}
.bh-dot {
  background: var(--text-3);
  animation: none;
}
.fs-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.fs-dot.buy {
  background: var(--up);
}
.fs-dot.sell {
  background: var(--down);
}
.fs-dot.none {
  background: transparent;
}
.fs-name {
  flex: 1;
  font-size: 12px;
  color: var(--text-2);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.fs-tag {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-3);
  text-transform: uppercase;
  flex-shrink: 0;
}
.fs-ret {
  font-size: 12px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  min-width: 46px;
  text-align: right;
  flex-shrink: 0;
}
.fs-ret.up {
  color: var(--up);
}
.fs-ret.down {
  color: var(--down);
}
.fs-dd {
  font-size: 12px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  min-width: 40px;
  text-align: right;
  flex-shrink: 0;
  color: var(--text-3);
}
.fs-dd.up {
  color: var(--up);
}
.fs-dd.down {
  color: var(--down);
}

/* ── Badges ── */
.badge {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 5px;
  border: 1px solid;
  border-radius: 3px;
  text-transform: uppercase;
  flex-shrink: 0;
  letter-spacing: 0.02em;
}
.badge.US {
  color: #60a5fa;
  border-color: #60a5fa;
}
.badge.CN {
  color: #f59e0b;
  border-color: #f59e0b;
}
.badge.HK {
  color: var(--text-3);
  border-color: var(--text-3);
}

/* ── Rank Delta ── */
.rank-delta {
  font-size: 11px;
  font-weight: 600;
  margin-left: 4px;
}
.rank-delta.down {
  color: var(--down);
}
.rank-delta.up {
  color: var(--up);
}

/* ── Signal Badges ── */
.sig-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.sig-badge::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.sig-badge.buy {
  color: var(--up);
}
.sig-badge.buy::before {
  background: var(--up);
}
.sig-badge.sell {
  color: var(--down);
}
.sig-badge.sell::before {
  background: var(--down);
}

/* ── Signal Strip ── */
.sig-strip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  border-radius: var(--r-sm);
  font-size: 12px;
  font-weight: 500;
  margin: 8px 0 12px;
}
.sig-strip .ss-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.sig-strip .ss-dot.buy {
  background: var(--up);
}
.sig-strip .ss-dot.sell {
  background: var(--down);
}
.sig-strip .ss-dot.hold {
  background: var(--text-3);
}
.sig-strip.buy {
  background: var(--up-bg);
  color: var(--up);
}
.sig-strip.sell {
  background: var(--down-bg);
  color: var(--down);
}
.sig-strip.hold {
  background: var(--surface);
  color: var(--text-2);
}

/* ── Metrics Grid ── */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  margin: 8px 0 2px;
}
.metric-cell {
  text-align: center;
  padding: 8px 4px;
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: background-color 0.15s;
}
.metric-cell:hover {
  background: var(--border-subtle);
}
.metric-cell .m-val {
  font-size: 12px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}
.metric-cell .m-val.up {
  color: var(--up);
}
.metric-cell .m-val.down {
  color: var(--down);
}
.metric-cell .m-sub {
  font-size: 9px;
  font-weight: 400;
  color: var(--text-3);
  margin-top: 1px;
}
.metric-cell .m-lbl {
  font-size: 10px;
  font-weight: 500;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-top: 4px;
}

/* ── Chart ── */
.chart {
  width: 100%;
  height: 300px;
  margin-top: 2px;
}

/* ── History ── */
.detail-history-card {
  max-height: 340px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.detail-history-card::-webkit-scrollbar {
  width: 4px;
}
.detail-history-card::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}
.detail-history-card::-webkit-scrollbar-track {
  background: transparent;
}

/* ── Signal Items (Signals tab) ── */
.sig-item {
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.12s;
}
.sig-item:hover {
  background: var(--border-subtle);
}
.sig-item-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 2px;
}
.sig-card-name {
  font-weight: 600;
  font-size: 15px;
}
.sig-card-strat {
  font-size: 13px;
  color: var(--text-2);
}
.sig-card-body {
  font-size: 13px;
  color: var(--text-3);
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sig-card-age {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-3);
  margin-top: 4px;
}

/* ── Detail Hero ── */
/* ── Detail Hero (two-row) ── */
.dh-hero {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.dh-hero-row {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
}
.dh-fund-row {
  gap: 10px;
}
.dh-price-lg {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}
.dh-change-lg {
  font-size: 14px;
  font-weight: 500;
}
.dh-change-lg.up {
  color: var(--up);
}
.dh-change-lg.down {
  color: var(--down);
}
.dh-mkt-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-3);
}
.dh-mkt-badge.US {
  color: #60a5fa;
  border-color: #60a5fa;
}
.dh-mkt-badge.CN {
  color: #f59e0b;
  border-color: #f59e0b;
}
.dh-mkt-badge.HK {
  color: var(--text-3);
  border-color: var(--text-3);
}
.dhf-inline {
  display: inline-flex;
  align-items: baseline;
  gap: 3px;
  font-size: 12px;
}
.dhf-inline-label {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.dhf-inline-val {
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}
.dhf-inline-note {
  font-size: 10px;
  font-weight: 600;
}
.dhf-inline-note.cheap {
  color: var(--up);
}
.dhf-inline-note.fair {
  color: var(--text-2);
}
.dhf-inline-note.rich {
  color: var(--down);
}

/* ── Detail Toolbar (chips + signal dot) ── */
.dh-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
}
.dh-toolbar .chip-row {
  flex: 1;
}
.sig-dot-inline {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.sig-dot-inline.buy {
  background: var(--up);
}
.sig-dot-inline.sell {
  background: var(--down);
}
.sig-dot-inline.hold {
  background: var(--text-3);
}

/* ── Strategy Description ── */
.strat-desc {
  font-size: 12px;
  color: var(--text-3);
  line-height: 1.45;
  margin: 8px 0 12px;
}

/* ── Empty / Error ── */
.empty-msg {
  text-align: center;
  padding: 48px 16px;
  color: var(--text-3);
  font-size: 14px;
}
.error-box {
  text-align: center;
  padding: 80px 0;
  color: var(--text-2);
  font-size: 14px;
}

/* ── Buttons ── */
.btn {
  font-family: var(--font);
  font-size: 13px;
  font-weight: 500;
  padding: 10px 20px;
  border: none;
  border-radius: var(--r-md);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  margin-top: 12px;
  transition: opacity 0.15s;
}
.btn:hover {
  opacity: 0.88;
}
.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── Responsive ── */
@media (max-width: 500px) {
  .metric-grid {
    grid-template-columns: repeat(4, 1fr);
  }
  .chart {
    height: 240px;
  }
  .dh-hero-row {
    gap: 8px;
  }
  .dh-price-lg {
    font-size: 20px;
  }
  .dh-change-lg {
    font-size: 13px;
  }
  .dhf-inline {
    font-size: 11px;
  }
  .sig-strip {
    margin: 6px 0 10px;
  }
  .metric-cell {
    padding: 6px 2px;
  }
}
@media (max-width: 600px) {
  .dh-price-lg {
    font-size: 22px;
  }
}

/* ── Reduced Motion ── */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
