* {
    box-sizing: border-box;
    user-select: none;
}

body {
    margin: 0;
    overflow: hidden;
    font-family: "Segoe UI", sans-serif;
    height: 100vh;
    display: flex;
    flex-direction: column;   /* 新增：使 body 变为列布局 */
    max-height: 100vh;        /* 防止安卓地址栏问题导致溢出 */
    min-height: 0;
    position: fixed;          /* 锁定视口，避免安卓地址栏缩放抖动 */
    top: 0; left: 0; right: 0;
}

#top-bar {
    flex-shrink: 0;           /* 确保顶部栏不收缩 */
}

#app-container {
    display: flex;
    flex: 1;                  /* 占据剩余高度 */
    width: 100vw;
}


#canvas-wrapper {
    flex: 0 0 60%;
    position: relative;
    background: #f5f2ed;
    overflow: hidden;
    min-width: 200px;
}
#connection-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
}

/* 画布图层：承载节点，支持整体缩放 */
#canvas-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    transform-origin: 0 0;
    z-index: 2;
    pointer-events: none;
}
#canvas-layer .node {
    pointer-events: auto;
}

#splitter {
    flex: 0 0 6px;
    background: #666;
    cursor: col-resize;
    transition: background 0.15s;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
}

#splitter:hover,
#splitter.active {
    background: #ffaa00;
}

#splitter::after {
    content: '⋮';
    color: #fff;
    font-size: 16px;
    line-height: 1;
    opacity: 0.7;
    pointer-events: none;
}

#right-panel {
    flex: 0 0 40%;
    background: #2d2d2d;
    color: #eee;
    display: flex;
    flex-direction: column;
    min-width: 200px;
    overflow: hidden;
}

#right-panel .tabs {
    display: flex;
    background: #222;
    border-bottom: 1px solid #444;
    flex-shrink: 0;
}

#right-panel .tab-btn {
    flex: 1;
    padding: 10px 0;
    background: transparent;
    border: none;
    color: #aaa;
    font-size: 14px;
    cursor: pointer;
    transition: 0.2s;
    border-bottom: 3px solid transparent;
}

#right-panel .tab-btn.active {
    color: #ffd700;
    border-bottom-color: #ffd700;
    background: #2a2a2a;
}

#right-panel .tab-content {
    flex: 1;
    display: none;
    flex-direction: column;
    padding: 16px;
    overflow: hidden;
}

#right-panel .tab-content.active {
    display: flex;
}

#code-output {
    flex: 1;
    background: #1e1e1e;
    border: none;
    color: #d4d4d4;
    font-family: 'Consolas', monospace;
    font-size: 13px;
    padding: 12px;
    border-radius: 6px;
    resize: none;
    white-space: pre;
    overflow: auto;
    width: 100%;
}

#code-output:focus {
    outline: 1px solid #ffd700;
}

.btn-group {
    margin-top: 12px;
    display: flex;
    gap: 8px;
    flex-shrink: 0;
    flex-wrap: wrap;
}

.btn-group button {
    flex: 1 1 auto;
    padding: 8px;
    background: #3a3a3a;
    border: none;
    border-radius: 4px;
    color: #fff;
    cursor: pointer;
    transition: 0.2s;
    font-size: 13px;
}

.btn-group button:hover {
    background: #555;
}

.btn-group button.primary {
    background: #ffd700;
    color: #222;
}

.btn-group button.primary:hover {
    background: #ffe44d;
}

.btn-group button.debug {
    background: #6c5ce7;
    color: #fff;
}

.btn-group button.debug:hover {
    background: #7d6ff0;
}

/* 标签管理界面 */
#label-manager {
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow-y: auto;
    flex: 1;
}

#label-manager .label-item {
    display: flex;
    align-items: center;
    gap: 8px;
    background: #3a3a3a;
    padding: 6px 12px;
    border-radius: 4px;
    flex-shrink: 0;
}

#label-manager .label-item span {
    flex: 1;
    color: #eee;
}

#label-manager .label-item button {
    background: #555;
    border: none;
    color: #fff;
    border-radius: 4px;
    padding: 2px 10px;
    cursor: pointer;
}

