body {
    margin: 0;
    padding: 0;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    display: flex;
    height: 100vh;
    overflow: hidden;
    background-color: #f0f0f0;
}

.toolbar {
    width: 250px;
    background: #333;
    color: white;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    box-shadow: 2px 0 5px rgba(0,0,0,0.1);
    z-index: 10;
    overflow-y: auto; /* 允許垂直滾動 */
    height: 100vh;    /* 確保佔滿高度 */
    box-sizing: border-box; /* 含 padding */
}

/* 美化滾動條 (Chrome/Safari) */
.toolbar::-webkit-scrollbar {
    width: 8px;
}
.toolbar::-webkit-scrollbar-track {
    background: #444; 
}
.toolbar::-webkit-scrollbar-thumb {
    background: #666; 
    border-radius: 4px;
}
.toolbar::-webkit-scrollbar-thumb:hover {
    background: #888; 
}

.toolbar label {
    display: flex;
    flex-direction: column;
    font-size: 12px;
    margin-bottom: 5px;
}

.toolbar input, .toolbar select {
    padding: 5px;
    margin-top: 3px;
    border-radius: 4px;
    border: none;
    width: 100%;             /* 關鍵：填滿容器 */
    box-sizing: border-box;  /* 關鍵：padding 包含在寬度內，不外擴 */
}

.toolbar button {
    padding: 10px;
    cursor: pointer;
    background: #007bff;
    color: white;
    border: none;
    border-radius: 4px;
    font-weight: bold;
}

.toolbar button:hover {
    background: #0056b3;
}

.workspace {
    flex: 1;
    overflow: auto;
    display: flex;
    /* 移除 justify/align，改用子元素 margin: auto 達成更穩健的置中 */
    padding: 40px;
    background: #525659; /* 常見的 PDF 閱讀器背景色 */
}

#canvas-container {
    position: relative;
    box-shadow: 0 0 10px rgba(0,0,0,0.5);
    background: white;
    transition: transform 0.3s ease; /* 加入平滑過渡 */
    margin: auto; /* 關鍵：確保在 flex 容器中自動置中，即使內容溢出也不會被切掉 */
}

#canvas-container.rotate-90 {
    transform-origin: center center;
    transform: rotate(90deg);
}

#pdf-render {
    display: block;
    transform-origin: 0 0;
    /* 先位移再縮放，確保位移是在原始座標系上，或者根據需求調整順序 */
    transform: translate(var(--bg-x, 0px), var(--bg-y, 0px)) scale(var(--bg-scale, 1));
}

#overlay-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none; /* 讓點擊能穿透到下層，但輸入框本身要有 pointer-events: auto */
    transform-origin: 0 0;
    transform: scale(var(--content-scale, 1));
}

.print-field {
    position: absolute;
    border: 1px dashed rgba(0, 0, 255, 0.5);
    background: rgba(255, 255, 255, 0.3);
    cursor: move;
    pointer-events: auto;
    min-width: 50px;
    min-height: 20px;
    white-space: nowrap;
    display: flex;
    align-items: center;
    user-select: none;
    padding: 0;
}

.print-field.selected {
    border: 1px solid #007bff;
    background: rgba(0, 123, 255, 0.05);
    z-index: 100;
}

/* 拖曳手柄 (只在選取時出現) */
.drag-handle {
    position: absolute;
    top: -24px;
    left: -1px;
    background: #007bff;
    color: white;
    font-size: 12px;
    padding: 2px 6px;
    cursor: grab;
    border-radius: 4px 4px 0 0;
    display: none;
    white-space: nowrap;
    z-index: 101;
    pointer-events: auto;
}

.drag-handle:active {
    cursor: grabbing;
}

.print-field.selected .drag-handle {
    display: block;
}

/* 內部輸入框 (改為 contenteditable div) */
.field-input {
    width: 100%;
    height: 100%;
    background: transparent;
    border: none;
    outline: none;
    font-family: inherit;
    font-size: inherit;
    letter-spacing: inherit;
    color: inherit;
    padding: 0;
    margin: 0;
    display: block;
    cursor: text;
    white-space: nowrap; /* 強制不換行 */
    overflow: visible;   /* 關鍵：讓文字超出邊界也能顯示 */
    line-height: 1.2;    /* 稍微調整行高以垂直置中 */
}

