.apage-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
    background-color: var(--bg-primary);
}

.left-panel {
    flex: 2;
    background-color: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.top-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    background-color: var(--bg-card);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.page-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

.top-actions {
    display: flex;
    align-items: center;
}

.template-name-input {
    margin-right: 12px;
}

.template-name-input input {
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    min-width: 220px;
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    transition: border-color 0.2s ease;
}

.template-name-input input:focus {
    outline: none;
    border-color: var(--accent);
}

.template-name-input input::placeholder {
    color: var(--text-muted);
}

.action-btn {
    margin-left: 10px;
    font-weight: 500;
    padding: 10px 18px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s ease;
}

.back-btn {
    background-color: var(--bg-card-hover);
    color: var(--text-secondary);
    padding: 10px 14px;
}

.back-btn:hover {
    background-color: var(--border);
    color: var(--text-primary);
}

.clear-btn {
    background-color: var(--bg-card-hover);
    color: var(--text-secondary);
}

.clear-btn:hover {
    background-color: var(--border);
    color: var(--text-primary);
}

.preview-btn {
    background-color: var(--bg-card-hover);
    color: var(--text-secondary);
}

.preview-btn:hover {
    background-color: var(--border);
    color: var(--text-primary);
}

.save-btn {
    background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
    color: white;
    box-shadow: 0 2px 8px rgba(91, 155, 213, 0.3);
}

.save-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(91, 155, 213, 0.4);
}

.save-btn:disabled {
    background: linear-gradient(135deg, #a8a8a8, #8a8a8a);
    box-shadow: none;
    cursor: not-allowed;
    opacity: 0.75;
    transform: none;
}

.modules-container {
    flex: 0 1 auto;
    align-self: center;
    overflow-y: auto;
    padding: 24px;
    min-height: 400px;
    max-width: 100%;
    width: 680px;
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: width 0.15s ease;
}

/* 编辑区宽度控制条 */
.canvas-size-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 24px;
    background-color: var(--bg-card);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.canvas-size-title {
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 6px;
}

.canvas-size-title i {
    color: var(--accent);
}

.canvas-size-bar input[type="range"] {
    flex: 1;
    max-width: 420px;
    accent-color: var(--accent);
    cursor: pointer;
}

.canvas-size-label {
    font-size: 12px;
    color: var(--accent);
    font-weight: 600;
    min-width: 52px;
    text-align: right;
    white-space: nowrap;
}

.added-module {
    position: relative;
    border: 1px solid #323232;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 16px;
    background-color: #252525;
    transition: all 0.3s ease;
    max-width: 800px;
    width: 100%;
}

/* 模块内图片自适应容器宽度，防止大图撑破布局 */
.added-module img {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    display: block;
    margin: 0 auto;
}

.added-module:hover {
    border-color: var(--accent);
    box-shadow: 0 4px 16px rgba(91, 155, 213, 0.15);
    background-color: #2a2a2a;
}

.added-module:hover .module-actions {
    display: flex;
}

.module-size-info {
    position: absolute;
    top: 40px;
    right: 12px;
    background-color: rgba(91, 155, 213, 0.9);
    color: white;
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 500;
    text-align: right;
    line-height: 1.5;
    box-shadow: 0 2px 8px rgba(91, 155, 213, 0.3);
    max-width: 200px;
    word-wrap: break-word;
}

.module-actions {
    position: absolute;
    top: 10px;
    right: 10px;
    display: none;
    background-color: var(--bg-secondary);
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    padding: 4px;
    align-items: center;
}

.action-icon {
    width: 28px;
    height: 28px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    margin-left: 4px;
    border-radius: 6px;
    transition: all 0.2s ease;
    color: var(--text-secondary);
}

.action-icon:hover {
    background-color: var(--bg-card-hover);
    color: var(--text-primary);
}

.drag-icon {
    cursor: grab;
}

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

.delete-icon:hover {
    color: #ef4444;
    background-color: rgba(239, 68, 68, 0.1);
}

.added-module.drag-source {
    opacity: 0.5;
    border-color: var(--accent);
    border-style: dashed;
}

.drag-clone {
    opacity: 0.92;
    box-shadow: 0 16px 48px rgba(91, 155, 213, 0.55), 0 0 0 1px var(--accent);
    border-color: var(--accent);
    border-radius: 12px;
    transform: rotate(2deg) scale(1.02);
    cursor: grabbing;
    /* 必须禁用过渡：克隆体每帧更新 left/top，若继承 .added-module 的
       transition: all 0.3s 会导致拖动严重滞后、不跟手 */
    transition: none !important;
}

.drag-indicator {
    height: 4px;
    background: linear-gradient(90deg, var(--gradient-start), var(--gradient-end));
    margin: 10px 0;
    border-radius: 2px;
    opacity: 1;
    box-shadow: 0 2px 8px rgba(91, 155, 213, 0.4);
    animation: indicatorPulse 1s ease infinite;
    /* 容器是 flex column + align-items:center，指示线作为 flex 子项会被压缩成 0×0，
       需保持自身高度不被收缩、宽度拉伸与容器一致，才能正常渲染出蓝色落点线 */
    flex: 0 0 auto;
    align-self: stretch;
}

@keyframes indicatorPulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.55; }
}

