// Home — single responsive page. Dark only.
// Rail layout (232px label + content) collapses to single column under 900px.

const { useState, useEffect } = React;

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "labelStyle": "rail",
  "fontSet": "A"
}/*EDITMODE-END*/;

const FONT_SETS = {
  A: { name: 'Editorial classic', credit: 'Source Serif · Inter Tight · JetBrains Mono' },
  B: { name: 'Warm character',    credit: 'Fraunces · Manrope · JetBrains Mono' },
  C: { name: 'Magazine literary', credit: 'Newsreader · Inter Tight · IBM Plex Mono' },
  D: { name: 'High-contrast display', credit: 'Instrument Serif · Inter Tight · JetBrains Mono' },
  E: { name: 'Technical / Linear-adjacent', credit: 'Spectral · Inter Tight · IBM Plex Mono' },
};

const Page = () => {
  const [t, setTweak] = window.useTweaks(TWEAK_DEFAULTS);

  // Reflect mode onto <body> so CSS can collapse rails for "none"/"inline".
  useEffect(() => {
    document.body.setAttribute('data-label-mode', t.labelStyle);
    document.body.setAttribute('data-fontset', t.fontSet);
    return () => {
      document.body.removeAttribute('data-label-mode');
      document.body.removeAttribute('data-fontset');
    };
  }, [t.labelStyle, t.fontSet]);

  return (
    <LabelStyleCtx.Provider value={t.labelStyle}>
      <Nav />
      <Hero />
      <Now />
      <SelectedWork />
      <Writing />
      <Experience />
      <Speaking />
      <About />
      <ContactFooter />

      <window.TweaksPanel title="Tweaks">
        <window.TweakSection label="Typography" />
        <window.TweakRadio
          label="Font set"
          value={t.fontSet}
          options={['A', 'B', 'C', 'D', 'E']}
          onChange={(v) => setTweak('fontSet', v)}
        />
        <div style={{
          fontSize: 10, color: 'rgba(41,38,27,.7)', marginTop: 4, lineHeight: 1.5,
          fontFamily: 'ui-monospace, monospace',
        }}>
          {t.fontSet} · {FONT_SETS[t.fontSet].name}<br/>
          <span style={{ color: 'rgba(41,38,27,.45)' }}>
            {FONT_SETS[t.fontSet].credit}
          </span>
        </div>

        <window.TweakSection label="Section labels" />
        <window.TweakRadio
          label="Style"
          value={t.labelStyle}
          options={['rail', 'inline', 'numeric', 'hairline', 'none']}
          onChange={(v) => setTweak('labelStyle', v)}
        />
        <div style={{ fontSize: 10, color: 'rgba(41,38,27,.55)', marginTop: 4, lineHeight: 1.4 }}>
          rail · current mono label rail<br/>
          inline · label sits above heading<br/>
          numeric · big chapter number, no §<br/>
          hairline · thin rule with tucked label<br/>
          none · headings only, full width
        </div>
      </window.TweaksPanel>
    </LabelStyleCtx.Provider>
  );
};

const LabelStyleCtx = React.createContext('rail');

// Shared section label — renders differently per mode.
// Caller passes (num, label, sub?).
const SectionLabel = ({ num, label, sub }) => {
  const mode = React.useContext(LabelStyleCtx);

  if (mode === 'none') return null;

  if (mode === 'numeric') {
    return (
      <div>
        <div style={{
          fontFamily: 'var(--serif)',
          fontSize: 64,
          lineHeight: 1,
          fontWeight: 300,
          letterSpacing: '-0.03em',
          color: 'var(--muted-2)',
        }}>{num}</div>
        <div className="mono" style={{ marginTop: 14 }}>{label}</div>
        {sub && (
          <div className="mono-md" style={{ marginTop: 8, color: 'var(--muted-2)', fontSize: 11 }}>
            {sub}
          </div>
        )}
      </div>
    );
  }

  if (mode === 'hairline') {
    return (
      <div>
        <div className="mono" style={{ fontSize: 10, marginBottom: 10 }}>{label}</div>
        <div style={{
          width: 56,
          height: 1,
          background: 'var(--ink-2)',
          marginBottom: 10,
        }}/>
        <div className="mono" style={{ fontSize: 10, color: 'var(--muted-2)' }}>
          {String(num).padStart(2, '0')}
        </div>
        {sub && (
          <div className="mono-md" style={{ marginTop: 14, color: 'var(--muted-2)', fontSize: 11 }}>
            {sub}
          </div>
        )}
      </div>
    );
  }

  // rail (default) and inline use the same DOM — CSS collapses the grid for inline.
  return (
    <div>
      <div className="mono">{label}</div>
      {sub && (
        <div className="mono-md" style={{ marginTop: 8, color: 'var(--muted-2)', fontSize: 11 }}>
          {sub}
        </div>
      )}
    </div>
  );
};

