:root {
  --bg: #f4f4f8;
  --card: #ffffff;
  --text: #33333b;
  --muted: #707078;
  --line: #e2e2e8;
  --blue: #4d90fe;
  --blue-dark: #3a7be6;
  --red: #e5322d;
  --input-bg: #ffffff;
  --track: #dde3ee;
  --spin-track: #dbe6fb;
  --err-bg: #fff4f4;
  --err-text: #a3221e;
  --disabled: #a9c6f7;
  --shadow: rgba(0,0,0,.05);
  --pdf-btn: #e5322d;
  --pdf-btn-dark: #c92a25;
  --ins-bg: #d6f5dc; --ins-text: #0d5c1f;
  --del-bg: #fcd9d7; --del-text: #8f1d18;
  color-scheme: light;
  font-family: "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); min-height: 100vh; transition: background .2s, color .2s; }

/* ---------- 深夜模式 ---------- */
:root[data-theme="dark"] {
  --bg: #15171c;
  --card: #1f2229;
  --text: #e6e6ec;
  --muted: #9a9ba6;
  --line: #343844;
  --blue: #5b9bff;
  --blue-dark: #78acff;
  --red: #ff6b66;
  --input-bg: #262a33;
  --track: #2e333f;
  --spin-track: #2e3a52;
  --err-bg: #3a1f22;
  --err-text: #ffb4ae;
  --disabled: #3d5580;
  --shadow: rgba(0,0,0,.35);
  --pdf-btn: #d93a35;
  --pdf-btn-dark: #c22f2a;
  --ins-bg: #1d4a2a; --ins-text: #b8f0c4;
  --del-bg: #5a2224; --del-text: #ffc9c5;
  color-scheme: dark;
}
:root[data-theme="dark"] .brand img { filter: brightness(.95); }

.topbar-right { display: flex; align-items: center; gap: 14px; }
.theme-btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px;
  border: 1px solid var(--line); border-radius: 999px; background: var(--bg);
  color: var(--text); font-size: 14px; cursor: pointer; font-family: inherit;
}
.theme-btn:hover { border-color: var(--blue); color: var(--blue); }
[hidden] { display: none !important; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 24px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; color: var(--text); text-decoration: none; }
.badge-local { font-size: 13px; color: var(--muted); }

h1 { font-size: clamp(28px, 4vw, 44px); text-align: center; margin: 40px 0 8px; }
.lead { text-align: center; color: var(--muted); font-size: 18px; margin: 0 auto 32px; max-width: 760px; }

/* ---------- 首頁 ---------- */
.home { max-width: 1100px; margin: 0 auto; padding: 0 16px 60px; }
.categories { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }
.cat { background: var(--card); border-radius: 12px; padding: 20px 24px; box-shadow: 0 1px 3px var(--shadow); }
.cat h2 { margin: 0 0 14px; font-size: 16px; color: var(--muted); font-weight: 600; }
.cat a {
  display: flex; align-items: center; gap: 14px; padding: 9px 8px; margin: 0 -8px;
  border-radius: 8px; color: var(--text); text-decoration: none; font-weight: 600;
}
.cat a:hover { background: var(--bg); color: var(--red); }
.cat a small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }

.icon {
  flex: none; width: 34px; height: 34px; border-radius: 7px; color: #fff;
  display: grid; place-items: center; font-size: 12px; font-weight: 800; letter-spacing: -.5px;
}

/* ---------- 工具頁 ---------- */
.tool { max-width: 860px; margin: 0 auto; padding: 0 16px 60px; }
.back { display: inline-block; margin-top: 20px; color: var(--muted); text-decoration: none; }
.back:hover { color: var(--blue); }

.pick { text-align: center; padding: 10px 0 30px; }
.big-btn {
  display: inline-block; min-width: 320px; padding: 26px 40px; border: 0; border-radius: 14px;
  background: var(--blue); color: #fff; font-size: 26px; font-weight: 700; cursor: pointer;
  text-decoration: none; text-align: center; box-shadow: 0 6px 16px rgba(77,144,254,.35);
  transition: background .15s, transform .1s;
}
.big-btn:hover { background: var(--blue-dark); }
.big-btn:active { transform: scale(.98); }
.big-btn:disabled { background: var(--disabled); cursor: not-allowed; box-shadow: none; }
.hint { color: var(--muted); font-size: 14px; text-align: center; }

.btn {
  padding: 10px 18px; border-radius: 8px; border: 1px solid var(--blue); background: var(--blue);
  color: #fff; font-size: 15px; cursor: pointer;
}
.btn.ghost { background: transparent; color: var(--blue); }
.btn:hover { filter: brightness(.95); }

.url-box { display: flex; gap: 8px; align-items: center; justify-content: center; flex-wrap: wrap; margin-top: 18px; }
.url-box input { width: min(420px, 100%); padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; background: var(--input-bg); color: var(--text); }

