// ==========================================================================
// Components / Data Display 家族 — thumbnails + detail pages
// ==========================================================================
const { Demo: __Demo_d, DocCard: __DocCard_d, Bar: __Bar_d, Spinner: __Spinner_d } = window;
const Demo = __Demo_d, DocCard = __DocCard_d, Bar = __Bar_d, Spinner = __Spinner_d;

// ---------- Thumbnails ----------
const T_Avatar = () => (
  <div style={{ display: "flex" }}>
    {["A", "B", "C"].map((l, i) =>
      <div key={l} style={{ width: 40, height: 40, borderRadius: "50%", background: ["var(--blue-500)", "var(--blue-400)", "var(--blue-300)"][i], color: "#fff", display: "flex", alignItems: "center", justifyContent: "center", fontWeight: 600, fontSize: 14, border: "2px solid #fff", marginLeft: i ? -10 : 0 }}>{l}</div>
    )}
  </div>
);
const T_Badge = () => (
  <div style={{ position: "relative", display: "inline-block" }}>
    <div style={{ width: 50, height: 50, background: "var(--blue-100)", border: "1.5px solid var(--blue-500)", borderRadius: 6 }} />
    <span style={{ position: "absolute", top: -4, right: -4, width: 12, height: 12, background: "var(--error)", borderRadius: "50%", border: "2px solid #fff" }} />
  </div>
);
const T_Chip = () => (
  <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
    {[0, 1].map(i =>
      <div key={i} style={{ display: "inline-flex", alignItems: "center", gap: 4, padding: "3px 8px 3px 10px", background: i === 0 ? "var(--blue-500)" : "var(--blue-50)", color: i === 0 ? "#fff" : "var(--blue-600)", borderRadius: 999, fontSize: 11 }}>
        <Bar w={36} h={3} c={i === 0 ? "rgba(255,255,255,.7)" : "var(--blue-400)"} />
        <span style={{ width: 14, height: 14, borderRadius: "50%", background: i === 0 ? "rgba(255,255,255,.3)" : "var(--blue-200)", display: "flex", alignItems: "center", justifyContent: "center", fontSize: 9 }}>×</span>
      </div>
    )}
  </div>
);
const T_Tag = () => (
  <div style={{ display: "flex", gap: 4, padding: 8, background: "#fff", border: "1px solid var(--n-200)", borderRadius: 4, width: 140 }}>
    <div style={{ width: 36, height: 16, background: "var(--blue-500)", borderRadius: 2 }} />
    <div style={{ flex: 1, display: "flex", flexDirection: "column", gap: 3, justifyContent: "center" }}>
      <Bar w="80%" h={3} c="var(--n-300)" />
      <Bar w="60%" h={3} c="var(--n-200)" />
    </div>
  </div>
);
const T_Divider = () => (
  <div style={{ width: 170, display: "flex", flexDirection: "column", gap: 6 }}>
    <Bar w="100%" h={4} c="var(--n-300)" />
    <div style={{ height: 1, background: "var(--blue-500)" }} />
    <Bar w="100%" h={4} c="var(--n-300)" />
    <Bar w="60%" h={4} c="var(--n-300)" />
  </div>
);
const T_Icons = () => (
  <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 6 }}>
    {["home", "heart", "star", "bell", "mail", "user", "cal", "set"].map(n =>
      <Icon key={n} name={n} size={16} color="var(--blue-500)" />
    )}
  </div>
);
const T_List = () => (
  <div style={{ width: 170, background: "#fff", border: "1px solid var(--n-200)", borderRadius: 4, padding: 6, display: "flex", flexDirection: "column", gap: 4 }}>
    <Bar w="40%" h={4} c="var(--blue-500)" />
    <Bar w="80%" h={3} c="var(--n-300)" />
    <Bar w="60%" h={3} c="var(--n-300)" />
    <Bar w="70%" h={3} c="var(--n-300)" />
  </div>
);
const T_Table = () => (
  <div style={{ width: 170, background: "#fff", border: "1px solid var(--n-200)", borderRadius: 4, overflow: "hidden" }}>
    <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 4, padding: 6, background: "var(--blue-500)" }}>
      <Bar w="100%" h={3} c="rgba(255,255,255,.8)" /><Bar w="100%" h={3} c="rgba(255,255,255,.8)" /><Bar w="100%" h={3} c="rgba(255,255,255,.8)" />
    </div>
    {[0, 1].map(r =>
      <div key={r} style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 4, padding: 6, borderTop: "1px solid var(--n-100)" }}>
        <Bar w="80%" h={3} c="var(--n-300)" /><Bar w="60%" h={3} c="var(--n-300)" /><Bar w="70%" h={3} c="var(--n-300)" />
      </div>
    )}
  </div>
);
const T_Tooltip = () => (
  <div style={{ position: "relative", paddingTop: 24 }}>
    <div style={{ padding: "3px 10px", background: "var(--n-900)", color: "#fff", borderRadius: 3, fontSize: 11, display: "inline-block", position: "relative" }}>
      Tooltip
      <span style={{ position: "absolute", top: "100%", left: "50%", transform: "translateX(-50%)", width: 0, height: 0, borderLeft: "4px solid transparent", borderRight: "4px solid transparent", borderTop: "4px solid var(--n-900)" }} />
    </div>
    <div style={{ marginTop: 8, width: 80, height: 6, background: "var(--blue-500)", borderRadius: 3 }} />
  </div>
);
const T_Popover = () => (
  <div style={{ background: "#fff", border: "1px solid var(--blue-500)", borderRadius: 6, padding: 8, width: 140, boxShadow: "var(--shadow-2)", position: "relative" }}>
    <Bar w="40%" h={4} c="var(--blue-500)" />
    <div style={{ marginTop: 6, display: "flex", flexDirection: "column", gap: 3 }}>
      <Bar w="100%" h={3} c="var(--n-300)" /><Bar w="80%" h={3} c="var(--n-300)" />
    </div>
  </div>
);
const T_Typography = () => (
  <div style={{ display: "flex", flexDirection: "column", gap: 2, lineHeight: 1 }}>
    <span style={{ fontFamily: "var(--ff-en)", fontSize: 22, fontWeight: 700, color: "var(--blue-500)" }}>Material UI</span>
    <span style={{ fontFamily: "var(--ff-en)", fontSize: 16, fontWeight: 500, color: "var(--blue-400)" }}>Material UI</span>
    <span style={{ fontFamily: "var(--ff-en)", fontSize: 11, color: "var(--blue-300)" }}>Material UI</span>
  </div>
);
const T_Carousel = () => (
  <div style={{ width: 160, height: 70, background: "#fff", border: "1px solid var(--n-300)", borderRadius: 4, display: "flex", alignItems: "center", justifyContent: "space-between", padding: 6 }}>
    <Icon name="chevL" size={14} color="var(--blue-500)" />
    <div style={{ display: "flex", gap: 3 }}>
      {[0, 1, 2].map(i => <span key={i} style={{ width: i === 0 ? 14 : 5, height: 5, borderRadius: 999, background: i === 0 ? "var(--blue-500)" : "var(--n-300)" }} />)}
    </div>
    <Icon name="chevR" size={14} color="var(--blue-500)" />
  </div>
);
const T_Accordion = () => (
  <div style={{ width: 170, background: "#fff", border: "1px solid var(--blue-500)", borderRadius: 4 }}>
    <div style={{ padding: 6, display: "flex", justifyContent: "space-between", alignItems: "center", borderBottom: "1px solid var(--n-200)" }}>
      <Bar w={70} h={4} c="var(--blue-500)" /><Icon name="chev" size={10} color="var(--blue-500)" />
    </div>
    <div style={{ padding: "6px 6px 6px 16px", display: "flex", flexDirection: "column", gap: 3 }}>
      <Bar w="100%" h={3} c="var(--n-300)" /><Bar w="80%" h={3} c="var(--n-300)" />
    </div>
  </div>
);
const T_Card = () => (
  <div style={{ width: 110, background: "#fff", border: "1px solid var(--n-200)", borderRadius: 4, boxShadow: "var(--shadow-2)", padding: 8 }}>
    <Bar w="70%" h={4} c="var(--blue-500)" />
    <div style={{ marginTop: 6, display: "flex", flexDirection: "column", gap: 3 }}>
      <Bar w="100%" h={3} c="var(--n-300)" /><Bar w="80%" h={3} c="var(--n-300)" /><Bar w="60%" h={3} c="var(--n-300)" />
    </div>
  </div>
);
const T_Paper = () => (
  <div style={{ position: "relative", width: 110, height: 70 }}>
    {[12, 8, 4, 0].map((d, i) =>
      <div key={d} style={{ position: "absolute", top: d, left: d, right: -d, bottom: -d, background: "#fff", border: "1px solid var(--blue-500)", borderRadius: 4, opacity: 0.8 - i * 0.1 }} />
    )}
  </div>
);
const T_Calendar = () => (
  <div style={{ width: 140, padding: 6, background: "#fff", border: "1px solid var(--n-300)", borderRadius: 4 }}>
    <div style={{ display: "grid", gridTemplateColumns: "repeat(7,1fr)", gap: 3 }}>
      {Array.from({ length: 21 }).map((_, i) =>
        <div key={i} style={{ height: 12, background: i === 11 ? "var(--blue-500)" : "var(--n-100)", borderRadius: 2 }} />
      )}
    </div>
  </div>
);
const T_Empty = () => (
  <div style={{ textAlign: "center", color: "var(--n-500)" }}>
    <div style={{ fontFamily: "var(--ff-en)", fontSize: 32, fontWeight: 700, color: "var(--blue-500)", lineHeight: 1 }}>404</div>
    <Bar w={70} h={3} c="var(--n-300)" />
  </div>
);
const T_QRCode = () => (
  <div style={{ display: "grid", gridTemplateColumns: "repeat(7,8px)", gridAutoRows: "8px", gap: 1, background: "#fff", padding: 4, border: "1px solid var(--n-300)" }}>
    {Array.from({ length: 49 }).map((_, i) => {
      const on = [0, 1, 2, 3, 6, 8, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28, 30, 33, 34, 38, 40, 42, 43, 44, 45, 46, 48].includes(i);
      return <div key={i} style={{ background: on ? "var(--n-900)" : "transparent" }} />;
    })}
  </div>
);

