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

:root {
  --bg: #0a0a0a;
  --bg-2: #0e0e10;
  --panel: #111114;
  --panel-2: #16161a;
  --line: rgba(255,255,255,0.08);
  --line-bright: rgba(255,255,255,0.16);
  --fg: #ededed;
  --fg-dim: #8a8a8f;
  --fg-faint: #4a4a50;
  --glow: rgba(255,255,255,0.08);

  --arab: #c08a5a;
  --arab-strong: #d9a472;

  --l-philosophical: #a78bfa;
  --l-political:     #f87171;
  --l-economic:      #fbbf24;
  --l-social:        #34d399;
  --l-cultural:      #f472b6;
  --l-technological: #22d3ee;
  --l-environmental: #a3e635;
}

html, body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: 'Tajawal', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-weight: 300;
}

body::before {
  content: "";
  position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background:
    radial-gradient(ellipse 80% 60% at 50% 40%, rgba(167,139,250,0.04) 0%, transparent 70%),
    radial-gradient(ellipse 60% 60% at 20% 80%, rgba(34,211,238,0.03) 0%, transparent 70%);
}

.app {
  position: relative; z-index: 1;
  display: grid;
  grid-template-columns: 320px 1fr 340px;
  grid-template-rows: 56px 1fr auto;
  grid-template-areas:
    "top   top    top"
    "left  stage  right"
    "bot   bot    bot";
  height: 100vh;
  width: 100vw;
}

/* ---------- TOP ---------- */
.topbar {
  grid-area: top;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 1.25rem;
  gap: 1rem;
}
.back {
  color: var(--fg-dim); text-decoration: none; font-size: 0.85rem;
  letter-spacing: 0.02em; transition: color 0.2s;
}
.back:hover { color: var(--fg); }

.brand { display: flex; align-items: center; gap: 0.6rem; }
.brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff 0%, #a78bfa 50%, #4c1d95 100%);
  box-shadow: 0 0 10px rgba(167,139,250,0.5);
}
.brand-title { font-size: 0.95rem; font-weight: 500; }
.brand-sub { font-size: 0.7rem; color: var(--fg-dim); letter-spacing: 0.15em; }

.live {
  display: flex; align-items: center; gap: 0.5rem;
  font-size: 0.8rem; color: var(--fg-dim);
  letter-spacing: 0.1em;
}
.live-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: #22d3ee;
  box-shadow: 0 0 8px #22d3ee;
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse { 0%,100% {opacity:1} 50% {opacity:0.35} }
.live-label { text-transform: uppercase; font-size: 0.65rem; }
.live-year {
  font-weight: 500; color: var(--fg); font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
  min-width: 3.5ch; text-align: center;
}

/* ---------- PANELS ---------- */
.panel {
  background: var(--panel);
  overflow-y: auto;
  padding: 1rem 1rem 2rem;
  display: flex; flex-direction: column; gap: 1.25rem;
}
.panel-left  { grid-area: left;  border-left: 1px solid var(--line); }
.panel-right { grid-area: right; border-right: 1px solid var(--line); }
.panel::-webkit-scrollbar { width: 4px; }
.panel::-webkit-scrollbar-thumb { background: var(--line-bright); border-radius: 2px; }

.section { display: flex; flex-direction: column; gap: 0.6rem; }
.section-head {
  display: flex; align-items: baseline; justify-content: space-between;
  padding-bottom: 0.4rem; border-bottom: 1px solid var(--line);
  font-size: 0.78rem; font-weight: 500; letter-spacing: 0.04em;
  color: var(--fg);
}
.section-sub, .link-btn {
  font-size: 0.62rem; color: var(--fg-dim); letter-spacing: 0.2em;
  font-weight: 500;
}
.link-btn {
  background: transparent; border: 1px solid var(--line); color: var(--fg-dim);
  padding: 0.2rem 0.55rem; border-radius: 3px; cursor: pointer;
  font-family: inherit; transition: all 0.15s;
}
.link-btn:hover { color: var(--fg); border-color: var(--line-bright); }

