// Drink detail / calorie calculator

function DrinkDetailScreen({ drink, onBack, onStateChange }) {
  const [size, setSize] = React.useState(1);    // 0 small, 1 medium, 2 large
  const [sweet, setSweet] = React.useState(3);  // 0 无糖, 1 三分, 2 五分, 3 七分, 4 全糖
  const [ice, setIce] = React.useState(2);
  const [toppings, setToppings] = React.useState([INGREDIENT_LIB[0].name]);

  const sizeMul = [0.7, 1, 1.3][size];
  const sweetMul = [0, 0.3, 0.5, 0.7, 1][sweet];

  const baseCal = drink.cal * sizeMul * (0.6 + 0.4 * sweetMul);
  const toppingCal = toppings.reduce((s, n) => {
    const t = INGREDIENT_LIB.find(i => i.name === n);
    return s + (t ? t.cal : 0);
  }, 0);
  const totalCal = Math.round(baseCal + toppingCal);
  const totalSugar = Math.round(drink.sugar * sizeMul * sweetMul +
    toppings.reduce((s, n) => s + (INGREDIENT_LIB.find(i => i.name === n)?.sugar || 0), 0));

  React.useEffect(() => {
    onStateChange && onStateChange({ totalCal, totalSugar });
  }, [totalCal, totalSugar]);

  const sizes = [
    { label: '小杯', sub: '400ml' },
    { label: '中杯', sub: '500ml' },
    { label: '大杯', sub: '700ml' },
  ];
  const sweetLevels = ['无糖', '三分', '五分', '七分', '全糖'];
  const iceLevels = ['热', '去冰', '少冰', '正常', '多冰'];

  const toggleTopping = (name) => {
    setToppings(t => t.includes(name) ? t.filter(x => x !== name) : [...t, name]);
  };

  return (
    <div style={{ paddingBottom: 140, background: 'var(--paper)' }}>
      {/* colored header area */}
      <div style={{
        background: `linear-gradient(180deg, ${drink.colorA} 0%, ${drink.colorA}55 60%, var(--paper) 100%)`,
        paddingBottom: 20,
      }}>
        <MpChrome title={drink.name} showBack onBack={onBack} />

        {/* cup illustration + header info */}
        <div style={{ padding: '8px 16px 0', display: 'flex', alignItems: 'center', gap: 20 }}>
          <div style={{ flexShrink: 0 }}>
            <CupPlaceholder colorA={drink.colorA} colorB={drink.colorB} size={120} />
          </div>
          <div style={{ flex: 1 }}>
            <div style={{ display: 'flex', gap: 4, marginBottom: 6 }}>
              {drink.tags.map(t => (
                <span key={t} className="chip" style={{
                  background: 'rgba(255,255,255,0.7)', color: 'var(--ink-2)',
                }}>{t}</span>
              ))}
            </div>
            <div style={{ fontSize: 20, fontWeight: 700, color: 'var(--ink-1)', marginBottom: 4 }}>
              {drink.name}
            </div>
            <div style={{ fontSize: 12, color: 'var(--ink-2)' }}>
              {drink.brand} · 标准 {drink.size}
            </div>
            <div style={{ marginTop: 10, display: 'flex', alignItems: 'baseline', gap: 6 }}>
              <span style={{ fontSize: 13, color: 'var(--ink-3)' }}>参考价</span>
              <span style={{ fontSize: 20, fontWeight: 700,
                             color: 'var(--ink-1)', fontFamily: 'var(--font-num)' }}>
                ¥{drink.price}
              </span>
            </div>
          </div>
        </div>
      </div>

      {/* live summary — big cal number, ring, sub metrics */}
      <div style={{ margin: '8px 16px 12px', padding: '18px 20px', borderRadius: 24,
                    background: 'var(--surface)', boxShadow: 'var(--sh-2)' }}>
        <CalorieMeter totalCal={totalCal} />
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 8,
                      marginTop: 14, paddingTop: 14,
                      borderTop: '0.5px solid var(--ink-5)' }}>
          <MiniStat label="蛋白质" value={Math.round(8 * sizeMul * 10) / 10} unit="g" color="var(--data-caf)" />
          <MiniStat label="碳水" value={Math.round(drink.cal * sizeMul * 0.12)} unit="g" color="#C78500" />
          <MiniStat label="健康分" value={drink.health} unit="/100" color="var(--data-price)" />
        </div>
      </div>

      {/* Sugar cubes viz */}
      <div style={{ margin: '0 16px 12px' }}>
        <SugarCubes grams={totalSugar} />
      </div>

      {/* Caffeine traffic light */}
      <div style={{ margin: '0 16px 16px' }}>
        <CaffeineTrafficLightCard mg={Math.round(drink.caffeine * sizeMul)} />
      </div>

      {/* controls */}
      <div style={{ padding: '0 16px' }}>
        <Control title="杯型">
          <OptionRow options={sizes} value={size} onChange={setSize} />
        </Control>
        <Control title="甜度">
          <Segmented options={sweetLevels} value={sweet} onChange={setSweet} accent="var(--data-sugar)" />
        </Control>
        <Control title="冰量">
          <Segmented options={iceLevels} value={ice} onChange={setIce} accent="var(--data-caf)" />
        </Control>
        <Control title="加料" subtitle="可多选 · 每份单独计算">
          <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
            {INGREDIENT_LIB.map(t => {
              const on = toppings.includes(t.name);
              return (
                <button key={t.name} onClick={() => toggleTopping(t.name)} style={{
                  display: 'flex', alignItems: 'center', gap: 6,
                  padding: '8px 12px', borderRadius: 999,
                  background: on ? 'var(--brand-yellow)' : 'var(--surface)',
                  color: on ? 'var(--ink-1)' : 'var(--ink-2)',
                  border: on ? '1px solid var(--brand-yellow)' : '1px solid var(--ink-5)',
                  fontSize: 12, fontWeight: on ? 600 : 500,
                  transition: 'all 160ms ease',
                }}>
                  <span>{t.emoji}</span>
                  {t.name}
                  <span style={{ fontSize: 10, opacity: 0.7, fontFamily: 'var(--font-num)' }}>+{t.cal}</span>
                </button>
              );
            })}
          </div>
        </Control>

        {/* ingredient breakdown */}
        <Control title="配料构成" subtitle="标准配方">
          <div style={{ padding: '12px 16px', background: 'var(--surface)', borderRadius: 16, boxShadow: 'var(--sh-1)' }}>
            {/* stacked bar */}
            <div style={{ display: 'flex', height: 10, borderRadius: 999, overflow: 'hidden', marginBottom: 12 }}>
              {drink.ingredients.map((ing, i) => (
                <div key={i} style={{
                  flex: ing.pct,
                  background: ['var(--data-cal)', 'var(--data-sugar)', 'var(--data-caf)',
                              'var(--data-price)', 'var(--data-match)'][i % 5],
                }} />
              ))}
            </div>
            {drink.ingredients.map((ing, i) => (
              <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 10,
                                    padding: '8px 0', borderTop: i ? '0.5px solid var(--ink-5)' : 'none' }}>
                <span style={{ width: 10, height: 10, borderRadius: 3,
                               background: ['var(--data-cal)', 'var(--data-sugar)', 'var(--data-caf)',
                                           'var(--data-price)', 'var(--data-match)'][i % 5] }} />
                <span style={{ fontSize: 13, color: 'var(--ink-1)', fontWeight: 500, flex: 1 }}>
                  {ing.name}
                </span>
                {ing.note && (
                  <span style={{ fontSize: 10, color: 'var(--ink-3)' }}>{ing.note}</span>
                )}
                <span style={{ fontSize: 12, color: 'var(--ink-2)', fontFamily: 'var(--font-num)',
                               minWidth: 36, textAlign: 'right' }}>
                  {ing.pct}%
                </span>
                <span style={{ fontSize: 12, color: 'var(--data-cal)', fontFamily: 'var(--font-num)',
                               fontWeight: 600, minWidth: 40, textAlign: 'right' }}>
                  {ing.cal}
                </span>
              </div>
            ))}
          </div>
        </Control>
      </div>
    </div>
  );
}

