/* Lichen v0.3 — visual substrate per biogame-fun-and-visual-meta
 * Okabe-Ito CVD-safe palette · system-ui (Atkinson recommended; CSP-compatible fallback)
 * Spring-feel easing · reduced-motion-first · WCAG 2.3.3 compliant
 *
 * Design principles applied (cycle-9 swarm synthesis):
 *   V1 vitality-as-organism-density (Suika principle) — see lichen.js renderOrganisms()
 *   V2 figure-ground separation (Gestalt) — alga=beads, fungus=filaments, distinct shape language
 *   V3 signifier-on-action-surface (Norman) — tap-organism IS the action
 *   V4 Okabe-Ito palette + spring easing + reduced-motion fallback
 */

:root {
  /* Okabe-Ito CVD-safe palette (Nature-adopted, 2008) */
  --bg-deep:    #1a1f1c;
  --bg-mid:     #242b27;
  --surface:    #2f3833;
  --ink:        #e8ebe6;
  --ink-mute:   #8a948c;
  --line:       #3a4540;

  --alga:       #56B4E9;   /* sky blue · autotroph · cool */
  --alga-low:   #2c5b75;
  --alga-halo:  rgba(86,180,233,0.08);

  --fungus:     #E69F00;   /* amber · heterotroph · warm */
  --fungus-low: #735030;
  --fungus-halo:rgba(230,159,0,0.07);

  --bond:       #009E73;   /* bluish-green, thriving cooperation */
  --warn:       #D55E00;   /* vermillion, crisis */
  --danger:     #b04a3a;

  /* Motion: spring-feel for organic, calm for chrome */
  --ease-organic: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-calm:    cubic-bezier(0.4, 0.0, 0.2, 1);
  --dur-breath:  4000ms;
  --dur-impact:   600ms;
  --dur-settle:  1200ms;

  /* Type — system-ui sans-serif, drop Georgia (signaled "blog post" not "game") */
  --font-system: system-ui, -apple-system, "Segoe UI", "Atkinson Hyperlegible", "Inter", Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

/* WCAG 2.3.3 reduced-motion: degrade to opacity/saturation only */
@media (prefers-reduced-motion: reduce) {
  :root {
    --dur-breath: 0ms;
    --dur-impact: 200ms;
    --dur-settle: 200ms;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 1rem 1rem 3rem;
  font-family: var(--font-system);
  background: radial-gradient(ellipse at 50% 30%, var(--bg-mid), var(--bg-deep) 70%);
  color: var(--ink);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  display: grid;
  gap: 1rem;
}

/* === Stage: the two organisms + bond arc === */
.stage {
  background: transparent;
  padding: 0.5rem 0 0;
  display: grid;
  place-items: center;
}

#organisms {
  width: 100%;
  height: auto;
  max-height: 60vh;
}

/* Idle "breathing" — the visual signature of two living beings phase-offset */
@keyframes breath-alga {
  0%, 100% { opacity: 0.92; transform: scale(1); }
  50%      { opacity: 1.00; transform: scale(1.02); }
}
@keyframes breath-fungus {
  0%, 100% { opacity: 0.94; transform: scale(0.98); }
  50%      { opacity: 1.00; transform: scale(1.015); }
}
@keyframes bond-flow {
  to { stroke-dashoffset: -28; }
}

#alga-beads {
  animation: breath-alga var(--dur-breath) ease-in-out infinite;
  transform-origin: center;
}
#fungus-filaments {
  animation: breath-fungus var(--dur-breath) ease-in-out infinite 1.3s;
  transform-origin: center;
}
#bond-arc {
  animation: bond-flow 3s linear infinite;
}

@media (prefers-reduced-motion: reduce) {
  #alga-beads, #fungus-filaments, #bond-arc {
    animation: none;
  }
}

/* Organism interactivity — tap is the action */
.organism {
  cursor: pointer;
  transition: filter 250ms var(--ease-calm);
}
.organism:focus-visible {
  outline: none;
}
.organism:focus-visible .halo {
  fill: rgba(255,255,255,0.06);
}
.organism:hover .halo {
  fill: rgba(255,255,255,0.04);
}

/* Tap response: anticipation → impact → settle, per Swink/Pichlmair */
.organism.tapped {
  animation: tap-pulse var(--dur-settle) var(--ease-organic);
}
@keyframes tap-pulse {
  0%   { transform: scale(1.00); }
  10%  { transform: scale(0.96); }   /* anticipation */
  25%  { transform: scale(1.08); }   /* impact */
  100% { transform: scale(1.00); }   /* spring settle */
}
@media (prefers-reduced-motion: reduce) {
  .organism.tapped {
    animation: none;
    filter: brightness(1.2);
  }
}