/* ---------- LEFT: concept card ---------- */
.concept-card {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0.9rem;
  display: flex; flex-direction: column; gap: 0.7rem;
  position: relative;
  overflow: hidden;
}
.concept-card::before {
  content: ""; position: absolute; top: 0; right: 0; bottom: 0; width: 2px;
  background: var(--layer-color, var(--fg-faint));
}
.concept-card .name-ar {
  font-size: 1.05rem; font-weight: 500; color: var(--fg);
}
.concept-card .name-en {
  font-size: 0.7rem; color: var(--fg-dim); letter-spacing: 0.1em;
  direction: ltr; text-align: right;
}
.concept-card .layer-tag {
  display: inline-block;
  font-size: 0.65rem; color: var(--layer-color, var(--fg-dim));
  border: 1px solid var(--layer-color, var(--line));
  padding: 0.12rem 0.45rem; border-radius: 99px;
  letter-spacing: 0.08em;
  width: fit-content;
  opacity: 0.85;
}
.concept-card .intensity-row {
  display: flex; align-items: center; gap: 0.6rem;
  padding-top: 0.5rem; border-top: 1px dashed var(--line);
}
.concept-card .i-label {
  font-size: 0.7rem; color: var(--fg-dim); letter-spacing: 0.08em;
}
.concept-card .i-bar {
  flex: 1; height: 4px; background: var(--line); border-radius: 2px;
  position: relative; overflow: hidden;
}
.concept-card .i-fill {
  position: absolute; inset: 0 auto 0 0; right: 0;
  background: var(--layer-color, var(--fg)); opacity: 0.9;
  border-radius: 2px; transition: width 0.1s linear;
}
.concept-card .i-val {
  font-size: 0.8rem; font-weight: 500; font-variant-numeric: tabular-nums;
  min-width: 3ch; text-align: left;
  color: var(--layer-color, var(--fg));
}
.concept-card .defn {
  font-size: 0.78rem; line-height: 1.7; color: var(--fg-dim);
  margin-top: 0.2rem;
}
.concept-card .keyrow {
  display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0.4rem;
  padding-top: 0.5rem; border-top: 1px dashed var(--line);
  font-size: 0.7rem;
}
.concept-card .keyrow div {
  display: flex; flex-direction: column; gap: 0.15rem;
}
.concept-card .keyrow .k { color: var(--fg-dim); font-size: 0.62rem; letter-spacing: 0.1em; }
.concept-card .keyrow .v { color: var(--fg); font-weight: 500; font-variant-numeric: tabular-nums; }

.empty-hint {
  color: var(--fg-dim); font-size: 0.78rem; line-height: 1.6;
  padding: 0.8rem 0; text-align: center;
}
.empty-hint.small { font-size: 0.72rem; padding: 0.4rem 0; }

.arab-list { display: flex; flex-direction: column; gap: 0.5rem; }
.arab-item {
  background: var(--panel-2); border: 1px solid var(--line);
  border-right: 2px solid var(--arab);
  padding: 0.55rem 0.7rem; border-radius: 3px;
  font-size: 0.75rem; line-height: 1.6;
  display: flex; flex-direction: column; gap: 0.2rem;
}
.arab-item .year {
  color: var(--arab-strong); font-weight: 500;
  font-variant-numeric: tabular-nums;
  font-size: 0.72rem;
}
.arab-item .lbl { color: var(--fg); font-weight: 500; }
.arab-item .note { color: var(--fg-dim); font-size: 0.72rem; }

.trivia {
  background: var(--panel-2); border: 1px dashed var(--line-bright);
  padding: 0.7rem 0.85rem; border-radius: 3px;
  font-size: 0.78rem; line-height: 1.7; color: var(--fg-dim);
}

/* ---------- STAGE ---------- */
.stage {
  grid-area: stage;
  position: relative;
  background:
    radial-gradient(ellipse at 50% 50%, rgba(255,255,255,0.02) 0%, transparent 70%),
    var(--bg);
  overflow: hidden;
}
.chart {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  cursor: crosshair;
}
.stage-hint {
  position: absolute; top: 1rem; right: 1rem;
  font-size: 0.72rem; color: var(--fg-faint);
  letter-spacing: 0.08em;
  pointer-events: none;
}

/* SVG elements */
.grid-line { stroke: rgba(255,255,255,0.04); stroke-width: 1; }
.grid-line.major { stroke: rgba(255,255,255,0.08); }
.axis-label {
  fill: var(--fg-faint); font-size: 10px;
  font-family: 'Tajawal', sans-serif;
  font-variant-numeric: tabular-nums;
}
.curve {
  fill: none; stroke-width: 1.5;
  opacity: 0.55;
  transition: opacity 0.2s, stroke-width 0.2s;
  pointer-events: stroke;
  cursor: pointer;
}
.curve:hover, .curve.selected {
  opacity: 1; stroke-width: 2.5;
  filter: drop-shadow(0 0 6px currentColor);
}
.curve.dim { opacity: 0.12; }
.curve-hit {
  fill: none; stroke: transparent; stroke-width: 12;
  cursor: pointer; pointer-events: stroke;
}
.curve-label {
  font-size: 11px; font-weight: 500;
  font-family: 'Tajawal', sans-serif;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}
