/* mobile.jsx — tab bar + five app screens, rendered across three palettes. */

/* ---------- Bottom tab bar ---------- */
window.TabBar = function TabBar({ palette, active, tone = "light" }) {
  const dark = tone === "dark";
  const items = [
    { id: "Home",   icon: "◎" },
    { id: "Barrel", icon: "▣" },
    { id: "Drop",   icon: "◈" },
    { id: "Events", icon: "▷" },
    { id: "Chat",   icon: "◇" },
  ];
  return (
    <div style={{
      position: "sticky", left: 0, right: 0, bottom: 0, zIndex: 5,
      marginTop: "auto",
      display: "flex", justifyContent: "space-around", alignItems: "center",
      padding: "8px 10px 22px",
      background: dark ? "rgba(27,14,8,0.92)" : "rgba(255,255,255,0.94)",
      backdropFilter: "blur(18px)",
      WebkitBackdropFilter: "blur(18px)",
      borderTop: `1px solid ${palette.rule}`,
    }}>
      {items.map(it => {
        const on = active === it.id;
        return (
          <div key={it.id} style={{
            display: "flex", flexDirection: "column", alignItems: "center", gap: 3,
            color: on ? palette.accent : palette.inkMuted,
          }}>
            <span style={{ fontSize: 17 }}>{it.icon}</span>
            <span style={{
              fontFamily: palette.mono, fontSize: 9, letterSpacing: "0.14em",
              textTransform: "uppercase",
            }}>{it.id}</span>
          </div>
        );
      })}
    </div>
  );
};

/* ---------- Helpers ---------- */
function Section({ children, style = {} }) {
  return <div style={{ padding: "0 20px", ...style }}>{children}</div>;
}
function Stat({ k, v, sub, palette }) {
  return (
    <div>
      <div style={{ fontFamily: palette.mono, fontSize: 9, letterSpacing: "0.16em",
        textTransform: "uppercase", color: palette.inkMuted }}>{k}</div>
      <div style={{ fontFamily: palette.display, fontSize: 22, fontWeight: 500,
        letterSpacing: "-0.01em", color: palette.ink, marginTop: 3 }}>{v}</div>
      {sub && <div style={{ fontFamily: palette.mono, fontSize: 10,
        color: palette.inkMuted, marginTop: 2 }}>{sub}</div>}
    </div>
  );
}