/* Concrete icons under each organism — universal-readable (☼ sun for alga, 💧 droplet for fungus) */
.org-glyph {
  font-size: 24px;
  fill: var(--ink-mute);
  pointer-events: none;
  user-select: none;
}

/* Beads (alga) and filaments (fungus) styled in JS where possible; defaults here */
.bead {
  fill: var(--alga);
  filter: drop-shadow(0 0 2px rgba(86,180,233,0.4));
}
.bead.dim { fill: var(--alga-low); filter: none; }

.filament {
  stroke: var(--fungus);
  fill: none;
  stroke-width: 3;
  stroke-linecap: round;
  filter: drop-shadow(0 0 1px rgba(230,159,0,0.4));
}
.filament.dim { stroke: var(--fungus-low); filter: none; }

/* Crisis state (vitality < 20%): rim warning, no blink */
.organism.crit .halo { fill: rgba(213,94,0,0.10); }

/* === Prompt: ambient state text === */
.prompt {
  margin: 0;
  text-align: center;
  font-size: 1rem;
  color: var(--ink-mute);
  min-height: 1.5em;
  font-style: italic;
  letter-spacing: 0.01em;
}

/* === Action surface — secondary only (primary is tap-organism) === */
.more {
  background: transparent;
}
.more summary {
  text-align: center;
  color: var(--ink-mute);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0.5rem;
  list-style: none;
}
.more summary::-webkit-details-marker { display: none; }
.more summary::after { content: " ▾"; }
.more[open] summary::after { content: " ▴"; }

.actions {
  display: grid;
  gap: 0.5rem;
  padding: 0.5rem 0;
}

.action {
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--ink);
  padding: 0.7rem 1rem;
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  border-radius: 6px;
  text-align: left;
  transition: background 200ms var(--ease-calm), transform 100ms;
}
.action:hover { background: var(--bg-mid); border-color: var(--bond); }
.action:active { transform: translateY(1px); }
.action:disabled { opacity: 0.5; cursor: not-allowed; }
.action.ghost { background: transparent; border-color: var(--line); }
.action.compact { padding: 0.5rem 0.9rem; text-align: center; }

/* === Meta strip — minimized lineage chrome === */
.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  justify-content: center;
  font-size: 0.85rem;
  color: var(--ink-mute);
  padding: 0.5rem 0.25rem;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.meta strong { color: var(--ink); font-variant-numeric: tabular-nums; font-weight: 500; }
.meta .health strong { color: var(--bond); }

/* === Cards (collapsible secondary panels) === */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.75rem 1rem;
  font-size: 0.9rem;
}
.card summary {
  cursor: pointer;
  font-weight: 500;
  list-style: none;
  user-select: none;
}
.card summary::-webkit-details-marker { display: none; }
.card summary::before { content: "› "; color: var(--ink-mute); transition: transform 200ms; }
.card[open] summary::before { content: "⌄ "; }

/* Pair connection */
.hidden { display: none !important; }

.conn-block {
  display: grid;
  gap: 0.5rem;
  padding: 0.5rem 0 0;
}
.conn-block p { margin: 0.25rem 0; }
.join-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.5rem;
}
#join-code {
  font: inherit;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--bg-deep);
  color: var(--ink);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-family: var(--font-mono);
}
#join-code:focus {
  outline: none;
  border-color: var(--bond);
  box-shadow: 0 0 0 2px rgba(0,158,115,0.25);
}

.hint { color: var(--ink-mute); font-size: 0.85rem; }
.hint.danger { color: var(--warn); }

/* Recent event log */
#log ul {
  margin: 0.5rem 0 0;
  padding: 0;
  list-style: none;
  font-size: 0.9rem;
}
#log li {
  padding: 0.4rem 0;
  border-top: 1px dotted var(--line);
  color: var(--ink-mute);
}
#log li:first-child {
  border-top: none;
  color: var(--ink);
}

/* Footer */
footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.75rem;
  color: var(--ink-mute);
  padding: 1rem 0.25rem 0;
}
.version { font-family: var(--font-mono); opacity: 0.6; }

.link {
  background: none;
  border: none;
  color: var(--bond);
  text-decoration: underline;
  cursor: pointer;
  font: inherit;
  font-size: 0.85rem;
  padding: 0;
}
.link.danger { color: var(--danger); }

code {
  background: var(--bg-deep);
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  font-family: var(--font-mono);
  font-size: 0.85em;
}