.curve-label.selected { opacity: 1; font-size: 12px; font-weight: 700; }
.curve-label.hovered { opacity: 0.9; }
.curve-label.dim { opacity: 0; }

.event-dot {
  stroke: #0a0a0a; stroke-width: 1.5;
  cursor: pointer; pointer-events: all;
  transition: r 0.15s;
}
.event-dot:hover { stroke-width: 0; }

.arab-mark {
  stroke: var(--arab); stroke-width: 2;
  opacity: 0.7;
}
.arab-mark-top {
  fill: var(--arab-strong);
}

.scrubber-line {
  stroke: rgba(255,255,255,0.35);
  stroke-width: 1; stroke-dasharray: 3 3;
}
.scrubber-line.live { stroke: #fff; stroke-dasharray: none; }

.focus-band {
  fill: rgba(255,255,255,0.015);
}

/* Tooltip */
.tooltip {
  position: absolute;
  background: var(--panel-2);
  border: 1px solid var(--line-bright);
  border-radius: 3px;
  padding: 0.5rem 0.7rem;
  font-size: 0.75rem;
  color: var(--fg);
  pointer-events: none;
  opacity: 0; transform: translateY(4px);
  transition: opacity 0.15s, transform 0.15s;
  max-width: 240px;
  line-height: 1.5;
  z-index: 20;
  box-shadow: 0 6px 24px rgba(0,0,0,0.5);
}
.tooltip.show { opacity: 1; transform: translateY(0); }
.tooltip .t-year {
  color: var(--fg-dim); font-size: 0.68rem; letter-spacing: 0.1em;
  font-variant-numeric: tabular-nums; margin-bottom: 0.2rem;
}
.tooltip .t-name { font-weight: 500; color: var(--fg); margin-bottom: 0.25rem; }
.tooltip .t-body { color: var(--fg-dim); font-size: 0.72rem; }

/* ---------- RIGHT: toggles + filters + scenario ---------- */
.toggles { display: flex; flex-direction: column; gap: 0.35rem; }
.toggle {
  display: grid;
  grid-template-columns: 10px 1fr auto;
  gap: 0.6rem; align-items: center;
  background: var(--panel-2);
  border: 1px solid var(--line);
  padding: 0.5rem 0.65rem;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.15s;
  position: relative;
  overflow: hidden;
}
.toggle:hover { border-color: var(--line-bright); background: #1a1a1f; }
.toggle .swatch {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--color);
  box-shadow: 0 0 6px color-mix(in srgb, var(--color) 60%, transparent);
}
.toggle .name { font-size: 0.82rem; color: var(--fg); font-weight: 400; }
.toggle .state {
  width: 28px; height: 14px; border-radius: 7px;
  background: var(--line); position: relative;
  transition: background 0.15s;
}
.toggle .state::before {
  content: ""; position: absolute; top: 2px; right: 2px;
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--fg-dim);
  transition: all 0.2s;
}
.toggle.on { border-color: color-mix(in srgb, var(--color) 40%, var(--line)); }
.toggle.on .name { color: var(--fg); font-weight: 500; }
.toggle.on .state { background: color-mix(in srgb, var(--color) 50%, transparent); }
.toggle.on .state::before { right: auto; left: 2px; background: var(--color); box-shadow: 0 0 6px var(--color); }
.toggle.selected {
  background: color-mix(in srgb, var(--color) 10%, var(--panel-2));
  border-color: color-mix(in srgb, var(--color) 60%, var(--line));
}

.layer-chips { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.layer-chip {
  font-size: 0.7rem; padding: 0.25rem 0.55rem;
  border: 1px solid var(--line); border-radius: 99px;
  color: var(--fg-dim); background: var(--panel-2);
  cursor: pointer; transition: all 0.15s;
  font-family: inherit;
}
.layer-chip .dot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--color); margin-inline-end: 0.35rem;
  vertical-align: middle;
}
.layer-chip.on {
  color: var(--fg);
  border-color: color-mix(in srgb, var(--color) 50%, var(--line));
  background: color-mix(in srgb, var(--color) 10%, var(--panel-2));
}