// ==========================================================================
// Detail Cards
// ==========================================================================
const DocDisplay = () => (
  <>
    <DocCard id="c-display-avatar" en="Avatar" intro="頭像用來顯示使用者或實體，可放圖片、首字或圖示。常見尺寸 24/32/40/56。">
      <Demo title="尺寸">
        {[24, 32, 40, 56].map(s => <AvatarChar key={s} size={s} color="var(--blue-500)">A</AvatarChar>)}
      </Demo>
      <Demo title="變體">
        <AvatarChar size={40} color="var(--blue-500)">L</AvatarChar>
        <AvatarChar size={40} color="var(--success)">B</AvatarChar>
        <AvatarChar size={40} color="var(--warning)">C</AvatarChar>
        <AvatarChar size={40} color="var(--error)" rounded={6}>D</AvatarChar>
        <AvatarChar size={40} color="var(--n-300)"><Icon name="user" size={20} color="#fff" /></AvatarChar>
      </Demo>
      <Demo title="群組 (Avatar Group)">
        <div style={{ display: "flex" }}>
          {["#2492ff", "#1fa101", "#faad14", "#a855f7", "#e8178a"].map((c, i) => (
            <div key={i} style={{ width: 36, height: 36, borderRadius: "50%", background: c, color: "#fff", display: "flex", alignItems: "center", justifyContent: "center", fontWeight: 600, fontSize: 13, border: "2px solid #fff", marginLeft: i ? -10 : 0 }}>{"ABCDE"[i]}</div>
          ))}
          <div style={{ width: 36, height: 36, borderRadius: "50%", background: "var(--n-200)", color: "var(--n-700)", display: "flex", alignItems: "center", justifyContent: "center", fontWeight: 600, fontSize: 12, border: "2px solid #fff", marginLeft: -10 }}>+12</div>
        </div>
      </Demo>
    </DocCard>

    <DocCard id="c-display-badge" en="Badge" intro="徽章用來標示通知數量、未讀狀態或新功能。通常附在圖示或按鈕的右上角。">
      <Demo title="數字徽章">
        <BadgeOn n="5"><Icon name="bell" size={24} /></BadgeOn>
        <BadgeOn n="9+"><Icon name="mail" size={24} /></BadgeOn>
        <BadgeOn n="99+"><Icon name="user" size={24} /></BadgeOn>
      </Demo>
      <Demo title="圓點徽章">
        <BadgeOn dot><Icon name="bell" size={24} /></BadgeOn>
        <BadgeOn dot color="var(--success)"><Icon name="mail" size={24} /></BadgeOn>
        <BadgeOn dot color="var(--warning)"><Icon name="user" size={24} /></BadgeOn>
      </Demo>
      <Demo title="獨立使用">
        <span className="ds-pill">預設</span>
        <span className="ds-pill ds-pill--success">成功</span>
        <span className="ds-pill ds-pill--warn">警示</span>
        <span className="ds-pill ds-pill--error">錯誤</span>
      </Demo>
    </DocCard>

    <DocCard id="c-display-chip" en="Chip" intro="片狀標籤可代表標籤、選項、人物，支援移除與點擊。比 Tag 更具互動性。">
      <Demo title="基本用法">
        <ChipFn label="Design" />
        <ChipFn label="System" />
        <ChipFn label="UX" />
      </Demo>
      <Demo title="可移除">
        <ChipRemovable />
      </Demo>
      <Demo title="搭配頭像">
        <span style={{ display: "inline-flex", alignItems: "center", gap: 6, padding: "3px 10px 3px 3px", background: "var(--blue-50)", color: "var(--blue-700)", borderRadius: 999 }}>
          <span style={{ width: 22, height: 22, borderRadius: "50%", background: "var(--blue-500)", color: "#fff", display: "flex", alignItems: "center", justifyContent: "center", fontWeight: 600, fontSize: 11 }}>L</span>
          <span style={{ fontSize: 13 }}>林小明</span>
        </span>
      </Demo>
    </DocCard>

    <DocCard id="c-display-tag" en="Tag" intro="輕量級的分類標示，靜態顯示為主。與 Chip 的差異：Tag 較不強調互動。">
      <Demo title="顏色變體">
        <TagSpan c="var(--blue-500)" bg="var(--blue-50)">Default</TagSpan>
        <TagSpan c="var(--success)" bg="var(--success-bg)">Success</TagSpan>
        <TagSpan c="#c97f00" bg="var(--warning-bg)">Warning</TagSpan>
        <TagSpan c="var(--error)" bg="var(--error-bg)">Error</TagSpan>
        <TagSpan c="var(--n-700)" bg="var(--n-100)">Default</TagSpan>
      </Demo>
      <Demo title="外框型 (Outlined)">
        <TagSpan c="var(--blue-500)" border>Blue</TagSpan>
        <TagSpan c="var(--success)" border>Green</TagSpan>
        <TagSpan c="var(--error)" border>Red</TagSpan>
      </Demo>
    </DocCard>

    <DocCard id="c-display-divider" en="Divider" intro="分隔線用於將內容分塊，可以是水平、垂直，並可帶有文字標籤。">
      <Demo title="水平分隔">
        <div style={{ width: "100%", maxWidth: 480, color: "var(--n-700)", fontSize: 13 }}>
          以上為基本資料
          <div style={{ height: 1, background: "var(--n-200)", margin: "16px 0" }} />
          以下為進階設定
        </div>
      </Demo>
      <Demo title="含文字的分隔">
        <div style={{ width: "100%", maxWidth: 480 }}>
          <div style={{ display: "flex", alignItems: "center", gap: 12, color: "var(--n-500)", fontSize: 12 }}>
            <div style={{ flex: 1, height: 1, background: "var(--n-200)" }} />
            或使用社群帳號登入
            <div style={{ flex: 1, height: 1, background: "var(--n-200)" }} />
          </div>
        </div>
      </Demo>
      <Demo title="垂直分隔">
        <div style={{ display: "flex", alignItems: "center", gap: 12, fontSize: 13, color: "var(--n-700)" }}>
          <span>編輯</span><div style={{ width: 1, height: 14, background: "var(--n-300)" }} />
          <span>分享</span><div style={{ width: 1, height: 14, background: "var(--n-300)" }} />
          <span>刪除</span>
        </div>
      </Demo>
    </DocCard>

    <DocCard id="c-display-icons" en="Icons" intro="圖示是視覺溝通的最小單位，搭配文字可加快理解，但不可單獨承載語意（須有 aria-label）。">
      <Demo title="尺寸">
        {[16, 20, 24, 32].map(s => (
          <div key={s} style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 6 }}>
            <Icon name="star" size={s} color="var(--blue-500)" />
            <span className="ds-cap">{s}px</span>
          </div>
        ))}
      </Demo>
      <Demo title="預設圖示集">
        <div style={{ display: "grid", gridTemplateColumns: "repeat(10, 1fr)", gap: 8, width: "100%" }}>
          {["check", "plus", "x", "search", "edit", "trash", "info", "warn", "err", "ok", "bell", "user", "home", "menu", "star", "heart", "set", "upload", "download", "eye", "cal", "mail", "file", "folder", "grid", "list", "filter", "refresh", "lock", "link"].map(n =>
            <div key={n} style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 6, padding: 10, border: "1px solid var(--n-200)", borderRadius: 6, background: "#fff" }}>
              <Icon name={n} size={20} color="var(--n-700)" />
              <span style={{ fontFamily: "var(--ff-mono)", fontSize: 10, color: "var(--n-500)" }}>{n}</span>
            </div>
          )}
        </div>
      </Demo>
    </DocCard>

    <DocCard id="c-display-list" en="Lists" intro="清單用於同類項目並列呈現。可分純文字、附頭像、附操作等多種變體。">
      <Demo title="基本清單"><ListBasic /></Demo>
      <Demo title="附頭像 + 描述"><ListWithAvatar /></Demo>
    </DocCard>

    <DocCard id="c-display-table" en="Table" intro="表格顯示結構化資料，支援排序、篩選、多選、行內操作。">
      <Demo title="基本用法" fitContent><TableBasic /></Demo>
    </DocCard>

    <DocCard id="c-display-tooltip" en="Tooltip" intro="hover 顯示的簡短提示文字，1 行為主。不可放主要資訊，僅作為輔助說明。">
      <Demo title="基本用法">
        <TooltipBtn label="儲存" tip="儲存目前變更 (⌘S)" />
        <TooltipBtn label="刪除" tip="刪除這筆資料" danger />
        <TooltipBtn label="設定" tip="開啟使用者偏好設定" />
      </Demo>
    </DocCard>

    <DocCard id="c-display-popover" en="Popover" intro="點擊或 hover 時彈出的小型卡片，內容比 Tooltip 更豐富，可放清單、表單、操作按鈕。">
      <Demo title="基本用法"><PopoverBasic /></Demo>
    </DocCard>

    <DocCard id="c-display-typography" en="Typography / Text" intro="文字排版元件，提供從 Title 1 ~ Caption 完整的字級階層。詳見 Foundations / Typography。">
      <Demo title="階層範例">
        <div style={{ display: "flex", flexDirection: "column", gap: 6, alignItems: "flex-start" }}>
          <div style={{ fontSize: 36, fontWeight: 700, lineHeight: 1.2 }}>Title 1 / 主頁標題</div>
          <div style={{ fontSize: 30, fontWeight: 600, lineHeight: 1.25 }}>Title 2 / 主頁標題</div>
          <div style={{ fontSize: 20, fontWeight: 600, lineHeight: 1.4 }}>Headline / 區塊標題</div>
          <div style={{ fontSize: 16, lineHeight: 1.5 }}>Body 1 / 一般內文</div>
          <div style={{ fontSize: 14, color: "var(--n-700)", lineHeight: 1.5 }}>Body 2 / 輔助文字</div>
          <div style={{ fontSize: 12, color: "var(--n-500)", lineHeight: 1.5 }}>Caption / 註釋</div>
        </div>
      </Demo>
    </DocCard>

    <DocCard id="c-display-carousel" en="Carousel" intro="輪播展示多張內容，支援自動播放與指示器。建議搭配 hover 時暫停。">
      <Demo title="基本用法"><CarouselDemo /></Demo>
    </DocCard>

    <DocCard id="c-display-accordion" en="Accordion" intro="摺疊面板，用於 FAQ、設定群組、長表單分段。可單開或多開。">
      <Demo title="基本用法">
        <Collapse defaultOpenIndex={0} items={[
          { title: "如何建立新專案？", body: "點擊右上角「新增」按鈕，填寫專案名稱與類型即可建立。建立後預設僅自己可見。" },
          { title: "如何邀請成員？", body: "於專案設定的「成員」分頁中，輸入 email 邀請。" },
          { title: "如何匯出資料？", body: "於列表頁右上角點擊「匯出」按鈕，可選擇 CSV、Excel 或 PDF。" },
        ]} />
      </Demo>
    </DocCard>

    <DocCard id="c-display-card" en="Card" intro="卡片是內容容器，可放統計數據、列表項目、商品資訊。預設 shadow-1，hover 提升為 shadow-3。">
      <Demo title="統計卡片">
        <div style={{ display: "grid", gridTemplateColumns: "repeat(3,1fr)", gap: 14, width: "100%" }}>
          {[
            { l: "資產總數", v: "1,284", d: "+24 本週", c: "var(--success)" },
            { l: "活躍成員", v: "32", d: "+3 本週", c: "var(--success)" },
            { l: "錯誤回報", v: "3", d: "+1 本週", c: "var(--error)" },
          ].map((c, i) => (
            <div key={i} style={{ background: "#fff", border: "1px solid var(--n-200)", borderRadius: 12, padding: 18 }}>
              <div className="ds-cap">{c.l}</div>
              <div style={{ fontSize: 28, fontWeight: 600, margin: "6px 0 4px" }}>{c.v}</div>
              <div style={{ fontSize: 12, color: c.c }}>{c.d}</div>
            </div>
          ))}
        </div>
      </Demo>
      <Demo title="內容卡片">
        <div style={{ display: "grid", gridTemplateColumns: "repeat(2,1fr)", gap: 14, width: "100%" }}>
          {[0, 1].map(i => (
            <div key={i} style={{ background: "#fff", border: "1px solid var(--n-200)", borderRadius: 12, overflow: "hidden" }}>
              <div style={{ height: 100, background: ["linear-gradient(135deg,var(--blue-300),var(--blue-600))", "linear-gradient(135deg,#f5a623,#e8178a)"][i] }} />
              <div style={{ padding: 16 }}>
                <div style={{ fontSize: 15, fontWeight: 600 }}>{["品牌識別系統", "App Onboarding"][i]}</div>
                <div style={{ fontSize: 13, color: "var(--n-600)", marginTop: 4 }}>{["設計團隊正在進行的專案", "重新設計新使用者首次體驗"][i]}</div>
              </div>
            </div>
          ))}
        </div>
      </Demo>
    </DocCard>

    <DocCard id="c-display-paper" en="Paper" intro="Paper 是最基礎的容器層，定義「層級」與「平面感」。Card 是 Paper 的特化版本。">
      <Demo title="陰影層級">
        {[1, 2, 3, 4].map(s => (
          <div key={s} style={{ width: 96, height: 96, background: "#fff", borderRadius: 8, boxShadow: `var(--shadow-${s})`, display: "flex", alignItems: "center", justifyContent: "center", fontFamily: "var(--ff-en)", fontSize: 13, fontWeight: 600, color: "var(--n-700)" }}>Shadow {s}</div>
        ))}
      </Demo>
    </DocCard>

    <DocCard id="c-display-calendar" en="Calendar" intro="日曆視圖，用於行事曆、待辦事項、日程安排。比 DatePicker 範圍更大、可顯示多項事件。">
      <Demo title="月視圖"><CalendarMonth /></Demo>
    </DocCard>

    <DocCard id="c-display-empty" en="Empty" intro="空狀態是新使用者的第一印象。要有「為什麼空」+「下一步動作」。">
      <Demo title="基本用法">
        <EmptyState icon="folder" title="還沒有任何資產" desc="建立第一個專案，開始管理你的資產。" cta="建立專案" />
      </Demo>
      <Demo title="搜尋無結果">
        <EmptyState icon="search" title="找不到符合的結果" desc="試試其他關鍵字，或調整篩選條件。" cta="清除篩選" outline />
      </Demo>
      <Demo title="404 錯誤">
        <div style={{ background: "var(--n-50)", borderRadius: 12, padding: 40, textAlign: "center", width: "100%" }}>
          <div style={{ fontSize: 72, fontWeight: 700, fontFamily: "var(--ff-en)", color: "var(--blue-500)", lineHeight: 1 }}>404</div>
          <div style={{ fontWeight: 600, fontSize: 16, marginTop: 8 }}>找不到此頁面</div>
          <div style={{ fontSize: 13, color: "var(--n-600)", margin: "6px 0 18px" }}>頁面可能已被移除或網址有誤。</div>
          <B kind="filled">回到首頁</B>
        </div>
      </Demo>
    </DocCard>

    <DocCard id="c-display-qrcode" en="QRCode" intro="QR Code 用於連結分享、APP 下載、會員票券等。建議搭配 logo 與下載按鈕。">
      <Demo title="基本用法">
        <div style={{ display: "flex", gap: 24, alignItems: "center" }}>
          <QRBox size={140} />
          <div>
            <div style={{ fontSize: 14, fontWeight: 600, marginBottom: 6 }}>掃描以開啟設計系統</div>
            <div className="ds-cap" style={{ marginBottom: 12 }}>https://example.com/design-system</div>
            <B kind="outline" size="sm" icon="download">下載 QR Code</B>
          </div>
        </div>
      </Demo>
      <Demo title="含中央 Logo">
        <QRBox size={140} withLogo />
      </Demo>
    </DocCard>
  </>
);