// ─────────── Nav ───────────
const Nav = () => (
  <nav style={{
    borderBottom: '1px solid var(--rule-2)',
    position: 'sticky',
    top: 0,
    background: 'color-mix(in oklab, var(--bg) 85%, transparent)',
    backdropFilter: 'blur(12px)',
    WebkitBackdropFilter: 'blur(12px)',
    zIndex: 10,
  }}>
    <div className="container" style={{
      padding: '22px 96px',
      display: 'flex',
      alignItems: 'center',
    }}>
      <a href="#" className="mono" style={{ fontSize: 11 }}>
        ramirohigonet.dev
      </a>
    </div>
  </nav>
);

// ─────────── Hero ───────────
const Hero = () => {
  const D = window.PF_DATA;
  return (
    <section className="container" style={{ padding: '140px 96px 120px' }}>
      <div style={{ maxWidth: 980 }}>
        <h1 style={{
          fontFamily: 'var(--serif)',
          fontSize: 'clamp(44px, 7vw, 84px)',
          lineHeight: 1.04,
          fontWeight: 400,
          letterSpacing: '-0.025em',
          margin: 0,
          color: 'var(--ink)',
          textWrap: 'balance',
          maxWidth: '18ch',
        }}>
          Ten years turning hard problems into shippable software.
        </h1>
        <p style={{
          fontFamily: 'var(--serif)',
          fontSize: 'clamp(17px, 1.6vw, 22px)',
          lineHeight: 1.45,
          color: 'var(--ink-2)',
          marginTop: 40,
          maxWidth: 680,
          fontWeight: 300,
        }}>
          {D.heroSub}
        </p>
        <div style={{ display: 'flex', gap: 36, marginTop: 44, alignItems: 'baseline', flexWrap: 'wrap' }}>
          <a href="#work" className="mono underline-link" style={{
            fontSize: 12,
            letterSpacing: '0.04em',
            paddingBottom: 4,
            display: 'inline-flex',
            gap: 10,
            alignItems: 'baseline',
          }}>
            See selected work <span style={{ opacity: 0.5 }}>→</span>
          </a>
          <a href={`mailto:${D.contact.email}`} className="mono underline-link" style={{ fontSize: 11, paddingBottom: 4 }}>
            {D.contact.email}
          </a>
        </div>
        <div className="mono" style={{ marginTop: 56, color: 'var(--muted-2)', fontSize: 10 }}>
          Senior Software Engineer · Buenos Aires, AR · Available 2026 Q3
        </div>
      </div>
    </section>
  );
};

// ─────────── Now ───────────
const Now = () => {
  const D = window.PF_DATA;
  return (
    <section className="container" style={{ padding: '64px 96px', borderTop: '1px solid var(--rule)' }}>
      <div className="rail">
        <SectionLabel num="01" label="Now" />
        <p style={{
          fontFamily: 'var(--serif)',
          fontSize: 'clamp(19px, 1.9vw, 26px)',
          lineHeight: 1.4,
          fontWeight: 300,
          margin: 0,
          color: 'var(--ink-2)',
          maxWidth: 720,
          textWrap: 'pretty',
        }}>
          {D.now}
        </p>
      </div>
    </section>
  );
};

// ─────────── Selected Work ───────────
const SelectedWork = () => {
  const D = window.PF_DATA;
  return (
    <section id="work" className="container" style={{
      padding: '120px 96px 60px',
      borderTop: '1px solid var(--rule)',
    }}>
      <div className="rail-3" style={{ marginBottom: 72 }}>
        <SectionLabel num="02" label="Selected work" />
        <h2 style={{
          fontFamily: 'var(--serif)',
          fontSize: 'clamp(32px, 4.5vw, 48px)',
          fontWeight: 400,
          letterSpacing: '-0.02em',
          margin: 0,
        }}>
          Things I've shipped that mattered.
        </h2>
        <a href="#" className="mono underline-link" style={{ fontSize: 11, paddingBottom: 4 }}>
          All work →
        </a>
      </div>

      {D.caseStudies.map((cs, i) => (
        <CaseStudyRow key={cs.id} cs={cs} isLast={i === D.caseStudies.length - 1} />
      ))}
    </section>
  );
};

