*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bar-h: 60px;
  --sidebar-w: 180px;
  --bar-bg: #1e293b;
  --bar-text: #e2e8f0;
  --accent: #4f46e5;
  --accent-light: #eef2ff;
}

html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; overflow: hidden; }

/* ── Top Bar ── */
#topbar {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--bar-h); background: var(--bar-bg); color: var(--bar-text);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 .75rem; gap: .5rem; z-index: 300;
  box-shadow: 0 2px 10px rgba(0,0,0,.4);
}
#topbar-left { display: flex; align-items: center; gap: .75rem; min-width: 180px; overflow: hidden; }
#backBtn { color: #94a3b8; text-decoration: none; font-size: .78rem; white-space: nowrap; transition: color .13s; }
#backBtn:hover { color: #fff; }
#filename { font-size: .75rem; color: #94a3b8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 180px; }

/* ── Toolbar (sibling of topbar; fixed-center on desktop, fixed-bottom on mobile) ──
   IMPORTANT: do NOT set top: 0 here. When mobile also sets bottom: 0, CSS spec says
   "ignore bottom" when top+height+bottom are all set → toolbar wrongly ends up at
   the top of the page covering everything. Let top default to auto (= static pos = 0). */
#toolbar {
  position: fixed; left: 0; right: 0;
  height: var(--bar-h); z-index: 301;
  display: flex; align-items: center; gap: .35rem; justify-content: center; flex-wrap: wrap;
  pointer-events: none;
  overflow: hidden;
}
.tool-group { display: flex; align-items: center; gap: .25rem; pointer-events: all; }
.toolbar-sep { width: 1px; height: 24px; background: rgba(255,255,255,.15); margin: 0 .2rem; flex-shrink: 0; }

.tool-btn {
  background: transparent; border: 1.5px solid transparent; color: var(--bar-text);
  padding: .2rem .6rem; border-radius: 7px; cursor: pointer;
  font-family: inherit; white-space: nowrap; transition: background .13s, border-color .13s;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  min-width: 52px; height: 44px; justify-content: center;
}
.tool-btn:hover { background: rgba(255,255,255,.1); border-color: rgba(255,255,255,.2); }
.tool-btn.active { background: var(--accent); border-color: var(--accent); }
.tool-icon { font-size: 1rem; line-height: 1; }
.tool-label { font-size: .62rem; letter-spacing: .02em; opacity: .85; line-height: 1; }

#fontSize {
  background: #334155; color: #fff; border: 1px solid #475569;
  border-radius: 5px; padding: .2rem .35rem; font-size: .78rem; width: 55px; height: 28px; cursor: pointer;
}
.fmt-btn {
  background: #334155; border: 1px solid #475569; color: #fff;
  width: 28px; height: 28px; border-radius: 5px; cursor: pointer; font-size: .85rem;
  display: flex; align-items: center; justify-content: center;
}
.fmt-btn:hover { background: #475569; }
.fmt-btn.active { background: var(--accent); border-color: var(--accent); }

#textColor, #drawColor {
  width: 28px; height: 28px; border-radius: 5px; border: 1px solid #475569;
  padding: 2px; cursor: pointer; background: #334155;
}
#drawWidth {
  background: #334155; color: #fff; border: 1px solid #475569;
  border-radius: 5px; padding: .2rem .35rem; font-size: .78rem; height: 28px; cursor: pointer;
}

