:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e2e5ef;
  --text: #171a2b;
  --muted: #646b85;
  --accent: #4f46e5;
  --accent-2: #6366f1;
  --accent-soft: #eef0ff;
  --danger: #dc2626;
  --success: #16a34a;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 8px 24px -12px rgba(16, 24, 40, .15);
  --radius: 14px;
  --c-pdf: #e5484d;
  --c-convert: #0b8a6b;
  --c-image: #8e4ec6;
  --c-secure: #d97706;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f111a; --surface: #171a26; --surface-2: #1f2333; --border: #2a2f44;
    --text: #e8eaf4; --muted: #9aa1bd; --accent: #818cf8; --accent-2: #a5b4fc; --accent-soft: #23264a;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px -12px rgba(0,0,0,.6);
    --c-pdf: #ff6369; --c-convert: #3dd68c; --c-image: #bf7af0; --c-secure: #f5a524;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f111a; --surface: #171a26; --surface-2: #1f2333; --border: #2a2f44;
  --text: #e8eaf4; --muted: #9aa1bd; --accent: #818cf8; --accent-2: #a5b4fc; --accent-soft: #23264a;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px -12px rgba(0,0,0,.6);
  --c-pdf: #ff6369; --c-convert: #3dd68c; --c-image: #bf7af0; --c-secure: #f5a524;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--text); line-height: 1.5;
  min-height: 100vh; display: flex; flex-direction: column;
}
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 24px;
  padding: 12px max(16px, calc((100% - 1160px) / 2));
  background: color-mix(in srgb, var(--surface) 85%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.15rem; color: var(--text); text-decoration: none; }
.brand-logo { width: 32px; height: 32px; color: var(--accent); display: inline-flex; }
.brand-logo svg { width: 100%; height: 100%; }
.topnav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.topnav a { padding: 6px 12px; border-radius: 8px; color: var(--muted); text-decoration: none; font-weight: 500; white-space: nowrap; }
.topnav a:hover, .topnav a.active { background: var(--surface-2); color: var(--text); }
.icon-btn { background: none; border: 1px solid var(--border); border-radius: 10px; width: 38px; height: 38px; display: grid; place-items: center; cursor: pointer; color: var(--muted); flex-shrink: 0; }
.icon-btn:hover { color: var(--text); background: var(--surface-2); }

main { flex: 1; width: 100%; max-width: 1160px; margin: 0 auto; padding: 32px 16px 48px; }
.footer { text-align: center; color: var(--muted); font-size: .9rem; padding: 24px 16px; border-top: 1px solid var(--border); }

/* Home */
.hero { text-align: center; margin: 16px auto 32px; max-width: 760px; }
.hero h1 { font-size: clamp(1.8rem, 4vw, 2.7rem); line-height: 1.15; margin: 0 0 12px; letter-spacing: -.02em; }
.hero h1 span { background: linear-gradient(90deg, var(--accent), var(--c-image)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.hero p { color: var(--muted); font-size: 1.1rem; margin: 0 0 24px; }
.search { position: relative; max-width: 520px; margin: 0 auto; }
.search input { width: 100%; padding: 14px 16px 14px 46px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow); font-size: 1rem; }
.search input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.search svg { position: absolute; left: 16px; top: 50%; transform: translateY(-50%); color: var(--muted); }

.quickdrop { margin: 0 auto 36px; max-width: 760px; border: 2px dashed var(--border); border-radius: var(--radius); padding: 18px; text-align: center; color: var(--muted); cursor: pointer; transition: .15s; }
.quickdrop:hover, .quickdrop.drag { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }
.quickdrop strong { color: var(--text); }
.suggest { margin: -20px auto 36px; max-width: 760px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.suggest h3 { margin: 0 0 10px; font-size: 1rem; }
.suggest .chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border: 1px solid var(--border); background: var(--surface-2); padding: 6px 12px; border-radius: 999px; cursor: pointer; font-size: .92rem; }
.chip:hover { border-color: var(--accent); color: var(--accent); }

.cat-title { display: flex; align-items: center; gap: 10px; margin: 32px 0 14px; font-size: 1.25rem; }
.cat-title .dot { width: 10px; height: 10px; border-radius: 50%; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 14px; }
.card {
  display: flex; gap: 14px; align-items: flex-start; text-align: left;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; cursor: pointer; text-decoration: none; color: var(--text);
  transition: transform .12s, box-shadow .12s, border-color .12s; box-shadow: var(--shadow);
}
.card:hover { transform: translateY(-2px); border-color: var(--tool-color, var(--accent)); }
.card .ico { font-size: 1.5rem; width: 46px; height: 46px; flex-shrink: 0; display: grid; place-items: center; border-radius: 12px; background: color-mix(in srgb, var(--tool-color) 14%, transparent); }
.card h3 { margin: 0 0 4px; font-size: 1rem; }
.card p { margin: 0; color: var(--muted); font-size: .88rem; }
.badge-new { font-size: .7rem; background: var(--accent); color: #fff; border-radius: 6px; padding: 1px 6px; margin-left: 6px; vertical-align: middle; }
.empty { color: var(--muted); text-align: center; padding: 40px; }

/* Tool page */
.back { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); text-decoration: none; margin-bottom: 16px; font-weight: 500; }
.back:hover { color: var(--text); }
.tool-head { display: flex; gap: 16px; align-items: center; margin-bottom: 24px; }
.tool-head .ico { font-size: 2rem; width: 64px; height: 64px; display: grid; place-items: center; border-radius: 16px; background: color-mix(in srgb, var(--tool-color) 14%, transparent); flex-shrink: 0; }
.tool-head h1 { margin: 0; font-size: clamp(1.4rem, 3vw, 2rem); letter-spacing: -.01em; }
.tool-head p { margin: 4px 0 0; color: var(--muted); }

.layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 20px; align-items: start; }
@media (max-width: 860px) { .layout { grid-template-columns: 1fr; } }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.panel h2 { font-size: 1rem; margin: 0 0 14px; }
.side { position: sticky; top: 80px; display: flex; flex-direction: column; gap: 16px; }
@media (max-width: 860px) { .side { position: static; } }

