/* ═══════════════════════════════════════════════
   ORGANISM — STYLESHEET
   Aesthetic: Clinical specimen viewer.
   Light mode. Warm whites. Something is wrong.
   ═══════════════════════════════════════════════ */

/* ── CUSTOM PROPERTIES ───────────────────────── */
:root {
  /* Base palette */
  --parchment:     #f5f2ed;
  --parchment-mid: #ede9e2;
  --parchment-deep:#e0dbd2;
  --ink:           #1a1814;
  --ink-faint:     #6b665e;
  --ink-ghost:     #a09a90;
  --rule:          #d4cfc7;

  /* Life stage accent colours */
  --accent-young:    #4a7c59;  /* forest green */
  --accent-mature:   #5a6fa0;  /* steel blue   */
  --accent-unstable: #a05a2c;  /* rust amber   */
  --accent-dying:    #888080;  /* ash grey     */

  /* Currently active accent (JS updates via inline style) */
  --accent: var(--accent-young);

  /* Vitals bar colours */
  --health-color:  #4a7c59;
  --mood-color:    #5a6fa0;
  --entropy-color: #a05a2c;

  /* Layout */
  --top-h:    48px;
  --side-w:   200px;
  --footer-h: 100px;

  /* Typography */
  --font-serif: 'Cormorant Garamond', Georgia, serif;
  --font-mono:  'JetBrains Mono', 'Courier New', monospace;
}

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

::selection {
  background: var(--accent);
  color: var(--parchment);
}

/* ── BASE ────────────────────────────────────── */
html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

body {
  background: var(--parchment);
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 300;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: var(--top-h) 1fr var(--footer-h);
  grid-template-columns: var(--side-w) 1fr;
  grid-template-areas:
    "top    top"
    "vitals stage"
    "foot   foot";
  overflow: hidden;
  transition: background 1.5s ease;
}

/* Body class modifications by life stage */
body.stage-unstable { background: #f0ece4; }
body.stage-dying    { background: #eceae8; }

/* ── TOP BAR ─────────────────────────────────── */
.top-bar {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  border-bottom: 1px solid var(--rule);
  background: var(--parchment);
  position: relative;
  z-index: 10;
}

.site-id {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.12em;
  color: var(--ink-faint);
  text-transform: lowercase;
}

#stage-label {
  color: var(--accent);
  transition: color 1s ease;
}

.uptime {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--ink-ghost);
  letter-spacing: 0.08em;
}

/* ── VITALS SIDEBAR ──────────────────────────── */
.vitals {
  grid-area: vitals;
  padding: 32px 20px;
  border-right: 1px solid var(--rule);
  display: flex;
  flex-direction: column;
  gap: 22px;
  background: var(--parchment);
}

.vital-row {
  display: grid;
  grid-template-columns: 60px 1fr 32px;
  align-items: center;
  gap: 8px;
}

.vital-row.solo {
  grid-template-columns: 60px auto;
}

.vital-key {
  font-size: 9.5px;
  letter-spacing: 0.14em;
  color: var(--ink-faint);
  text-transform: lowercase;
}

.vital-bar-wrap {
  height: 1px;
  background: var(--rule);
  position: relative;
  overflow: visible;
}

.vital-bar {
  position: absolute;
  left: 0;
  top: 0;
  height: 1px;
  background: var(--health-color);
  transition: width 1.5s ease, background 1.5s ease;
  width: 0%;
}

.vital-bar::after {
  content: '';
  position: absolute;
  right: -1px;
  top: -2px;
  width: 3px;
  height: 5px;
  background: inherit;
  border-radius: 1px;
}

#bar-mood  { background: var(--mood-color); }
.entropy-bar { background: var(--entropy-color) !important; }

.vital-val {
  font-size: 9px;
  color: var(--ink-ghost);
  letter-spacing: 0.06em;
  text-align: right;
}

.vital-val.solo {
  grid-column: 2 / -1;
  text-align: left;
  font-size: 10px;
  color: var(--ink-faint);
}

