:root {
  --bg: #e8ebf0;
  --panel: #ffffff;
  --ink: #161a22;
  --muted: #5f6878;
  --line: #d8dce4;
  --soft: #f3f5f9;
  --accent: #2b59ff;
  --accent-soft: #e7edff;
  --danger: #d92d20;
  --sel: #2b59ff;
  --radius: 9px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn svg { width: 18px; height: 18px; flex: none; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px 16px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: -0.01em; white-space: nowrap; }
.tools { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; flex: 1; }
.export { display: flex; align-items: center; gap: 8px; margin-left: auto; }

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: var(--radius);
  padding: 7px 12px;
  font: inherit;
  font-weight: 560;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  white-space: nowrap;
  min-height: 36px;
  transition: background .12s, border-color .12s, color .12s;
}
.btn:hover { background: var(--soft); }
.btn:focus-visible, .sb:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: #1f47e0; }
.btn[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.btn.icon { padding: 7px 8px; }
.zoom { display: inline-flex; }
.zoom .btn { border-radius: 0; margin-left: -1px; }
.zoom .btn:first-child { border-radius: var(--radius) 0 0 var(--radius); margin-left: 0; }
.zoom .btn:last-child { border-radius: 0 var(--radius) var(--radius) 0; }
.zoom-label { min-width: 62px; font-variant-numeric: tabular-nums; }

.limit {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 0 10px; min-height: 36px; color: var(--muted); font-weight: 560;
}
.limit input {
  width: 52px; border: 0; font: inherit; color: var(--ink); text-align: right;
  background: transparent; padding: 6px 0;
}
.limit input:focus { outline: none; }
.limit:focus-within { border-color: var(--accent); }

/* ---------- layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }

.tray {
  width: 212px; flex: none;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.tray-head { padding: 12px 12px 4px; display: flex; flex-direction: column; gap: 2px; }
.tray-head small { color: var(--muted); font-size: 12px; line-height: 1.35; }
.tray-list {
  flex: 1; overflow: auto; padding: 10px 12px;
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-content: start;
}
.tray-add, .thumb {
  position: relative; aspect-ratio: 1; border-radius: 8px; overflow: hidden;
}
.tray-add {
  border: 1.5px dashed #b9c0cc; background: var(--soft); color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  font: inherit; font-size: 12px; font-weight: 560; cursor: pointer;
}
.tray-add:hover { border-color: var(--accent); color: var(--accent); }
.tray-add svg { width: 22px; height: 22px; }
.thumb {
  border: 1px solid var(--line); background: var(--soft); cursor: grab;
  touch-action: manipulation;
}
.thumb:hover { border-color: var(--accent); }
.thumb img { width: 100%; height: 100%; object-fit: contain; display: block; pointer-events: none; }
.thumb .count {
  position: absolute; left: 4px; bottom: 4px; background: rgba(22,26,34,.78); color: #fff;
  font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 99px;
}
.thumb .x {
  position: absolute; right: 3px; top: 3px; width: 22px; height: 22px; border-radius: 50%;
  border: 0; background: rgba(22,26,34,.72); color: #fff; font-size: 14px; line-height: 22px;
  padding: 0; cursor: pointer; opacity: 0; transition: opacity .12s;
}
.thumb:hover .x, .thumb:focus-within .x { opacity: 1; }
.tray-tip { margin: 0; padding: 10px 12px 14px; color: var(--muted); font-size: 12px; border-top: 1px solid var(--line); }
kbd {
  font: 600 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 1px 4px; background: #fff;
}

/* ---------- viewer ---------- */
#viewer { flex: 1; overflow: auto; position: relative; overscroll-behavior: contain; }
#pages {
  padding: 22px 16px 140px;
  display: flex; flex-direction: column; align-items: center; gap: 20px;
  width: max-content; min-width: 100%;
}
.page-block { display: flex; flex-direction: column; gap: 6px; }
.pagebar { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); min-height: 26px; }
.pagebar .pnum { font-weight: 600; }
.pagebar .spacer { flex: 1; }
.pb-btn {
  border: 1px solid transparent; background: transparent; color: var(--muted);
  font: inherit; font-weight: 560; padding: 3px 8px; border-radius: 6px; cursor: pointer;
}
.pb-btn:hover { background: #fff; border-color: var(--line); color: var(--ink); }
.pb-btn.danger:hover { color: var(--danger); }

.page {
  position: relative; background: #fff;
  box-shadow: 0 1px 2px rgba(16,24,40,.10), 0 6px 18px rgba(16,24,40,.07);
}
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.page.drop-hover { box-shadow: 0 0 0 3px var(--accent), 0 6px 18px rgba(16,24,40,.07); }
.layer { position: absolute; inset: 0; }
body.text-mode .layer { cursor: text; }

/* ---------- placed items ---------- */
.item { position: absolute; touch-action: none; user-select: none; -webkit-user-select: none; }
.item.image { cursor: move; }
.item.image img { width: 100%; height: 100%; display: block; pointer-events: none; -webkit-user-drag: none; }
.item.image:hover { outline: 1px solid rgba(43,89,255,.5); }
.item.image.selected { outline: 2px solid var(--sel); }
.item.dragging { opacity: .9; }

.handle {
  position: absolute; width: 14px; height: 14px; background: #fff;
  border: 2px solid var(--sel); border-radius: 50%; display: none; z-index: 2;
}
.item.selected .handle { display: block; }
.handle.nw { left: -7px; top: -7px; cursor: nwse-resize; }
.handle.ne { right: -7px; top: -7px; cursor: nesw-resize; }
.handle.sw { left: -7px; bottom: -7px; cursor: nesw-resize; }
.handle.se { right: -7px; bottom: -7px; cursor: nwse-resize; }

.item.text { cursor: move; }
.item.text .txt {
  white-space: pre; line-height: 1.2; outline: none; margin: 0; padding: 0;
  min-width: 3px; min-height: 1.2em;
}
.item.text:hover { outline: 1px dashed rgba(43,89,255,.55); outline-offset: 3px; }
.item.text.selected { outline: 1.5px dashed var(--sel); outline-offset: 3px; }
.item.text.editing { cursor: text; }
.item.text.editing .txt { cursor: text; user-select: text; -webkit-user-select: text; }
.item.text.editing .txt:empty::before { content: "Type here"; color: #98a2b3; }
.grip {
  position: absolute; left: -4px; top: -30px; width: 24px; height: 24px;
  display: none; align-items: center; justify-content: center;
  background: var(--sel); color: #fff; border-radius: 6px; cursor: move;
}
.grip svg { width: 15px; height: 15px; }
.item.text.selected .grip { display: flex; }

@media (pointer: coarse) {
  .handle { width: 22px; height: 22px; }
  .handle.nw { left: -11px; top: -11px; } .handle.ne { right: -11px; top: -11px; }
  .handle.sw { left: -11px; bottom: -11px; } .handle.se { right: -11px; bottom: -11px; }
  .grip { width: 30px; height: 30px; top: -36px; }
  .thumb .x { opacity: 1; }
}

/* ---------- empty state ---------- */
.empty { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.dropzone {
  width: min(560px, 100%); background: var(--panel); border: 2px dashed #b9c0cc; border-radius: 16px;
  padding: 34px 28px 26px; text-align: center; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  transition: border-color .12s, background .12s;
}
.dropzone.hover { border-color: var(--accent); background: var(--accent-soft); }
.dropzone > svg { color: var(--accent); }
.dropzone h1 { margin: 0; font-size: 20px; color: var(--ink); letter-spacing: -0.01em; }
.dropzone .row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.steps { text-align: left; margin: 10px 0 0; padding-left: 20px; display: grid; gap: 6px; color: var(--ink); }
.steps b { font-weight: 650; }
.private { margin: 4px 0 0; font-size: 12px; }

/* ---------- selection toolbar ---------- */
.selbar {
  position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%);
  background: #161a22; color: #fff; border-radius: 12px; padding: 6px;
  display: flex; align-items: center; gap: 4px; z-index: 20;
  box-shadow: 0 10px 30px rgba(16,24,40,.28); max-width: calc(100vw - 24px); overflow-x: auto;
}
.selbar[hidden] { display: none; }
.selbar .grp { display: flex; align-items: center; gap: 4px; }
.selbar .grp[hidden] { display: none; }
.sb {
  appearance: none; border: 0; background: transparent; color: #fff; font: inherit; font-weight: 560;
  padding: 7px 10px; border-radius: 8px; cursor: pointer; white-space: nowrap; min-height: 34px;
  display: inline-flex; align-items: center; justify-content: center;
}
.sb:hover { background: rgba(255,255,255,.12); }
.sb[aria-pressed="true"] { background: rgba(255,255,255,.22); }
.sb.danger:hover { background: rgba(217,45,32,.85); }
.selbar select, .selbar input[type="number"] {
  background: rgba(255,255,255,.1); color: #fff; border: 0; border-radius: 8px;
  font: inherit; padding: 7px 8px; min-height: 34px;
}
.selbar select option { color: #000; }
.selbar input[type="number"] { width: 58px; text-align: center; }
.sb.color { padding: 6px; }
.sb.color input { width: 24px; height: 24px; border: 0; padding: 0; background: none; cursor: pointer; }
.sb.color input::-webkit-color-swatch-wrapper { padding: 0; }
.sb.color input::-webkit-color-swatch { border: 2px solid #fff; border-radius: 50%; }
.sb.color input::-moz-color-swatch { border: 2px solid #fff; border-radius: 50%; }
.sep { width: 1px; height: 22px; background: rgba(255,255,255,.18); margin: 0 2px; }

/* ---------- modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(16,20,28,.45); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal[hidden] { display: none; }
.card {
  width: min(460px, 100%); background: #fff; border-radius: 16px; padding: 22px 22px 18px;
  box-shadow: 0 24px 60px rgba(16,24,40,.3);
}
.card h2 { margin: 0 0 12px; font-size: 18px; letter-spacing: -0.01em; }
.bar { height: 6px; background: var(--soft); border-radius: 99px; overflow: hidden; position: relative; }
.bar > div { position: absolute; inset: 0 auto 0 0; width: 35%; background: var(--accent); border-radius: 99px; animation: slide 1.1s ease-in-out infinite; }
.bar.done > div { width: 100%; animation: none; background: #12b76a; }
.bar.fail > div { width: 100%; animation: none; background: var(--danger); }
@keyframes slide { 0% { left: -35%; } 100% { left: 100%; } }
.status { margin: 12px 0 6px; font-weight: 560; }
.status .big { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; display: block; }
.log {
  margin: 0; padding: 8px 10px 8px 28px; background: var(--soft); border-radius: 10px;
  max-height: 170px; overflow: auto; font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--muted);
}
.log:empty { display: none; }
.log .ok { color: #067647; }
.log .no { color: #b42318; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; top: 66px; transform: translate(-50%, -8px);
  background: #161a22; color: #fff; padding: 9px 14px; border-radius: 10px; font-weight: 560;
  opacity: 0; pointer-events: none; transition: opacity .18s, transform .18s; z-index: 60;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- small screens ---------- */
@media (max-width: 760px) {
  .topbar { padding: 8px 10px; gap: 8px; }
  .brand span { display: none; }
  .tools .btn span { display: none; }
  .tools .btn { padding: 7px 9px; }
  .export { margin-left: 0; flex: 1; justify-content: flex-end; }
  .export .btn span { display: inline; }
  .layout { flex-direction: column-reverse; }
  .tray { width: auto; border-right: 0; border-top: 1px solid var(--line); flex-direction: row; align-items: stretch; }
  .tray-head, .tray-tip { display: none; }
  .tray-list { display: flex; overflow-x: auto; overflow-y: hidden; padding: 8px 10px; gap: 8px; }
  .tray-add, .thumb { width: 64px; height: 64px; flex: none; aspect-ratio: auto; }
  .tray-add span { display: none; }
  #pages { padding: 14px 10px 150px; gap: 14px; }
  .selbar { bottom: 96px; }
}


/* ---------- polish ---------- */
body { -webkit-font-smoothing: antialiased; }
.topbar { box-shadow: 0 1px 0 var(--line), 0 2px 10px rgba(16,24,40,.04); border-bottom: 0; }
.brand span { background: linear-gradient(90deg, #1d3fd6, #7c3aed); -webkit-background-clip: text; background-clip: text; color: transparent; }
#viewer { background: radial-gradient(1200px 600px at 50% -200px, #f4f6fb 0%, var(--bg) 70%); }
.btn { box-shadow: 0 1px 2px rgba(16,24,40,.05); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn.primary { background: linear-gradient(180deg, #3a66ff, #2550f0); box-shadow: 0 1px 2px rgba(16,24,40,.12), inset 0 1px 0 rgba(255,255,255,.18); }
.page { border-radius: 2px; transition: box-shadow .15s; }
.page-block:hover .page { box-shadow: 0 2px 4px rgba(16,24,40,.10), 0 12px 28px rgba(16,24,40,.10); }
.thumb { transition: transform .12s, border-color .12s, box-shadow .12s; }
.thumb:hover { transform: translateY(-1px); box-shadow: 0 6px 14px rgba(16,24,40,.12); }
.dropzone { box-shadow: 0 10px 40px rgba(16,24,40,.06); }
.modal { backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); animation: fade .15s ease-out; }
.card { animation: pop .18s ease-out; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }
.selbar { animation: pop .14s ease-out; }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

.site-link { color: var(--muted); font-weight: 600; text-decoration: none; white-space: nowrap; padding: 6px 4px; }
.site-link:hover { color: var(--accent); text-decoration: underline; }
.log-wrap { margin-top: 10px; }
.log-wrap summary { cursor: pointer; color: var(--muted); font-size: 12px; font-weight: 600; margin-bottom: 6px; }
.private a { color: var(--accent); font-weight: 600; }
@media (max-width: 760px) { .site-link { display: none; } }
