/* 全域樣式 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Arial', sans-serif;
    background-color: #f2f2f3;
    background-image: url('尺規網頁背景.png');
    background-position: center center;
    background-repeat: no-repeat;
    background-size: cover;
    /* 允許頁面垂直滾動，但維持視窗高度，避免物件縮放變化 */
    overflow-x: hidden;
    overflow-y: auto;
    height: 100vh;
    user-select: none;
}


/* 主容器 */
#container {
    position: relative;
    width: 100vw;
    height: 100vh; /* 維持原本尺寸，避免內容因容器變高而縮放 */
    display: flex;
    align-items: center;
    justify-content: center;
}

/* 置於視窗下半部的固定黑色點線 */
#fixed-dotted-line {
    position: fixed;
    left: 0;
    right: 0;
    /* 置於視窗下半部（可依需求微調） */
    top: 65vh;
    height: 2px;
    z-index: 2000;
    pointer-events: none;
    /* 重複的水平黑色點線效果 */
    background-image: repeating-linear-gradient(
        to right,
        #000000 0px,
        #000000 6px,
        transparent 6px,
        transparent 12px
    );
}

/* 額外滾動空間，不影響主視覺尺寸 */
#scroll-spacer {
    width: 100%;
    height: 0; /* 縮短頁面長度，讓黃色區域緊貼主背景底部 */
}

/* 下方工作區域（淺黃色背景） */
#secondary-area {
    position: relative;
    width: 100vw;
    height: 100vh; /* 與上方工作區域等高 */
    background-color: #f2f2f3; /* 與頁面背景一致 */
    overflow: hidden;
}

#secondary-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: transparent;
    z-index: 5;
}

/* 繪製區域標籤 */
#secondary-label {
    position: absolute;
    top: 12px;
    left: 12px;
    z-index: 6;
    background: rgba(255, 255, 255, 0.9);
    color: #000;
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 12px;
    font-weight: 600;
}

/* 黃色區域的控制列 */
#secondary-controls {
    position: absolute;
    bottom: 16px;
    right: 16px;
    top: auto;
    z-index: 6;
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 10px;
    padding: 8px;
    display: flex;
    gap: 8px;
    align-items: center;
    box-shadow: 0 6px 20px rgba(0,0,0,0.15);
}

#secondary-controls button {
    width: auto;
    display: inline-block;
    padding: 8px 12px;
    font-size: 12px;
}

/* 左側 SVG 列表 */
#svg-list {
    position: fixed;
    left: 20px;
    top: 50%;
    transform: translateY(-50%) scale(0.8); /* 等比例縮小80% */
    width: 180px;
    max-height: 70vh; /* 調整為70vh */
    overflow-y: auto;
    overflow-x: hidden; /* 隱藏橫向滾動條 */
    background: rgba(255, 255, 255, 0.95);
    border-radius: 12px;
    box-shadow: none; /* 移除陰影 */
    backdrop-filter: blur(10px);
    border: 1px solid #000000; /* 改為黑框線 */
    z-index: 30;
    padding: 15px;
}

/* 隱藏滾動條但保留滾動功能 */
#svg-list::-webkit-scrollbar {
    display: none;
}

#svg-list {
    -ms-overflow-style: none; /* IE 和 Edge */
    scrollbar-width: none; /* Firefox */
}

#svg-list h3 {
    color: #333;
    margin-bottom: 15px;
    font-size: 16px;
    font-weight: 600;
    text-align: center;
    border-bottom: 2px solid #667eea;
    padding-bottom: 8px;
}

.svg-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 0px; /* 調整為0px */
    padding: 10px 30px; /* 增加左右padding讓容器更寬 */
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    border: 2px solid transparent;
    overflow: visible; /* 讓放大效果不被遮到 */
}

/* 讓所有縮圖都享有與 active 狀態相同的樣式保護 */
.svg-item .svg-preview svg {
    background: transparent !important;
    filter: none !important;
    opacity: 1 !important;
    visibility: visible !important;
}

.svg-item:hover {
    background: transparent;
    border-color: transparent;
    transform: scale(1.0);
}

.svg-item.active {
    background: transparent;
    border-color: transparent;
    transform: scale(1.0);
}

