/* =========================================================================
   LIFE OS — v2data.jsx   ·   THE CURIOSITY EXPANSION
   Loads AFTER data.jsx. Mutates window.LifeData in place: splices a roaming
   Curiosity creature + hunch-only Mutations into LifeData.E (so the merge /
   dex / sprite systems pick them up for free), then hangs all the new V2
   content tables on window.LifeData.V2.

   The new resource is WONDER (◇) — the only currency you earn purely by
   poking at things. Gus calls it "Why?", "hunches", "sparks" interchangeably;
   the joke is that curiosity has many names.
   ========================================================================= */
(function () {
  const D = window.LifeData;
  const E = D.E;

  /* ----------------------------------------------------------- CURIOSITY
     The sixth feeling that refuses the org chart. home:'roam' means no
     department box wants it — it darts across the whole stage instead.
     The ONLY creature that generates Wonder, and its output scales with how
     much of the world you've explored. */
  E.curiosity = {
    name: 'Curiosity', tier: 0, body: 'comet', home: 'roam', family: 'wonder',
    c: ['#6FE6E0', '#2BA8C8'], raw: 'Wonder', icon: '🛰', special: 'curiosity',
    blurb: 'Not on any org chart. Roams the halls opening doors marked DO NOT OPEN. The only employee who generates Wonder \u2014 and the more of the world you\u2019ve poked at, the faster it hums.',
    out: { wonder: 0.42 }, need: { fed: .25, rested: .2, seen: .55 },
    careLine: 'Curiosity found a door. It would VERY much like to know what is behind the door. Now. Please.'
  };

  /* ----------------------------------------------------------- MUTATIONS
     Tier 4. You cannot hatch or recipe these into existence. They only
     emerge from a HUNCH — spending Wonder to combine two feelings that
     officially "have no reaction." Curiosity, rewarded. */
  const MUTS = {
    whimsy: {
      name: 'Whimsy', tier: 4, body: 'comet', home: 'roam', family: 'mutant', mutant: true,
      c: ['#FFD1F0', '#B06FE6'], raw: 'Delight-at-Nothing', icon: '🎈',
      blurb: 'Nobody ordered this feeling. It showed up anyway, wearing a tiny hat. A hunch made it real.',
      out: { energy: 2.2, wonder: 0.18 }, need: { fed: .4, rested: .3, seen: .3 }
    },
    spite: {
      name: 'Spite', tier: 4, body: 'jitter', home: 'roam', family: 'mutant', mutant: true,
      c: ['#9BE85A', '#3E7A1E'], raw: 'Pure Cussedness', icon: '😤',
      blurb: 'Will finish the project out of sheer refusal to let it win. Surprisingly load-bearing. A hunch dredged it up.',
      out: { energy: 3.4 }, need: { fed: .3, rested: .5, seen: .2 }
    },
    awe: {
      name: 'Awe', tier: 4, body: 'orb', home: 'roam', family: 'mutant', mutant: true,
      c: ['#8FB8FF', '#2E3FA0'], raw: 'The Sublime', icon: '🌌',
      blurb: 'The feeling of being very small in a way that doesn\u2019t hurt. You can\u2019t schedule it. A hunch caught it mid-flight.',
      out: { clarity: 1.6, wonder: 0.3 }, need: { fed: .2, rested: .3, seen: .5 }
    },
    ennui: {
      name: 'Ennui', tier: 4, body: 'void', home: 'roam', family: 'mutant', mutant: true,
      c: ['#A0A6C0', '#3A3550'], raw: 'Elegant Boredom', icon: '🥱',
      blurb: 'Too sophisticated to be mere boredom. Sighs in French. A hunch is the only thing interesting enough to summon it.',
      out: { clarity: 1.1 }, need: { fed: .3, rested: .5, seen: .2 }
    },
    deja_vu: {
      name: 'D\u00e9j\u00e0 Vu', tier: 4, body: 'mirror', home: 'roam', family: 'mutant', mutant: true,
      c: ['#C0B0E8', '#6E5A99'], raw: 'The Echo', icon: '🔁',
      blurb: 'Have you read this blurb before? It\u2019s sure you have. It\u2019s usually wrong. A hunch keeps producing it.',
      out: { clarity: 0.8, wonder: 0.22 }, need: { fed: .3, rested: .3, seen: .4 }
    },
    schadenfreude: {
      name: 'Schadenfreude', tier: 4, body: 'spark', home: 'roam', family: 'mutant', mutant: true,
      c: ['#FF9E5E', '#C43E6E'], raw: 'Guilty Glee', icon: '😈',
      blurb: 'A feeling you\u2019d never admit to in the team meeting. Off the books. A hunch let it out.',
      out: { energy: 4.0 }, need: { fed: .4, rested: .3, seen: .3 }
    },
  };
  Object.assign(E, MUTS);
  const MUT_KEYS = Object.keys(MUTS);

  // keep dex order in sync now that E has grown
  D.order = Object.keys(E);

  /* ----------------------------------------------------------- HUNCH OUTCOMES
     Weighted reward table for spending Wonder on an uncertain merge. The
     engine rolls one of these. mutation/hint are rarer; small payouts and the
     occasional comic fizzle keep it honest. */
  const HUNCH = {
    cost: 14,                 // Wonder per hunch
    costGrowth: 1.12,         // each played hunch nudges the price up a little
    table: [
      { type: 'mutation', w: 16, line: 'Something that was NOT on the org chart just clocked in.' },
      { type: 'hint',     w: 16, line: 'A recipe card half-slides out of a drawer. You catch a glimpse.' },
      { type: 'wonder',   w: 20, line: 'The hunch pays itself back, with curiosity-interest.', min: 24, max: 60 },
      { type: 'clarity',  w: 20, line: 'Two feelings refused to merge \u2014 but the friction threw off Clarity.', min: 18, max: 48 },
      { type: 'lore',     w: 18, line: 'A fragment surfaces. Gus pretends he wasn\u2019t listening.' },
      { type: 'fizzle',   w: 10, line: 'Nothing. A little puff of nothing. Curiosity is its own reward, allegedly.' },
    ],
    // funny "fizzle" consolation lines (Gus)
    fizzles: [
      '\u201cI had a feeling about that. The feeling was wrong. It happens.\u201d',
      '\u201cInconclusive. Science! Mostly science. Some vibes.\u201d',
      '\u201cThe two of them looked at each other and decided, respectfully, no.\u201d',
      '\u201cThat\u2019s a paddlin\u2019. I mean \u2014 that\u2019s a learning experience.\u201d',
    ],
  };

  /* ----------------------------------------------------------- QUESTIONS
     Gus posts these to a board. Each is a tiny self-tracking investigation
     (same shape as the Act goals: p(state) -> [cur,max]). Some are FREE to
     pursue; "deep" ones cost Wonder to even unlock the asking. Completing one
     pays a reward + a lore fragment.
       gate(state)  -> is this question visible/available yet?
       p(state)     -> [current, max] progress
       reward       -> {wonder?,clarity?,energy?,hint?:true,mutation?:true}
   */
  const has = (s, k) => s.creatures.some(c => c.key === k && !c.shadow);
  const dexCount = (s, t) => Object.keys(s.dex || {}).filter(k => E[k] && E[k].tier === t).length;
  const QUESTIONS = [
    { id: 'q_refine', cat: 'The Work',
      q: 'What is Sadness actually refining down there?',
      hint: 'Keep Sadness on staff and let it process.',
      gate: s => has(s, 'sadness'),
      p: s => [Math.min(40, Math.floor(s.stats.processed)), 40],
      reward: { wonder: 20, lore: 'sadness_refine' } },
    { id: 'q_firsthatch', cat: 'About You',
      q: 'Why do you always reach for the same feeling first?',
      hint: 'Hatch all five base feelings \u2014 give the others a turn.',
      gate: () => true,
      p: s => [Math.min(5, [...new Set(s.creatures.filter(c => E[c.key] && E[c.key].tier === 0 && !E[c.key].special).map(c => c.key))].length), 5],
      reward: { wonder: 26, lore: 'first_reach' } },
    { id: 'q_shadowlight', cat: 'The Building',
      q: 'What happens when something neglected is finally seen?',
      hint: 'Heal one Shadow back to the light.',
      gate: () => true,
      p: s => [Math.min(1, s.stats.healed || 0), 1],
      reward: { wonder: 30, clarity: 20, lore: 'shadow_seen' } },
    { id: 'q_vending', cat: 'Absurd', deep: true, askCost: 12,
      q: 'What\u2019s in the vending machine of the soul?',
      hint: 'Play a hunch. Any hunch. Just poke at something.',
      gate: s => has(s, 'curiosity'),
      p: s => [Math.min(2, s.stats.hunches || 0), 2],
      reward: { wonder: 28, hint: true, lore: 'vending' } },
    { id: 'q_door', cat: 'The Building', deep: true, askCost: 16,
      q: 'There\u2019s a door in the basement nobody will talk about. Why?',
      hint: 'Spend Wonder to open the Unmarked Door.',
      gate: s => has(s, 'curiosity'),
      p: s => [Math.min(1, (s.rooms && s.rooms.unmarked_door) ? 1 : 0), 1],
      reward: { wonder: 40, lore: 'the_door' } },
    { id: 'q_gus', cat: 'About Gus', deep: true, askCost: 20,
      q: 'Who was Gus before the brain incorporated him?',
      hint: 'Get curious about Gus \u2014 open his office.',
      gate: s => has(s, 'curiosity'),
      p: s => [Math.min(1, (s.rooms && s.rooms.gus_office) ? 1 : 0), 1],
      reward: { wonder: 50, lore: 'gus_origin' } },
    { id: 'q_complex', cat: 'The Work', deep: true, askCost: 14,
      q: 'How deep does a feeling go before it becomes a different feeling?',
      hint: 'Discover a Tier-II complex state.',
      gate: s => Object.keys(s.dex || {}).some(k => E[k] && E[k].tier === 1),
      p: s => [Math.min(1, dexCount(s, 2)), 1],
      reward: { wonder: 36, clarity: 40, lore: 'how_deep' } },
    { id: 'q_mutant', cat: 'Absurd', deep: true, askCost: 18,
      q: 'Are there feelings that aren\u2019t in any official handbook?',
      hint: 'Coax a Mutation out of a hunch.',
      gate: s => (s.stats.hunches || 0) >= 1,
      p: s => [Math.min(1, (s.stats.mutations || 0)), 1],
      reward: { wonder: 44, mutation: false, lore: 'off_book' } },
  ];

  /* ----------------------------------------------------------- ROOMS
     Spaces that aren't on the org chart. Spend Wonder to open each. Opening
     reveals lore + grants a small permanent effect. Pillar 4. */
  const ROOMS = [
    { id: 'unmarked_door', name: 'The Unmarked Door', icon: '🚪', tag: 'basement',
      blurb: 'A door in the basement, painted over so many times the handle barely turns. Behind it: the feelings you stopped feeling on purpose.',
      cost: 30,
      openLine: 'You opened it. Cold air, dust, and a dozen feelings you filed under \u201clater\u201d still waiting in the dark. They look up. They remember you.',
      effect: '+15% Wonder generation \u2014 the forgotten feelings hum when noticed.',
      effectKey: 'wonderBoost',
      lore: ['the_door', 'forgotten_1', 'forgotten_2'] },
    { id: 'archive', name: 'The Archive', icon: '🗄', tag: 'records',
      blurb: 'Floor-to-ceiling drawers. Every feeling you\u2019ve ever processed is filed here, cross-referenced, dated. You can revisit any of them.',
      cost: 70,
      openLine: 'The Archive opens with a sigh of old paper. Every discovery you\u2019ve made is here, waiting to be re-read. Memory, it turns out, is a room you can walk back into.',
      effect: 'Re-read every discovery. +1 Clarity/s per 4 emotions catalogued.',
      effectKey: 'archiveClarity',
      lore: ['archive_1', 'archive_2'] },
    { id: 'gus_office', name: 'Gus\u2019s Office', icon: '🗝', tag: 'private',
      blurb: 'You\u2019ve never been invited in. The nameplate still says GUSTO under a strip of tape. There are photos on the desk you weren\u2019t supposed to see.',
      cost: 120,
      openLine: 'Gus doesn\u2019t stop you. He just gets quiet. \u201cYeah. This is where I keep the before. Back when I was yours and nobody else\u2019s. Look around. Carefully.\u201d',
      effect: 'Unlocks Gus\u2019s story, told in fragments. +20% all output \u2014 it helps, being known.',
      effectKey: 'gusBond',
      lore: ['gus_origin', 'gus_1', 'gus_2', 'gus_3'] },
  ];

  /* ----------------------------------------------------------- LORE FRAGMENTS
     Unlocked by questions, rooms and hunches. Collected in the Archive. */
  const LORE = {
    sadness_refine: { t: 'What Sadness Refines', x: 'It\u2019s not making you sad. It\u2019s taking the raw, unsorted ache and pressing it, slowly, into something you can actually hold: Clarity. The most productive employee in the building works in the dark and never asks for credit.' },
    first_reach: { t: 'The One You Reach For', x: 'There\u2019s always a first feeling \u2014 the one your hand finds before you think. It\u2019s not the truest one. It\u2019s just the most rehearsed. The others were always allowed to clock in too.' },
    shadow_seen: { t: 'Being Seen', x: 'A Shadow isn\u2019t a broken feeling. It\u2019s a feeling that waited too long to be noticed and learned to stop asking. Attention is the whole cure. That\u2019s the entire job, really.' },
    vending: { t: 'The Vending Machine of the Soul', x: 'Row A: nostalgia, slightly expired. Row B: a courage you forgot you bought. The machine takes Wonder, never exact change, and occasionally drops two things at once. Nobody knows who restocks it. (It\u2019s Gus. It\u2019s always been Gus.)' },
    the_door: { t: 'The Door Downstairs', x: 'Every building you live in has one. The room where you put the feelings that were too much on the day they arrived. They don\u2019t rot down there. They wait. Curiosity is the only one brave enough to knock.' },
    forgotten_1: { t: 'Filed Under Later', x: 'A grief you postponed because there was a deadline. It understood. It\u2019s still understanding. It would like, eventually, to be felt.' },
    forgotten_2: { t: 'The Coat That Still Fits', x: 'Down here, an old joy you decided you\u2019d outgrown. Try it on. Funny \u2014 it still fits. It always did.' },
    archive_1: { t: 'Cross-Referenced', x: 'Turns out every feeling is filed next to the one it became. Joy sits beside Nostalgia sits beside Acceptance. Read the drawers in order and you\u2019re reading your own becoming.' },
    archive_2: { t: 'Dated and Kept', x: 'Nothing you felt was wasted. It\u2019s all here, dated, kept. Even the feelings you\u2019re embarrassed about have a drawer. Especially those.' },
    gus_origin: { t: 'Used to be Gusto', x: 'He was an imaginary friend. Bright orange, far too loud, invented by a six-year-old who needed someone on their side. \u201cGusto,\u201d because that kid did everything with too much of it. Then the kid grew up, the brain incorporated, HR shortened the name, and Gusto became Gus: Assistant Manager, Feelings Division. Same guy. Quieter hat.' },
    gus_1: { t: 'The Photos on the Desk', x: 'A drawing in crayon: a stick figure and a big orange blob holding hands under a sun with too many rays. He keeps it framed. He\u2019d deny it under oath.' },
    gus_2: { t: 'Why He Stayed', x: '\u201cMost imaginary friends get laid off around age nine. I filed a transfer instead. Somebody had to stick around and tell you the numbers going up isn\u2019t the point.\u201d' },
    gus_3: { t: 'The Pay Is Emotional', x: '\u201cYou asked once why I never left. Kid \u2014 I AM the part of you that never stopped rooting for you. There\u2019s no severance package for that. There\u2019s no leaving.\u201d' },
    off_book: { t: 'Off the Books', x: 'The handbook lists the feelings HR will admit to. The real roster is longer, weirder, and only Curiosity has clearance. Whimsy. Spite. Awe. Ennui. They were always working here. You just never asked.' },
    how_deep: { t: 'How Deep It Goes', x: 'Push a feeling far enough and it doesn\u2019t intensify \u2014 it transforms. Worry compounded becomes Dread becomes, eventually, something almost like peace. The spectrum was never a line. It was a spiral going down into something gentler.' },
  };

  /* ----------------------------------------------------------- MEMORIES ("Why?")
     Pillar 5. Each feeling, asked "why are you here?", peels back 2-3 layers
     of origin. Costs a little Wonder per layer. Turns stats into people. */
  const MEMORIES = {
    joy: ['Born the first time someone clapped for you and meant it.', 'Learned, later, that it\u2019s allowed to be quiet. That it doesn\u2019t have to perform to count.', 'Is secretly terrified of being the only one in the room. That\u2019s why it overschedules the fun.'],
    sadness: ['Arrived the day you understood that some things don\u2019t come back.', 'Discovered it was good at something: turning ache into understanding.', 'Just wants to be acknowledged. That\u2019s the whole ask. It was never trying to drown you.'],
    anger: ['Showed up the first time something unfair happened and nobody else said anything.', 'Got mistaken for the villain so often it almost believed it.', 'Is, underneath, the part of you that knows you\u2019re worth defending.'],
    fear: ['Came online the first time you realized you could lose something.', 'Ran nine thousand simulations so you\u2019d survive the one that mattered.', 'Is exhausted. It has been on watch your entire life. It would love a nap and a thank-you.'],
    disgust: ['Started as a toddler refusing the green thing. Became your taste, your standards, your no.', 'Learned the hard way that turned inward, it curdles into shame.', 'At its best, it\u2019s just discernment with strong opinions. It\u2019s keeping the cringe off you.'],
    curiosity: ['Hatched the moment you wondered instead of looked away.', 'Doesn\u2019t live anywhere because everywhere is interesting.', 'Is the part of you that still believes the next door might be the good one. It\u2019s usually right.'],
  };

  /* ----------------------------------------------------------- GUS · V2 VOICE */
  const GUS_V2 = {
    curiosityArrived: [
      'Huh. Something just\u2026 wandered in. No badge, no department, no respect for the org chart. It\u2019s called Curiosity, and apparently it only shows up once you start discovering things. It makes Wonder \u2014 the one resource you earn purely by poking at stuff you were told not to poke at. Don\u2019t feed it after midnight. Actually, do. See what happens. That\u2019s the whole bit.',
    ],
    hunchWin: [
      'See? SEE? You poked the thing and the thing poked back \u2014 in a good way this time.',
      '\u201cNo reaction,\u201d the handbook said. The handbook is a coward. Look what you found.',
    ],
    hunchMutation: [
      'That is not in any handbook. That is not on any payroll. You hunched a brand-new feeling into existence. I\u2019m\u2026 actually a little proud. Don\u2019t make it weird.',
    ],
    hunchFizzle: [
      'Nothing. A puff of nothing. But you tried the weird thing, and trying the weird thing is the entire personality of this place. Go again.',
    ],
    questionAsked: [
      'Good question. Genuinely. I\u2019ve pinned it to the board. Go find out.',
    ],
    questionAnswered: [
      'You answered it. The board\u2019s lighter and so, somehow, are you.',
    ],
    roomOpened: [
      'You opened a door that was painted shut for a reason. The reason was fear. The fear was yours. Anyway \u2014 mind the dust.',
    ],
    whyAsked: [
      'You asked it WHY it\u2019s here. Nobody asks the feelings why. They just tell you. Look at you, doing the brave thing.',
    ],
    wonderOn: [
      'Wonder Mode. The walls go see-through and you can finally see the threads running between everything. It was all connected. It was always all connected.',
    ],
    wonderOff: [
      'Back to opaque. The threads are still there. You just can\u2019t see them right now. That\u2019s okay. Knowing they\u2019re there is most of it.',
    ],
  };
  Object.assign(D.GUS, GUS_V2);

  window.LifeData.V2 = { MUT_KEYS, HUNCH, QUESTIONS, ROOMS, LORE, MEMORIES };
})();
