:root {
  --green: #00C896;
  --green-dark: #009E78;
  --green-glow: rgba(0, 200, 150, 0.15);
  --red: #FF4B4B;
  --gold: #F5A623;
  --bg: #0A0F1E;
  --bg2: #0F1628;
  --bg3: #141C35;
  --surface: #1A2340;
  --surface2: #1E2A4A;
  --border: rgba(255, 255, 255, 0.08);
  --text: #F0F4FF;
  --text-muted: #8A9BC0;
  --text-dim: #5A6A8A;
  --font: 'Cairo', 'Tajawal', sans-serif;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  position: relative;
  overflow-x: hidden;
  font-variant-numeric: tabular-nums;
}

::selection { background: var(--green-glow); color: var(--text); }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--bg2); }
::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: var(--green-dark); }

button, input { font-family: var(--font); }

a, button { -webkit-tap-highlight-color: transparent; }

:focus-visible {
  outline: 2px solid var(--green);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ===== Background atmosphere ===== */
.bg-grid {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-image:
    linear-gradient(var(--border) 1px, transparent 1px),
    linear-gradient(90deg, var(--border) 1px, transparent 1px);
  background-size: 42px 42px;
  -webkit-mask-image: radial-gradient(ellipse 90% 60% at 50% 0%, #000 20%, transparent 75%);
  mask-image: radial-gradient(ellipse 90% 60% at 50% 0%, #000 20%, transparent 75%);
  opacity: 0.5;
}

.bg-glow {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(600px 300px at 15% -5%, var(--green-glow), transparent 60%),
    radial-gradient(500px 260px at 100% 10%, rgba(245, 166, 35, 0.08), transparent 60%);
}

/* ===== Ticker tape ===== */
.ticker-wrap {
  position: relative;
  z-index: 5;
  width: 100%;
  overflow: hidden;
  background: linear-gradient(180deg, #0D1326, var(--bg2));
  border-bottom: 1px solid var(--border);
  padding: 9px 0;
}

.ticker-track {
  display: inline-flex;
  white-space: nowrap;
  animation: ticker-scroll 55s linear infinite;
  will-change: transform;
}

.ticker-wrap:hover .ticker-track { animation-play-state: paused; }

@keyframes ticker-scroll {
  from { transform: translateX(0); }
  to { transform: translateX(50%); }
}

.ticker-loading {
  color: var(--text-dim);
  font-size: 0.82rem;
  padding: 0 20px;
}

.ticker-item {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 0 20px;
  font-size: 0.82rem;
  font-weight: 600;
  border-left: 1px solid var(--border);
  color: var(--text-muted);
}

.ticker-item .t-name { color: var(--text); font-weight: 700; }
.ticker-item .t-pct { font-weight: 800; letter-spacing: 0.2px; }
.ticker-item .t-pct.up { color: var(--green); }
.ticker-item .t-pct.down { color: var(--red); }
.ticker-item .t-pct.flat { color: var(--gold); }

/* ===== Header ===== */
.site-header {
  position: relative;
  z-index: 5;
  border-bottom: 1px solid var(--border);
  background: rgba(10, 15, 30, 0.7);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
}

.header-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 0 1px transparent, 0 6px 18px -6px rgba(0,200,150,0.35);
}

.brand-svg { width: 26px; height: 26px; }

.brand-text h1 {
  margin: 0;
  font-size: 1.28rem;
  font-weight: 800;
  letter-spacing: 0.2px;
  line-height: 1.1;
}

.brand-text h1 span { color: var(--green); }

.brand-sub {
  margin: 2px 0 0;
  font-size: 0.72rem;
  color: var(--text-dim);
  letter-spacing: 0.3px;
}

.header-status {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 7px 13px;
  border-radius: 100px;
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 600;
}

.status-pill .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 0 8px var(--gold);
}

.status-pill.live .dot {
  background: var(--green);
  box-shadow: 0 0 8px var(--green);
  animation: pulse-dot 1.6s ease-in-out infinite;
}

.status-pill.error .dot { background: var(--red); box-shadow: 0 0 8px var(--red); animation: none; }

@keyframes pulse-dot {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.5); opacity: 0.55; }
}

.clock {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 7px 12px;
  border-radius: 100px;
  min-width: 84px;
  text-align: center;
  letter-spacing: 0.5px;
}

/* ===== Hero ===== */
.hero {
  position: relative;
  z-index: 2;
  padding: 46px 24px 26px;
}

.hero-inner {
  max-width: 1280px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 32px;
  align-items: flex-end;
  justify-content: space-between;
}

.eyebrow {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 1.5px;
  color: var(--green);
  background: var(--green-glow);
  padding: 5px 12px;
  border-radius: 100px;
  margin-bottom: 14px;
  text-transform: uppercase;
}

.hero-copy h2 {
  margin: 0 0 14px;
  font-size: clamp(1.6rem, 3.4vw, 2.5rem);
  font-weight: 900;
  line-height: 1.25;
  max-width: 620px;
}