.svg-preview {
    width: 200px; /* 增寬到200px讓放大效果不會被遮到 */
    height: 90px; /* 保持原高度 */
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent !important;
    border-radius: 6px;
    overflow: visible; /* 改為visible讓放大效果不被遮到 */
}

.svg-preview svg {
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: transform 0.3s ease;
}

/* 確保 SVG 內部元素保持原始樣式 */
.svg-preview svg * {
    transition: none !important;
}

/* 防止預覽縮圖內容被外部影響 */
.svg-preview svg rect,
.svg-preview svg circle,
.svg-preview svg path,
.svg-preview svg text {
    transition: none !important;
}

/* 確保所有預覽縮圖在任何狀態下都保持原始顏色 */
.svg-item .svg-preview,
.svg-item:hover .svg-preview,
.svg-item.active .svg-preview,
.svg-item:active .svg-preview {
    background: transparent !important;
}

/* 確保所有狀態下的 SVG 都有相同的保護 */
.svg-item .svg-preview svg,
.svg-item:hover .svg-preview svg,
.svg-item.active .svg-preview svg,
.svg-item:active .svg-preview svg {
    background: transparent !important;
    filter: none !important;
    isolation: auto !important;
    opacity: 1 !important;
    visibility: visible !important;
    transform: none !important;
}

/* 確保 SVG 內容不被外部影響 */
.svg-preview svg {
    display: block !important;
    background: transparent !important;
    filter: none !important;
    opacity: 1 !important;
    visibility: visible !important;
}

/* 最高優先級保護，確保所有縮圖都正確顯示 */
#svg-list .svg-item .svg-preview svg {
    background: transparent !important;
    filter: none !important;
    opacity: 1 !important;
    visibility: visible !important;
    transform: none !important;
    isolation: auto !important;
}

/* 確保 SVG 內部所有元素都保持原始樣式 */
.svg-preview svg * {
    transition: none !important;
    animation: none !important;
}

/* 預覽縮圖的黑線樣式 */
.svg-preview svg .preview-svg * {
    fill: none !important;
    stroke: #000000 !important;
    stroke-width: 1px !important;
    transition: none !important;
    animation: none !important;
}

.svg-item:hover .svg-preview svg {
    transform: scale(1.2);
}

.svg-name {
    display: none; /* 隱藏名稱文字 */
}

.svg-item:hover .svg-name {
    display: none; /* 懸停時也隱藏 */
}

/* 人為類別縮圖懸停放大效果 */
.svg-item.human-category:hover .svg-preview img {
    transform: scale(1.2);
    transition: transform 0.3s ease, filter 0.3s ease;
    filter: brightness(0.92) drop-shadow(2px 2px 2px rgba(0, 0, 0, 0.2));
}

.svg-item.human-category .svg-preview img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: transform 0.3s ease;
    filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, 0.2));
}

/* 自然類別縮圖懸停放大效果 - 與人為類別一致 */
.svg-item.nature-category:hover .svg-preview img {
    transform: scale(1.2);
    transition: transform 0.3s ease, filter 0.3s ease;
    filter: brightness(0.92) drop-shadow(2px 2px 2px rgba(0, 0, 0, 0.2));
}

.svg-item.nature-category .svg-preview img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: transform 0.3s ease;
    filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, 0.2));
}

/* 動物類別縮圖懸停放大效果 - 與人為類別一致 */
.svg-item.animal-category:hover .svg-preview img {
    transform: scale(1.2);
    transition: transform 0.3s ease, filter 0.3s ease;
    filter: brightness(0.92) drop-shadow(2px 2px 2px rgba(0, 0, 0, 0.2));
}

.svg-item.animal-category .svg-preview img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: transform 0.3s ease;
    filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, 0.2));
}

/* 主內容區域 */
#main-content {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* SVG 尺規容器 - 絕對定位在中央 */
#ruler-container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1;
    pointer-events: none;
}

#snap-canvas {
    /* 與尺規位置對齊，絕對定位，跟著頁面捲動 */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 6; /* 介於背景與互動畫布之間 */
    pointer-events: none;
    background: transparent;
}

