// fs-ui.jsx — shared UI for ShortsFieldStation.org
// Depends on window.FS_SEALS (fs-seals.js) and the FS_* data globals.

const { useState, useEffect } = React;

/* ---- navigation: real anchors that crawlers can follow, SPA takeover on click ---- */
function hrefTo(to){ return '/'+(to==='home'?'':to); }
function navTo(to){ history.pushState(null,'',hrefTo(to)); window.dispatchEvent(new Event('srfs:navigate')); }
function NavA(props){
  const { to, children, ...rest } = props;
  const onClick = (e)=>{ e.stopPropagation(); if(e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||e.button===1) return; e.preventDefault(); navTo(to); };
  return <a href={hrefTo(to)} onClick={onClick} {...rest}>{children}</a>;
}

/* ---- seals: render the real artwork via dangerouslySetInnerHTML ---- */
function Seal({ dept, size=120, house=false, className, style, title }){
  let markup;
  if(house) markup = FS_SEALS.houseSealR4(size, 'var(--ink)');
  else {
    const opts = dept.legendOverride ? { legend: dept.s1 } : {};
    markup = FS_SEALS.sealRound4(dept.key, dept.s1, dept.s2, dept.code, dept.plate, dept.color, size, opts);
  }
  const a11y = title ? { role:'img', 'aria-label':title } : { 'aria-hidden':'true' };
  return <div className={className} style={style} title={title} {...a11y}
    dangerouslySetInnerHTML={{__html:markup}}/>;
}
const DEPT_BY = {}; FS_SEALS.DEPTS.forEach(d=>{ DEPT_BY[d.key]=d; });

/* ---- masthead ---- */
function Masthead({ route, go }){
  const [open,setOpen] = useState(false);
  const nav = ()=> setOpen(false);
  return (
    <header className="mast">
      <div className="mast-rule" aria-hidden="true">
        <span>Short&rsquo;s Resort Field Station</span>
        <span>Lampasas County · Texas Hill Country</span>
      </div>
      <div className="mast-in">
        <a className="brand" href="/" style={{color:'inherit'}} onClick={(e)=>{e.preventDefault();go('home');nav();}}>
          <Seal house size={54} className="wm-seal" title="Short's Resort house seal"/>
          <div className="wm">
            <div className="n">Short&rsquo;s Resort<br/>Field Station</div>
            <div className="s">Est. 2026 · No. 01</div>
          </div>
        </a>
        <button className="burger" onClick={()=>setOpen(o=>!o)} aria-label="Menu"><span></span><span></span><span></span></button>
        <nav className={'nav'+(open?' open':'')}>
          {FS_SECTIONS.map(s=>(
            <a key={s.id} href={hrefTo(s.id)} className={route===s.id?'on':''} onClick={(e)=>{e.preventDefault();go(s.id);nav();}}>
              <span className="num">{s.no}</span>{s.title}
            </a>
          ))}
        </nav>
      </div>
    </header>
  );
}

/* ---- folio header on each sheet ---- */
function Folio({ left, right }){
  return <div className="folio"><span>{left}</span><span>{right}</span></div>;
}
function PlateStamp({ no }){
  return (
    <div className="plate-stamp" aria-hidden="true">
      <span className="ps-pl">Plate</span>
      <span className="ps-no">{no}</span>
      <span className="ps-fs">SRFS</span>
    </div>
  );
}
function SectionHead({ no, title, deck, aside, seal }){
  return (
    <div className="shead-wrap" style={{marginBottom:'8px'}}>
      {seal && (seal==='house'
        ? <Seal house size={560} className="shead-wm" title=""/>
        : <Seal dept={DEPT_BY[seal]} size={560} className="shead-wm" title=""/>)}
      <div className="shead">
        <div>
          <div className="eyebrow">Section {no} · Short&rsquo;s Resort Field Station</div>
          <h1 className="disp">{title}</h1>
        </div>
        {aside || null}
      </div>
      {deck && <p className="deck">{deck}</p>}
    </div>
  );
}