.scenario-btn {
  background: transparent;
  border: 1px solid var(--line-bright);
  color: var(--fg); font-family: inherit;
  padding: 0.55rem 0.7rem; border-radius: 3px;
  font-size: 0.8rem; cursor: pointer;
  transition: all 0.15s;
}
.scenario-btn:hover { background: var(--panel-2); border-color: var(--fg-dim); }
.scenario-btn.active {
  border-color: #f472b6; color: #f472b6;
  background: rgba(244,114,182,0.06);
}
.scenario-note { font-size: 0.7rem; color: var(--fg-faint); line-height: 1.6; }

/* ---------- SCRUBBER ---------- */
.scrubber-bar {
  grid-area: bot;
  background: var(--bg-2);
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: 320px 1fr 340px;
  grid-template-rows: auto auto auto;
  grid-template-areas:
    "modal   focus   ."
    "modal   scrub   ."
    ".       range   .";
  column-gap: 0;
  row-gap: 0.45rem;
  padding: 0.7rem 0 0.8rem;
  align-items: center;
  --chart-pad: 40px;
}
.modal-open {
  grid-area: modal;
  align-self: center;
  justify-self: center;
  background: transparent; color: var(--fg);
  border: 1px solid var(--line-bright);
  padding: 0.5rem 0.85rem; border-radius: 3px;
  font-family: inherit; font-size: 0.78rem; cursor: pointer;
  transition: all 0.15s;
}
.modal-open:hover { border-color: var(--fg-dim); background: var(--panel-2); }

/* ---------- FOCUS CONTROLS ---------- */
.focus-controls {
  grid-area: focus;
  padding: 0 var(--chart-pad);
  display: flex; flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.9rem;
}
.focus-presets { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.focus-preset {
  font-family: inherit;
  font-size: 0.7rem;
  color: var(--fg-dim);
  background: var(--panel-2);
  border: 1px solid var(--line);
  padding: 0.25rem 0.6rem;
  border-radius: 99px;
  cursor: pointer;
  letter-spacing: 0.04em;
  transition: all 0.15s;
}
.focus-preset:hover { color: var(--fg); border-color: var(--line-bright); }
.focus-preset.active {
  color: var(--fg);
  border-color: color-mix(in srgb, #22d3ee 50%, var(--line));
  background: color-mix(in srgb, #22d3ee 10%, var(--panel-2));
}

.focus-inputs { display: flex; gap: 0.5rem; margin-inline-start: auto; }
.focus-input {
  display: flex; align-items: center; gap: 0.35rem;
  font-size: 0.68rem; color: var(--fg-dim);
  letter-spacing: 0.08em;
}
.focus-input input {
  width: 6.5ch;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--fg);
  font-family: inherit;
  font-variant-numeric: tabular-nums;
  font-size: 0.78rem;
  padding: 0.25rem 0.35rem;
  border-radius: 3px;
  text-align: center;
  outline: none;
  transition: border-color 0.15s;
  direction: ltr;
}
.focus-input input:focus { border-color: var(--line-bright); }
.focus-input input.invalid {
  border-color: #f87171;
  animation: focusShake 0.25s;
}
@keyframes focusShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-2px); }
  75% { transform: translateX(2px); }
}

.scrubber {
  grid-area: scrub;
  position: relative; height: 38px; cursor: pointer;
  user-select: none;
}
.scrubber-inner {
  position: absolute;
  top: 0; bottom: 0;
  left: var(--chart-pad); right: var(--chart-pad);
}
.scrubber-track {
  position: absolute; top: 50%; right: 0; left: 0;
  height: 1px; background: var(--line-bright); transform: translateY(-50%);
}
.scrubber-decades {
  position: absolute; top: 50%; right: 0; left: 0;
  height: 8px; transform: translateY(-4px);
  pointer-events: none;
}
.scrubber-decades span {
  position: absolute;
  width: 1px; height: 100%; background: rgba(255,255,255,0.08);
}
.scrubber-decades span.major { background: rgba(255,255,255,0.2); height: 12px; top: -2px; }

.scrubber-arab {
  position: absolute; top: 50%; right: 0; left: 0; height: 14px;
  transform: translateY(-50%); pointer-events: none;
}
.scrubber-arab span {
  position: absolute; width: 2px; height: 14px;
  background: var(--arab);
  box-shadow: 0 0 6px var(--arab-strong);
}