.files { display: flex; flex-direction: column; align-items: center; gap: 18px; }
.file-list { list-style: none; padding: 0; margin: 0; width: 100%; display: flex; flex-direction: column; gap: 8px; }
.file-list li {
  display: flex; align-items: center; gap: 12px; background: var(--card); border-radius: 10px;
  padding: 10px 14px; box-shadow: 0 1px 2px var(--shadow);
}
.file-list .thumb { width: 44px; height: 44px; border-radius: 6px; object-fit: cover; background: var(--bg); flex: none; }
.file-list .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-list .size { color: var(--muted); font-size: 13px; }
.file-list button { border: 0; background: none; cursor: pointer; font-size: 16px; color: var(--muted); padding: 4px 6px; border-radius: 6px; }
.file-list button:hover { background: var(--bg); color: var(--text); }

.options {
  width: 100%; background: var(--card); border-radius: 12px; padding: 18px 22px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px 24px;
}
.options:empty { display: none; }
.pick .options { margin-top: 24px; text-align: left; }
.options label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.options select, .options input[type=number] {
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; color: var(--text); background: var(--input-bg);
}
.options .check { flex-direction: row; align-items: center; color: var(--text); }

.busy, .done, .error { text-align: center; padding: 30px 0; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.spinner { width: 48px; height: 48px; border: 5px solid var(--spin-track); border-top-color: var(--blue); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.bar { width: min(420px, 100%); height: 8px; background: var(--track); border-radius: 4px; overflow: hidden; }
#bar-fill { height: 100%; width: 0; background: var(--blue); transition: width .2s; }
.done-title { font-size: 26px; font-weight: 700; margin: 0; }
.errors {
  text-align: left; white-space: pre-wrap; background: var(--err-bg); color: var(--err-text); border-radius: 8px;
  padding: 12px 16px; max-width: 100%; font-family: inherit; font-size: 14px;
}

.drop-overlay {
  position: fixed; inset: 0; background: rgba(77,144,254,.18); border: 4px dashed var(--blue);
  display: grid; place-items: center; font-size: 30px; font-weight: 700; color: var(--blue-dark);
  pointer-events: none; z-index: 10;
}

/* ---------- PDF 工具用紅色按鈕 ---------- */
.pdf-tool .big-btn { background: var(--pdf-btn); box-shadow: 0 6px 16px rgba(229,50,45,.3); }
.pdf-tool .big-btn:hover { background: var(--pdf-btn-dark); }
.pdf-tool .big-btn:disabled { background: var(--disabled); box-shadow: none; }

/* ---------- 選項：文字、顏色、滑桿、檔案 ---------- */
.options input[type=text], .options input[type=password], .options input[type=file] {
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px;
  color: var(--text); background: var(--input-bg); font-family: inherit; min-width: 0;
}
.options input[type=color] { width: 64px; height: 38px; padding: 2px; border: 1px solid var(--line); border-radius: 8px; background: var(--input-bg); }
.options input[type=range] { accent-color: var(--blue); }
.options output { color: var(--text); font-weight: 600; }

/* ---------- 浮水印預覽 ---------- */
.preview:empty { display: none; }
.preview { width: 100%; text-align: center; }
.preview-img { max-width: 100%; max-height: 520px; border-radius: 10px; box-shadow: 0 2px 10px var(--shadow);
  background: repeating-conic-gradient(#ccc 0 25%, #fff 0 50%) 0 0 / 16px 16px; }

/* ---------- 互動編輯器 ---------- */
.editor:empty { display: none; }
.editor { width: 100%; }
.ed-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; background: var(--card); border-radius: 12px; padding: 12px 16px; margin-bottom: 14px; }
.ed-bar .hint { margin: 0; }
.ed-search { display: flex; gap: 6px; flex: 1; min-width: 240px; }
.ed-search input { flex: 1; min-width: 0; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--input-bg); color: var(--text); font-size: 14px; }
.ed-count { font-weight: 700; color: var(--red); }
.ed-tip { margin: 10px 0; }
.ed-pages { display: flex; flex-direction: column; align-items: center; gap: 26px; max-height: 78vh; overflow: auto; padding: 14px 14px 30px; background: var(--line); border-radius: 12px; }
.ed-page { position: relative; width: min(760px, 100%); background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.25); flex: none; }
.ed-page canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.ed-layer { position: absolute; inset: 0; cursor: crosshair; touch-action: none; z-index: 1; }
.ed-num { position: absolute; right: 8px; bottom: -22px; font-size: 12px; color: var(--muted); }

.rd-box { position: absolute; background: rgba(0,0,0,.82); outline: 2px solid #e5322d; }
.rd-box.drawing { background: rgba(229,50,45,.25); outline-style: dashed; }
.rd-box button { position: absolute; top: -10px; right: -10px; width: 22px; height: 22px; border-radius: 50%; border: 0;
  background: #e5322d; color: #fff; font-size: 12px; cursor: pointer; line-height: 22px; padding: 0; }

.sg-maker { background: var(--card); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.sg-tabs { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.sg-tabs > button { padding: 8px 14px; border: 1px solid var(--line); border-radius: 999px; background: var(--bg); color: var(--text); cursor: pointer; font-size: 14px; font-family: inherit; }
.sg-tabs > button.active { background: var(--blue); border-color: var(--blue); color: #fff; }
.sg-colors { margin-left: auto; display: flex; gap: 8px; }
.sg-color { width: 26px; height: 26px; border-radius: 50%; border: 3px solid var(--card); outline: 1px solid var(--line); cursor: pointer; padding: 0; }
.sg-color.active { outline: 2px solid var(--blue); }
.sg-pane { display: flex; flex-direction: column; gap: 10px; align-items: flex-start; }
.sg-pad { width: 100%; aspect-ratio: 10 / 3; background: #fff; border: 2px dashed var(--line); border-radius: 10px; touch-action: none; cursor: crosshair; }
.sg-text { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 16px; background: var(--input-bg); color: var(--text); }
.sg-fonts { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px; width: 100%; }
.sg-font { background: #fff; border: 2px solid var(--line); border-radius: 10px; padding: 10px; font-size: 30px; cursor: pointer; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; display: flex; flex-direction: column; align-items: center; }
.sg-font small { font: 12px system-ui, sans-serif; color: #888; margin-top: 4px; }
.sg-font.active { border-color: var(--blue); }
.sg-up { max-width: 100%; max-height: 200px; background: repeating-conic-gradient(#ddd 0 25%, #fff 0 50%) 0 0 / 14px 14px; border-radius: 8px; }
.sg-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.sg-list { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.sg-list:empty { display: none; }
.sg-item { height: 56px; padding: 6px 10px; background: #fff; border: 2px solid var(--line); border-radius: 8px; cursor: pointer; }
.sg-item.active { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(77,144,254,.25); }
.sg-item img { height: 100%; }
.sg-layer { cursor: copy; }
.sg-placed { position: absolute; outline: 1.5px dashed var(--blue); cursor: move; touch-action: none; }
.sg-placed img { width: 100%; height: 100%; display: block; pointer-events: none; user-select: none; }
.sg-del { position: absolute; top: -11px; right: -11px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: #e5322d; color: #fff; font-size: 12px; cursor: pointer; padding: 0; }
.sg-resize { position: absolute; right: -7px; bottom: -7px; width: 14px; height: 14px; background: var(--blue); border: 2px solid #fff; border-radius: 3px; cursor: nwse-resize; }

/* ---------- 比較結果 ---------- */
.view { padding: 10px 0 0; }
.view-actions { display: flex; gap: 8px; margin-bottom: 12px; }
#tool:has(#step-view:not([hidden])) { max-width: 1200px; }
.compare { display: flex; flex-direction: column; gap: 12px; }
.cmp-tabs { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.cmp-tab { padding: 8px 16px; border: 1px solid var(--line); border-radius: 999px; background: var(--card); color: var(--text); cursor: pointer; font-family: inherit; font-size: 14px; }
.cmp-tab.active { background: var(--blue); color: #fff; border-color: var(--blue); }
.cmp-sum { color: var(--muted); font-size: 14px; }
.cmp-next { margin-left: auto; }
.cmp-text { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.cmp-col { background: var(--card); border-radius: 12px; overflow: hidden; display: flex; flex-direction: column; min-width: 0; }
.cmp-col h3 { margin: 0; padding: 10px 14px; font-size: 14px; border-bottom: 1px solid var(--line); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cmp-body { padding: 12px 16px; height: 70vh; overflow: auto; line-height: 1.75; font-size: 14px; word-break: break-word; }
.cmp-page { margin: 14px 0 6px; padding-top: 6px; border-top: 1px dashed var(--line); font-size: 12px; color: var(--muted); }
.cmp-page:first-child { margin-top: 0; border-top: 0; }
ins { background: var(--ins-bg); color: var(--ins-text); text-decoration: none; border-radius: 3px; padding: 0 1px; }
del { background: var(--del-bg); color: var(--del-text); border-radius: 3px; padding: 0 1px; }
.cmp-focus { outline: 2px solid var(--blue); }
.cmp-pages { display: flex; flex-direction: column; align-items: center; gap: 20px; }
.cmp-pages figure { margin: 0; text-align: center; }
.cmp-pages figcaption { color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.cmp-img { max-width: 100%; box-shadow: 0 2px 10px var(--shadow); }

@media (max-width: 600px) {
  .cmp-text { grid-template-columns: 1fr; }
  .cmp-body { height: 45vh; }
  .big-btn { min-width: 0; width: 100%; font-size: 22px; padding: 20px; }
  .topbar { padding: 10px 16px; }
  .badge-local { display: none; }
}