/* === 列印模式 === */
@media print {
    @page {
        margin: 0; /* 告訴瀏覽器不要加頁邊距 */
        /* size 由 JS 動態控制 */
    }
    
    html, body {
        width: 100%;
        height: 100%;
        margin: 0 !important;
        padding: 0 !important;
        overflow: visible !important; /* 關鍵修改：允許內容超出邊界 */
    }

    .toolbar {
        display: none !important;
    }

    .workspace {
        padding: 0;
        margin: 0;
        background: white;
        display: block;
    }

    #canvas-container {
        box-shadow: none;
    }

    /* 隱藏 PDF 底圖，只留文字 */
    #pdf-render {
        display: none !important; /* 直接不顯示，避免佔位或干擾 */
    }

    /* 套用全域偏移 - 改用 margin 以免被裁切 */
    #canvas-container {
        margin-left: var(--print-offset-x, 0mm) !important;
        margin-top: var(--print-offset-y, 0mm) !important;
        overflow: visible !important;
    }

    #overlay-layer {
        overflow: visible !important;
        transform-origin: 0 0; /* 確保從左上角開始縮放 */
        transform: scale(var(--content-scale, 1));
    }

    /* 隱藏編輯輔助元素 */
    .print-field {
        border: none !important;
        background: transparent !important;
    }
    
    .drag-handle {
        display: none !important;
    }
    #canvas-container.rotate-90 {
        transform-origin: center center;
        /* 
           位移修正邏輯：
           1. translate(X, Y): 
              X = -diff (向左移，抵消旋轉造成的右移)
              Y = diff  (向下移，抵消旋轉造成的上移/裁切)
           2. 疊加全域偏移 (print-offset)
           注意：transform 順序很重要，先位移修正原點，再旋轉，或旋轉後再位移。
           這裡我們採用：先旋轉，再位移。
           因為 rotate 在前，座標系也轉了... 不，transform 屬性中的多個函式是從右向左應用（矩陣乘法），
           但在 CSS 語法寫法上，是從左到右執行。
           
           測試結果最佳化：
           直接在旋轉後的座標系上操作比較直觀？不，保持 center center 旋轉最單純。
           Center 不變。
           Original Top-Left (0,0) -> Visual Top-Right.
           Visual Top-Left is at (diff, -diff).
           So we need translate(-diff, diff) to bring Visual Top-Left to (0,0).
           
           加上全域偏移:
           X 軸 (紙張橫向): -diff + var(--print-offset-x)
           Y 軸 (紙張縱向): diff + var(--print-offset-y)
        */
        transform: translate(
            calc(-1 * var(--rotate-diff) + var(--print-offset-x, 0mm)), 
            calc(var(--rotate-diff) + var(--print-offset-y, 0mm))
        ) rotate(90deg);
        
        /* 覆蓋原本的 margin 設定，因為已經用 transform 處理了位移 */
        margin: 0 !important; 
    }
}

/* === Toast Notification === */
#toast {
    visibility: hidden;
    min-width: 250px;
    background-color: #333;
    color: #fff;
    text-align: center;
    border-radius: 8px;
    padding: 12px 24px;
    position: fixed;
    z-index: 2000;
    left: 50%;
    bottom: 30px;
    transform: translateX(-50%);
    font-size: 16px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.2);
    opacity: 0;
    transition: opacity 0.3s, bottom 0.3s;
}

#toast.show {
    visibility: visible;
    opacity: 1;
    bottom: 50px; /* 稍微往上浮動的效果 */
}

/* === Resize Handle (Bottom-Right) === */
.resize-handle {
    width: 12px;
    height: 12px;
    background: #28a745;
    position: absolute;
    right: -6px;
    bottom: -6px;
    cursor: nwse-resize;
    z-index: 102;
    border-radius: 50%;
    border: 1px solid white;
    display: none;
}

.print-field.selected .resize-handle {
    display: block;
}

@media print {
    .resize-handle {
        display: none !important;
    }
}