/* ==========================================================================
   NeuroDifference — skin layer (v3.0)
   Loaded AFTER styles.css. Deliberately overrides: motion system, living
   background, visual skins, text-overflow hardening, focus and touch ergonomics.
   Rule of the file: animate transform/opacity only. Never layout, never
   box-shadow, never filter on large areas — that is what made the old build
   feel janky on Android.
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. Motion + surface tokens
   -------------------------------------------------------------------------- */

:root {
  --t-1: 120ms;
  --t-2: 180ms;
  --t-3: 260ms;
  --t-4: 420ms;

  --e-out: cubic-bezier(0.22, 0.61, 0.36, 1);
  --e-in: cubic-bezier(0.4, 0, 0.7, 0.2);
  --e-spring: cubic-bezier(0.2, 0.9, 0.25, 1.06);

  /* Derived from --accent so a custom hex propagates everywhere. */
  --accent-soft: color-mix(in srgb, var(--accent) 16%, transparent);
  --accent-line: color-mix(in srgb, var(--accent) 38%, transparent);
  --accent-glow: color-mix(in srgb, var(--accent) 42%, transparent);

  --glass: color-mix(in srgb, var(--bg-2) 78%, transparent);
  --glass-line: color-mix(in srgb, var(--text) 9%, transparent);
  --glass-hi: color-mix(in srgb, var(--text) 6%, transparent);

  --fx-opacity: 0.6;
  --ring: 0 0 0 2px var(--bg);
}

/* Scrollbars: thin, tinted, never jumpy. */
* {
  scrollbar-width: thin;
  scrollbar-color: color-mix(in srgb, var(--text) 26%, transparent) transparent;
}
*::-webkit-scrollbar { width: 9px; height: 9px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb {
  background: color-mix(in srgb, var(--text) 22%, transparent);
  border-radius: 99px;
  border: 2px solid transparent;
  background-clip: content-box;
}
*::-webkit-scrollbar-thumb:hover { background: color-mix(in srgb, var(--text) 34%, transparent); background-clip: content-box; }

/* --------------------------------------------------------------------------
   2. Living background — aurora blobs + hairline grid + grain
   Markup: <div class="bgfx" aria-hidden="true"><i></i><i></i><i></i></div>
   Cost: three composited layers moving on transform. No filters, no repaint.
   -------------------------------------------------------------------------- */

.bgfx {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
  contain: strict;
  opacity: var(--fx-opacity);
}

.bgfx i {
  position: absolute;
  display: block;
  border-radius: 50%;
  will-change: transform;
  transform: translate3d(0, 0, 0);
}

.bgfx i:nth-child(1) {
  width: 88vmax;
  height: 88vmax;
  left: -30vmax;
  top: -34vmax;
  background: radial-gradient(circle at 50% 50%, var(--accent-soft) 0%, transparent 62%);
  animation: nd-drift-a 58s var(--e-out) infinite alternate;
}

.bgfx i:nth-child(2) {
  width: 72vmax;
  height: 72vmax;
  right: -26vmax;
  top: 6vmax;
  background: radial-gradient(circle at 50% 50%, color-mix(in srgb, var(--accent) 12%, transparent) 0%, transparent 60%);
  animation: nd-drift-b 74s var(--e-out) infinite alternate;
  opacity: 0.85;
}

.bgfx i:nth-child(3) {
  width: 96vmax;
  height: 96vmax;
  left: 8vmax;
  bottom: -46vmax;
  background: radial-gradient(circle at 50% 50%, color-mix(in srgb, var(--accent) 9%, transparent) 0%, transparent 64%);
  animation: nd-drift-c 92s var(--e-out) infinite alternate;
  opacity: 0.7;
}

/* Hairline grid, masked to the top so content stays clean. */
.bgfx::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(to right, var(--accent-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--accent-line) 1px, transparent 1px);
  background-size: 64px 64px, 64px 64px;
  opacity: 0.07;
  -webkit-mask-image: radial-gradient(120% 90% at 50% -10%, #000 0%, transparent 70%);
  mask-image: radial-gradient(120% 90% at 50% -10%, #000 0%, transparent 70%);
}

/* Grain: a single tiny radial tile, repeated. One paint, no filter. */
.bgfx::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: radial-gradient(color-mix(in srgb, var(--text) 40%, transparent) 0.5px, transparent 0.5px);
  background-size: 3px 3px;
  opacity: 0.045;
  mix-blend-mode: overlay;
}

@keyframes nd-drift-a {
  from { transform: translate3d(0, 0, 0) scale(1); }
  to   { transform: translate3d(6vmax, 8vmax, 0) scale(1.12); }
}
@keyframes nd-drift-b {
  from { transform: translate3d(0, 0, 0) scale(1.06); }
  to   { transform: translate3d(-8vmax, 6vmax, 0) scale(0.94); }
}
@keyframes nd-drift-c {
  from { transform: translate3d(0, 0, 0) scale(0.96); }
  to   { transform: translate3d(4vmax, -7vmax, 0) scale(1.1); }
}

/* The shell must sit above the background layer. */
#root,
.shell,
.app-shell,
#view {
  position: relative;
  z-index: 1;
}