// ── Sticky CTA for detail view — rendered at App level so it stays fixed
function DetailStickyCTA({ drink, totalCal, onAdd }) {
  const [added, setAdded] = React.useState(false);
  const handleClick = () => {
    setAdded(true);
    onAdd();
    setTimeout(() => setAdded(false), 1600);
  };
  return (
    <div style={{
      position: 'absolute', bottom: 0, left: 0, right: 0, zIndex: 40,
      pointerEvents: 'none',
    }}>
      <div style={{
        height: 36,
        background: 'linear-gradient(180deg, rgba(254,252,247,0), var(--paper))',
      }} />
      <div style={{
        padding: '4px 16px 28px',
        background: 'var(--paper)',
        pointerEvents: 'auto',
        display: 'flex', gap: 8,
      }}>
        <button style={{
          width: 48, height: 48, borderRadius: 999, background: 'var(--surface)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          boxShadow: 'var(--sh-1)',
        }}>
          <IconBookmark size={20} color="var(--ink-2)" />
        </button>
        <button onClick={handleClick} style={{
          flex: 1, height: 48, borderRadius: 999,
          background: 'var(--ink-1)', color: 'var(--paper)',
          fontSize: 15, fontWeight: 600,
          display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
          boxShadow: 'var(--sh-2)',
        }}>
          {added ? (
            <>
              <IconCheck size={18} color="var(--paper)" />
              已加入今日打卡
            </>
          ) : (
            <>
              加入今日打卡
              <span style={{ fontFamily: 'var(--font-num)', opacity: 0.8 }}>· {totalCal} kcal</span>
            </>
          )}
        </button>
      </div>
    </div>
  );
}