.dropzone {
  border: 2px dashed var(--border); border-radius: var(--radius); padding: 40px 20px; text-align: center;
  cursor: pointer; transition: .15s; background: var(--surface-2);
}
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone .big { font-size: 2.4rem; }
.dropzone h3 { margin: 8px 0 4px; }
.dropzone p { margin: 0; color: var(--muted); font-size: .9rem; }
.dropzone.small { padding: 14px; }
.dropzone.small .big { display: none; }
.dropzone.small h3 { font-size: .95rem; margin: 0; }

.files { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.file { display: flex; align-items: center; gap: 12px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.file .thumb { width: 44px; height: 44px; border-radius: 6px; background: var(--surface-2); object-fit: cover; flex-shrink: 0; display: grid; place-items: center; font-size: .7rem; font-weight: 700; color: var(--muted); overflow: hidden; }
.file .thumb img, .file .thumb canvas { width: 100%; height: 100%; object-fit: contain; }
.file .meta { flex: 1; min-width: 0; }
.file .name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file .size { color: var(--muted); font-size: .82rem; }
.file .handle { cursor: grab; color: var(--muted); padding: 4px; }
.file .rm { background: none; border: none; cursor: pointer; color: var(--muted); font-size: 1.2rem; padding: 4px 8px; border-radius: 6px; }
.file .rm:hover { color: var(--danger); background: var(--surface-2); }
.files-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; color: var(--muted); font-size: .88rem; flex-wrap: wrap; gap: 8px; }
.linkbtn { background: none; border: none; color: var(--accent); cursor: pointer; padding: 0; font-weight: 500; }

.field { margin-bottom: 14px; }
.field > label, .field .lbl { display: block; font-weight: 500; font-size: .9rem; margin-bottom: 6px; }
.field .hint { color: var(--muted); font-size: .8rem; margin-top: 4px; }
.field input[type=text], .field input[type=password], .field input[type=number], .field select, .field textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-2);
}
.field textarea { min-height: 120px; resize: vertical; font-family: inherit; }
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.field input[type=range] { width: 100%; accent-color: var(--accent); }
.field input[type=color] { width: 48px; height: 34px; border: 1px solid var(--border); border-radius: 8px; background: none; padding: 2px; }
.field .row { display: flex; gap: 8px; align-items: center; }
.field .row > * { flex: 1; }
.check { display: flex; gap: 8px; align-items: center; font-size: .92rem; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { flex: 1; min-width: 60px; padding: 7px 10px; border: 1px solid var(--border); background: var(--surface-2); border-radius: 9px; cursor: pointer; font-size: .9rem; }
.seg button.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; width: 100%;
  padding: 13px 18px; border-radius: 11px; border: none; cursor: pointer; font-weight: 600; font-size: 1rem;
  background: var(--accent); color: #fff; transition: .15s; text-decoration: none;
}
.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .5; cursor: not-allowed; filter: none; }
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.small { width: auto; padding: 7px 12px; font-size: .88rem; }

