/* ==========================================================================
   Membrane Science — Mohammed Anwar Mahram
   Same foundations as ghost545a.github.io: system type, quiet surfaces,
   translucent chrome, one accent colour. Logical properties so the Arabic
   page mirrors without a second stylesheet.
   ========================================================================== */

:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --stage: #e8e8ed;
  --text: #1d1d1f;
  --text-2: #6e6e73;
  --text-3: #86868b;
  --hairline: rgba(0, 0, 0, 0.1);
  --chip: rgba(0, 0, 0, 0.05);
  --accent: #0066cc;
  --accent-fill: #0071e3;
  --accent-fill-hover: #0077ed;
  --accent-tint: rgba(0, 113, 227, 0.1);
  --cl: #1f9d55;          /* chloride, as in the simulation */
  --na: #8e44ad;          /* sodium */
  --warn: #b25000;
  --warn-tint: rgba(255, 149, 0, 0.14);
  --nav-bg: rgba(251, 251, 253, 0.72);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.05), 0 12px 32px rgba(0, 0, 0, 0.07);
  --focus: 0 0 0 3px rgba(0, 113, 227, 0.55);

  --gutter: 16px;
  --max: 1120px;
  --prose: 44rem;
  --radius: 22px;
  --ease-out: cubic-bezier(0.22, 0.75, 0.2, 1);

  --font-text: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI Variable Text", "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-display: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI Variable Display", "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, Menlo, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #161617;
    --stage: #101012;
    --text: #f5f5f7;
    --text-2: #a1a1a6;
    --text-3: #86868b;
    --hairline: rgba(255, 255, 255, 0.14);
    --chip: rgba(255, 255, 255, 0.09);
    --accent: #2997ff;
    --accent-tint: rgba(41, 151, 255, 0.16);
    --cl: #34c759;
    --na: #bf5af2;
    --warn: #ff9f0a;
    --warn-tint: rgba(255, 159, 10, 0.16);
    --nav-bg: rgba(22, 22, 23, 0.72);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 12px 32px rgba(0, 0, 0, 0.5);
    --focus: 0 0 0 3px rgba(41, 151, 255, 0.6);
  }
}

@media (min-width: 640px) { :root { --gutter: 24px; } }

/* ---------- Base ---------- */

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html {
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  scroll-padding-top: 64px;
}
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

body {
  margin: 0;
  font-family: var(--font-text);
  font-size: 1.0625rem;
  line-height: 1.55;
  letter-spacing: -0.006em;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-x: hidden;
}
:lang(ar) body, body:lang(ar) { letter-spacing: 0; line-height: 1.75; }

img { display: block; max-width: 100%; height: auto; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 0.18em; }
h1, h2, h3, h4 { margin: 0; font-family: var(--font-display); }
p { margin: 0 0 1em; }
p:last-child { margin-bottom: 0; }
button { font: inherit; color: inherit; }
sub, sup { line-height: 0; }
code { font-family: var(--font-mono); font-size: 0.88em; padding: 0.06em 0.32em; border-radius: 6px; background: var(--chip); }
:focus { outline: none; }
:focus-visible { box-shadow: var(--focus); border-radius: 10px; }

.wrap { width: min(var(--max), 100% - var(--gutter) * 2); margin-inline: auto; }

.skip {
  position: absolute; inset-inline-start: var(--gutter); top: -100px; z-index: 100;
  padding: 10px 16px; border-radius: 999px; background: var(--accent-fill); color: #fff;
}
.skip:focus-visible { top: 10px; }

/* ---------- Nav ---------- */