/* --------------------------------------------------------------------------
   3. Route transition + entrance choreography
   Applied to every navigation because main.js builds a fresh #view element.
   -------------------------------------------------------------------------- */

@keyframes nd-view-in {
  from { opacity: 0; transform: translate3d(0, 10px, 0) scale(0.996); }
  to   { opacity: 1; transform: none; }
}

#view {
  animation: nd-view-in var(--t-3) var(--e-out) both;
}

@keyframes nd-rise {
  from { opacity: 0; transform: translate3d(0, 8px, 0); }
  to   { opacity: 1; transform: none; }
}

/* Only the first handful of a page animate: a 148-card grid must not. */
#view > * { animation: nd-rise var(--t-3) var(--e-out) both; }
#view > *:nth-child(1) { animation-delay: 0ms; }
#view > *:nth-child(2) { animation-delay: 34ms; }
#view > *:nth-child(3) { animation-delay: 60ms; }
#view > *:nth-child(4) { animation-delay: 84ms; }
#view > *:nth-child(5) { animation-delay: 104ms; }
#view > *:nth-child(6) { animation-delay: 120ms; }
#view > *:nth-child(n + 7) { animation-delay: 120ms; }

/* Opt-in stagger for small, hand-picked groups. */
.stagger > * { animation: nd-rise var(--t-3) var(--e-out) both; }
.stagger > *:nth-child(1) { animation-delay: 0ms; }
.stagger > *:nth-child(2) { animation-delay: 40ms; }
.stagger > *:nth-child(3) { animation-delay: 74ms; }
.stagger > *:nth-child(4) { animation-delay: 100ms; }
.stagger > *:nth-child(5) { animation-delay: 124ms; }
.stagger > *:nth-child(6) { animation-delay: 144ms; }
.stagger > *:nth-child(n + 7) { animation-delay: 160ms; }

/* --------------------------------------------------------------------------
   4. Surfaces, hover, press
   -------------------------------------------------------------------------- */

.card {
  position: relative;
  transition:
    transform var(--t-2) var(--e-out),
    border-color var(--t-2) var(--e-out),
    background-color var(--t-2) var(--e-out);
}

/* Glow is a pseudo-element fading in — never an animated box-shadow. */
.card.hoverable::after,
a.card::after {
  content: "";
  position: absolute;
  inset: -1px;
  border-radius: inherit;
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--t-2) var(--e-out);
  background: radial-gradient(120% 80% at 50% 0%, var(--accent-soft) 0%, transparent 62%);
}

.card.hoverable:hover,
a.card:hover {
  transform: translate3d(0, -2px, 0);
  border-color: var(--accent-line);
}

.card.hoverable:hover::after,
a.card:hover::after { opacity: 1; }

.card.hoverable:active,
a.card:active { transform: translate3d(0, 0, 0) scale(0.995); }

@media (hover: none) {
  .card.hoverable:hover,
  a.card:hover { transform: none; border-color: inherit; }
  .card.hoverable:hover::after,
  a.card:hover::after { opacity: 0; }
  .card.hoverable:active,
  a.card:active { transform: scale(0.99); }
}

.btn,
.chip,
.chip-btn,
.icon-btn,
.nm-row,
.list-row,
.tabs [data-act] {
  transition:
    transform var(--t-1) var(--e-out),
    background-color var(--t-1) var(--e-out),
    border-color var(--t-1) var(--e-out),
    color var(--t-1) var(--e-out);
}

.btn:active,
.chip-btn:active,
.icon-btn:active { transform: scale(0.965); }

.btn.primary {
  box-shadow: 0 1px 0 var(--glass-hi) inset;
}

/* Keyboard focus: visible, never on mouse click. */
:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 10px;
}

/* Touch targets: comfortable minimum on touch devices. */
@media (pointer: coarse) {
  .icon-btn { min-width: 44px; min-height: 44px; }
  .chip-btn { min-height: 34px; }
  .nm-row { min-height: 48px; }
}

/* --------------------------------------------------------------------------
   5. Text overflow hardening
   The single biggest source of "text hits the right wall": flex/grid children
   refusing to shrink, and unbreakable strings (ICD codes, CYP3A4, long ids).
   -------------------------------------------------------------------------- */

:where(
  .card, .row, .stack, .split, .split-wide, .grid-2, .grid-3, .grid-4, .grid-auto,
  .detail-head, .list-row, .block, .block-head, .atlas-grid, .netmap, .nm-row,
  .organ-layout, .effect-cols, .effect-box, .tabs, .stat-tile, .paper, .pm-search
) > * {
  min-width: 0;
}

:where(h1, h2, h3, h4, h5, p, li, dd, dt, td, th, figcaption, blockquote, label, small) {
  overflow-wrap: anywhere;
  word-break: normal;
  hyphens: auto;
}