/* Vitals separator line */
.vitals::after {
  content: '';
  display: block;
  flex: 1;
  border-top: 1px solid var(--parchment-deep);
  margin-top: auto;
}

/* ── MAIN ORGANISM STAGE ─────────────────────── */
.organism-stage {
  grid-area: stage;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 40px 60px;
}

/* Grain texture overlay */
.organism-stage::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)' opacity='0.03'/%3E%3C/svg%3E");
  pointer-events: none;
  opacity: 0.4;
}

/* ── PULSE RING ──────────────────────────────── */
.pulse-ring {
  position: absolute;
  width: 180px;
  height: 180px;
  border-radius: 50%;
  border: 1px solid var(--accent);
  opacity: 0.2;
  animation: pulse-expand 4s ease-in-out infinite;
  pointer-events: none;
  transition: border-color 1.5s ease;
}

.pulse-core {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.6;
  animation: core-breathe 4s ease-in-out infinite;
  pointer-events: none;
  transition: background 1.5s ease;
}

@keyframes pulse-expand {
  0%, 100% { transform: scale(1); opacity: 0.2; }
  50%       { transform: scale(1.25); opacity: 0.06; }
}

@keyframes core-breathe {
  0%, 100% { transform: scale(1); opacity: 0.6; }
  50%       { transform: scale(1.5); opacity: 0.3; }
}

/* Stage: unstable — ring jitters */
body.stage-unstable .pulse-ring {
  animation: pulse-expand 1.8s ease-in-out infinite, glitch-ring 0.3s steps(2) infinite;
}

@keyframes glitch-ring {
  0%   { clip-path: inset(0 0 0 0); }
  33%  { clip-path: inset(10% 0 60% 0); transform: scale(1.1) translate(-2px, 1px); }
  66%  { clip-path: inset(50% 0 10% 0); transform: scale(0.95) translate(2px, -1px); }
  100% { clip-path: inset(0 0 0 0); }
}

/* Stage: dying — ring fades */
body.stage-dying .pulse-ring {
  animation: pulse-expand 8s ease-in-out infinite;
  opacity: 0.06;
}
body.stage-dying .pulse-core {
  animation: core-breathe 8s ease-in-out infinite;
}

/* ── THOUGHT DISPLAY ─────────────────────────── */
.thought-display {
  position: relative;
  z-index: 2;
  text-align: center;
  max-width: 520px;
  margin-bottom: 36px;
}

.thought-text {
  font-family: var(--font-serif);
  font-size: 22px;
  font-weight: 300;
  line-height: 1.55;
  color: var(--ink);
  font-style: italic;
  letter-spacing: 0.02em;
  transition: opacity 0.8s ease;
}

.thought-text.fading { opacity: 0; }

/* Stage: dying — thought shrinks */
body.stage-dying .thought-text {
  font-size: 15px;
  color: var(--ink-faint);
}

/* ── RESPONSE LOG ─────────────────────────────── */
.response-log {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 560px;
  max-height: 200px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 0 4px;
  scrollbar-width: thin;
  scrollbar-color: var(--rule) transparent;
}

.log-entry {
  font-family: var(--font-mono);
  font-size: 10.5px;
  line-height: 1.6;
  color: var(--ink-faint);
  letter-spacing: 0.04em;
  padding: 8px 12px;
  border-left: 1px solid var(--rule);
  animation: entry-appear 0.5s ease;
  position: relative;
}

.log-entry.entry-user {
  color: var(--ink-ghost);
  border-left-color: transparent;
  font-size: 10px;
  text-align: right;
  padding-right: 0;
}

.log-entry.entry-user::before {
  content: '›';
  margin-right: 6px;
  color: var(--ink-ghost);
}

.log-entry.entry-organism {
  border-left-color: var(--accent);
  color: var(--ink-faint);
  font-family: var(--font-serif);
  font-size: 13px;
  font-weight: 300;
  font-style: italic;
}