.drag-indicator.visible {
    opacity: 1;
}

.right-panel {
    flex: 1;
    background-color: var(--bg-secondary);
    display: flex;
    flex-direction: column;
}

.tab-container {
    display: flex;
    background-color: var(--bg-card);
    border-bottom: 1px solid var(--border);
}

.tab-btn {
    flex: 1;
    padding: 14px;
    background-color: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    border-radius: 0;
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s ease;
}

.tab-btn:hover {
    color: var(--text-primary);
    background-color: var(--bg-card-hover);
}

.tab-btn.active {
    background-color: transparent;
    border-bottom-color: var(--accent);
    color: var(--accent);
    font-weight: 600;
}

.modules-list {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background-color: #1f1f1f;
}

.module-category {
    display: none;
    column-count: 2;
    column-gap: 12px;
}

.module-category.active {
    display: block;
}

.module-item {
    background-color: #252525;
    border: 1px solid #323232;
    border-radius: 10px;
    padding: 12px;
    margin-bottom: 12px;
    cursor: pointer;
    transition: all 0.3s ease;
    text-align: center;
    break-inside: avoid;
    width: 100%;
}

.module-item {
    position: relative;
}

.module-item:hover {
    border-color: var(--accent);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(91, 155, 213, 0.2);
}

.module-item h3 {
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 10px;
    color: var(--text-primary);
    line-height: 1.4;
}

.module-item img {
    max-width: 100%;
    height: auto;
    border-radius: 6px;
    border: 1px solid var(--border);
}

.module-item.added {
    opacity: 0.5;
    background-color: var(--bg-secondary);
    border-color: var(--border);
}

.module-item.added:hover {
    box-shadow: none;
    transform: none;
    border-color: var(--border);
}

.module-item.added h3 {
    color: var(--text-muted);
}

.module-item.added img {
    filter: grayscale(100%);
    opacity: 0.6;
    border-color: var(--border);
}

.module-item.added .module-size {
    background-color: rgba(113, 113, 122, 0.9);
}

