*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { display: flex; flex-direction: column; background: var(--bg); color: var(--ink); font: 400 14px/1.4 var(--ts-font-body); overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
:focus-visible { outline: 2px solid var(--ts-sun); outline-offset: 2px; }
body:not(.has-doc) [data-needs-doc] { display: none !important; }
/* pdf.js appends helper elements to <body> (text-measuring canvas per language); its own viewer CSS hides
   them, we don't ship that CSS. Without this they become flex items and squash the app (Samson, page 6). */
.hiddenCanvasElement { position: absolute; top: 0; left: 0; width: 0; height: 0; display: none; }

/* Top bar */
.topbar { display: flex; align-items: center; gap: 14px; height: 54px; padding: 0 14px; background: var(--ts-night); color: #fff; flex: none; }
.brand { display: flex; align-items: center; gap: 8px; color: #fff; text-decoration: none; font: 600 17px var(--ts-font-head); white-space: nowrap; }
.brand b { color: var(--ts-sun-light); }
.doc-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: #b9cad8; }
.doc-name .dirty { color: var(--ts-sun-light); }
.modes { display: flex; background: rgba(255, 255, 255, .08); border-radius: 10px; padding: 3px; }
.modes button { border: 0; background: none; color: #cfe0ea; padding: 6px 14px; border-radius: 8px; cursor: pointer; font-weight: 500; }
.modes button.on { background: var(--ts-sun); color: var(--ts-night); font-weight: 600; }
.modes button.locked { opacity: .45; cursor: not-allowed; }
.actions { display: flex; gap: 6px; align-items: center; }
.topbar .btn { color: #fff; border-color: rgba(255, 255, 255, .2); background: transparent; }
.topbar .btn:hover { background: rgba(255, 255, 255, .1); }
.topbar .btn-accent { background: var(--ts-sun); color: var(--ts-night); border-color: var(--ts-sun); }
.topbar .btn-accent:hover { background: var(--ts-sun-light); }

/* Buttons */
.btn { display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 12px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); border-radius: 8px; cursor: pointer; white-space: nowrap; font-weight: 500; }
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.icon { width: 34px; padding: 0; justify-content: center; }
.btn-accent { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-accent:hover { filter: brightness(1.08); background: var(--accent); }
.btn.big { height: 48px; padding: 0 28px; font: 600 16px var(--ts-font-head); border-radius: 12px; }
.btn.armed { outline: 2px solid var(--ts-sun); background: var(--surface-2); }

/* Toolbars */
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-height: 48px; padding: 7px 12px; background: var(--surface); border-bottom: 1px solid var(--line); flex: none; }
.toolbar .group { display: flex; align-items: center; gap: 4px; }
.toolbar .spacer { flex: 1; }
.toolbar .search { margin-left: auto; }
.num { width: 46px; height: 34px; text-align: center; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-2); }
.of { color: var(--muted); padding: 0 4px; }
select, #search { height: 34px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-2); padding: 0 8px; }
#search { width: 190px; }
#search-count { min-width: 54px; text-align: right; font-size: 12px; }

/* Workspace */
.workspace { position: relative; flex: 1; display: flex; min-height: 0; }
.sidebar { width: 168px; flex: none; overflow-y: auto; background: var(--surface); border-right: 1px solid var(--line); padding: 12px 0; }
body.no-sidebar .sidebar { display: none !important; }
.thumb { display: block; width: 110px; margin: 0 auto 14px; padding: 0; border: 0; background: none; cursor: pointer; text-align: center; color: var(--muted); font-size: 12px; }
.thumb canvas, .thumb .ph { display: block; width: 100%; background: #fff; box-shadow: var(--shadow); border: 2px solid transparent; border-radius: 3px; margin-bottom: 4px; }
.thumb.on canvas, .thumb.on .ph { border-color: var(--accent); }
.thumb.on { color: var(--ink); font-weight: 600; }

.viewer { flex: 1; overflow: auto; background: var(--canvas-bg); padding: 20px 12px 60px; outline: none; }
.page { position: relative; margin: 0 auto 18px; background: #fff; box-shadow: var(--shadow); }
.page canvas { display: block; width: 100%; height: 100%; }
.page .loading { position: absolute; inset: 0; display: grid; place-items: center; color: #9aa; font-size: 13px; }

/* PDF.js text layer (from pdfjs-dist web/pdf_viewer.css, trimmed) */
.textLayer { position: absolute; text-align: initial; inset: 0; overflow: clip; opacity: 1; line-height: 1; text-size-adjust: none; forced-color-adjust: none; transform-origin: 0 0; caret-color: CanvasText; z-index: 0; }
.textLayer :is(span, br) { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; }
.textLayer > :not(.markedContent), .textLayer .markedContent span:not(.markedContent) { z-index: 1; }
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer ::selection { background: rgba(42, 157, 143, .35); }
.textLayer br::selection { background: transparent; }
.textLayer .endOfContent { display: block; position: absolute; inset: 100% 0 0; z-index: -1; cursor: default; user-select: none; }
.textLayer .hl { margin: -1px; padding: 1px; border-radius: 3px; background: rgba(255, 183, 3, .45); position: initial; color: transparent; }
.textLayer .hl.cur { background: rgba(247, 127, 0, .7); }

/* Form fields + placed items */
.overlay { position: absolute; inset: 0; z-index: 2; pointer-events: none; }
.overlay > * { pointer-events: auto; }
.field { position: absolute; border: 1px solid rgba(42, 157, 143, .55); background: rgba(221, 241, 238, .55); color: #111; font: 12px/1.2 Helvetica, Arial, sans-serif; padding: 1px 3px; border-radius: 2px; }
.field:focus { background: rgba(255, 241, 193, .8); outline: 2px solid var(--ts-sun); }
.field[type="checkbox"], .field[type="radio"] { margin: 0; padding: 0; accent-color: var(--ts-teal-dark); }
textarea.field { resize: none; }
.placed { position: absolute; cursor: move; touch-action: none; border: 1px dashed transparent; }
.placed img { width: 100%; height: 100%; display: block; pointer-events: none; user-select: none; }
.placed .txt { width: 100%; height: 100%; display: flex; align-items: center; white-space: nowrap; color: #111; font-family: Helvetica, Arial, sans-serif; line-height: 1; outline: none; cursor: text; }
.placed.sel { border-color: var(--ts-teal); background: rgba(42, 157, 143, .06); }
.placed .handle { position: absolute; right: -7px; bottom: -7px; width: 14px; height: 14px; border-radius: 50%; background: var(--ts-teal); border: 2px solid #fff; cursor: nwse-resize; display: none; }
.placed .del { position: absolute; right: -10px; top: -12px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: var(--ts-coral); color: #fff; font-size: 14px; line-height: 22px; padding: 0; cursor: pointer; display: none; }
.placed.sel .handle, .placed.sel .del { display: block; }
body.placing .page { cursor: crosshair; }
/* Sign-here boxes (#13): ours (.placed.box) and the file's signature fields (.sign-here) look the same */
.placed.box, .sign-here { border: 1.5px dashed var(--ts-teal); border-radius: 4px; background: rgba(255, 209, 102, .22); }
.placed.box { cursor: pointer; }
.placed.box.sel { border-style: solid; background: rgba(255, 209, 102, .3); }
.sign-here { position: absolute; padding: 0; cursor: pointer; font: inherit; }
.sign-here:hover, .placed.box:hover { background: rgba(255, 209, 102, .4); }
.sign-label { display: flex; align-items: center; gap: 4px; width: 100%; height: 100%; padding: 0 6px; overflow: hidden; color: #1D6F66; font: 600 12px "Inter", system-ui, sans-serif; white-space: nowrap; pointer-events: none; }
.sign-label svg { flex: none; width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 2; }

/* Organizer */
.organizer { flex: 1; overflow: auto; background: var(--canvas-bg); padding: 22px; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 22px; align-content: start; }
.card { position: relative; background: var(--surface); border: 2px solid transparent; border-radius: 10px; padding: 10px; box-shadow: var(--shadow); cursor: pointer; user-select: none; text-align: center; }
.card.sel { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--surface)); }
.card .pv { height: 170px; display: grid; place-items: center; }
.card canvas { max-width: 100%; max-height: 170px; box-shadow: 0 1px 4px rgba(0, 0, 0, .2); background: #fff; }
.card .n { margin-top: 8px; color: var(--muted); font-size: 12px; }
.card .tools { position: absolute; top: 6px; right: 6px; display: none; gap: 4px; }
.card:hover .tools, .card.sel .tools { display: flex; }
.card .tools button { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 6px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer; padding: 0; }
.card .tools svg { width: 15px; height: 15px; }
.card.dragging { opacity: .35; }
.card.drop-before { box-shadow: -6px 0 0 -2px var(--ts-sun), var(--shadow); }
.card.drop-after { box-shadow: 6px 0 0 -2px var(--ts-sun), var(--shadow); }

/* Welcome */
.welcome { position: absolute; inset: 0; display: grid; place-items: center; padding: 20px; background: radial-gradient(circle at 50% 110%, rgba(255, 183, 3, .28), transparent 60%), var(--bg); overflow: auto; }
body.has-doc .welcome { display: none; }
.drop { text-align: center; max-width: 520px; padding: 40px 30px; border: 2px dashed var(--line); border-radius: 22px; background: var(--surface); box-shadow: var(--shadow); }
.drop img { margin: 0 auto 8px; }
.drop h1 { font: 700 34px var(--ts-font-head); margin: 0 0 6px; }
.drop h1 span { color: var(--ts-ember); }
.pitch { font-size: 17px; color: var(--muted); margin: 0 0 22px; }
.pitch b { color: var(--ink); }
.features { list-style: none; padding: 0; margin: 22px 0 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.features li { background: var(--surface-2); border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; font-size: 13px; }
.dropzone-overlay { position: absolute; inset: 10px; z-index: 50; display: grid; place-items: center; border: 3px dashed var(--ts-sun); border-radius: 18px; background: rgba(11, 37, 64, .75); color: #fff; font: 600 24px var(--ts-font-head); pointer-events: none; }

/* Footer */
.madeby { flex: none; text-align: center; padding: 6px; background: var(--surface); border-top: 1px solid var(--line); font-size: 12px; }
.madeby a { color: var(--muted); text-decoration: none; }
.madeby a:hover { color: var(--accent); }
.madeby b { color: var(--ink); }

/* Dialogs */
.dialog { border: 0; border-radius: 16px; padding: 22px; width: min(620px, calc(100vw - 24px)); background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, .35); }
.dialog::backdrop { background: rgba(11, 37, 64, .55); }
.dialog h2 { font: 600 20px var(--ts-font-head); margin: 0 0 12px; }
.tabs { display: flex; gap: 4px; margin-bottom: 12px; border-bottom: 1px solid var(--line); }
.tabs button { border: 0; background: none; padding: 8px 14px; cursor: pointer; border-bottom: 2px solid transparent; color: var(--muted); }
.tabs button.on { border-color: var(--accent); color: var(--ink); font-weight: 600; }
.tab { min-height: 210px; display: grid; gap: 10px; align-content: start; justify-items: start; }
#sig-pad { width: 100%; height: auto; aspect-ratio: 560 / 200; background: #fff; border: 1px solid var(--line); border-radius: 10px; touch-action: none; cursor: crosshair; }
#sig-typed { width: 100%; height: 40px; border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; background: var(--surface-2); }
.typed-preview { width: 100%; min-height: 110px; display: grid; place-items: center; background: #fff; color: #10233a; border-radius: 10px; font: 600 56px "Caveat", cursive; }
#sig-upload-preview { max-width: 100%; max-height: 150px; background: #fff; }
.saved-list { display: flex; flex-wrap: wrap; gap: 10px; }
.saved-list button { position: relative; width: 170px; height: 70px; background: #fff; border: 2px solid var(--line); border-radius: 8px; cursor: pointer; padding: 4px; }
.saved-list button.on { border-color: var(--accent); }
.saved-list img { max-width: 100%; max-height: 100%; }
.saved-list .x { position: absolute; top: -8px; right: -8px; width: 20px; height: 20px; border-radius: 50%; background: var(--ts-coral); color: #fff; font-size: 12px; line-height: 20px; }
.check { display: flex; gap: 8px; align-items: center; margin-top: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.toast { position: fixed; left: 50%; bottom: 44px; transform: translate(-50%, 20px); background: var(--ts-night); color: #fff; padding: 10px 18px; border-radius: 10px; opacity: 0; pointer-events: none; transition: .2s; z-index: 100; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Print: only the rendered pages */
#print-root { display: none; }
@media print {
  @page { margin: 0; }
  body { display: block; overflow: visible; background: #fff; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block; }
  #print-root img { display: block; width: 100%; height: auto; break-after: page; page-break-after: always; }
}

/* Phone */
@media (max-width: 760px) {
  .topbar { gap: 8px; padding: 6px 8px; height: auto; flex-wrap: wrap; }
  .topbar .modes { order: 3; width: 100%; justify-content: center; }
  .topbar .modes button { flex: 1; }
  .doc-name { display: block !important; flex: 1; }
  .brand span, .actions .btn span { display: none; }
  .actions .btn:not(.icon) { width: 34px; padding: 0; justify-content: center; }
  .modes button { padding: 6px 9px; font-size: 13px; }
  .sidebar, #btn-sidebar { display: none !important; }
  .toolbar { gap: 6px; padding: 6px 8px; }
  .toolbar .search { margin-left: 0; width: 100%; }
  #search { flex: 1; width: auto; }
  #org-hint, #sign-hint { display: none; }
  .organizer { grid-template-columns: repeat(2, 1fr); padding: 12px; gap: 12px; }
  .card .tools { display: flex; }
  .viewer { padding: 10px 4px 40px; }
}