/* Highlight color swatches */
.color-swatch {
  width: 22px; height: 22px; border-radius: 4px;
  border: 2px solid transparent; cursor: pointer; transition: border-color .13s;
}
.color-swatch.active { border-color: #fff; }
.color-swatch:hover { border-color: rgba(255,255,255,.6); }

.zoom-btn {
  background: #334155; border: 1px solid #475569; color: #fff;
  width: 26px; height: 26px; border-radius: 5px; cursor: pointer; font-size: .9rem;
  display: flex; align-items: center; justify-content: center;
}
.zoom-btn:hover { background: #475569; }
#zoomLevel { font-size: .75rem; color: #94a3b8; min-width: 40px; text-align: center; }

/* ── Top-right ── */
#topbar-right { display: flex; align-items: center; gap: .5rem; min-width: 190px; justify-content: flex-end; }
#annotCount { font-size: .72rem; color: #94a3b8; white-space: nowrap; }

.open-btn {
  background: #334155; color: var(--bar-text); border: 1px solid #475569;
  padding: .3rem .7rem; border-radius: 6px; font-size: .78rem; cursor: pointer;
  white-space: nowrap; transition: background .13s;
}
.open-btn:hover { background: #475569; }

#saveBtn {
  background: var(--accent); color: #fff; border: none;
  padding: .35rem 1rem; border-radius: 6px; font-size: .82rem; font-weight: 700;
  cursor: pointer; font-family: inherit; height: 32px; white-space: nowrap; transition: background .13s;
}
#saveBtn:hover:not(:disabled) { background: #4338ca; }
#saveBtn:disabled { opacity: .38; cursor: not-allowed; }

/* ── Upload Overlay ── */
#uploadOverlay {
  position: fixed; inset: var(--bar-h) 0 0 0;
  display: flex; align-items: center; justify-content: center;
  background: #f1f5f9; z-index: 10;
}
#uploadBox {
  background: #fff; border: 2px dashed #c7d2fe; border-radius: 18px;
  padding: 3rem 4rem; text-align: center; max-width: 480px;
  transition: border-color .15s, background .15s;
}
#uploadBox.drag-over { border-color: var(--accent); background: var(--accent-light); }
.upload-icon { font-size: 3rem; margin-bottom: .75rem; display: block; }
#uploadBox h2 { font-size: 1.5rem; font-weight: 800; color: #0f172a; margin-bottom: .4rem; }
#uploadBox > p { color: #64748b; font-size: .88rem; margin-bottom: 1.5rem; line-height: 1.55; }
.upload-btn {
  display: inline-flex; align-items: center; gap: .4rem;
  background: var(--accent); color: #fff; padding: .65rem 1.75rem;
  border-radius: 8px; cursor: pointer; font-size: .9rem; font-weight: 700;
  transition: background .13s;
}
.upload-btn:hover { background: #4338ca; }
.upload-hint { font-size: .75rem; color: #94a3b8; margin-top: .75rem; }

/* ── Editor Layout ── */
#editorLayout { position: fixed; inset: var(--bar-h) 0 0 0; display: none; }

/* ── Sidebar ── */
#sidebar {
  position: absolute; left: 0; top: 0; bottom: 0; width: var(--sidebar-w);
  background: #e2e8f0; border-right: 1px solid #cbd5e1; overflow-y: auto; z-index: 10;
}
#sidebarHeader {
  position: sticky; top: 0; background: #d5dbe3; border-bottom: 1px solid #cbd5e1;
  padding: .5rem .65rem; font-size: .62rem; font-weight: 700; letter-spacing: .1em; color: #64748b;
}
#thumbnailList { padding: .5rem .4rem; display: flex; flex-direction: column; gap: .5rem; }
.thumb-item {
  cursor: pointer; border: 2px solid transparent; border-radius: 6px;
  overflow: hidden; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.12);
  transition: border-color .13s;
}
.thumb-item canvas { width: 100%; display: block; }
.thumb-item:hover { border-color: #a5b4fc; }
.thumb-item.active { border-color: var(--accent); }
.thumb-num { text-align: center; font-size: .6rem; color: #64748b; padding: 2px 0 4px; background: #fff; }

/* ── Main Area ── */
#mainArea {
  position: absolute; left: var(--sidebar-w); top: 0; right: 0; bottom: 0;
  overflow: auto; background: #475569; display: flex; justify-content: center;
}
#pdfContainer { padding: 1.75rem; display: flex; flex-direction: column; align-items: center; gap: 1.75rem; min-width: min-content; }

/* ── Page Wrapper ── */
.page-wrapper { position: relative; background: #fff; box-shadow: 0 4px 24px rgba(0,0,0,.3); display: block; flex-shrink: 0; }
.pdf-canvas { display: block; }

/* Draw canvas — same size as pdf-canvas, sits on top */
.draw-canvas { position: absolute; inset: 0; pointer-events: none; }
.draw-canvas.active { pointer-events: all; }

/* Annotation layer */
.annot-layer { position: absolute; inset: 0; pointer-events: none; }
.annot-layer.interactive { pointer-events: all; }

/* ── Rect annotations (highlight, whiteout) ── */
.rect-annot {
  position: absolute; cursor: move; border: 1.5px dashed transparent;
  transition: border-color .13s; overflow: visible;
}
.rect-annot:hover, .rect-annot.selected { border-color: var(--accent); }
.rect-del {
  position: absolute; top: -18px; right: 0;
  background: var(--accent); color: #fff; border: none;
  font-size: .65rem; padding: 1px 5px; border-radius: 3px 3px 0 0;
  cursor: pointer; display: none;
}
.rect-annot:hover .rect-del, .rect-annot.selected .rect-del { display: block; }

/* ── Text annotation ── */
.text-annot { position: absolute; cursor: move; z-index: 20; overflow: visible; }
.text-annot-bar {
  position: absolute; top: -22px; left: 0;
  background: var(--accent); color: #fff; font-size: .62rem; padding: 2px 6px;
  border-radius: 3px 3px 0 0; display: flex; align-items: center; gap: 5px;
  opacity: 0; pointer-events: none; transition: opacity .12s; white-space: nowrap;
}
.text-annot:hover .text-annot-bar, .text-annot.selected .text-annot-bar { opacity: 1; pointer-events: all; }
.annot-del-btn { background: none; border: none; color: #fff; cursor: pointer; font-size: .72rem; opacity: .8; }
.annot-del-btn:hover { opacity: 1; }
.text-annot-content {
  outline: none; min-width: 60px; min-height: 1em; white-space: pre;
  cursor: text; line-height: 1.25; padding: 2px 4px;
  border: 1.5px dashed transparent; border-radius: 3px; transition: border-color .13s, background .13s;
}
.text-annot:hover .text-annot-content, .text-annot.selected .text-annot-content {
  border-color: var(--accent); background: rgba(238,242,255,.55);
}

/* ── Image annotation ── */
.img-annot {
  position: absolute; cursor: move; border: 1.5px dashed transparent; overflow: visible;
  transition: border-color .13s;
}
.img-annot:hover, .img-annot.selected { border-color: var(--accent); }
.img-annot img { width: 100%; height: 100%; object-fit: contain; display: block; pointer-events: none; }
.img-del {
  position: absolute; top: -20px; right: 0;
  background: var(--accent); color: #fff; border: none; font-size: .65rem;
  padding: 1px 5px; border-radius: 3px 3px 0 0; cursor: pointer; display: none;
}
.img-annot:hover .img-del, .img-annot.selected .img-del { display: block; }

/* ── 4-Corner resize handles (all annotation types) ── */
.corner-handle {
  position: absolute; width: 11px; height: 11px;
  background: var(--accent); border: 2px solid #fff; border-radius: 50%;
  z-index: 30; display: none;
}
.corner-nw { top: -6px;  left: -6px;  cursor: nw-resize; }
.corner-ne { top: -6px;  right: -6px; cursor: ne-resize; }
.corner-sw { bottom: -6px; left: -6px;  cursor: sw-resize; }
.corner-se { bottom: -6px; right: -6px; cursor: se-resize; }

.rect-annot:hover .corner-handle, .rect-annot.selected .corner-handle,
.img-annot:hover  .corner-handle, .img-annot.selected  .corner-handle,
.text-annot:hover .corner-handle, .text-annot.selected .corner-handle { display: block; }

/* ── Rotation handle ── */
.rotate-handle {
  position: absolute;
  top: -36px; left: 50%;
  transform: translateX(-50%);
  width: 22px; height: 22px;
  background: #0ea5e9; border: 2px solid #fff; border-radius: 50%;
  display: none; align-items: center; justify-content: center;
  color: #fff; font-size: 14px; cursor: grab; z-index: 35;
  user-select: none; line-height: 1;
}
.rotate-handle:active { cursor: grabbing; }
.rect-annot:hover  .rotate-handle, .rect-annot.selected  .rotate-handle,
.img-annot:hover   .rotate-handle, .img-annot.selected   .rotate-handle,
.text-annot:hover  .rotate-handle, .text-annot.selected  .rotate-handle,
.shape-annot:hover .rotate-handle, .shape-annot.selected .rotate-handle { display: flex; }

/* ── Signature Modal ── */
#signModal {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: none; align-items: center; justify-content: center; z-index: 600;
}
#signModal.open { display: flex; }
.sign-modal-card {
  background: #fff; border-radius: 14px; overflow: hidden;
  box-shadow: 0 24px 64px rgba(0,0,0,.35); width: 560px; max-width: 96vw;
}
.sign-modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .85rem 1.25rem; background: #f8fafc; border-bottom: 1px solid #e2e8f0;
}
.sign-modal-header h3 { font-size: 1rem; font-weight: 700; color: #1e293b; }
#signModalClose {
  background: none; border: none; font-size: 1.1rem; cursor: pointer; color: #64748b;
}
#signCanvas {
  display: block; background: #fafafa; cursor: crosshair; width: 100%;
  border-bottom: 1px solid #e2e8f0;
  /* Guideline at ~77% height — visual only, not captured in PNG */
  background-image: linear-gradient(transparent calc(77% - 1px), #d1d5db calc(77% - 1px), #d1d5db 77%, transparent 77%);
}
.sign-modal-footer {
  display: flex; align-items: center; gap: .5rem; padding: .75rem 1rem;
  background: #f8fafc;
}
.btn-secondary {
  background: #e2e8f0; border: none; color: #1e293b; padding: .4rem .9rem;
  border-radius: 6px; cursor: pointer; font-size: .84rem; font-family: inherit;
  transition: background .13s;
}
.btn-secondary:hover { background: #cbd5e1; }
.btn-primary-sm {
  background: var(--accent); color: #fff; border: none; padding: .4rem 1rem;
  border-radius: 6px; font-size: .84rem; font-weight: 700; cursor: pointer; font-family: inherit;
  transition: background .13s;
}
.btn-primary-sm:hover { background: #4338ca; }

/* ── Pen type / Shape type buttons ── */
.pen-type-btn, .shape-type-btn {
  background: #334155; border: 1px solid #475569; color: #fff;
  height: 26px; padding: 0 .55rem; border-radius: 5px; cursor: pointer;
  font-size: .72rem; white-space: nowrap; transition: background .13s;
}
.pen-type-btn:hover, .shape-type-btn:hover { background: #475569; }
.pen-type-btn.active, .shape-type-btn.active { background: var(--accent); border-color: var(--accent); }

/* ── Shape annotation ── */
.shape-annot {
  position: absolute; cursor: move; overflow: visible; z-index: 22;
  border: 1.5px dashed transparent; transition: border-color .13s;
}
.shape-annot:hover, .shape-annot.selected { border-color: var(--accent); }
.shape-annot:hover .img-del, .shape-annot.selected .img-del { display: block; }
.shape-annot:hover .corner-handle, .shape-annot.selected .corner-handle { display: block; }

/* ── Opt labels (shape options row) ── */
.opt-label {
  display: flex; align-items: center; gap: .3rem; font-size: .72rem;
  color: #cbd5e1; white-space: nowrap;
}
.opt-select {
  background: #334155; color: #fff; border: 1px solid #475569;
  border-radius: 5px; padding: .2rem .3rem; font-size: .75rem; height: 26px; cursor: pointer;
}

/* ── Floating text context toolbar ── */
#floatTextBar {
  position: fixed;
  z-index: 500;
  background: #1e293b;
  border: 1px solid rgba(255,255,255,.2);
  border-radius: 10px;
  padding: .3rem .5rem;
  display: flex;
  align-items: center;
  gap: .3rem;
  box-shadow: 0 4px 20px rgba(0,0,0,.5);
  pointer-events: all;
}
#floatTextBar select {
  background: #334155; color: #fff; border: 1px solid #475569;
  border-radius: 5px; padding: .15rem .3rem; font-size: .78rem; height: 28px; cursor: pointer;
}
#floatFont { width: 58px; }
#floatSize { width: 46px; }
.float-fmt-btn {
  background: #334155; border: 1px solid #475569; color: #fff;
  width: 28px; height: 28px; border-radius: 5px; cursor: pointer; font-size: .85rem;
  display: flex; align-items: center; justify-content: center;
}
.float-fmt-btn:hover { background: #475569; }
.float-fmt-btn.active { background: var(--accent); border-color: var(--accent); }
#floatColor { width: 28px; height: 28px; border-radius: 5px; border: 1px solid #475569; padding: 2px; cursor: pointer; background: #334155; }

/* ── Loading ── */
#loadingOverlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.65);
  z-index: 700; display: none; align-items: center; justify-content: center;
}
#loadingOverlay.active { display: flex; }
.loading-box { background: #fff; border-radius: 12px; padding: 2rem 2.5rem; text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.loading-spinner { width: 40px; height: 40px; border: 4px solid #e2e8f0; border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; margin: 0 auto .85rem; }
#loadingMsg { font-size: .88rem; color: #64748b; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Mobile (≤ 700px) ─────────────────────────────────────────────────────── */
@media (max-width: 700px) {
  /* Single-row topbar — just nav + save/open */
  :root { --bar-h: 50px; --bottom-bar-h: 58px; }

  #topbar {
    height: var(--bar-h);
    padding: 0 .6rem;
    flex-wrap: nowrap;
    gap: .4rem;
  }
  #topbar-left  { min-width: 0; flex: 1; gap: .4rem; overflow: hidden; }
  #topbar-right { min-width: 0; gap: .35rem; flex-shrink: 0; }

  #filename   { max-width: 130px; font-size: .72rem; }
  #annotCount { display: none; }

  /* Icon-only save/open */
  .btn-label { display: none; }
  #saveBtn   { font-size: 1.15rem; padding: .15rem .5rem; height: 32px; min-width: 36px; }
  .open-btn  { font-size: 1.15rem; padding: .15rem .45rem; }

  /* ── Bottom toolbar — thumb-friendly, scrolls horizontally ── */
  #toolbar {
    pointer-events: all;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: var(--bottom-bar-h);
    background: var(--bar-bg);
    border-top: 1px solid rgba(255,255,255,.15);
    box-shadow: 0 -4px 16px rgba(0,0,0,.35);
    padding: .35rem .5rem;
    z-index: 400;
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: .2rem;
    /* Pull out of topbar flow */
    flex: none;
    order: unset;
    width: auto;
    margin: 0;
  }
  #toolbar::-webkit-scrollbar { display: none; }

  /* Icon-only tool buttons */
  .tool-group { flex-shrink: 0; }
  .tool-label { display: none; }
  .tool-btn   { min-width: 42px; height: 42px; padding: .2rem .35rem; flex-shrink: 0; }
  .tool-icon  { font-size: 1.2rem; }

  /* Separators inside bottom bar */
  .toolbar-sep { height: 28px; }

  /* Editor layout & upload overlay — leave room for bottom toolbar */
  #editorLayout  { inset: var(--bar-h) 0 var(--bottom-bar-h) 0; }
  #uploadOverlay { inset: var(--bar-h) 0 var(--bottom-bar-h) 0; }

  /* Hide sidebar — PDF fills full width */
  #sidebar  { display: none; }
  #mainArea { left: 0 !important; }

  /* Upload box */
  #uploadBox    { padding: 2rem 1.25rem; max-width: 90vw; }
  #uploadBox h2 { font-size: 1.25rem; }
  .upload-hint  { font-size: .72rem; }

  /* Signature modal */
  .sign-modal-card { border-radius: 10px; }
  .sign-modal-footer { flex-wrap: wrap; gap: .4rem; }
  .btn-primary-sm, .btn-secondary { flex: 1; text-align: center; }
}