.module-size {
    position: absolute;
    bottom: 10px;
    right: 10px;
    background-color: rgba(91, 155, 213, 0.9);
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 500;
    text-align: right;
    line-height: 1.5;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.size-dot {
    display: inline-block;
    background-color: #ef4444;
    color: white;
    border-radius: 2px;
    padding: 0 4px;
    font-size: 9px;
    line-height: 14px;
    margin: 0 2px;
    vertical-align: middle;
    font-weight: bold;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-right: 40px;
    position: relative;
}

.modal-header.draggable {
    cursor: move;
    user-select: none;
}

.modal-header.draggable:active {
    cursor: grabbing;
}

.modal-header h2 {
    margin: 0;
    color: var(--text-primary);
    font-size: 18px;
    font-weight: 600;
}

.close {
    position: absolute;
    top: 16px;
    right: 16px;
    font-size: 24px;
    font-weight: bold;
    color: var(--text-muted);
    cursor: pointer;
    z-index: 10;
    transition: color 0.2s ease;
}

.close:hover {
    color: var(--text-primary);
}

.modal-content {
    position: relative;
    background-color: var(--bg-card);
    margin: 10% auto;
    padding: 24px;
    border: 1px solid var(--border);
    border-radius: 12px;
    width: 85%;
    max-width: 900px;
}

.preview-content {
    padding: 20px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background-color: var(--bg-secondary);
    display: flex;
    flex-direction: column;
    align-items: center;
}

.preview-content .added-module {
    margin-bottom: 16px;
    max-width: 680px;
    width: 100%;
}

.preview-content .added-module .module-actions {
    display: none !important;
}

.preview-content .added-module:hover .module-actions {
    display: none !important;
}

.module-edit-form {
    background-color: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 20px;
    max-width: 680px;
    width: 100%;
    align-self: center;
}

.module-edit-form h2 {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 20px;
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.form-control {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    background-color: var(--bg-secondary);
    color: var(--text-primary);
}

.form-control:focus {
    outline: none;
    border-color: var(--accent);
}

.image-size-item {
    display: flex;
    gap: 12px;
    margin-bottom: 8px;
    align-items: center;
}

.image-size-item input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background-color: var(--bg-secondary);
    color: var(--text-primary);
}

.remove-size-btn {
    background-color: #ef4444;
    color: white;
    border: none;
    padding: 8px 14px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
    transition: background-color 0.2s ease;
}

.remove-size-btn:hover {
    background-color: #dc2626;
}

.module-image-display {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    background-color: var(--bg-secondary);
    min-height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.module-image-display img {
    max-height: 60px;
    max-width: 100%;
    height: auto;
    border-radius: 4px;
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.form-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.form-header h2 {
    margin: 0;
}

.secondary-btn {
    background-color: var(--bg-card-hover);
    color: var(--text-secondary);
}

.secondary-btn:hover {
    background-color: var(--border);
    color: var(--text-primary);
}

.delete-btn {
    background-color: #ef4444;
    color: white;
}

.delete-btn:hover {
    background-color: #dc2626;
}

.small-btn {
    padding: 6px 12px;
    font-size: 13px;
}

.image-sizes-wrapper {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.code-editor {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 13px;
    line-height: 1.6;
    min-height: 200px;
    resize: vertical;
}

.preview-container {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 16px;
    background-color: white;
    min-height: 150px;
}

.preview-iframe {
    width: 100%;
    min-height: 150px;
    border: none;
    border-radius: 4px;
}

.empty-placeholder {
    color: var(--text-muted);
    font-size: 14px;
    text-align: center;
    margin: 0;
    padding: 20px;
}

/* 参与AI运算 开关 */
.switch-toggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    user-select: none;
}

/* 表单内 label 默认 display:block，覆盖以保持开关 flex 布局 */
.form-group .switch-toggle {
    display: inline-flex;
    font-weight: normal;
    margin-bottom: 0;
}

.switch-toggle input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.switch-slider {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 22px;
    background-color: #555;
    border-radius: 22px;
    transition: background-color 0.2s ease;
    flex-shrink: 0;
}

.switch-slider::before {
    content: "";
    position: absolute;
    top: 3px;
    left: 3px;
    width: 16px;
    height: 16px;
    background-color: #fff;
    border-radius: 50%;
    transition: transform 0.2s ease;
}

.switch-toggle input:checked + .switch-slider {
    background-color: var(--accent);
}

.switch-toggle input:checked + .switch-slider::before {
    transform: translateX(18px);
}

.switch-text {
    font-size: 12px;
    color: var(--text-secondary);
}

/* 模块卡片中的开关：紧凑布局，靠左对齐，避免与尺寸角标重叠 */
.module-item .ai-compute-toggle {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 10px;
    padding-right: 54px;
    cursor: pointer;
    user-select: none;
}

.module-item .ai-compute-toggle .switch-slider {
    width: 32px;
    height: 18px;
}

.module-item .ai-compute-toggle .switch-slider::before {
    top: 2px;
    left: 2px;
    width: 14px;
    height: 14px;
}

.module-item .ai-compute-toggle input:checked + .switch-slider::before {
    transform: translateX(14px);
}

.module-item .ai-compute-toggle .switch-text {
    font-size: 11px;
}

.toast {
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 14px 24px;
    border-radius: 8px;
    color: white;
    font-weight: 500;
    z-index: 9999;
    display: none;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

.toast.success {
    background: linear-gradient(135deg, #10b981, #059669);
}

.toast.error {
    background: linear-gradient(135deg, #ef4444, #dc2626);
}

.toast.warning {
    background: linear-gradient(135deg, #f59e0b, #d97706);
}

@media (max-width: 768px) {
    .apage-container {
        flex-direction: column;
    }
    
    .left-panel,
    .right-panel {
        flex: none;
        height: 50vh;
    }
    
    .right-panel {
        border-top: 1px solid var(--border);
        border-right: none;
    }
    
    .top-header {
        flex-direction: column;
        gap: 12px;
        align-items: stretch;
    }
    
    .header-left {
        justify-content: space-between;
    }
    
    .top-actions {
        justify-content: flex-end;
    }
    
    .template-name-input input {
        min-width: 100%;
    }
    
    .module-category {
        column-count: 1;
    }
    
    .image-size-item {
        flex-direction: column;
        align-items: stretch;
    }
    
    .image-size-item input {
        margin-bottom: 8px;
    }
    
    .form-header {
        flex-direction: column;
        gap: 12px;
        align-items: stretch;
    }
    
    .form-header .action-btn {
        width: 100%;
        margin: 0;
    }
}