:where(.chip, .tag, .mono, .detail-alias, .detail-title, .stat-value, .nm-badge, .badge) {
  overflow-wrap: anywhere;
  min-width: 0;
}

h1, h2, h3 { text-wrap: balance; }
p, li { text-wrap: pretty; }

/* Long compound ids must never push a row wider than its container. */
.nm-row-name,
.nm-row-meta,
.compound-card .card-title,
.list-row span { min-width: 0; overflow: hidden; text-overflow: ellipsis; }

/* Images and inline SVG never overflow. */
:where(img, svg, canvas, video) { max-width: 100%; }

/* --------------------------------------------------------------------------
   6. Visual skins
   Skins change texture and typography, not hue — the hex accent stays king.
   -------------------------------------------------------------------------- */

/* 6a. Aurora Glass — default: glass panels, soft inner highlight. */
[data-skin="aurora"] .card,
[data-skin="aurora"] .sheet,
[data-skin="aurora"] .overlay-panel {
  background: var(--glass);
  border-color: var(--glass-line);
  box-shadow:
    inset 0 1px 0 var(--glass-hi),
    var(--shadow-2, 0 8px 24px rgb(0 0 0 / 0.28));
}

[data-skin="aurora"] .card.spot::before { opacity: 0.9; }

/* 6b. Oscilloscope — lab instrument: mono type, phosphor edges, scanlines. */
[data-skin="scope"] {
  --sans: var(--mono);
}

[data-skin="scope"] .card,
[data-skin="scope"] .sheet {
  background: color-mix(in srgb, var(--bg) 88%, var(--accent) 4%);
  border-color: color-mix(in srgb, var(--accent) 22%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 8%, transparent);
}

[data-skin="scope"] .card-title,
[data-skin="scope"] h1,
[data-skin="scope"] h2 {
  letter-spacing: 0.01em;
  text-shadow: 0 0 18px var(--accent-soft);
  font-weight: 500;
}

[data-skin="scope"] .bgfx::before { opacity: 0.16; background-size: 32px 32px, 32px 32px; }

[data-skin="scope"] body::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  background: repeating-linear-gradient(to bottom, color-mix(in srgb, var(--text) 5%, transparent) 0 1px, transparent 1px 3px);
  opacity: 0.35;
  mix-blend-mode: soft-light;
}

/* 6c. Prism — iridescent hairline, sharp corners, chromatic headings. */
[data-skin="prism"] .card {
  background: color-mix(in srgb, var(--bg-2) 92%, transparent);
  border-color: transparent;
  border-radius: 6px;
}

[data-skin="prism"] .card::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  padding: 1px;
  pointer-events: none;
  background: conic-gradient(
    from 210deg,
    color-mix(in srgb, var(--accent) 70%, transparent),
    color-mix(in srgb, #6ee7ff 55%, transparent),
    color-mix(in srgb, var(--accent) 40%, transparent),
    color-mix(in srgb, #ff8ad4 45%, transparent),
    color-mix(in srgb, var(--accent) 70%, transparent)
  );
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  mask-composite: exclude;
  opacity: 0.5;
  transition: opacity var(--t-3) var(--e-out);
}

[data-skin="prism"] .card:hover::before { opacity: 0.95; }

[data-skin="prism"] h1,
[data-skin="prism"] .page-title {
  background: linear-gradient(92deg, var(--text) 0%, var(--accent) 55%, #6ee7ff 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

/* --------------------------------------------------------------------------
   7. Reduced motion and the in-app switch
   -------------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
  .bgfx i { animation: none !important; }
}

html[data-motion="off"] *,
html[data-motion="off"] *::before,
html[data-motion="off"] *::after {
  animation-duration: 0.001ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.001ms !important;
}

html[data-motion="off"] .bgfx i { animation: none !important; transform: none !important; }

html[data-motion="off"] .bgfx { opacity: 0.4; }

/* --------------------------------------------------------------------------
   8. Small ergonomics
   -------------------------------------------------------------------------- */

html { scroll-behavior: smooth; }

::selection {
  background: color-mix(in srgb, var(--accent) 34%, transparent);
  color: var(--text);
}

/* Sticky headers inside long pages shouldn't cover anchors. */
:where([id]) { scroll-margin-top: calc(var(--top-h, 56px) + 12px); }

/* Long tables scroll horizontally instead of breaking the layout. */
.table-wrap { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-wrap > table { min-width: 520px; }

/* Skeleton shimmer used by research + lazy panels. */
@keyframes nd-shimmer {
  from { opacity: 0.45; }
  to   { opacity: 0.85; }
}
.skeleton { animation: nd-shimmer 1.1s var(--e-out) infinite alternate; }

/* Live dot pulse. */
@keyframes nd-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50%      { transform: scale(1.55); opacity: 0.45; }
}
.live-dot::after { animation: nd-pulse 2.1s var(--e-out) infinite; }