/* ---------- Home ---------- */
window.HomeScreen = function HomeScreen({ palette, variant = "editorial" }) {
  const Wordmark = window.Wordmark;
  return (
    <div style={{
      background: palette.bg, color: palette.ink, minHeight: "100%",
      paddingBottom: 100, fontFamily: palette.sans,
    }}>
      {/* Greeting */}
      <Section style={{ paddingTop: 12, paddingBottom: 18,
        display: "flex", alignItems: "center", justifyContent: "space-between" }}>
        <Wordmark palette={palette} size={18} />
        <div style={{
          width: 32, height: 32, borderRadius: 999,
          background: palette.surface, border: `1px solid ${palette.rule}`,
          display: "inline-flex", alignItems: "center", justifyContent: "center",
          fontFamily: palette.display, fontSize: 13, color: palette.ink,
        }}>SR</div>
      </Section>

      <Section>
        <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.18em",
          textTransform: "uppercase", color: palette.accent }}>Good evening, Sam</div>
        <div style={{ fontFamily: palette.display, fontSize: 32, fontWeight: 500,
          letterSpacing: "-0.01em", marginTop: 6, lineHeight: 1.1 }}>
          Your cask turned one yesterday.
        </div>
      </Section>

      {/* Featured cask card */}
      <Section style={{ marginTop: 22 }}>
        <div style={{
          background: variant === "vintage" ? palette.surface : palette.card,
          border: `1px solid ${palette.rule}`,
          borderRadius: palette.radius, padding: 18,
        }}>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start" }}>
            <div>
              <div style={{ fontFamily: palette.mono, fontSize: 9.5, letterSpacing: "0.16em",
                textTransform: "uppercase", color: palette.inkMuted }}>Your barrel</div>
              <div style={{ fontFamily: palette.display, fontSize: 22, fontWeight: 500,
                marginTop: 4, letterSpacing: "-0.01em" }}>No. 0142 · Heaven's Floor</div>
            </div>
            <window.BarrelArt size={60} palette={palette} />
          </div>
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 10,
            borderTop: `1px solid ${palette.rule}`, borderBottom: `1px solid ${palette.rule}`,
            padding: "14px 0", margin: "14px 0" }}>
            <Stat k="Age" v="13 mo" sub="of ~48" palette={palette} />
            <Stat k="Proof" v="121.4°" sub="+2.1" palette={palette} />
            <Stat k="Angels" v="3.8%" sub="YTD" palette={palette} />
          </div>
          <div style={{ fontFamily: palette.mono, fontSize: 9.5, letterSpacing: "0.14em",
            textTransform: "uppercase", color: palette.inkMuted, marginBottom: 6 }}>
            72 / 400 staves sponsored
          </div>
          <div style={{ height: 4, background: "rgba(0,0,0,0.07)", borderRadius: 999 }}>
            <div style={{ height: "100%", width: "18%", background: palette.accent, borderRadius: 999 }} />
          </div>
        </div>
      </Section>

      {/* Today band */}
      <Section style={{ marginTop: 20 }}>
        <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.18em",
          textTransform: "uppercase", color: palette.inkMuted, marginBottom: 10 }}>Today</div>
        {[
          ["Vote open", "Pull 0141 summer or hold", "Ends Thu"],
          ["Journal", "'The angels took 37 gallons'", "2 min"],
          ["Livecam", "Rickhouse 5F · 78° F", "Watching 12"],
        ].map(([k, t, m]) => (
          <div key={t} style={{
            display: "grid", gridTemplateColumns: "72px 1fr auto",
            alignItems: "center", padding: "12px 0",
            borderBottom: `1px solid ${palette.rule}`, gap: 10,
          }}>
            <span style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.14em",
              textTransform: "uppercase", color: palette.accent }}>{k}</span>
            <span style={{ fontFamily: palette.serif, fontSize: 15, fontStyle: "italic" }}>{t}</span>
            <span style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.14em",
              color: palette.inkMuted, textTransform: "uppercase" }}>{m}</span>
          </div>
        ))}
      </Section>

      {/* Next drop strip */}
      <Section style={{ marginTop: 20 }}>
        <div style={{
          background: palette.ink, color: palette.bg, borderRadius: palette.radius,
          padding: 16, display: "flex", justifyContent: "space-between", alignItems: "center",
        }}>
          <div>
            <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.18em",
              textTransform: "uppercase", color: palette.accent }}>Next bottling</div>
            <div style={{ fontFamily: palette.display, fontSize: 18, marginTop: 4 }}>
              Cask 0139 · ships Jun 14
            </div>
          </div>
          <button style={{
            padding: "8px 14px", background: palette.bg, color: palette.ink,
            border: "none", borderRadius: 999, fontSize: 11.5, fontWeight: 500,
            letterSpacing: "0.08em", textTransform: "uppercase", cursor: "pointer",
          }}>Label it</button>
        </div>
      </Section>
    </div>
  );
};