.log-entry.entry-system {
  color: var(--ink-ghost);
  font-size: 9.5px;
  letter-spacing: 0.1em;
  border-left-color: var(--rule);
}

@keyframes entry-appear {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── RARE FLASH ──────────────────────────────── */
.rare-flash {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  max-width: 480px;
  width: 90%;
  font-family: var(--font-serif);
  font-size: 13px;
  font-style: italic;
  color: var(--ink);
  text-align: center;
  padding: 12px 20px;
  background: var(--parchment-mid);
  border: 1px solid var(--rule);
  letter-spacing: 0.03em;
  opacity: 0;
  pointer-events: none;
  z-index: 20;
  transition: opacity 0.6s ease;
}

.rare-flash.visible {
  opacity: 1;
}

/* ── INPUT AREA ──────────────────────────────── */
.input-area {
  grid-area: foot;
  border-top: 1px solid var(--rule);
  padding: 0 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--parchment);
  position: relative;
  z-index: 10;
}

.input-wrapper {
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 540px;
  gap: 12px;
  border-bottom: 1px solid var(--ink);
  padding-bottom: 4px;
  transition: border-color 0.3s ease;
}

.input-wrapper:focus-within {
  border-bottom-color: var(--accent);
}

.msg-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 300;
  color: var(--ink);
  letter-spacing: 0.06em;
  caret-color: var(--accent);
}

.msg-input::placeholder {
  color: var(--ink-ghost);
}

.send-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--accent);
  padding: 4px;
  display: flex;
  align-items: center;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.send-btn svg { width: 14px; height: 14px; }

.send-btn:hover { opacity: 0.7; transform: translateX(1px); }

.send-btn:disabled {
  color: var(--ink-ghost);
  cursor: default;
  transform: none;
}

.input-hint {
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--ink-ghost);
  letter-spacing: 0.12em;
  text-align: center;
  transition: opacity 0.5s ease;
}

/* Stage: dying — hint changes */
body.stage-dying .input-hint { opacity: 0.4; }

/* ── DYING OVERLAY ───────────────────────────── */
.dying-overlay {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
  opacity: 0;
  transition: opacity 3s ease;
  background: radial-gradient(ellipse at center, transparent 60%, rgba(26,24,20,0.08) 100%);
}

body.stage-dying .dying-overlay {
  opacity: 1;
}

/* ── GLITCH TEXT EFFECT (applied via JS) ─────── */
.glitch {
  position: relative;
  display: inline-block;
}

.glitch::before,
.glitch::after {
  content: attr(data-text);
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.glitch::before {
  color: #a05a2c;
  animation: glitch-1 3s steps(2) infinite;
  clip-path: inset(40% 0 40% 0);
}

.glitch::after {
  color: #5a6fa0;
  animation: glitch-2 2.7s steps(2) infinite;
  clip-path: inset(60% 0 0 0);
}

@keyframes glitch-1 {
  0%, 90%, 100% { transform: translate(0); }
  92%           { transform: translate(-2px, 1px); }
  95%           { transform: translate(2px, -1px); }
}

@keyframes glitch-2 {
  0%, 85%, 100% { transform: translate(0); }
  87%           { transform: translate(2px, -1px); }
  92%           { transform: translate(-1px, 2px); }
}

/* ── SCROLLBAR ───────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--rule); border-radius: 2px; }

/* ── RESPONSIVE ──────────────────────────────── */
@media (max-width: 640px) {
  body {
    grid-template-columns: 1fr;
    grid-template-rows: var(--top-h) auto 1fr var(--footer-h);
    grid-template-areas:
      "top"
      "vitals"
      "stage"
      "foot";
  }

  .vitals {
    border-right: none;
    border-bottom: 1px solid var(--rule);
    flex-direction: row;
    flex-wrap: wrap;
    padding: 16px 20px;
    gap: 12px;
  }

  .vital-row {
    grid-template-columns: 50px 80px 28px;
    gap: 6px;
  }

  .organism-stage { padding: 32px 24px; }
  .input-area { padding: 0 24px; }
}
