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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    color: #333;
    position: relative;
    min-height: 100vh;
    overflow-x: hidden;
}

/* 動態背景效果 */
.background-animation {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    background: linear-gradient(135deg, #667eea, #764ba2);
    overflow: hidden;
}

.animated-bg-item {
    position: absolute;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    animation: float-animation 15s infinite ease-in-out;
}

@keyframes float-animation {
    0%, 100% {
        transform: translate(0, 0) rotate(0deg) scale(1);
    }
    25% {
        transform: translate(10%, 15%) rotate(5deg) scale(1.1);
    }
    50% {
        transform: translate(5%, 10%) rotate(10deg) scale(0.9);
    }
    75% {
        transform: translate(-5%, 5%) rotate(-5deg) scale(1.05);
    }
}

.container {
    max-width: 1800px;
    width: 95%;
    margin: 0 auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

header {
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    padding: 20px;
    border-radius: 15px;
    text-align: center;
    box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.4);
}

header h1 {
    color: #2c3e50;
    margin-bottom: 15px;
    font-size: 2.5em;
}

.info-text {
    color: #7f8c8d;
    font-size: 16px;
}

.info-text p {
    margin: 0;
}

.workspace {
    display: grid;
    grid-template-columns: 260px 1fr;
    gap: 25px;
}

.components-panel {
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    padding: 25px;
    border-radius: 15px;
    box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.4);
    height: fit-content;
    display: flex;
    flex-direction: column;
    gap: 25px;
    position: sticky;
    top: 20px;
}

.components-panel h3 {
    color: #2c3e50;
    text-align: center;
    margin: 0;
    padding-bottom: 10px;
    border-bottom: 1px solid #eee;
}

.component-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.component-group h4 {
    color: #34495e;
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin: 0;
    padding: 5px 0;
    border-bottom: 1px dashed #eee;
}

