GRAY, BL, OR = "#B9B9B4", "#406ED8", "#C8322B"
RD = OR

GCSS = f"""
.gc{{background:rgba(245,245,240,.9);border-radius:34px;padding:30px 50px 34px;box-shadow:0 12px 44px rgba(0,0,0,.35);display:flex;flex-direction:column;align-items:center;max-width:1640px}}
.gb{{position:absolute;left:120px;right:120px;top:96px;bottom:296px;display:flex;flex-direction:column;justify-content:center;align-items:center;text-align:center}}
.gt{{font-size:70px;font-weight:900;margin-bottom:40px;line-height:1.22;text-align:center}}
.gt small{{display:block;font-size:40px;font-weight:700;color:{SUB};margin-top:12px}}
.hb{{display:flex;align-items:center;margin:18px 0;width:1240px}}
.hb .l{{width:380px;font-size:48px;font-weight:700;text-align:right;padding-right:30px;line-height:1.15}}
.hb .l small{{display:block;font-size:32px;color:{SUB}}}
.hb .tr{{flex:1;position:relative;height:110px}}
.hb .bar{{position:absolute;left:0;top:0;bottom:0;border-radius:10px}}
.hb .v{{position:absolute;top:50%;font-size:68px;font-weight:900;white-space:nowrap}}
.box2{{background:#fff;border-radius:24px;padding:34px 18px;text-align:center;font-size:54px;font-weight:900;line-height:1.25;border:6px solid transparent;white-space:nowrap}}
.box2 small{{display:block;font-size:32px;font-weight:700;color:{SUB};margin-top:10px;white-space:normal}}
.arr{{font-size:60px;font-weight:900;color:{SUB};padding:0 6px;white-space:nowrap}}
.chip{{display:inline-block;background:#fff;border-radius:16px;padding:20px 26px;font-size:46px;font-weight:900;margin:10px}}
.hl2{{color:{OR}}} .bl2{{color:{BL}}}
"""


_G_USE = {}


def G(name, inner, title="", sub="", tag=None, ei_pose=None, poses=None, bg=None, ss=None):
    """図・グラフの場面。背景に映像を流し、半透明の板の上に図を描く。中身は画面の中央（x 960）にそろえる
    （2026-11-26 エイイチの指示「テキストが右寄り → センター合わせ」）。板は y 786 までで、左下のハコブくんの区画に入らない"""
    if bg is None:
        bg = next(f for k, f in G_BG if name.startswith(k))
    if ss is None:
        ss = _G_USE.get(bg, 0); _G_USE[bg] = (ss + 4) % 20
    t = f'<div class="gt" data-a="up" data-t="0.05" data-d="0.4">{title}' + (f'<small>{sub}</small>' if sub else '') + '</div>' if title else ''
    html = overlay(f'<style>{GCSS}</style><div class="gb"><div class="gc">{t}{inner}</div></div>')
    if ei_pose:
        html = add(html, ei_wipe(ei_pose, name, poses=poses))
    S[name] = html
    MOTION[name] = dict(type='html', bg=dict(file=bg, ss=ss, dim=0.7, slow=1.0), html=html)


def hbars(rows, maxv, t0=0.4, step=0.6, scale=0.68):
    """横の棒グラフ。rows=[(ラベル, 値, 色, 値の文字, 出す秒 or None)]。棒は左から伸ばす（write）"""
    out = ''
    for k, (lab, v, col, vt, *tt) in enumerate(rows):
        t = tt[0] if tt and tt[0] is not None else round(t0 + k * step, 2)
        w = max(v / maxv * scale * 100, 0.6)
        out += (f'<div class="hb"><div class="l" data-a="fade" data-t="{t}" data-d="0.3">{lab}</div><div class="tr">'
                f'<div class="bar" style="width:{w:.1f}%;background:{col}" data-a="write" data-t="{t}" data-d="0.7"></div>'
                f'<div class="v" style="left:calc({w:.1f}% + 20px);color:{col if col != GRAY else SUB}" data-base="translateY(-50%)" data-a="pop" data-t="{round(t + 0.55, 2)}" data-d="0.35">{vt}</div>'
                '</div></div>')
    return out


