/* ============================================================
   PDF 工具箱 — 設計系統
   淺色為預設；深色透過 [data-theme="dark"] 或系統偏好套用。
   ============================================================ */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --ink: #16191d;
  --ink-2: #4b5563;
  --muted: #8b94a1;
  --line: #e4e7ec;
  --line-2: #d3d8e0;

  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --on-accent: #ffffff;

  --danger: #d92d20;
  --danger-soft: #fef3f2;
  --danger-line: #fda29b;
  --warn-soft: #fffaeb;
  --warn-line: #fedf89;
  --warn-ink: #93370d;
  --ok: #067647;
  --ok-soft: #ecfdf3;
  --ok-line: #abefc6;

  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
  --shadow: 0 2px 8px rgba(16, 24, 40, .07), 0 1px 2px rgba(16, 24, 40, .05);
  --shadow-lg: 0 12px 28px rgba(16, 24, 40, .12);

  --r-sm: 8px;
  --r: 12px;
  --r-lg: 16px;

  --maxw: 1120px;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1d212a;
    --ink: #e8eaee;
    --ink-2: #b3bac5;
    --muted: #7d8794;
    --line: #262b34;
    --line-2: #333a45;

    --accent: #7c74ff;
    --accent-hover: #918bff;
    --accent-soft: #1e1f3d;
    --on-accent: #0d0f1a;

    --danger: #ff8a80;
    --danger-soft: #2a1618;
    --danger-line: #5c2b2c;
    --warn-soft: #2a2314;
    --warn-line: #574a24;
    --warn-ink: #f5d48a;
    --ok: #6ee7a0;
    --ok-soft: #10241a;
    --ok-line: #244934;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow: 0 2px 10px rgba(0, 0, 0, .45);
    --shadow-lg: 0 14px 34px rgba(0, 0, 0, .55);

    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #171a20;
  --surface-2: #1d212a;
  --ink: #e8eaee;
  --ink-2: #b3bac5;
  --muted: #7d8794;
  --line: #262b34;
  --line-2: #333a45;

  --accent: #7c74ff;
  --accent-hover: #918bff;
  --accent-soft: #1e1f3d;
  --on-accent: #0d0f1a;

  --danger: #ff8a80;
  --danger-soft: #2a1618;
  --danger-line: #5c2b2c;
  --warn-soft: #2a2314;
  --warn-line: #574a24;
  --warn-ink: #f5d48a;
  --ok: #6ee7a0;
  --ok-soft: #10241a;
  --ok-line: #244934;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow: 0 2px 10px rgba(0, 0, 0, .45);
  --shadow-lg: 0 14px 34px rgba(0, 0, 0, .55);

  color-scheme: dark;
}

* { box-sizing: border-box; }

/* 必須放在所有版面規則之前並加 !important：hidden 屬性只靠瀏覽器預設樣式的
   display:none，任何作者端的 display（本檔的 grid / flex）都會蓋過它。 */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.6 "Inter", "Segoe UI", "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { line-height: 1.25; letter-spacing: -.015em; }

/* ==================== 頂列 ==================== */

.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 0 24px; height: 60px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex; align-items: center; gap: 9px;
  font: inherit; font-size: 16px; font-weight: 640; letter-spacing: -.02em;
  color: var(--ink); background: none; border: 0; padding: 6px 8px 6px 0; cursor: pointer;
}
.brand-mark { width: 21px; height: 21px; fill: var(--accent); }
.brand-mark .fold { fill: color-mix(in srgb, var(--accent) 45%, var(--surface)); }

.topbar-right { display: flex; align-items: center; gap: 12px; }

.privacy {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; color: var(--ink-2);
  background: var(--ok-soft); border: 1px solid var(--ok-line);
  padding: 5px 11px; border-radius: 99px; white-space: nowrap;
}
.privacy svg { width: 13px; height: 13px; fill: var(--ok); flex: none; }
@media (max-width: 620px) { .privacy { display: none; } }

.icon-btn {
  width: 34px; height: 34px; display: grid; place-items: center; cursor: pointer;
  background: var(--surface-2); color: var(--ink);
  border: 1px solid var(--line); border-radius: 9px;
}
.icon-btn:hover { border-color: var(--line-2); }
.theme-icon::before { content: "🌙"; font-size: 15px; line-height: 1; }
:root[data-theme="dark"] .theme-icon::before { content: "☀️"; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-icon::before { content: "☀️"; }
}

/* ==================== 首頁 ==================== */

.home, .workspace { max-width: var(--maxw); margin: 0 auto; padding: 40px 24px 64px; }