/* Micro-survey overlay (cycle 11) */
.micro-survey {
  position: fixed;
  inset: 0;
  background: rgba(26, 31, 28, 0.78);
  backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 50;
  animation: fade-in 300ms var(--ease-calm);
}
@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.micro-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1.5rem 1.25rem;
  max-width: 360px;
  width: calc(100% - 2rem);
  text-align: center;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}
.micro-q {
  margin: 0 0 1rem;
  font-size: 1rem;
  color: var(--ink);
}
.micro-scale {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.4rem;
  margin: 0.5rem 0;
}
.micro-btn {
  background: var(--bg-deep);
  border: 1px solid var(--line);
  color: var(--ink);
  padding: 0.6rem 0;
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  border-radius: 4px;
  transition: background 200ms, border-color 200ms;
}
.micro-btn:hover { background: var(--bond); border-color: var(--bond); color: var(--bg-deep); }
.micro-btn:active { transform: translateY(1px); }
.micro-hint {
  margin: 0.75rem 0 0.5rem;
  font-size: 0.8rem;
  color: var(--ink-mute);
}
.micro-skip {
  margin-top: 0.5rem;
  font-size: 0.85rem;
}
@media (prefers-reduced-motion: reduce) {
  .micro-survey { animation: none; }
}

@media (max-width: 480px) {
  body { padding: 0.5rem 0.5rem 3rem; }
  .meta { gap: 0.5rem 0.9rem; font-size: 0.8rem; }
  .micro-scale { gap: 0.25rem; }
  .micro-btn { padding: 0.5rem 0; font-size: 0.85rem; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   Tidekin v0.1 — cycle-13 mutation overlay (closes V2 figure-ground).
   Adds: substrate (water + sand), day/night cycle, bioluminescence, ambient shimmer.
   Preserves: Okabe-Ito organism palette, spring easing, reduced-motion semantics.
   ═══════════════════════════════════════════════════════════════════════════ */

:root {
  /* Tide-pool palette — varies by data-time-of-day */
  --water-top:    #2a7a8a;
  --water-mid:    #1a5560;
  --water-deep:   #0a3a3e;
  --sand:         #c9b27a;
  --sand-shadow:  #95825a;
  --light-tint:   transparent;            /* drives palette mood */
  --bio-glow:     #7ee8c8;                /* bioluminescence color */
  --bio-strength: 0;                      /* 0..1, set by JS from pair-health */
  --shimmer-alpha: 0.08;                  /* water-shimmer intensity */
  --star-alpha:   0;                      /* stars fade in only at night */
}

body[data-time-of-day="dawn"] {
  --water-top: #d68a7e;  --water-mid: #6b4660; --water-deep: #2a3a52;
  --sand: #e0bd92;       --light-tint: rgba(244,160,140,0.12);
  --shimmer-alpha: 0.10;
}
body[data-time-of-day="day"] {
  --water-top: #56b4e9;  --water-mid: #1e6f8a; --water-deep: #0a3a4e;
  --sand: #c9b27a;       --light-tint: rgba(255,255,255,0.05);
  --shimmer-alpha: 0.12;
}
body[data-time-of-day="sunset"] {
  --water-top: #e69f5a;  --water-mid: #6b4e8e; --water-deep: #2a1a40;
  --sand: #b89860;       --light-tint: rgba(230,159,80,0.14);
  --shimmer-alpha: 0.09;
}
body[data-time-of-day="night"] {
  --water-top: #1a3050;  --water-mid: #0a1530; --water-deep: #050a1c;
  --sand: #4a4030;       --light-tint: rgba(40,30,80,0.18);
  --shimmer-alpha: 0.04;
  --star-alpha: 0.7;
}

/* Override Lichen's void background — substrate takes over. */
body {
  background: var(--water-deep);
  transition: background 8s var(--ease-calm);
}

/* ── Substrate stack — full-viewport behind everything */
.substrate {
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  pointer-events: none;
}
.substrate .water {
  position: absolute;
  inset: 0 0 18% 0;
  background: linear-gradient(
    to bottom,
    var(--water-top) 0%,
    var(--water-mid) 55%,
    var(--water-deep) 100%
  );
  transition: background 8s var(--ease-calm);
}
.substrate .water::after {
  /* Light tint — tinted glaze layered over water */
  content: "";
  position: absolute;
  inset: 0;
  background: var(--light-tint);
  transition: background 8s var(--ease-calm);
  pointer-events: none;
}
.substrate .shimmer {
  /* Slow conic-gradient sweep across water surface — caustics-like ambient motion */
  position: absolute;
  inset: -10% -10% 18% -10%;
  background: conic-gradient(
    from 0deg at 50% 30%,
    transparent 0deg,
    rgba(255,255,255,var(--shimmer-alpha)) 60deg,
    transparent 120deg,
    rgba(255,255,255,calc(var(--shimmer-alpha) * 0.6)) 200deg,
    transparent 280deg,
    rgba(255,255,255,calc(var(--shimmer-alpha) * 0.4)) 340deg,
    transparent 360deg
  );
  filter: blur(40px);
  animation: tk-shimmer-rotate 90s linear infinite;
  transition: background 8s var(--ease-calm);
}
.substrate .sand {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 18%;
  background:
    radial-gradient(ellipse at 30% 20%, var(--sand-shadow) 0%, transparent 40%),
    radial-gradient(ellipse at 70% 60%, var(--sand-shadow) 0%, transparent 35%),
    linear-gradient(to bottom, var(--sand) 0%, var(--sand-shadow) 100%);
  transition: background 8s var(--ease-calm);
  /* Subtle granular noise via SVG data-uri turbulence */
  --noise: url("data:image/svg+xml,%3Csvg viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' seed='3'/%3E%3CfeColorMatrix values='0 0 0 0 0.3  0 0 0 0 0.25  0 0 0 0 0.15  0 0 0 0.35 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.55'/%3E%3C/svg%3E");
  background-image: var(--noise),
    radial-gradient(ellipse at 30% 20%, var(--sand-shadow) 0%, transparent 40%),
    radial-gradient(ellipse at 70% 60%, var(--sand-shadow) 0%, transparent 35%),
    linear-gradient(to bottom, var(--sand) 0%, var(--sand-shadow) 100%);
  background-size: 240px 240px, auto, auto, auto;
}
.substrate .stars {
  /* Twinkling night stars — only visible when --star-alpha > 0 */
  position: absolute;
  inset: 0 0 18% 0;
  background-image:
    radial-gradient(1.5px 1.5px at 12% 18%, rgba(255,255,255,0.9) 100%, transparent),
    radial-gradient(1px 1px at 24% 8%, rgba(255,255,255,0.7) 100%, transparent),
    radial-gradient(1.2px 1.2px at 38% 22%, rgba(255,255,255,0.85) 100%, transparent),
    radial-gradient(1px 1px at 52% 12%, rgba(255,255,255,0.6) 100%, transparent),
    radial-gradient(1.4px 1.4px at 67% 28%, rgba(255,255,255,0.95) 100%, transparent),
    radial-gradient(1px 1px at 78% 9%, rgba(255,255,255,0.7) 100%, transparent),
    radial-gradient(1.3px 1.3px at 91% 19%, rgba(255,255,255,0.85) 100%, transparent),
    radial-gradient(1px 1px at 6% 34%, rgba(255,255,255,0.55) 100%, transparent),
    radial-gradient(1.2px 1.2px at 45% 38%, rgba(255,255,255,0.7) 100%, transparent),
    radial-gradient(1px 1px at 84% 41%, rgba(255,255,255,0.6) 100%, transparent);
  opacity: var(--star-alpha);
  transition: opacity 6s var(--ease-calm);
  animation: tk-twinkle 8s ease-in-out infinite;
}

/* ── Bioluminescence — drop-shadow halo on organisms; fades in at night with health */
.organism {
  filter: drop-shadow(0 0 calc(18px * var(--bio-strength)) var(--bio-glow))
          drop-shadow(0 0 calc(6px * var(--bio-strength)) var(--bio-glow));
  transition: filter 1.5s var(--ease-calm);
}
.organism .halo {
  /* Slight breathing — soft organic life signal */
  transform-origin: center;
  animation: tk-breathe var(--dur-breath) ease-in-out infinite;
}
.organism.fungus .halo { animation-delay: -2s; }  /* offset breathing */

/* ── Tide-pool meta indicator */
.meta .tod {
  font-size: 0.75rem;
  color: var(--ink-mute);
  font-variant: small-caps;
  letter-spacing: 0.05em;
}

/* Stage rendered above substrate */
#app { position: relative; z-index: 1; }

/* ── Animations */
@keyframes tk-shimmer-rotate {
  to { transform: rotate(360deg); }
}
@keyframes tk-breathe {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.04); }
}
@keyframes tk-twinkle {
  0%, 100% { filter: brightness(1); }
  50%      { filter: brightness(1.4); }
}

@media (prefers-reduced-motion: reduce) {
  .substrate .shimmer,
  .substrate .stars,
  .organism .halo {
    animation: none;
  }
  .organism { transition: filter 200ms var(--ease-calm); }
}