/* ---- the property plat (real trace from the section atlas) ---- */
function PlatMap(){
  return (
    <svg className="map" viewBox="0 0 760 805" xmlns="http://www.w3.org/2000/svg" fontFamily="JetBrains Mono, monospace">
      <g stroke="#1B1813" strokeWidth="1.2" strokeLinejoin="round" fillOpacity="0.55">
        <polygon points="131,4 300,130 220,300 72,214" fill="#8E9B85"/>
        <polygon points="72,214 220,300 250,500 290,760 226,724 150,520 40,300" fill="#5C7A78"/>
        <polygon points="220,300 300,130 415,148 345,788 290,760 250,500" fill="#5C7A78"/>
        <polygon points="415,148 515,195 398,800 345,788" fill="#5C7A78"/>
        <polygon points="515,195 610,242 450,802 398,800" fill="#B58A3C"/>
        <polygon points="610,242 700,283 470,801 450,802" fill="#B58A3C"/>
        <polygon points="700,283 755,309 628,472 470,801" fill="#8E9B85"/>
      </g>
      <path d="M62,206 L40,300 L150,520 L210,690 L232,732" fill="none" stroke="#5C7A78" strokeWidth="9" strokeLinecap="round" strokeLinejoin="round"/>
      <path d="M62,206 L40,300 L150,520 L210,690 L232,732" fill="none" stroke="#1B1813" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round"/>
      <polygon points="131,4 326,102 755,309 628,472 470,801 226,724 150,520 40,300 72,214 140,250 176,198" fill="none" stroke="#1B1813" strokeWidth="2.6" strokeLinejoin="round"/>
      <path d="M755,309 L628,472 L470,801" fill="none" stroke="#1B1813" strokeWidth="1" strokeDasharray="2 5" strokeLinejoin="round"/>
      <rect x="556" y="498" width="15" height="15" fill="#1B1813"/>
      <circle cx="165" cy="470" r="6" fill="#5C7A78" stroke="#1B1813" strokeWidth="1.2"/>
      <circle cx="430" cy="640" r="6" fill="#5C7A78" stroke="#1B1813" strokeWidth="1.2"/>
      <g fill="#1B1813" fontSize="11" fontWeight="700" letterSpacing="1">
        <text x="78" y="300">G·01</text><text x="664" y="330">G·02</text><text x="360" y="770">G·03</text>
      </g>
      <g fill="#1B1813" fontFamily="Zilla Slab, serif" fontWeight="900" fontSize="30" textAnchor="middle" style={{paintOrder:'stroke'}} stroke="#F4ECD8" strokeWidth="3.5">
        <text x="178" y="158">4</text><text x="140" y="430">1</text><text x="305" y="430">5</text>
        <text x="428" y="470">3</text><text x="528" y="495">7</text><text x="582" y="540">6</text><text x="648" y="412">2</text>
      </g>
    </svg>
  );
}

/* ---- footer ---- */
/* ---- agency partner marks (official art, rendered in station ink) ----
   USDA symbol traced from the department's official logo (Wikimedia Commons, PD);
   TPWD square is the department's own vector (tpwd.texas.gov/icon.svg), text
   knockouts preserved via mask so the paper shows through. */