.nav {
  position: sticky; top: 0; z-index: 50;
  background: var(--nav-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--hairline);
}
.nav-inner { display: flex; align-items: center; gap: 20px; height: 52px; }
.brand { font-weight: 600; font-size: 0.95rem; color: var(--text); white-space: nowrap; letter-spacing: -0.01em; }
.brand:hover { text-decoration: none; }
.nav-links { display: flex; gap: 18px; margin-inline-start: auto; overflow-x: auto; scrollbar-width: none; }
.nav-links::-webkit-scrollbar { display: none; }
.nav-links a { color: var(--text-2); font-size: 0.82rem; white-space: nowrap; }
.nav-links a:hover { color: var(--text); text-decoration: none; }
.langs { display: flex; gap: 2px; padding: 2px; border-radius: 999px; background: var(--chip); flex: none; }
.langs a { font-size: 0.78rem; padding: 3px 10px; border-radius: 999px; color: var(--text-2); }
.langs a:hover { text-decoration: none; color: var(--text); }
.langs a[aria-current="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,0.12); }
@media (max-width: 820px) { .nav-links { display: none; } .langs { margin-inline-start: auto; } }

/* ---------- Hero ---------- */

.hero { padding: clamp(56px, 10vw, 120px) 0 clamp(40px, 6vw, 72px); }
.hero h1 {
  font-size: clamp(2.6rem, 7vw, 4.75rem);
  line-height: 1.04; letter-spacing: -0.035em; font-weight: 700;
}
:lang(ar) .hero h1 { letter-spacing: 0; line-height: 1.25; }
.hero .by { margin-top: 14px; font-size: clamp(1.2rem, 2.4vw, 1.6rem); color: var(--text-2); letter-spacing: -0.015em; font-family: var(--font-display); }
.hero .lede { margin-top: 28px; max-width: 40rem; font-size: 1.2rem; line-height: 1.5; color: var(--text); }
.hero .credit { margin-top: 22px; color: var(--text-2); font-size: 0.95rem; max-width: 40rem; }
.hero .actions { margin-top: 28px; display: flex; flex-wrap: wrap; gap: 12px; }

.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 20px; border-radius: 999px; border: 0; cursor: pointer;
  font-size: 0.95rem; font-weight: 500; line-height: 1.2;
  transition: background-color 0.2s, scale 0.25s var(--ease-out);
}
.btn:hover { text-decoration: none; }
.btn:active { scale: 0.97; }
.btn-fill { background: var(--accent-fill); color: #fff; }
.btn-fill:hover { background: var(--accent-fill-hover); }
.btn-tinted { background: var(--accent-tint); color: var(--accent); }
.btn-plain { background: var(--chip); color: var(--text); }
.btn[aria-pressed="true"] { background: var(--accent-fill); color: #fff; }
.btn-sm { padding: 7px 14px; font-size: 0.85rem; }

/* ---------- Sections ---------- */

.section { padding: clamp(56px, 8vw, 104px) 0; border-top: 1px solid var(--hairline); }
.eyebrow { font-size: 0.95rem; font-weight: 600; color: var(--text-2); margin-bottom: 10px; }
.section h2 {
  font-size: clamp(2rem, 4.4vw, 3rem); line-height: 1.08; letter-spacing: -0.028em; font-weight: 700;
  max-width: 22ch; margin-bottom: 20px;
}
:lang(ar) .section h2 { letter-spacing: 0; line-height: 1.3; }
.section h3 { font-size: 1.35rem; letter-spacing: -0.018em; margin: 44px 0 12px; font-weight: 650; }
:lang(ar) .section h3 { letter-spacing: 0; }
.section h4 { font-size: 1.05rem; margin: 22px 0 8px; font-weight: 600; }
.intro { font-size: 1.2rem; color: var(--text-2); max-width: var(--prose); margin-bottom: 8px; }
.prose { max-width: var(--prose); }
.prose ul, .prose ol { padding-inline-start: 1.3em; margin: 0 0 1em; }
.prose li { margin-bottom: 0.4em; }
.prose li::marker { color: var(--text-3); }

/* Equations: KaTeX display blocks, with a plain caption under each */
.eq {
  margin: 18px 0 20px; padding: 18px 20px; border-radius: 16px; background: var(--surface);
  box-shadow: var(--shadow); overflow-x: auto; max-width: var(--prose);
}
.eq .katex-display { margin: 0; }
.eq .where { margin-top: 10px; font-size: 0.88rem; color: var(--text-2); }
.eq[dir], .katex { direction: ltr; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 18px 0 24px; border-radius: 16px; background: var(--surface); box-shadow: var(--shadow); }
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; font-variant-numeric: tabular-nums; }
th, td { text-align: start; padding: 11px 16px; border-bottom: 1px solid var(--hairline); white-space: nowrap; }
th { font-weight: 600; color: var(--text-2); font-size: 0.82rem; }
tr:last-child td { border-bottom: 0; }
td.wrap-ok { white-space: normal; min-width: 14rem; }

/* Figures */
figure { margin: 24px 0; }
figure img { border-radius: 16px; background: var(--surface); }
figcaption { margin-top: 10px; font-size: 0.88rem; color: var(--text-2); max-width: var(--prose); }
.fig-row { display: grid; gap: 20px; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); margin: 24px 0; }
.fig-row figure { margin: 0; }