.scrubber-handle {
  position: absolute; top: 50%; transform: translate(50%, -50%);
  width: 2px; height: 28px;
  background: #fff;
  box-shadow: 0 0 12px rgba(255,255,255,0.6);
  cursor: grab;
  outline: none;
}
.scrubber-handle:focus-visible { box-shadow: 0 0 14px #22d3ee; background: #22d3ee; }
.scrubber-handle:active { cursor: grabbing; }
.handle-year {
  position: absolute; top: -22px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--line-bright);
  padding: 0.1rem 0.4rem;
  border-radius: 2px;
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
  color: var(--fg);
  white-space: nowrap;
}

.scrubber-range {
  grid-area: range;
  display: flex; justify-content: space-between;
  font-size: 0.65rem; color: var(--fg-faint);
  letter-spacing: 0.1em; font-variant-numeric: tabular-nums;
  padding: 0 var(--chart-pad);
  pointer-events: none;
  direction: ltr;
}
.scrubber-range #focusRange { color: var(--fg-dim); }

/* ---------- MODAL ---------- */
.modal {
  position: fixed; inset: 0; z-index: 100;
  display: none; align-items: center; justify-content: center;
  padding: 2rem;
}
.modal.show { display: flex; }
.modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(5,5,8,0.7); backdrop-filter: blur(6px);
}
.modal-shell {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line-bright);
  border-radius: 6px;
  max-width: 960px; width: 100%;
  max-height: 90vh; overflow: hidden;
  display: flex; flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
}
.modal-head {
  display: flex; align-items: center; gap: 1rem;
  padding: 0.9rem 1.2rem;
  border-bottom: 1px solid var(--line);
}
.modal-title { font-weight: 500; font-size: 1rem; }
.modal-sub { font-size: 0.72rem; color: var(--fg-dim); letter-spacing: 0.08em; }
.modal-close {
  margin-inline-start: auto;
  width: 28px; height: 28px; border-radius: 50%;
  background: transparent; border: 1px solid var(--line-bright);
  color: var(--fg); cursor: pointer;
  font-size: 1.2rem; line-height: 1;
  display: flex; align-items: center; justify-content: center;
}
.modal-close:hover { background: var(--panel-2); }
.modal-body {
  padding: 1rem 1.2rem;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 0.7rem;
}
.modal-card {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0.8rem 0.9rem;
  position: relative;
  cursor: pointer;
  transition: all 0.15s;
  overflow: hidden;
}
.modal-card::before {
  content: ""; position: absolute; top: 0; right: 0; bottom: 0; width: 2px;
  background: var(--color, var(--fg-faint));
}
.modal-card:hover {
  border-color: color-mix(in srgb, var(--color) 50%, var(--line-bright));
  transform: translateY(-2px);
}
.modal-card .m-name {
  font-size: 0.95rem; font-weight: 500; margin-bottom: 0.2rem;
}
.modal-card .m-layer {
  font-size: 0.65rem; color: var(--color); letter-spacing: 0.08em;
  margin-bottom: 0.5rem;
}
.modal-card .m-short {
  font-size: 0.75rem; color: var(--fg-dim); line-height: 1.6;
}
.modal-card .m-span {
  margin-top: 0.5rem; font-size: 0.7rem; color: var(--fg-faint);
  font-variant-numeric: tabular-nums;
  padding-top: 0.4rem; border-top: 1px dashed var(--line);
}

/* ---------- RESPONSIVE ---------- */
@media (max-width: 980px) {
  html, body { overflow: auto; height: auto; }
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: 56px auto auto auto;
    grid-template-areas:
      "top"
      "stage"
      "bot"
      "right";
    height: auto;
    min-height: 100dvh;
  }
  .stage { height: 60vh; min-height: 360px; }
  .panel-left { display: none; }
  .panel-right {
    border-right: none;
    border-top: 1px solid var(--line);
    max-height: none;
    padding: 1rem;
  }
  .stage-hint { display: none; }
  .scrubber-bar {
    grid-template-columns: 1fr;
    grid-template-areas:
      "focus"
      "scrub"
      "range"
      "modal";
    padding: 0.7rem 0 calc(0.8rem + env(safe-area-inset-bottom));
    column-gap: 0;
    row-gap: 0.5rem;
  }
  .modal-open { justify-self: center; margin-top: 0.3rem; }
  .focus-inputs { margin-inline-start: 0; }
}

@media (max-width: 620px) {
  .topbar { padding: 0 0.75rem; }
  .brand-sub { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
