:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #fbfcfd;
  --border: #e6e9ee;
  --text: #1c2430;
  --text-dim: #667085;
  --text-faint: #98a2b3;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --accent-2: #0ea5a4;
  --ok: #16a34a;
  --warn: #d97706;
  --err: #dc2626;
  --shadow: 0 1px 2px rgba(16,24,40,.04), 0 8px 24px rgba(16,24,40,.06);
  --radius: 14px;
  --radius-sm: 9px;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", "Consolas", monospace;
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---------- 版面 ---------- */
.app { display: flex; flex-direction: column; height: 100vh; }

header.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .2px; }
.brand .logo {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid; place-items: center; color: #fff; font-size: 16px;
}
.brand small { font-weight: 500; color: var(--text-faint); font-size: 12px; }
.spacer { flex: 1; }

.conn {
  display: flex; align-items: center; gap: 8px;
  font-size: 12.5px; color: var(--text-dim);
  padding: 6px 12px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--panel-2);
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-faint); box-shadow: 0 0 0 3px rgba(0,0,0,.03); }
.dot.on { background: var(--ok); box-shadow: 0 0 0 3px rgba(22,163,74,.15); }
.dot.off { background: var(--err); box-shadow: 0 0 0 3px rgba(220,38,38,.12); }

.main { flex: 1; display: grid; grid-template-columns: minmax(360px, 460px) 1fr; min-height: 0; }
.left { border-right: 1px solid var(--border); background: var(--panel); overflow-y: auto; }
.right { overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 18px; }