#label-manager .label-item button:hover {
    background: #777;
}

#label-manager .add-label {
    display: flex;
    gap: 8px;
    margin-top: 8px;
    flex-shrink: 0;
}

#label-manager .add-label input {
    flex: 1;
    padding: 6px;
    border-radius: 4px;
    border: 1px solid #555;
    background: #3a3a3a;
    color: #fff;
}

#label-manager .add-label button {
    padding: 6px 16px;
    background: #ffd700;
    border: none;
    border-radius: 4px;
    color: #222;
    cursor: pointer;
}

/* 节点样式 (保持不变) */
.node {
    position: absolute;
    min-width: 170px;
    background: #e8e4de;
    border-radius: 10px;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
    z-index: 10;
    cursor: default;
    border: 2px solid transparent;
    transition: border-color 0.15s;
    touch-action: none;  /* 节点上触摸由拖动逻辑处理，禁止浏览器默认滚动/缩放 */
}

.node.selected {
    border-color: #ffaa00;
    box-shadow: 0 0 0 2px #ffaa0088;
}

.node-header {
    height: 30px;
    line-height: 30px;
    padding: 0 10px;
    border-radius: 8px 8px 0 0;
    color: white;
    font-weight: 600;
    font-size: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: grab;
}

.node-header:active {
    cursor: grabbing;
}

.node-header .node-type-badge {
    background: rgba(255, 255, 255, 0.2);
    padding: 0 8px;
    border-radius: 20px;
    font-size: 10px;
}

.node-header .close-btn {
    width: 16px;
    height: 16px;
    background: #ff3b30;
    border-radius: 50%;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 12px;
    line-height: 1;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.2);
    transition: 0.15s;
}

.node-header .close-btn:hover {
    transform: scale(1.15);
}

.node-body {
    padding: 8px 12px 12px;
}

.setting-item {
    margin-bottom: 6px;
    font-size: 12px;
}

.setting-item label {
    display: block;
    font-weight: 500;
    color: #333;
    margin-bottom: 2px;
    font-size: 11px;
}

.setting-item input,
.setting-item select {
    width: 100%;
    padding: 3px 6px;
    border: 1px solid #b0b0b0;
    border-radius: 4px;
    background: white;
    font-size: 12px;
    outline: none;
    transition: 0.15s;
}

.setting-item input:focus,
.setting-item select:focus {
    border-color: #2d2d2d;
    box-shadow: 0 0 0 2px rgba(45, 45, 45, 0.1);
}

.setting-item .btn {
    display: inline-block;
    padding: 3px 10px;
    background: #f0f0f0;
    border: 1px solid #b0b0b0;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    transition: 0.15s;
}

.setting-item .btn:hover {
    background: #e0e0e0;
}

.setting-item .btn-primary {
    background: #2d2d2d;
    color: white;
    border-color: #2d2d2d;
}

.setting-item .btn-primary:hover {
    background: #444;
}

.setting-item .checkbox-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.setting-item .checkbox-row input[type="checkbox"] {
    width: auto;
    margin: 0;
}

.setting-item .inline-group {
    display: flex;
    gap: 6px;
    align-items: center;
}

.setting-item .inline-group select {
    flex: 1;
}

/* 端口 */
.port {
    position: absolute;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 2px solid #fff;
    box-shadow: 0 0 0 2px #555;
    background: #888;
    cursor: crosshair;
    z-index: 20;
    transition: 0.1s;
}

/* 连线目标高亮（触屏拖线靠近时） */
.port.highlight-target {
    box-shadow: 0 0 0 4px #ffd700, 0 0 0 7px rgba(255, 215, 0, 0.4);
    transform: translateY(-50%) scale(1.4);
    background: #ffd700;
}

.port.input {
    left: -7px;
    top: 50%;
    transform: translateY(-50%);
    background: #4a90d9;
}

.port.output {
    right: -7px;
    top: 50%;
    transform: translateY(-50%);
    background: #e67e22;
}

.port:hover {
    transform: translateY(-50%) scale(1.3);
    box-shadow: 0 0 0 3px #ffaa00;
}

.port.connected {
    background: #2ecc71;
}