.hero-copy p {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.98rem;
  max-width: 480px;
  line-height: 1.7;
}

.hero-stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: 12px;
  flex: 1;
  min-width: 320px;
  max-width: 560px;
}

.stat-card {
  background: linear-gradient(160deg, var(--surface), var(--bg3));
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: transform 0.25s ease, border-color 0.25s ease;
}

.stat-card:hover { transform: translateY(-3px); border-color: rgba(0,200,150,0.3); }

.stat-label {
  font-size: 0.68rem;
  color: var(--text-dim);
  font-weight: 600;
}

.stat-value {
  font-size: 1.3rem;
  font-weight: 800;
  color: var(--text);
  letter-spacing: 0.3px;
}

.stat-value.up { color: var(--green); }
.stat-value.down { color: var(--red); }

/* ===== Controls ===== */
.controls {
  position: relative;
  z-index: 4;
  padding: 6px 24px 18px;
  position: sticky;
  top: 66px;
}

.controls-inner {
  max-width: 1280px;
  margin: 0 auto;
  background: rgba(20, 28, 53, 0.75);
  backdrop-filter: blur(14px);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 14px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  box-shadow: 0 14px 34px -18px rgba(0,0,0,0.6);
}

.search-box {
  position: relative;
  flex: 1 1 260px;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  right: 14px;
  width: 18px;
  height: 18px;
  color: var(--text-dim);
  pointer-events: none;
}

.search-box input {
  width: 100%;
  background: var(--bg2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 42px 12px 38px;
  border-radius: 12px;
  font-size: 0.92rem;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.search-box input::placeholder { color: var(--text-dim); }

.search-box input:focus {
  border-color: var(--green-dark);
  box-shadow: 0 0 0 3px var(--green-glow);
  outline: none;
}

.clear-btn {
  position: absolute;
  left: 10px;
  background: var(--surface2);
  color: var(--text-muted);
  border: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.clear-btn:hover { background: var(--red); color: #fff; }

.market-tabs {
  display: flex;
  gap: 6px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px;
  flex-wrap: wrap;
}

.tab {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 8px 16px;
  border-radius: 9px;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
  white-space: nowrap;
}

.tab:hover { color: var(--text); }

.tab.active {
  background: linear-gradient(135deg, var(--green), var(--green-dark));
  color: #04231a;
  box-shadow: 0 4px 14px -4px var(--green-glow);
}

.meta-row {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-inline-start: auto;
}

.results-count {
  font-size: 0.78rem;
  color: var(--text-dim);
  font-weight: 600;
  white-space: nowrap;
}

.refresh-btn {
  display: flex;
  align-items: center;
  gap: 7px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 10px;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.2s;
}

.refresh-btn:hover { border-color: var(--green-dark); }
.refresh-btn:active { transform: scale(0.96); }
.refresh-icon { width: 15px; height: 15px; }
.refresh-btn.spinning .refresh-icon { animation: spin 0.8s linear infinite; }

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Table ===== */
.table-section {
  position: relative;
  z-index: 2;
  max-width: 1280px;
  margin: 0 auto;
  padding: 4px 24px 60px;
}

.table-wrap {
  border: 1px solid var(--border);
  border-radius: 18px;
  overflow: auto;
  background: linear-gradient(180deg, var(--surface), var(--bg2));
  box-shadow: 0 24px 48px -30px rgba(0,0,0,0.7);
}

.stock-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 900px;
}

.stock-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg3);
  color: var(--text-muted);
  font-size: 0.74rem;
  font-weight: 700;
  text-align: right;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  transition: color 0.2s;
}

.stock-table thead th:hover { color: var(--green); }

.stock-table thead th::after {
  content: '';
  display: inline-block;
  width: 0; height: 0;
  margin-inline-start: 5px;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 5px solid var(--text-dim);
  opacity: 0.5;
  vertical-align: middle;
}

.stock-table thead th.sort-asc::after {
  border-top: none;
  border-bottom: 5px solid var(--green);
  opacity: 1;
}
.stock-table thead th.sort-desc::after {
  border-top: 5px solid var(--green);
  opacity: 1;
}

.stock-table thead th.col-rank { width: 46px; }
.stock-table thead th.col-num, .stock-table thead th.col-pct { text-align: left; }

.stock-table tbody td {
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 0.86rem;
  color: var(--text);
  vertical-align: middle;
}

.stock-table tbody tr {
  transition: background 0.15s ease;
}

.stock-table tbody tr:hover { background: rgba(0, 200, 150, 0.05); }
.stock-table tbody tr:last-child td { border-bottom: none; }

.col-rank { color: var(--text-dim); font-weight: 700; font-size: 0.78rem; }

.name-cell { display: flex; flex-direction: column; gap: 2px; max-width: 260px; }
.name-cell .n-main {
  font-weight: 700;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.name-cell .n-eng {
  font-size: 0.7rem;
  color: var(--text-dim);
  font-family: 'Segoe UI', sans-serif;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.code-badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.4px;
  color: var(--gold);
  background: rgba(245, 166, 35, 0.1);
  border: 1px solid rgba(245, 166, 35, 0.25);
  padding: 3px 9px;
  border-radius: 7px;
  font-family: 'Consolas', monospace;
  direction: ltr;
}

.market-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 100px;
  background: var(--surface2);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.market-badge .m-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--green); }