// ---------- 小型 demo 元件 ----------
const AvatarChar = ({ size = 40, color, rounded = "50%", children }) => (
  <div style={{ width: size, height: size, borderRadius: rounded, background: color, color: "#fff", display: "inline-flex", alignItems: "center", justifyContent: "center", fontWeight: 600, fontSize: size * 0.4 }}>{children}</div>
);

const BadgeOn = ({ n, dot, color = "var(--error)", children }) => (
  <div style={{ position: "relative", display: "inline-block" }}>
    {children}
    <span style={{
      position: "absolute", top: -4, right: -8,
      minWidth: dot ? 8 : 18, height: dot ? 8 : 18,
      padding: dot ? 0 : "0 5px",
      background: color, color: "#fff", borderRadius: 999, fontSize: 10, fontWeight: 600,
      display: "inline-flex", alignItems: "center", justifyContent: "center",
      border: "2px solid #fff",
    }}>{dot ? null : n}</span>
  </div>
);

const ChipFn = ({ label }) => (
  <span style={{ display: "inline-flex", alignItems: "center", gap: 4, padding: "5px 12px", background: "var(--blue-50)", color: "var(--blue-700)", borderRadius: 999, fontSize: 13, fontWeight: 500 }}>{label}</span>
);

const ChipRemovable = () => {
  const [chips, setChips] = React.useState(["Design", "System", "UX", "Branding"]);
  return (
    <div style={{ display: "flex", flexWrap: "wrap", gap: 6 }}>
      {chips.map(c => (
        <span key={c} style={{ display: "inline-flex", alignItems: "center", gap: 4, padding: "5px 6px 5px 12px", background: "var(--blue-50)", color: "var(--blue-700)", borderRadius: 999, fontSize: 13 }}>
          {c}
          <span onClick={() => setChips(chips.filter(x => x !== c))} style={{ width: 18, height: 18, borderRadius: "50%", background: "var(--blue-200)", display: "inline-flex", alignItems: "center", justifyContent: "center", cursor: "pointer" }}>
            <Icon name="x" size={10} color="var(--blue-700)" />
          </span>
        </span>
      ))}
      {chips.length === 0 && <span className="ds-cap">已全部移除</span>}
    </div>
  );
};