function MiniStat({ label, value, unit, color }) {
  return (
    <div>
      <div style={{ fontSize: 10, color: 'var(--ink-3)' }}>{label}</div>
      <div style={{ display: 'flex', alignItems: 'baseline', gap: 2 }}>
        <span style={{ fontSize: 18, fontWeight: 700, color, fontFamily: 'var(--font-num)' }}>{value}</span>
        {unit && <span style={{ fontSize: 10, color: 'var(--ink-3)' }}>{unit}</span>}
      </div>
    </div>
  );
}

function Control({ title, subtitle, children }) {
  return (
    <div style={{ marginBottom: 20 }}>
      <div style={{ display: 'flex', alignItems: 'baseline', gap: 8, marginBottom: 10 }}>
        <span style={{ fontSize: 14, fontWeight: 600, color: 'var(--ink-1)' }}>{title}</span>
        {subtitle && <span style={{ fontSize: 10, color: 'var(--ink-3)' }}>{subtitle}</span>}
      </div>
      {children}
    </div>
  );
}

function OptionRow({ options, value, onChange }) {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: `repeat(${options.length}, 1fr)`, gap: 8 }}>
      {options.map((o, i) => {
        const on = i === value;
        return (
          <button key={i} onClick={() => onChange(i)} style={{
            padding: '12px 8px', borderRadius: 14,
            background: on ? 'var(--brand-yellow)' : 'var(--surface)',
            border: on ? '1px solid var(--brand-yellow)' : '1px solid var(--ink-5)',
            boxShadow: on ? 'var(--sh-1)' : 'none',
            display: 'flex', flexDirection: 'column', gap: 2, alignItems: 'center',
            transition: 'all 160ms ease',
          }}>
            <span style={{ fontSize: 13, fontWeight: on ? 700 : 600, color: 'var(--ink-1)' }}>
              {o.label}
            </span>
            <span style={{ fontSize: 10, color: on ? 'var(--ink-2)' : 'var(--ink-3)' }}>{o.sub}</span>
          </button>
        );
      })}
    </div>
  );
}

function Segmented({ options, value, onChange, accent = 'var(--brand-yellow)' }) {
  return (
    <div style={{ position: 'relative', background: 'var(--sunken)', borderRadius: 999,
                  padding: 4, display: 'flex', height: 40 }}>
      <div style={{
        position: 'absolute', top: 4, bottom: 4,
        left: `calc(${value * (100 / options.length)}% + 4px)`,
        width: `calc(${100 / options.length}% - 8px)`,
        background: accent, borderRadius: 999,
        transition: 'left 200ms ease',
        boxShadow: 'var(--sh-1)',
      }} />
      {options.map((o, i) => (
        <button key={i} onClick={() => onChange(i)} style={{
          flex: 1, position: 'relative', zIndex: 1, fontSize: 12,
          color: i === value ? 'var(--ink-1)' : 'var(--ink-3)',
          fontWeight: i === value ? 600 : 500,
        }}>{o}</button>
      ))}
    </div>
  );
}

// ── Caffeine traffic light ─────────────────────────────────────
// NOTE: thresholds are placeholders — waiting on client's official grading.
// Swap values in CAFFEINE_THRESHOLDS when spec arrives.
const CAFFEINE_THRESHOLDS = {
  green:  { max: 100, label: '轻咖啡因', sub: '适合全天饮用' },
  yellow: { max: 200, label: '中咖啡因', sub: '建议午后前饮用' },
  red:    { max: Infinity, label: '高咖啡因', sub: '下午 2 点后避免' },
};