.market-badge.nilex .m-dot { background: var(--gold); }

.num-cell {
  text-align: left;
  direction: ltr;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.num-cell.dim { color: var(--text-dim); font-weight: 500; }

.pct-cell { text-align: left; direction: ltr; min-width: 130px; }
.pct-wrap { display: flex; align-items: center; gap: 8px; }
.pct-bar-track {
  flex: 1;
  height: 6px;
  background: var(--bg2);
  border-radius: 4px;
  overflow: hidden;
  min-width: 46px;
}
.pct-bar-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.6s ease;
}
.pct-num {
  font-weight: 800;
  font-size: 0.82rem;
  min-width: 46px;
  text-align: left;
}

.loading-row td {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
  border-bottom: none;
}

.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--surface2);
  border-top-color: var(--green);
  border-radius: 50%;
  margin: 0 auto 14px;
  animation: spin 0.8s linear infinite;
}

.empty-state {
  text-align: center;
  padding: 70px 20px;
  color: var(--text-dim);
}
.empty-icon { width: 44px; height: 44px; margin-bottom: 14px; opacity: 0.6; }
.empty-state p { margin: 0 0 16px; font-size: 0.95rem; }
.empty-state button {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--green);
  padding: 9px 18px;
  border-radius: 10px;
  font-weight: 700;
  cursor: pointer;
  font-size: 0.82rem;
}
.empty-state button:hover { border-color: var(--green-dark); }

/* ===== Footer ===== */
.site-footer {
  position: relative;
  z-index: 2;
  text-align: center;
  padding: 20px 20px 40px;
  color: var(--text-dim);
  font-size: 0.75rem;
  border-top: 1px solid var(--border);
}

/* ===== Row entrance ===== */
@keyframes row-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
.stock-table tbody tr { animation: row-in 0.3s ease both; }

/* ===================================================== */
/* ===================== MOBILE ========================= */
/* ===================================================== */
@media (max-width: 760px) {
  .header-inner { padding: 12px 16px; }
  .brand-text h1 { font-size: 1.08rem; }
  .clock { display: none; }

  .hero { padding: 26px 16px 16px; }
  .hero-inner { gap: 20px; }
  .hero-stats { grid-template-columns: repeat(2, 1fr); max-width: 100%; }
  .hero-copy p { max-width: 100%; }

  .controls { padding: 6px 16px 14px; top: 58px; }
  .controls-inner { padding: 12px; border-radius: 16px; }
  .market-tabs { width: 100%; overflow-x: auto; }
  .meta-row { width: 100%; margin-inline-start: 0; justify-content: space-between; }

  .table-section { padding: 4px 12px 40px; }

  /* Turn table into stacked cards */
  .stock-table, .stock-table thead, .stock-table tbody, .stock-table tr, .stock-table td {
    display: block;
    width: 100%;
  }
  .stock-table { min-width: 0; }
  .stock-table thead { display: none; }

  .stock-table tbody tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    margin-bottom: 12px;
    padding: 14px;
    position: relative;
  }
  .stock-table tbody tr:hover { background: var(--surface); }

  .stock-table tbody td {
    border-bottom: none;
    padding: 6px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    font-size: 0.85rem;
  }

  .stock-table tbody td.col-rank { display: none; }

  .stock-table tbody td.col-name {
    padding-bottom: 10px;
    margin-bottom: 8px;
    border-bottom: 1px dashed var(--border);
    display: block;
  }
  .name-cell { max-width: 100%; flex-direction: row; align-items: center; justify-content: space-between; gap: 8px; }
  .name-cell .n-main, .name-cell .n-eng { white-space: normal; }
  .name-cell .n-block { display: flex; flex-direction: column; gap: 2px; overflow: hidden; }

  .stock-table tbody td.col-code::before,
  .stock-table tbody td.col-market::before,
  .stock-table tbody td[data-label]::before {
    content: attr(data-label);
    color: var(--text-dim);
    font-size: 0.72rem;
    font-weight: 700;
  }

  .num-cell, .pct-cell { text-align: right; }
  .pct-wrap { flex-direction: row-reverse; }
}

@media (min-width: 761px) {
  .stock-table tbody td.col-name { max-width: 280px; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .ticker-track { animation: none; }
  .stock-table tbody tr { animation: none; }
  .status-pill.live .dot { animation: none; }
  * { transition-duration: 0.01ms !important; }
}