const TagSpan = ({ c, bg, border, children }) => (
  <span style={{
    display: "inline-flex", alignItems: "center", padding: "2px 10px",
    background: border ? "transparent" : bg,
    color: c, borderRadius: 4, fontSize: 12, fontWeight: 500,
    border: border ? `1px solid ${c}` : "1px solid transparent",
  }}>{children}</span>
);

const ListBasic = () => (
  <div style={{ width: 280, background: "#fff", border: "1px solid var(--n-200)", borderRadius: 8, overflow: "hidden" }}>
    {["儀表板", "專案", "成員", "設定"].map((t, i) => (
      <div key={t} style={{ padding: "12px 16px", borderBottom: i < 3 ? "1px solid var(--n-100)" : "none", fontSize: 14, color: "var(--n-800)", display: "flex", alignItems: "center", justifyContent: "space-between", cursor: "pointer" }}>
        {t}<Icon name="chevR" size={14} color="var(--n-400)" />
      </div>
    ))}
  </div>
);

const ListWithAvatar = () => (
  <div style={{ width: 360, background: "#fff", border: "1px solid var(--n-200)", borderRadius: 8, overflow: "hidden" }}>
    {[
      { n: "林小明", e: "ming@example.com", c: "#2492ff" },
      { n: "陳大華", e: "dahua@example.com", c: "#1fa101" },
      { n: "王美玲", e: "meiling@example.com", c: "#e8178a" },
    ].map((u, i) => (
      <div key={u.n} style={{ padding: "12px 16px", borderBottom: i < 2 ? "1px solid var(--n-100)" : "none", display: "flex", alignItems: "center", gap: 12 }}>
        <div style={{ width: 36, height: 36, borderRadius: "50%", background: u.c, color: "#fff", display: "flex", alignItems: "center", justifyContent: "center", fontWeight: 600 }}>{u.n[0]}</div>
        <div style={{ flex: 1 }}>
          <div style={{ fontSize: 14, fontWeight: 500 }}>{u.n}</div>
          <div style={{ fontSize: 12, color: "var(--n-500)" }}>{u.e}</div>
        </div>
        <B kind="outline" size="sm">追蹤</B>
      </div>
    ))}
  </div>
);