function gradeCaffeine(mg) {
  if (mg <= 0) return 'none';
  if (mg <= CAFFEINE_THRESHOLDS.green.max) return 'green';
  if (mg <= CAFFEINE_THRESHOLDS.yellow.max) return 'yellow';
  return 'red';
}

function CaffeineTrafficLight({ mg }) {
  const grade = gradeCaffeine(mg);
  const lights = [
    { id: 'red',    color: '#E06953', on: '#E06953' },
    { id: 'yellow', color: '#E8A33B', on: '#F4B63B' },
    { id: 'green',  color: '#4FB07A', on: '#4FB07A' },
  ];
  const activeLabel = grade === 'none'
    ? { label: '无咖啡因', sub: '任何时间都可饮用', color: 'var(--ink-3)' }
    : { ...CAFFEINE_THRESHOLDS[grade],
        color: grade === 'red' ? '#E06953' : grade === 'yellow' ? '#E8A33B' : '#4FB07A' };

  return (
    <div style={{
      background: 'var(--surface)', borderRadius: 18,
      padding: '14px 16px', display: 'flex', alignItems: 'center', gap: 14,
      boxShadow: 'var(--sh-1)',
    }}>
      {/* label left */}
      <div style={{ fontSize: 11, color: 'var(--ink-2)', fontWeight: 500,
                    writingMode: 'horizontal-tb', flexShrink: 0,
                    lineHeight: 1.3, textAlign: 'center' }}>
        咖啡因<br/>红绿灯
      </div>

      {/* lights capsule */}
      <div style={{
        background: 'var(--surface)', borderRadius: 999,
        padding: '6px 10px', display: 'flex', gap: 8,
        boxShadow: 'inset 0 1px 2px rgba(0,0,0,0.06)', flexShrink: 0,
      }}>
        {lights.map(l => {
          const active = grade === l.id;
          const dim = grade === 'none' || (grade !== l.id);
          return (
            <div key={l.id} style={{
              width: 18, height: 18, borderRadius: 999,
              background: active ? l.on : l.color,
              opacity: active ? 1 : (dim ? 0.15 : 1),
              boxShadow: active
                ? `0 0 0 3px ${l.on}33, 0 1px 2px rgba(0,0,0,0.1)`
                : 'none',
              transition: 'all 240ms ease',
            }} />
          );
        })}
      </div>

      {/* verdict right */}
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 6 }}>
          <span style={{ fontSize: 13, fontWeight: 700, color: activeLabel.color }}>
            {grade === 'none' ? '⚪' :
             grade === 'green' ? '🟢' :
             grade === 'yellow' ? '🟡' : '🔴'}
          </span>
          <span style={{ fontSize: 13, fontWeight: 700, color: 'var(--ink-1)' }}>
            {activeLabel.label}
          </span>
        </div>
        <div style={{ fontSize: 11, color: 'var(--ink-3)', marginTop: 2,
                      fontFamily: 'var(--font-num)' }}>
          咖啡因 ≈ <b style={{ color: 'var(--ink-1)' }}>{mg}</b> mg/杯
          <span style={{ margin: '0 4px', color: 'var(--ink-4)' }}>·</span>
          <span style={{ fontFamily: 'var(--font-sans)' }}>{activeLabel.sub}</span>
        </div>
      </div>
    </div>
  );
}

// ── Calorie meter — burning color gradient by tier ──────────────
// Thresholds: 0-100 绿, 100-200 黄, 200-400 橘, 400+ 红
const CALORIE_TIERS = [
  { max: 100, color: '#4FB07A', label: '低热量',  emoji: '🟢', sub: '轻负担·随便喝' },
  { max: 200, color: '#F4B63B', label: '中热量',  emoji: '🟡', sub: '适量饮用' },
  { max: 400, color: '#F08B3B', label: '高热量',  emoji: '🟠', sub: '建议分享或减量' },
  { max: Infinity, color: '#E05353', label: '超高热量', emoji: '🔴', sub: '约需慢跑 45 分钟' },
];

function gradeCalorie(kcal) {
  return CALORIE_TIERS.find(t => kcal <= t.max);
}