.progress { margin-top: 12px; }
.progress .bar { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress .bar div { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); width: 0; transition: width .2s; }
.progress .txt { color: var(--muted); font-size: .85rem; margin-top: 6px; }
.error { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); padding: 10px 12px; border-radius: 9px; font-size: .9rem; margin-top: 12px; }
.note { color: var(--muted); background: var(--surface-2); padding: 10px 12px; border-radius: 9px; font-size: .85rem; margin-top: 12px; }

.results { margin-top: 20px; }
.results-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.results-head h2 { margin: 0; font-size: 1.1rem; color: var(--success); }
.result-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.result { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--surface); display: flex; flex-direction: column; }
.result .pv { aspect-ratio: 4/3; background: var(--surface-2) repeating-conic-gradient(var(--border) 0 25%, transparent 0 50%) 50% / 16px 16px; display: grid; place-items: center; overflow: hidden; }
.result .pv img, .result .pv canvas { max-width: 100%; max-height: 100%; object-fit: contain; }
.result .pv .ext { font-weight: 800; font-size: 1.3rem; color: var(--muted); background: var(--surface); padding: 10px 14px; border-radius: 10px; }
.result .info { padding: 10px; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.result .name { font-size: .85rem; font-weight: 500; word-break: break-all; }
.result .size { font-size: .78rem; color: var(--muted); }
.result .size .gain { color: var(--success); font-weight: 600; }
.result .size .loss { color: var(--danger); font-weight: 600; }
.result a.btn { margin-top: auto; }

/* Organize pages */
.pages { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 14px; margin-top: 14px; }
.page { border: 2px solid var(--border); border-radius: 10px; padding: 8px; background: var(--surface); text-align: center; position: relative; cursor: grab; user-select: none; }
.page.off { opacity: .35; border-style: dashed; }
.page .cv { height: 150px; display: grid; place-items: center; overflow: hidden; }
.page canvas { max-width: 100%; max-height: 140px; box-shadow: 0 1px 4px rgba(0,0,0,.2); transition: transform .2s; background: #fff; }
.page .num { font-size: .8rem; color: var(--muted); margin-top: 4px; }
.page .tools { display: flex; justify-content: center; gap: 4px; margin-top: 4px; }
.page .tools button { border: 1px solid var(--border); background: var(--surface-2); border-radius: 7px; cursor: pointer; padding: 2px 8px; font-size: .95rem; }
.page .tools button:hover { border-color: var(--accent); }
.sortable-ghost { opacity: .4; }

.textout { width: 100%; min-height: 260px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .85rem; padding: 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); }

@media (max-width: 600px) {
  .topbar { gap: 12px; }
  .brand span:last-child { display: none; }
  main { padding-top: 20px; }
  .tool-head .ico { width: 52px; height: 52px; font-size: 1.6rem; }
}

/* Signature */
.sig-tabs { margin-bottom: 10px; }
.sigpad { width: 100%; height: 170px; border: 1px dashed var(--border); border-radius: 10px; background: #fff; touch-action: none; cursor: crosshair; display: block; }
.sig-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 8px; }
.sig-row select, .sig-row input[type=text] { padding: 7px 10px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-2); flex: 1; min-width: 140px; }
.swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--border); cursor: pointer; padding: 0; }
.swatch.on { outline: 2px solid var(--accent); outline-offset: 2px; }
.sig-current { display: flex; align-items: center; gap: 10px; margin-top: 10px; font-size: .88rem; color: var(--muted); }
.sig-current img { height: 40px; background: #fff; border: 1px solid var(--border); border-radius: 6px; padding: 2px 6px; }
.pagenav { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin: 18px 0 10px; }
.pagenav .grp { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.stage { position: relative; margin: 0 auto; box-shadow: 0 2px 10px rgba(0,0,0,.18); background: #fff; cursor: copy; max-width: 100%; }
.stage canvas { display: block; width: 100%; height: auto; }
.placed { position: absolute; border: 1.5px dashed var(--accent); cursor: move; touch-action: none; }
.placed img { width: 100%; height: auto; display: block; pointer-events: none; }
.placed .del { position: absolute; top: -12px; right: -12px; width: 22px; height: 22px; border-radius: 50%; border: none; background: var(--danger); color: #fff; cursor: pointer; font-size: 14px; line-height: 1; }
.placed .rs { position: absolute; right: -7px; bottom: -7px; width: 14px; height: 14px; border-radius: 3px; background: var(--accent); cursor: nwse-resize; touch-action: none; }