/* ---------- Sponsor a barrel ---------- */
window.BarrelScreen = function BarrelScreen({ palette, variant = "editorial" }) {
  const [qty, setQty] = React.useState(1);
  return (
    <div style={{
      background: palette.bg, color: palette.ink, minHeight: "100%",
      paddingBottom: 120, fontFamily: palette.sans,
    }}>
      {/* Hero */}
      <div style={{
        background: variant === "vintage" ? palette.surface : palette.surface,
        padding: "16px 20px 22px",
      }}>
        <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.18em",
          textTransform: "uppercase", color: palette.accent }}>Sponsor a stave</div>
        <div style={{ fontFamily: palette.display, fontSize: 30, fontWeight: 500,
          marginTop: 6, letterSpacing: "-0.01em", lineHeight: 1.08 }}>
          Cask 0142<br/>
          <span style={{ fontStyle: "italic", fontWeight: 400, color: palette.inkMuted }}>
            Heaven's Floor
          </span>
        </div>
        <div style={{ display: "flex", justifyContent: "center", marginTop: 14 }}>
          <window.BarrelArt size={140} palette={palette} />
        </div>
      </div>

      {/* Live readings */}
      <Section style={{ marginTop: 18 }}>
        <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.18em",
          textTransform: "uppercase", color: palette.inkMuted, marginBottom: 8 }}>Readings · live</div>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 12,
          padding: "12px 0", borderTop: `1px solid ${palette.rule}`,
          borderBottom: `1px solid ${palette.rule}` }}>
          <Stat k="Rickhouse" v="78° F" palette={palette} />
          <Stat k="Proof" v="121.4°" palette={palette} />
          <Stat k="Age" v="13 mo" palette={palette} />
        </div>
      </Section>

      {/* Tasting note */}
      <Section style={{ marginTop: 18 }}>
        <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.18em",
          textTransform: "uppercase", color: palette.inkMuted, marginBottom: 8 }}>Q1 tasting</div>
        <div style={{ fontFamily: palette.serif, fontStyle: "italic",
          fontSize: 16, lineHeight: 1.5, color: palette.ink }}>
          "Tangerine peel, toasted almond, a whisper of leather. Still
          tight on the mid-palate — another summer would carry it well."
        </div>
        <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.14em",
          color: palette.inkMuted, marginTop: 8, textTransform: "uppercase" }}>
          — R. Hollis, master
        </div>
      </Section>

      {/* Stave selector */}
      <Section style={{ marginTop: 22 }}>
        <div style={{
          background: palette.card, border: `1px solid ${palette.rule}`,
          borderRadius: palette.radius, padding: 16,
        }}>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
            <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.16em",
              textTransform: "uppercase", color: palette.inkMuted }}>Staves</div>
            <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
              <button onClick={() => setQty(Math.max(1, qty - 1))} style={{
                width: 28, height: 28, borderRadius: 999, border: `1px solid ${palette.rule}`,
                background: "transparent", color: palette.ink, cursor: "pointer",
              }}>−</button>
              <span style={{ fontFamily: palette.display, fontSize: 22, fontWeight: 500, minWidth: 24, textAlign: "center" }}>{qty}</span>
              <button onClick={() => setQty(qty + 1)} style={{
                width: 28, height: 28, borderRadius: 999, border: `1px solid ${palette.rule}`,
                background: "transparent", color: palette.ink, cursor: "pointer",
              }}>+</button>
            </div>
          </div>
          <div style={{ height: 1, background: palette.rule, margin: "14px 0" }} />
          <div style={{ display: "flex", justifyContent: "space-between", fontSize: 13,
            color: palette.inkMuted, marginBottom: 6 }}>
            <span>{qty} × $72</span><span>${(qty * 72).toFixed(0)}.00</span>
          </div>
          <div style={{ display: "flex", justifyContent: "space-between", fontSize: 13,
            color: palette.inkMuted }}>
            <span>Projected bottles</span><span>{qty * 2}</span>
          </div>
          <button style={{
            width: "100%", marginTop: 16, padding: "14px 16px", background: palette.accent,
            color: variant === "vintage" ? palette.bg : "#FFF", border: "none",
            borderRadius: 999, cursor: "pointer",
            fontFamily: palette.sans, fontSize: 13, letterSpacing: "0.1em",
            textTransform: "uppercase", fontWeight: 500,
          }}>Sponsor {qty} stave{qty > 1 ? "s" : ""}</button>
        </div>
      </Section>

      {/* Scrollable gutter space */}
      <Section style={{ marginTop: 22 }}>
        <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.18em",
          textTransform: "uppercase", color: palette.inkMuted, marginBottom: 10 }}>The charter</div>
        <div style={{ fontFamily: palette.serif, fontSize: 15, color: palette.inkMuted,
          lineHeight: 1.55 }}>
          One stave is one vote and roughly two bottles. Majority rules on pull
          date, cut proof, and label. Bottles ship at maturity, at-cost for members.
        </div>
      </Section>
    </div>
  );
};