function CalorieMeter({ totalCal }) {
  const tier = gradeCalorie(totalCal);
  const pct = Math.min(1, totalCal / 500);
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 18 }}>
      {/* burning ring */}
      <div style={{ position: 'relative', width: 104, height: 104, flexShrink: 0 }}>
        <svg width="104" height="104" style={{ transform: 'rotate(-90deg)' }}>
          <defs>
            <linearGradient id="burn-grad" x1="0" x2="1" y1="0" y2="1">
              <stop offset="0%" stopColor="#4FB07A" />
              <stop offset="33%" stopColor="#F4B63B" />
              <stop offset="66%" stopColor="#F08B3B" />
              <stop offset="100%" stopColor="#E05353" />
            </linearGradient>
          </defs>
          <circle cx="52" cy="52" r="44" stroke="var(--ink-5)" strokeWidth="9" fill="none" />
          <circle cx="52" cy="52" r="44" stroke="url(#burn-grad)" strokeWidth="9" fill="none"
                  strokeDasharray={2 * Math.PI * 44} strokeLinecap="round"
                  strokeDashoffset={2 * Math.PI * 44 * (1 - pct)}
                  style={{ transition: 'stroke-dashoffset 400ms ease' }} />
        </svg>
        <div style={{ position: 'absolute', inset: 0, display: 'flex',
                      flexDirection: 'column', alignItems: 'center', justifyContent: 'center' }}>
          <div style={{ fontSize: 26, fontWeight: 700, fontFamily: 'var(--font-num)',
                        color: tier.color, lineHeight: 1 }}>{totalCal}</div>
          <div style={{ fontSize: 10, color: 'var(--ink-3)', marginTop: 2 }}>kcal</div>
        </div>
      </div>
      {/* segmented tier bar */}
      <div style={{ flex: 1 }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 6, marginBottom: 8 }}>
          <span style={{ fontSize: 16 }}>{tier.emoji}</span>
          <span style={{ fontSize: 15, fontWeight: 700, color: tier.color }}>{tier.label}</span>
        </div>
        <div style={{ fontSize: 11, color: 'var(--ink-3)', marginBottom: 10, lineHeight: 1.4 }}>
          {tier.sub}
        </div>
        {/* 4-tier bar with indicator */}
        <div style={{ position: 'relative', height: 10, borderRadius: 99,
                      display: 'flex', overflow: 'hidden',
                      background: 'var(--sunken)' }}>
          <div style={{ flex: 1, background: '#4FB07A' }} />
          <div style={{ flex: 1, background: '#F4B63B' }} />
          <div style={{ flex: 2, background: '#F08B3B' }} />
          <div style={{ flex: 2, background: '#E05353' }} />
        </div>
        <div style={{ position: 'relative', height: 12, marginTop: 2 }}>
          <div style={{
            position: 'absolute', top: -4, left: `calc(${Math.min(100, (totalCal / 800) * 100)}% - 6px)`,
            width: 0, height: 0,
            borderLeft: '6px solid transparent', borderRight: '6px solid transparent',
            borderBottom: `7px solid ${tier.color}`,
            transition: 'left 300ms ease',
          }} />
        </div>
        <div style={{ display: 'flex', fontSize: 9, color: 'var(--ink-4)',
                      fontFamily: 'var(--font-num)', marginTop: -4 }}>
          <span style={{ flex: 1 }}>0</span>
          <span style={{ flex: 1, textAlign: 'center' }}>100</span>
          <span style={{ flex: 2, textAlign: 'center' }}>200</span>
          <span style={{ flex: 2, textAlign: 'center' }}>400</span>
          <span style={{ width: 20, textAlign: 'right' }}>800+</span>
        </div>
      </div>
    </div>
  );
}

// ── Sugar cubes — 1 cube = 4.5g sugar ────────────────────────────
const SUGAR_PER_CUBE = 4.5;

