// ─── THE TUTORIAL ───
// A hands-on walk through a REAL game, not a slideshow. Every step asks the
// player to do the actual thing — tap a character, move it, take a shot — and
// waits until they have. The game underneath is the game; nothing is faked and
// nothing is simulated, so what is learned here is true on the board afterwards.
//
// Each step is {text, hint, done(ctx)}. `done` is checked against live state on
// every commit, so the tutorial follows the player rather than the player
// following the tutorial: do the right thing early and it simply moves on, and
// wander off and it waits without complaining.
//
// Predicates are deliberately permissive. A tutorial that insists on one exact
// order is a tutorial that gets stuck, and a stuck tutorial is worse than none.
const COACH=[
  {text:"This is your warband, drawn up in your base. Everything moves and shoots by hex.\n\nTap one of your characters to select it.",
   hint:"Tap a standee on your side",
   done:c=>!!c.selMine},

  {text:"The blue hexes are everywhere that character can reach this turn. You cannot pass through another figure, a border line, or crossed-off ground.\n\nTap a blue hex to move there.",
   hint:"Tap any blue hex",
   done:c=>c.movedAny},

  {text:"You can move as many characters as you like, and change your mind freely — nothing is committed while the movement phase is open.\n\nWhen you are done moving, press End Movement.",
   hint:"End Movement",
   done:c=>c.phase==="action"},

  {text:"Now the action phase. Each character may do ONE thing: attack, roll for coin, or use a special.\n\nThe catch: the moment anyone acts, movement is over for your whole team. Move everybody first, then act.\n\nTap a character to see what it can do.",
   hint:"Tap one of your characters",
   done:c=>!!c.selMine},

  {text:"This panel is everything that character can do. Anything marked FREE costs no action — always take those too.\n\nIf an enemy is in range, choose Attack and tap them. If not, use Roll for Coin.",
   hint:"Attack, or Roll for Coin",
   done:c=>c.actedAny},

  {text:"That is the exchange. You roll attack dice equal to your Attack, the defender rolls theirs, and the damage is your hits minus their shields — thrown one roll at a time so you can watch it happen.\n\nThe pulsing arrow on the right steps through it at your pace. On an opponent's turn, it is how you follow what they did.",
   hint:"",manual:true},

  {text:"Coin is the other half of the game. Rolling for coin spends a character's action: a d6, and you collect if it matches a number on their card.\n\nThree coin buys a diplomacy card. Tap Diplomacy at the top to see your hand.",
   hint:"Diplomacy, top of the screen",
   done:c=>c.sheet==="hand"},

  {text:"Diplomacy cards are the swing: enhancements that last, one-off attacks, reviving the dead. Some — King's Guard, Last Stand — you simply hold, and they fire on their own at the right moment.\n\nA faction card you cannot use returns for three coin.",
   hint:"",manual:true},

  {text:"Two ways to see the board. Overhead is for planning a move; Battle View drops you among the figures.\n\nTap the view button to swap.",
   hint:"Overhead / Battle View",
   done:c=>c.viewChanged},

  {text:"Everything else is in the menu — your warbands and theirs, the battle log, and the Special Index with all 44 cards and every back-card special.\n\nOpen the menu to look.",
   hint:"The menu button, top right",
   done:c=>c.menuOpen},

  {text:"That is the whole game.\n\nMove everyone, then act. Take the Rock at the centre when you can. When one of your cards falls, bring on a reinforcement next turn. Win by being the last warband standing.\n\nThis battle is real and still yours — carry on, or leave and start fresh.",
   hint:"",manual:true,last:true}
];

function CoachOverlay({step,ctx,onNext,onSkip,vp}){
  const s=COACH[step];
  const [open,setOpen]=useState(true);
  useEffect(()=>{setOpen(true)},[step]);   // a new step is worth reading
  if(!s)return null;
  const ready=s.manual||(s.done&&s.done(ctx));

  // Pinned under the top bar rather than over the bottom edge. The bottom
  // carries the narration, the tips, the action bars, the roster and the
  // sheets, and this was landing on top of all of it.
  //
  // The panel takes NO pointer events; only its own buttons do. So even where
  // it overlaps the board, a tap passes through it to the hex underneath —
  // nothing the tutorial says can get between the player and the game.
  //
  // And it collapses to a single line, because by the third step the words
  // matter less than the hint.
  const wrap={position:"absolute",zIndex:70,pointerEvents:"none",
    left:"50%",transform:"translateX(-50%)",
    top:"calc(58px + var(--sa-t))",
    width:vp.compact?"min(92vw,340px)":"min(88vw,420px)"};
  const btn={pointerEvents:"auto",background:"none",border:0,cursor:"pointer",flex:"none"};

  return(
    <div style={wrap}>
      <div className="glass anim-fade" style={{padding:open?"var(--s-2) var(--s-3)":"6px var(--s-3)",
        border:"1px solid var(--gold-500)",
        boxShadow:"0 0 0 2px rgba(20,10,5,.45),0 14px 30px -16px #000"}}>
        <div className="row gap-2" style={{alignItems:"center"}}>
          <span className="t-micro" style={{color:"var(--gold-300)",letterSpacing:".14em",
            textTransform:"uppercase",flex:"none"}}>{step+1}/{COACH.length}</span>
          {!open&&<span className="t-micro" style={{color:"var(--parch-100)",minWidth:0,
            overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"}}>
            {s.manual?"tap to read":s.hint}</span>}
          <div className="grow"/>
          {!open&&(s.manual||ready)&&<span style={{pointerEvents:"auto"}}>
            <Btn size="sm" icon={s.last?"sword":"chevR"} onClick={onNext}>
              {s.last?"Finish":"Next"}</Btn></span>}
          <button onClick={()=>setOpen(o=>!o)} title={open?"Collapse":"Expand"}
            style={{...btn,color:"var(--gold-300)",padding:"2px 6px"}}>
            <Icon n={open?"chevU":"chevD"} size={16}/>
          </button>
          <button onClick={onSkip} className="t-micro"
            style={{...btn,color:"var(--parch-400)",padding:"2px 4px"}}>Skip</button>
        </div>

        {open&&<>
          <div className="t-sm" style={{color:"var(--parch-100)",lineHeight:1.4,
            whiteSpace:"pre-line",marginTop:4}}>{s.text}</div>
          <div className="row gap-2" style={{alignItems:"center",marginTop:"var(--s-2)"}}>
            {!s.manual&&<span className="t-micro"
              style={{color:ready?"var(--gold-300)":"var(--parch-400)",
                animation:ready?"none":"pulseSoft 1.6s ease-in-out infinite"}}>
              {ready?"Done":"→ "+s.hint}</span>}
            <div className="grow"/>
            {(s.manual||ready)&&<span style={{pointerEvents:"auto"}}>
              <Btn size="sm" icon={s.last?"sword":"chevR"} onClick={onNext}>
                {s.last?"Finish":"Next"}</Btn></span>}
          </div>
        </>}

        <div className="row gap-1" style={{marginTop:6}}>
          {COACH.map((_,i)=>(
            <span key={i} style={{height:3,flex:1,borderRadius:2,
              background:i<=step?"var(--gold-500)":"var(--ink-500)"}}/>))}
        </div>
      </div>
    </div>);
}