const CaseStudyRow = ({ cs, isLast }) => {
  const [hover, setHover] = useState(false);
  return (
    <a
      href={`case-study.html?id=${cs.id}`}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{
        display: 'grid',
        gridTemplateColumns: 'minmax(180px, 232px) 1fr minmax(220px, 280px)',
        gap: 24,
        padding: '40px 0',
        borderTop: '1px solid var(--rule)',
        borderBottom: isLast ? '1px solid var(--rule)' : 'none',
        alignItems: 'start',
        cursor: 'pointer',
        transition: 'background .25s',
        background: hover ? 'var(--rule-2)' : 'transparent',
        marginInline: hover ? -16 : 0,
        paddingInline: hover ? 16 : 0,
        borderRadius: 2,
      }}
    >
      <div>
        <div className="mono" style={{ fontSize: 10 }}>
          / Case {cs.number}
        </div>
        <div style={{
          fontFamily: 'var(--serif)',
          fontSize: 64,
          lineHeight: 1,
          fontWeight: 300,
          color: hover ? 'var(--ink-2)' : 'var(--muted-2)',
          marginTop: 12,
          letterSpacing: '-0.02em',
          transition: 'color .25s',
        }}>{cs.year}</div>
        <div className="mono-md" style={{ marginTop: 16, fontSize: 11 }}>{cs.kicker}</div>
      </div>

      <div style={{ paddingRight: 40 }}>
        <h3 style={{
          fontFamily: 'var(--serif)',
          fontSize: 'clamp(24px, 3vw, 38px)',
          lineHeight: 1.1,
          fontWeight: 400,
          letterSpacing: '-0.02em',
          margin: 0,
          textWrap: 'balance',
        }}>{cs.title}</h3>
        <p style={{
          fontSize: 16,
          lineHeight: 1.55,
          color: 'var(--ink-2)',
          marginTop: 18,
          maxWidth: 560,
          fontWeight: 300,
        }}>{cs.blurb}</p>
        <div style={{ marginTop: 22, display: 'flex', gap: 8, flexWrap: 'wrap' }}>
          {cs.stack.map(s => (
            <span key={s} className="mono" style={{
              fontSize: 10, padding: '4px 9px',
              border: '1px solid var(--rule)', borderRadius: 2,
              textTransform: 'none', letterSpacing: 0,
            }}>{s}</span>
          ))}
        </div>
      </div>

      <div style={{ paddingTop: 6 }}>
        <MetaPair label="Role" value={cs.role} />
        <MetaPair label="Duration" value={cs.duration} />
        <MetaPair label="At" value={cs.org} />
        <div style={{
          marginTop: 28,
          fontFamily: 'var(--mono)',
          fontSize: 11,
          color: 'var(--ink)',
          transform: hover ? 'translateX(4px)' : 'none',
          transition: 'transform .25s',
        }}>Read case →</div>
      </div>
    </a>
  );
};

const MetaPair = ({ label, value }) => (
  <div style={{ marginBottom: 14 }}>
    <div className="mono" style={{ fontSize: 10 }}>{label}</div>
    <div style={{ fontSize: 13, color: 'var(--ink-2)', marginTop: 3 }}>{value}</div>
  </div>
);

// ─────────── Writing ───────────
const Writing = () => {
  const D = window.PF_DATA;
  return (
    <section id="writing" className="container" style={{
      padding: '120px 96px 80px',
      borderTop: '1px solid var(--rule)',
    }}>
      <div className="rail-3" style={{ marginBottom: 56 }}>
        <SectionLabel num="03" label="Recent writing" />
        <h2 style={{
          fontFamily: 'var(--serif)',
          fontSize: 'clamp(32px, 4.5vw, 48px)',
          fontWeight: 400, letterSpacing: '-0.02em', margin: 0,
        }}>Notes on the work.</h2>
        <a href="#" className="mono underline-link" style={{ fontSize: 11, paddingBottom: 4 }}>
          Archive →
        </a>
      </div>

      <div className="rail">
        <div></div>
        <div>
          {D.writing.map((w, i) => (
            <a key={i} href="#" style={{
              display: 'grid',
              gridTemplateColumns: '90px 1fr 120px 80px',
              gap: 24,
              alignItems: 'baseline',
              padding: '22px 0',
              borderTop: '1px solid var(--rule)',
              borderBottom: i === D.writing.length - 1 ? '1px solid var(--rule)' : 'none',
            }}>
              <span className="mono" style={{ fontSize: 11 }}>{w.date}</span>
              <span style={{
                fontFamily: 'var(--serif)', fontSize: 22,
                fontWeight: 400, letterSpacing: '-0.01em',
              }}>{w.title}</span>
              <span className="mono" style={{ fontSize: 10, color: 'var(--muted-2)' }}>{w.tag}</span>
              <span className="mono" style={{ fontSize: 10, color: 'var(--muted-2)', textAlign: 'right' }}>{w.read}</span>
            </a>
          ))}
        </div>
      </div>
    </section>
  );
};