const TableBasic = () => (
  <div style={{ width: "100%", border: "1px solid var(--n-200)", borderRadius: 8, overflow: "hidden" }}>
    <table style={{ width: "100%", borderCollapse: "collapse", fontSize: 13 }}>
      <thead>
        <tr style={{ background: "var(--n-900)" }}>
          {["名稱", "狀態", "擁有者", "更新時間"].map(h => (
            <th key={h} style={{ textAlign: "left", padding: "12px 16px", fontSize: 12, fontWeight: 600, color: "#fff", textTransform: "uppercase", letterSpacing: ".04em" }}>{h}</th>
          ))}
        </tr>
      </thead>
      <tbody>
        {[
          ["品牌資產 A.png", "success", "已完成", "Alice", "2 小時前"],
          ["設計稿 V2.fig", "warn", "處理中", "Bob", "昨天"],
          ["合約_2024.pdf", "error", "失敗", "Cathy", "3 天前"],
        ].map((r, i) => (
          <tr key={i} style={{ borderTop: "1px solid var(--n-100)" }}>
            <td style={{ padding: "14px 16px", fontWeight: 500 }}>{r[0]}</td>
            <td style={{ padding: "14px 16px" }}><span className={`ds-pill ds-pill--${r[1]}`}>{r[2]}</span></td>
            <td style={{ padding: "14px 16px", color: "var(--n-700)" }}>{r[3]}</td>
            <td style={{ padding: "14px 16px", color: "var(--n-600)" }}>{r[4]}</td>
          </tr>
        ))}
      </tbody>
    </table>
  </div>
);