function AgencyMark({ kind, size=46 }){
  if(kind==='nrcs') return (
    <svg width={Math.round(size*1.52)} height={size} viewBox="-8 -8 282 186" role="img" aria-label="USDA NRCS" style={{color:'var(--ink)',flex:'none'}}>
      <g transform="matrix(1.3333 0 0 -1.3333 24.472 150.84)">
        <g transform="matrix(1.2496 0 0 1.2496 -17.586 -13.475)">
          <path fill="currentColor" d="m126.33 100.58s-16.305-38.069-17.483-40.425c-0.62-1.178-1.1786-1.8601-2.9136-3.0381h8.5547c-1.674 0.744-1.9833 1.6131-1.4253 3.1011 0.124 0.682 1.6117 4.3402 3.4717 8.5562h15.19c1.736-4.216 3.3487-7.9991 3.5347-8.6191 0.433-1.301 0.12325-2.1701-1.4898-3.0381h13.27c-2.046 1.054-2.4801 1.7371-3.1011 3.0381-1.115 2.356-17.609 40.425-17.609 40.425zm-74.754-0.61963c-5.642 0-13.516-3.3483-13.516-11.78 0-4.712 2.4801-8.0602 8.1841-11.222 5.642-3.038 11.036-3.8434 11.036-9.7954 0-3.039-1.8601-7.4414-7.998-7.4414-6.448 0-10.167 5.8285-11.345 10.23l0.30908-10.478c2.17-1.054 6.2628-2.9766 11.905-2.9766 7.626 0 14.446 5.2695 14.446 13.02 0 8.619-6.0143 10.664-11.78 13.393-5.828 2.666-8.4946 4.093-8.4946 7.875 0 2.666 1.8608 5.9517 6.5728 5.9517 5.952 0 9.4855-4.9596 10.354-8.2456l-0.30908 9.1758c-3.163 1.86-7.2553 2.2939-9.3633 2.2939zm-51.579-0.68262c0.992-0.496 1.9834-1.3631 1.9834-2.7891v-25.793c0-10.975 9.2387-14.199 16.493-14.199 8.432 0 14.694 4.0913 14.694 14.632l0.18604 25.359c0 1.426 0.68133 2.2931 1.6113 2.7891h-7.1909c0.931-0.496 1.6743-1.3631 1.6743-2.7891l0.12305-25.484c0-6.138-2.6664-10.973-10.044-10.973-8.06 0-9.5479 6.137-9.5479 10.478v25.979c0 1.426 0.92988 2.2931 1.9219 2.7891zm68.689 0c0.93-0.496 1.6099-1.3631 1.6099-2.7891v-36.582c0-1.364-0.67986-2.2325-1.6099-2.7905h15.933c16.74 0 25.049 10.913 25.049 21.702 0 6.014-4.0917 20.459-24.491 20.459zm15.189-3.0996c12.834 0 17.175-9.9199 17.175-17.36 0-9.053-3.9064-18.476-17.919-18.476h-4.96v35.711c0.931 0.124 4.2161 0.12451 5.7041 0.12451zm40.1-8.9897s3.4097-8.3084 6.3237-15.19h-12.526c1.984 4.65 6.2022 15.19 6.2022 15.19z"/>
          <path fill="currentColor" d="m141.2 51.2c-18 0.039266-92.923-1.3602-139.68-26.275 0 0 56.631 22.395 143.66 20.155v6.0791s-1.41 0.03541-3.9814 0.04101zm-139.68-0.041015v-5.7583c10.559 1.599 49.271-1.5996 49.271-1.5996-18.877 7.039-49.271 7.3579-49.271 7.3579zm18.076-9.5742c-9.5767 0.031298-18.076-0.34277-18.076-0.34277v-5.7598c3.1997 1.6 34.875 5.439 34.875 5.439-4.9192 0.47962-11.053 0.6448-16.799 0.66358zm116.93-1.2495c-1.8444-8e-3 -3.9542-0.03049-6.3032-0.07324l-0.46289-0.0088c-26.237-0.50299-81.257-3.6693-128.22-20.26v-19.992l143.66 0.026367v40.235s-3.1388 0.09729-8.6719 0.07324z"/>
        </g>
      </g>
    </svg>
  );
  return (
    <svg width={size} height={size} viewBox="0 0 288 288" role="img" aria-label="Texas Parks and Wildlife" style={{color:'var(--ink)',flex:'none'}}>
      <defs>
        <mask id="tpwd-knockout" maskUnits="userSpaceOnUse" x="0" y="0" width="288" height="288">
          <path fill="#fff" d="M274.76,7.28c3.29,0,5.96,2.67,5.96,5.96v261.52c0,3.29-2.67,5.96-5.96,5.96H13.24c-3.29,0-5.96-2.67-5.96-5.96V13.24c0-3.29,2.67-5.96,5.96-5.96h261.51M274.76,0H13.24C5.94,0,0,5.94,0,13.24v261.52c0,7.3,5.94,13.24,13.24,13.24h261.51c7.3,0,13.24-5.94,13.24-13.24V13.24C288,5.94,282.06,0,274.76,0h0ZM272.37,269.91V18.09c0-1.36-1.1-2.46-2.46-2.46H18.09c-1.36,0-2.46,1.1-2.46,2.46v252.5c0,.98.8,1.78,1.78,1.78h252.5c1.36,0,2.46-1.1,2.46-2.46Z"/>
          <path fill="#000" d="M78.68,204.43l-7.46,24.16-7.37-24.16h-5l-7.54,24.03-7.25-24.03h-5.62l9.67,31.61h5.79l7.33-23.39,7.21,23.39h5.75l9.67-31.61h-5.16ZM88.65,236.04h5.42v-31.61h-5.42v31.61ZM102.35,236.04h20.67v-4.97h-15.25v-26.65h-5.42v31.61ZM127.72,236.04h12.75c9.46,0,15.92-6.32,15.92-15.81s-6.46-15.81-15.92-15.81h-12.75v31.61ZM133.13,231.07v-21.68h7.08c6.5,0,10.71,4.29,10.71,10.84s-4.21,10.84-10.71,10.84h-7.08ZM161.07,236.04h20.67v-4.97h-15.25v-26.65h-5.42v31.61ZM187.78,236.04h5.42v-31.61h-5.42v31.61ZM222.25,209.35v-4.92h-21.29v31.61h5.42v-12.11h14.08v-4.97h-14.08v-9.62h15.87ZM233.32,231.11v-8.71h14.09v-4.83h-14.09v-8.22h15.88v-4.92h-21.3v31.61h21.88v-4.93h-16.46ZM72.7,59.09v-6.81h-35.02v6.81h13.8v38.13h7.37v-38.13h13.86ZM113.24,59.09v-6.81h-31.22v44.93h31.97v-6.8h-24.6v-12.39h21.35v-6.8h-21.35v-12.13h23.85ZM148.35,73.85l14.67-21.57h-8.62l-10.74,15.6-10.68-15.6h-8.56l14.8,21.89-15.92,23.04h8.62l11.74-16.95,11.8,16.95h8.8l-15.92-23.37ZM249.76,56.58c-4.06-2.76-9.61-4.62-15.3-4.62-9.49,0-15.92,4.82-15.92,12.52,0,15.98,23.6,10.4,23.6,20.42,0,3.47-3.06,5.45-8.18,5.45-4.68,0-10.3-2.31-14.8-6.55l-3.19,6.55c4.5,4.24,10.99,7.13,17.86,7.13,9.61,0,16.48-4.94,16.48-12.97.06-16.18-23.54-10.91-23.54-20.73,0-3.02,2.81-4.68,7.12-4.68,3.31,0,8.05,1.22,12.8,4.17l3.06-6.68M212.85,97.22h-7.78l-4.14-10.05h-21.79l-4.14,10.05h-7.59l19.08-44.53h7.47l18.89,44.53ZM198.15,80.49l-8.15-19.79-8.15,19.79h16.3ZM51.85,133.67c8.24,0,12.97,4.19,12.97,11.5s-4.73,12.03-12.97,12.03h-7.53v10.15h-5.53v-33.69h13.06M51.62,152.1c5.05,0,7.91-2.21,7.91-6.79s-2.86-6.54-7.91-6.54h-7.3v13.33h7.3M132.64,167.35h-6.27l-5.99-10.2c-.47.05-.98.05-1.5.05h-7.77v10.15h-5.52v-33.69h13.29c8.38,0,13.2,4.19,13.2,11.5,0,5.39-2.44,9.19-6.79,10.93l7.35,11.26ZM118.88,152.1c5.06,0,7.96-2.21,7.96-6.79s-2.9-6.54-7.96-6.54h-7.77v13.33h7.77M162.69,167.35h6.64l-13.53-19.29,12.69-14.39h-6.51l-15.63,17.42v-17.42h-5.53v33.69h5.53v-8.57l5.57-6.3,10.77,14.87ZM198.07,136.89c-3.04-2.07-7.21-3.47-11.47-3.47-7.11,0-11.93,3.61-11.93,9.39,0,11.99,17.7,7.8,17.7,15.31,0,2.6-2.3,4.09-6.13,4.09-3.51,0-7.72-1.73-11.1-4.91l-2.39,4.91c3.37,3.17,8.24,5.34,13.39,5.34,7.21,0,12.36-3.71,12.36-9.72.05-12.13-17.65-8.18-17.65-15.54,0-2.26,2.11-3.51,5.34-3.51,2.48,0,6.04.91,9.6,3.13l2.3-5M99.6,167.55h-5.83l-3.1-7.53h-16.34l-3.1,7.53h-5.69l14.3-33.39h5.6l14.16,33.39ZM88.59,155l-6.11-14.84-6.11,14.84h12.22ZM249.99,163.6l-3.77-3.91c1.24-2.1,2.14-4.61,2.7-7.45l-4.34-1.53c-.38,2.1-.94,3.95-1.67,5.49l-6.48-6.7c4.34-2.66,6.13-5.08,6.13-8.43,0-4.43-3.17-7.17-8.02-7.17-5.28,0-8.71,3.12-8.71,7.68,0,2.38.9,4.52,3.35,7.17-4.89,2.98-6.95,5.82-6.95,9.87,0,5.12,4.59,8.71,10.95,8.71,3.95,0,7.38-1.4,10.09-3.91l3.78,3.96,2.96-3.77ZM240.08,160.11c-1.85,1.63-4.03,2.47-6.52,2.47-3.69,0-6.09-1.86-6.09-4.56,0-2.33,1.21-4.05,4.68-6.15l7.94,8.25M230.72,141.48c0-2,1.42-3.45,3.82-3.45,2.23,0,3.48,1.26,3.48,3.12,0,1.77-1.12,3.12-4.59,5.17-2.14-2.33-2.71-3.4-2.71-4.84M249.26,184.22H38.29v3.83h210.97v-3.83ZM249.26,113.22H38.29v3.83h210.97v-3.83Z"/>
        </mask>
      </defs>
      <rect width="288" height="288" fill="currentColor" mask="url(#tpwd-knockout)"/>
    </svg>
  );
}