/* ---------- Collection ---------- */
window.CollectionScreen = function CollectionScreen({ palette }) {
  const bottles = [
    { n: "No. 0138", name: "Winter Thaw", age: "4y 2m", cut: "108°", status: "DRINKING" },
    { n: "No. 0139", name: "Third Coast", age: "3y 11m", cut: "110°", status: "SHIPS JUN 14" },
    { n: "No. 0141", name: "Paper Mill", age: "3y 5m", cut: "TBD",   status: "VOTE OPEN" },
    { n: "No. 0142", name: "Heaven's Floor", age: "1y 1m", cut: "—", status: "AGING" },
  ];
  return (
    <div style={{
      background: palette.bg, color: palette.ink, minHeight: "100%",
      paddingBottom: 110, fontFamily: palette.sans, paddingTop: 8,
    }}>
      <Section style={{ paddingTop: 14 }}>
        <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.18em",
          textTransform: "uppercase", color: palette.accent }}>My collection</div>
        <div style={{ fontFamily: palette.display, fontSize: 30, fontWeight: 500,
          letterSpacing: "-0.01em", marginTop: 6 }}>4 casks, 19 staves</div>
        <div style={{ fontFamily: palette.serif, fontStyle: "italic", fontSize: 15,
          color: palette.inkMuted, marginTop: 2 }}>≈ 38 bottles pending · since Mar '24</div>
      </Section>

      <Section style={{ marginTop: 18 }}>
        {bottles.map((b, i) => (
          <div key={b.n} style={{
            display: "grid", gridTemplateColumns: "54px 1fr auto",
            gap: 12, alignItems: "center", padding: "14px 0",
            borderTop: i === 0 ? `1px solid ${palette.rule}` : "none",
            borderBottom: `1px solid ${palette.rule}`,
          }}>
            <window.BarrelArt size={54} palette={palette} />
            <div>
              <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.14em",
                color: palette.inkMuted, textTransform: "uppercase" }}>{b.n} · {b.age}</div>
              <div style={{ fontFamily: palette.display, fontSize: 20, fontWeight: 500,
                letterSpacing: "-0.01em", marginTop: 2 }}>{b.name}</div>
              <div style={{ fontFamily: palette.serif, fontSize: 13, color: palette.inkMuted,
                fontStyle: "italic", marginTop: 2 }}>Cut {b.cut}</div>
            </div>
            <div style={{
              padding: "6px 10px", borderRadius: 999, fontFamily: palette.mono,
              fontSize: 9.5, letterSpacing: "0.14em",
              background: b.status === "VOTE OPEN" ? palette.accent : "rgba(0,0,0,0.06)",
              color: b.status === "VOTE OPEN" ? "#FFF" : palette.inkMuted,
            }}>{b.status}</div>
          </div>
        ))}
      </Section>
    </div>
  );
};

/* ---------- Events ---------- */
window.EventsScreen = function EventsScreen({ palette }) {
  const events = [
    { date: "MAY 02", title: "Blind tasting · 0141", place: "Rickhouse 5F, 7pm", seats: "12 left" },
    { date: "MAY 19", title: "Coopers' open house", place: "Barrel yard, all day", seats: "Open" },
    { date: "JUN 14", title: "Bottling party · 0139", place: "Label it yourself", seats: "SOLD OUT" },
    { date: "JUL 03", title: "Summer quarterly pour", place: "Mailed to members", seats: "Auto" },
  ];
  return (
    <div style={{
      background: palette.bg, color: palette.ink, minHeight: "100%",
      paddingBottom: 110, fontFamily: palette.sans,
    }}>
      <Section style={{ paddingTop: 16 }}>
        <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.18em",
          textTransform: "uppercase", color: palette.accent }}>Events</div>
        <div style={{ fontFamily: palette.display, fontSize: 30, fontWeight: 500,
          letterSpacing: "-0.01em", marginTop: 6 }}>Rickhouse calendar</div>
      </Section>
      <Section style={{ marginTop: 16 }}>
        {events.map((e, i) => (
          <div key={e.title} style={{
            display: "grid", gridTemplateColumns: "66px 1fr auto",
            alignItems: "center", gap: 14, padding: "16px 0",
            borderTop: i === 0 ? `1px solid ${palette.rule}` : "none",
            borderBottom: `1px solid ${palette.rule}`,
          }}>
            <div style={{ fontFamily: palette.mono, fontSize: 11, letterSpacing: "0.14em",
              color: palette.accent, textTransform: "uppercase" }}>{e.date}</div>
            <div>
              <div style={{ fontFamily: palette.display, fontSize: 18, fontWeight: 500,
                letterSpacing: "-0.01em" }}>{e.title}</div>
              <div style={{ fontFamily: palette.serif, fontSize: 13, color: palette.inkMuted,
                fontStyle: "italic", marginTop: 2 }}>{e.place}</div>
            </div>
            <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.14em",
              color: e.seats === "SOLD OUT" ? palette.inkMuted : palette.ink,
              textTransform: "uppercase" }}>{e.seats}</div>
          </div>
        ))}
      </Section>
    </div>
  );
};