function SugarCubes({ grams }) {
  const cubes = grams / SUGAR_PER_CUBE;
  const fullCubes = Math.floor(cubes);
  const hasPartial = cubes - fullCubes > 0.1;
  const partialFill = cubes - fullCubes;
  const display = Math.min(fullCubes + (hasPartial ? 1 : 0), 16);
  const warn = cubes >= 6;

  return (
    <div style={{
      background: 'var(--surface)', borderRadius: 18, padding: '14px 16px',
      boxShadow: 'var(--sh-1)',
    }}>
      <div style={{ display: 'flex', alignItems: 'baseline', gap: 8, marginBottom: 10 }}>
        <span style={{ fontSize: 13, fontWeight: 600, color: 'var(--ink-1)' }}>糖分含量</span>
        <span style={{ fontSize: 10, color: 'var(--ink-3)' }}>
          1 块方糖 ≈ {SUGAR_PER_CUBE}g · WHO 建议每日 ≤ 25g
        </span>
      </div>

      {/* The big highlighted number — inspired by reference */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 14 }}>
        <div style={{
          display: 'inline-flex', alignItems: 'baseline', gap: 4,
          background: warn ? 'var(--data-cal)' : 'var(--brand-yellow)',
          color: warn ? '#fff' : 'var(--ink-1)',
          padding: '6px 14px 6px 10px', borderRadius: 6,
          fontFamily: 'var(--font-num)', fontWeight: 800,
          boxShadow: 'var(--sh-1)',
        }}>
          <span style={{ fontSize: 14, opacity: 0.7 }}>≈</span>
          <span style={{ fontSize: 26, lineHeight: 1 }}>{grams.toFixed(1)}</span>
          <span style={{ fontSize: 14 }}>g</span>
        </div>
        <div style={{ fontSize: 13, color: 'var(--ink-2)', fontFamily: 'var(--font-num)' }}>
          <span style={{ color: 'var(--ink-3)' }}>(</span>
          <b style={{ color: warn ? 'var(--data-cal)' : 'var(--ink-1)', fontSize: 15 }}>
            {cubes.toFixed(1)}
          </b>
          <span style={{ color: 'var(--ink-3)' }}> 块方糖)</span>
        </div>
      </div>

      {/* cubes row */}
      <div style={{ display: 'flex', gap: 4, flexWrap: 'wrap' }}>
        {Array.from({ length: 16 }).map((_, i) => {
          const isFull = i < fullCubes;
          const isPartial = i === fullCubes && hasPartial;
          const isEmpty = i > fullCubes || (i === fullCubes && !hasPartial);
          return (
            <SugarCube key={i}
              fill={isFull ? 1 : isPartial ? partialFill : 0}
              dimmed={isEmpty}
              color={warn && (isFull || isPartial) ? '#E05353' : '#F4B63B'}
            />
          );
        })}
      </div>

      {cubes >= 6 && (
        <div style={{
          marginTop: 10, padding: '8px 10px', borderRadius: 10,
          background: 'var(--data-cal-soft)', display: 'flex', gap: 6, alignItems: 'center',
          fontSize: 11, color: 'var(--data-cal)',
        }}>
          <IconInfo size={13} color="var(--data-cal)" />
          <span>已超过 WHO 建议每日糖分的 <b>{Math.round((grams / 25) * 100)}%</b>，建议减甜</span>
        </div>
      )}
    </div>
  );
}

// 等距 (isometric) 立体方糖 — 三面着色：顶最亮、左中、右最暗。
// fill: 0..1（部分填充用透明度过渡），dimmed: 空槽（灰色），color: 警示色或常规色
function SugarCube({ fill, dimmed, color }) {
  const w = 22, h = 26;
  const isWarn = color === '#E05353';
  const palette = dimmed
    ? { top: '#EFE9DA', left: '#D8D0BB', right: '#B7AC92' }
    : isWarn
      ? { top: '#F4A0A0', left: '#E05353', right: '#B73D3D' }
      : { top: '#FBD976', left: '#F4B63B', right: '#C58E22' };

  // 部分填充：整体透明度从 0.55 渐变到 1
  const partialOpacity = dimmed ? 0.55 : 0.55 + 0.45 * fill;

  return (
    <svg width={w} height={h} viewBox="0 0 22 26"
         style={{ display: 'block', opacity: partialOpacity, transition: 'opacity 300ms ease' }}>
      {/* 顶面 */}
      <polygon points="11,1 21,6 11,11 1,6"
               fill={palette.top}
               stroke="rgba(0,0,0,0.12)" strokeWidth="0.6" strokeLinejoin="round" />
      {/* 左面 */}
      <polygon points="1,6 11,11 11,24 1,19"
               fill={palette.left}
               stroke="rgba(0,0,0,0.12)" strokeWidth="0.6" strokeLinejoin="round" />
      {/* 右面 */}
      <polygon points="21,6 11,11 11,24 21,19"
               fill={palette.right}
               stroke="rgba(0,0,0,0.12)" strokeWidth="0.6" strokeLinejoin="round" />
      {/* 顶面高光 */}
      {!dimmed && (
        <polygon points="11,2.5 14,4 11,5.5 8,4" fill="rgba(255,255,255,0.45)" />
      )}
    </svg>
  );
}

const CaffeineTrafficLightCard = CaffeineTrafficLight;
Object.assign(window, { DrinkDetailScreen, CaffeineTrafficLight, CaffeineTrafficLightCard, DetailStickyCTA, CalorieMeter, SugarCubes });