/* 右键菜单 */
.context-menu {
    position: fixed;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
    padding: 6px 0;
    min-width: 180px;
    z-index: 999;
    display: none;
}

.context-menu .menu-item {
    padding: 6px 20px;
    cursor: pointer;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: 0.1s;
    position: relative;
}

.context-menu .menu-item:hover {
    background: #f0f0f0;
}

.context-menu .menu-item .color-dot {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 3px;
}

.context-menu .menu-item .arrow {
    margin-left: auto;
    font-size: 12px;
    color: #888;
}

.context-menu .menu-divider {
    height: 1px;
    background: #ddd;
    margin: 4px 12px;
}

.context-menu .sub-menu {
    display: none;
    position: absolute;
    left: 100%;
    top: 0;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
    padding: 6px 0;
    min-width: 150px;
}

/* 触屏：点击展开子菜单（JS 控制），hover 保留作为鼠标增强 */
.context-menu .menu-item.has-sub:hover .sub-menu {
    display: block;
}

/* 覆盖层 - 物品选择器 */
.cover {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.cover.active {
    display: flex;
}

.cover-content {
    background: white;
    width: 600px;
    max-height: 80vh;
    border-radius: 12px;
    padding: 24px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.cover-content .search input {
    width: 100%;
    padding: 8px 14px;
    border: 1px solid #ccc;
    border-radius: 6px;
    font-size: 16px;
    outline: none;
}

.cover-content .search input:focus {
    border-color: #66afe9;
    box-shadow: 0 0 8px rgba(102, 175, 233, 0.3);
}

.cover-content .cards {
    flex: 1;
    overflow-y: auto;
    margin-top: 16px;
}

.card {
    display: flex;
    align-items: center;
    padding: 8px 14px;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    margin-bottom: 8px;
    cursor: pointer;
    transition: 0.1s;
}

.card:hover {
    background: #f7f7f7;
    border-color: #aaa;
}

.card img {
    width: 40px;
    height: 40px;
    object-fit: contain;
    margin-right: 12px;
}

.card .info {
    flex: 1;
}

.card .info h4 {
    margin: 0;
    font-size: 14px;
}

.card .info p {
    margin: 2px 0 0;
    font-size: 12px;
    color: #888;
}

.card .info .mod {
    font-size: 11px;
    color: #6c5ce7;
    font-style: italic;
    text-align: right;
}

.loading-indicator {
    text-align: center;
    padding: 8px;
    color: #888;
    font-size: 13px;
}

/* 标签栏 */
#tab-bar {
    display: flex;
    align-items: center;
    background: #3a3a3a;
    padding: 0 8px;
    height: 36px;
    border-bottom: 1px solid #555;
    flex-shrink: 0;
    gap: 4px;
}
#tabs-container {
    display: flex;
    flex: 1;
    overflow-x: auto;
    height: 100%;
    align-items: center;
    gap: 2px;
}
.tab-item {
    display: inline-flex;
    align-items: center;
    height: 28px;
    padding: 0 10px;
    background: #555;
    border-radius: 4px 4px 0 0;
    color: #ccc;
    font-size: 13px;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.2s;
    border: 1px solid transparent;
    border-bottom: none;
    user-select: none;
}
.tab-item:hover {
    background: #666;
}
.tab-item.active {
    background: #2d2d2d;
    color: #fff;
    border-color: #666;
}
.tab-item .tab-close {
    margin-left: 8px;
    font-size: 14px;
    color: #aaa;
    cursor: pointer;
    line-height: 1;
}
.tab-item .tab-close:hover {
    color: #ff6b6b;
}
#btn-new-tab {
    background: transparent;
    border: none;
    color: #aaa;
    font-size: 20px;
    cursor: pointer;
    padding: 0 10px;
    height: 28px;
    line-height: 28px;
}
#btn-new-tab:hover {
    color: #fff;
}
#tab-home {
    background: #ffd700 !important;
    color: #222 !important;
    border-color: #ffd700 !important;
    flex-shrink: 0;
}
#tab-home:hover {
    opacity: 0.85;
}

/* ============================================================
   安卓适配：响应式布局
   ============================================================ */