def vbars(groups, maxv, series, t0=0.4, step=0.35, h=400, line=None):
    """縦の棒グラフ（場所ごとに2本など）。groups=[(ラベル, [値...])]、series=[(名前, 色)]。line=(値, 文字) で目安の破線"""
    cols = ''
    for g, (lab, vals) in enumerate(groups):
        bars = ''
        for s, v in enumerate(vals):
            col = series[min(s, len(series) - 1)][1]
            if isinstance(v, tuple):
                v, col = v
            t = round(t0 + (g * len(vals) + s) * step, 2)
            hh = v / maxv * h
            bars += (f'<div style="display:flex;flex-direction:column;align-items:center;justify-content:flex-end;width:104px;height:{h + 70}px">'
                     f'<div data-a="pop" data-t="{round(t + 0.4, 2)}" data-d="0.3" style="font-size:42px;font-weight:900;color:{col if col != GRAY else SUB};margin-bottom:6px">{v}</div>'
                     f'<div data-a="up" data-t="{t}" data-d="0.5" style="width:94px;height:{hh:.0f}px;background:{col};border-radius:8px 8px 0 0"></div></div>')
        cols += (f'<div style="display:flex;flex-direction:column;align-items:center;margin:0 18px">'
                 f'<div style="display:flex;align-items:flex-end;gap:8px">{bars}</div>'
                 f'<div style="font-size:44px;font-weight:700;margin-top:12px">{lab}</div></div>')
    ln = ''
    if line:
        y = line[0] / maxv * h
        ln = (f'<div style="position:absolute;left:0;right:0;bottom:{y + 70:.0f}px;border-top:4px dashed {INK}" data-a="fade" data-t="{t0}" data-d="0.4">'
              f'<span style="position:absolute;right:0;top:-56px;font-size:40px;font-weight:900">{line[1]}</span></div>')
    leg = ''.join(f'<span style="display:inline-flex;align-items:center;margin-right:30px"><i style="display:inline-block;width:30px;height:30px;background:{c};border-radius:6px;margin-right:10px"></i>{n}</span>' for n, c in series)
    return (f'<div style="font-size:40px;font-weight:700;margin-bottom:16px">{leg}</div>'
            f'<div style="position:relative;display:flex;align-items:flex-end;border-bottom:4px solid {INK};align-self:flex-start">{ln}{cols}</div>')


def strip(cells, h=92, gap=7, t=0.4, dt=0.05, k=1.35):
    """時間の帯。cells=[(幅, 色, 文字)]。左から順に出す"""
    out = ''
    for i, (w, c, tx) in enumerate(cells):
        out += (f'<div data-a="fade" data-t="{round(t + i * dt, 2)}" data-d="0.2" style="width:{w * k:.0f}px;height:{h}px;background:{c};border-radius:6px;margin-right:{gap}px;'
                f'display:flex;align-items:center;justify-content:center;color:#fff;font-size:38px;font-weight:900;flex:none">{tx}</div>')
    return f'<div style="display:flex">{out}</div>'


def row_lab(lab, inner, w=300):
    return f'<div style="display:flex;align-items:center;margin:26px 0;align-self:stretch"><div style="width:{w}px;font-size:50px;font-weight:900;flex:none">{lab}</div>{inner}</div>'


def flow(boxes, arrows=None, t0=0.3, step=0.7, hi=None):
    """箱を矢印でつなぐ流れ図。boxes=[(文字, 小さい文字, 枠の色 or None, 出す秒 or None)]"""
    out = ''
    for k, (a, b, c, *tt) in enumerate(boxes):
        t = tt[0] if tt and tt[0] is not None else round(t0 + k * step, 2)
        bd = f'border-color:{c}' if c else ''
        sm = f'<small>{b}</small>' if b else ''
        if k:
            ar = (arrows[k - 1] if arrows else '→')
            out += f'<div class="arr" data-a="fade" data-t="{round(t - 0.15, 2)}" data-d="0.3">{ar}</div>'
        out += f'<div class="box2" style="flex:1;{bd}" data-a="pop" data-t="{t}" data-d="0.4">{a}{sm}</div>'
    return f'<div style="display:flex;align-items:center;width:1340px">{out}</div>'


def places(marks, t0=0.4, step=0.35, note=None):
    """5か所の結果（○ ✕）。marks=[(○ か ✕, 色, 出す秒 or None)]"""
    out = ''
    for k, (m, c, *tt) in enumerate(marks):
        t = tt[0] if tt and tt[0] is not None else round(t0 + k * step, 2)
        mk = f'<div style="width:120px;height:120px;border-radius:50%;border:22px solid {c}"></div>' if m == '○' else m
        out += (f'<div style="display:flex;flex-direction:column;align-items:center;margin:0 26px">'
                f'<div data-a="pop" data-t="{t}" data-d="0.4" style="width:210px;height:210px;border-radius:50%;background:#fff;display:flex;align-items:center;justify-content:center;font-size:150px;font-weight:900;color:{c}">{mk}</div>'
                f'<div style="font-size:48px;font-weight:900;margin-top:16px">場所{k + 1}</div></div>')
    nt = f'<div style="font-size:46px;font-weight:700;color:{SUB};margin-top:34px;text-align:center">{note}</div>' if note else ''
    return f'<div style="display:flex;justify-content:center">{out}</div>{nt}'




def dots_n(n, red, t0, dt=0.05, size=78):
    out = ''.join(f'<div data-a="pop" data-t="{round(t0 + k * dt, 2)}" data-d="0.25" style="width:{size}px;height:{size}px;border-radius:50%;background:{OR if k < red else BL};margin:8px"></div>' for k in range(n))
    return f'<div style="display:flex;flex-wrap:wrap;width:{(size + 16) * 11}px;justify-content:center">{out}</div>'


def big(lines, sub=""):
    sb = f'<div style="font-size:50px;font-weight:700;color:{SUB};margin-top:26px">{sub}</div>' if sub else ''
    return f'<div style="text-align:center"><div style="font-size:130px;font-weight:900;line-height:1.2">{"<br>".join(lines)}</div>{sb}</div>'