.component {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 60px;
    height: 60px;
    margin: 5px;
    border-radius: 10px; /* 組件預設圓角 */
    cursor: grab;
    user-select: none;
    font-weight: bold;
    font-size: 14px;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    box-shadow: 
        inset 0 0 10px rgba(255,255,255,0.3),
        0 4px 8px rgba(0, 0, 0, 0.2);
    text-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

.component:hover {
    transform: scale(1.15) translateY(-2px);
    box-shadow: 
        inset 0 0 15px rgba(255,255,255,0.4),
        0 8px 15px rgba(0, 0, 0, 0.3);
    z-index: 10;
}

.component:active {
    cursor: grabbing;
}

/* 確保磷酸基在組件庫和畫布上都是圓形 */
.component.phosphate,
.canvas-component.phosphate {
    background: linear-gradient(135deg, #e74c3c, #c0392b);
    color: white;
    border-radius: 50% !important; /* 強制圓形 - 代表磷酸基團 */
    clip-path: none !important;
    width: 60px !important;
    height: 60px !important;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.sugar,
.canvas-component.sugar {
    color: white;
    font-weight: bold;
    font-size: 12px;
    /* 五邊形形狀 - 代表五碳醣 */
    clip-path: polygon(50% 0%, 100% 38%, 82% 100%, 18% 100%, 0% 38%) !important;
    width: 60px !important;
    height: 60px !important;
    border: 2px solid rgba(255, 255, 255, 0.2);
}

.sugar.deoxyribose,
.canvas-component.sugar.deoxyribose {
    background: linear-gradient(135deg, #2ecc71, #27ae60);
}

.sugar.ribose,
.canvas-component.sugar.ribose {
    background: linear-gradient(135deg, #3498db, #2980b9);
}

.bases-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    place-items: center;
}

.base,
.canvas-component.base {
    font-size: 24px;
    font-weight: bold;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    /* 六邊形 - 代表含氮鹼基 */
    clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%) !important;
    width: 60px !important;
    height: 60px !important;
    border: 2px solid rgba(255, 255, 255, 0.2);
}

.adenine,
.canvas-component.adenine {
    background: linear-gradient(135deg, #e74c3c, #c0392b);
}

.thymine,
.canvas-component.thymine {
    background: linear-gradient(135deg, #3498db, #2980b9);
}

.cytosine,
.canvas-component.cytosine {
    background: linear-gradient(135deg, #f1c40f, #f39c12);
}

.guanine,
.canvas-component.guanine {
    background: linear-gradient(135deg, #9b59b6, #8e44ad);
}

.uracil,
.canvas-component.uracil {
    background: linear-gradient(135deg, #1abc9c, #16a085);
}

.assembly-area {
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    padding: 25px;
    border-radius: 15px;
    box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.4);
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.assembly-area h3 {
    color: #2c3e50;
    text-align: center;
    margin: 0;
    padding-bottom: 10px;
    border-bottom: 1px solid #eee;
}

.work-canvas {
    min-height: 70vh;
    border: 2px solid rgba(189, 195, 199, 0.6);
    border-radius: 15px;
    background-color: rgba(245, 247, 250, 0.6);
    background-image: 
        linear-gradient(rgba(0, 0, 0, 0.05) 1px, transparent 1px),
        linear-gradient(90deg, rgba(0, 0, 0, 0.05) 1px, transparent 1px);
    background-size: 20px 20px;
    box-shadow: inset 0 0 20px rgba(0,0,0,0.05);
    position: relative;
    overflow: hidden;
    transition: border-color 0.3s ease, background-color 0.3s ease;
}

.work-canvas.drag-over {
    border-color: #3498db;
    background-color: rgba(52, 152, 219, 0.15);
}

.canvas-hint {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #95a5a6;
    font-size: 18px;
    font-weight: bold;
    pointer-events: none;
    z-index: 0;
}

.canvas-component {
    position: absolute;
    width: 60px;
    height: 60px;
    cursor: grab;
    user-select: none;
    font-weight: bold;
    font-size: 14px;
    transition: box-shadow 0.3s ease, border 0.3s ease, --component-scale 0.2s ease, --component-rotate 0.2s ease; /* 過渡效果 */
    box-shadow: 
        inset 0 0 10px rgba(255,255,255,0.3),
        0 4px 8px rgba(0, 0, 0, 0.2);
    text-shadow: 0 1px 2px rgba(0,0,0,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1;
    /* 使用 CSS 變數來控制 transform */
    transform: rotate(var(--component-rotate, 0deg)) scale(var(--component-scale, 1));
}

.canvas-component:hover {
    box-shadow: 
        inset 0 0 15px rgba(255,255,255,0.4),
        0 8px 15px rgba(0, 0, 0, 0.3);
    z-index: 10;
}

.canvas-component:not(.dragging):not(.selected):not(.rotation-mode):hover {
    transform: rotate(var(--component-rotate, 0deg)) scale(1.05);
}

.canvas-component:active {
    cursor: grabbing;
}

.canvas-component.dragging {
    opacity: 0.8;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
    z-index: 100;
    cursor: grabbing !important;
}

.canvas-component.selected {
    /* transform: scale(1.1); */ /* 移除此行，scale 由 --component-scale 控制 */
    animation: pulse 1.5s infinite; /* pulse 動畫只應影響 box-shadow */
    border: 3px dashed #3498db;
    /* box-shadow 由 pulse 動畫控制 */
    z-index: 20;
    cursor: crosshair;
}

@keyframes pulse { /* 確保 pulse 動畫不修改 transform scale */
    0% { box-shadow: 0 0 15px rgba(52, 152, 219, 0.7); }
    50% { box-shadow: 0 0 20px rgba(52, 152, 219, 0.9); }
    100% { box-shadow: 0 0 15px rgba(52, 152, 219, 0.7); }
}

/* 連線樣式 */
.connection-line {
    position: absolute;
    height: 3px;
    background: #2c3e50;
    z-index: 5;
    transform-origin: left center;
    box-shadow: 0 0 4px rgba(44, 62, 80, 0.5);
    border-radius: 1px;
    pointer-events: all;
    cursor: pointer;
    /* 移除transition以提高效能 */
}

.connection-line.invalid {
    background: linear-gradient(90deg, #e74c3c, #c0392b);
    box-shadow: 0 0 8px rgba(231, 76, 60, 0.6);
    height: 5px;
    z-index: 16;
}

.temp-line {
    background: repeating-linear-gradient(
        90deg,
        #3498db 0px,
        #3498db 5px,
        transparent 5px,
        transparent 10px
    );
    opacity: 0.6;
    pointer-events: none;
    height: 3px;
    z-index: 15;
}

/* 連線視覺效果改進 */
.connection-line:hover {
    filter: brightness(1.2);
    transform-origin: left center;
}

/* 不同類型連線的顏色區分 - 更科學的表示 */
.connection-line.strong-bond {
    /* 共價鍵：粗實線 */
    background: #2c3e50;
    height: 4px;
    box-shadow: 0 0 4px rgba(44, 62, 80, 0.8);
}

.connection-line.weak-bond {
    /* 糖苷鍵：波浪線效果 */
    background: repeating-linear-gradient(
        90deg,
        #e67e22 0px,
        #e67e22 8px,
        transparent 8px,
        transparent 12px
    );
    height: 3px;
    box-shadow: 0 0 4px rgba(230, 126, 34, 0.5);
}

.connection-line.hydrogen-bond {
    /* 氫鍵：虛線（較弱的鍵） */
    background: repeating-linear-gradient(
        90deg,
        #3498db 0px,
        #3498db 8px,
        transparent 8px,
        transparent 16px
    );
    height: 3px;
    box-shadow: 0 0 6px rgba(52, 152, 219, 0.6);
}

/* 加入鍵盤操作提示 */
.key-hint {
    position: absolute;
    bottom: 10px;
    left: 10px;
    padding: 5px 10px;
    background: rgba(255, 255, 255, 0.8);
    border-radius: 5px;
    font-size: 12px;
    color: #666;
    display: none;
}

.work-canvas.connection-mode .key-hint {
    display: block;
}

.controls {
    display: flex;
    gap: 15px;
    justify-content: center;
}

.controls button {
    padding: 12px 24px;
    border: none;
    border-radius: 25px;
    cursor: pointer;
    font-size: 16px;
    font-weight: bold;
    transition: all 0.3s ease;
    background: linear-gradient(135deg, #3498db, #2980b9);
    color: white;
    flex: 1;
    max-width: 180px;
    box-shadow: 0 4px 10px rgba(52, 152, 219, 0.3);
    text-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

.controls button:hover {
    background: linear-gradient(135deg, #2980b9, #2573a7);
    transform: translateY(-2px);
    box-shadow: 0 6px 15px rgba(52, 152, 219, 0.4);
}

.controls button:active {
    transform: translateY(1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.validation-result {
    text-align: center;
    padding: 15px;
    border-radius: 10px;
    font-weight: bold;
    min-height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.validation-result.success {
    background: rgba(46, 204, 113, 0.2);
    color: #27ae60;
    border: 2px solid #27ae60;
}

.validation-result.error {
    background: rgba(231, 76, 60, 0.2);
    color: #e74c3c;
    border: 2px solid #e74c3c;
}

.validation-result.info {
    background: rgba(52, 152, 219, 0.2);
    color: #3498db;
    border: 2px solid #3498db;
}

.info-panel {
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    padding: 25px;
    border-radius: 15px;
    box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.4);
    text-align: center;
}

.info-panel h3 {
    color: #2c3e50;
    margin-bottom: 15px;
    border-bottom: 1px solid #eee;
    padding-bottom: 10px;
}

.info-panel ul {
    list-style: none;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.info-panel li {
    padding: 10px;
    background: rgba(52, 152, 219, 0.1);
    border-radius: 8px;
    font-weight: bold;
    text-align: left;
}

/* 雙擊提示 */
.info-panel li:last-child::after {
    content: "（雙擊組件可刪除所有連線）";
    display: block;
    font-size: 0.9em;
    color: #e74c3c;
    margin-top: 5px;
}

.canvas-component.dragging {
    opacity: 0.8;
    z-index: 100;
    cursor: grabbing !important;
}

/* 糖類形狀已在上方統一定義，此處保持一致性 */
.component.sugar.deoxyribose,
.component.sugar.ribose,
.canvas-component.sugar.deoxyribose,
.canvas-component.sugar.ribose {
    border-radius: 0 !important;
}

@media (max-width: 768px) {
    .workspace {
        grid-template-columns: 1fr;
    }
    
    .work-canvas {
        min-height: 300px;
    }
    
    .bases-container {
        grid-template-columns: repeat(4, 1fr);
    }
    
    .component {
        width: 50px;
        height: 50px;
    }
    
    .canvas-component {
        width: 50px;
        height: 50px;
        font-size: 12px;
    }
}

/* 旋轉模式樣式 */
.canvas-component.rotation-mode {
    border: 3px solid #e74c3c;
    box-shadow: 0 0 20px rgba(231, 76, 60, 0.8);
    animation: rotation-pulse 2s infinite;
    z-index: 50;
}

@keyframes rotation-pulse {
    0% { 
        box-shadow: 0 0 20px rgba(231, 76, 60, 0.8);
        border-color: #e74c3c;
    }
    50% { 
        box-shadow: 0 0 30px rgba(231, 76, 60, 1);
        border-color: #c0392b;
    }
    100% { 
        box-shadow: 0 0 20px rgba(231, 76, 60, 0.8);
        border-color: #e74c3c;
    }
}

/* 工作畫布在旋轉模式下的樣式 */
.work-canvas.rotation-mode {
    cursor: grab;
}

.work-canvas.rotation-mode:active {
    cursor: grabbing;
}

/* 化學鍵類型示例 */
.bond-example {
    display: inline-block;
    width: 40px;
    height: 3px;
    vertical-align: middle;
    margin-right: 8px;
}

.bond-example.strong-bond {
    background: #2c3e50;
    height: 4px;
}

.bond-example.weak-bond {
    background: repeating-linear-gradient(
        90deg,
        #e67e22 0px,
        #e67e22 8px,
        transparent 8px,
        transparent 12px
    );
    height: 3px;
}

.bond-example.hydrogen-bond {
    background: repeating-linear-gradient(
        90deg,
        #3498db 0px,
        #3498db 8px,
        transparent 8px,
        transparent 16px
    );
    height: 3px;
}

/* 模式指示器 */
.mode-indicator {
    position: absolute;
    top: 10px;
    right: 10px;
    padding: 8px 15px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    border-radius: 20px;
    font-size: 12px;
    font-weight: bold;
    z-index: 100;
    display: none;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.mode-indicator.show {
    display: block;
    opacity: 1;
}

.mode-indicator.connection-mode {
    background: rgba(52, 152, 219, 0.9);
}

.mode-indicator.rotation-mode {
    background: rgba(231, 76, 60, 0.9);
}