const TooltipBtn = ({ label, tip, danger }) => {
  const [show, setShow] = React.useState(false);
  const color = danger ? "var(--error)" : "var(--n-900)";
  return (
    <span style={{ position: "relative" }} onMouseEnter={() => setShow(true)} onMouseLeave={() => setShow(false)}>
      <B kind="outline">{label}</B>
      {show && (
        <span style={{ position: "absolute", bottom: "calc(100% + 8px)", left: "50%", transform: "translateX(-50%)", background: color, color: "#fff", fontSize: 12, padding: "6px 10px", borderRadius: 4, whiteSpace: "nowrap", pointerEvents: "none" }}>
          {tip}
          <span style={{ position: "absolute", top: "100%", left: "50%", transform: "translateX(-50%)", border: "5px solid transparent", borderTopColor: color }} />
        </span>
      )}
    </span>
  );
};

const PopoverBasic = () => {
  const [open, setOpen] = React.useState(false);
  const ref = React.useRef(null);
  React.useEffect(() => {
    const fn = (e) => { if (ref.current && !ref.current.contains(e.target)) setOpen(false); };
    document.addEventListener("mousedown", fn); return () => document.removeEventListener("mousedown", fn);
  }, []);
  return (
    <div ref={ref} style={{ position: "relative" }}>
      <B kind="outline" onClick={() => setOpen(!open)} icon="set">操作選單</B>
      {open && (
        <div className="ds-menu" style={{ position: "absolute", top: "calc(100% + 6px)", left: 0, zIndex: 5 }}>
          <div className="ds-menu__item"><Icon name="edit" size={14} />編輯</div>
          <div className="ds-menu__item"><Icon name="copy" size={14} />複製</div>
          <div className="ds-menu__item"><Icon name="link" size={14} />分享連結</div>
          <div className="ds-menu__sep" />
          <div className="ds-menu__item" style={{ color: "var(--error)" }}>
            <Icon name="trash" size={14} color="var(--error)" />刪除
          </div>
        </div>
      )}
    </div>
  );
};