// ─────────── Experience ───────────
const Experience = () => {
  const D = window.PF_DATA;
  return (
    <section className="container" style={{
      padding: '120px 96px 80px',
      borderTop: '1px solid var(--rule)',
    }}>
      <div className="rail" style={{ marginBottom: 56 }}>
        <SectionLabel num="04" label="Career" />
        <h2 style={{
          fontFamily: 'var(--serif)',
          fontSize: 'clamp(32px, 4.5vw, 48px)',
          fontWeight: 400, letterSpacing: '-0.02em', margin: 0,
        }}>A decade, more or less.</h2>
      </div>

      <div className="rail">
        <div></div>
        <div style={{ maxWidth: 760 }}>
          {D.experience.map((e, i) => (
            <div key={i} style={{
              display: 'grid',
              gridTemplateColumns: '160px 1fr 1fr',
              gap: 24,
              alignItems: 'baseline',
              padding: '20px 0',
              borderTop: '1px solid var(--rule)',
              borderBottom: i === D.experience.length - 1 ? '1px solid var(--rule)' : 'none',
            }}>
              <span className="mono" style={{ fontSize: 11 }}>{e.years}</span>
              <span style={{ fontFamily: 'var(--serif)', fontSize: 22, fontWeight: 400 }}>{e.org}</span>
              <span style={{ fontSize: 14, color: 'var(--muted)', fontWeight: 300 }}>{e.role}</span>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
};

// ─────────── Speaking ───────────
const Speaking = () => {
  const D = window.PF_DATA;
  return (
    <section className="container" style={{
      padding: '120px 96px 80px',
      borderTop: '1px solid var(--rule)',
    }}>
      <div className="rail" style={{ marginBottom: 56 }}>
        <SectionLabel num="05" label="Speaking" />
        <h2 style={{
          fontFamily: 'var(--serif)',
          fontSize: 'clamp(32px, 4.5vw, 48px)',
          fontWeight: 400, letterSpacing: '-0.02em', margin: 0,
        }}>When asked.</h2>
      </div>

      <div className="rail">
        <div></div>
        <div style={{ maxWidth: 760 }}>
          {D.speaking.map((s, i) => (
            <div key={i} style={{
              display: 'grid',
              gridTemplateColumns: '120px 1fr 200px',
              gap: 24, alignItems: 'baseline',
              padding: '22px 0',
              borderTop: '1px solid var(--rule)',
              borderBottom: i === D.speaking.length - 1 ? '1px solid var(--rule)' : 'none',
            }}>
              <span className="mono" style={{ fontSize: 11 }}>{s.date}</span>
              <span style={{
                fontFamily: 'var(--serif)', fontSize: 22,
                fontWeight: 400, letterSpacing: '-0.01em',
              }}>{s.talk}</span>
              <span className="mono-md" style={{ fontSize: 12 }}>{s.venue}</span>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
};

// ─────────── About ───────────
const About = () => (
  <section id="about" style={{
    padding: '120px 0',
    borderTop: '1px solid var(--rule)',
    background: 'var(--rule-2)',
  }}>
    <div className="container" style={{ padding: '0 96px' }}>
      <div className="rail">
        <div>
          <SectionLabel num="06" label="About" />
          <figure style={{ margin: '24px 0 0', maxWidth: 168 }}>
            <img
              src="assets/portrait.jpg"
              alt="Ramiro Higonet, Florence, 2024"
              style={{
                width: '100%',
                height: 'auto',
                display: 'block',
                borderRadius: 2,
                filter: 'grayscale(100%) contrast(1.02)',
                border: '1px solid var(--rule)',
              }}
            />
            <figcaption className="mono" style={{
              marginTop: 10, fontSize: 9, color: 'var(--muted-2)',
              textTransform: 'none', letterSpacing: 0,
            }}>
              Piazzale Michelangelo, Firenze · May 2024
            </figcaption>
          </figure>
        </div>
        <div style={{ maxWidth: 720 }}>
          <p style={{
            fontFamily: 'var(--serif)',
            fontSize: 'clamp(22px, 2.4vw, 30px)',
            lineHeight: 1.4,
            fontWeight: 300,
            margin: 0,
            color: 'var(--ink-2)',
            textWrap: 'pretty',
          }}>
            I am most useful on small teams shipping things that have to work — the kind where a bug shows up in someone's bank statement, or in a court filing, or in a Sunday-morning page. I write Go, Rust, and a defensive amount of SQL. I review my own code before anyone else reviews it.
          </p>
          <p style={{
            fontFamily: 'var(--serif)',
            fontSize: 18,
            lineHeight: 1.6,
            fontWeight: 300,
            marginTop: 28,
            color: 'var(--muted)',
            maxWidth: 620,
          }}>
            Outside work: bouldering badly, cooking moderately, reading too much about monetary history. Lived in Buenos Aires, Lisbon, Berlin. Currently the first.
          </p>
        </div>
      </div>
    </div>
  </section>
);

// ─────────── Contact / Footer ───────────
const useFontSet = () => {
  const [fs, setFs] = React.useState(() =>
    (typeof document !== 'undefined' && document.body.dataset.fontset) || 'A'
  );
  React.useEffect(() => {
    const obs = new MutationObserver(() => {
      setFs(document.body.dataset.fontset || 'A');
    });
    obs.observe(document.body, { attributes: true, attributeFilter: ['data-fontset'] });
    return () => obs.disconnect();
  }, []);
  return fs;
};

const ContactFooter = () => {
  const D = window.PF_DATA;
  const fs = useFontSet();
  return (
    <footer id="contact" className="container" style={{
      padding: '100px 96px 60px',
      borderTop: '1px solid var(--rule)',
    }}>
      <div className="rail-3" style={{ alignItems: 'start', gridTemplateColumns: '232px 1fr 1fr' }}>
        <SectionLabel num="07" label="Contact" />
        <div>
          <h3 style={{
            fontFamily: 'var(--serif)',
            fontSize: 'clamp(40px, 5.5vw, 56px)',
            fontWeight: 400,
            letterSpacing: '-0.02em',
            margin: 0,
            lineHeight: 1,
          }}>Say hello.</h3>
          <p style={{
            fontSize: 15, color: 'var(--muted)',
            marginTop: 20, maxWidth: 380,
            lineHeight: 1.5, fontWeight: 300,
          }}>
            Best for: senior IC roles, technical advisory on payments / infra, the occasional long-form writing commission.
          </p>
        </div>
        <div>
          <ContactRow label="Email" value={D.contact.email} />
          <ContactRow label="GitHub" value={D.contact.github} />
          <ContactRow label="LinkedIn" value={D.contact.linkedin} />
          <ContactRow label="Medium" value={D.contact.medium} />
        </div>
      </div>

      <div style={{
        marginTop: 80, paddingTop: 24,
        borderTop: '1px solid var(--rule)',
        display: 'flex', justifyContent: 'space-between',
        flexWrap: 'wrap', gap: 16,
      }}>
        <span className="mono" style={{ fontSize: 10 }}>© 2026 Ramiro Higonet</span>
        <span className="mono" style={{ fontSize: 10, color: 'var(--muted-2)' }}>
          {FONT_SETS[fs]?.credit || FONT_SETS.A.credit} · Built quietly.
        </span>
        <span className="mono" style={{ fontSize: 10 }}>v 4.2 · /colophon</span>
      </div>
    </footer>
  );
};

const ContactRow = ({ label, value }) => (
  <a href="#" style={{
    display: 'grid',
    gridTemplateColumns: '80px 1fr',
    gap: 16, alignItems: 'baseline',
    padding: '14px 0',
    borderBottom: '1px solid var(--rule)',
  }}>
    <span className="mono" style={{ fontSize: 10 }}>{label}</span>
    <span style={{ fontSize: 15, color: 'var(--ink-2)' }}>
      {value}
      <span style={{ marginLeft: 6, color: 'var(--muted)' }}>↗</span>
    </span>
  </a>
);

window.HomePage = Page;