.hero { text-align: center; margin-bottom: 40px; }
.hero h1 { margin: 0 0 12px; font-size: clamp(28px, 4.5vw, 40px); }
.hero p {
  margin: 0 auto; max-width: 560px; color: var(--ink-2); font-size: 16px;
}

.cat { margin-bottom: 34px; }
.cat-title {
  margin: 0 0 14px; font-size: 12px; font-weight: 650;
  letter-spacing: .09em; text-transform: uppercase; color: var(--muted);
}

.grid {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fill, minmax(232px, 1fr));
}

.card {
  display: flex; align-items: flex-start; gap: 13px; text-align: left;
  padding: 17px; cursor: pointer; text-decoration: none;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: var(--r);
  box-shadow: var(--shadow-sm);
  transition: transform .14s ease, box-shadow .14s ease, border-color .14s ease;
  font: inherit;
}
.card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
  border-color: color-mix(in srgb, var(--accent) 40%, var(--line));
}
.card:active { transform: translateY(0); }

.card-icon {
  flex: none; width: 40px; height: 40px; border-radius: 10px;
  display: grid; place-items: center; font-size: 19px;
}
.card-body h3 { margin: 1px 0 3px; font-size: 15px; font-weight: 620; }
.card-body p { margin: 0; font-size: 13px; color: var(--ink-2); line-height: 1.5; }

