/* ============ Theme tokens ============ */
:root {
  /* default dark */
  --bg: #0a0c14;
  --bg-elev: #11141f;
  --bg-elev-2: #161a28;
  --line: rgba(255, 255, 255, 0.06);
  --line-2: rgba(255, 255, 255, 0.1);
  --text: #f0f2f8;
  --text-1: #c7ccdc;
  --text-2: #8a90a8;
  --text-3: #5a6076;
  --up: #ff4d6d;
  --down: #16d39a;
  --accent: #6e8aff;
  --accent-2: #9a7bff;

  --topbar-bg: rgba(10, 12, 20, 0.78);
  --row-hover: rgba(255, 255, 255, 0.02);
  --row-active: rgba(255, 255, 255, 0.04);
  --detail-bg: rgba(0, 0, 0, 0.22);
  --dashed: rgba(255, 255, 255, 0.05);
  --toast-bg: rgba(20, 23, 38, 0.96);
  --toast-fg: #f0f2f8;
  --shimmer: linear-gradient(110deg, rgba(255,255,255,0.04) 30%, rgba(255,255,255,0.10) 50%, rgba(255,255,255,0.04) 70%);
  --bg-glow: radial-gradient(900px 460px at 50% -200px, rgba(110, 138, 255, 0.08), transparent 60%);

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --mono: "SF Mono", ui-monospace, "JetBrains Mono", "Menlo", monospace;
}

html[data-theme="light"] {
  --bg: #f5f6fa;
  --bg-elev: #ffffff;
  --bg-elev-2: #f0f2f8;
  --line: rgba(15, 19, 32, 0.08);
  --line-2: rgba(15, 19, 32, 0.14);
  --text: #0f1320;
  --text-1: #2a3045;
  --text-2: #5a6076;
  --text-3: #8b90a8;
  --up: #e0344e;
  --down: #0ba37e;
  --accent: #4f6dff;
  --accent-2: #8b5cff;

  --topbar-bg: rgba(255, 255, 255, 0.82);
  --row-hover: rgba(15, 19, 32, 0.02);
  --row-active: rgba(15, 19, 32, 0.04);
  --detail-bg: rgba(15, 19, 32, 0.025);
  --dashed: rgba(15, 19, 32, 0.06);
  --toast-bg: rgba(20, 23, 38, 0.94);
  --toast-fg: #ffffff;
  --shimmer: linear-gradient(110deg, rgba(15,19,32,0.04) 30%, rgba(15,19,32,0.09) 50%, rgba(15,19,32,0.04) 70%);
  --bg-glow: radial-gradient(900px 460px at 50% -200px, rgba(79, 109, 255, 0.07), transparent 60%);
}

