  :root {
    --bg-dark: #000000;
    --bg-canvas: #0a0a0e;
    --surface: rgba(28, 28, 30, 0.7);
    --surface-solid: #1c1c1e;
    --border: rgba(255, 255, 255, 0.15);
    --text-main: #ffffff;
    --text-sec: #ebebf599;
    --blue: #0A84FF;
    --indigo: #5E5CE6;
    --green: #30D158;
    --red: #FF453A;
    --orange: #FF9F0A;
    --purple: #BF5AF2;
    --teal: #5AC8FA;
    --font-ui: -apple-system, BlinkMacSystemFont, "SF Pro Display", sans-serif;
    --font-mono: 'JetBrains Mono', monospace;
  }
  * { box-sizing: border-box; margin: 0; padding: 0; outline: none; -webkit-tap-highlight-color: transparent; }
  body {
    background-color: var(--bg-dark); color: var(--text-main);
    font-family: var(--font-ui); height: 100dvh; overflow: hidden;
    display: flex; flex-direction: column; user-select: none; overscroll-behavior: none;
  }
  .top-nav {
    height: 60px; padding: 0 20px; padding-top: env(safe-area-inset-top);
    background: var(--surface); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between; z-index: 1000; flex-shrink: 0;
  }
  .nav-title { font-size: 18px; font-weight: 700; background: linear-gradient(90deg, var(--blue), var(--indigo)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
  .nav-actions { display: flex; gap: 10px; }
  .btn-icon {
    width: 36px; height: 36px; border-radius: 10px; background: rgba(255,255,255,0.1);
    display: flex; align-items: center; justify-content: center; color: var(--text-main);
    border: none; cursor: pointer; transition: 0.2s; flex-shrink: 0;
  }
  .btn-icon:active { background: var(--blue); transform: scale(0.95); }
  .canvas-wrapper {
    flex: 1; position: relative; overflow: hidden;
    background: radial-gradient(circle at 50% 50%, #1a1a24 0%, var(--bg-canvas) 100%);
    touch-action: none;
  }
  .canvas-wrapper::before {
    content:''; position:absolute; inset:0;
    background-image: radial-gradient(circle, rgba(255,255,255,0.05) 1px, transparent 1px);
    background-size: 30px 30px; pointer-events: none;
  }
  #workspace { position: absolute; transform-origin: 0 0; will-change: transform; }
  .canvas-board {
    position: relative; background: var(--surface-solid);
    box-shadow: 0 20px 60px rgba(0,0,0,0.8), 0 0 0 1px var(--border);
    border-radius: 4px;
  }
  #bg-image {
    display: block; pointer-events: none; opacity: 0; transition: opacity 0.3s;
    width: 100%; height: 100%; border-radius: 4px;
  }
  #bg-image.loaded { opacity: 1; }
  .empty-bg-text {
    position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
    color: var(--text-sec); font-size: 14px; font-weight: 600; text-align: center; padding: 20px;
  }

  /* NODES */
  .node {
    position: absolute; z-index: 10; touch-action: none;
    transform-origin: center center; will-change: transform, opacity;
  }
  .node.selected { z-index: 50; }
  .node-clip-wrapper {
    width: 100%; height: 100%; overflow: hidden; position: relative;
  }
  .spider-svg { width: 100%; height: 100%; overflow: visible; pointer-events: none; }
  .spider-line { stroke: rgba(10, 132, 255, 0.5); stroke-width: 2; }
  .spider-circle { fill: var(--bg-dark); stroke: rgba(10, 132, 255, 0.8); stroke-width: 2; }
  .spider-text { fill: #fff; font-family: var(--font-mono); font-size: 24px; font-weight: bold; text-anchor: middle; dominant-baseline: central; }
  .spider-border { fill: rgba(10, 132, 255, 0.05); stroke: var(--blue); stroke-width: 1; stroke-dasharray: 6 6; }
  .node.selected .spider-line { stroke: var(--indigo); stroke-width: 3; opacity: 0.9; }
  .node.selected .spider-circle { stroke: var(--indigo); stroke-width: 4; fill: rgba(94, 92, 230, 0.3); }
  .node.selected .spider-border { fill: rgba(94, 92, 230, 0.1); stroke: var(--indigo); stroke-dasharray: none; stroke-width: 2; }
  .node-image { width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
  .node.selected .node-clip-wrapper { outline: 3px solid var(--indigo); box-shadow: 0 0 20px rgba(94, 92, 230, 0.4); }
  .node-hitbox { position: absolute; inset: 0; cursor: move; touch-action: none; z-index: 5; }
  .node-hitbox::after { content: ''; position: absolute; top: -15px; left: -15px; right: -15px; bottom: -15px; }

  /* FLOATING PANEL */
  .floating-panel {
    position: absolute; width: 320px; height: 520px;
    background: rgba(20, 20, 25, 0.92); backdrop-filter: blur(25px); -webkit-backdrop-filter: blur(25px);
    border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 24px;
    box-shadow: 0 25px 50px rgba(0,0,0,0.6), 0 0 0 1px rgba(0,0,0,0.5);
    display: none; flex-direction: column; overflow: hidden;
    z-index: 1000; touch-action: none;
    left: 20px; top: 20px;
  }
  .floating-panel.show { display: flex; animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275); }
  @keyframes popIn { from { opacity: 0; transform: scale(0.9); } to { opacity: 1; transform: scale(1); } }
  .panel-header {
    background: rgba(255,255,255,0.05); padding: 12px 16px;
    display: flex; justify-content: space-between; align-items: center;
    border-bottom: 1px solid rgba(255,255,255,0.1); cursor: move; touch-action: none; flex-shrink: 0;
  }
  .panel-title { font-size: 14px; font-weight: 700; font-family: var(--font-mono); color: var(--blue); pointer-events: none; }
  .panel-close {
    background: rgba(255,69,58,0.2); color: var(--red); border: none;
    width: 24px; height: 24px; border-radius: 12px; font-size: 12px; display: flex; align-items: center; justify-content: center;
  }
  .panel-body {
    padding: 16px; display: flex; flex-direction: column; gap: 14px;
    flex: 1; overflow-y: auto; overscroll-behavior: contain; padding-bottom: 36px;
  }
  .panel-resize-handle {
    position: absolute; bottom: 0; right: 0;
    width: 30px; height: 30px; cursor: nwse-resize;
    display: flex; align-items: flex-end; justify-content: flex-end; padding: 6px;
    color: rgba(255,255,255,0.4); touch-action: none; z-index: 10;
  }
  .panel-resize-handle svg { width: 14px; height: 14px; }

  /* SIDE PANELS (shape + bgremover) */
  .side-panel {
    position: absolute; width: 280px;
    background: rgba(15, 15, 22, 0.95); backdrop-filter: blur(25px); -webkit-backdrop-filter: blur(25px);
    border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 20px;
    box-shadow: 0 20px 50px rgba(0,0,0,0.7);
    display: none; flex-direction: column; overflow: hidden;
    z-index: 1100; touch-action: none;
  }
  .side-panel.show { display: flex; animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275); }
  .side-panel-header {
    background: rgba(255,255,255,0.05); padding: 10px 14px;
    display: flex; justify-content: space-between; align-items: center;
    border-bottom: 1px solid rgba(255,255,255,0.1); cursor: move; touch-action: none; flex-shrink: 0;
  }
  .side-panel-title { font-size: 13px; font-weight: 700; font-family: var(--font-mono); pointer-events: none; }
  .side-panel-body { padding: 14px; display: flex; flex-direction: column; gap: 12px; overflow-y: auto; max-height: 520px; overscroll-behavior: contain; padding-bottom: 20px; }

  /* SVG rope */
  #rope-svg {
    position: absolute; pointer-events: none; z-index: 1050; overflow: visible;
    top: 0; left: 0; width: 100%; height: 100%;
  }
  #rope-path { fill: none; stroke: rgba(10, 132, 255, 0.5); stroke-width: 2; stroke-dasharray: 6 4; }
  #rope-path-bgr { fill: none; stroke: rgba(191, 90, 242, 0.5); stroke-width: 2; stroke-dasharray: 6 4; }

  /* Shape grid */
  .shape-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
  .shape-btn {
    aspect-ratio: 1; background: rgba(255,255,255,0.06); border: 1.5px solid rgba(255,255,255,0.1);
    border-radius: 12px; display: flex; align-items: center; justify-content: center; cursor: pointer;
    transition: 0.15s; padding: 6px; touch-action: manipulation;
  }
  .shape-btn svg { width: 100%; height: 100%; pointer-events: none; }
  .shape-btn:active, .shape-btn.active { background: rgba(10,132,255,0.2); border-color: var(--blue); }
  .shape-btn.active { border-color: var(--blue); box-shadow: 0 0 8px rgba(10,132,255,0.4); }
  .shape-params { display: flex; flex-direction: column; gap: 8px; }
  .shape-param-title { font-size: 11px; color: var(--text-sec); font-weight: 600; font-family: var(--font-mono); }

  /* BG Remover */
  .bgr-preview {
    width: 100%; aspect-ratio: 1; background: repeating-conic-gradient(#222 0% 25%, #111 0% 50%) 0 0/16px 16px;
    border-radius: 12px; overflow: hidden; position: relative; border: 1px solid var(--border);
  }
  .bgr-preview canvas { width: 100%; height: 100%; object-fit: contain; }
  .bgr-status {
    position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
    background: rgba(0,0,0,0.6); font-size: 12px; color: var(--text-sec); font-family: var(--font-mono);
    text-align: center; padding: 10px;
  }
  .toggle-row {
    display: flex; align-items: center; justify-content: space-between;
    background: rgba(0,0,0,0.3); padding: 10px 12px; border-radius: 12px;
    border: 1px solid rgba(255,255,255,0.05);
  }
  .toggle-label { font-size: 12px; color: var(--text-main); font-weight: 600; }
  .toggle-sub { font-size: 10px; color: var(--text-sec); margin-top: 2px; }
  .ios-toggle {
    width: 44px; height: 26px; background: rgba(255,255,255,0.15); border-radius: 13px;
    position: relative; transition: 0.3s; flex-shrink: 0; border: none;
  }
  .ios-toggle.on { background: var(--green); }
  .ios-toggle::after {
    content: ''; position: absolute; top: 3px; left: 3px;
    width: 20px; height: 20px; background: #fff; border-radius: 50%;
    transition: 0.3s; box-shadow: 0 2px 6px rgba(0,0,0,0.4);
  }
  .ios-toggle.on::after { left: 21px; }
  .btn-apply {
    width: 100%; padding: 11px; border-radius: 12px; font-size: 13px; font-weight: 700;
    border: none; display: flex; align-items: center; justify-content: center; gap: 8px;
  }
  .bgr-params { display: flex; flex-direction: column; gap: 8px; }

  /* Movepad */
  .movepad-container { display: flex; flex-direction: column; align-items: center; gap: 12px; flex-shrink: 0; }
  .pad-mode-switch {
    display: flex; background: rgba(0,0,0,0.4); border-radius: 12px; padding: 4px; gap: 4px; width: 210px;
  }
  .pad-mode-btn {
    flex: 1; background: transparent; border: none; color: var(--text-sec);
    font-family: var(--font-ui); font-size: 11px; font-weight: 600; padding: 8px 0;
    border-radius: 9px; transition: 0.2s;
  }
  .pad-mode-btn.active { background: var(--blue); color: #fff; }
  .movepad {
    width: 180px; height: 180px; background: rgba(0,0,0,0.6);
    border: 2px dashed rgba(255,255,255,0.2); border-radius: 24px;
    display: flex; align-items: center; justify-content: center;
    position: relative; touch-action: none; overflow: hidden;
  }
  .movepad.active { border-color: var(--blue); background: rgba(10, 132, 255, 0.15); }
  .movepad-hint { color: var(--text-sec); font-size: 13px; font-weight: 600; text-align: center; pointer-events: none; }
  .movepad-dot {
    position: absolute; left: 0; top: 0; width: 44px; height: 44px; background: rgba(10, 132, 255, 0.85);
    border-radius: 50%; opacity: 0; pointer-events: none; box-shadow: 0 0 10px var(--blue);
    will-change: transform; border: 2px solid rgba(255,255,255,0.55);
  }
  .dpad {
    width: 180px; height: 180px; display: grid;
    grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; gap: 6px;
  }
  .dpad-btn {
    background: rgba(255,255,255,0.08); border: 1px solid rgba(255,255,255,0.15);
    border-radius: 14px; color: #fff; font-size: 20px; display: flex;
    align-items: center; justify-content: center; touch-action: none; transition: 0.1s;
  }
  .dpad-btn.pressed { background: var(--blue); transform: scale(0.92); }
  .dpad-center {
    display: flex; align-items: center; justify-content: center;
    background: rgba(255,255,255,0.03); border-radius: 14px; color: var(--text-sec); font-size: 10px;
  }
  .direct-msg {
    width: 180px; min-height: 180px; display: flex; align-items: center; justify-content: center;
    text-align: center; color: var(--text-sec); font-size: 13px; font-weight: 600;
    background: rgba(0,0,0,0.3); border: 2px dashed rgba(255,255,255,0.15); border-radius: 24px; padding: 16px;
  }

  /* Controls */
  .control-row {
    display: flex; flex-direction: column; gap: 8px; background: rgba(0,0,0,0.3);
    padding: 12px; border-radius: 16px; border: 1px solid rgba(255,255,255,0.05); flex-shrink: 0;
  }
  .control-header { display: flex; justify-content: space-between; align-items: center; }
  .control-label { font-size: 12px; color: var(--text-sec); font-weight: 600; }
  .control-input {
    background: rgba(255,255,255,0.1); border: 1px solid rgba(255,255,255,0.1);
    color: #fff; font-family: var(--font-mono); font-size: 14px; text-align: center;
    width: 60px; border-radius: 8px; padding: 4px;
  }
  .control-input:focus { border-color: var(--blue); }
  .slider-wrapper { display: flex; align-items: center; gap: 10px; width: 100%; }
  input[type=range] {
    -webkit-appearance: none; flex: 1; height: 6px; background: rgba(255,255,255,0.1);
    border-radius: 3px; outline: none;
  }
  input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none; width: 24px; height: 24px; border-radius: 50%;
    background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,0.5); border: 2px solid var(--blue);
  }
  .url-input {
    width: 100%; background: rgba(0,0,0,0.4); border: 1px solid rgba(255,255,255,0.1);
    color: #fff; padding: 10px; border-radius: 12px; font-size: 12px; font-family: var(--font-mono); direction: ltr;
  }
  .btn-danger {
    width: 100%; background: rgba(255,69,58,0.15); color: var(--red); border: none; flex-shrink: 0;
    padding: 12px; border-radius: 12px; font-size: 14px; font-weight: 600;
  }
  .btn-clone {
    width: 100%; background: rgba(10,132,255,0.15); color: var(--blue); border: none; flex-shrink: 0;
    padding: 12px; border-radius: 12px; font-size: 14px; font-weight: 600;
  }
  .btn-shape-open {
    width: 100%; background: rgba(255,159,10,0.15); color: var(--orange); border: 1px solid rgba(255,159,10,0.3);
    padding: 11px; border-radius: 12px; font-size: 13px; font-weight: 600; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center; gap: 8px;
  }
  .btn-shape-open.active { background: rgba(255,159,10,0.25); border-color: var(--orange); }
  .btn-bgr-open {
    width: 100%; background: rgba(191,90,242,0.15); color: var(--purple); border: 1px solid rgba(191,90,242,0.3);
    padding: 11px; border-radius: 12px; font-size: 13px; font-weight: 600; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center; gap: 8px;
  }
  .btn-bgr-open.active { background: rgba(191,90,242,0.25); border-color: var(--purple); }

  /* HUD & Modals */
  .hud-info {
    position: absolute; top: 15px; left: 50%; transform: translateX(-50%);
    background: rgba(0,0,0,0.6); padding: 6px 16px; border-radius: 20px;
    font-size: 12px; font-family: var(--font-mono); backdrop-filter: blur(10px);
    border: 1px solid rgba(255,255,255,0.1); z-index: 500; pointer-events: none;
  }
  .full-modal {
    position: fixed; inset: 0; background: rgba(0,0,0,0.8); backdrop-filter: blur(15px);
    z-index: 2000; display: flex; flex-direction: column;
    transform: translateY(100%); transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  }
  .full-modal.open { transform: translateY(0); }
  .modal-content { padding: 20px; overflow-y: auto; flex: 1; }
  .code-box {
    background: #000; padding: 16px; border-radius: 16px; border: 1px solid var(--border);
    font-family: var(--font-mono); font-size: 11px; line-height: 1.6; color: #a9b7d1;
    white-space: pre-wrap; overflow-x: auto; text-align: left; direction: ltr;
  }
  .toast {
    position: fixed; top: 80px; left: 50%; transform: translate(-50%, -20px);
    background: rgba(48, 209, 88, 0.2); border: 1px solid var(--green); color: var(--green);
    padding: 12px 24px; border-radius: 20px; font-weight: 600; font-size: 14px;
    backdrop-filter: blur(10px); opacity: 0; pointer-events: none; transition: 0.3s; z-index: 3000;
  }
  .toast.show { opacity: 1; transform: translate(-50%, 0); }

  /* AI Import section in BG modal */
  .section-divider {
    display: flex; align-items: center; gap: 10px; margin: 6px 0;
  }
  .section-divider::before, .section-divider::after {
    content: ''; flex: 1; height: 1px; background: rgba(255,255,255,0.1);
  }
  .section-divider span { font-size: 11px; color: var(--text-sec); font-weight: 600; white-space: nowrap; }
  .ai-import-box {
    background: rgba(94,92,230,0.08); border: 1px solid rgba(94,92,230,0.3);
    border-radius: 16px; padding: 16px; display: flex; flex-direction: column; gap: 12px;
  }
  .ai-import-header { font-size: 13px; font-weight: 700; color: var(--indigo); display: flex; align-items: center; gap: 8px; }
  .ai-textarea {
    width: 100%; background: rgba(0,0,0,0.5); border: 1px solid rgba(94,92,230,0.3);
    color: #fff; padding: 12px; border-radius: 12px; font-size: 12px;
    font-family: var(--font-mono); resize: none; min-height: 120px; direction: ltr;
  }
  .ai-textarea:focus { border-color: var(--indigo); }
  .ai-status {
    font-size: 11px; color: var(--text-sec); font-family: var(--font-mono);
    background: rgba(0,0,0,0.3); padding: 8px 12px; border-radius: 8px;
    min-height: 36px; display: none;
  }
  .ai-status.show { display: block; }
  .ai-loading { display: inline-block; animation: spin 1s linear infinite; }
  @keyframes spin { from{transform:rotate(0deg)} to{transform:rotate(360deg)} }
  .progress-bar {
    width: 100%; height: 4px; background: rgba(255,255,255,0.1); border-radius: 2px; overflow: hidden;
  }
  .progress-fill {
    height: 100%; background: linear-gradient(90deg, var(--purple), var(--blue));
    border-radius: 2px; transition: width 0.3s; width: 0%;
  }
  .elements-manager {
    background: rgba(0,0,0,0.35); border: 1px solid rgba(255,255,255,0.1);
    border-radius: 16px; padding: 14px; display: flex; flex-direction: column; gap: 10px;
  }
  .elements-manager-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
  .elements-manager-title { font-size: 13px; font-weight: 700; color: var(--text-main); }
  .elements-manager-actions { display: flex; gap: 8px; flex-shrink: 0; }
  .mini-btn {
    border: 1px solid rgba(255,255,255,0.12); background: rgba(255,255,255,0.08); color: #fff;
    padding: 8px 10px; border-radius: 10px; font-size: 12px; font-weight: 700;
  }
  .mini-btn.danger { color: var(--red); background: rgba(255,69,58,0.12); border-color: rgba(255,69,58,0.28); }
  .elements-list { display: flex; flex-direction: column; gap: 8px; max-height: 260px; overflow-y: auto; }
  .element-row {
    display: grid; grid-template-columns: 26px 42px 1fr 34px; gap: 10px; align-items: center;
    background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.08); border-radius: 12px; padding: 8px;
  }
  .element-row.active { border-color: var(--blue); background: rgba(10,132,255,0.12); }
  .element-check { width: 20px; height: 20px; accent-color: var(--blue); }
  .element-thumb {
    width: 42px; height: 42px; border-radius: 8px; background: rgba(255,255,255,0.08);
    border: 1px solid rgba(255,255,255,0.1); overflow: hidden; display: flex; align-items: center; justify-content: center;
    color: var(--text-sec); font-size: 11px; font-family: var(--font-mono);
  }
  .element-thumb img { width: 100%; height: 100%; object-fit: cover; }
  .element-meta { min-width: 0; display: flex; flex-direction: column; gap: 3px; }
  .element-name { font-size: 12px; font-weight: 700; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .element-sub { font-size: 10px; color: var(--text-sec); font-family: var(--font-mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .element-del {
    width: 34px; height: 34px; border-radius: 10px; border: 1px solid rgba(255,69,58,0.28);
    color: var(--red); background: rgba(255,69,58,0.12); display: flex; align-items: center; justify-content: center; font-size: 16px;
  }
  .elements-empty { color: var(--text-sec); text-align: center; font-size: 12px; padding: 12px; }

  .sessions-list { display: flex; flex-direction: column; gap: 10px; touch-action: manipulation; }
  .session-new-btn {
    width: 100%; border: 1px solid rgba(48,209,88,0.35); background: rgba(48,209,88,0.14); color: var(--green);
    padding: 14px; border-radius: 12px; font-size: 14px; font-weight: 800; appearance: none; -webkit-appearance: none;
  }
  .session-row {
    display: grid; grid-template-columns: 1fr 42px; gap: 10px; align-items: center;
    background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.1); border-radius: 12px; padding: 12px; cursor: pointer;
  }
  .session-row.active { border-color: var(--orange); background: rgba(255,159,10,0.12); }
  .session-main {
    min-width: 0; display: flex; flex-direction: column; gap: 4px; text-align: right; width: 100%;
    border: 0; background: transparent; color: inherit; padding: 0; appearance: none; -webkit-appearance: none; font: inherit; cursor: pointer;
  }
  .session-title { color: #fff; font-size: 13px; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .session-sub { color: var(--text-sec); font-size: 11px; font-family: var(--font-mono); }
  .session-delete {
    width: 38px; height: 38px; border-radius: 10px; border: 1px solid rgba(255,69,58,0.3);
    color: var(--red); background: rgba(255,69,58,0.12); font-size: 17px; appearance: none; -webkit-appearance: none; cursor: pointer;
  }