/* 触屏全局优化：禁止长按选择/菜单、禁止双指缩放 */
* { -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
#canvas-wrapper { touch-action: none; }
#canvas-wrapper #zoom-controls, #canvas-wrapper #settings-btn, #canvas-wrapper #canvas-zoom-controls { touch-action: auto; }
#right-panel { touch-action: pan-y; }
#code-output { touch-action: pan-y; }
#tab-bar { touch-action: pan-x; }

/* 小屏幕（手机竖屏）: 改为上下布局 */
@media (max-width: 768px) {
    body { overflow: auto; height: auto; min-height: 100vh; position: static; max-height: none; }
    #app-container { flex-direction: column; width: 100vw; }

    #top-bar {
        flex-wrap: wrap; height: auto !important; padding: 6px 10px; gap: 4px;
    }
    #top-bar > div:first-child { width: 100%; }
    #top-bar > div:last-child {
        flex-wrap: wrap; width: 100%; gap: 4px !important;
    }
    #top-bar button, #top-bar label {
        font-size: 11px !important; padding: 4px 8px !important;
    }
    #top-bar button {
        font-size: 10px !important; padding: 3px 6px !important;
    }
    #program-name { width: 80px !important; }

    #canvas-wrapper {
        flex: 0 0 55vh; width: 100% !important; min-height: 150px;
    }
    #splitter {
        flex: 0 0 28px; width: 100%;
        background: #666;
        cursor: pointer;
        touch-action: auto;
        position: relative;
        z-index: 10;
    }
    #splitter::after {
        content: '⋯'; font-size: 14px; line-height: 28px;
    }
    #splitter:active {
        background: #ffaa00;
    }

    #right-panel {
        flex: 1; width: 100% !important; min-height: 0;
    }
    #right-panel .tabs { flex-shrink: 0; }
    #right-panel .tab-content { min-height: 0; overflow-y: auto; }
    #right-panel .tab-btn { font-size: 12px; padding: 8px 0; }
    #code-output { font-size: 11px; padding: 8px; }
    .btn-group button { font-size: 11px; padding: 6px; }

    .cover-content { width: 95vw; max-height: 90vh; padding: 16px; }
    .cover-content .search input { font-size: 14px; }
    .card { padding: 6px 10px; }
    .card img { width: 32px; height: 32px; }
    .card .info h4 { font-size: 13px; }

    /* 安卓竖屏：节点缩小 50% */
    .node { width: 90px !important; min-width: 85px; }
    .node-header { height: 18px; line-height: 18px; font-size: 9px; padding: 0 6px; }
    .node-header .node-type-badge { font-size: 8px; padding: 0 4px; }
    .node-header .close-btn { width: 12px; height: 12px; font-size: 8px; }
    .node-body { padding: 3px 5px 5px; }
    .node-content .setting-item { font-size: 9px; margin-bottom: 3px; }
    .node-content .setting-item label { font-size: 8px; }
    .node-content .setting-item input,
    .node-content .setting-item select { font-size: 9px; padding: 1px 3px; }
    .node-content .setting-item button { font-size: 9px; padding: 2px 5px; }
    .port { width: 10px; height: 10px; }

    #tab-bar { height: 30px; }
    .tab-item { height: 24px; font-size: 12px; padding: 0 8px; }

    .context-menu { min-width: 150px; }
    .context-menu .menu-item { font-size: 12px; padding: 5px 14px; }
    .context-menu .sub-menu { min-width: 130px; }

    #clipboard-modal > div { width: 95vw; padding: 12px; }
    #clipboard-textarea { min-height: 120px; font-size: 11px; }
}

/* 超小屏（< 400px）进一步压缩 */
@media (max-width: 400px) {
    #top-bar button, #top-bar label { font-size: 10px !important; padding: 3px 6px !important; }
    #program-name { width: 60px !important; font-size: 10px !important; }
    #canvas-wrapper { flex: 0 0 45vh; }
    .node { min-width: 120px; }
    .node-header { height: 22px; line-height: 22px; font-size: 10px; }
    .node-body { padding: 4px 6px 6px; }
    .setting-item { font-size: 10px; }
    .setting-item input, .setting-item select { font-size: 10px; }
    .port { width: 10px; height: 10px; }
}