const CarouselDemo = () => {
  const [idx, setIdx] = React.useState(0);
  const slides = [
    { c: "linear-gradient(135deg,var(--blue-400),var(--blue-700))", t: "歡迎使用設計系統 V1.0" },
    { c: "linear-gradient(135deg,#f59e0b,#e43131)", t: "全新色彩與字級階層" },
    { c: "linear-gradient(135deg,#1fa101,#0ea5e9)", t: "10 大基礎 + 60+ 元件" },
  ];
  return (
    <div style={{ width: "100%", maxWidth: 480 }}>
      <div style={{ height: 160, borderRadius: 8, background: slides[idx].c, color: "#fff", display: "flex", alignItems: "center", justifyContent: "center", fontSize: 18, fontWeight: 600, position: "relative" }}>
        {slides[idx].t}
        <button onClick={() => setIdx((idx - 1 + slides.length) % slides.length)} style={{ position: "absolute", left: 12, top: "50%", transform: "translateY(-50%)", width: 32, height: 32, borderRadius: "50%", background: "rgba(0,0,0,.3)", color: "#fff", border: "none", cursor: "pointer", display: "flex", alignItems: "center", justifyContent: "center" }}>‹</button>
        <button onClick={() => setIdx((idx + 1) % slides.length)} style={{ position: "absolute", right: 12, top: "50%", transform: "translateY(-50%)", width: 32, height: 32, borderRadius: "50%", background: "rgba(0,0,0,.3)", color: "#fff", border: "none", cursor: "pointer", display: "flex", alignItems: "center", justifyContent: "center" }}>›</button>
      </div>
      <div style={{ display: "flex", justifyContent: "center", gap: 6, marginTop: 12 }}>
        {slides.map((_, i) => (
          <span key={i} onClick={() => setIdx(i)} style={{ width: i === idx ? 24 : 8, height: 8, borderRadius: 999, background: i === idx ? "var(--blue-500)" : "var(--n-300)", cursor: "pointer", transition: "all var(--dur-fast) var(--ease)" }} />
        ))}
      </div>
    </div>
  );
};