/* ============ Base ============ */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg-glow), var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "PingFang SC",
    "Helvetica Neue", "Segoe UI", Roboto, "Microsoft YaHei", sans-serif;
  font-size: calc(14px * var(--fs-scale, 1));
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  min-height: 100vh;
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* ============ Topbar ============ */
.topbar {
  position: sticky; top: 0; z-index: 30;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  display: flex; align-items: center; gap: 10px;
  background: var(--topbar-bg);
  backdrop-filter: saturate(180%) blur(22px);
  -webkit-backdrop-filter: saturate(180%) blur(22px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 7px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  box-shadow: 0 6px 16px -6px var(--accent);
}
.brand-title {
  font-size: calc(13px * var(--fs-scale, 1)); font-weight: 600; letter-spacing: 0.3px;
  font-feature-settings: "ss01" on;
}

.header-stats {
  flex: 1;
  display: flex; align-items: center; justify-content: center;
  gap: 14px;
  min-width: 0;
}
.hstat { display: inline-flex; align-items: baseline; gap: 3px; font-size: calc(11px * var(--fs-scale, 1)); }
.hstat b {
  font-family: var(--mono); font-size: calc(14px * var(--fs-scale, 1)); font-weight: 700;
  color: var(--text); letter-spacing: -0.2px;
}
.hstat b.up { color: var(--up); }
.hstat b.down { color: var(--down); }
.hstat i { font-style: normal; color: var(--text-3); font-size: calc(10.5px * var(--fs-scale, 1)); }
@media (max-width: 360px) {
  .brand-title { display: none; }
  .header-stats { gap: 10px; }
}

.icon-btn {
  width: 32px; height: 32px; border-radius: 9px;
  display: grid; place-items: center;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  color: var(--text-1);
  cursor: pointer; transition: all 0.18s ease;
}
.icon-btn:hover { color: var(--text); background: var(--bg-elev-2); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.font-btn-label { font-size: calc(13px * var(--fs-scale, 1)); font-weight: 700; line-height: 1; letter-spacing: -0.5px; }

.theme-btn .icon-sun,
.theme-btn .icon-moon,
.theme-btn .icon-auto { display: none; }
html[data-theme-pref="dark"]  .theme-btn .icon-moon { display: block; }
html[data-theme-pref="light"] .theme-btn .icon-sun  { display: block; }
html[data-theme-pref="auto"]  .theme-btn .icon-auto { display: block; }

/* ============ Container ============ */
.container {
  max-width: 720px; margin: 0 auto;
  padding: 16px 14px calc(36px + var(--safe-bottom));
}

/* ============ Notice ============ */
.notice[hidden] { display: none; }
.notice {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 12px;
  margin-bottom: 12px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text-2);
  font-size: calc(11.5px * var(--fs-scale, 1));
  line-height: 1.4;
}
.notice svg { flex-shrink: 0; color: var(--accent); opacity: 0.85; }

/* ============ Indexes ============ */
.indexes {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
  margin-bottom: 14px;
}
.indexes.indexes--3 { grid-template-columns: repeat(3, 1fr); }
:root { --fs-scale: 1; }
.idx {
  padding: 10px 10px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
.idx-name { font-size: calc(11px * var(--fs-scale, 1)); color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.idx-val { font-size: calc(14px * var(--fs-scale, 1)); font-weight: 600; font-family: var(--mono); margin-top: 4px; letter-spacing: -0.2px; }
.idx-val.up { color: var(--up); }
.idx-val.down { color: var(--down); }
.idx-val.flat { color: var(--text-2); }

/* ============ Section head ============ */
.section-head {
  display: flex; align-items: baseline; justify-content: space-between;
  padding: 4px 4px 8px;
}
.section-head h2 {
  font-size: calc(12px * var(--fs-scale, 1)); font-weight: 600; color: var(--text-2);
  margin: 0; letter-spacing: 0.6px; text-transform: uppercase;
}
.section-count { font-size: calc(11px * var(--fs-scale, 1)); color: var(--text-3); font-family: var(--mono); }

/* ============ Fund list ============ */
.fund-list {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
.row {
  cursor: pointer;
  border-bottom: 1px solid var(--line);
  transition: background 0.15s ease;
}
.row:last-child { border-bottom: 0; }
.row:hover { background: var(--row-hover); }
.row.open { background: var(--row-hover); }
.row:active { background: var(--row-active); }

.row-main {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center; gap: 10px;
  padding: 9px 12px;
}
.row-rank {
  font-family: var(--mono); font-size: calc(11px * var(--fs-scale, 1));
  color: var(--text-3);
  width: 20px; text-align: center;
}
.row-info {
  min-width: 0;
  display: flex; align-items: baseline; gap: 6px;
}
.row-name {
  font-size: calc(13.5px * var(--fs-scale, 1)); font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  min-width: 0; flex-shrink: 1;
}
.row-meta {
  font-size: calc(11px * var(--fs-scale, 1)); color: var(--text-3);
  font-family: var(--mono);
  white-space: nowrap; flex-shrink: 0;
}
.row-impact {
  font-family: var(--mono); font-weight: 600;
  font-size: calc(14px * var(--fs-scale, 1)); letter-spacing: -0.2px;
  text-align: right; min-width: 64px;
}
.row-impact.up { color: var(--up); }
.row-impact.down { color: var(--down); }
.row-impact.flat { color: var(--text-2); }
.row-caret {
  color: var(--text-3); display: grid; place-items: center;
  width: 14px;
  transition: transform 0.25s ease;
}
.row.open .row-caret { transform: rotate(90deg); color: var(--text-1); }

/* expand detail */
.row-detail {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.28s ease;
  background: var(--detail-bg);
  border-top: 1px solid transparent;
}
.row.open .row-detail {
  grid-template-rows: 1fr;
  border-top-color: var(--line);
}
.row-detail-inner { overflow: hidden; min-height: 0; }
.detail-pad { padding: 12px 12px 14px; }

.detail-head {
  font-size: calc(10.5px * var(--fs-scale, 1)); color: var(--text-2);
  text-transform: uppercase; letter-spacing: 0.6px;
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.detail-head .badge {
  font-family: var(--mono);
  font-size: calc(10px * var(--fs-scale, 1));
  color: var(--text-3);
  padding: 2px 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
}

/* top movers strip */
.movers {
  display: flex; gap: 6px;
  overflow-x: auto;
  margin: 0 -12px 14px;
  padding: 0 12px 6px;
  scrollbar-width: none;
}
.movers::-webkit-scrollbar { display: none; }
.mover {
  flex-shrink: 0;
  padding: 6px 9px;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: calc(11px * var(--fs-scale, 1));
  display: flex; flex-direction: column; gap: 2px;
  min-width: 88px;
}
.mover-name {
  color: var(--text-1);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 96px;
}
.mover-change { font-family: var(--mono); font-weight: 600; font-size: calc(12px * var(--fs-scale, 1)); }
.mover-change.up { color: var(--up); }
.mover-change.down { color: var(--down); }
.mover-change.flat { color: var(--text-2); }

.stock-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
}
@media (min-width: 520px) {
  .stock-grid { grid-template-columns: 1fr 1fr; column-gap: 16px; }
}
.stock-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 12px;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px dashed var(--dashed);
  font-size: calc(12px * var(--fs-scale, 1));
}
.stock-row:last-child { border-bottom: 0; }
.stock-name {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  color: var(--text-1);
}
.stock-weight { font-family: var(--mono); color: var(--text-3); font-size: calc(11px * var(--fs-scale, 1)); min-width: 50px; text-align: right; }
.stock-change { font-family: var(--mono); font-weight: 600; min-width: 56px; text-align: right; }
.stock-change.up { color: var(--up); }
.stock-change.down { color: var(--down); }
.stock-change.flat { color: var(--text-2); }

/* footer */
.footer {
  margin-top: 24px;
  text-align: center;
  color: var(--text-3);
  font-size: calc(11px * var(--fs-scale, 1));
  line-height: 1.7;
}

/* toast */
.toast {
  position: fixed; left: 50%; bottom: calc(28px + var(--safe-bottom));
  transform: translate(-50%, 30px);
  background: var(--toast-bg);
  border: 1px solid var(--line-2);
  color: var(--toast-fg);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: calc(12px * var(--fs-scale, 1));
  opacity: 0; pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
  z-index: 50;
  backdrop-filter: blur(8px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* skeleton rows */
.sk-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px; align-items: center;
  padding: 9px 12px;
  border-bottom: 1px solid var(--line);
}
.sk-row:last-child { border-bottom: 0; }
.sk-pill {
  background: var(--shimmer);
  background-size: 200% 100%;
  animation: shimmer 1.4s linear infinite;
  border-radius: 6px;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.empty {
  padding: 36px 12px; text-align: center;
  color: var(--text-3); font-size: calc(13px * var(--fs-scale, 1));
}
.empty svg { display: block; margin: 0 auto 10px; opacity: 0.5; }

/* ============ Font sheet ============ */
.font-sheet[hidden] { display: none; }
.font-sheet {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: flex-end; justify-content: center;
}
.font-sheet-backdrop {
  position: absolute; inset: 0;
  background: rgba(0, 0, 0, 0.42);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  animation: fade-in 0.18s ease;
}
.font-sheet-card {
  position: relative;
  width: 100%; max-width: 480px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  padding: 14px 16px calc(18px + var(--safe-bottom));
  box-shadow: 0 -8px 28px -8px rgba(0, 0, 0, 0.35);
  animation: sheet-up 0.22s cubic-bezier(0.2, 0.7, 0.2, 1);
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }

.font-sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}
.font-sheet-title { font-size: calc(14px * var(--fs-scale, 1)); font-weight: 600; color: var(--text); }
.font-sheet-close {
  width: 28px; height: 28px; border-radius: 8px;
  background: var(--bg-elev-2); border: 0; color: var(--text-2);
  font-size: 18px; line-height: 1; cursor: pointer;
}
.font-sheet-close:hover { color: var(--text); }

.font-sheet-presets {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  margin-bottom: 12px;
}
.font-preset {
  padding: 10px 6px;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text-1);
  font-size: calc(12px * var(--fs-scale, 1));
  font-weight: 500;
  cursor: pointer;
  transition: all 0.16s ease;
}
.font-preset:hover { color: var(--text); border-color: var(--line-2); }
.font-preset.active {
  background: color-mix(in srgb, var(--accent) 16%, var(--bg-elev-2));
  border-color: color-mix(in srgb, var(--accent) 42%, transparent);
  color: var(--accent);
}

.font-sheet-stepper {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.font-step {
  width: 36px; height: 32px; border-radius: 8px;
  background: transparent; border: 0; color: var(--text-1);
  font-size: 20px; line-height: 1; cursor: pointer;
}
.font-step:hover { background: var(--bg-elev); color: var(--text); }
.font-step:disabled { color: var(--text-3); cursor: not-allowed; }
.font-step:disabled:hover { background: transparent; }
.font-step-value {
  font-family: var(--mono); font-size: calc(13px * var(--fs-scale, 1));
  color: var(--text); font-weight: 600;
}
