:root {
  --bg: #eef0f5; --card: #fff; --text: #1c1f2a; --muted: #6c7287;
  --accent: #4f46e5; --accent-soft: #e9e8fc; --border: #dfe2ea; --danger: #d92d3f;
  --shadow: 0 1px 3px rgba(20,22,40,.08), 0 8px 24px rgba(20,22,40,.08);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #14151a; --card: #1e2027; --text: #eceef4; --muted: #9a9fb3; --accent: #8b8bf0; --accent-soft: #292a48; --border: #30333f; --danger: #ff6b78; }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); min-height: 100vh; padding-bottom: env(safe-area-inset-bottom); }
h1 { margin: 8px 0; font-size: 1.7rem; letter-spacing: -.02em; }
h2 { margin: 0 0 12px; font-size: 1.2rem; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

.topbar { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--border); }
.brand { font-weight: 700; }
.logo { color: var(--accent); }
.file-name { color: var(--muted); font-size: .9rem; margin-right: auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

.toolbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: color-mix(in srgb, var(--card) 92%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--border); overflow-x: auto; }
.toolbar .btn { white-space: nowrap; flex: none; }
.zoom { display: flex; align-items: center; gap: 4px; flex: none; }
.zoom span { min-width: 44px; text-align: center; font-size: .85rem; color: var(--muted); }

.btn { font: inherit; font-weight: 500; color: var(--text); background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 8px 14px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; min-height: 40px; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.icon { padding: 8px 12px; min-width: 40px; }
.btn.big { min-height: 48px; padding: 10px 22px; font-size: 1rem; }
.btn:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* empty state */
.drop { display: grid; place-items: center; min-height: calc(100vh - 80px); padding: 20px; }
.drop-inner { text-align: center; max-width: 440px; padding: 40px 24px; background: var(--card); border: 2px dashed var(--border); border-radius: 20px; }
.drop.over .drop-inner { border-color: var(--accent); background: var(--accent-soft); }
.drop-icon { font-size: 3rem; }
.drop-inner .btn { margin: 12px 0 4px; }

.loading { text-align: center; padding: 40px; color: var(--muted); }

/* pages */
.pages { display: flex; flex-direction: column; align-items: center; gap: 18px; padding: 16px 12px 220px; overflow-x: auto; }
.page-wrap { flex: none; }
.page { position: relative; background: #fff; box-shadow: var(--shadow); user-select: none; -webkit-user-select: none; }
.page canvas { display: block; width: 100%; height: 100%; }
.layer { position: absolute; inset: 0; overflow: hidden; }
.page-num { text-align: center; font-size: .75rem; color: var(--muted); margin-top: 6px; }

.item { position: absolute; touch-action: none; cursor: grab; }
.item.selected { outline: 2px dashed var(--accent); outline-offset: 3px; z-index: 2; }
.item.text { white-space: pre; line-height: 1.15; font-family: Helvetica, Arial, sans-serif; padding: 0; }
.item.sig img { width: 100%; height: 100%; display: block; pointer-events: none; -webkit-user-drag: none; }
.handle { display: none; position: absolute; right: -16px; bottom: -16px; width: 26px; height: 26px; border-radius: 50%; background: var(--accent); border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.3); touch-action: none; cursor: nwse-resize; }
.item.selected .handle { display: block; }

/* selection bar */
.bar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; background: var(--card); border-top: 1px solid var(--border); box-shadow: 0 -4px 20px rgba(0,0,0,.12); padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; max-width: 760px; width: 100%; margin: 0 auto; }
.bar-group { display: flex; gap: 6px; align-items: center; }
textarea { width: 100%; font: inherit; font-size: 16px; /* prevents iOS zoom */ color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; resize: none; }
.swatches { display: flex; gap: 6px; margin-left: 6px; }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--border); cursor: pointer; padding: 0; }
.swatch.on { outline: 2px solid var(--accent); outline-offset: 2px; }

/* modals */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(10,12,20,.55); display: grid; place-items: center; padding: 16px; }
.sheet { background: var(--card); border-radius: 18px; padding: 20px; width: 100%; max-width: 560px; box-shadow: var(--shadow); }
#sig-canvas { width: 100%; height: 220px; background: #fff; border: 1px solid var(--border); border-radius: 12px; touch-action: none; display: block; cursor: crosshair; background-image: linear-gradient(#0000 calc(100% - 50px), #d6d9e2 calc(100% - 50px), #d6d9e2 calc(100% - 49px), #0000 calc(100% - 49px)); }
.sheet-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; align-items: center; }
.sheet-actions.col { flex-direction: column; align-items: stretch; }

.foot { display: flex; justify-content: center; gap: 12px; flex-wrap: wrap; padding: 14px 16px 18px; font-size: .8rem; color: var(--muted); }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--accent); text-decoration: underline; cursor: pointer; }
.sheet.scroll { max-height: 85vh; overflow-y: auto; }
.agree { display: flex; gap: 10px; align-items: center; margin-top: 14px; padding: 12px; border: 1px solid var(--border); border-radius: 12px; cursor: pointer; font-weight: 500; }
.agree input { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.sheet h3 { margin: 14px 0 4px; font-size: .95rem; }
.sheet p { margin: 0 0 8px; font-size: .92rem; }

.toast { position: fixed; left: 50%; bottom: 90px; transform: translate(-50%, 20px); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; opacity: 0; pointer-events: none; transition: all .25s; font-size: .9rem; z-index: 60; max-width: 90vw; text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 520px) {
  .toolbar .btn { padding: 8px 10px; }
  .zoom { display: none; } /* pinch-to-zoom works on phones */
  h1 { font-size: 1.4rem; }
}