#ruler-svg {
    width: 80vw;
    height: 80vh;
    max-width: 1200px;
    max-height: 800px;
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.3));
    opacity: 1;
    pointer-events: none;
}

/* 人為類別尺規：隱藏SVG，只顯示PNG */
#ruler-svg[data-is-human="true"] {
    display: none;
}

#excavator-png[data-display="human"] {
    display: block !important;
}

#excavator-png {
    max-width: 80vw;
    max-height: 80vh;
    width: auto;
    height: auto;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 0;
    pointer-events: none;
    object-fit: contain; /* 保持原比例，等比例縮放 */
    filter: drop-shadow(2px 2px 4px rgba(0, 0, 0, 0.3));
}

/* 互動畫布層 */
#drawing-canvas {
    /* 固定於視窗，隨頁面下滑保持位置不變 */
    position: fixed;
    top: 0;
    left: 0;
    transform: none;
    z-index: 10;
    cursor: crosshair;
    pointer-events: auto;
    background: transparent;
}

/* 控制面板 */
#control-panel {
    position: absolute;
    top: 20px;
    right: 20px;
    background: rgba(255, 255, 255, 0.95);
    padding: 15px;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    z-index: 20;
    max-width: 220px;
    font-size: 14px;
}

#control-panel h3 {
    color: #333;
    margin-bottom: 10px;
    font-size: 16px;
    font-weight: 600;
}

.instructions {
    margin-bottom: 15px;
    padding: 12px;
    background: rgba(102, 126, 234, 0.1);
    border-radius: 8px;
    border-left: 4px solid #667eea;
}

.instructions p {
    margin: 4px 0;
    color: #555;
    font-size: 13px;
    line-height: 1.3;
}

.instructions p:before {
    content: "→";
    color: #667eea;
    font-weight: bold;
    margin-right: 8px;
}

.keyboard-controls {
    margin-bottom: 15px;
    padding: 12px;
    background: rgba(118, 75, 162, 0.1);
    border-radius: 8px;
    border-left: 4px solid #764ba2;
}

.keyboard-controls h4 {
    color: #333;
    margin-bottom: 8px;
    font-size: 14px;
    font-weight: 600;
}

.keyboard-controls p {
    margin: 3px 0;
    color: #555;
    font-size: 12px;
    line-height: 1.3;
}

.keyboard-controls strong {
    color: #764ba2;
    font-weight: 600;
}

/* 按鈕樣式 */
button {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    padding: 8px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    margin: 3px 0;
    transition: all 0.3s ease;
    box-shadow: 0 3px 10px rgba(102, 126, 234, 0.3);
    display: block;
    width: 100%;
}

button:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

button:active {
    transform: translateY(0);
}

button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

/* 撤銷按鈕特殊樣式 */
#undo-btn {
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
    box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
}

#undo-btn:hover:not(:disabled) {
    box-shadow: 0 6px 20px rgba(255, 107, 107, 0.4);
}

/* 狀態顯示 */
#status {
    position: absolute;
    bottom: 70px;
    right: 70px;
    z-index: 20;
    max-width: 360px;
    transform: scale(1.2);
    transform-origin: bottom right;
}

#message-history {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: flex-end;
}

.message-bubble {
    background: var(--bubble-bg, rgba(255, 255, 255, 0.95));
    padding: 10px 14px;
    border-radius: 0;
    box-shadow: none;
    border: 1px solid rgba(0, 0, 0, 0.1);
    font-size: 14px;
    color: #333;
    font-weight: 400;
    font-family: 'PingFang TC', 'Noto Sans TC', 'Microsoft JhengHei', sans-serif;
    animation: slideIn 0.3s ease-out;
    position: relative;
    width: fit-content;
    max-width: 320px;
    align-self: flex-end;
}

.message-bubble.message-typing {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 6px;
    background: rgba(255, 255, 255, 0.8);
}

.message-bubble.message-oldest {
    opacity: 0.3;
    transition: opacity 0.4s ease;
}

.message-bubble.message-older {
    opacity: 0.6;
    transition: opacity 0.4s ease;
}

.message-bubble.message-latest {
    opacity: 1;
    transition: opacity 0.2s ease;
}

.typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #555555;
    opacity: 0.3;
    animation: typingBlink 1s infinite ease-in-out;
}

.typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typingBlink {
    0% { opacity: 0.2; transform: translateY(0); }
    20% { opacity: 1; transform: translateY(-1px); }
    100% { opacity: 0.2; transform: translateY(0); }
}

.message-bubble::before {
    display: none;
    content: none;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message-bubble.fade-out {
    animation: fadeOut 0.5s ease-out forwards;
}

@keyframes fadeOut {
    from {
        opacity: 1;
        transform: translateY(0);
    }
    to {
        opacity: 0;
        transform: translateY(-10px);
    }
}

#drawing-status {
    color: #333;
    font-weight: 500;
    font-size: 13px;
}

/* 響應式設計 */
@media (max-width: 768px) {
    #ruler-svg {
        width: 98vw;
        height: 98vh;
        max-width: 100vw;
        max-height: 100vh;
    }
    
    #control-panel {
        top: 10px;
        right: 10px;
        left: 10px;
        min-width: auto;
    }
    
    #status {
        bottom: 20px;
        right: 20px;
        left: auto;
        text-align: right;
        transform: scale(1.1);
        transform-origin: bottom right;
    }
}

/* 繪圖狀態指示 */
.drawing-active #drawing-canvas {
    cursor: crosshair;
}

.drawing-complete #drawing-canvas {
    cursor: crosshair;
}

/* 動畫效果 */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

#control-panel, #status {
    animation: fadeIn 0.6s ease-out;
}

/* 成功完成的視覺回饋 */
.success-feedback {
    animation: pulse 0.6s ease-in-out;
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

/* 視頻播放器樣式 */
#video-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.7);
    z-index: 9999;
    display: none;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(2px);
    animation: fadeIn 0.3s ease-out;
}

#video-container {
    background: #000;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 25px 80px rgba(0, 0, 0, 0.8);
    border: 3px solid rgba(255, 255, 255, 0.1);
    transform: scale(0.9);
    animation: popIn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) forwards; /* 彈性緩動，增加衝擊力 */
    max-width: 70vw;
    max-height: 70vh;
    position: relative;
}

#completion-video {
    width: 100%;
    height: 100%;
    display: block;
    border-radius: 13px;
}

/* 彈跳動畫 */
@keyframes popIn {
    0% {
        transform: scale(0.8) translateY(120vh); /* 從螢幕下方開始 */
        opacity: 0;
    }
    60% {
        transform: scale(1.05) translateY(-15px); /* 稍微超過目標位置，增加彈性 */
        opacity: 0.95;
    }
    100% {
        transform: scale(1) translateY(0);
        opacity: 1;
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes fadeOut {
    from {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}

@keyframes popOut {
    0% {
        transform: scale(1) translateY(0);
        opacity: 1;
    }
    100% {
        transform: scale(0.7) translateY(50px);
        opacity: 0;
    }
}

/* 關閉按鈕 - 固定在頁面右上角 */
#video-close-btn {
    position: fixed;
    top: 20px;
    right: 20px;
    width: 50px;
    height: 50px;
    background: rgba(255, 255, 255, 0.95);
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    color: #333;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: all 0.3s ease;
    z-index: 10001;
}

#video-close-btn:hover {
    background: #fff;
    transform: scale(1.1);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
}

#video-close-btn:before {
    content: '×';
    font-weight: 300;
    text-shadow: 0 0 0.5px currentColor;
}

/* 懸浮視窗 */
.floating-window {
    position: fixed;
    left: 50px;
    top: calc(50% - 35vh - 60px); /* 列表頂部位置減去偏移量 */
    margin-left: 80px; /* 往右移80px (50px + 30px) */
    margin-top: 100px; /* 往下移100px */
    transform: translateY(-50%) scale(2.68) translateY(-10px);
    width: 120px;
    height: auto;
    animation: floatAnimation 2s ease-in-out infinite;
    z-index: 100;
    pointer-events: none;
}

@keyframes floatAnimation {
    0%, 100% {
        transform: translateY(-50%) scale(2.68) translateY(-10px) translateY(0);
    }
    50% {
        transform: translateY(-50%) scale(2.78) translateY(-10px) translateY(-10px);
    }
}
