:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --border: rgba(0,0,0,0.1);
  --text: #1a1a18;
  --muted: #70706d;
  --accent: #185FA5;
  --success: #3B6D11;
  --success-bg: #EAF3DE;
  --danger: #A32D2D;
  --danger-bg: #FCEBEB;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a18;
    --surface: #242422;
    --border: rgba(255,255,255,0.09);
    --text: #ebebea;
    --muted: #888780;
    --accent: #378ADD;
    --success: #97C459;
    --success-bg: #173404;
    --danger: #F09595;
    --danger-bg: #3a1010;
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.state-container {
  display: none;
  flex: 1;
  flex-direction: column;
}
.state-container.show {
  display: flex !important;
}

#state-loading, #state-error, #state-done {
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 2rem;
}

.spinner {
  width: 36px; height: 36px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.state-label {
  font-size: 14px;
  color: var(--muted);
}

.state-icon {
  width: 56px; height: 56px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 24px;
}
.icon-error { background: var(--danger-bg); color: var(--danger); }
.icon-done  { background: var(--success-bg); color: var(--success); }
.state-title { font-size: 18px; font-weight: 500; }
.state-sub { font-size: 14px; color: var(--muted); text-align: center; max-width: 320px; line-height: 1.6; }

#doc-header {
  background: var(--surface);
  border-bottom: 0.5px solid var(--border);
  padding: 0 1.5rem;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 10;
}
.doc-name { font-size: 14px; font-weight: 500; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 60%; }
.page-nav { display: flex; align-items: center; gap: 10px; }
.page-btn { width: 30px; height: 30px; border: 0.5px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); cursor: pointer; display: flex; align-items: center; justify-content: center; }
.page-btn:hover { background: var(--bg); }
.page-btn:disabled { opacity: 0.35; cursor: default; }
.page-label { font-size: 13px; color: var(--muted); min-width: 48px; text-align: center; }

#canvas-scroll {
  flex: 1;
  overflow: auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}
#canvas-wrap {
  position: relative;
  box-shadow: 0 2px 20px rgba(0,0,0,0.12);
  border-radius: 4px;
  line-height: 0;
  max-width: 100%;
}
#pdf-canvas { display: block; max-width: 100%; height: auto; }
#sig-canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; cursor: crosshair; touch-action: none; }

.sign-hint {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,0.6);
  color: #fff;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 13px;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.25s;
}
.sign-hint.hidden { opacity: 0; }

#doc-footer {
  background: var(--surface);
  border-top: 0.5px solid var(--border);
  padding: 1rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-shrink: 0;
}
.sig-tools { display: flex; align-items: center; gap: 10px; }
.size-slider { width: 72px; accent-color: var(--accent); }
.tool-sep { width: 0.5px; height: 20px; background: var(--border); }
.ghost-btn { height: 32px; padding: 0 12px; border: 0.5px solid var(--border); border-radius: 6px; background: transparent; color: var(--muted); font-size: 13px; cursor: pointer; }
.ghost-btn:hover { background: var(--bg); color: var(--text); }

/* ── BOTÃO CONCLUIR ── */
.btn-conclude { height: 40px; padding: 0 24px; background: var(--accent); color: white; border: none; border-radius: 8px; font-size: 14px; font-weight: 500; cursor: pointer; display: flex; align-items: center; gap: 8px; transition: opacity 0.12s; }
.btn-conclude:hover { opacity: 0.9; }
.btn-conclude:disabled { opacity: 0.45; cursor: default; }
.btn-spinner { width: 16px; height: 16px; border: 2px solid rgba(255,255,255,0.4); border-top-color: white; border-radius: 50%; animation: spin 0.6s linear infinite; display: none; }

/* ── TOAST SISTEMA ── */
#toast { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: 8px 18px; border-radius: 20px; font-size: 13px; opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 100; max-width: calc(100% - 32px); text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── MOBILE ── */
@media (max-width: 640px) {
  #doc-header { padding: 0 0.75rem; height: 48px; }
  .doc-name { font-size: 13px; max-width: 50%; }

  #canvas-scroll { padding: 0.75rem; }

  #doc-footer { flex-direction: column; align-items: stretch; padding: 0.75rem; gap: 0.75rem; }
  .sig-tools { justify-content: center; flex-wrap: wrap; }
  .btn-conclude { width: 100%; justify-content: center; }
}