:root {
  --bg: #f2f4f5;
  --surface: #ffffff;
  --surface-2: #eef1f2;
  --text: #11181c;
  --muted: #5f6b73;
  --line: #dde3e6;
  --accent: #0f766e;
  --accent-soft: #d5f0ec;
  --on-accent: #ffffff;
  --up: #12855b;
  --down: #c4352b;
  --up-soft: rgba(18, 133, 91, 0.14);
  --down-soft: rgba(196, 53, 43, 0.14);
  --warn: #9a6200;
  --warn-soft: #fff1d6;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.05);
  --radius: 14px;
  --tabbar-h: 58px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0f11; --surface: #151b1e; --surface-2: #1d2529; --text: #e8eef0; --muted: #93a1a8;
    --line: #273136; --accent: #2bb3a3; --accent-soft: #123a36; --on-accent: #04201d;
    --up: #34c37f; --down: #ff6b5e; --up-soft: rgba(52, 195, 127, 0.16); --down-soft: rgba(255, 107, 94, 0.16);
    --warn: #f0b44c; --warn-soft: #3a2c10; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0f11; --surface: #151b1e; --surface-2: #1d2529; --text: #e8eef0; --muted: #93a1a8;
  --line: #273136; --accent: #2bb3a3; --accent-soft: #123a36; --on-accent: #04201d;
  --up: #34c37f; --down: #ff6b5e; --up-soft: rgba(52, 195, 127, 0.16); --down-soft: rgba(255, 107, 94, 0.16);
  --warn: #f0b44c; --warn-soft: #3a2c10; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text); font: 16px/1.4 var(--font);
  min-height: 100dvh; overflow-x: hidden; -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.up { color: var(--up); }
.down { color: var(--down); }
.hidden { display: none !important; }

/* ---------- chrome ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(14px); -webkit-backdrop-filter: saturate(1.6) blur(14px);
}
.topbar h1 { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--surface); color: var(--text);
  display: grid; place-items: center; box-shadow: var(--shadow); cursor: pointer;
}
main { padding: 4px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px); max-width: 720px; margin: 0 auto; outline: none; }
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; display: flex; justify-content: space-around;
  padding: 6px 4px calc(env(safe-area-inset-bottom) + 4px); background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; text-decoration: none;
  color: var(--muted); font-size: 11px; padding: 4px 0; min-width: 0;
}
.tabbar svg { width: 24px; height: 24px; }
.tabbar a.active { color: var(--accent); }

/* ---------- building blocks ---------- */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin: 12px 0; }
.card h2 { font-size: 17px; margin-bottom: 8px; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 20px 4px 6px; font-weight: 600; }
.btn {
  border: 0; border-radius: 12px; padding: 11px 16px; background: var(--accent); color: var(--on-accent);
  font-weight: 600; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.ghost { background: transparent; color: var(--accent); padding: 8px 10px; }
.btn.danger { background: var(--down-soft); color: var(--down); }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.btn-row .btn { flex: 1; }

.banner { border-radius: 12px; padding: 10px 12px; font-size: 14px; margin: 8px 0; display: flex; gap: 8px; align-items: flex-start; }
.banner.ok { background: var(--accent-soft); color: var(--text); }
.banner.warn, .banner.stale, .banner.error { background: var(--warn-soft); color: var(--text); }
.banner.empty { background: var(--down-soft); }
.banner b { display: block; }
.banner > div { min-width: 0; overflow-wrap: anywhere; }
.banner .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-top: 6px; flex: none; }
.banner.warn .dot, .banner.stale .dot, .banner.error .dot { background: var(--warn); }
.banner.empty .dot { background: var(--down); }

.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; white-space: nowrap; }
.badge.halal { background: var(--accent-soft); color: var(--accent); }
.badge.nothalal { background: var(--surface-2); color: var(--muted); }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 7px 13px;
  font-size: 14px; cursor: pointer; white-space: nowrap;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

.segmented { display: flex; background: var(--surface-2); border-radius: 10px; padding: 2px; gap: 2px; }
.segmented button { flex: 1; border: 0; background: transparent; padding: 6px 4px; border-radius: 8px; font-size: 14px; cursor: pointer; }
.segmented button.on { background: var(--surface); box-shadow: var(--shadow); font-weight: 600; }