/* Diagrams drawn in SVG */
.diagram { background: var(--surface); border-radius: 16px; box-shadow: var(--shadow); padding: 16px; }
.diagram svg { width: 100%; height: auto; display: block; }
.diagram text { fill: var(--text); font-family: var(--font-text); font-size: 13px; }
.diagram .muted { fill: var(--text-2); }

/* Cards */
.cards { display: grid; gap: 20px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); margin-top: 32px; }
.card { background: var(--surface); border-radius: var(--radius); padding: 26px 26px 24px; box-shadow: var(--shadow); }
.card h3 { margin: 0 0 4px; font-size: 1.3rem; }
.card .where { color: var(--text-2); font-size: 0.9rem; margin-bottom: 14px; }
.card dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0 0 14px; font-size: 0.92rem; }
.card dt { color: var(--text-2); }
.card dd { margin: 0; font-variant-numeric: tabular-nums; }
.card p { font-size: 0.97rem; }
.card .tech { display: inline-block; font-size: 0.78rem; font-weight: 600; color: var(--text-2); margin-bottom: 8px; }
.card .tech.pro { color: var(--warn); }
.note {
  margin: 22px 0; padding: 16px 18px; border-radius: 14px; background: var(--accent-tint);
  max-width: var(--prose); font-size: 0.97rem;
}

/* ---------- 3D stage ---------- */

.stage-wrap { margin-top: 32px; }
.stage {
  position: relative; border-radius: var(--radius); overflow: hidden; background: var(--stage);
  aspect-ratio: 16 / 9; min-height: 340px; touch-action: none;
}
@media (max-width: 640px) { .stage { aspect-ratio: 1 / 1; } }
.stage canvas { display: block; width: 100%; height: 100%; }
.stage .fallback { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; text-align: center; color: var(--text-2); }
.labels { position: absolute; inset: 0; pointer-events: none; }
.label3d {
  position: absolute; left: 0; top: 0; transform: translate(-50%, -100%);
  font-size: 0.74rem; font-weight: 600; color: var(--text); white-space: nowrap;
  padding: 3px 8px; border-radius: 8px; background: var(--nav-bg);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  box-shadow: 0 1px 2px rgba(0,0,0,0.12);
  transition: opacity 0.2s;
}
.label3d.small { font-weight: 500; font-size: 0.7rem; color: var(--text-2); }
.labels-off .label3d { opacity: 0; }
.tip {
  position: absolute; pointer-events: none; z-index: 3; max-width: 260px;
  padding: 10px 12px; border-radius: 12px; font-size: 0.84rem; line-height: 1.4;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.tip b { display: block; margin-bottom: 2px; }

.stage-hint { position: absolute; inset-inline-start: 14px; bottom: 12px; font-size: 0.75rem; color: var(--text-2); pointer-events: none; }
.phase-tag {
  position: absolute; inset-inline-start: 14px; top: 12px; z-index: 2;
  font-size: 0.8rem; font-weight: 600; padding: 5px 10px; border-radius: 10px;
  background: var(--nav-bg); -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
}
.phase-tag.hypo { color: var(--warn); background: var(--warn-tint); }

/* Controls under the stage */
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; margin-top: 16px; }
.controls .spacer { flex: 1; }
.scrub { flex: 1 1 260px; display: flex; align-items: center; gap: 12px; }
.scrub input { flex: 1; accent-color: var(--accent-fill); }
.scrub output { font-variant-numeric: tabular-nums; font-size: 0.85rem; color: var(--text-2); min-width: 7.5em; text-align: end; }
.legend { display: flex; gap: 16px; font-size: 0.85rem; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-inline-end: 6px; vertical-align: -1px; }