const CalendarMonth = () => (
  <div style={{ width: 340, padding: 16, background: "#fff", border: "1px solid var(--n-200)", borderRadius: 8 }}>
    <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: 10 }}>
      <Icon name="chevL" size={16} color="var(--n-600)" />
      <div style={{ fontSize: 14, fontWeight: 600 }}>2026 年 4 月</div>
      <Icon name="chevR" size={16} color="var(--n-600)" />
    </div>
    <div style={{ display: "grid", gridTemplateColumns: "repeat(7,1fr)", gap: 2, fontSize: 11, color: "var(--n-500)", textAlign: "center", marginBottom: 4 }}>
      {["日", "一", "二", "三", "四", "五", "六"].map(d => <div key={d}>{d}</div>)}
    </div>
    <div style={{ display: "grid", gridTemplateColumns: "repeat(7,1fr)", gap: 2 }}>
      {Array.from({ length: 30 }, (_, i) => i + 1).map(n => {
        const events = [5, 12, 18, 22, 25].includes(n);
        return (
          <div key={n} style={{ height: 38, padding: 4, borderRadius: 4, cursor: "pointer", background: n === 15 ? "var(--blue-50)" : "transparent", border: n === 15 ? "1px solid var(--blue-500)" : "1px solid transparent" }}>
            <div style={{ fontSize: 13, color: n === 15 ? "var(--blue-600)" : "var(--n-800)", fontWeight: n === 15 ? 600 : 400 }}>{n}</div>
            {events && <div style={{ width: 6, height: 6, borderRadius: "50%", background: "var(--success)", marginTop: 2 }} />}
          </div>
        );
      })}
    </div>
  </div>
);

const EmptyState = ({ icon, title, desc, cta, outline }) => (
  <div style={{ background: outline ? "var(--n-50)" : "var(--n-50)", border: outline ? "1px dashed var(--n-300)" : "none", borderRadius: 12, padding: 40, textAlign: "center", width: "100%" }}>
    <div style={{ width: 56, height: 56, borderRadius: "50%", background: "#fff", margin: "0 auto 14px", display: "flex", alignItems: "center", justifyContent: "center" }}>
      <Icon name={icon} size={28} color="var(--n-500)" />
    </div>
    <div style={{ fontWeight: 600, fontSize: 15 }}>{title}</div>
    <div style={{ fontSize: 13, color: "var(--n-600)", margin: "6px 0 16px" }}>{desc}</div>
    <B kind={outline ? "outline" : "filled"} icon={outline ? null : "plus"}>{cta}</B>
  </div>
);

const QRBox = ({ size = 140, withLogo }) => (
  <div style={{ position: "relative", width: size, height: size, padding: 8, background: "#fff", border: "1px solid var(--n-200)", borderRadius: 8 }}>
    <div style={{ display: "grid", gridTemplateColumns: `repeat(21, 1fr)`, gridAutoRows: "1fr", gap: 1, width: "100%", height: "100%" }}>
      {Array.from({ length: 441 }).map((_, i) => {
        const r = i % 21, c = Math.floor(i / 21);
        const inFinder = (r < 7 && c < 7) || (r > 13 && c < 7) || (r < 7 && c > 13);
        const seed = (r * 31 + c * 17) % 100;
        const on = inFinder ? !((r > 0 && r < 6 && c > 0 && c < 6) && !(r > 1 && r < 5 && c > 1 && c < 5)) : seed > 55;
        return <div key={i} style={{ background: on ? "var(--n-900)" : "transparent" }} />;
      })}
    </div>
    {withLogo && (
      <div style={{ position: "absolute", top: "50%", left: "50%", transform: "translate(-50%,-50%)", width: 32, height: 32, borderRadius: 8, background: "var(--blue-500)", color: "#fff", display: "flex", alignItems: "center", justifyContent: "center", fontFamily: "var(--ff-en)", fontWeight: 700, border: "3px solid #fff" }}>D</div>
    )}
  </div>
);

Object.assign(window, {
  T_Avatar, T_Badge, T_Chip, T_Tag, T_Divider, T_Icons, T_List, T_Table,
  T_Tooltip, T_Popover, T_Typography, T_Carousel, T_Accordion, T_Card,
  T_Paper, T_Calendar, T_Empty, T_QRCode,
  DocDisplay,
});