/* ---------- 模式分頁 ---------- */
.tabs { display: flex; flex-wrap: wrap; gap: 6px; padding: 14px 16px 0; }
.tab {
  border: 1px solid var(--border); background: var(--panel-2);
  padding: 7px 13px; border-radius: 999px; cursor: pointer;
  font-size: 13px; color: var(--text-dim); transition: .15s;
}
.tab:hover { border-color: #cfd6e0; color: var(--text); }
.tab.active { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

.form { padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.mode-desc { font-size: 12.5px; color: var(--text-faint); margin: -4px 2px 0; }

.field { display: flex; flex-direction: column; gap: 7px; }
.field > label { font-weight: 600; font-size: 12.5px; color: var(--text); display: flex; align-items: center; gap: 8px; }
.field .hint { color: var(--text-faint); font-weight: 400; font-size: 11.5px; }

textarea, input[type=text], input[type=number], select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: #fff; color: var(--text);
  font-size: 13.5px; font-family: inherit; outline: none; transition: .15s;
}
textarea { resize: vertical; min-height: 92px; line-height: 1.55; }
textarea:focus, input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.row { display: flex; gap: 10px; }
.row > * { flex: 1; }

.seed-row { display: flex; gap: 8px; align-items: stretch; }
.seed-row input { flex: 1; font-family: var(--mono); }
.btn-mini {
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text-dim);
  border-radius: var(--radius-sm); padding: 0 12px; cursor: pointer; font-size: 12.5px; white-space: nowrap;
}
.btn-mini:hover { border-color: #cfd6e0; color: var(--text); }
.chk { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--text-dim); font-weight: 400; }

/* ---------- 圖片上傳 ---------- */
.uploads { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.drop {
  position: relative; aspect-ratio: 1; border: 1.5px dashed #cfd6e0; border-radius: var(--radius-sm);
  background: var(--panel-2); display: grid; place-items: center; cursor: pointer; overflow: hidden;
  color: var(--text-faint); font-size: 12px; text-align: center; transition: .15s;
}
.drop:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.drop.has-img { border-style: solid; border-color: var(--border); }
.drop img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.drop .tag { position: absolute; top: 6px; left: 6px; background: rgba(28,36,48,.72); color: #fff; font-size: 10.5px; padding: 2px 7px; border-radius: 6px; }
.drop .clear { position: absolute; top: 6px; right: 6px; background: rgba(28,36,48,.72); color:#fff; border:none; width:20px;height:20px;border-radius:6px;cursor:pointer; display:none; }
.drop.has-img .clear { display: block; }

/* ---------- 遮罩畫布 ---------- */
.mask-wrap { display: flex; flex-direction: column; gap: 8px; }
.mask-stage { position: relative; border: 1px solid var(--border); border-radius: var(--radius-sm); background: #eef1f5; overflow: hidden; display: grid; place-items: center; min-height: 120px; }
.mask-stage canvas { display: block; max-width: 100%; touch-action: none; cursor: crosshair; }
.mask-tools { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--text-dim); }
.mask-tools input[type=range] { flex: 1; min-width: 90px; }

/* ---------- 按鈕 ---------- */
.actions { display: flex; gap: 10px; position: sticky; bottom: 0; padding-top: 6px; }
.btn-run {
  flex: 1; border: none; background: linear-gradient(135deg, var(--accent), #6d5cf0);
  color: #fff; font-weight: 700; font-size: 15px; padding: 13px; border-radius: var(--radius-sm);
  cursor: pointer; box-shadow: 0 6px 16px rgba(79,70,229,.28); transition: .15s;
}
.btn-run:hover { filter: brightness(1.05); transform: translateY(-1px); }
.btn-run:disabled { opacity: .5; cursor: not-allowed; box-shadow: none; transform: none; }
.btn-ghost { border: 1px solid var(--border); background: var(--panel); color: var(--text-dim); border-radius: var(--radius-sm); padding: 0 16px; cursor: pointer; font-size: 13px; }
.btn-ghost:hover { color: var(--err); border-color: #f1c4c4; }

/* ---------- 右側：即時面板 ---------- */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card .card-head { padding: 13px 16px; border-bottom: 1px solid var(--border); font-weight: 700; font-size: 13px; display: flex; align-items: center; gap: 8px; }
.card .card-body { padding: 16px; }

.progress-top { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 10px; }
.progress-pct { font-size: 30px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -.5px; }
.progress-stage { font-size: 12.5px; color: var(--text-dim); }
.bar { height: 12px; background: #eef1f5; border-radius: 999px; overflow: hidden; }
.bar > i { display: block; height: 100%; width: 0; border-radius: 999px; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width .2s ease; }

.metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 14px; }
.metric { background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 11px 12px; }
.metric .k { font-size: 11px; color: var(--text-faint); }
.metric .v { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.metric .v small { font-size: 12px; color: var(--text-dim); font-weight: 500; }

.preview-live { margin: 14px auto 0; width: fit-content; max-width: 100%; border-radius: var(--radius-sm); overflow: hidden; border: 1px solid var(--border); background: repeating-conic-gradient(#f0f2f5 0% 25%, #fafbfc 0% 50%) 50% / 20px 20px; display: none; }
.preview-live.on { display: block; }
.preview-live img { display: block; max-width: 100%; max-height: 320px; width: auto; height: auto; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.gallery:empty::after { content: "尚無結果，按下「生成」後成品會出現在這裡。"; color: var(--text-faint); font-size: 13px; grid-column: 1/-1; padding: 24px; text-align: center; }
.result { border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; background: var(--panel-2); position: relative; }
.result img { display: block; width: 100%; cursor: zoom-in; }
.result a.dl { position: absolute; bottom: 6px; right: 6px; background: rgba(28,36,48,.75); color: #fff; text-decoration: none; font-size: 11px; padding: 3px 8px; border-radius: 6px; }

/* ---------- log ---------- */
.log { font-family: var(--mono); font-size: 11.5px; color: var(--text-dim); max-height: 150px; overflow-y: auto; white-space: pre-wrap; }
.log .l-err { color: var(--err); }
.log .l-ok { color: var(--ok); }
.log .l-warn { color: var(--warn); }
.log .t { color: var(--text-faint); }

/* ---------- lightbox ---------- */
.lightbox { position: fixed; inset: 0; background: rgba(12,16,22,.82); display: none; place-items: center; z-index: 50; padding: 30px; }
.lightbox.on { display: grid; }
.lightbox img { max-width: 96vw; max-height: 92vh; border-radius: 8px; box-shadow: 0 20px 60px rgba(0,0,0,.5); }

.toggle-inline { display:flex; align-items:center; gap:14px; flex-wrap: wrap; font-size:12.5px; color: var(--text-dim); }
.toggle-inline label { display:flex; align-items:center; gap:6px; cursor:pointer; }

@media (max-width: 900px) {
  .main { grid-template-columns: 1fr; }
  .left { border-right: none; border-bottom: 1px solid var(--border); }
}

/* ---------- 手機 ---------- */
@media (max-width: 680px) {
  /* 整頁自然捲動，不要左右欄各自捲動 */
  .app { height: auto; min-height: 100vh; }
  .main { display: block; min-height: 0; }
  .left, .right { overflow: visible; }
  .right { padding: 14px; gap: 14px; }

  /* 頂部列：第一行 品牌 + 連線狀態；第二行 三個開關平均分佈 */
  header.topbar { flex-wrap: wrap; gap: 10px; padding: 10px 14px; }
  .brand { font-size: 14px; }
  .brand small { display: none; }
  .spacer { display: none; }
  .conn { order: 2; margin-left: auto; }
  .toggle-inline { order: 3; width: 100%; justify-content: space-between; gap: 8px; font-size: 12px; }

  .form { padding: 14px; gap: 14px; }
  .tabs { padding: 12px 14px 0; }
  .tab { padding: 8px 13px; }               /* 觸控目標大一點 */
  .actions { position: static; }             /* 手機不要 sticky，避免蓋到內容 */

  .progress-pct { font-size: 26px; }
  .metrics { gap: 8px; }
  .metric { padding: 9px 10px; }
  .metric .v { font-size: 17px; }
  .preview-live img { max-height: 260px; }
}