function Footer({ go }){
  return (
    <footer className="foot">
      <div className="foot-in">
        <div className="foot-cols">
          <div>
            <h5>About this site</h5>
            <p className="colophon">
              Short&rsquo;s Resort Field Station (SRFS) is the public record of an ongoing land restoration in the Texas Hill Country, above the Lampasas River.
              We&rsquo;re rebuilding oak savanna, prairie and riparian habitat one season at a time &mdash; and posting the whole thing in the open. The documents and code here are free to borrow.
            </p>
            <div style={{display:'flex',gap:'18px',marginTop:'20px',flexWrap:'wrap'}}>
              {FS_FUNDERS.map(f=>(
                <a key={f.k} href={f.url} target="_blank" rel="noopener" title={f.program} style={{display:'flex',alignItems:'center',gap:'10px',color:'var(--ink)'}}>
                  <AgencyMark kind={f.mark} size={38}/>
                  <span className="mono" style={{fontSize:'10px',letterSpacing:'0.1em',textTransform:'uppercase',lineHeight:1.4}}>{f.k}<br/><span style={{opacity:.65}}>{f.short} ↗</span></span>
                </a>
              ))}
            </div>
          </div>
          <div>
            <h5>Sections</h5>
            <ul>
              {FS_SECTIONS.map(s=><li key={s.id}><NavA to={s.id}>{s.no} · {s.title}</NavA></li>)}
            </ul>
          </div>
          <div>
            <h5>Station</h5>
            <ul>
              <li><a href={'https://'+FS_REPO.path} target="_blank" rel="noopener">Source repository ↗</a></li>
              <li><NavA to={'departments'}>The Departments</NavA></li>
              <li><NavA to={'ii'}>The Propagation Unit</NavA></li>
            </ul>
          </div>
        </div>
        <div className="foot-base">
          <span>Supported by <a href={FS_FUNDERS[0].url} target="_blank" rel="noopener">USDA–NRCS · EQIP</a> &middot; <a href={FS_FUNDERS[1].url} target="_blank" rel="noopener">Texas Parks &amp; Wildlife · Pastures for Upland Birds</a></span>
          <span>ShortsFieldStation.org &middot; No. 01 &middot; MMXXVI</span>
        </div>
      </div>
    </footer>
  );
}

Object.assign(window, { Seal, DEPT_BY, Masthead, Folio, SectionHead, PlatMap, AgencyMark, Footer });