/* HUD: live numbers + chart */
.hud { display: grid; gap: 20px; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); margin-top: 20px; }
.readouts { display: grid; grid-template-columns: repeat(2, 1fr); gap: 1px; background: var(--hairline); border-radius: 18px; overflow: hidden; box-shadow: var(--shadow); }
.readout { background: var(--surface); padding: 14px 16px; }
.readout .k { font-size: 0.78rem; color: var(--text-2); }
.readout .v { font-size: 1.35rem; font-weight: 600; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }
.readout .v small { font-size: 0.75rem; font-weight: 500; color: var(--text-2); letter-spacing: 0; }
.chart-card { background: var(--surface); border-radius: 18px; box-shadow: var(--shadow); padding: 14px 14px 10px; }
.chart-card .k { font-size: 0.78rem; color: var(--text-2); margin: 0 4px 6px; }
.chart-card canvas { width: 100%; height: 200px; display: block; }
.fineprint { font-size: 0.82rem; color: var(--text-2); margin-top: 14px; max-width: var(--prose); }

/* Coming soon */
.soon {
  margin-top: 40px; padding: 30px 28px; border-radius: var(--radius);
  border: 1.5px dashed var(--hairline); color: var(--text-2);
}
.soon h3 { margin: 0 0 6px; color: var(--text); }

/* References & footer */
.refs { font-size: 0.85rem; color: var(--text-2); padding-inline-start: 1.4em; max-width: 52rem; }
.refs li { margin-bottom: 8px; }
.footer { padding: 48px 0 64px; border-top: 1px solid var(--hairline); color: var(--text-2); font-size: 0.88rem; }
.footer p { max-width: 48rem; }
.footer strong { color: var(--text); font-weight: 600; }

/* ---------- Hero simulation ----------
   The looping diffusion cell sits beside the title on wide screens and under
   it on phones, so the first thing a visitor sees is ions moving. */
.hero-grid { display: grid; gap: 28px; align-items: center; }
.hero-sim { margin: 0; }
.stage-hero { aspect-ratio: 16 / 9; min-height: 240px; }
.hero-sim .legend { margin-top: 12px; flex-wrap: wrap; align-items: center; }
.hero-sim .legend a { margin-inline-start: auto; font-size: 0.85rem; }
@media (min-width: 760px) {
  .hero-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr); gap: 48px; }
  .hero .lede, .hero .credit { max-width: 32rem; }
  .stage-hero { aspect-ratio: 4 / 3; }
}
@media (max-width: 640px) { .stage-hero { aspect-ratio: 4 / 3; } }

/* Text under the simulation: part of the narrative, not a footnote. */
.sim-text { max-width: var(--prose); margin: 18px 0 0; color: var(--text-2); }

/* Phone-only: comfortable touch targets (nothing here applies on a PC-width screen) */
@media (max-width: 600px) {
  .langs a { padding: 9px 12px; }
  .btn-sm { padding: 11px 16px; }
  .scrub input { height: 36px; }
}