/* ---------- lists ---------- */
.list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin: 8px 0; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line);
  cursor: pointer; text-decoration: none; color: inherit; min-width: 0;
}
.row:last-child { border-bottom: 0; }
.row:active { background: var(--surface-2); }
.row .main { flex: 1; min-width: 0; }
.row .sym { font-weight: 700; font-size: 16px; display: flex; gap: 6px; align-items: center; }
.row .name { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .side { text-align: right; flex: none; }
.row .price { font-weight: 600; font-variant-numeric: tabular-nums; border-radius: 6px; padding: 0 4px; transition: background 0.6s; }
.row .chg { font-size: 13px; font-variant-numeric: tabular-nums; }
.pill { display: inline-block; min-width: 72px; text-align: center; padding: 3px 6px; border-radius: 7px; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.pill.up { background: var(--up-soft); }
.pill.down { background: var(--down-soft); }
.flash-up { background: var(--up-soft); }
.flash-down { background: var(--down-soft); }
.logo { width: 36px; height: 36px; border-radius: 9px; background: var(--surface-2); object-fit: contain; flex: none; display: grid; place-items: center; font-weight: 700; font-size: 13px; color: var(--muted); overflow: hidden; }
.empty-state { text-align: center; color: var(--muted); padding: 28px 16px; }
.empty-state b { color: var(--text); display: block; font-size: 17px; margin-bottom: 4px; }

/* ---------- screener form ---------- */
.filters { display: grid; grid-template-columns: 1fr; gap: 10px; }
.range { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr) minmax(0, 1fr); gap: 8px; align-items: center; }
.range label { font-size: 14px; }
.range label small { color: var(--muted); }
.field input, .range input, .field select, .search-input {
  width: 100%; min-width: 0; border: 1px solid var(--line); background: var(--surface-2); border-radius: 10px;
  padding: 9px 10px; font-size: 16px; outline: none;
}
.field input:focus, .range input:focus, .search-input:focus { border-color: var(--accent); }
.field { display: grid; gap: 4px; margin: 10px 0; }
.field > span { font-size: 14px; color: var(--muted); }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 12px 2px 4px; flex-wrap: wrap; }
.toolbar select { border: 0; background: transparent; color: var(--accent); font-weight: 600; font-size: 15px; }
details.more summary { cursor: pointer; color: var(--accent); font-weight: 600; padding: 6px 0; list-style: none; }
details.more summary::-webkit-details-marker { display: none; }
.progress { height: 3px; background: var(--surface-2); border-radius: 2px; overflow: hidden; margin: 6px 0; }
.progress > div { height: 100%; background: var(--accent); width: 0; transition: width 0.3s; }

/* ---------- sheet (stock detail, search) ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 40; background: var(--bg); overflow-y: auto; -webkit-overflow-scrolling: touch;
  animation: rise 0.22s ease-out; padding-bottom: calc(env(safe-area-inset-bottom) + 24px);
}
@keyframes rise { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-head {
  position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px; background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
}
.sheet-head .grow { flex: 1; min-width: 0; }
.sheet-body { padding: 0 16px; max-width: 720px; margin: 0 auto; }
.quote-big { font-size: 34px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 16px; }
.stat { display: flex; justify-content: space-between; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14px; min-width: 0; }
.stat span:first-child { color: var(--muted); }
.stat span:last-child { font-weight: 600; text-align: right; overflow-wrap: anywhere; }
.chart { height: 220px; margin: 8px -4px 0; }
.desc { font-size: 15px; line-height: 1.5; }
.desc.clamp { display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
.peers { display: flex; flex-wrap: wrap; gap: 6px; }

/* ---------- news ---------- */
.news-item { display: flex; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); text-decoration: none; color: inherit; }
.news-item:last-child { border-bottom: 0; }
.news-item img { width: 72px; height: 72px; object-fit: cover; border-radius: 10px; flex: none; background: var(--surface-2); }
.news-item h3 { font-size: 15px; line-height: 1.3; font-weight: 600; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.news-item .meta { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ---------- alerts / settings ---------- */
.alert-card .top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.alert-card .desc { font-size: 13px; color: var(--muted); margin-top: 4px; }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; position: absolute; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 999px; transition: 0.2s; }
.switch span::after { content: ""; position: absolute; width: 26px; height: 26px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: 0.2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }
.steps { padding-left: 20px; margin: 6px 0; }
.steps li { margin: 4px 0; }
.kbd { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; overflow-wrap: anywhere; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 60;
  opacity: 0; pointer-events: none; transition: 0.25s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--down); color: #fff; }

.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--line), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 6px; height: 14px; margin: 6px 0; }
@keyframes shimmer { to { background-position: -200% 0; } }

@media (min-width: 600px) {
  .filters { grid-template-columns: 1fr 1fr; column-gap: 20px; }
  .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