/* 各工具的色票 */
.t-merge    { background: #eef2ff; color: #4f46e5; }
.t-split    { background: #ecfdf5; color: #059669; }
.t-organize { background: #fff7ed; color: #ea580c; }
.t-pagenum  { background: #fdf4ff; color: #a21caf; }
.t-alternate{ background: #eef6ff; color: #1d4ed8; }
.t-nup      { background: #f0f9ff; color: #0369a1; }
.t-pdf2img  { background: #eff6ff; color: #2563eb; }
.t-img2pdf  { background: #f0fdfa; color: #0d9488; }
.t-pdf2txt  { background: #f8fafc; color: #475569; }
.t-watermark{ background: #fef2f2; color: #dc2626; }
.t-metadata { background: #f5f3ff; color: #7c3aed; }
.t-flatten  { background: #fdf2f8; color: #be185d; }
.t-compress { background: #fefce8; color: #ca8a04; }
.t-deblank  { background: #f7fee7; color: #4d7c0f; }
.t-grayscale{ background: #f1f5f9; color: #334155; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .card-icon { background: var(--surface-2); }
}
:root[data-theme="dark"] .card-icon { background: var(--surface-2); }

/* ==================== 工作區 ==================== */

.crumbs { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--muted); margin-bottom: 18px; }
.crumbs .sep { opacity: .5; }
.linkish {
  font: inherit; color: var(--accent); background: none; border: 0; padding: 0; cursor: pointer;
}
.linkish:hover { text-decoration: underline; }

.ws-head { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.ws-icon { flex: none; width: 46px; height: 46px; border-radius: 12px; display: grid; place-items: center; font-size: 22px; }
.ws-head h1 { margin: 0 0 2px; font-size: 24px; }
.ws-head p { margin: 0; color: var(--ink-2); font-size: 14px; }

.note {
  display: flex; gap: 9px; align-items: flex-start;
  background: var(--warn-soft); border: 1px solid var(--warn-line); color: var(--warn-ink);
  padding: 11px 14px; border-radius: var(--r); font-size: 13.5px; margin-bottom: 18px;
}

/* ---- 投放區 ---- */
.drop {
  display: flex; flex-direction: column; align-items: center; gap: 7px;
  padding: 40px 20px; cursor: pointer; text-align: center;
  background: var(--surface);
  border: 2px dashed var(--line-2); border-radius: var(--r-lg);
  transition: border-color .15s, background .15s;
}
.drop:hover { border-color: var(--accent); background: var(--accent-soft); }
.drop.over {
  border-color: var(--accent); background: var(--accent-soft);
  box-shadow: inset 0 0 0 3px color-mix(in srgb, var(--accent) 14%, transparent);
}
.drop-icon { width: 30px; height: 30px; stroke: var(--accent); fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.drop-main { font-size: 15px; }
.drop-main b { color: var(--accent); }
.drop-sub { font-size: 12.5px; color: var(--muted); }

/* ---- 檔案清單 ---- */
.filelist { list-style: none; margin: 16px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.filelist:empty { margin: 0; }

.filerow {
  display: flex; align-items: center; gap: 12px; padding: 11px 13px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r); box-shadow: var(--shadow-sm);
}
.filerow.dragging { opacity: .4; }
.filerow.drag-over { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.filerow .handle { cursor: grab; color: var(--muted); font-size: 15px; line-height: 1; user-select: none; letter-spacing: -2px; }
.filerow .handle:active { cursor: grabbing; }
.filerow .idx {
  flex: none; width: 22px; height: 22px; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
  border-radius: 6px; font-size: 11.5px; font-weight: 650;
}
.filerow .fname { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.filerow .fmeta { flex: none; color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }

/* ---- 選項 ---- */
.options {
  display: grid; gap: 16px 20px; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  margin-top: 20px; padding: 18px 20px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r);
}
.options + .options { margin-top: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field.wide { grid-column: 1 / -1; }
.field label { font-size: 12.5px; font-weight: 600; color: var(--ink-2); }
.field small { font-size: 12px; color: var(--muted); }
.field output { font-weight: 500; color: var(--accent); font-variant-numeric: tabular-nums; }

input[type="text"], input[type="password"], input[type="number"], select {
  font: inherit; font-size: 14px; padding: 8px 11px; width: 100%;
  color: var(--ink); background: var(--surface-2);
  border: 1px solid var(--line-2); border-radius: var(--r-sm);
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 16%, transparent);
}
input[type="color"] { width: 100%; height: 36px; padding: 3px; background: var(--surface-2); border: 1px solid var(--line-2); border-radius: var(--r-sm); cursor: pointer; }
input[type="range"] { width: 100%; accent-color: var(--accent); }

.segmented { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line-2); border-radius: var(--r-sm); padding: 3px; gap: 3px; }
.segmented button {
  flex: 1 1 auto; font: inherit; font-size: 13px; padding: 6px 12px; cursor: pointer; white-space: nowrap;
  background: transparent; color: var(--ink-2); border: 0; border-radius: 6px;
}
.segmented button.on { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: var(--shadow-sm); }

code { background: var(--surface-2); border: 1px solid var(--line); border-radius: 5px; padding: 1px 5px; font-size: 12px; }

/* ---- 浮水印：圖片挑選與效果預覽 ---- */

.wm-imgpick {
  display: flex; align-items: center; gap: 10px; cursor: pointer;
  padding: 9px 12px; font-size: 13.5px; color: var(--ink-2);
  background: var(--surface-2); border: 1px dashed var(--line-2); border-radius: var(--r-sm);
}
.wm-imgpick:hover { border-color: var(--accent); color: var(--accent); }
.wm-imgpick span { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.wm-preview {
  display: flex; align-items: center; justify-content: center;
  min-height: 220px; padding: 14px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--r);
}
.wm-preview canvas {
  max-width: 100%; max-height: 420px; width: auto; height: auto;
  display: block; background: #fff; box-shadow: var(--shadow); border-radius: 3px;
}
.wm-prev-empty { color: var(--muted); font-size: 13px; text-align: center; }
#wm-prevnote { margin-left: 8px; font-weight: 400; }

/* ---- 縮圖 ---- */
.thumb-toolbar { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; margin: 20px 0 14px; }
.grow { flex: 1 1 auto; }
.muted { color: var(--muted); font-size: 13px; }

.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(126px, 1fr)); gap: 12px; }

.thumb {
  position: relative; padding: 8px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r); box-shadow: var(--shadow-sm);
  transition: border-color .12s, box-shadow .12s, opacity .12s;
}
.thumb:hover { border-color: var(--line-2); box-shadow: var(--shadow); }
.thumb.deleted { opacity: .34; }
.thumb.deleted .tcanvas { filter: grayscale(1); }
.thumb.dragging { opacity: .4; }
.thumb.drag-over { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.thumb .tcanvas {
  display: block; width: 100%; height: auto; cursor: grab;
  background: #fff; border-radius: 6px; border: 1px solid var(--line);
}
.thumb .tnum { text-align: center; font-size: 11.5px; color: var(--muted); padding: 6px 0 5px; font-variant-numeric: tabular-nums; }
.thumb .tbar { display: flex; justify-content: center; gap: 4px; }
.thumb .tbar button {
  width: 27px; height: 25px; display: grid; place-items: center;
  font-size: 12px; line-height: 1; cursor: pointer;
  background: var(--surface-2); color: var(--ink-2);
  border: 1px solid var(--line); border-radius: 6px;
}
.thumb .tbar button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.thumb .tbar button:disabled { opacity: .3; cursor: default; }

/* ---- 按鈕 ---- */
.actions { margin-top: 24px; }
.btn {
  font: inherit; font-size: 14px; font-weight: 560; cursor: pointer;
  padding: 8px 16px; border-radius: var(--r-sm);
  border: 1px solid var(--line-2); background: var(--surface); color: var(--ink);
  transition: background .12s, border-color .12s, transform .08s;
}
.btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.ghost { background: transparent; }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--on-accent); }
.btn.big { padding: 12px 30px; font-size: 15px; border-radius: var(--r); }

/* ---- 進度 ---- */
.progress { margin-top: 20px; }
.bar { height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .25s ease; }
.progress-msg { margin-top: 8px; font-size: 13.5px; color: var(--ink-2); }

/* ---- 提示 ---- */
.alert { margin-top: 20px; padding: 13px 15px; border-radius: var(--r); font-size: 14px; white-space: pre-wrap; }
.alert.error { background: var(--danger-soft); border: 1px solid var(--danger-line); color: var(--danger); }

/* ---- 結果 ---- */
.results {
  margin-top: 24px; padding: 20px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg);
  box-shadow: var(--shadow);
}
.results-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.results-head h2 { margin: 0; font-size: 16px; display: flex; align-items: center; gap: 7px; }
.results-head h2::before { content: "✓"; color: var(--ok); font-weight: 700; }
.results-actions { display: flex; gap: 8px; }
.results ul { list-style: none; margin: 16px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.resrow {
  display: flex; align-items: center; gap: 12px; padding: 11px 13px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--r);
}
.resrow .rname { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.resrow .rsize { flex: none; color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }
.resrow a.dl {
  flex: none; padding: 6px 15px; font-size: 13px; font-weight: 580; text-decoration: none;
  background: var(--accent); color: var(--on-accent); border-radius: var(--r-sm);
}
.resrow a.dl:hover { background: var(--accent-hover); }

.chain {
  margin-top: 18px; padding-top: 16px; border-top: 1px dashed var(--line-2);
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  font-size: 13.5px; color: var(--ink-2);
}
.chain #chain-buttons { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---- Toast ---- */
.toasts { position: fixed; right: 18px; bottom: 18px; z-index: 100; display: flex; flex-direction: column; gap: 9px; }
.toast {
  padding: 11px 15px; font-size: 13.5px; max-width: 330px;
  background: var(--surface); border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: var(--r-sm); box-shadow: var(--shadow-lg);
  animation: slide-in .22s ease;
}
.toast.ok { border-left-color: var(--ok); }
.toast.err { border-left-color: var(--danger); }
@keyframes slide-in { from { opacity: 0; transform: translateX(18px); } to { opacity: 1; transform: none; } }

/* ---- 放大預覽 ---- */

.lightbox { position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center; padding: 20px; }
.lb-backdrop { position: absolute; inset: 0; background: rgba(9, 11, 15, .72); backdrop-filter: blur(3px); }

.lb-panel {
  position: relative; display: flex; flex-direction: column;
  width: min(1000px, 100%); max-height: 100%;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-lg); box-shadow: var(--shadow-lg); overflow: hidden;
}

.lb-bar { display: flex; align-items: center; gap: 8px; padding: 11px 14px; border-bottom: 1px solid var(--line); }
.lb-title { font-size: 14px; font-weight: 600; }
.lb-btn {
  width: 32px; height: 30px; display: grid; place-items: center; cursor: pointer; font-size: 14px;
  background: var(--surface-2); color: var(--ink-2);
  border: 1px solid var(--line); border-radius: 7px;
}
.lb-btn:hover { border-color: var(--accent); color: var(--accent); }

.lb-stage {
  flex: 1 1 auto; min-height: 0; overflow: auto; padding: 18px;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface-2);
}
.lb-stage canvas {
  max-width: 100%; height: auto; display: block;
  background: #fff; box-shadow: var(--shadow); border-radius: 3px;
}
.lb-stage .lb-msg { color: var(--muted); font-size: 14px; }

.lb-nav { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 11px 14px; border-top: 1px solid var(--line); }

/* 縮圖：滑過時提示可點開 */
.thumb .tcanvas { cursor: zoom-in; }
.thumb .tzoom {
  position: absolute; top: 12px; right: 12px;
  width: 24px; height: 24px; display: grid; place-items: center;
  font-size: 11px; pointer-events: none; opacity: 0;
  background: rgba(15, 17, 21, .72); color: #fff; border-radius: 6px;
  transition: opacity .12s;
}
.thumb:hover .tzoom { opacity: 1; }

/* ---- 頁尾 ---- */
.foot {
  max-width: var(--maxw); margin: 0 auto; padding: 26px 24px 40px;
  display: flex; justify-content: space-between; gap: 14px; flex-wrap: wrap;
  font-size: 12.5px; color: var(--muted); border-top: 1px solid var(--line);
}
.foot a { color: var(--muted); }

@media (max-width: 560px) {
  .home, .workspace { padding: 26px 16px 48px; }
  .topbar { padding: 0 16px; }
  .options { padding: 16px; grid-template-columns: 1fr; }
  .segmented { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