/* ---------- Chat / Clubhouse ---------- */
window.ChatScreen = function ChatScreen({ palette }) {
  const msgs = [
    { who: "Hollis", role: "master", text: "Pulled Q1 sample from 0142 this morning. Tangerine's louder than I'd have guessed." },
    { who: "Sam R.", role: "stave × 4", text: "Voting hold. Another summer and that mid-palate relaxes." },
    { who: "Maren", role: "stave × 2", text: "Agree, but only if we can get an August temp reading before we commit." },
    { who: "W.P.",   role: "cooper", text: "North side floor is running a touch hot — 80°F at noon. I'll post a chart." },
    { who: "Sam R.", role: "stave × 4", text: "Perfect. Pass that along to R." },
  ];
  return (
    <div style={{
      background: palette.bg, color: palette.ink, minHeight: "100%",
      paddingBottom: 110, fontFamily: palette.sans, display: "flex",
      flexDirection: "column",
    }}>
      <Section style={{ paddingTop: 14, paddingBottom: 12,
        borderBottom: `1px solid ${palette.rule}` }}>
        <div style={{ fontFamily: palette.mono, fontSize: 10, letterSpacing: "0.18em",
          textTransform: "uppercase", color: palette.accent }}>Clubhouse · #0142</div>
        <div style={{ fontFamily: palette.display, fontSize: 22, fontWeight: 500,
          marginTop: 4, letterSpacing: "-0.01em" }}>Heaven's Floor</div>
      </Section>

      <Section style={{ flex: 1, paddingTop: 14, display: "flex", flexDirection: "column", gap: 16 }}>
        {msgs.map((m, i) => (
          <div key={i} style={{
            display: "flex", gap: 10, alignItems: "flex-start",
          }}>
            <div style={{
              width: 28, height: 28, borderRadius: 999,
              background: palette.surface, border: `1px solid ${palette.rule}`,
              display: "inline-flex", alignItems: "center", justifyContent: "center",
              fontFamily: palette.display, fontSize: 11, color: palette.ink, flexShrink: 0,
            }}>{m.who.slice(0, 1)}</div>
            <div style={{ flex: 1 }}>
              <div style={{ display: "flex", alignItems: "baseline", gap: 6 }}>
                <span style={{ fontFamily: palette.display, fontSize: 14, fontWeight: 500 }}>{m.who}</span>
                <span style={{ fontFamily: palette.mono, fontSize: 9, letterSpacing: "0.14em",
                  color: palette.inkMuted, textTransform: "uppercase" }}>{m.role}</span>
              </div>
              <div style={{ fontFamily: palette.serif, fontSize: 15, lineHeight: 1.5,
                color: palette.ink, marginTop: 3 }}>{m.text}</div>
            </div>
          </div>
        ))}
      </Section>

      <div style={{ padding: "12px 16px 20px", borderTop: `1px solid ${palette.rule}` }}>
        <div style={{
          display: "flex", alignItems: "center", gap: 10,
          background: palette.surface, border: `1px solid ${palette.rule}`,
          borderRadius: 999, padding: "10px 14px",
        }}>
          <span style={{ color: palette.inkMuted, fontSize: 14 }}>✎</span>
          <span style={{ fontFamily: palette.serif, fontSize: 14, fontStyle: "italic",
            color: palette.inkMuted }}>Say something considered…</span>
        </div>
      </div>
    </div>
  );
};

